@restatedev/sessions 0.9.0 → 0.10.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 +52 -26
- 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 +549 -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 +653 -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/src/serve.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
// `serve`: the services of a program, on PORT, with what they need beside
|
|
2
2
|
// them (a library's objects, such as each @restatedev/agents agent's companion).
|
|
3
|
+
//
|
|
4
|
+
// A session object's invocations run on the explicit recovery VM whichever of
|
|
5
|
+
// these serves them, and so does an endpoint built by hand: `endpoint.ts`
|
|
6
|
+
// wraps the SDK's endpoint class, which every transport builds its request
|
|
7
|
+
// handler from.
|
|
3
8
|
import * as restate from '@restatedev/restate-sdk';
|
|
4
9
|
import { isRemote } from './calls.ts';
|
|
5
10
|
|
package/src/session.ts
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
import { Buffer } from 'node:buffer';
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
import type * as restate from '@restatedev/restate-sdk';
|
|
8
|
-
import { serde, TerminalError } from '@restatedev/restate-sdk';
|
|
8
|
+
import { CancelledError, serde, TerminalError } from '@restatedev/restate-sdk';
|
|
9
9
|
import { millisOrDurationToMillis, type Serde } from '@restatedev/restate-sdk-core';
|
|
10
10
|
import {
|
|
11
11
|
open,
|
|
12
|
+
openOnVm,
|
|
12
13
|
type Row,
|
|
13
14
|
sqlOver,
|
|
14
15
|
SqlError,
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
type Store,
|
|
21
22
|
type ConnectionTransaction,
|
|
22
23
|
} from './database.ts';
|
|
24
|
+
import type { Vm } from './vm.ts';
|
|
23
25
|
import { Link } from './link.ts';
|
|
24
26
|
import { apply, dropAll, ensureOf, ensuring, type Schema } from './schema.ts';
|
|
25
27
|
import { type Send, sendOf } from './calls.ts';
|
|
@@ -69,6 +71,19 @@ export interface SqlHost {
|
|
|
69
71
|
/** The object's state keys. */
|
|
70
72
|
stateKeys(): PromiseLike<string[]>;
|
|
71
73
|
run(name: string, action: () => void): PromiseLike<void>;
|
|
74
|
+
/**
|
|
75
|
+
* The attempt's VM, when it has one: the addon then reads and writes the
|
|
76
|
+
* object's state as the VM's own commands, and none of it crosses into
|
|
77
|
+
* JavaScript (see `openOnVm`).
|
|
78
|
+
*/
|
|
79
|
+
readonly vm?: Vm;
|
|
80
|
+
/**
|
|
81
|
+
* Whether a cancellation is owed to the handler's next await, taking it:
|
|
82
|
+
* the attempt's driver has one when a cancellation arrived that failed
|
|
83
|
+
* nothing the handler awaits. Absent where the SDK's context serves the
|
|
84
|
+
* attempt, whose own awaits fail with it.
|
|
85
|
+
*/
|
|
86
|
+
readonly takeCancellation?: () => boolean;
|
|
72
87
|
genericSend(send: restate.GenericSend<Uint8Array>): unknown;
|
|
73
88
|
}
|
|
74
89
|
|
|
@@ -161,6 +176,26 @@ const unbeside = (name: string) => (key: string) => {
|
|
|
161
176
|
* the life of the process. One the SDK cancels fails as any await does: the
|
|
162
177
|
* session it ends fails its calls with the CancelledError (see `open`).
|
|
163
178
|
*/
|
|
179
|
+
/**
|
|
180
|
+
* The store of a database the addon keeps on the VM: it reads and writes the
|
|
181
|
+
* state itself, and calls this only at a commit, through `sync`, where the
|
|
182
|
+
* boundary runs what the committing transaction gave `withCommit`. Reaching
|
|
183
|
+
* anything else is a bug in the addon, and says so rather than going quiet.
|
|
184
|
+
*/
|
|
185
|
+
function commitOnly(): Store {
|
|
186
|
+
return {
|
|
187
|
+
get: servedByVm('get'),
|
|
188
|
+
set: servedByVm('set'),
|
|
189
|
+
clear: servedByVm('clear'),
|
|
190
|
+
sync: async () => {},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** What a store the addon never calls does if it is called after all. */
|
|
195
|
+
const servedByVm = (op: string) => () => {
|
|
196
|
+
throw new Error(`@restatedev/sessions: the VM serves the state; the store's ${op} is not called`);
|
|
197
|
+
};
|
|
198
|
+
|
|
164
199
|
function objectStore(ctx: SqlHost, end: AttemptEnd, dbName: string | undefined): Store {
|
|
165
200
|
const keyOf = dbName === undefined ? (key: string) => key : beside(dbName);
|
|
166
201
|
const request = <T>(promise: PromiseLike<T>) => end.race(promise);
|
|
@@ -444,10 +479,10 @@ export class Attempt {
|
|
|
444
479
|
this.name = name;
|
|
445
480
|
this.hasOutbox = !ctx.shared && name === undefined;
|
|
446
481
|
this.db = {
|
|
447
|
-
...sqlOver((sql, params) => this.call((db) => db.queryColumns(sql, params)), handlerError),
|
|
448
|
-
transaction: (body) => this.transaction(body),
|
|
482
|
+
...sqlOver((sql, params) => this.asked(() => this.call((db) => db.queryColumns(sql, params))), handlerError),
|
|
483
|
+
transaction: (body) => this.asked(() => this.transaction(body)),
|
|
449
484
|
};
|
|
450
|
-
ensuring(this.db, (schemas) => this.call((db) => this.ensureSchemas(db, schemas)));
|
|
485
|
+
ensuring(this.db, (schemas) => this.asked(() => this.call((db) => this.ensureSchemas(db, schemas))));
|
|
451
486
|
ATTEMPT.on(this.db, this);
|
|
452
487
|
}
|
|
453
488
|
|
|
@@ -459,6 +494,26 @@ export class Attempt {
|
|
|
459
494
|
return other.db;
|
|
460
495
|
}
|
|
461
496
|
|
|
497
|
+
/**
|
|
498
|
+
* `call`, which the handler asked for, unless a cancellation is owed to
|
|
499
|
+
* it: then the call fails with the cancellation, and does nothing.
|
|
500
|
+
*
|
|
501
|
+
* This is where the database delivers a cancellation, and the only place.
|
|
502
|
+
* What the handler asks for passes through here; what the session does for
|
|
503
|
+
* itself does not — the open, the object's schema, the sends a crashed
|
|
504
|
+
* attempt left unmade, emptying the outbox after a commit — and a
|
|
505
|
+
* cancellation that failed one of those would be delivered to nobody, or
|
|
506
|
+
* would report a transaction as cancelled after it committed. And it is
|
|
507
|
+
* before the call issues anything, so a call that fails with
|
|
508
|
+
* `CancelledError` wrote nothing and sent nothing: a call already under way
|
|
509
|
+
* when the cancellation arrives runs to its end, and the cancellation goes
|
|
510
|
+
* to what the handler awaits next.
|
|
511
|
+
*/
|
|
512
|
+
private asked<T>(call: () => Promise<T>): Promise<T> {
|
|
513
|
+
if (this.ctx.takeCancellation?.()) return Promise.reject(new CancelledError());
|
|
514
|
+
return call();
|
|
515
|
+
}
|
|
516
|
+
|
|
462
517
|
/**
|
|
463
518
|
* Open the session now, as the attempt starts, rather than on its first
|
|
464
519
|
* call: the open makes the sends a crashed attempt committed and left
|
|
@@ -486,8 +541,8 @@ export class Attempt {
|
|
|
486
541
|
* handler, its sends commit with it, in the outbox, and are made with its
|
|
487
542
|
* commit, right after its writes and before it waits for anything
|
|
488
543
|
* (`withCommit`): Restate stores an attempt's commands in order, so they
|
|
489
|
-
* are stored only if the commit is, and whatever fails the commit's wait
|
|
490
|
-
*
|
|
544
|
+
* are stored only if the commit is, and whatever fails the commit's wait
|
|
545
|
+
* (the attempt's end, say) comes after them. A second commit empties the outbox
|
|
491
546
|
* before `transaction` resolves: left until the handler's end, they would
|
|
492
547
|
* be made again by an attempt that resumes a long wait (an approval), past
|
|
493
548
|
* the idempotency retention that drops a repeat.
|
|
@@ -564,9 +619,23 @@ export class Attempt {
|
|
|
564
619
|
// database (a name has no `/`).
|
|
565
620
|
const key = `${ctx.request().target.service}/${encodeURIComponent(ctx.key)}`;
|
|
566
621
|
const namespace = this.name === undefined ? key : `${key}/${this.name}`;
|
|
567
|
-
const object = objectStore(ctx, end, this.name);
|
|
568
622
|
const wrap = this.config.wrapStore;
|
|
569
|
-
|
|
623
|
+
// With a VM, the addon reads and writes the state itself and nothing of
|
|
624
|
+
// the store crosses into JavaScript. A wrapper (fault injection, through
|
|
625
|
+
// `withStoreWrapper`) would then have nothing to wrap, and using the
|
|
626
|
+
// JavaScript store instead would quietly test the other path: the tests
|
|
627
|
+
// that wrap give their attempts no VM, so this says so rather than
|
|
628
|
+
// choosing for them.
|
|
629
|
+
if (ctx.vm !== undefined && wrap !== undefined) {
|
|
630
|
+
throw new TerminalError(
|
|
631
|
+
'withStoreWrapper wraps the store of an attempt served by the SDK; an attempt on the VM has none to wrap',
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
const onVm = ctx.vm !== undefined;
|
|
635
|
+
const object = onVm ? commitOnly() : objectStore(ctx, end, this.name);
|
|
636
|
+
const db = onVm
|
|
637
|
+
? await openOnVm(namespace, object, ctx.vm, { name: this.name, reader: ctx.shared })
|
|
638
|
+
: await open(namespace, wrap ? wrap(object) : object, { reader: ctx.shared });
|
|
570
639
|
try {
|
|
571
640
|
// A view writes nothing: a write fails, rather than vanish with the attempt.
|
|
572
641
|
if (ctx.shared) await db.execute('PRAGMA query_only = 1');
|
package/src/vm.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// The VM of a session object's attempt: shared-core's explicit recovery state
|
|
2
|
+
// machine, through the addon (`native/src/vm.rs`).
|
|
3
|
+
//
|
|
4
|
+
// It never replays a journal: every attempt runs the handler from the start
|
|
5
|
+
// and recovers from the state the earlier attempts stored. An action returns
|
|
6
|
+
// a handle, and its result comes back as a notification of that handle
|
|
7
|
+
// (`takeNotification`), which the driver settles a promise with. There is no
|
|
8
|
+
// `do_progress` and no single await point, so a handler awaits what it likes,
|
|
9
|
+
// with its own combinators.
|
|
10
|
+
//
|
|
11
|
+
// The VM is synchronous and does no I/O: every call only buffers output.
|
|
12
|
+
import { addon } from './addon.ts';
|
|
13
|
+
|
|
14
|
+
/** A header of the request, or of a call. */
|
|
15
|
+
export interface VmHeader {
|
|
16
|
+
key: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A terminal failure: what a handler throws, and what a failed action completes with. */
|
|
21
|
+
export interface VmFailure {
|
|
22
|
+
code: number;
|
|
23
|
+
message: string;
|
|
24
|
+
metadata: VmHeader[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The response's head: written before the body. */
|
|
28
|
+
export interface VmResponseHead {
|
|
29
|
+
statusCode: number;
|
|
30
|
+
headers: VmHeader[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The invocation's input, once the VM is ready to execute. */
|
|
34
|
+
export interface VmInput {
|
|
35
|
+
invocationId: string;
|
|
36
|
+
key: string;
|
|
37
|
+
headers: VmHeader[];
|
|
38
|
+
input: Buffer;
|
|
39
|
+
scope?: string;
|
|
40
|
+
limitKey?: string;
|
|
41
|
+
idempotencyKey?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The handles of a call: its invocation id, and its result. */
|
|
45
|
+
export interface VmCallHandle {
|
|
46
|
+
invocationId: number;
|
|
47
|
+
result: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A ready result: `kind` says which field holds it. */
|
|
51
|
+
export type VmNotification = { handle: number } & (
|
|
52
|
+
| { kind: 'void' }
|
|
53
|
+
| { kind: 'success'; bytes: Buffer }
|
|
54
|
+
| { kind: 'failure'; failure: VmFailure }
|
|
55
|
+
| { kind: 'stateKeys'; keys: string[] }
|
|
56
|
+
| { kind: 'invocationId'; id: string }
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
/** Where the attempt is: `starting` until the input is in, then `running`, then `closed`. */
|
|
60
|
+
export type VmState = 'starting' | 'running' | 'closed';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The attempt's state machine. A command returns a handle, notified later
|
|
64
|
+
* through {@link Vm.takeNotification}; a fire-and-forget command with `ack`
|
|
65
|
+
* is notified once Restate stores it, and at once without.
|
|
66
|
+
*/
|
|
67
|
+
export interface Vm {
|
|
68
|
+
/**
|
|
69
|
+
* What the VM calls, on this thread, after the addon acts on it from the
|
|
70
|
+
* runtime's — the database's store reading or writing the object's state.
|
|
71
|
+
* It is the driver's tick: it writes the output and settles what completed.
|
|
72
|
+
*/
|
|
73
|
+
setScheduler(scheduler: () => void): void;
|
|
74
|
+
|
|
75
|
+
getResponseHead(): VmResponseHead;
|
|
76
|
+
|
|
77
|
+
notifyInput(buffer: Uint8Array): void;
|
|
78
|
+
/** The runtime closed the input stream. While the handler runs, this shuts the attempt down. */
|
|
79
|
+
notifyInputClosed(): void;
|
|
80
|
+
/** The bytes buffered so far for the response body. */
|
|
81
|
+
takeOutput(): Buffer;
|
|
82
|
+
|
|
83
|
+
isReadyToExecute(): boolean;
|
|
84
|
+
input(): VmInput;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The next ready result, in no particular order, or `null`. Throws once
|
|
88
|
+
* the attempt is closed: {@link isShutDown} tells a shutdown from a failure.
|
|
89
|
+
*/
|
|
90
|
+
takeNotification(): VmNotification | null;
|
|
91
|
+
|
|
92
|
+
getState(key: string, name?: string): number;
|
|
93
|
+
getStateKeys(name?: string): number;
|
|
94
|
+
setState(key: string, buffer: Uint8Array, name?: string, ack?: boolean): number;
|
|
95
|
+
clearState(key: string, name?: string, ack?: boolean): number;
|
|
96
|
+
clearAllState(name?: string, ack?: boolean): number;
|
|
97
|
+
|
|
98
|
+
call(
|
|
99
|
+
service: string,
|
|
100
|
+
handler: string,
|
|
101
|
+
buffer: Uint8Array,
|
|
102
|
+
key?: string,
|
|
103
|
+
idempotencyKey?: string,
|
|
104
|
+
headers?: VmHeader[],
|
|
105
|
+
name?: string,
|
|
106
|
+
): VmCallHandle;
|
|
107
|
+
/** Returns the handle of the invocation id. `delay` is in milliseconds. */
|
|
108
|
+
send(
|
|
109
|
+
service: string,
|
|
110
|
+
handler: string,
|
|
111
|
+
buffer: Uint8Array,
|
|
112
|
+
key?: string,
|
|
113
|
+
delay?: number,
|
|
114
|
+
idempotencyKey?: string,
|
|
115
|
+
headers?: VmHeader[],
|
|
116
|
+
name?: string,
|
|
117
|
+
): number;
|
|
118
|
+
attachInvocation(invocationId: string, name?: string): number;
|
|
119
|
+
cancelInvocation(invocationId: string, name?: string, ack?: boolean): number;
|
|
120
|
+
|
|
121
|
+
/** A trace, shown in Restate's UI as a run until it is ended. */
|
|
122
|
+
startTrace(name: string, parent?: number): number;
|
|
123
|
+
endTraceSuccess(trace: number, buffer: Uint8Array): void;
|
|
124
|
+
endTraceFailure(trace: number, failure: VmFailure): void;
|
|
125
|
+
|
|
126
|
+
/** Tells Restate the attempt makes progress, delaying its shutdown. */
|
|
127
|
+
heartbeat(): void;
|
|
128
|
+
/** Whether the invocation was cancelled, in this attempt or an earlier one. */
|
|
129
|
+
isCancelled(): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Fail what the database's store waits for, once the attempt is over and
|
|
132
|
+
* nothing will answer: its reads and acks live in the addon, so the driver
|
|
133
|
+
* cannot reject them itself.
|
|
134
|
+
*/
|
|
135
|
+
endStateWaits(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Let go of the scheduler. It holds the driver's tick, and so the driver and
|
|
138
|
+
* this VM: an attempt that kept it would never be freed.
|
|
139
|
+
*/
|
|
140
|
+
clearScheduler(): void;
|
|
141
|
+
state(): VmState;
|
|
142
|
+
|
|
143
|
+
endSuccess(buffer: Uint8Array): void;
|
|
144
|
+
endFailure(failure: VmFailure): void;
|
|
145
|
+
/** Fails the attempt, for example with a retryable error of the handler. */
|
|
146
|
+
notifyError(message: string, stacktrace?: string, delayOverrideMs?: number): void;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface VmConstructor {
|
|
150
|
+
/**
|
|
151
|
+
* `bidirectional` is false on a request/response transport (AWS Lambda,
|
|
152
|
+
* HTTP/1.1), which cannot serve such a handler: no response could reach
|
|
153
|
+
* it. The VM then fails the invocation at once, which pauses it.
|
|
154
|
+
*/
|
|
155
|
+
new (headers: VmHeader[], bidirectional: boolean): Vm;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export const Vm = (addon as { Vm: VmConstructor }).Vm;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Restate's signature on a request, as the SDK verifies it before dispatch.
|
|
162
|
+
* This package runs a session object's invocation ahead of that handler, so it
|
|
163
|
+
* verifies the signature itself.
|
|
164
|
+
*/
|
|
165
|
+
export interface Identity {
|
|
166
|
+
/**
|
|
167
|
+
* `null` if the headers carry a signature of `path` by one of the keys, and
|
|
168
|
+
* why not otherwise. It is `null` and not `undefined`: the addon answers
|
|
169
|
+
* `None`, which is how that crosses the boundary.
|
|
170
|
+
*/
|
|
171
|
+
verify(path: string, headers: VmHeader[]): string | null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
interface IdentityConstructor {
|
|
175
|
+
/** The v1 public keys a deployment accepts (`publickeyv1_…`). */
|
|
176
|
+
new (keys: string[]): Identity;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export const Identity = (addon as { Identity: IdentityConstructor }).Identity;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* What the database's store's waits fail with once the attempt is over, as
|
|
183
|
+
* the addon's errors carry it: the text crosses the boundary, the class does
|
|
184
|
+
* not (`native/src/vm.rs`).
|
|
185
|
+
*/
|
|
186
|
+
export const STATE_WAIT_OVER = 'restate-sessions: the attempt is over';
|
|
187
|
+
|
|
188
|
+
/** The message of the error a closed attempt throws when Restate shut it down. */
|
|
189
|
+
const SHUT_DOWN = 'ATTEMPT_SHUT_DOWN';
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Whether `error` says the attempt was shut down (Restate closed the input),
|
|
193
|
+
* rather than failed. A new attempt runs the handler from the start.
|
|
194
|
+
*/
|
|
195
|
+
export const isShutDown = (error: unknown): boolean => error instanceof Error && error.message.startsWith(SHUT_DOWN);
|
package/dist/selector.d.ts
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/** A future of the VM, as `do_progress` takes it. */
|
|
2
|
-
export type UnresolvedFuture = {
|
|
3
|
-
Single: number;
|
|
4
|
-
} | {
|
|
5
|
-
FirstCompleted: UnresolvedFuture[];
|
|
6
|
-
} | {
|
|
7
|
-
AllCompleted: UnresolvedFuture[];
|
|
8
|
-
} | {
|
|
9
|
-
FirstSucceededOrAllFailed: UnresolvedFuture[];
|
|
10
|
-
} | {
|
|
11
|
-
AllSucceededOrFirstFailed: UnresolvedFuture[];
|
|
12
|
-
} | {
|
|
13
|
-
Unknown: UnresolvedFuture[];
|
|
14
|
-
};
|
|
15
|
-
/** What `do_progress` returns. */
|
|
16
|
-
export type DoProgressResult = 'AnyCompleted' | 'WaitExternalProgress' | {
|
|
17
|
-
ExecuteRun: number;
|
|
18
|
-
} | 'CancelSignalReceived' | {
|
|
19
|
-
EphemeralNotificationReady: number;
|
|
20
|
-
};
|
|
21
|
-
/** What the selector needs of a promise: the shape of InternalRestatePromise. */
|
|
22
|
-
export interface Selectable {
|
|
23
|
-
/** Takes the result if it is ready, and settles the promise with it. */
|
|
24
|
-
tryComplete(): Promise<void>;
|
|
25
|
-
/** What it still waits for, or null once settled. */
|
|
26
|
-
unresolvedFuture(): UnresolvedFuture | null;
|
|
27
|
-
/** Rejects it with a CancelledError. */
|
|
28
|
-
tryCancel(): void;
|
|
29
|
-
}
|
|
30
|
-
/** What the selector needs of the invocation. */
|
|
31
|
-
export interface SelectorDriver {
|
|
32
|
-
/**
|
|
33
|
-
* Asks the VM for progress on `future`. With nothing ready, the VM tells
|
|
34
|
-
* Restate what the handler waits for, unless a run is executing; and once
|
|
35
|
-
* the input closed, it suspends the invocation on it (a throw), unless a
|
|
36
|
-
* run of the future is executing.
|
|
37
|
-
*/
|
|
38
|
-
doProgress(future: UnresolvedFuture): DoProgressResult;
|
|
39
|
-
/** Writes out what the VM has buffered. */
|
|
40
|
-
flushOutput(): Promise<void>;
|
|
41
|
-
/** Resolves on the next signal of external progress (input, a finished run, a registration). */
|
|
42
|
-
awaitExternalProgress(): Promise<void>;
|
|
43
|
-
/** Signals external progress. */
|
|
44
|
-
wake(): void;
|
|
45
|
-
/**
|
|
46
|
-
* The handle of the attempt's working run, made at the first call: a run
|
|
47
|
-
* that the VM hands out like any other (`ExecuteRun`) and that never ends
|
|
48
|
-
* while the attempt lasts. It stands for the handler's work outside Restate.
|
|
49
|
-
*/
|
|
50
|
-
work(): number;
|
|
51
|
-
executeRun(handle: number): void;
|
|
52
|
-
/** The attempt cannot go on: suspension, or an error. */
|
|
53
|
-
onError(e: unknown): void;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* The points of the loop where a test steps in: `pass`, as a pass starts,
|
|
57
|
-
* and `wait`, between a check that found nothing and the wait for progress.
|
|
58
|
-
*/
|
|
59
|
-
export type Failpoint = 'pass' | 'wait';
|
|
60
|
-
/**
|
|
61
|
-
* For tests: what every selector's loop awaits at each of its failpoints, to
|
|
62
|
-
* hold it there while something else happens (the input closes, a signal
|
|
63
|
-
* comes). At `wait`, it gets what the check asked for, and whether that was
|
|
64
|
-
* the invocation's cancellation alone (with the working run). Empty
|
|
65
|
-
* otherwise, and the loop then awaits nothing there.
|
|
66
|
-
*/
|
|
67
|
-
export declare const failpoints: Map<Failpoint, (asked?: UnresolvedFuture, watching?: boolean) => unknown>;
|
|
68
|
-
/**
|
|
69
|
-
* One loop that drives every pending promise of an invocation, so that the
|
|
70
|
-
* invocation has a single await point however many promises user code awaits
|
|
71
|
-
* at once, with its own combinators or none.
|
|
72
|
-
*
|
|
73
|
-
* A promise is registered once and stays until it settles. Each pass of the
|
|
74
|
-
* loop settles what is ready, then asks the VM for progress on the first of
|
|
75
|
-
* all the pending promises to complete: so a suspension waits for any of
|
|
76
|
-
* them, a pending run starts whichever promise is awaited first, and a
|
|
77
|
-
* cancellation reaches all of them. Registering wakes the loop, so a promise
|
|
78
|
-
* registered while it waits is part of the next pass. The loop runs while
|
|
79
|
-
* there is something to ask the VM for, and is the only one waiting for
|
|
80
|
-
* external progress.
|
|
81
|
-
*
|
|
82
|
-
* To ask is to say that the handler waits: the VM tells Restate what for,
|
|
83
|
-
* and suspends the invocation on it once the input closed (Restate's
|
|
84
|
-
* inactivity timeout). That holds for every promise but one. A handler with
|
|
85
|
-
* explicit cancellation awaits its cancellation (`cancelled(ctx)`) to stop
|
|
86
|
-
* work it does outside Restate, a model's request, say: it watches for it,
|
|
87
|
-
* and does not wait for it. Reported as awaited, the cancel signal would be
|
|
88
|
-
* all Restate believes the handler waits for; and suspended on it, the
|
|
89
|
-
* invocation would end its attempt in the middle of that work, with a
|
|
90
|
-
* cancellation as the only thing to resume it.
|
|
91
|
-
*
|
|
92
|
-
* So with only the cancellation pending, the loop asks for it together with
|
|
93
|
-
* the attempt's working run (`SelectorDriver.work`), which says what is true:
|
|
94
|
-
* the handler is at work on something of its own, as inside `ctx.run`. While
|
|
95
|
-
* a run executes, the VM reports no await, and does not suspend on a future
|
|
96
|
-
* the run is part of, whether the input is open or closed; and it still
|
|
97
|
-
* finds the cancel signal when it comes. See `awaited`.
|
|
98
|
-
*/
|
|
99
|
-
export declare class PromiseSelector {
|
|
100
|
-
private readonly pending;
|
|
101
|
-
private running;
|
|
102
|
-
private closed;
|
|
103
|
-
private waiting;
|
|
104
|
-
private watching;
|
|
105
|
-
private readonly driver;
|
|
106
|
-
constructor(driver: SelectorDriver);
|
|
107
|
-
register(promise: Selectable): void;
|
|
108
|
-
/** Stops the loop (it finds nothing pending), at the end of the attempt. */
|
|
109
|
-
close(): void;
|
|
110
|
-
private wake;
|
|
111
|
-
/** The promises still pending, for tests. */
|
|
112
|
-
get size(): number;
|
|
113
|
-
/**
|
|
114
|
-
* What to ask the VM for: the first of the pending promises to complete,
|
|
115
|
-
* or undefined with nothing pending. With only the invocation's
|
|
116
|
-
* cancellation pending, the handler watches and waits for nothing: the
|
|
117
|
-
* working run goes with it (see the class).
|
|
118
|
-
*/
|
|
119
|
-
private awaited;
|
|
120
|
-
private loop;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Drive every promise that the invocation of `ctx` (the SDK's context, not a
|
|
124
|
-
* proxy of it) awaits from here on with one selector. The returned function
|
|
125
|
-
* stops it, at the end of the attempt, and gives the invocation back its
|
|
126
|
-
* own loops.
|
|
127
|
-
*/
|
|
128
|
-
export declare function hookSelector(ctx: object): () => void;
|
|
129
|
-
//# sourceMappingURL=selector.d.ts.map
|
package/dist/selector.d.ts.map
DELETED
|
@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