Skip to content

Commit 4868e29

Browse files
Merge commit '4b172b60ee5ac3667affca647bd13a0cba71ccc2' into named-side-effects
2 parents 0c5917b + 4b172b6 commit 4868e29

File tree

2 files changed

+104
-17
lines changed

2 files changed

+104
-17
lines changed

sdk-core/src/main/service-protocol/dev/restate/service/protocol.proto

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ message ErrorMessage {
7575
string message = 2;
7676
// Contains a verbose error description, e.g. the exception stacktrace.
7777
string description = 3;
78+
79+
// Entry that caused the failure. This may be outside the current stored journal size.
80+
// If no specific entry caused the failure, the current replayed/processed entry can be used.
81+
uint32 related_entry_index = 4;
82+
// Name of the entry that caused the failure. Empty if no name was set.
83+
string related_entry_name = 5;
84+
// Entry type. 0 if unknown.
85+
uint32 related_entry_type = 6;
7886
}
7987

8088
// Type: 0x0000 + 4
@@ -96,7 +104,7 @@ message EndMessage {
96104
// * bytes value = 14 for carrying the result value
97105
// * Failure failure = 15 for carrying a failure
98106
//
99-
// The tag numbers 13, 14 and 15 are reserved and shouldn't be used for other fields.
107+
// The tag numbers 12, 13, 14 and 15 are reserved and shouldn't be used for other fields.
100108

101109
// ------ Input and output ------
102110

@@ -107,6 +115,9 @@ message InputEntryMessage {
107115
repeated Header headers = 1;
108116

109117
bytes value = 14;
118+
119+
// Entry name
120+
string name = 12;
110121
}
111122

112123
// Completable: No
@@ -117,6 +128,9 @@ message OutputEntryMessage {
117128
bytes value = 14;
118129
Failure failure = 15;
119130
};
131+
132+
// Entry name
133+
string name = 12;
120134
}
121135

122136
// ------ State access ------
@@ -132,6 +146,9 @@ message GetStateEntryMessage {
132146
bytes value = 14;
133147
Failure failure = 15;
134148
};
149+
150+
// Entry name
151+
string name = 12;
135152
}
136153

137154
// Completable: No
@@ -140,19 +157,27 @@ message GetStateEntryMessage {
140157
message SetStateEntryMessage {
141158
bytes key = 1;
142159
bytes value = 3;
160+
161+
// Entry name
162+
string name = 12;
143163
}
144164

145165
// Completable: No
146166
// Fallible: No
147167
// Type: 0x0800 + 2
148168
message ClearStateEntryMessage {
149169
bytes key = 1;
170+
171+
// Entry name
172+
string name = 12;
150173
}
151174

152175
// Completable: No
153176
// Fallible: No
154177
// Type: 0x0800 + 3
155178
message ClearAllStateEntryMessage {
179+
// Entry name
180+
string name = 12;
156181
}
157182

158183
// Completable: Yes
@@ -167,6 +192,9 @@ message GetStateKeysEntryMessage {
167192
StateKeys value = 14;
168193
Failure failure = 15;
169194
};
195+
196+
// Entry name
197+
string name = 12;
170198
}
171199

172200
// ------ Syscalls ------
@@ -183,6 +211,9 @@ message SleepEntryMessage {
183211
Empty empty = 13;
184212
Failure failure = 15;
185213
}
214+
215+
// Entry name
216+
string name = 12;
186217
}
187218

188219
// Completable: Yes
@@ -203,6 +234,9 @@ message InvokeEntryMessage {
203234
bytes value = 14;
204235
Failure failure = 15;
205236
};
237+
238+
// Entry name
239+
string name = 12;
206240
}
207241

208242
// Completable: No
@@ -224,6 +258,9 @@ message BackgroundInvokeEntryMessage {
224258

225259
// If this invocation has a key associated (e.g. for objects and workflows), then this key is filled in. Empty otherwise.
226260
string key = 6;
261+
262+
// Entry name
263+
string name = 12;
227264
}
228265

229266
// Completable: Yes
@@ -235,6 +272,9 @@ message AwakeableEntryMessage {
235272
bytes value = 14;
236273
Failure failure = 15;
237274
};
275+
276+
// Entry name
277+
string name = 12;
238278
}
239279

240280
// Completable: No
@@ -248,6 +288,9 @@ message CompleteAwakeableEntryMessage {
248288
bytes value = 5;
249289
Failure failure = 6;
250290
};
291+
292+
// Entry name
293+
string name = 12;
251294
}
252295

253296
// --- Nested messages

sdk-core/src/main/service-protocol/service-invocation-protocol.md

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The system is composed of two actors:
1111
- SDK, which contains the implementation of the Restate Protocol
1212
- User business logic, which interacts with the SDK to access Restate system calls (or syscalls)
1313

14-
Each service method invocation is modeled by the protocol as a state machine, where state transitions can be caused
14+
Each invocation is modeled by the protocol as a state machine, where state transitions can be caused
1515
either by user code or by _Runtime events_.
1616

