@restatedev/sessions 0.11.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +330 -178
- package/dist/addon.d.ts +1 -0
- package/dist/addon.d.ts.map +1 -1
- package/dist/addon.js.map +1 -1
- package/dist/calls.d.ts +28 -28
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +25 -25
- package/dist/calls.js.map +1 -1
- package/dist/database.d.ts +90 -166
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +137 -249
- package/dist/database.js.map +1 -1
- package/dist/driver.d.ts +129 -126
- package/dist/driver.d.ts.map +1 -1
- package/dist/driver.js +337 -276
- package/dist/driver.js.map +1 -1
- package/dist/endpoint.d.ts +47 -5
- package/dist/endpoint.d.ts.map +1 -1
- package/dist/endpoint.js +175 -60
- package/dist/endpoint.js.map +1 -1
- package/dist/handler.d.ts +20 -17
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js +161 -127
- package/dist/handler.js.map +1 -1
- package/dist/host.d.ts +29 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +2 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -26
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +11 -11
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +20 -16
- package/dist/internal.js.map +1 -1
- package/dist/link.d.ts +1 -2
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +4 -5
- package/dist/link.js.map +1 -1
- package/dist/log.d.ts +14 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +21 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +217 -142
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +195 -241
- package/dist/object.js.map +1 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +4 -6
- package/dist/platform.js.map +1 -1
- package/dist/schema.d.ts +14 -22
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +31 -69
- package/dist/schema.js.map +1 -1
- package/dist/serve.d.ts +11 -17
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +21 -39
- package/dist/serve.js.map +1 -1
- package/dist/session.d.ts +58 -240
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +206 -532
- package/dist/session.js.map +1 -1
- package/dist/stop.d.ts +59 -0
- package/dist/stop.d.ts.map +1 -0
- package/dist/stop.js +220 -0
- package/dist/stop.js.map +1 -0
- package/dist/vm.d.ts +108 -29
- package/dist/vm.d.ts.map +1 -1
- package/dist/vm.js +43 -10
- package/dist/vm.js.map +1 -1
- package/package.json +4 -4
- package/src/addon.ts +1 -0
- package/src/calls.ts +51 -47
- package/src/database.ts +195 -339
- package/src/driver.ts +379 -315
- package/src/endpoint.ts +217 -62
- package/src/handler.ts +183 -164
- package/src/host.ts +28 -0
- package/src/index.ts +6 -26
- package/src/internal.ts +28 -39
- package/src/link.ts +4 -5
- package/src/log.ts +42 -0
- package/src/object.ts +379 -335
- package/src/platform.ts +4 -6
- package/src/schema.ts +37 -103
- package/src/serve.ts +20 -41
- package/src/session.ts +236 -629
- package/src/stop.ts +225 -0
- package/src/vm.ts +151 -55
- package/dist/scope.d.ts +0 -38
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js +0 -55
- package/dist/scope.js.map +0 -1
- package/src/scope.ts +0 -78
package/README.md
CHANGED
|
@@ -7,21 +7,28 @@ state, so there is no database server to run, and you still get what
|
|
|
7
7
|
Restate gives any virtual object: one writer per key, retries, and durable
|
|
8
8
|
calls to other services.
|
|
9
9
|
|
|
10
|
+
The documentation, from getting started to operations, starts at [`docs/README.md`](../../docs/README.md).
|
|
11
|
+
|
|
10
12
|
Session handlers don't use Restate's journal replay. When an attempt fails,
|
|
11
|
-
Restate runs the handler again from the start (
|
|
12
|
-
|
|
13
|
-
database. The storage that makes this safe is a Rust addon in
|
|
13
|
+
Restate runs the handler again from the start (_explicit recovery_), and the
|
|
14
|
+
handler continues from whatever it has committed to its database. The storage that makes this safe is a Rust addon in
|
|
14
15
|
[`native/`](native/README.md), built on Turso's SQLite-compatible engine.
|
|
15
16
|
|
|
16
|
-
It runs
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
It runs beside the published SDK, version 1.18.2, which it pins
|
|
18
|
+
([why](#concurrent-awaits)).
|
|
19
|
+
|
|
20
|
+
A session object runs on Restate's **explicit recovery protocol**, version 1
|
|
21
|
+
(content type `application/vnd.restate.invocation.explicitrecovery.v1`), over
|
|
22
|
+
HTTP/2 only. It needs the runtime image
|
|
23
|
+
`ghcr.io/restatedev/restate:session-objects-protocol`, with the experimental
|
|
24
|
+
flag on (`RESTATE_EXPERIMENTAL_ENABLE_EXPLICIT_RECOVERY_PROTOCOL=true`). This
|
|
25
|
+
package serves discovery itself, as manifest v6: every session object's
|
|
26
|
+
handler is declared with `protocolFamily: EXPLICIT_RECOVERY`, beside the
|
|
27
|
+
protocol's range (`explicitRecoveryProtocol`); a Restate that takes no
|
|
28
|
+
manifest v6 is told it is too old. An invocation of a session object on
|
|
29
|
+
another protocol is answered 415. Journaled services and virtual objects are
|
|
30
|
+
unaffected: they are the SDK's, at any version it supports, and keep its
|
|
31
|
+
protocol range in the manifest.
|
|
25
32
|
|
|
26
33
|
```ts
|
|
27
34
|
import * as restate from '@restatedev/sessions';
|
|
@@ -42,6 +49,10 @@ const ledger = restate.sessionObject({
|
|
|
42
49
|
});
|
|
43
50
|
```
|
|
44
51
|
|
|
52
|
+
What a handler and a view do, rule by rule, what the tests show of it and
|
|
53
|
+
what it assumes of Restate is specified in
|
|
54
|
+
[`docs/semantics.md`](../../docs/semantics.md).
|
|
55
|
+
|
|
45
56
|
## Defining a session object
|
|
46
57
|
|
|
47
58
|
`@restatedev/sessions` re-exports `@restatedev/restate-sdk`, so session objects
|
|
@@ -55,21 +66,30 @@ sit next to your regular services, virtual objects and workflows.
|
|
|
55
66
|
|
|
56
67
|
A name is either a handler or a view, not both. Each one is a function, or
|
|
57
68
|
`{ handler, ...options }` with the SDK's usual handler options (`input`,
|
|
58
|
-
`output`, `
|
|
59
|
-
`
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
`output`, `serde`, `asTerminalError`, `explicitCancellation`, `hooks`,
|
|
70
|
+
`description`, `ingressPrivate`, …). You only annotate the input; `ctx` is
|
|
71
|
+
inferred. `description`, `options` and `metadata` work as on any virtual
|
|
72
|
+
object, and each option resolves as the SDK resolves it: the handler's own,
|
|
73
|
+
else its object's `options`, else the endpoint's default service options;
|
|
74
|
+
`hooks` add up, the object's (else the endpoint's default) before the
|
|
75
|
+
handler's. A hook's `handler` interceptor wraps each attempt; a session
|
|
76
|
+
object's context has no `ctx.run`, so a `run` interceptor is never called. Three options are
|
|
77
|
+
refused: a session object starts an invocation with only its database's head
|
|
78
|
+
preloaded, and hides the database's state operations in Restate's UI, so it
|
|
79
|
+
sets `state` and `enableLazyState` itself; and its attempts are never
|
|
80
|
+
replayed, so `onJournalMismatchErrors` has nothing to act on. Any of them in
|
|
81
|
+
a handler's options or in `options` throws an `Error` when the object is
|
|
82
|
+
defined, and in the endpoint's default service options when the endpoint
|
|
83
|
+
serves it.
|
|
63
84
|
|
|
64
85
|
SDK handlers (`restate.handlers.*`) aren't accepted. Journaled work belongs
|
|
65
86
|
in a regular `restate.object`, `restate.service` or `restate.workflow`, which
|
|
66
87
|
your session object can call.
|
|
67
88
|
|
|
68
89
|
`restate.serve(...services)` serves them, session objects and the SDK's
|
|
69
|
-
alike, on `PORT` (9080 by default)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
connects out rather than being called in:
|
|
90
|
+
alike, on `PORT` (9080 by default).
|
|
91
|
+
`restate.connect` does the same where the program connects out to Restate
|
|
92
|
+
rather than being called in:
|
|
73
93
|
|
|
74
94
|
```ts
|
|
75
95
|
restate.connect({ services: [notes, orders], name: 'notes-v1' });
|
|
@@ -77,9 +97,8 @@ restate.connect({ services: [notes, orders], name: 'notes-v1' });
|
|
|
77
97
|
|
|
78
98
|
`name` is the deployment you register (`connect://notes-v1`), and the key
|
|
79
99
|
is `RESTATE_CONNECT_KEY`. It returns at once, with the SDK's `Connection`;
|
|
80
|
-
`await restate.connect(…).ready` waits for the first handshake. Use this one, not the SDK's own `connect`:
|
|
81
|
-
|
|
82
|
-
them, so an agent would run without its companion.
|
|
100
|
+
`await restate.connect(…).ready` waits for the first handshake. Use this one, not the SDK's own `connect`: it
|
|
101
|
+
refuses a remote handle and honours `RESTATE_SERVE_ONLY`, as `serve` does.
|
|
83
102
|
|
|
84
103
|
`restate.remote` is a handle on something another program serves: named and
|
|
85
104
|
typed here, implemented there. Type it with the definition it names,
|
|
@@ -97,13 +116,12 @@ Wrong input, an unknown handler and a misspelt name are each a type error.
|
|
|
97
116
|
`serve` refuses a handle, since a service with no handlers would register
|
|
98
117
|
over the real one.
|
|
99
118
|
|
|
100
|
-
`RESTATE_SERVE_ONLY=a,b` serves only the services it names
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
([not supported](#not-supported)).
|
|
119
|
+
`RESTATE_SERVE_ONLY=a,b` serves only the services it names: one program,
|
|
120
|
+
deployed in parts. For anything else (another port, an HTTP/1.1 server), the
|
|
121
|
+
SDK's `restate.endpoint()` is there as ever: bind it what
|
|
122
|
+
`restate.served([...services])` lists. A session object needs a
|
|
123
|
+
bidirectional stream, so an HTTP/1.1 handler serves one only when built with
|
|
124
|
+
`{ bidirectional: true }` ([not supported](#not-supported)).
|
|
107
125
|
|
|
108
126
|
### The contexts
|
|
109
127
|
|
|
@@ -113,17 +131,20 @@ attempt:
|
|
|
113
131
|
```ts
|
|
114
132
|
interface SessionContext {
|
|
115
133
|
readonly key: string;
|
|
116
|
-
readonly request:
|
|
134
|
+
readonly request: SessionRequest; // id (the same on every attempt), attempt (1 for the first), headers, target...
|
|
117
135
|
readonly db: Database;
|
|
118
136
|
readonly rpc: Rpc;
|
|
119
137
|
readonly send: Send;
|
|
120
138
|
readonly cancel: Cancel; // cancel an invocation, by its id
|
|
121
139
|
working<T>(fn: () => T | Promise<T>): Promise<T>; // keep the attempt alive while fn runs
|
|
140
|
+
stoppable<T>(fn: (signal: AbortSignal) => T | Promise<T>): Promise<T>; // the same, with a stop signal
|
|
141
|
+
suspendable<T>(fn: () => T | Promise<T>): Promise<T>; // the scopes around fn stop keeping the attempt alive
|
|
142
|
+
span<T>(name: string, fn: () => Promise<T>): Promise<T>; // fn's calls, sends and commits nested in a span of Restate's UI
|
|
122
143
|
}
|
|
123
144
|
|
|
124
145
|
interface ViewContext {
|
|
125
146
|
readonly key: string;
|
|
126
|
-
readonly request:
|
|
147
|
+
readonly request: SessionRequest;
|
|
127
148
|
readonly db: DatabaseReader;
|
|
128
149
|
readonly rpc: Rpc;
|
|
129
150
|
readonly cancel: Cancel;
|
|
@@ -143,17 +164,77 @@ one answers, so a wait of hours or days holds no process. An attempt that
|
|
|
143
164
|
waits for nothing starts again at once.
|
|
144
165
|
|
|
145
166
|
Work of the handler's own that may take longer than that, such as a model's
|
|
146
|
-
request, runs in `ctx.working`. While it runs, the
|
|
147
|
-
|
|
167
|
+
request, runs in `ctx.working` or `ctx.stoppable`. While it runs, the
|
|
168
|
+
attempt sends Restate a heartbeat whenever it has been silent for a third of
|
|
169
|
+
the handler's inactivity timeout (20 seconds for the default minute;
|
|
170
|
+
`RESTATE_SESSIONS_HEARTBEAT`, in milliseconds, overrides it), whatever else
|
|
171
|
+
it waits for.
|
|
172
|
+
The two differ in how the work stops:
|
|
173
|
+
|
|
174
|
+
- `ctx.working(fn)` only keeps the attempt alive. `fn` gets no signal and
|
|
175
|
+
runs to its end; a cancellation of the invocation reaches the handler at
|
|
176
|
+
its next `ctx.db` call or `ctx.rpc`.
|
|
177
|
+
- `ctx.stoppable(fn)` does the same, and hands `fn` a stop signal. It aborts
|
|
178
|
+
once: with the `CancelledError` when Restate cancels the invocation, or
|
|
179
|
+
with the error that ended the attempt when the attempt ends first (a scope
|
|
180
|
+
opened after either gets it aborted already). After a cancellation, the
|
|
181
|
+
attempt's end is signalled by `ctx.request.attemptCompletedSignal`. Work
|
|
182
|
+
that takes a signal, as a
|
|
183
|
+
model's request does, belongs here: it stops at once, rather than run on
|
|
184
|
+
for an invocation that is cancelled or an attempt that can store nothing.
|
|
148
185
|
|
|
149
186
|
```ts
|
|
150
|
-
|
|
187
|
+
// Work that takes no signal runs to its end.
|
|
188
|
+
const summary = await ctx.working(() => summarize(document));
|
|
189
|
+
|
|
190
|
+
// A model's request that takes one is aborted at a cancellation.
|
|
191
|
+
const response = await ctx.stoppable((signal) =>
|
|
192
|
+
fetch('https://api.openai.com/v1/responses', { method: 'POST', headers, body, signal }),
|
|
193
|
+
);
|
|
151
194
|
```
|
|
152
195
|
|
|
153
|
-
Keep long waits on Restate (a call, a signal) out of
|
|
154
|
-
holds its process for as long as they take. Work cut off outside
|
|
155
|
-
again by the next attempt, which runs from the start. The database's
|
|
156
|
-
calls keep the attempt alive by themselves.
|
|
196
|
+
Keep long waits on Restate (a call, a signal) out of both, or the attempt
|
|
197
|
+
holds its process for as long as they take. Work cut off outside them is
|
|
198
|
+
done again by the next attempt, which runs from the start. The database's
|
|
199
|
+
own calls keep the attempt alive by themselves.
|
|
200
|
+
|
|
201
|
+
A wait on Restate inside such a scope goes in `ctx.suspendable(fn)`: while
|
|
202
|
+
`fn` runs, the `ctx.working` and `ctx.stoppable` scopes it is called inside,
|
|
203
|
+
however deep, do not keep the attempt alive, and when it settles they do
|
|
204
|
+
again. That is for work that is kept alive as a whole but waits on Restate
|
|
205
|
+
for part of it, such as a framework's tool that calls another service: the
|
|
206
|
+
tool's own work is kept alive, and its wait on the call is not.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
await ctx.stoppable(async (signal) => {
|
|
210
|
+
// Work of its own: kept alive, and stopped by the signal.
|
|
211
|
+
const order = await draftOrder(prompt, { signal });
|
|
212
|
+
// Only a wait on Restate: the invocation may suspend on it.
|
|
213
|
+
return ctx.suspendable(() => ctx.rpc(shop, 'main', { idempotencyKey: ctx.request.id }).place(order));
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The attempt heartbeats while any scope keeps it alive. So with two pieces
|
|
218
|
+
of work at once, one of its own and one waiting in `ctx.suspendable`, the
|
|
219
|
+
attempt stays alive; once every open scope is inside a `ctx.suspendable`, it
|
|
220
|
+
sends nothing, and Restate may suspend the invocation on what it waits for.
|
|
221
|
+
What then holds:
|
|
222
|
+
|
|
223
|
+
- When the call answers, a new attempt runs the handler from its first line.
|
|
224
|
+
What ran before the `ctx.suspendable` runs again, and the call inside it
|
|
225
|
+
needs an idempotency key to reach the same invocation rather than start
|
|
226
|
+
another.
|
|
227
|
+
- Other work running at the same time in the same scope is not kept alive
|
|
228
|
+
while the `ctx.suspendable` is open: it is that scope that stops counting.
|
|
229
|
+
A scope opened inside `fn` (a `ctx.working` there, or a `ctx.db` call)
|
|
230
|
+
keeps the attempt alive as ever. Outside any scope, `fn` just runs.
|
|
231
|
+
- Cancellation is as ever: an await of Restate inside `fn` fails with the
|
|
232
|
+
cancellation, and a `ctx.stoppable` scope around it gets its signal
|
|
233
|
+
([cancelling an invocation](#cancelling-an-invocation), and its
|
|
234
|
+
[known limits](#known-limits)).
|
|
235
|
+
- A `ctx.suspendable` does not pause a _pinned_ scope
|
|
236
|
+
(`openScope(ctx, { pinned: true })`): for an integration whose framework
|
|
237
|
+
cannot resume a suspended tool call.
|
|
157
238
|
|
|
158
239
|
### Views
|
|
159
240
|
|
|
@@ -161,8 +242,11 @@ A view reads the session's database as committed, while handlers may be
|
|
|
161
242
|
committing at the same time. A chat UI can read a conversation while the
|
|
162
243
|
agent is still working on it, for example. A view reads a snapshot of the
|
|
163
244
|
database as it was when the view's invocation started, whatever the
|
|
164
|
-
handlers commit afterwards.
|
|
165
|
-
|
|
245
|
+
handlers commit afterwards. What that snapshot reads stays until the
|
|
246
|
+
handlers' second checkpoint after it. A checkpoint comes every 128 commits,
|
|
247
|
+
or sooner by size. A view that is still running after that fails ("is
|
|
248
|
+
gone"), and Restate retries it (readers are described in sdk-shared-core's
|
|
249
|
+
`src/state/docs/DESIGN.md`).
|
|
166
250
|
|
|
167
251
|
A view's `db` has no `execute` or `transaction`, and its context has no
|
|
168
252
|
`send`. A write through `query` fails too, because a view's database is
|
|
@@ -192,12 +276,13 @@ idempotency key ([calls and sends](#calls-and-sends)).
|
|
|
192
276
|
|
|
193
277
|
What each operation does when a new attempt reaches it again:
|
|
194
278
|
|
|
195
|
-
| Operation
|
|
196
|
-
|
|
|
197
|
-
| `ctx.db`
|
|
198
|
-
| `ctx.rpc`
|
|
199
|
-
| `ctx.send`
|
|
200
|
-
| `tx.send`
|
|
279
|
+
| Operation | On a new attempt |
|
|
280
|
+
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------- |
|
|
281
|
+
| `ctx.db` | Runs again against the database as committed. Record a stable id with your writes to recognize work already done. |
|
|
282
|
+
| `ctx.rpc` | Makes a new call, unless it has an idempotency key; then it reaches the invocation the earlier attempt started. |
|
|
283
|
+
| `ctx.send` | Sends again, unless it has an idempotency key; then Restate drops the repeat, and a delayed send keeps its first timer. |
|
|
284
|
+
| `tx.send` | Sent with its transaction's commit, once per commit ([sends in a transaction](#sends-in-a-transaction)). |
|
|
285
|
+
| `ctx.working`, `ctx.stoppable` | Run their work again. The stop signal is the new attempt's: aborted already if the invocation was cancelled before the attempt began. |
|
|
201
286
|
|
|
202
287
|
## The database
|
|
203
288
|
|
|
@@ -217,32 +302,26 @@ Outside a transaction, each statement is a commit of its own, and a
|
|
|
217
302
|
statement that changes nothing writes nothing. `ctx.db.transaction(tx => …)`
|
|
218
303
|
commits its body's statements together when the body resolves, and rolls
|
|
219
304
|
them back if it rejects. Every commit is one round trip to Restate, so a
|
|
220
|
-
transaction also makes a group of writes cheaper (
|
|
221
|
-
|
|
305
|
+
transaction also makes a group of writes cheaper (its sends ride the same
|
|
306
|
+
commit, see [sends in a transaction](#sends-in-a-transaction)).
|
|
222
307
|
|
|
223
308
|
Calls to `ctx.db` run one at a time in the order you make them, even when
|
|
224
309
|
you await them together, and so do a transaction's statements. A
|
|
225
310
|
transaction holds `ctx.db` until it ends: inside its body, use `tx`, since a
|
|
226
|
-
call to `ctx.db` is refused.
|
|
227
|
-
|
|
228
|
-
A body that holds its transaction open for more work (a library that batches
|
|
229
|
-
the commits of what it does) can ask whether another call is waiting:
|
|
230
|
-
`tx.queued` is a promise that settles once a call to `ctx.db` has queued
|
|
231
|
-
behind the transaction. A body that returns on it commits, and the waiting
|
|
232
|
-
call runs after that commit and sees it, so that call waits for one commit
|
|
233
|
-
and no more. A call made before the transaction began does not settle it, and
|
|
234
|
-
a body that ignores it behaves as before.
|
|
311
|
+
call to `ctx.db` is refused (but see [known limits](#known-limits)).
|
|
235
312
|
|
|
236
313
|
If a crash happens between two commits, the first one stays. Statements that
|
|
237
314
|
must succeed or fail together belong in one transaction.
|
|
238
315
|
|
|
239
316
|
Libraries can keep their data in `ctx.db` too: the
|
|
240
|
-
[pi integration](../agents/
|
|
317
|
+
[pi integration](../agents/README.md#pi) stores pi-durable's state there.
|
|
241
318
|
|
|
242
319
|
### Errors
|
|
243
320
|
|
|
244
|
-
An SQL error rejects with a `TerminalError`
|
|
245
|
-
since a retry would fail the same way.
|
|
321
|
+
An SQL error rejects with a `TerminalError` that carries the SQL error's
|
|
322
|
+
message, since a retry would fail the same way. Its `cause` is an `Error`
|
|
323
|
+
whose `name` is `'SqlError'`; the package exports no class for it, so tell
|
|
324
|
+
it apart by that name. That covers a statement the engine
|
|
246
325
|
rejects, a parameter it can't bind, and a call `ctx.db` refuses, including
|
|
247
326
|
the ones that close it: a `COMMIT` inside a transaction's body, or a
|
|
248
327
|
transaction still open when the handler returns. A storage error closes
|
|
@@ -298,6 +377,8 @@ that in the key, or use no key.
|
|
|
298
377
|
|
|
299
378
|
`ctx.cancel(invocationId)` cancels an invocation, as Restate's admin API
|
|
300
379
|
does. It is made at once and not awaited. Handlers and views both have it.
|
|
380
|
+
An id that is not an invocation id throws a `TerminalError` (400) at once,
|
|
381
|
+
and nothing is sent.
|
|
301
382
|
A view that cancels its own object's running handler is a Stop button: the
|
|
302
383
|
handler is cancelled, and the next message waiting for the key runs.
|
|
303
384
|
|
|
@@ -315,16 +396,16 @@ started once the first attempt's cancel took effect. Get the id from a call
|
|
|
315
396
|
with an idempotency key instead, which gives every attempt the same answer
|
|
316
397
|
([calls and sends](#calls-and-sends)).
|
|
317
398
|
|
|
318
|
-
A cancellation reaches a handler as
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
`
|
|
325
|
-
`ctx.db` says the call did nothing:
|
|
326
|
-
|
|
327
|
-
and its sends are made, once.
|
|
399
|
+
A cancellation reaches a handler once, as a `CancelledError`. When it comes,
|
|
400
|
+
the results of `ctx.rpc` calls still pending reject with it, and the calls
|
|
401
|
+
the attempt made that have not answered are cancelled. If none was pending,
|
|
402
|
+
the cancellation is owed to the handler: its next `ctx.db` call or its next
|
|
403
|
+
`ctx.rpc` fails with it, before anything is written or sent, unless work in
|
|
404
|
+
`ctx.stoppable` takes it (below). `ctx.send` and `ctx.cancel` never fail
|
|
405
|
+
with it. A `ctx.db` call already under way when the cancellation comes runs
|
|
406
|
+
to its end. So a `CancelledError` from `ctx.db` says the call did nothing:
|
|
407
|
+
it wrote nothing, and a `transaction()` neither committed nor made its
|
|
408
|
+
sends. A transaction that resolves committed, and its sends are made, once.
|
|
328
409
|
|
|
329
410
|
The calls after it work. So a handler can compensate, or make the call
|
|
330
411
|
again:
|
|
@@ -341,20 +422,73 @@ try {
|
|
|
341
422
|
}
|
|
342
423
|
```
|
|
343
424
|
|
|
425
|
+
Work that awaits nothing of Restate, such as a model's request that streams
|
|
426
|
+
for minutes, learns of a cancellation only from the stop signal of
|
|
427
|
+
`ctx.stoppable` ([long work outside Restate](#long-work-outside-restate)). A
|
|
428
|
+
cancellation that arrives while such a scope is open, or is still owed to
|
|
429
|
+
the handler when one opens, is told to the work by the signal, and is not
|
|
430
|
+
also owed to the handler's next call. So the database calls the work makes
|
|
431
|
+
as it stops (a framework storing the turn it stopped) are stored.
|
|
432
|
+
|
|
433
|
+
However the cancellation reached the handler (a `stoppable` scope's signal,
|
|
434
|
+
or a `ctx.rpc` or `ctx.db` call that failed with it, in this attempt or, for
|
|
435
|
+
a cancellation that came while the invocation was suspended, the attempt
|
|
436
|
+
that follows), its end is the cancellation's. A handler that then fails ends
|
|
437
|
+
the invocation as cancelled (`[409] Cancelled`), whatever it fails with and
|
|
438
|
+
however a library wrapped the error; one that returns answers it. This is
|
|
439
|
+
decided when the handler's function settles. A handler that returned, and
|
|
440
|
+
whose database then failed to save, ends with that failure (a storage error
|
|
441
|
+
is retried).
|
|
442
|
+
|
|
443
|
+
With the SDK's handler option `explicitCancellation: true`, nothing the
|
|
444
|
+
handler awaits fails and no call is cancelled. The stop signal aborts, and
|
|
445
|
+
that is all: the handler's end is its own.
|
|
446
|
+
|
|
447
|
+
A call that should be cancelled with its handler, and takes no longer than
|
|
448
|
+
an attempt may stay alive for, goes inside `ctx.stoppable`; a wait of hours
|
|
449
|
+
or days stays outside, where the invocation suspends and holds no process.
|
|
450
|
+
The pi integration of [`@restatedev/agents`](../agents/README.md) runs each model
|
|
451
|
+
request and each tool call in `ctx.stoppable`, a call the tool makes
|
|
452
|
+
included, so a pi tool needs no scope of its own.
|
|
453
|
+
|
|
344
454
|
A transaction whose body awaits something else (a `ctx.rpc`) that a
|
|
345
455
|
cancellation reaches fails with it and rolls back, unless the body catches
|
|
346
|
-
it. A retry of a cancelled invocation, after a crash, gets
|
|
347
|
-
at its first
|
|
348
|
-
the session's own, and is not failed.
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
456
|
+
it. A retry of a cancelled invocation, after a crash or a suspension, gets
|
|
457
|
+
the cancellation at its first `ctx.db` call or `ctx.rpc`; the database's
|
|
458
|
+
open is the session's own, and is not failed. An invocation suspended after
|
|
459
|
+
its attempt learned of the cancellation is woken only by a result it waited
|
|
460
|
+
for, not by the cancellation again. Once Restate has shut an attempt down, a cancellation owed to the handler is
|
|
461
|
+
not delivered: a `ctx.rpc` then fails with the attempt's end, the first
|
|
462
|
+
`ctx.db` call on the session the end closed with a plain `Error` ("the
|
|
463
|
+
database is closed") and the next with the attempt's end, and the next
|
|
464
|
+
attempt gets the cancellation.
|
|
465
|
+
|
|
466
|
+
### Known limits
|
|
467
|
+
|
|
468
|
+
- **A cancellation does not cancel a call an earlier attempt made.** A call
|
|
469
|
+
that outlives Restate's inactivity timeout, outside `ctx.working` and
|
|
470
|
+
`ctx.stoppable` or inside `ctx.suspendable`, suspends the invocation. A
|
|
471
|
+
cancellation that arrives then ends the turn as cancelled, in the attempt
|
|
472
|
+
that follows, but that call is not cancelled and runs to its end.
|
|
473
|
+
- **A call the handler does not await still takes the cancellation.** The
|
|
474
|
+
result of a call made with `void ctx.rpc(...)` rejects with the
|
|
475
|
+
cancellation like any pending call, and that counts as delivering it: the
|
|
476
|
+
handler's next `ctx.db` call is then not failed.
|
|
477
|
+
- **A deadlock inside a transaction is not always detected.** A `ctx.db`
|
|
478
|
+
call made from inside a transaction's body is refused. One made from work
|
|
479
|
+
started before the transaction, which the body then awaits, waits for the
|
|
480
|
+
transaction to end, and the transaction waits for it.
|
|
481
|
+
- **Routing covers one copy of the SDK.** Session objects are routed on
|
|
482
|
+
endpoints built from the copy of `@restatedev/restate-sdk` that
|
|
483
|
+
`@restatedev/sessions` imports. An endpoint from a CommonJS copy of the
|
|
484
|
+
SDK, or a process with two copies of `@restatedev/sessions`, is not
|
|
485
|
+
covered: the handler fails with a terminal error that says the endpoint
|
|
486
|
+
does not route to it.
|
|
353
487
|
|
|
354
488
|
## Sends in a transaction
|
|
355
489
|
|
|
356
|
-
A send made with `tx.send` goes out
|
|
357
|
-
|
|
490
|
+
A send made with `tx.send` goes out with its transaction's commit, and never
|
|
491
|
+
if it rolls back:
|
|
358
492
|
|
|
359
493
|
```ts
|
|
360
494
|
ctx.db.transaction(async (tx) => {
|
|
@@ -364,30 +498,19 @@ ctx.db.transaction(async (tx) => {
|
|
|
364
498
|
});
|
|
365
499
|
```
|
|
366
500
|
|
|
367
|
-
The sends are
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
The next exclusive attempt opens the database when it starts, and sends them
|
|
381
|
-
again with the same keys and whatever remains of their delays, which count
|
|
382
|
-
from the commit. Restate drops a send whose key it has already seen. So
|
|
383
|
-
every send of a committed transaction is made once, even if no later attempt
|
|
384
|
-
uses the database, and the sends of a transaction that rolled back or was
|
|
385
|
-
lost are never made.
|
|
386
|
-
|
|
387
|
-
A transaction without sends costs nothing extra; one with sends costs one
|
|
388
|
-
more commit. If the sends fail after the commit because the attempt was
|
|
389
|
-
replaced, `transaction()` rejects even though it committed: that attempt is
|
|
390
|
-
over, and the next one makes them.
|
|
501
|
+
The sends are part of the transaction's commit: the commit is one atomic
|
|
502
|
+
group of the protocol, with the pages and the head first and the sends after
|
|
503
|
+
them, and Restate applies all of it or none of it. So a send goes out exactly
|
|
504
|
+
when its rows are stored, once per commit, with no idempotency key of this
|
|
505
|
+
package's making and nothing to remember across a crash. A retry that runs
|
|
506
|
+
the transaction again commits again, and sends again: check for the work
|
|
507
|
+
first. A transaction that writes nothing (its body only sends) makes its
|
|
508
|
+
sends in a group of their own, and `transaction()` resolves once Restate
|
|
509
|
+
applied it; that group leaves nothing in the database, so every attempt that
|
|
510
|
+
reaches the transaction sends again. A delay counts from the commit. A send Restate would refuse (an empty idempotency key) fails the
|
|
511
|
+
commit with a `TerminalError` (400), and the group is dropped with its rows.
|
|
512
|
+
|
|
513
|
+
A transaction with sends costs nothing extra: they ride its commit.
|
|
391
514
|
|
|
392
515
|
A send doesn't return an invocation id. When you need a call's result, make
|
|
393
516
|
the call before the transaction, with an idempotency key of its own, and
|
|
@@ -398,31 +521,29 @@ record its outcome in the transaction. The shop's `Orders` does this, and its
|
|
|
398
521
|
|
|
399
522
|
**Where the data lives.** The database is stored in the object's state,
|
|
400
523
|
under the keys `v22/head`, `v22/tail/…`, `v22/manifest/…` and `v22/pack/…`.
|
|
401
|
-
The addon reads and writes them as the attempt's own state
|
|
402
|
-
|
|
403
|
-
it. Handlers and views can't reach any other state key, since a session has
|
|
524
|
+
The addon reads and writes them as the attempt's own state actions,
|
|
525
|
+
recorded as nothing: Restate's UI shows none of the pages. Handlers and views can't reach any other state key, since a session has
|
|
404
526
|
no key/value state of its own.
|
|
405
527
|
|
|
406
528
|
**Reads and writes.** A read is a state read. It goes to the runtime unless
|
|
407
529
|
this attempt wrote the key or the invocation started with it. An invocation
|
|
408
530
|
starts with only the database's head (`state: { preload: ['v22/head'] }`):
|
|
409
531
|
its open's first read needs no round trip, and it doesn't start by loading
|
|
410
|
-
every page.
|
|
411
|
-
ephemeral get, which the journal doesn't record; on v7, which Restate 1.8
|
|
412
|
-
negotiates, it is a lazy get, recorded in the journal with its value. A
|
|
532
|
+
every page. The protocol has no journal: nothing records a read. A
|
|
413
533
|
write or a clear is a state write, and returns at once. Restate keeps an
|
|
414
534
|
attempt's writes and sends in the order they were made, so a crash can lose
|
|
415
535
|
the last ones, but never a write without those before it.
|
|
416
536
|
|
|
417
537
|
**Commits.** A transaction writes the pages it changed, and its records, as
|
|
418
538
|
one tail entry under a key of its own. Then it writes the head, which points
|
|
419
|
-
to that entry; this is the commit point. The
|
|
420
|
-
|
|
421
|
-
|
|
539
|
+
to that entry; this is the commit point. The tail entry, the head and the
|
|
540
|
+
transaction's sends are one atomic group, whose result the commit waits
|
|
541
|
+
for, so a commit is one round trip. A commit larger than 8 MiB writes what
|
|
542
|
+
exceeds that first, as plain writes nothing names until the head does. Whatever a
|
|
422
543
|
crash keeps, the stored head only points to tail entries and checkpoints
|
|
423
544
|
that are stored too. Now and then, a commit first folds the tail into a
|
|
424
|
-
checkpoint (see the
|
|
425
|
-
|
|
545
|
+
checkpoint (see the state's architecture, `src/state/docs/ARCHITECTURE.md`
|
|
546
|
+
in sdk-shared-core).
|
|
426
547
|
|
|
427
548
|
**The page cache.** The process caches the pages it read and wrote, and the
|
|
428
549
|
database's state as of its last head, for the key's next invocation.
|
|
@@ -456,7 +577,7 @@ the head, the tail of recent commits (up to two checkpoints' worth: 128
|
|
|
456
577
|
commits or 1 MiB each, plus the commit that passed it) and the last two
|
|
457
578
|
checkpoints' manifests (a few bytes per 16 KiB of database, and records of
|
|
458
579
|
up to 1 KiB). A cold read of anything older than the tail is an object
|
|
459
|
-
store read, and each checkpoint
|
|
580
|
+
store read, and each checkpoint (every 128 commits, or sooner by size) an upload and a
|
|
460
581
|
listing of the database's directory. Until its first checkpoint, a
|
|
461
582
|
database is all tail, in Restate. The value may change from one deployment
|
|
462
583
|
to the next: a lower one moves what Restate holds at the next checkpoint, a
|
|
@@ -467,10 +588,13 @@ is not a size fails every open with its message.
|
|
|
467
588
|
**No journal replay.** On a retry or a resume, the SDK normally replays the
|
|
468
589
|
journal: each operation returns its recorded result, and must match it.
|
|
469
590
|
That can't work here, because a later attempt makes different reads,
|
|
470
|
-
depending on what the page cache holds.
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
591
|
+
depending on what the page cache holds. The explicit recovery protocol has
|
|
592
|
+
no journal: every attempt runs the handler again from the start against the
|
|
593
|
+
state as it is, and Restate records what each attempt did for its UI
|
|
594
|
+
(attempts, calls, commits and spans; a `db.transaction` opens no span of
|
|
595
|
+
its own, and its commit is inside the `ctx.span` it runs in, if any, so
|
|
596
|
+
`ctx.span('save', () => ctx.db.transaction(...))` shows it as one).
|
|
597
|
+
Recovery is then up to the storage:
|
|
474
598
|
opening the database reads the head, so a commit the crashed attempt didn't
|
|
475
599
|
finish is as if it never started.
|
|
476
600
|
|
|
@@ -492,10 +616,10 @@ own, and lets only one of them wait for the server at a time
|
|
|
492
616
|
`awaitNext already pending`, and it is retried forever.
|
|
493
617
|
|
|
494
618
|
A session object does not run on that VM. It runs on shared-core's
|
|
495
|
-
`
|
|
496
|
-
([`docs/explicit-recovery.md`](docs/explicit-recovery.md)):
|
|
497
|
-
a notification handle, results come back
|
|
498
|
-
each promise as its handle is notified. There is no single await point to
|
|
619
|
+
`CoreExplicitRecoveryVM`, the state machine of the explicit recovery protocol
|
|
620
|
+
([`docs/explicit-recovery.md`](docs/explicit-recovery.md)): an action returns
|
|
621
|
+
a notification handle, results come back as notifications, and the driver
|
|
622
|
+
settles each promise as its handle is notified. There is no single await point to
|
|
499
623
|
contend for, so a handler awaits whatever it likes.
|
|
500
624
|
|
|
501
625
|
`src/endpoint.ts` routes a session object's invocation there, by patching
|
|
@@ -503,7 +627,8 @@ contend for, so a handler awaits whatever it likes.
|
|
|
503
627
|
and `connect`'s tunnel all build theirs from. That prototype and the
|
|
504
628
|
endpoint's builder are SDK internals as of 1.18.2, so the package pins that
|
|
505
629
|
version. A fetch or Lambda endpoint is not built from it and cannot serve a
|
|
506
|
-
session object; it is refused rather than half-served
|
|
630
|
+
session object; it is refused rather than half-served (see also
|
|
631
|
+
[known limits](#known-limits)).
|
|
507
632
|
|
|
508
633
|
What you get:
|
|
509
634
|
|
|
@@ -511,8 +636,9 @@ What you get:
|
|
|
511
636
|
handle, so a call awaited late isn't held up by a slow call made earlier.
|
|
512
637
|
- **An operation starts when you make it.** A call goes out without an
|
|
513
638
|
`await`, and a failure nobody awaits is not an unhandled rejection.
|
|
514
|
-
- **Cancellation reaches every
|
|
515
|
-
`CancelledError`; a `ctx.db` call already under way runs to its end
|
|
639
|
+
- **Cancellation reaches every pending call**, and each one rejects with
|
|
640
|
+
`CancelledError`; a `ctx.db` call already under way runs to its end, and
|
|
641
|
+
work in `ctx.stoppable` gets it as its stop signal
|
|
516
642
|
([cancelling an invocation](#cancelling-an-invocation)).
|
|
517
643
|
|
|
518
644
|
```ts
|
|
@@ -547,55 +673,50 @@ the tests use them:
|
|
|
547
673
|
the stock, and an unpaid order releases it when it expires.
|
|
548
674
|
|
|
549
675
|
Each one runs on its own (`node test/shop.ts`, from here) or together with
|
|
550
|
-
the others (`
|
|
551
|
-
|
|
676
|
+
the others (`node serve.ts` in [`packages/e2e/`](../e2e), or
|
|
677
|
+
`node serve.ts ledger shop` for a few), on port 9080 or `PORT`:
|
|
552
678
|
|
|
553
679
|
```sh
|
|
554
680
|
pnpm install && pnpm build # from the repository root
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
npx @restatedev/restate deployments register http://
|
|
681
|
+
cd packages/e2e && node serve.ts ledger shop # the ledger and the shop, on port 9080
|
|
682
|
+
# Restate, in another terminal: its image, as docs/getting-started.md says
|
|
683
|
+
npx @restatedev/restate -y deployments register http://host.docker.internal:9080
|
|
558
684
|
curl localhost:8080/Ledger/bank/init --json '[8, 1000]'
|
|
559
685
|
curl localhost:8080/Warehouse/apple/restock --json '{"id": "first", "qty": 10}' # orders need stock
|
|
560
686
|
curl localhost:8080/Orders/o1/place --json '{"sku": "apple", "qty": 2}'
|
|
561
687
|
```
|
|
562
688
|
|
|
563
689
|
Both run through crashes against a real server in
|
|
564
|
-
[the crash test](../e2e/crash.ts) (`pnpm --filter e2e crash`).
|
|
565
|
-
node, 200 ledger transfers in a row take 18.8 ms each.
|
|
690
|
+
[the crash test](../e2e/crash.ts) (`pnpm --filter e2e crash`).
|
|
566
691
|
|
|
567
692
|
For an agent that keeps its state in `ctx.db`, see
|
|
568
|
-
[`@restatedev/agents/pi`](../agents/
|
|
693
|
+
[`@restatedev/agents/pi`](../agents/README.md#pi), which runs pi-durable's
|
|
569
694
|
storage in the session's database.
|
|
570
695
|
|
|
571
696
|
## Caveats
|
|
572
697
|
|
|
573
|
-
- **On protocol v7, every read is journaled with its value.** A state read
|
|
574
|
-
that goes to the runtime adds the value to the invocation's journal, so
|
|
575
|
-
reading pages costs log writes, and the page cache matters more than
|
|
576
|
-
usual. Restate 1.8 negotiates v7; on v8 such a read is ephemeral. A
|
|
577
|
-
completed invocation's journal, reads included, is kept for Restate's
|
|
578
|
-
default retention.
|
|
579
|
-
- **The journal grows across attempts.** Entries an attempt skips stay, and
|
|
580
|
-
each attempt adds its own.
|
|
581
698
|
- **A shutdown is a restart.** Restate shuts an attempt down once its
|
|
582
699
|
inactivity timeout passes with nothing heard from it, and the next attempt
|
|
583
700
|
runs from the start: when a call it waits for answers, or at once if it
|
|
584
|
-
waits for none. Only work in `ctx.working`
|
|
701
|
+
waits for none. Only work in `ctx.working` or `ctx.stoppable`
|
|
585
702
|
([long work outside Restate](#long-work-outside-restate)) and the
|
|
586
|
-
database's own calls keep an attempt alive
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
703
|
+
database's own calls keep an attempt alive: it sends a heartbeat whenever
|
|
704
|
+
it has been silent for a third of the inactivity timeout that the handler,
|
|
705
|
+
else its object, else the endpoint's default service options set (a
|
|
706
|
+
minute when none does).
|
|
707
|
+
`RESTATE_SESSIONS_HEARTBEAT`, in milliseconds, overrides the interval. A
|
|
708
|
+
timeout set only on the server or through the admin API is not seen by the
|
|
709
|
+
SDK: a deployment that shortens it there must shorten the interval with it.
|
|
710
|
+
- **One round trip per commit.** Every commit waits for Restate to apply its
|
|
711
|
+
atomic group, a round trip to the runtime. Statements that commit one by
|
|
591
712
|
one pay it each time, so group them in a transaction.
|
|
592
713
|
- **10 GiB per database.** A statement that would grow a key's database past
|
|
593
714
|
that fails with SQLite's "database or disk is full", as an SQL error (a
|
|
594
715
|
`TerminalError`), and `ctx.db` keeps working.
|
|
595
|
-
- **Assumptions still to confirm with the runtime.**
|
|
596
|
-
comes only
|
|
597
|
-
abandoned attempt's
|
|
598
|
-
(fencing). State that a
|
|
716
|
+
- **Assumptions still to confirm with the runtime.** An atomic group's
|
|
717
|
+
result comes only once it and everything before it are applied. An
|
|
718
|
+
abandoned attempt's actions are never applied after a new attempt starts
|
|
719
|
+
(fencing). State that a read returns is durable. The crash tests kill
|
|
599
720
|
processes, so they check recovery, not durability across a machine crash.
|
|
600
721
|
|
|
601
722
|
## Not supported
|
|
@@ -616,33 +737,64 @@ storage in the session's database.
|
|
|
616
737
|
their sends, views, SQL errors and lost writes.
|
|
617
738
|
- [`native/`](native/README.md#build-and-test): the addon's tests
|
|
618
739
|
(`pnpm --filter @restatedev/sessions test:rust`).
|
|
740
|
+
- [`e2e/test/`](../e2e/test): against the runtime image, in a container
|
|
741
|
+
(`pnpm --filter e2e test:e2e`): attempts, keep-alive, the stop signal,
|
|
742
|
+
cancellation and suspension as [`docs/semantics.md`](../../docs/semantics.md)
|
|
743
|
+
states them, the protocol's record of each attempt, and the endpoint.
|
|
619
744
|
- [`e2e/concurrency.ts`](../e2e/concurrency.ts) and
|
|
620
|
-
[`e2e/crash.ts`](../e2e/crash.ts): against
|
|
745
|
+
[`e2e/crash.ts`](../e2e/crash.ts): against the image's `restate-server`,
|
|
746
|
+
on Linux.
|
|
621
747
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
748
|
+
To test a session object of your own, run its handler attempt by attempt
|
|
749
|
+
with `invoke` of `@restatedev/sessions/internal` over its state in a `Map`,
|
|
750
|
+
as [testing](../../docs/sessions/testing.md) shows. Inside this repository,
|
|
751
|
+
[`test/support.ts`](test/support.ts) has richer hosts, through crashes and
|
|
752
|
+
fencing, which `@restatedev/agents`'s tests use too; it is not part of the
|
|
753
|
+
published package.
|
|
625
754
|
|
|
626
755
|
`@restatedev/sessions/internal` exposes what the tests take apart:
|
|
627
756
|
|
|
628
|
-
- `invoke`: run a handler for one attempt, over
|
|
629
|
-
|
|
630
|
-
- `
|
|
757
|
+
- `invoke`: run a handler for one attempt, over an `AttemptHost` of your own
|
|
758
|
+
(its key, request, `Stop`, the object's state as a `Store`, and calls);
|
|
759
|
+
- `contextOf`: a handler's or view's context over such a host;
|
|
760
|
+
- `Attempt` and `withAttempt`: an attempt's database, and how a handler ends;
|
|
631
761
|
- `open` and `asDatabase`: a database over a store of your own;
|
|
632
|
-
- `
|
|
633
|
-
- `
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
one
|
|
638
|
-
the
|
|
639
|
-
`
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
762
|
+
- `configOf`: what a session object's attempts get, for attempts of a test's own;
|
|
763
|
+
- `lazyDatabase`: a database opened on first use, as `ctx.db` is;
|
|
764
|
+
- `Connection`, `ConnectionTransaction` and `Store`: the types of `open`'s
|
|
765
|
+
database, of one of its transactions, and of the store under it;
|
|
766
|
+
- `Stop` and `Scope`: an attempt's cancellation, stop signal and keep-alive
|
|
767
|
+
scopes, and one such scope; `Working`, `Stoppable` and `Suspendable` are
|
|
768
|
+
the types of `ctx.working`, `ctx.stoppable` and `ctx.suspendable`;
|
|
769
|
+
- `AttemptDriver`, `Vm` and `AttemptShutDown`: an attempt driven on the
|
|
770
|
+
explicit recovery VM, the VM, and the error an attempt Restate shut down
|
|
771
|
+
ends with; `CONTENT_TYPE`, `manifestV6` and `goAway`: the protocol's
|
|
772
|
+
content type, the discovery manifest, and the end of a process's attempts
|
|
773
|
+
on a SIGTERM;
|
|
774
|
+
- `HEAD_KEY`: the state key of a database's head.
|
|
775
|
+
|
|
776
|
+
It also has what a library that runs in the database can use: `Schema`
|
|
777
|
+
(tables a library owns, applied where it uses them with
|
|
778
|
+
`schema.ensure(ctx.db)`, which fails with a `TerminalError` given a
|
|
779
|
+
transaction's `tx`; `ensureSchemas` applies several in one commit), `database` (another database of the same key),
|
|
780
|
+
`sdk` (the copy of the Restate
|
|
781
|
+
SDK that `@restatedev/sessions` runs, so a library's journaled handlers
|
|
782
|
+
serve beside its objects) and `served` (also exported from
|
|
783
|
+
`@restatedev/sessions`: what `serve` serves, for an endpoint of your own);
|
|
784
|
+
and what an integration of a framework can: `openScope` (a `ctx.stoppable`
|
|
785
|
+
scope held open until it is released, for a framework's hooks; `pinned`, a
|
|
786
|
+
`ctx.suspendable` inside it does not stop it), and `locateScopes` (to tell
|
|
787
|
+
`ctx.suspendable` the scope a framework's hook opened apart from the work it
|
|
788
|
+
keeps alive; it returns a function that takes the locator back).
|
|
789
|
+
Provisional, for libraries, from a context of an attempt on the VM:
|
|
790
|
+
`invocationState` (state of the invocation's own, which every attempt of it
|
|
791
|
+
finds), `readSignal` and `sendSignal` (signals addressed to an invocation, by
|
|
792
|
+
name), and `flush` (resolves once every action before it is applied). A
|
|
793
|
+
view's context writes no invocation state and sends no signal: those throw a
|
|
794
|
+
`TerminalError`. A cancellation takes `readSignal` as it takes a `ctx.rpc`;
|
|
795
|
+
it never fails an invocation-state read or a flush.
|
|
796
|
+
|
|
797
|
+
Nothing is kept per process but the native page cache, and the wrapper the
|
|
798
|
+
package installs on the SDK's endpoint class. What a session
|
|
799
|
+
object is given (its schema) is on its definition, and a
|
|
800
|
+
database or transaction this package makes holds its own link to its session.
|