@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/dist/driver.js
CHANGED
|
@@ -1,44 +1,75 @@
|
|
|
1
|
-
// One attempt of a session object's handler, on the explicit recovery
|
|
2
|
-
// (`vm.ts`, `native/src/vm.rs`).
|
|
1
|
+
// One attempt of a session object's handler, on the explicit recovery
|
|
2
|
+
// protocol's VM (`vm.ts`, `native/src/vm.rs`).
|
|
3
3
|
//
|
|
4
|
-
// The
|
|
5
|
-
//
|
|
4
|
+
// The protocol has no journal: every attempt runs the handler from its first
|
|
5
|
+
// line, and recovers from the state the earlier attempts stored. An action
|
|
6
6
|
// returns a handle, and its result comes back as a notification of that
|
|
7
|
-
// handle, which settles the action's promise here.
|
|
8
|
-
//
|
|
7
|
+
// handle, which settles the action's promise here. So the handler can await
|
|
8
|
+
// actions in any combination (`Promise.all`, `Promise.race`).
|
|
9
9
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
10
|
+
// Two things move the attempt forward:
|
|
11
|
+
// - The input pump (handler.ts) is the only reader of the request body. It
|
|
12
|
+
// passes each chunk, and the end of the body, to the VM.
|
|
13
|
+
// - The tick runs after every call into the VM, at most once per microtask
|
|
14
|
+
// turn. It writes the VM's output, settles the promises of the actions that
|
|
15
|
+
// completed, and notices the end of the attempt. It is the only place
|
|
16
|
+
// notifications are taken: taking one is processing it, and it counts in
|
|
17
|
+
// the `ShutdownMessage` of an attempt shut down. The cancel signal is the
|
|
18
|
+
// notification of the reserved handle 1.
|
|
18
19
|
//
|
|
19
20
|
// Restate shuts an attempt down by closing the request body, after its
|
|
20
|
-
// inactivity timeout
|
|
21
|
-
//
|
|
22
|
-
//
|
|
21
|
+
// inactivity timeout; the VM answers with `ShutdownMessage` at once. From then
|
|
22
|
+
// on every action throws, pending promises reject with the reason, the stop
|
|
23
|
+
// signal and `request.attemptCompletedSignal` abort, and Restate decides what
|
|
24
|
+
// follows: it suspends the invocation if a result is outstanding, and starts
|
|
25
|
+
// a new attempt at once otherwise. After the attempt's last message Restate
|
|
26
|
+
// closes its side first, and the response closes after it (`whenClosable`). A
|
|
27
|
+
// process going away ends its attempts with an unasked `ShutdownMessage`
|
|
28
|
+
// (`goAway`).
|
|
29
|
+
//
|
|
30
|
+
// Heartbeats keep Restate from shutting the attempt down while a keep-alive
|
|
31
|
+
// scope is open (see stop.ts). One timer serves them and outlives a run of
|
|
32
|
+
// scopes, so the per-statement database keep-alive does not arm and clear a
|
|
33
|
+
// timer each time. It re-arms only while a scope is open, and beats only after
|
|
34
|
+
// a full interval with nothing written: silence never reaches twice that.
|
|
35
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
23
36
|
import * as restate from '@restatedev/restate-sdk';
|
|
24
|
-
import {
|
|
37
|
+
import { Stop } from './stop.js';
|
|
38
|
+
import { invalidInput, isShutDown, } from './vm.js';
|
|
39
|
+
/** Restate's inactivity timeout when neither the handler nor its object sets one: the server's default. */
|
|
40
|
+
export const DEFAULT_INACTIVITY_TIMEOUT_MS = 60_000;
|
|
41
|
+
/** The longest delay a timer holds: Node runs a longer one after 1 ms. */
|
|
42
|
+
export const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
25
43
|
/**
|
|
26
|
-
* How often an attempt
|
|
27
|
-
* milliseconds: RESTATE_SESSIONS_HEARTBEAT
|
|
44
|
+
* How often an attempt with a keep-alive scope open tells Restate it makes
|
|
45
|
+
* progress, in milliseconds: RESTATE_SESSIONS_HEARTBEAT if it is a positive
|
|
46
|
+
* number, else a third of the handler's inactivity timeout
|
|
47
|
+
* (`inactivityTimeoutMs`, a minute when it sets none); never more than a
|
|
48
|
+
* timer holds ({@link MAX_TIMER_MS}).
|
|
28
49
|
*
|
|
29
50
|
* Restate shuts down an attempt it has heard nothing from for its inactivity
|
|
30
|
-
* timeout, a
|
|
31
|
-
*
|
|
32
|
-
*
|
|
51
|
+
* timeout, and a third of it leaves room for a late beat. A timeout set only
|
|
52
|
+
* on the server, or on the endpoint's default service options, is not seen
|
|
53
|
+
* here: a deployment that shortens it there must shorten this with it.
|
|
33
54
|
*/
|
|
34
|
-
function heartbeatMs() {
|
|
55
|
+
export function heartbeatMs(inactivityTimeoutMs) {
|
|
35
56
|
const set = Number(process.env.RESTATE_SESSIONS_HEARTBEAT);
|
|
36
|
-
|
|
57
|
+
if (Number.isFinite(set) && set > 0)
|
|
58
|
+
return Math.min(set, MAX_TIMER_MS);
|
|
59
|
+
const timeout = inactivityTimeoutMs !== undefined && Number.isFinite(inactivityTimeoutMs) && inactivityTimeoutMs > 0
|
|
60
|
+
? inactivityTimeoutMs
|
|
61
|
+
: DEFAULT_INACTIVITY_TIMEOUT_MS;
|
|
62
|
+
return Math.min(MAX_TIMER_MS, Math.max(1, Math.floor(timeout / 3)));
|
|
37
63
|
}
|
|
38
64
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
65
|
+
* A value user code passed to an action is one Restate would refuse as
|
|
66
|
+
* malformed (an invocation id that is not one, an empty idempotency key), as
|
|
67
|
+
* the VM checks it (code 400). That action fails alone: nothing was sent, the
|
|
68
|
+
* attempt goes on, and user code may catch it as any `TerminalError`.
|
|
41
69
|
*/
|
|
70
|
+
export class InvalidInput extends restate.TerminalError {
|
|
71
|
+
}
|
|
72
|
+
/** Restate shut the attempt down. A new attempt runs the handler from the start. */
|
|
42
73
|
export class AttemptShutDown extends Error {
|
|
43
74
|
constructor() {
|
|
44
75
|
super('Restate shut the attempt down; a new attempt will run the handler from the start');
|
|
@@ -52,7 +83,7 @@ export class AttemptFailed extends Error {
|
|
|
52
83
|
this.name = 'AttemptFailed';
|
|
53
84
|
}
|
|
54
85
|
}
|
|
55
|
-
/** A failed action
|
|
86
|
+
/** A failed action as the SDK's `TerminalError`, so that user code catches it. */
|
|
56
87
|
function terminal(failure) {
|
|
57
88
|
return new restate.TerminalError(failure.message, {
|
|
58
89
|
errorCode: failure.code,
|
|
@@ -60,16 +91,13 @@ function terminal(failure) {
|
|
|
60
91
|
});
|
|
61
92
|
}
|
|
62
93
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* — a race it lost, say — would otherwise bring the process down. Attaching a
|
|
66
|
-
* handler marks the promise handled; the caller still sees the rejection.
|
|
94
|
+
* Mark `promise` handled: the end of the attempt rejects every pending promise,
|
|
95
|
+
* and one nobody awaits (a lost race) would crash the process.
|
|
67
96
|
*/
|
|
68
|
-
function quiet(promise) {
|
|
97
|
+
export function quiet(promise) {
|
|
69
98
|
promise.catch(() => { });
|
|
70
99
|
return promise;
|
|
71
100
|
}
|
|
72
|
-
/** What a notification settles its promise with. */
|
|
73
101
|
function settled(notification) {
|
|
74
102
|
switch (notification.kind) {
|
|
75
103
|
case 'void':
|
|
@@ -83,15 +111,10 @@ function settled(notification) {
|
|
|
83
111
|
case 'invocationId':
|
|
84
112
|
return { value: notification.id };
|
|
85
113
|
default:
|
|
86
|
-
//
|
|
87
|
-
// whose promise stays pending forever.
|
|
114
|
+
// An unknown kind settles with no value rather than leaving the promise pending forever.
|
|
88
115
|
return { value: undefined };
|
|
89
116
|
}
|
|
90
117
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Drives one attempt over its VM: the input pump, the tick, and the promises
|
|
93
|
-
* of the actions it issued.
|
|
94
|
-
*/
|
|
95
118
|
export class AttemptDriver {
|
|
96
119
|
vm;
|
|
97
120
|
write;
|
|
@@ -99,65 +122,43 @@ export class AttemptDriver {
|
|
|
99
122
|
/** Set once the attempt is over, to what ended it. */
|
|
100
123
|
ended;
|
|
101
124
|
waiting = new Map();
|
|
102
|
-
|
|
125
|
+
onClosable = new Set();
|
|
126
|
+
isClosable = false;
|
|
103
127
|
queued = false;
|
|
104
|
-
/** The
|
|
128
|
+
/** The reserved handle of the cancel signal. */
|
|
129
|
+
cancelHandle;
|
|
130
|
+
/** The span the code running now is inside of: actions it makes are inside it too. */
|
|
131
|
+
span = new AsyncLocalStorage();
|
|
132
|
+
/** The calls this attempt made, keyed by the handle of their result. */
|
|
105
133
|
calls = new Map();
|
|
106
|
-
/** Issuing the cancellations of the calls
|
|
134
|
+
/** Issuing the cancellations of the calls; the handler's end waits for it. */
|
|
107
135
|
cancelling;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
* next thing it does await: a result of Restate's ({@link promise}), or a
|
|
112
|
-
* call of its database ({@link takeCarriedCancellation}). A retry of a
|
|
113
|
-
* cancelled invocation knows it was cancelled before the handler starts,
|
|
114
|
-
* and its first call has to fail all the same, rather than run to
|
|
115
|
-
* completion.
|
|
116
|
-
*
|
|
117
|
-
* There is one, and it is here. The addon once carried a second for the
|
|
118
|
-
* database's waits, and the two could not agree on who had delivered: a
|
|
119
|
-
* handler saw one cancellation twice.
|
|
120
|
-
*/
|
|
121
|
-
carriedCancellation = false;
|
|
122
|
-
cancelledPromise;
|
|
123
|
-
/**
|
|
124
|
-
* The one timer of the heartbeats, armed for `beatDue`. It outlives a run of
|
|
125
|
-
* scopes, so the database's scopes (one a statement) do not start and clear
|
|
126
|
-
* a timer each: it fires, finds no scope open, and is not armed again.
|
|
127
|
-
*/
|
|
136
|
+
cancellationIn = false;
|
|
137
|
+
/** The one cancellation, stop signal and keep-alive scopes of this attempt: see stop.ts. */
|
|
138
|
+
stop = new Stop(() => this.startHeartbeats());
|
|
128
139
|
heartbeats;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
/** The run of scopes now open: when the first opened, and its interval. */
|
|
133
|
-
runEvery = 0;
|
|
134
|
-
/** When the run's next heartbeat is due. */
|
|
135
|
-
nextBeat = 0;
|
|
136
|
-
/** The heartbeat interval, read from the environment on first use. */
|
|
137
|
-
beatEvery;
|
|
138
|
-
/** When the attempt last wrote to Restate, which restarts Restate's inactivity timer. */
|
|
140
|
+
/** The heartbeat interval, read once as the attempt starts (see {@link heartbeatMs}). */
|
|
141
|
+
interval;
|
|
142
|
+
/** Any write restarts Restate's inactivity timer. */
|
|
139
143
|
wroteAt = Date.now();
|
|
140
144
|
aborter = new AbortController();
|
|
141
|
-
/**
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*/
|
|
145
|
+
/** The request's `attemptCompletedSignal`: aborts with what ended the attempt. */
|
|
146
|
+
completed = this.aborter.signal;
|
|
147
|
+
/** Read once: taking the input copies the body across the addon. */
|
|
145
148
|
theInput;
|
|
146
149
|
constructor(vm, write, options) {
|
|
147
150
|
this.vm = vm;
|
|
148
151
|
this.write = write;
|
|
149
152
|
this.options = options;
|
|
150
|
-
this.
|
|
151
|
-
|
|
152
|
-
});
|
|
153
|
+
this.cancelHandle = vm.cancelHandle();
|
|
154
|
+
this.interval = heartbeatMs(options.inactivityTimeoutMs);
|
|
153
155
|
}
|
|
154
156
|
// --- Input and output
|
|
155
|
-
/** A chunk of the request body. */
|
|
156
157
|
notifyInput(chunk) {
|
|
157
158
|
this.vm.notifyInput(chunk);
|
|
158
159
|
this.schedule();
|
|
159
160
|
}
|
|
160
|
-
/** The end of the request body
|
|
161
|
+
/** The end of the request body. While the handler runs, this means Restate shut the attempt down. */
|
|
161
162
|
notifyInputClosed() {
|
|
162
163
|
this.vm.notifyInputClosed();
|
|
163
164
|
this.schedule();
|
|
@@ -165,48 +166,86 @@ export class AttemptDriver {
|
|
|
165
166
|
isReadyToExecute() {
|
|
166
167
|
return this.vm.isReadyToExecute();
|
|
167
168
|
}
|
|
168
|
-
/**
|
|
169
|
-
* Schedule a tick from outside: what the VM calls after the database's store
|
|
170
|
-
* acts on it from the runtime's thread, so that what it wrote is written and
|
|
171
|
-
* what it waits for is routed to it.
|
|
172
|
-
*/
|
|
169
|
+
/** Called by the VM after the database store acted on it from the runtime thread. */
|
|
173
170
|
scheduleTick() {
|
|
174
171
|
this.schedule();
|
|
175
172
|
}
|
|
176
|
-
/** Schedule a tick, once per turn of the event loop. */
|
|
177
173
|
schedule() {
|
|
178
174
|
if (this.queued)
|
|
179
175
|
return;
|
|
180
176
|
this.queued = true;
|
|
181
177
|
queueMicrotask(() => (this.ended === undefined ? this.tick() : this.flush()));
|
|
182
178
|
}
|
|
183
|
-
/**
|
|
179
|
+
/**
|
|
180
|
+
* Write what the VM has buffered. A closed attempt still flushes its last
|
|
181
|
+
* output, and then, once Restate closed its side too, the response may close.
|
|
182
|
+
*/
|
|
184
183
|
flush() {
|
|
185
184
|
this.queued = false;
|
|
186
185
|
const output = this.vm.takeOutput();
|
|
187
|
-
if (output.length
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
186
|
+
if (output.length > 0) {
|
|
187
|
+
this.write(output);
|
|
188
|
+
this.wroteAt = Date.now();
|
|
189
|
+
}
|
|
190
|
+
if (this.vm.shouldCloseOutput())
|
|
191
|
+
this.closable();
|
|
192
|
+
}
|
|
193
|
+
/** The response may close now: tell the listeners, once. */
|
|
194
|
+
closable() {
|
|
195
|
+
const listeners = [...this.onClosable];
|
|
196
|
+
this.onClosable.clear();
|
|
197
|
+
this.isClosable = true;
|
|
198
|
+
for (const listener of listeners)
|
|
199
|
+
listener();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Call `listener` once the response may close: the attempt's last message
|
|
203
|
+
* is written and Restate closed its side (Restate closes first).
|
|
204
|
+
*/
|
|
205
|
+
whenClosable(listener) {
|
|
206
|
+
if (this.isClosable)
|
|
207
|
+
listener();
|
|
208
|
+
else
|
|
209
|
+
this.onClosable.add(listener);
|
|
191
210
|
}
|
|
192
211
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
212
|
+
* The process is going away (SIGTERM): end a running attempt with an
|
|
213
|
+
* unasked `ShutdownMessage`, after which Restate starts a new attempt
|
|
214
|
+
* elsewhere, and write it. The caller closes the response.
|
|
195
215
|
*/
|
|
216
|
+
goAway() {
|
|
217
|
+
if (this.vm.state() !== 'closed')
|
|
218
|
+
this.vm.shutdown();
|
|
219
|
+
this.tick();
|
|
220
|
+
this.end(this.ended ?? new AttemptShutDown());
|
|
221
|
+
this.flush();
|
|
222
|
+
}
|
|
196
223
|
tick() {
|
|
197
224
|
this.queued = false;
|
|
198
225
|
this.flush();
|
|
199
226
|
if (this.ended !== undefined)
|
|
200
227
|
return;
|
|
228
|
+
// The actions of an atomic group the store aborted in the addon: never notified.
|
|
229
|
+
for (const handle of this.vm.takeAborted()) {
|
|
230
|
+
const waiting = this.waiting.get(handle);
|
|
231
|
+
if (waiting === undefined)
|
|
232
|
+
continue;
|
|
233
|
+
this.waiting.delete(handle);
|
|
234
|
+
waiting.reject(new Error('the atomic group of this action was aborted'));
|
|
235
|
+
}
|
|
201
236
|
let notification;
|
|
202
237
|
try {
|
|
203
238
|
while ((notification = this.vm.takeNotification()) !== null) {
|
|
239
|
+
if (notification.handle === this.cancelHandle) {
|
|
240
|
+
this.cancelled();
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
204
243
|
// A call is over once its result arrives, whatever it is.
|
|
205
244
|
const call = this.calls.get(notification.handle);
|
|
206
245
|
if (call !== undefined)
|
|
207
246
|
call.done = true;
|
|
208
247
|
const waiting = this.waiting.get(notification.handle);
|
|
209
|
-
// A handle
|
|
248
|
+
// A handle nothing waits for (a send's invocation id), or settled already.
|
|
210
249
|
if (waiting === undefined)
|
|
211
250
|
continue;
|
|
212
251
|
this.waiting.delete(notification.handle);
|
|
@@ -219,16 +258,16 @@ export class AttemptDriver {
|
|
|
219
258
|
}
|
|
220
259
|
catch (error) {
|
|
221
260
|
this.end(isShutDown(error) ? new AttemptShutDown() : new AttemptFailed(String(error)));
|
|
261
|
+
this.flush();
|
|
222
262
|
return;
|
|
223
263
|
}
|
|
224
|
-
//
|
|
225
|
-
//
|
|
264
|
+
// Cancelled before this attempt (`StartMessage.cancelled`): the VM knows
|
|
265
|
+
// from the start, and no notification comes.
|
|
226
266
|
if (this.vm.isCancelled())
|
|
227
267
|
this.cancelled();
|
|
228
268
|
if (this.vm.state() === 'closed')
|
|
229
269
|
this.end(new AttemptShutDown());
|
|
230
270
|
}
|
|
231
|
-
/** The attempt ended: fail what still waits, and tell the listeners. */
|
|
232
271
|
end(reason) {
|
|
233
272
|
if (this.ended !== undefined)
|
|
234
273
|
return;
|
|
@@ -238,108 +277,55 @@ export class AttemptDriver {
|
|
|
238
277
|
for (const one of waiting)
|
|
239
278
|
one.reject(reason);
|
|
240
279
|
this.stopHeartbeats();
|
|
241
|
-
// Nothing will answer the database's
|
|
242
|
-
//
|
|
280
|
+
// Nothing will answer the database's pending reads now. The scheduler
|
|
281
|
+
// would keep this driver and the VM alive through the addon.
|
|
243
282
|
this.vm.endStateWaits();
|
|
244
283
|
this.vm.clearScheduler();
|
|
284
|
+
// After `ended` is set, so nothing the stopping work does is stored. Before the request's signal, so a call
|
|
285
|
+
// its listeners make is not failed by an owed cancellation.
|
|
286
|
+
this.stop.end(reason);
|
|
245
287
|
this.aborter.abort(reason);
|
|
246
|
-
const listeners = [...this.onEnd];
|
|
247
|
-
this.onEnd.clear();
|
|
248
|
-
for (const listener of listeners)
|
|
249
|
-
listener();
|
|
250
288
|
}
|
|
251
|
-
/** Whether the attempt is over, and with what. */
|
|
252
289
|
get over() {
|
|
253
290
|
return this.ended;
|
|
254
291
|
}
|
|
255
|
-
/** Call `listener` when the attempt ends; the returned function unregisters it. */
|
|
256
|
-
whenEnded(listener) {
|
|
257
|
-
if (this.ended !== undefined) {
|
|
258
|
-
listener();
|
|
259
|
-
return () => { };
|
|
260
|
-
}
|
|
261
|
-
this.onEnd.add(listener);
|
|
262
|
-
return () => this.onEnd.delete(listener);
|
|
263
|
-
}
|
|
264
292
|
// --- Cancellation
|
|
265
|
-
/**
|
|
266
|
-
* Restate cancelled the invocation. A handler with `explicitCancellation`
|
|
267
|
-
* stops its own work and nothing else happens; otherwise the calls in flight
|
|
268
|
-
* are cancelled and every await of Restate fails, as a cancelled
|
|
269
|
-
* invocation's do.
|
|
270
|
-
*/
|
|
293
|
+
/** Restate cancelled the invocation: cancel the calls in flight and reject pending results (see stop.ts). */
|
|
271
294
|
cancelled() {
|
|
272
|
-
|
|
273
|
-
this.cancelledResolve?.();
|
|
274
|
-
this.cancelledResolve = undefined;
|
|
275
|
-
if (!first || this.options.explicitCancellation)
|
|
295
|
+
if (this.cancellationIn)
|
|
276
296
|
return;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
//
|
|
283
|
-
//
|
|
284
|
-
//
|
|
297
|
+
this.cancellationIn = true;
|
|
298
|
+
if (this.options.explicitCancellation) {
|
|
299
|
+
this.stop.cancel({ signalOnly: true });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
// Database waits in the addon are left to finish: Restate answers them promptly, and failing
|
|
303
|
+
// the result of a commit's group already sent would report a landed commit as cancelled.
|
|
304
|
+
// `serveSession` holds the handler's end until the cancellations are issued.
|
|
285
305
|
this.cancelling = quiet(this.cancelPreviousCalls().then(() => undefined));
|
|
286
306
|
const failure = new restate.CancelledError();
|
|
287
|
-
// Deleting the entry
|
|
288
|
-
// handlers in a later microtask, so nothing is added
|
|
307
|
+
// Deleting the current entry during iteration is safe, and rejection
|
|
308
|
+
// handlers run in a later microtask, so nothing is added meanwhile.
|
|
289
309
|
let failedAResult = false;
|
|
290
310
|
for (const [handle, waiting] of this.waiting) {
|
|
291
|
-
|
|
292
|
-
if (waiting.waits === 'callId')
|
|
311
|
+
if (waiting.spared)
|
|
293
312
|
continue;
|
|
294
|
-
|
|
295
|
-
failedAResult = true;
|
|
313
|
+
failedAResult = true;
|
|
296
314
|
this.waiting.delete(handle);
|
|
297
315
|
waiting.reject(failure);
|
|
298
316
|
}
|
|
299
|
-
|
|
300
|
-
// in `waiting` and is rejected here, but `genericSend` voided it, so the
|
|
301
|
-
// handler saw no cancellation and is still owed its one.
|
|
302
|
-
this.carriedCancellation = !failedAResult;
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Whether a cancellation is owed to the handler's next await, which the
|
|
306
|
-
* caller then delivers: the handler's call of its database, before it
|
|
307
|
-
* issues anything, so that a call that fails with the cancellation did
|
|
308
|
-
* nothing.
|
|
309
|
-
*/
|
|
310
|
-
takeCarriedCancellation() {
|
|
311
|
-
// The attempt is over: what the handler does then fails with that, as
|
|
312
|
-
// its awaits of Restate do, and the attempt that follows gets the
|
|
313
|
-
// cancellation.
|
|
314
|
-
if (this.ended !== undefined)
|
|
315
|
-
return false;
|
|
316
|
-
const carried = this.carriedCancellation;
|
|
317
|
-
this.carriedCancellation = false;
|
|
318
|
-
return carried;
|
|
317
|
+
this.stop.cancel({ failedAnAwait: failedAResult });
|
|
319
318
|
}
|
|
320
|
-
/** Issuing the cancellations of the calls, if a cancellation started them. */
|
|
321
319
|
get cancellingCalls() {
|
|
322
320
|
return this.cancelling;
|
|
323
321
|
}
|
|
324
|
-
/** Resolves once a cancellation arrived, in this attempt or an earlier one. */
|
|
325
|
-
cancellation() {
|
|
326
|
-
if (this.vm.isCancelled())
|
|
327
|
-
this.cancelled();
|
|
328
|
-
return this.cancelledPromise;
|
|
329
|
-
}
|
|
330
|
-
isCancelled() {
|
|
331
|
-
return this.vm.isCancelled();
|
|
332
|
-
}
|
|
333
322
|
/**
|
|
334
|
-
* Cancel the calls
|
|
335
|
-
*
|
|
336
|
-
* made, so Restate will answer with its id, and leaving it uncancelled would
|
|
337
|
-
* leave an invocation running that nothing waits for any more.
|
|
323
|
+
* Cancel the calls that have not answered. A call whose id has not arrived
|
|
324
|
+
* is waited for: an uncancelled callee would run on with nothing waiting.
|
|
338
325
|
*/
|
|
339
326
|
async cancelPreviousCalls() {
|
|
340
327
|
const cancelled = [];
|
|
341
|
-
// A snapshot:
|
|
342
|
-
// own business, not one this cancellation set out to stop.
|
|
328
|
+
// A snapshot: a call made while this loop awaits is not one this cancellation set out to stop.
|
|
343
329
|
for (const call of Array.from(this.calls.values())) {
|
|
344
330
|
if (call.done || call.cancelled)
|
|
345
331
|
continue;
|
|
@@ -359,7 +345,7 @@ export class AttemptDriver {
|
|
|
359
345
|
return cancelled;
|
|
360
346
|
}
|
|
361
347
|
// --- Actions
|
|
362
|
-
/** Call the VM
|
|
348
|
+
/** Call the VM, then schedule a tick. Throws once the attempt has ended, and an invalid input as a TerminalError. */
|
|
363
349
|
act(act) {
|
|
364
350
|
if (this.ended !== undefined)
|
|
365
351
|
throw this.ended;
|
|
@@ -371,30 +357,32 @@ export class AttemptDriver {
|
|
|
371
357
|
this.end(new AttemptShutDown());
|
|
372
358
|
throw this.ended;
|
|
373
359
|
}
|
|
360
|
+
const refused = invalidInput(error);
|
|
361
|
+
if (refused !== undefined)
|
|
362
|
+
throw new InvalidInput(refused.message, { errorCode: refused.code });
|
|
374
363
|
throw error;
|
|
375
364
|
}
|
|
376
365
|
finally {
|
|
377
366
|
this.schedule();
|
|
378
367
|
}
|
|
379
368
|
}
|
|
380
|
-
|
|
381
|
-
promise(handle, waits = 'result') {
|
|
369
|
+
promise(handle, spared = false) {
|
|
382
370
|
if (this.ended !== undefined)
|
|
383
371
|
return quiet(Promise.reject(this.ended));
|
|
384
|
-
// Only a result takes it: see {@link Waits}.
|
|
385
|
-
if (this.carriedCancellation && waits === 'result') {
|
|
386
|
-
this.carriedCancellation = false;
|
|
387
|
-
return quiet(Promise.reject(new restate.CancelledError()));
|
|
388
|
-
}
|
|
389
372
|
return quiet(new Promise((resolve, reject) => {
|
|
390
|
-
this.waiting.set(handle, { resolve, reject,
|
|
373
|
+
this.waiting.set(handle, { resolve, reject, spared });
|
|
391
374
|
}));
|
|
392
375
|
}
|
|
393
376
|
/**
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
|
|
397
|
-
|
|
377
|
+
* Whether an owed cancellation fails a wait on Restate before it is made
|
|
378
|
+
* (see stop.ts): once made, nothing would cancel what it started.
|
|
379
|
+
*/
|
|
380
|
+
owed() {
|
|
381
|
+
return this.ended === undefined && this.stop.take();
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* A fire-and-forget action. An ended attempt drops it, and a retry makes it
|
|
385
|
+
* again; an invalid input throws, for user code to catch.
|
|
398
386
|
*/
|
|
399
387
|
tell(act) {
|
|
400
388
|
if (this.ended !== undefined)
|
|
@@ -402,12 +390,28 @@ export class AttemptDriver {
|
|
|
402
390
|
try {
|
|
403
391
|
this.act(act);
|
|
404
392
|
}
|
|
405
|
-
catch {
|
|
406
|
-
|
|
393
|
+
catch (error) {
|
|
394
|
+
if (error instanceof InvalidInput)
|
|
395
|
+
throw error;
|
|
396
|
+
// Otherwise the attempt ended meanwhile.
|
|
407
397
|
}
|
|
408
398
|
}
|
|
409
|
-
/**
|
|
399
|
+
/**
|
|
400
|
+
* A wait on Restate that takes a cancellation as a call's result does (a
|
|
401
|
+
* signal read), and its promise: an owed cancellation fails it before it is
|
|
402
|
+
* made, and a cancellation rejects it while it waits.
|
|
403
|
+
*/
|
|
410
404
|
issue(act) {
|
|
405
|
+
if (this.owed())
|
|
406
|
+
return quiet(Promise.reject(new restate.CancelledError()));
|
|
407
|
+
return this.ask(act, { cancellable: true });
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* An action whose result the handler awaits, and its promise. Unless
|
|
411
|
+
* `cancellable`, a cancellation neither fails it before it is made nor
|
|
412
|
+
* rejects it while it waits: the session's own waits (S-20).
|
|
413
|
+
*/
|
|
414
|
+
ask(act, { cancellable = false } = {}) {
|
|
411
415
|
let handle;
|
|
412
416
|
try {
|
|
413
417
|
handle = this.act(act);
|
|
@@ -415,121 +419,178 @@ export class AttemptDriver {
|
|
|
415
419
|
catch (error) {
|
|
416
420
|
return quiet(Promise.reject(error instanceof Error ? error : new Error(String(error))));
|
|
417
421
|
}
|
|
418
|
-
return this.promise(handle);
|
|
422
|
+
return this.promise(handle, !cancellable);
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* The options of an action named `name`, inside the span the code running
|
|
426
|
+
* now is inside of, if any.
|
|
427
|
+
*/
|
|
428
|
+
optionsOf(name, record) {
|
|
429
|
+
const parentSpan = this.span.getStore();
|
|
430
|
+
return {
|
|
431
|
+
...(name !== undefined ? { name } : {}),
|
|
432
|
+
...(parentSpan !== undefined ? { parentSpan } : {}),
|
|
433
|
+
...(record !== undefined ? { record } : {}),
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/** The span the code running now is inside of, if any. */
|
|
437
|
+
get currentSpan() {
|
|
438
|
+
return this.span.getStore();
|
|
419
439
|
}
|
|
420
440
|
// --- Calls
|
|
421
|
-
/** A call: the promise of its result, and of its invocation id. */
|
|
422
441
|
call(target) {
|
|
442
|
+
// An owed cancellation fails the call before it is made: once made, the
|
|
443
|
+
// callee would run and nothing would cancel it.
|
|
444
|
+
if (this.owed()) {
|
|
445
|
+
const cancelled = quiet(Promise.reject(new restate.CancelledError()));
|
|
446
|
+
return { result: cancelled, invocationId: cancelled };
|
|
447
|
+
}
|
|
423
448
|
let handles;
|
|
424
449
|
try {
|
|
425
|
-
handles = this.act((vm) => vm.call(
|
|
450
|
+
handles = this.act((vm) => vm.call({
|
|
451
|
+
service: target.service,
|
|
452
|
+
handler: target.handler,
|
|
453
|
+
key: target.key,
|
|
454
|
+
idempotencyKey: target.idempotencyKey,
|
|
455
|
+
headers: target.headers,
|
|
456
|
+
}, target.input, undefined, this.optionsOf(`${target.service}/${target.handler}`)));
|
|
426
457
|
}
|
|
427
458
|
catch (error) {
|
|
428
459
|
const rejected = quiet(Promise.reject(error instanceof Error ? error : new Error(String(error))));
|
|
429
460
|
return { result: rejected, invocationId: rejected };
|
|
430
461
|
}
|
|
431
|
-
const invocationId = quiet(this.promise(handles.invocationId,
|
|
462
|
+
const invocationId = quiet(this.promise(handles.invocationId, true).then((id) => String(id)));
|
|
432
463
|
const result = quiet(this.promise(handles.result).then((value) => (value === undefined ? null : value)));
|
|
433
|
-
// Keyed by the result's handle, which is what says the call is over.
|
|
434
464
|
this.calls.set(handles.result, { id: invocationId, done: false, cancelled: false });
|
|
435
465
|
return { result, invocationId };
|
|
436
466
|
}
|
|
437
|
-
/** A send
|
|
467
|
+
/** A one-way send. A cancellation never fails it. */
|
|
438
468
|
send(target) {
|
|
439
469
|
if (this.ended !== undefined)
|
|
440
|
-
return
|
|
441
|
-
let handle;
|
|
470
|
+
return;
|
|
442
471
|
try {
|
|
443
|
-
|
|
472
|
+
this.act((vm) => vm.send({
|
|
473
|
+
service: target.service,
|
|
474
|
+
handler: target.handler,
|
|
475
|
+
key: target.key,
|
|
476
|
+
idempotencyKey: target.idempotencyKey,
|
|
477
|
+
headers: target.headers,
|
|
478
|
+
}, target.input, target.delay, this.optionsOf(`${target.service}/${target.handler}`)));
|
|
444
479
|
}
|
|
445
480
|
catch (error) {
|
|
446
|
-
// An attempt
|
|
447
|
-
//
|
|
448
|
-
// number of milliseconds, say — and throws, as it does on the SDK's
|
|
449
|
-
// context, rather than vanishing into a promise nobody holds.
|
|
481
|
+
// An ended attempt drops the send; the next attempt sends it again.
|
|
482
|
+
// Any other error is the caller's (an invalid input, a bad delay) and throws, as on the SDK context.
|
|
450
483
|
if (this.ended !== undefined)
|
|
451
|
-
return
|
|
484
|
+
return;
|
|
452
485
|
throw error;
|
|
453
486
|
}
|
|
454
|
-
return quiet(this.promise(handle, 'sendId').then((id) => String(id)));
|
|
455
487
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
488
|
+
cancel(invocationId) {
|
|
489
|
+
this.tell((vm) => vm.cancelInvocation(invocationId, false, this.optionsOf('cancel')));
|
|
490
|
+
}
|
|
491
|
+
// --- Invocation state: this invocation's own, which every attempt of it
|
|
492
|
+
// finds, and which ends with it. Bytes only. Its reads are the session's
|
|
493
|
+
// own waits: a cancellation neither refuses nor rejects them.
|
|
494
|
+
stateGet(scope, key) {
|
|
495
|
+
return quiet(this.ask((vm) => vm.getState(scope, key, this.optionsOf(key))).then((value) => value === undefined ? null : value));
|
|
496
|
+
}
|
|
497
|
+
stateKeys(scope) {
|
|
498
|
+
return quiet(this.ask((vm) => vm.getStateKeys(scope, this.optionsOf())).then((keys) => keys));
|
|
459
499
|
}
|
|
460
|
-
|
|
461
|
-
this.
|
|
500
|
+
stateSet(scope, key, value) {
|
|
501
|
+
this.act((vm) => vm.setState(scope, key, value, false, this.optionsOf(key)));
|
|
462
502
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
return this.act((vm) => vm.startTrace(name, parent));
|
|
503
|
+
stateClear(scope, key) {
|
|
504
|
+
this.act((vm) => vm.clearState(scope, key, false, this.optionsOf(key)));
|
|
466
505
|
}
|
|
467
|
-
|
|
468
|
-
this.act((vm) => vm.
|
|
506
|
+
stateClearAll(scope) {
|
|
507
|
+
this.act((vm) => vm.clearAllState(scope, false, this.optionsOf()));
|
|
469
508
|
}
|
|
470
|
-
// ---
|
|
471
|
-
//
|
|
472
|
-
// Restate shuts an attempt down once its inactivity timeout passes with
|
|
473
|
-
// nothing heard. That is the default: the attempt suspends on what it waits
|
|
474
|
-
// for (or, waiting for nothing, starts again at once), and a wait of days
|
|
475
|
-
// holds no process. Work that must not be cut off says so with `working`:
|
|
476
|
-
// while any of it runs, the attempt heartbeats, whatever else it waits for.
|
|
509
|
+
// --- Signals: addressed to an invocation, by name
|
|
477
510
|
/**
|
|
478
|
-
*
|
|
479
|
-
* `
|
|
480
|
-
* comes at once if the attempt has been silent for `everyMs` already (most
|
|
481
|
-
* of the timeout may be gone), so the silence never reaches twice that;
|
|
482
|
-
* otherwise the scopes the database opens for each of its calls would each
|
|
483
|
-
* send one.
|
|
511
|
+
* The signal `index` (from 0) of `name` this invocation received: its
|
|
512
|
+
* payload, `null` if it carries nothing, or a TerminalError if a failure.
|
|
484
513
|
*/
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
514
|
+
signalRead(name, index) {
|
|
515
|
+
return quiet(this.issue((vm) => vm.signalRead(name, index, this.optionsOf(`${name}#${index}`))).then((value) => value === undefined ? null : value));
|
|
516
|
+
}
|
|
517
|
+
/** A signal `name` to invocation `invocationId`, with `value`, a failure, or nothing. */
|
|
518
|
+
signalSend(invocationId, name, value) {
|
|
519
|
+
const failure = value instanceof restate.TerminalError
|
|
520
|
+
? {
|
|
521
|
+
code: carried(value.code),
|
|
522
|
+
message: value.message,
|
|
523
|
+
metadata: Object.entries(value.metadata ?? {}).map(([key, v]) => ({ key, value: v })),
|
|
524
|
+
}
|
|
525
|
+
: null;
|
|
526
|
+
const bytes = value instanceof Uint8Array ? value : null;
|
|
527
|
+
this.act((vm) => vm.signalSend(invocationId, name, bytes, failure, false, this.optionsOf(name)));
|
|
528
|
+
}
|
|
529
|
+
/** Resolves once every action made before it is applied. A cancellation neither refuses nor rejects it. */
|
|
530
|
+
flushActions() {
|
|
531
|
+
return quiet(this.ask((vm) => vm.flush(this.optionsOf())).then(() => undefined));
|
|
532
|
+
}
|
|
533
|
+
// --- Spans, shown in Restate's UI: the actions made inside one are nested
|
|
534
|
+
// under it
|
|
535
|
+
/**
|
|
536
|
+
* Run `fn` inside a span `name`: the actions it makes (calls, sends,
|
|
537
|
+
* commits) are inside it. It ends with `fn`: with its value (`output`
|
|
538
|
+
* serializes it, if given), or its failure. An attempt that ended leaves
|
|
539
|
+
* it to Restate, which ends what an attempt left open.
|
|
540
|
+
*/
|
|
541
|
+
async inSpan(name, fn, output) {
|
|
542
|
+
if (this.ended !== undefined)
|
|
543
|
+
return fn();
|
|
544
|
+
let span;
|
|
488
545
|
try {
|
|
489
|
-
|
|
546
|
+
span = this.act((vm) => vm.spanStart(name, this.span.getStore()));
|
|
490
547
|
}
|
|
491
|
-
|
|
492
|
-
|
|
548
|
+
catch {
|
|
549
|
+
return fn();
|
|
550
|
+
}
|
|
551
|
+
let value;
|
|
552
|
+
try {
|
|
553
|
+
value = await this.span.run(span, fn);
|
|
493
554
|
}
|
|
555
|
+
catch (error) {
|
|
556
|
+
this.tell((vm) => vm.spanEnd(span, null, {
|
|
557
|
+
code: carried(error.code),
|
|
558
|
+
message: error instanceof Error ? error.message : String(error),
|
|
559
|
+
metadata: [],
|
|
560
|
+
}));
|
|
561
|
+
throw error;
|
|
562
|
+
}
|
|
563
|
+
this.tell((vm) => vm.spanEnd(span, output?.(value) ?? null, null));
|
|
564
|
+
return value;
|
|
494
565
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
* on firing it beats if the run it finds is that old, waits out the rest if
|
|
499
|
-
* a run that opened later is younger, and is let go if no scope is open.
|
|
500
|
-
*/
|
|
501
|
-
startHeartbeats(everyMs) {
|
|
566
|
+
// --- Keep-alive
|
|
567
|
+
/** The first scope opened: beat now if silent for a full interval, and arm the timer. */
|
|
568
|
+
startHeartbeats() {
|
|
502
569
|
if (this.ended !== undefined)
|
|
503
570
|
return;
|
|
504
|
-
|
|
505
|
-
if (now - this.wroteAt >= everyMs)
|
|
571
|
+
if (Date.now() - this.wroteAt >= this.interval)
|
|
506
572
|
this.heartbeat();
|
|
507
|
-
this.
|
|
508
|
-
|
|
509
|
-
this.arm(this.nextBeat);
|
|
510
|
-
}
|
|
511
|
-
/** Arm the timer for `due`, unless it is armed for that or earlier. */
|
|
512
|
-
arm(due) {
|
|
513
|
-
if (this.heartbeats !== undefined) {
|
|
514
|
-
if (this.beatDue <= due)
|
|
515
|
-
return;
|
|
516
|
-
clearTimeout(this.heartbeats);
|
|
517
|
-
}
|
|
518
|
-
this.beatDue = due;
|
|
519
|
-
this.heartbeats = setTimeout(() => this.beat(), Math.max(0, due - Date.now()));
|
|
520
|
-
this.heartbeats.unref?.();
|
|
573
|
+
if (this.heartbeats === undefined && this.ended === undefined)
|
|
574
|
+
this.arm(this.interval);
|
|
521
575
|
}
|
|
522
|
-
/** The timer fired. */
|
|
576
|
+
/** The timer fired. Stop if no scope is open; else beat if silent for a full interval, or wait out the rest. */
|
|
523
577
|
beat() {
|
|
524
578
|
this.heartbeats = undefined;
|
|
525
|
-
if (this.ended !== undefined || this.
|
|
579
|
+
if (this.ended !== undefined || this.stop.alive === 0)
|
|
580
|
+
return;
|
|
581
|
+
const silent = Date.now() - this.wroteAt;
|
|
582
|
+
if (silent < this.interval) {
|
|
583
|
+
this.arm(this.interval - silent);
|
|
526
584
|
return;
|
|
527
|
-
const now = Date.now();
|
|
528
|
-
if (now >= this.nextBeat) {
|
|
529
|
-
this.heartbeat();
|
|
530
|
-
this.nextBeat = now + this.runEvery;
|
|
531
585
|
}
|
|
532
|
-
this.
|
|
586
|
+
this.heartbeat();
|
|
587
|
+
// A heartbeat can end the attempt.
|
|
588
|
+
if (this.ended === undefined)
|
|
589
|
+
this.arm(this.interval);
|
|
590
|
+
}
|
|
591
|
+
arm(ms) {
|
|
592
|
+
this.heartbeats = setTimeout(() => this.beat(), ms);
|
|
593
|
+
this.heartbeats.unref?.();
|
|
533
594
|
}
|
|
534
595
|
heartbeat() {
|
|
535
596
|
if (this.ended !== undefined)
|
|
@@ -548,20 +609,18 @@ export class AttemptDriver {
|
|
|
548
609
|
this.heartbeats = undefined;
|
|
549
610
|
}
|
|
550
611
|
// --- The input, and the end
|
|
551
|
-
/** The invocation's input, read from the VM once. */
|
|
552
612
|
input() {
|
|
553
613
|
this.theInput ??= this.vm.input();
|
|
554
614
|
return this.theInput;
|
|
555
615
|
}
|
|
556
|
-
/**
|
|
616
|
+
/** Available once the VM is ready to execute. */
|
|
557
617
|
get key() {
|
|
558
618
|
return this.input().key;
|
|
559
619
|
}
|
|
560
|
-
/** The handler's input. */
|
|
561
620
|
get body() {
|
|
562
621
|
return this.input().input;
|
|
563
622
|
}
|
|
564
|
-
/** The invocation's request, as
|
|
623
|
+
/** The invocation's request, as `ctx.request`. */
|
|
565
624
|
request(attemptHeaders = new Map()) {
|
|
566
625
|
const input = this.input();
|
|
567
626
|
const target = {
|
|
@@ -584,29 +643,27 @@ export class AttemptDriver {
|
|
|
584
643
|
idempotencyKey: input.idempotencyKey,
|
|
585
644
|
scope: input.scope,
|
|
586
645
|
limitKey: input.limitKey,
|
|
587
|
-
|
|
646
|
+
attempt: input.attempt,
|
|
647
|
+
attemptCompletedSignal: this.completed,
|
|
588
648
|
};
|
|
589
649
|
}
|
|
590
|
-
/** The handler ended with its output. */
|
|
591
650
|
endSuccess(output) {
|
|
592
651
|
this.last((vm) => vm.endSuccess(output));
|
|
593
652
|
}
|
|
594
|
-
/** The handler ended with a terminal failure. */
|
|
595
653
|
endFailure(error) {
|
|
596
654
|
this.last((vm) => vm.endFailure({
|
|
597
|
-
code: error.code
|
|
655
|
+
code: carried(error.code),
|
|
598
656
|
message: error.message,
|
|
599
657
|
metadata: Object.entries(error.metadata ?? {}).map(([key, value]) => ({ key, value })),
|
|
600
658
|
}));
|
|
601
659
|
}
|
|
602
|
-
/**
|
|
660
|
+
/** Fail the attempt; Restate retries it. */
|
|
603
661
|
fail(message, stacktrace, retryAfterMs) {
|
|
604
662
|
if (this.ended !== undefined)
|
|
605
663
|
return;
|
|
606
664
|
this.vm.notifyError(message, stacktrace, retryAfterMs);
|
|
607
665
|
this.schedule();
|
|
608
666
|
}
|
|
609
|
-
/** End the attempt with `act`, unless it ended already. */
|
|
610
667
|
last(act) {
|
|
611
668
|
if (this.ended !== undefined)
|
|
612
669
|
return;
|
|
@@ -614,9 +671,13 @@ export class AttemptDriver {
|
|
|
614
671
|
act(this.vm);
|
|
615
672
|
}
|
|
616
673
|
catch {
|
|
617
|
-
// The attempt ended
|
|
674
|
+
// The attempt ended meanwhile; the VM wrote why.
|
|
618
675
|
}
|
|
619
676
|
this.schedule();
|
|
620
677
|
}
|
|
621
678
|
}
|
|
679
|
+
/** A failure's code as the protocol carries it: one that is not an integer in 0..65535 is sent as 500, not truncated. */
|
|
680
|
+
function carried(code) {
|
|
681
|
+
return typeof code === 'number' && Number.isInteger(code) && code >= 0 && code <= 0xffff ? code : 500;
|
|
682
|
+
}
|
|
622
683
|
//# sourceMappingURL=driver.js.map
|