1717
Every state transition is logged in the _Invocation journal_, used to implement Restate's durable execution model. The
@@ -32,11 +32,12 @@ The state machine is summarized in the following diagram:
3232
```mermaid
3333
sequenceDiagram
3434
Note over Runtime,SDK: Start
35-
Runtime->>SDK: HTTP Request to /invoke/{service}/{method}
35+
Runtime->>SDK: HTTP Request to /invoke/{service}/{handler}
3636
Runtime->>SDK: StartMessage
3737
Note over Runtime,SDK: Replaying
3838
Runtime->>SDK: [...]EntryMessage(s)
3939
Note over Runtime,SDK: Processing
40+
SDK->>Runtime: HTTP Response headers
4041
loop
4142
SDK->>Runtime: [...]EntryMessage
4243
Runtime->>SDK: CompletionMessage and/or EntryAckMessage
@@ -102,9 +103,7 @@ protocol mandates the following messages:
102103

103104
### Message stream
104105

105-
In order to execute a service method invocation, service deployment and restate Runtime open a single stream between the
106-
runtime and the service deployment. Given 10 concurrent service method invocations to a service deployment, there are 10
107-
concurrent streams, each of them mapping to a specific invocation.
106+
In order to execute an invocation, service deployment and restate Runtime open a single stream between the runtime and the service deployment. Given 10 concurrent invocations to a service deployment, there are 10 concurrent streams, each of them mapping to a specific invocation.
108107

109108
Every unit of the stream contains a Message serialized using the
110109
[Protobuf encoding](https://protobuf.dev/programming-guides/encoding/), using the definitions in
@@ -119,10 +118,23 @@ in two modes:
119118
runtime. Once the service deployment starts sending messages to the runtime, the runtime cannot send messages anymore
120119
back to the service deployment.
121120

122-
When opening the stream, the request method MUST be `POST` and the request path MUST have the following format:
121+
A message stream MUST start with `StartMessage` and MUST end with either:
122+
123+
- One [`SuspensionMessage`](#suspension)
124+
- One [`ErrorMessage`](#failures)
125+
- One `EndMessage`
126+
127+
If the message stream does not end with any of these two messages, it will be considered equivalent to sending an
128+
`ErrorMessage` with an [unknown failure](#failures).
129+
130+
The `EndMessage` marks the end of the invocation lifecycle, that is the end of the journal.
131+
132+
### Initiating the stream
133+
134+
When opening the stream, the HTTP request method MUST be `POST` and the request path MUST have the following format:
123135

124136
```
125-
/invoke/{fullyQualifiedServiceName}/{methodName}
137+
/invoke/{serviceName}/{handlerName}
126138
```
127139

128140
For example:
@@ -133,19 +145,29 @@ For example:
133145

134146
An arbitrary path MAY prepend the aforementioned path format.
135147

136-
In case the path format is not respected, or `fullyQualifiedServiceName` or `methodName` is unknown, the SDK MUST close
137-
the stream replying back with a `404` status code.
148+
In case the path format is not respected, or `serviceName` or `handlerName` is unknown, the SDK MUST close the stream replying back with a `404` status code.
138149

139-
A message stream MUST start with `StartMessage` and MUST end with either:
150+
In case the invocation is accepted, `200` status code MUST be returned.
140151

141-
- One [`SuspensionMessage`](#suspension)
142-
- One [`ErrorMessage`](#failures)
143-
- One `EndMessage`
152+
Additionally, the header `x-restate-user-agent` MAY be sent back, with the following format:
144153

145-
If the message stream does not end with any of these two messages, it will be considered equivalent to sending an
146-
`ErrorMessage` with an [unknown failure](#failures).
154+
```http request
155+
x-restate-user-agent: <sdk-name> / <sdk-version>; <additional-metadata?>
156+
```
147157

148-
The `EndMessage` marks the end of the invocation lifecycle, that is the end of the journal.
158+
E.g.:
159+
160+
```http request
161+
x-restate-user-agent: restate-sdk-java/0.8.0
162+
```
163+
164+
Or:
165+
166+
```http request
167+
x-restate-user-agent: restate-sdk-java/0.8.0; gitHash=0c5917b
168+
```
169+
170+
This header is used for observability purposes by the Restate observability tools.
149171

150172
### Message header
151173

@@ -275,6 +297,11 @@ index of the corresponding entry.
275297
| Length |
276298
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
277299

300+
#### Entry names
301+
302+
Every Journal entry has a field `string name = 12`, which can be set by the SDK when recording the entry. This field is
303+
used for observability purposes by Restate observability tools.
304+
278305
### Journal entries reference
279306

280307
The following tables describe the currently available journal entries. For more details, check the protobuf message
@@ -365,6 +392,11 @@ additional features to the users.
365392
The protocol allows the SDK to register an arbitrary entry type within the journal. The type MUST be `>= 0xFC00`. The
366393
runtime will treat this entry as any other entry, persisting it and sending it during replay in the correct order.
367394

395+
Custom entries MAY have the entry name field `12`, as described in [entry names](#entry-names).
396+
397+
The field numbers 13, 14 and 15 MUST not be used, as they're reserved for completable journal entries, as described in
398+
[completable journal entries](#completable-journal-entries-and-completionmessage).
399+
368400
**Header**
369401

370402
0 1 2 3
@@ -405,3 +437,15 @@ A possible implementation could be the following. Given a user requests a state
405437

406438
In order for the aforementioned algorithm to work, set, clear and clear all state operations must be reflected on the
407439
local `state_map` as well.
440+
441+
### Side effect/Run
442+
443+
The side effect/run feature allows users to execute arbitrary non-deterministic code within their service and record the
444+
result, such that on re-executions the stored value will be used instead of replaying the given code.
445+
446+
SDKs MAY implement the side effect feature by storing the result using a custom entry message, as described in
447+
[Custom entry](#custom-entry-messages). By convention, the SDKs SHOULD use the entry type `FC01` for side effects.
448+
449+
When storing side effects, SDKs MAY need to wait for the
450+
[acknowledgment of the stored entry](#acknowledgment-of-stored-entries) before continuing the execution of the
451+
invocation.

0 commit comments

Comments
 (0)