@restatedev/sessions 0.9.0 → 0.10.1
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 +131 -73
- package/dist/addon.d.ts +9 -0
- package/dist/addon.d.ts.map +1 -0
- package/dist/addon.js +20 -0
- package/dist/addon.js.map +1 -0
- package/dist/database.d.ts +13 -0
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +44 -17
- package/dist/database.js.map +1 -1
- package/dist/driver.d.ts +186 -0
- package/dist/driver.d.ts.map +1 -0
- package/dist/driver.js +554 -0
- package/dist/driver.js.map +1 -0
- package/dist/endpoint.d.ts +13 -0
- package/dist/endpoint.d.ts.map +1 -0
- package/dist/endpoint.js +186 -0
- package/dist/endpoint.js.map +1 -0
- package/dist/handler.d.ts +35 -0
- package/dist/handler.d.ts.map +1 -0
- package/dist/handler.js +218 -0
- package/dist/handler.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +4 -2
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +7 -3
- package/dist/internal.js.map +1 -1
- package/dist/object.d.ts +15 -2
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +51 -12
- package/dist/object.js.map +1 -1
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +5 -0
- package/dist/serve.js.map +1 -1
- package/dist/session.d.ts +32 -2
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +59 -9
- package/dist/session.js.map +1 -1
- package/dist/vm.d.ts +151 -0
- package/dist/vm.d.ts.map +1 -0
- package/dist/vm.js +28 -0
- package/dist/vm.js.map +1 -0
- package/package.json +4 -4
- package/src/addon.ts +29 -0
- package/src/database.ts +48 -19
- package/src/driver.ts +657 -0
- package/src/endpoint.ts +220 -0
- package/src/handler.ts +272 -0
- package/src/index.ts +2 -1
- package/src/internal.ts +7 -4
- package/src/object.ts +75 -13
- package/src/serve.ts +5 -0
- package/src/session.ts +77 -8
- package/src/vm.ts +195 -0
- package/dist/selector.d.ts +0 -129
- package/dist/selector.d.ts.map +0 -1
- package/dist/selector.js +0 -251
- package/dist/selector.js.map +0 -1
- package/src/selector.ts +0 -323
package/dist/selector.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
// Concurrent awaits in a handler with SQL: one loop, the selector, drives
|
|
2
|
-
// every promise the handler awaits.
|
|
3
|
-
//
|
|
4
|
-
// The published SDK drives each awaited RestatePromise with a loop of its own,
|
|
5
|
-
// and only one of them may wait for progress from the server at a time
|
|
6
|
-
// (`ExternalProgressChannel.awaitNext`): two promises awaited at once that
|
|
7
|
-
// both need the server, such as two state reads with lazy state, fail the
|
|
8
|
-
// attempt with "awaitNext already pending". With journal replay, that order of
|
|
9
|
-
// awaits is part of what a replay must match; with explicit recovery nothing
|
|
10
|
-
// is replayed, so a handler with SQL hands every awaited promise to one loop,
|
|
11
|
-
// which asks the VM about all of them at once (`hookSelector`). The hook
|
|
12
|
-
// replaces the invocation's `promisesExecutor.doProgress`: it relies on the
|
|
13
|
-
// SDK's internals, as of the version this package pins (1.18.2).
|
|
14
|
-
//
|
|
15
|
-
// TODO: drop the selector, and the pin, once an SDK release lets a handler
|
|
16
|
-
// with explicit recovery await concurrently.
|
|
17
|
-
import { setImmediate } from 'node:timers/promises';
|
|
18
|
-
/** The handle of the invocation's cancel signal: the VM's, and what `ctx.cancellation()` awaits. */
|
|
19
|
-
const CANCELLATION = 1;
|
|
20
|
-
/** Whether `future` waits for the invocation's cancellation, and nothing else. */
|
|
21
|
-
const cancellationAlone = (future) => 'Single' in future ? future.Single === CANCELLATION : Object.values(future)[0].every(cancellationAlone);
|
|
22
|
-
/**
|
|
23
|
-
* For tests: what every selector's loop awaits at each of its failpoints, to
|
|
24
|
-
* hold it there while something else happens (the input closes, a signal
|
|
25
|
-
* comes). At `wait`, it gets what the check asked for, and whether that was
|
|
26
|
-
* the invocation's cancellation alone (with the working run). Empty
|
|
27
|
-
* otherwise, and the loop then awaits nothing there.
|
|
28
|
-
*/
|
|
29
|
-
export const failpoints = new Map();
|
|
30
|
-
/**
|
|
31
|
-
* One loop that drives every pending promise of an invocation, so that the
|
|
32
|
-
* invocation has a single await point however many promises user code awaits
|
|
33
|
-
* at once, with its own combinators or none.
|
|
34
|
-
*
|
|
35
|
-
* A promise is registered once and stays until it settles. Each pass of the
|
|
36
|
-
* loop settles what is ready, then asks the VM for progress on the first of
|
|
37
|
-
* all the pending promises to complete: so a suspension waits for any of
|
|
38
|
-
* them, a pending run starts whichever promise is awaited first, and a
|
|
39
|
-
* cancellation reaches all of them. Registering wakes the loop, so a promise
|
|
40
|
-
* registered while it waits is part of the next pass. The loop runs while
|
|
41
|
-
* there is something to ask the VM for, and is the only one waiting for
|
|
42
|
-
* external progress.
|
|
43
|
-
*
|
|
44
|
-
* To ask is to say that the handler waits: the VM tells Restate what for,
|
|
45
|
-
* and suspends the invocation on it once the input closed (Restate's
|
|
46
|
-
* inactivity timeout). That holds for every promise but one. A handler with
|
|
47
|
-
* explicit cancellation awaits its cancellation (`cancelled(ctx)`) to stop
|
|
48
|
-
* work it does outside Restate, a model's request, say: it watches for it,
|
|
49
|
-
* and does not wait for it. Reported as awaited, the cancel signal would be
|
|
50
|
-
* all Restate believes the handler waits for; and suspended on it, the
|
|
51
|
-
* invocation would end its attempt in the middle of that work, with a
|
|
52
|
-
* cancellation as the only thing to resume it.
|
|
53
|
-
*
|
|
54
|
-
* So with only the cancellation pending, the loop asks for it together with
|
|
55
|
-
* the attempt's working run (`SelectorDriver.work`), which says what is true:
|
|
56
|
-
* the handler is at work on something of its own, as inside `ctx.run`. While
|
|
57
|
-
* a run executes, the VM reports no await, and does not suspend on a future
|
|
58
|
-
* the run is part of, whether the input is open or closed; and it still
|
|
59
|
-
* finds the cancel signal when it comes. See `awaited`.
|
|
60
|
-
*/
|
|
61
|
-
export class PromiseSelector {
|
|
62
|
-
pending = new Set();
|
|
63
|
-
running = false;
|
|
64
|
-
closed = false;
|
|
65
|
-
// Between a check that found nothing and the next external progress.
|
|
66
|
-
waiting = false;
|
|
67
|
-
// Whether the last check asked for the cancellation alone, with the working run.
|
|
68
|
-
watching = false;
|
|
69
|
-
driver;
|
|
70
|
-
constructor(driver) {
|
|
71
|
-
this.driver = driver;
|
|
72
|
-
}
|
|
73
|
-
register(promise) {
|
|
74
|
-
if (this.closed || this.pending.has(promise))
|
|
75
|
-
return;
|
|
76
|
-
this.pending.add(promise);
|
|
77
|
-
if (this.running) {
|
|
78
|
-
this.wake();
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
this.running = true;
|
|
82
|
-
void this.loop();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/** Stops the loop (it finds nothing pending), at the end of the attempt. */
|
|
86
|
-
close() {
|
|
87
|
-
if (this.closed)
|
|
88
|
-
return;
|
|
89
|
-
this.closed = true;
|
|
90
|
-
this.pending.clear();
|
|
91
|
-
if (this.running)
|
|
92
|
-
this.wake();
|
|
93
|
-
}
|
|
94
|
-
// Only a waiting loop needs waking, once: any other picks up the change on
|
|
95
|
-
// its next pass. (Waking it otherwise would buffer a signal per promise, and
|
|
96
|
-
// as many needless passes, each of which tells Restate what is awaited.)
|
|
97
|
-
wake() {
|
|
98
|
-
if (!this.waiting)
|
|
99
|
-
return;
|
|
100
|
-
this.waiting = false;
|
|
101
|
-
this.driver.wake();
|
|
102
|
-
}
|
|
103
|
-
/** The promises still pending, for tests. */
|
|
104
|
-
get size() {
|
|
105
|
-
return this.pending.size;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* What to ask the VM for: the first of the pending promises to complete,
|
|
109
|
-
* or undefined with nothing pending. With only the invocation's
|
|
110
|
-
* cancellation pending, the handler watches and waits for nothing: the
|
|
111
|
-
* working run goes with it (see the class).
|
|
112
|
-
*/
|
|
113
|
-
awaited() {
|
|
114
|
-
const futures = [];
|
|
115
|
-
let watching = true; // for the cancellation, and waiting for nothing
|
|
116
|
-
for (const promise of this.pending) {
|
|
117
|
-
const future = promise.unresolvedFuture();
|
|
118
|
-
if (future === null) {
|
|
119
|
-
this.pending.delete(promise);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
futures.push(future);
|
|
123
|
-
watching &&= cancellationAlone(future);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
this.watching = watching && futures.length > 0;
|
|
127
|
-
if (futures.length === 0)
|
|
128
|
-
return undefined;
|
|
129
|
-
if (watching)
|
|
130
|
-
futures.push({ Single: this.driver.work() });
|
|
131
|
-
return futures.length === 1 ? futures[0] : { FirstCompleted: futures };
|
|
132
|
-
}
|
|
133
|
-
async loop() {
|
|
134
|
-
try {
|
|
135
|
-
await this.driver.flushOutput();
|
|
136
|
-
for (;;) {
|
|
137
|
-
if (failpoints.size > 0)
|
|
138
|
-
await failpoints.get('pass')?.();
|
|
139
|
-
// Settle what is ready.
|
|
140
|
-
for (const promise of this.pending) {
|
|
141
|
-
await promise.tryComplete();
|
|
142
|
-
}
|
|
143
|
-
// A turn of the event loop: what was settled runs, and registers
|
|
144
|
-
// what it awaits next, so that one check covers that too (a
|
|
145
|
-
// combinator also flags itself completed in a .finally, a tick late).
|
|
146
|
-
// An optimization: what is registered later wakes the loop anyway.
|
|
147
|
-
await setImmediate();
|
|
148
|
-
// From here to the end of the pass, or to `waiting`, in one turn:
|
|
149
|
-
// what is registered after the check finds the loop gone, and starts
|
|
150
|
-
// another, or finds it waiting, and wakes it.
|
|
151
|
-
const future = this.awaited();
|
|
152
|
-
if (future === undefined)
|
|
153
|
-
return;
|
|
154
|
-
const result = this.driver.doProgress(future);
|
|
155
|
-
if (result === 'AnyCompleted') {
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
else if (result === 'WaitExternalProgress') {
|
|
159
|
-
this.waiting = true;
|
|
160
|
-
if (failpoints.size > 0)
|
|
161
|
-
await failpoints.get('wait')?.(future, this.watching);
|
|
162
|
-
await this.driver.flushOutput();
|
|
163
|
-
await this.driver.awaitExternalProgress();
|
|
164
|
-
this.waiting = false;
|
|
165
|
-
}
|
|
166
|
-
else if (result === 'CancelSignalReceived') {
|
|
167
|
-
// Reaches every pending promise; what is awaited after it is not cancelled.
|
|
168
|
-
for (const promise of this.pending)
|
|
169
|
-
promise.tryCancel();
|
|
170
|
-
this.pending.clear();
|
|
171
|
-
}
|
|
172
|
-
else if ('ExecuteRun' in result) {
|
|
173
|
-
// It starts in the turn the next pass takes before its check.
|
|
174
|
-
this.driver.executeRun(result.ExecuteRun);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
// An ephemeral command's notification: a handler with SQL makes none.
|
|
178
|
-
throw new Error(`@restatedev/sessions: unexpected progress from the VM: ${JSON.stringify(result)}`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
catch (e) {
|
|
183
|
-
this.close();
|
|
184
|
-
this.driver.onError(e);
|
|
185
|
-
}
|
|
186
|
-
finally {
|
|
187
|
-
this.running = false;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
const FIELDS = ['coreVm', 'outputPump', 'externalProgressChannel', 'runClosuresTracker', 'errorCallback'];
|
|
192
|
-
/**
|
|
193
|
-
* Drive every promise that the invocation of `ctx` (the SDK's context, not a
|
|
194
|
-
* proxy of it) awaits from here on with one selector. The returned function
|
|
195
|
-
* stops it, at the end of the attempt, and gives the invocation back its
|
|
196
|
-
* own loops.
|
|
197
|
-
*/
|
|
198
|
-
export function hookSelector(ctx) {
|
|
199
|
-
const executor = ctx.promisesExecutor;
|
|
200
|
-
const missing = executor ? FIELDS.filter((field) => executor[field] === undefined) : ['promisesExecutor'];
|
|
201
|
-
if (missing.length === 0) {
|
|
202
|
-
const { coreVm, runClosuresTracker } = executor;
|
|
203
|
-
for (const method of ['do_progress', 'sys_run', 'propose_run_completion_success']) {
|
|
204
|
-
if (typeof coreVm[method] !== 'function')
|
|
205
|
-
missing.push(`coreVm.${method}`);
|
|
206
|
-
}
|
|
207
|
-
if (typeof runClosuresTracker.registerRunClosure !== 'function')
|
|
208
|
-
missing.push('runClosuresTracker.registerRunClosure');
|
|
209
|
-
}
|
|
210
|
-
if (missing.length > 0 || executor === undefined) {
|
|
211
|
-
throw new Error(`@restatedev/sessions drives a handler's promises through the SDK's internals, which this SDK version does not have ` +
|
|
212
|
-
`(${missing.join(', ')}): use the @restatedev/restate-sdk that @restatedev/sessions pins`);
|
|
213
|
-
}
|
|
214
|
-
const { coreVm, outputPump, externalProgressChannel, runClosuresTracker } = executor;
|
|
215
|
-
// The working run: journaled as a run named `work`, with no closure of its
|
|
216
|
-
// own to end it. It ends with the attempt.
|
|
217
|
-
let work;
|
|
218
|
-
const selector = new PromiseSelector({
|
|
219
|
-
doProgress: (future) => coreVm.do_progress(future),
|
|
220
|
-
flushOutput: () => outputPump.awaitNextProgress(),
|
|
221
|
-
awaitExternalProgress: () => externalProgressChannel.awaitNext(),
|
|
222
|
-
wake: () => externalProgressChannel.signal(),
|
|
223
|
-
work: () => {
|
|
224
|
-
if (work === undefined) {
|
|
225
|
-
work = coreVm.sys_run('work').handle;
|
|
226
|
-
runClosuresTracker.registerRunClosure(work, () => new Promise(() => { }));
|
|
227
|
-
}
|
|
228
|
-
return work;
|
|
229
|
-
},
|
|
230
|
-
executeRun: (handle) => runClosuresTracker.executeRun(handle),
|
|
231
|
-
onError: (e) => executor.errorCallback(e),
|
|
232
|
-
});
|
|
233
|
-
// An own property, over the class's method: removing it gives the method back.
|
|
234
|
-
executor.doProgress = async (promise) => selector.register(promise);
|
|
235
|
-
return () => {
|
|
236
|
-
selector.close();
|
|
237
|
-
delete executor.doProgress;
|
|
238
|
-
// The work is over: so is its run, in the journal of an invocation that
|
|
239
|
-
// goes on to answer. (A VM that an error or a suspension closed refuses.)
|
|
240
|
-
if (work !== undefined) {
|
|
241
|
-
try {
|
|
242
|
-
coreVm.propose_run_completion_success(work, new Uint8Array());
|
|
243
|
-
}
|
|
244
|
-
catch {
|
|
245
|
-
// The attempt ended: the next one, or nothing, ends the run.
|
|
246
|
-
}
|
|
247
|
-
work = undefined;
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
//# sourceMappingURL=selector.js.map
|
package/dist/selector.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"selector.js","sourceRoot":"","sources":["../src/selector.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,oCAAoC;AACpC,EAAE;AACF,+EAA+E;AAC/E,uEAAuE;AACvE,2EAA2E;AAC3E,0EAA0E;AAC1E,+EAA+E;AAC/E,6EAA6E;AAC7E,8EAA8E;AAC9E,yEAAyE;AACzE,4EAA4E;AAC5E,iEAAiE;AACjE,EAAE;AACF,2EAA2E;AAC3E,6CAA6C;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAmBpD,oGAAoG;AACpG,MAAM,YAAY,GAAG,CAAC,CAAC;AAEvB,kFAAkF;AAClF,MAAM,iBAAiB,GAAG,CAAC,MAAwB,EAAW,EAAE,CAC9D,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,KAAK,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAE,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;AA4C3G;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAwE,CAAC;AAE1G;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,eAAe;IACT,OAAO,GAAG,IAAI,GAAG,EAAc,CAAC;IACzC,OAAO,GAAG,KAAK,CAAC;IAChB,MAAM,GAAG,KAAK,CAAC;IACvB,qEAAqE;IAC7D,OAAO,GAAG,KAAK,CAAC;IACxB,iFAAiF;IACzE,QAAQ,GAAG,KAAK,CAAC;IAER,MAAM,CAAi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AC,8BAA8B,CAAC,IAAI,EAAE,IAAI,UAAU,EAAE,CAAC,CAAC;YAChE,CAAC;YAAC,MAAM,CAAC;gBACP,6DAA6D;YAC/D,CAAC;YACD,IAAI,GAAG,SAAS,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/src/selector.ts
DELETED
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
// Concurrent awaits in a handler with SQL: one loop, the selector, drives
|
|
2
|
-
// every promise the handler awaits.
|
|
3
|
-
//
|
|
4
|
-
// The published SDK drives each awaited RestatePromise with a loop of its own,
|
|
5
|
-
// and only one of them may wait for progress from the server at a time
|
|
6
|
-
// (`ExternalProgressChannel.awaitNext`): two promises awaited at once that
|
|
7
|
-
// both need the server, such as two state reads with lazy state, fail the
|
|
8
|
-
// attempt with "awaitNext already pending". With journal replay, that order of
|
|
9
|
-
// awaits is part of what a replay must match; with explicit recovery nothing
|
|
10
|
-
// is replayed, so a handler with SQL hands every awaited promise to one loop,
|
|
11
|
-
// which asks the VM about all of them at once (`hookSelector`). The hook
|
|
12
|
-
// replaces the invocation's `promisesExecutor.doProgress`: it relies on the
|
|
13
|
-
// SDK's internals, as of the version this package pins (1.18.2).
|
|
14
|
-
//
|
|
15
|
-
// TODO: drop the selector, and the pin, once an SDK release lets a handler
|
|
16
|
-
// with explicit recovery await concurrently.
|
|
17
|
-
import { setImmediate } from 'node:timers/promises';
|
|
18
|
-
|
|
19
|
-
/** A future of the VM, as `do_progress` takes it. */
|
|
20
|
-
export type UnresolvedFuture =
|
|
21
|
-
| { Single: number }
|
|
22
|
-
| { FirstCompleted: UnresolvedFuture[] }
|
|
23
|
-
| { AllCompleted: UnresolvedFuture[] }
|
|
24
|
-
| { FirstSucceededOrAllFailed: UnresolvedFuture[] }
|
|
25
|
-
| { AllSucceededOrFirstFailed: UnresolvedFuture[] }
|
|
26
|
-
| { Unknown: UnresolvedFuture[] };
|
|
27
|
-
|
|
28
|
-
/** What `do_progress` returns. */
|
|
29
|
-
export type DoProgressResult =
|
|
30
|
-
| 'AnyCompleted'
|
|
31
|
-
| 'WaitExternalProgress'
|
|
32
|
-
| { ExecuteRun: number }
|
|
33
|
-
| 'CancelSignalReceived'
|
|
34
|
-
| { EphemeralNotificationReady: number };
|
|
35
|
-
|
|
36
|
-
/** The handle of the invocation's cancel signal: the VM's, and what `ctx.cancellation()` awaits. */
|
|
37
|
-
const CANCELLATION = 1;
|
|
38
|
-
|
|
39
|
-
/** Whether `future` waits for the invocation's cancellation, and nothing else. */
|
|
40
|
-
const cancellationAlone = (future: UnresolvedFuture): boolean =>
|
|
41
|
-
'Single' in future ? future.Single === CANCELLATION : Object.values(future)[0]!.every(cancellationAlone);
|
|
42
|
-
|
|
43
|
-
/** What the selector needs of a promise: the shape of InternalRestatePromise. */
|
|
44
|
-
export interface Selectable {
|
|
45
|
-
/** Takes the result if it is ready, and settles the promise with it. */
|
|
46
|
-
tryComplete(): Promise<void>;
|
|
47
|
-
/** What it still waits for, or null once settled. */
|
|
48
|
-
unresolvedFuture(): UnresolvedFuture | null;
|
|
49
|
-
/** Rejects it with a CancelledError. */
|
|
50
|
-
tryCancel(): void;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** What the selector needs of the invocation. */
|
|
54
|
-
export interface SelectorDriver {
|
|
55
|
-
/**
|
|
56
|
-
* Asks the VM for progress on `future`. With nothing ready, the VM tells
|
|
57
|
-
* Restate what the handler waits for, unless a run is executing; and once
|
|
58
|
-
* the input closed, it suspends the invocation on it (a throw), unless a
|
|
59
|
-
* run of the future is executing.
|
|
60
|
-
*/
|
|
61
|
-
doProgress(future: UnresolvedFuture): DoProgressResult;
|
|
62
|
-
/** Writes out what the VM has buffered. */
|
|
63
|
-
flushOutput(): Promise<void>;
|
|
64
|
-
/** Resolves on the next signal of external progress (input, a finished run, a registration). */
|
|
65
|
-
awaitExternalProgress(): Promise<void>;
|
|
66
|
-
/** Signals external progress. */
|
|
67
|
-
wake(): void;
|
|
68
|
-
/**
|
|
69
|
-
* The handle of the attempt's working run, made at the first call: a run
|
|
70
|
-
* that the VM hands out like any other (`ExecuteRun`) and that never ends
|
|
71
|
-
* while the attempt lasts. It stands for the handler's work outside Restate.
|
|
72
|
-
*/
|
|
73
|
-
work(): number;
|
|
74
|
-
executeRun(handle: number): void;
|
|
75
|
-
/** The attempt cannot go on: suspension, or an error. */
|
|
76
|
-
onError(e: unknown): void;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* The points of the loop where a test steps in: `pass`, as a pass starts,
|
|
81
|
-
* and `wait`, between a check that found nothing and the wait for progress.
|
|
82
|
-
*/
|
|
83
|
-
export type Failpoint = 'pass' | 'wait';
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* For tests: what every selector's loop awaits at each of its failpoints, to
|
|
87
|
-
* hold it there while something else happens (the input closes, a signal
|
|
88
|
-
* comes). At `wait`, it gets what the check asked for, and whether that was
|
|
89
|
-
* the invocation's cancellation alone (with the working run). Empty
|
|
90
|
-
* otherwise, and the loop then awaits nothing there.
|
|
91
|
-
*/
|
|
92
|
-
export const failpoints = new Map<Failpoint, (asked?: UnresolvedFuture, watching?: boolean) => unknown>();
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* One loop that drives every pending promise of an invocation, so that the
|
|
96
|
-
* invocation has a single await point however many promises user code awaits
|
|
97
|
-
* at once, with its own combinators or none.
|
|
98
|
-
*
|
|
99
|
-
* A promise is registered once and stays until it settles. Each pass of the
|
|
100
|
-
* loop settles what is ready, then asks the VM for progress on the first of
|
|
101
|
-
* all the pending promises to complete: so a suspension waits for any of
|
|
102
|
-
* them, a pending run starts whichever promise is awaited first, and a
|
|
103
|
-
* cancellation reaches all of them. Registering wakes the loop, so a promise
|
|
104
|
-
* registered while it waits is part of the next pass. The loop runs while
|
|
105
|
-
* there is something to ask the VM for, and is the only one waiting for
|
|
106
|
-
* external progress.
|
|
107
|
-
*
|
|
108
|
-
* To ask is to say that the handler waits: the VM tells Restate what for,
|
|
109
|
-
* and suspends the invocation on it once the input closed (Restate's
|
|
110
|
-
* inactivity timeout). That holds for every promise but one. A handler with
|
|
111
|
-
* explicit cancellation awaits its cancellation (`cancelled(ctx)`) to stop
|
|
112
|
-
* work it does outside Restate, a model's request, say: it watches for it,
|
|
113
|
-
* and does not wait for it. Reported as awaited, the cancel signal would be
|
|
114
|
-
* all Restate believes the handler waits for; and suspended on it, the
|
|
115
|
-
* invocation would end its attempt in the middle of that work, with a
|
|
116
|
-
* cancellation as the only thing to resume it.
|
|
117
|
-
*
|
|
118
|
-
* So with only the cancellation pending, the loop asks for it together with
|
|
119
|
-
* the attempt's working run (`SelectorDriver.work`), which says what is true:
|
|
120
|
-
* the handler is at work on something of its own, as inside `ctx.run`. While
|
|
121
|
-
* a run executes, the VM reports no await, and does not suspend on a future
|
|
122
|
-
* the run is part of, whether the input is open or closed; and it still
|
|
123
|
-
* finds the cancel signal when it comes. See `awaited`.
|
|
124
|
-
*/
|
|
125
|
-
export class PromiseSelector {
|
|
126
|
-
private readonly pending = new Set<Selectable>();
|
|
127
|
-
private running = false;
|
|
128
|
-
private closed = false;
|
|
129
|
-
// Between a check that found nothing and the next external progress.
|
|
130
|
-
private waiting = false;
|
|
131
|
-
// Whether the last check asked for the cancellation alone, with the working run.
|
|
132
|
-
private watching = false;
|
|
133
|
-
|
|
134
|
-
private readonly driver: SelectorDriver;
|
|
135
|
-
|
|
136
|
-
constructor(driver: SelectorDriver) {
|
|
137
|
-
this.driver = driver;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
register(promise: Selectable): void {
|
|
141
|
-
if (this.closed || this.pending.has(promise)) return;
|
|
142
|
-
this.pending.add(promise);
|
|
143
|
-
if (this.running) {
|
|
144
|
-
this.wake();
|
|
145
|
-
} else {
|
|
146
|
-
this.running = true;
|
|
147
|
-
void this.loop();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Stops the loop (it finds nothing pending), at the end of the attempt. */
|
|
152
|
-
close(): void {
|
|
153
|
-
if (this.closed) return;
|
|
154
|
-
this.closed = true;
|
|
155
|
-
this.pending.clear();
|
|
156
|
-
if (this.running) this.wake();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Only a waiting loop needs waking, once: any other picks up the change on
|
|
160
|
-
// its next pass. (Waking it otherwise would buffer a signal per promise, and
|
|
161
|
-
// as many needless passes, each of which tells Restate what is awaited.)
|
|
162
|
-
private wake(): void {
|
|
163
|
-
if (!this.waiting) return;
|
|
164
|
-
this.waiting = false;
|
|
165
|
-
this.driver.wake();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** The promises still pending, for tests. */
|
|
169
|
-
get size(): number {
|
|
170
|
-
return this.pending.size;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* What to ask the VM for: the first of the pending promises to complete,
|
|
175
|
-
* or undefined with nothing pending. With only the invocation's
|
|
176
|
-
* cancellation pending, the handler watches and waits for nothing: the
|
|
177
|
-
* working run goes with it (see the class).
|
|
178
|
-
*/
|
|
179
|
-
private awaited(): UnresolvedFuture | undefined {
|
|
180
|
-
const futures: UnresolvedFuture[] = [];
|
|
181
|
-
let watching = true; // for the cancellation, and waiting for nothing
|
|
182
|
-
for (const promise of this.pending) {
|
|
183
|
-
const future = promise.unresolvedFuture();
|
|
184
|
-
if (future === null) {
|
|
185
|
-
this.pending.delete(promise);
|
|
186
|
-
} else {
|
|
187
|
-
futures.push(future);
|
|
188
|
-
watching &&= cancellationAlone(future);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
this.watching = watching && futures.length > 0;
|
|
192
|
-
if (futures.length === 0) return undefined;
|
|
193
|
-
if (watching) futures.push({ Single: this.driver.work() });
|
|
194
|
-
return futures.length === 1 ? futures[0] : { FirstCompleted: futures };
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private async loop(): Promise<void> {
|
|
198
|
-
try {
|
|
199
|
-
await this.driver.flushOutput();
|
|
200
|
-
for (;;) {
|
|
201
|
-
if (failpoints.size > 0) await failpoints.get('pass')?.();
|
|
202
|
-
// Settle what is ready.
|
|
203
|
-
for (const promise of this.pending) {
|
|
204
|
-
await promise.tryComplete();
|
|
205
|
-
}
|
|
206
|
-
// A turn of the event loop: what was settled runs, and registers
|
|
207
|
-
// what it awaits next, so that one check covers that too (a
|
|
208
|
-
// combinator also flags itself completed in a .finally, a tick late).
|
|
209
|
-
// An optimization: what is registered later wakes the loop anyway.
|
|
210
|
-
await setImmediate();
|
|
211
|
-
// From here to the end of the pass, or to `waiting`, in one turn:
|
|
212
|
-
// what is registered after the check finds the loop gone, and starts
|
|
213
|
-
// another, or finds it waiting, and wakes it.
|
|
214
|
-
const future = this.awaited();
|
|
215
|
-
if (future === undefined) return;
|
|
216
|
-
const result = this.driver.doProgress(future);
|
|
217
|
-
if (result === 'AnyCompleted') {
|
|
218
|
-
continue;
|
|
219
|
-
} else if (result === 'WaitExternalProgress') {
|
|
220
|
-
this.waiting = true;
|
|
221
|
-
if (failpoints.size > 0) await failpoints.get('wait')?.(future, this.watching);
|
|
222
|
-
await this.driver.flushOutput();
|
|
223
|
-
await this.driver.awaitExternalProgress();
|
|
224
|
-
this.waiting = false;
|
|
225
|
-
} else if (result === 'CancelSignalReceived') {
|
|
226
|
-
// Reaches every pending promise; what is awaited after it is not cancelled.
|
|
227
|
-
for (const promise of this.pending) promise.tryCancel();
|
|
228
|
-
this.pending.clear();
|
|
229
|
-
} else if ('ExecuteRun' in result) {
|
|
230
|
-
// It starts in the turn the next pass takes before its check.
|
|
231
|
-
this.driver.executeRun(result.ExecuteRun);
|
|
232
|
-
} else {
|
|
233
|
-
// An ephemeral command's notification: a handler with SQL makes none.
|
|
234
|
-
throw new Error(`@restatedev/sessions: unexpected progress from the VM: ${JSON.stringify(result)}`);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
} catch (e) {
|
|
238
|
-
this.close();
|
|
239
|
-
this.driver.onError(e);
|
|
240
|
-
} finally {
|
|
241
|
-
this.running = false;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/** The SDK's promises executor, as far as the hook needs it: its fields are private in its types. */
|
|
247
|
-
interface Executor {
|
|
248
|
-
doProgress(promise: Selectable): Promise<void>;
|
|
249
|
-
coreVm: {
|
|
250
|
-
do_progress(future: UnresolvedFuture): DoProgressResult;
|
|
251
|
-
sys_run(name: string): { handle: number };
|
|
252
|
-
propose_run_completion_success(handle: number, value: Uint8Array): void;
|
|
253
|
-
};
|
|
254
|
-
outputPump: { awaitNextProgress(): Promise<void> };
|
|
255
|
-
externalProgressChannel: { awaitNext(): Promise<void>; signal(): void };
|
|
256
|
-
runClosuresTracker: {
|
|
257
|
-
executeRun(handle: number): void;
|
|
258
|
-
registerRunClosure(handle: number, run: () => Promise<unknown>): void;
|
|
259
|
-
};
|
|
260
|
-
errorCallback: (e: unknown) => void;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const FIELDS = ['coreVm', 'outputPump', 'externalProgressChannel', 'runClosuresTracker', 'errorCallback'] as const;
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Drive every promise that the invocation of `ctx` (the SDK's context, not a
|
|
267
|
-
* proxy of it) awaits from here on with one selector. The returned function
|
|
268
|
-
* stops it, at the end of the attempt, and gives the invocation back its
|
|
269
|
-
* own loops.
|
|
270
|
-
*/
|
|
271
|
-
export function hookSelector(ctx: object): () => void {
|
|
272
|
-
const executor = (ctx as { promisesExecutor?: Executor }).promisesExecutor;
|
|
273
|
-
const missing: string[] = executor ? FIELDS.filter((field) => executor[field] === undefined) : ['promisesExecutor'];
|
|
274
|
-
if (missing.length === 0) {
|
|
275
|
-
const { coreVm, runClosuresTracker } = executor!;
|
|
276
|
-
for (const method of ['do_progress', 'sys_run', 'propose_run_completion_success'] as const) {
|
|
277
|
-
if (typeof coreVm[method] !== 'function') missing.push(`coreVm.${method}`);
|
|
278
|
-
}
|
|
279
|
-
if (typeof runClosuresTracker.registerRunClosure !== 'function')
|
|
280
|
-
missing.push('runClosuresTracker.registerRunClosure');
|
|
281
|
-
}
|
|
282
|
-
if (missing.length > 0 || executor === undefined) {
|
|
283
|
-
throw new Error(
|
|
284
|
-
`@restatedev/sessions drives a handler's promises through the SDK's internals, which this SDK version does not have ` +
|
|
285
|
-
`(${missing.join(', ')}): use the @restatedev/restate-sdk that @restatedev/sessions pins`,
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
const { coreVm, outputPump, externalProgressChannel, runClosuresTracker } = executor;
|
|
289
|
-
// The working run: journaled as a run named `work`, with no closure of its
|
|
290
|
-
// own to end it. It ends with the attempt.
|
|
291
|
-
let work: number | undefined;
|
|
292
|
-
const selector = new PromiseSelector({
|
|
293
|
-
doProgress: (future) => coreVm.do_progress(future),
|
|
294
|
-
flushOutput: () => outputPump.awaitNextProgress(),
|
|
295
|
-
awaitExternalProgress: () => externalProgressChannel.awaitNext(),
|
|
296
|
-
wake: () => externalProgressChannel.signal(),
|
|
297
|
-
work: () => {
|
|
298
|
-
if (work === undefined) {
|
|
299
|
-
work = coreVm.sys_run('work').handle;
|
|
300
|
-
runClosuresTracker.registerRunClosure(work, () => new Promise(() => {}));
|
|
301
|
-
}
|
|
302
|
-
return work;
|
|
303
|
-
},
|
|
304
|
-
executeRun: (handle) => runClosuresTracker.executeRun(handle),
|
|
305
|
-
onError: (e) => executor.errorCallback(e),
|
|
306
|
-
});
|
|
307
|
-
// An own property, over the class's method: removing it gives the method back.
|
|
308
|
-
executor.doProgress = async (promise) => selector.register(promise);
|
|
309
|
-
return () => {
|
|
310
|
-
selector.close();
|
|
311
|
-
delete (executor as Partial<Executor>).doProgress;
|
|
312
|
-
// The work is over: so is its run, in the journal of an invocation that
|
|
313
|
-
// goes on to answer. (A VM that an error or a suspension closed refuses.)
|
|
314
|
-
if (work !== undefined) {
|
|
315
|
-
try {
|
|
316
|
-
coreVm.propose_run_completion_success(work, new Uint8Array());
|
|
317
|
-
} catch {
|
|
318
|
-
// The attempt ended: the next one, or nothing, ends the run.
|
|
319
|
-
}
|
|
320
|
-
work = undefined;
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
}
|