durability 5.0.0 → 5.2.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 +238 -7
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +23 -6
- package/dist/{durability-alarms.js → alarms/index.js} +96 -52
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +24 -0
- package/dist/experimental.d.ts +11 -8
- package/dist/experimental.js +8 -5
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +55 -49
- package/dist/fanout/migrations.d.ts +9 -0
- package/dist/fanout/migrations.js +181 -0
- package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +13 -8
- package/dist/{log.d.ts → log/index.d.ts} +15 -3
- package/dist/{log.js → log/index.js} +152 -124
- package/dist/log/migrations.d.ts +18 -0
- package/dist/log/migrations.js +297 -0
- package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
- package/dist/{memoize.js → memoize/index.js} +14 -11
- package/dist/memoize/migrations.d.ts +10 -0
- package/dist/memoize/migrations.js +21 -0
- package/dist/migrations.d.ts +1 -103
- package/dist/migrations.js +1 -570
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +63 -46
- package/dist/operations/migrations.d.ts +10 -0
- package/dist/operations/migrations.js +150 -0
- package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
- package/dist/{outbox.js → outbox/index.js} +18 -63
- package/dist/outbox/migrations.d.ts +9 -0
- package/dist/outbox/migrations.js +43 -0
- package/dist/{routing-client.js → routing/client.js} +1 -1
- package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
- package/dist/{routing.js → routing/index.js} +3 -3
- package/dist/{load.d.ts → routing/load.d.ts} +1 -1
- package/dist/schedule/index.d.ts +165 -0
- package/dist/schedule/index.js +667 -0
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +122 -22
- package/dist/scheduler/index.js +925 -0
- package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
- package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
- package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
- package/dist/{semaphore.js → semaphore/index.js} +7 -7
- package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
- package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
- package/dist/semaphore/migrations.d.ts +18 -0
- package/dist/semaphore/migrations.js +444 -0
- package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
- package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
- package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
- package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +87 -75
- package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
- package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
- package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
- package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
- package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
- package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
- package/dist/{client.d.ts → socket/client.d.ts} +3 -3
- package/dist/{client.js → socket/client.js} +4 -4
- package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
- package/dist/{socket.js → socket/index.js} +66 -20
- package/dist/socket/migrations.d.ts +9 -0
- package/dist/socket/migrations.js +57 -0
- package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
- package/dist/storage.d.ts +9 -2
- package/dist/storage.js +24 -11
- package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
- package/dist/{topology-client.js → topology/client.js} +3 -3
- package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
- package/dist/{topology.js → topology/index.js} +92 -58
- package/dist/topology/migrations.d.ts +10 -0
- package/dist/topology/migrations.js +138 -0
- package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
- package/dist/{tracker.js → tracker/index.js} +2 -2
- package/package.json +11 -11
- package/dist/scheduler.js +0 -577
- /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
- /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
- /package/dist/{load.js → routing/load.js} +0 -0
- /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
- /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
- /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
- /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
- /package/dist/{tracked-source.js → tracker/source.js} +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the outbound event log owned by
|
|
3
|
+
* {@link DurabilitySocket}. Applied automatically on construction.
|
|
4
|
+
*/
|
|
5
|
+
export const durabilitySocketMigrations = [
|
|
6
|
+
{
|
|
7
|
+
name: 'durability_0006_create_socket_events',
|
|
8
|
+
up: `
|
|
9
|
+
CREATE TABLE IF NOT EXISTS durability_socket_events (
|
|
10
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
+
name TEXT NOT NULL,
|
|
12
|
+
payload TEXT NOT NULL,
|
|
13
|
+
created_at INTEGER NOT NULL
|
|
14
|
+
);
|
|
15
|
+
`,
|
|
16
|
+
down: 'DROP TABLE IF EXISTS durability_socket_events;',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
// Each channel numbers its own events, so existing events keep their
|
|
20
|
+
// sequence numbers on the default channel.
|
|
21
|
+
name: 'durability_0023_socket_channels',
|
|
22
|
+
up: `
|
|
23
|
+
ALTER TABLE durability_socket_events RENAME TO durability_socket_events_before_channels;
|
|
24
|
+
CREATE TABLE durability_socket_events (
|
|
25
|
+
seq INTEGER NOT NULL,
|
|
26
|
+
name TEXT NOT NULL,
|
|
27
|
+
payload TEXT NOT NULL,
|
|
28
|
+
created_at INTEGER NOT NULL,
|
|
29
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
30
|
+
PRIMARY KEY (channel, seq)
|
|
31
|
+
);
|
|
32
|
+
INSERT INTO durability_socket_events (seq, name, payload, created_at, channel)
|
|
33
|
+
SELECT seq, name, payload, created_at, 'default'
|
|
34
|
+
FROM durability_socket_events_before_channels;
|
|
35
|
+
DROP TABLE durability_socket_events_before_channels;
|
|
36
|
+
`,
|
|
37
|
+
down: `
|
|
38
|
+
CREATE TABLE durability_socket_events_channel_guard (
|
|
39
|
+
channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
|
|
40
|
+
);
|
|
41
|
+
INSERT INTO durability_socket_events_channel_guard
|
|
42
|
+
SELECT DISTINCT channel FROM durability_socket_events;
|
|
43
|
+
DROP TABLE durability_socket_events_channel_guard;
|
|
44
|
+
ALTER TABLE durability_socket_events RENAME TO durability_socket_events_with_channels;
|
|
45
|
+
CREATE TABLE durability_socket_events (
|
|
46
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
47
|
+
name TEXT NOT NULL,
|
|
48
|
+
payload TEXT NOT NULL,
|
|
49
|
+
created_at INTEGER NOT NULL
|
|
50
|
+
);
|
|
51
|
+
INSERT INTO durability_socket_events (seq, name, payload, created_at)
|
|
52
|
+
SELECT seq, name, payload, created_at
|
|
53
|
+
FROM durability_socket_events_with_channels;
|
|
54
|
+
DROP TABLE durability_socket_events_with_channels;
|
|
55
|
+
`,
|
|
56
|
+
},
|
|
57
|
+
];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type { SerializedError } from '
|
|
2
|
+
import type { SerializedError } from '../errors.js';
|
|
3
3
|
export declare const socketProtocolVersion = 1;
|
|
4
4
|
/** Largest text frame either side accepts, in UTF-16 code units. */
|
|
5
5
|
export declare const socketMaxFrameLength: number;
|
package/dist/storage.d.ts
CHANGED
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
export type DurableTable = {
|
|
6
6
|
name: string;
|
|
7
7
|
key: string;
|
|
8
|
+
channel?: string;
|
|
8
9
|
};
|
|
10
|
+
export declare const channelClause: (table: DurableTable) => string;
|
|
11
|
+
export declare const channelBindings: (table: DurableTable) => string[];
|
|
12
|
+
export declare const tableOwner: (table: DurableTable) => string;
|
|
9
13
|
/** One attempt of one record generation. */
|
|
10
14
|
export type AttemptRef = {
|
|
11
15
|
key: string;
|
|
@@ -13,8 +17,11 @@ export type AttemptRef = {
|
|
|
13
17
|
attempt: number;
|
|
14
18
|
};
|
|
15
19
|
export declare const pendingGeneration: (table: DurableTable) => string;
|
|
16
|
-
/**
|
|
17
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Starts the next attempt, optionally persisting its deadline in the same write.
|
|
22
|
+
* Returns undefined when the generation is gone or out of attempts.
|
|
23
|
+
*/
|
|
24
|
+
export declare const claimAttempt: (sql: SqlStorage, table: DurableTable, key: string, generation: string, maxAttempts: number, nextAttemptAt?: number) => number | undefined;
|
|
18
25
|
/**
|
|
19
26
|
* Applies `assignments` (a SQL `SET` list) to the record if the attempt still
|
|
20
27
|
* owns it, and reports whether it did.
|
package/dist/storage.js
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
export const channelClause = (table) => table.channel === undefined ? '' : ' AND channel = ?';
|
|
3
|
+
export const channelBindings = (table) => table.channel === undefined ? [] : [table.channel];
|
|
4
|
+
// Keep existing memo owners on the default channel, without restricting record IDs.
|
|
5
|
+
export const tableOwner = (table) => table.channel === undefined || table.channel === 'default'
|
|
6
|
+
? table.name
|
|
7
|
+
: JSON.stringify([table.name, table.channel]);
|
|
2
8
|
const attemptRowSchema = z.object({
|
|
3
9
|
attempt: z.number().int().nonnegative(),
|
|
4
10
|
});
|
|
5
11
|
// Every write is conditional on the record's generation and pending status, so a
|
|
6
12
|
// stale execution can never overwrite a replacement or a later attempt.
|
|
7
|
-
export const pendingGeneration = (table) => `${table.key} = ? AND generation_id = ? AND status = 'pending'`;
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
export const pendingGeneration = (table) => `${table.key} = ? AND generation_id = ? AND status = 'pending'${channelClause(table)}`;
|
|
14
|
+
/**
|
|
15
|
+
* Starts the next attempt, optionally persisting its deadline in the same write.
|
|
16
|
+
* Returns undefined when the generation is gone or out of attempts.
|
|
17
|
+
*/
|
|
18
|
+
export const claimAttempt = (sql, table, key, generation, maxAttempts, nextAttemptAt) => {
|
|
19
|
+
const deadlineAssignment = nextAttemptAt === undefined ? '' : ', next_attempt_at = ?';
|
|
20
|
+
return attemptRowSchema.optional().parse(sql
|
|
21
|
+
.exec(`UPDATE ${table.name}
|
|
22
|
+
SET attempt = attempt + 1${deadlineAssignment}
|
|
12
23
|
WHERE ${pendingGeneration(table)} AND attempt < ?
|
|
13
|
-
RETURNING attempt`, key, generation, maxAttempts)
|
|
14
|
-
|
|
24
|
+
RETURNING attempt`, ...(nextAttemptAt === undefined ? [] : [nextAttemptAt]), key, generation, ...channelBindings(table), maxAttempts)
|
|
25
|
+
.toArray()[0])?.attempt;
|
|
26
|
+
};
|
|
15
27
|
/**
|
|
16
28
|
* Applies `assignments` (a SQL `SET` list) to the record if the attempt still
|
|
17
29
|
* owns it, and reports whether it did.
|
|
@@ -20,20 +32,20 @@ export const updateAttempt = (sql, table, ref, assignments, ...values) => sql
|
|
|
20
32
|
.exec(`UPDATE ${table.name}
|
|
21
33
|
SET ${assignments}
|
|
22
34
|
WHERE ${pendingGeneration(table)} AND attempt = ?
|
|
23
|
-
RETURNING ${table.key}`, ...values, ref.key, ref.generation, ref.attempt)
|
|
35
|
+
RETURNING ${table.key}`, ...values, ref.key, ref.generation, ...channelBindings(table), ref.attempt)
|
|
24
36
|
.toArray().length > 0;
|
|
25
37
|
/** Deletes the record if the attempt still owns it, and reports whether it did. */
|
|
26
38
|
export const deleteAttempt = (sql, table, ref) => sql
|
|
27
39
|
.exec(`DELETE FROM ${table.name}
|
|
28
40
|
WHERE ${pendingGeneration(table)} AND attempt = ?
|
|
29
|
-
RETURNING ${table.key}`, ref.key, ref.generation, ref.attempt)
|
|
41
|
+
RETURNING ${table.key}`, ref.key, ref.generation, ...channelBindings(table), ref.attempt)
|
|
30
42
|
.toArray().length > 0;
|
|
31
43
|
/** Fails a generation that has used every attempt and returns its attempt count. */
|
|
32
44
|
export const exhaustAttempts = (sql, table, key, generation, maxAttempts, error) => attemptRowSchema.optional().parse(sql
|
|
33
45
|
.exec(`UPDATE ${table.name}
|
|
34
46
|
SET status = 'failed', last_error = ?, last_error_name = ?
|
|
35
47
|
WHERE ${pendingGeneration(table)} AND attempt >= ?
|
|
36
|
-
RETURNING attempt`, error.message, error.name, key, generation, maxAttempts)
|
|
48
|
+
RETURNING attempt`, error.message, error.name, key, generation, ...channelBindings(table), maxAttempts)
|
|
37
49
|
.toArray()[0])?.attempt;
|
|
38
50
|
const createdCursorSchema = z.tuple([
|
|
39
51
|
z.number().int().nonnegative(),
|
|
@@ -53,9 +65,10 @@ export const listCreated = (sql, table, nameColumn, query) => {
|
|
|
53
65
|
.exec(`SELECT * FROM ${table.name}
|
|
54
66
|
WHERE (? IS NULL OR status = ?)
|
|
55
67
|
AND (? IS NULL OR ${nameColumn} = ?)
|
|
68
|
+
${channelClause(table)}
|
|
56
69
|
AND (created_at > ? OR (created_at = ? AND ${table.key} > ?))
|
|
57
70
|
ORDER BY created_at ASC, ${table.key} ASC
|
|
58
|
-
LIMIT ?`, status, status, name, name, createdAt, createdAt, key, query.limit + 1)
|
|
71
|
+
LIMIT ?`, status, status, name, name, ...channelBindings(table), createdAt, createdAt, key, query.limit + 1)
|
|
59
72
|
.toArray();
|
|
60
73
|
const rows = found.slice(0, query.limit);
|
|
61
74
|
const last = rows[rows.length - 1];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type TopologyFallbackReason } from '
|
|
2
|
-
import { type TopologyMemberInput, type TopologySnapshot } from './
|
|
1
|
+
import { type TopologyFallbackReason } from '../errors.js';
|
|
2
|
+
import { type TopologyMemberInput, type TopologySnapshot } from './snapshot.js';
|
|
3
3
|
export type TopologySource = 'published' | 'stale' | 'seed' | 'fallback';
|
|
4
4
|
export type TopologyView<Meta = undefined> = TopologySnapshot<Meta> & {
|
|
5
5
|
source: TopologySource;
|
|
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _TopologyClient_instances, _TopologyClient_config, _TopologyClient_readTimeoutMs, _TopologyClient_ttlMs, _TopologyClient_negativeTtlMs, _TopologyClient_cache, _TopologyClient_inflight, _TopologyClient_load, _TopologyClient_read, _TopologyClient_fallback, _TopologyClient_remember;
|
|
13
|
-
import { TopologyUnavailableError, } from '
|
|
14
|
-
import { assertPositiveInteger } from '
|
|
15
|
-
import { decodeSnapshot, } from './
|
|
13
|
+
import { TopologyUnavailableError, } from '../errors.js';
|
|
14
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
15
|
+
import { decodeSnapshot, } from './snapshot.js';
|
|
16
16
|
// High-cardinality groups (one per tenant) must not grow an isolate without bound; the least recently used go first.
|
|
17
17
|
const maxCachedGroups = 1024;
|
|
18
18
|
class ReadTimeoutError extends Error {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type DurabilityOutboxEvent } from '
|
|
2
|
-
import { type SchedulerAttachment } from '
|
|
3
|
-
import { TopologyClient, type TopologyClientConfig } from './
|
|
4
|
-
import { type TopologyMember, type TopologyMemberInput, type TopologySnapshot } from './
|
|
1
|
+
import { type DurabilityOutboxEvent } from '../outbox/index.js';
|
|
2
|
+
import { type SchedulerAttachment } from '../scheduler/index.js';
|
|
3
|
+
import { TopologyClient, type TopologyClientConfig } from './client.js';
|
|
4
|
+
import { type TopologyMember, type TopologyMemberInput, type TopologySnapshot } from './snapshot.js';
|
|
5
5
|
/** `write` channels reach active members; `read` channels reach draining members too. */
|
|
6
6
|
export type TopologyRole = 'write' | 'read';
|
|
7
7
|
/** Publication and sync never give up: a stale topology must still converge. */
|
|
@@ -32,11 +32,21 @@ export type TopologyChannel<Value, Meta> = TopologyRetryOptions & {
|
|
|
32
32
|
deliver(member: TopologyMember<Meta>, update: TopologyStateUpdate<Value>): unknown;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
|
-
* Publication and sync run on outbox channels
|
|
36
|
-
* group,
|
|
35
|
+
* Publication and sync run on outbox channels keyed by group and by
|
|
36
|
+
* `[group, member]`. On the default instance channel these are
|
|
37
|
+
* `durability.topology` and `durability.topology.<state>`; on another instance
|
|
38
|
+
* channel they are JSON arrays: `["durability.topology", <channel>,
|
|
39
|
+
* "publication"]` and `["durability.topology", <channel>, "state", <state>]`.
|
|
37
40
|
*/
|
|
38
41
|
export type DurabilityTopologyEvent = DurabilityOutboxEvent;
|
|
39
42
|
export type DurabilityTopologyConfig<Meta, States extends Record<string, unknown>> = SchedulerAttachment & TopologyRetryOptions & {
|
|
43
|
+
/**
|
|
44
|
+
* Stable instance ID. Topologies on one scheduler share storage,
|
|
45
|
+
* partitioned by channel; each owns its groups, states, publisher, and
|
|
46
|
+
* deliveries, so the same group name in two channels is two groups.
|
|
47
|
+
* Defaults to `default`.
|
|
48
|
+
*/
|
|
49
|
+
channel?: string;
|
|
40
50
|
/** Without a publisher, membership is only visible inside this object. */
|
|
41
51
|
publish?: TopologyPublisher<Meta>;
|
|
42
52
|
states?: {
|
|
@@ -66,7 +76,7 @@ export declare class DurabilityTopology<Meta = undefined, States extends Record<
|
|
|
66
76
|
/** A read-only client for stateless Workers. It never contacts the coordinator. */
|
|
67
77
|
static client<Meta = undefined>(config: TopologyClientConfig<Meta>): TopologyClient<Meta>;
|
|
68
78
|
/** Reverts only the topology tables; outbox channels migrateCapability with `DurabilityOutbox.migrate`. */
|
|
69
|
-
static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("
|
|
79
|
+
static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): import("../migrations.js").DurabilityMigrationResult;
|
|
70
80
|
alarm(info?: AlarmInvocationInfo): Promise<void>;
|
|
71
81
|
/**
|
|
72
82
|
* Applies one change in a single transaction. `next` runs inside it and must
|
|
@@ -9,17 +9,26 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
|
|
12
|
+
var _DurabilityTopology_instances, _DurabilityTopology_engine, _DurabilityTopology_ownerChannel, _DurabilityTopology_coordinator, _DurabilityTopology_publication, _DurabilityTopology_channels, _DurabilityTopology_publishNow, _DurabilityTopology_stagePublication, _DurabilityTopology_publishSnapshots, _DurabilityTopology_settleLate, _DurabilityTopology_stageLatest, _DurabilityTopology_deliver, _DurabilityTopology_parseDraft, _DurabilityTopology_normalize, _DurabilityTopology_writeStates, _DurabilityTopology_stageDeliveries;
|
|
13
13
|
import { z } from 'zod';
|
|
14
|
-
import { reportFailure, TopologyError } from '
|
|
15
|
-
import { durabilityTopologyMigrations
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
|
|
14
|
+
import { reportFailure, TopologyError } from '../errors.js';
|
|
15
|
+
import { durabilityTopologyMigrations } from './migrations.js';
|
|
16
|
+
import { migrateCapability } from '../migrations.js';
|
|
17
|
+
import { DurabilityOutbox, discardWithin, publishNow, stageWithin, } from '../outbox/index.js';
|
|
18
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
19
|
+
import { assertChannel, defaultChannel, DurabilityScheduler, engineFor, } from '../scheduler/index.js';
|
|
20
|
+
import { canonical, deserialize, serialize } from '../serialization.js';
|
|
21
|
+
import { TopologyClient } from './client.js';
|
|
22
|
+
import { encodeSnapshot, memberInputSchema, membersSchema, } from './snapshot.js';
|
|
23
|
+
const outboxChannelPrefix = 'durability.topology';
|
|
24
|
+
// The default instance keeps the names it used before instance channels
|
|
25
|
+
// existed, so outbox rows staged by an older version are still delivered.
|
|
26
|
+
const publicationChannelFor = (ownerChannel) => ownerChannel === defaultChannel
|
|
27
|
+
? outboxChannelPrefix
|
|
28
|
+
: JSON.stringify([outboxChannelPrefix, ownerChannel, 'publication']);
|
|
29
|
+
const stateChannelFor = (ownerChannel, state) => ownerChannel === defaultChannel
|
|
30
|
+
? `${outboxChannelPrefix}.${state}`
|
|
31
|
+
: JSON.stringify([outboxChannelPrefix, ownerChannel, 'state', state]);
|
|
23
32
|
const groupRowSchema = z.object({
|
|
24
33
|
group_id: z.string(),
|
|
25
34
|
version: z.number().int().positive(),
|
|
@@ -76,15 +85,16 @@ const outboxOptions = (timeoutMs, options, fallback) => {
|
|
|
76
85
|
* land. KV has no conditional write, so some bound is required.
|
|
77
86
|
*/
|
|
78
87
|
const landingWindowMs = 5 * 60000;
|
|
79
|
-
const forgetWrite = (sql, id) => {
|
|
80
|
-
sql.exec('DELETE FROM durability_topology_writes WHERE id = ?', id);
|
|
88
|
+
const forgetWrite = (sql, ownerChannel, id) => {
|
|
89
|
+
sql.exec('DELETE FROM durability_topology_writes WHERE owner_channel = ? AND id = ?', ownerChannel, id);
|
|
81
90
|
};
|
|
82
91
|
/** Drops expired records and returns when the group's last outstanding write can no longer land. */
|
|
83
|
-
const outstandingWritesUntil = (sql, group, now) => {
|
|
84
|
-
sql.exec(
|
|
92
|
+
const outstandingWritesUntil = (sql, ownerChannel, group, now) => {
|
|
93
|
+
sql.exec(`DELETE FROM durability_topology_writes
|
|
94
|
+
WHERE owner_channel = ? AND group_id = ? AND land_by <= ?`, ownerChannel, group, now);
|
|
85
95
|
const { until } = z.object({ until: z.number().int().nullable() }).parse(sql
|
|
86
96
|
.exec(`SELECT MAX(land_by) AS until FROM durability_topology_writes
|
|
87
|
-
WHERE group_id = ?`, group)
|
|
97
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
88
98
|
.toArray()[0]);
|
|
89
99
|
return until ?? undefined;
|
|
90
100
|
};
|
|
@@ -97,13 +107,14 @@ const deliveryKey = (group, member) => JSON.stringify([group, member]);
|
|
|
97
107
|
const isThenable = (value) => value !== null &&
|
|
98
108
|
typeof value === 'object' &&
|
|
99
109
|
typeof Reflect.get(value, 'then') === 'function';
|
|
100
|
-
const readGroup = (sql, group) => groupRowSchema.optional().parse(sql
|
|
110
|
+
const readGroup = (sql, ownerChannel, group) => groupRowSchema.optional().parse(sql
|
|
101
111
|
.exec(`SELECT group_id, version, members, deleted
|
|
102
|
-
FROM durability_topology_groups
|
|
112
|
+
FROM durability_topology_groups
|
|
113
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
103
114
|
.toArray()[0]);
|
|
104
|
-
const readStates = (sql, group) => stateRowSchema.array().parse(sql
|
|
115
|
+
const readStates = (sql, ownerChannel, group) => stateRowSchema.array().parse(sql
|
|
105
116
|
.exec(`SELECT channel, value FROM durability_topology_states
|
|
106
|
-
WHERE group_id = ?`, group)
|
|
117
|
+
WHERE owner_channel = ? AND group_id = ?`, ownerChannel, group)
|
|
107
118
|
.toArray());
|
|
108
119
|
const parseMembers = (row) => membersSchema.parse(JSON.parse(row.members));
|
|
109
120
|
const inRole = (member, role) => role === 'read' || member.state === 'active';
|
|
@@ -117,9 +128,11 @@ export class DurabilityTopology {
|
|
|
117
128
|
constructor(config) {
|
|
118
129
|
_DurabilityTopology_instances.add(this);
|
|
119
130
|
_DurabilityTopology_engine.set(this, void 0);
|
|
131
|
+
_DurabilityTopology_ownerChannel.set(this, void 0);
|
|
120
132
|
_DurabilityTopology_coordinator.set(this, void 0);
|
|
121
133
|
_DurabilityTopology_publication.set(this, void 0);
|
|
122
134
|
_DurabilityTopology_channels.set(this, new Map());
|
|
135
|
+
const ownerChannel = assertChannel('DurabilityTopology', config.channel ?? defaultChannel);
|
|
123
136
|
// The outboxes must share this helper's engine, so a private scheduler is
|
|
124
137
|
// created explicitly instead of letting each attach its own.
|
|
125
138
|
const scheduler = config.scheduler ?? new DurabilityScheduler(config);
|
|
@@ -128,37 +141,51 @@ export class DurabilityTopology {
|
|
|
128
141
|
if (coordinator === undefined) {
|
|
129
142
|
throw new TopologyError('DurabilityTopology needs the Durable Object context, which carries its ID');
|
|
130
143
|
}
|
|
144
|
+
__classPrivateFieldSet(this, _DurabilityTopology_ownerChannel, ownerChannel, "f");
|
|
131
145
|
__classPrivateFieldSet(this, _DurabilityTopology_coordinator, coordinator, "f");
|
|
132
146
|
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").migrateSchema('topology', durabilityTopologyMigrations);
|
|
147
|
+
const publisher = config.publish;
|
|
148
|
+
if (publisher !== undefined &&
|
|
149
|
+
(typeof publisher.write !== 'function' ||
|
|
150
|
+
typeof publisher.delete !== 'function')) {
|
|
151
|
+
throw new TopologyError('publish needs write and delete functions');
|
|
152
|
+
}
|
|
153
|
+
const publication = publisher === undefined
|
|
154
|
+
? undefined
|
|
155
|
+
: { publisher, timeoutMs: timeoutOf(publisher, config) };
|
|
156
|
+
const states = config.states ?? {};
|
|
157
|
+
const channels = Object.entries(states).map(([name, channel]) => {
|
|
158
|
+
if (!name || typeof channel.deliver !== 'function') {
|
|
159
|
+
throw new TopologyError(`Invalid topology channel "${name}"`);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
name,
|
|
163
|
+
channel,
|
|
164
|
+
timeoutMs: timeoutOf(channel, config),
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
// Validate before claiming so config validation failures leave the channel
|
|
168
|
+
// free. Even without outboxes, an instance still owns its channel's groups.
|
|
169
|
+
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").claimChannel('durability_topology_groups', ownerChannel, 'DurabilityTopology');
|
|
133
170
|
// The outbox only emits outbox events, which is all this hook accepts.
|
|
134
171
|
const onLifecycleEvent = config.onLifecycleEvent;
|
|
135
172
|
const lifecycle = onLifecycleEvent === undefined ? {} : { onLifecycleEvent };
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (typeof publisher.write !== 'function' ||
|
|
139
|
-
typeof publisher.delete !== 'function') {
|
|
140
|
-
throw new TopologyError('publish needs write and delete functions');
|
|
141
|
-
}
|
|
142
|
-
const timeoutMs = timeoutOf(publisher, config);
|
|
173
|
+
if (publication !== undefined) {
|
|
174
|
+
const { timeoutMs } = publication;
|
|
143
175
|
__classPrivateFieldSet(this, _DurabilityTopology_publication, new DurabilityOutbox({
|
|
144
176
|
scheduler,
|
|
145
|
-
channel:
|
|
146
|
-
...outboxOptions(timeoutMs, publisher, config),
|
|
177
|
+
channel: publicationChannelFor(ownerChannel),
|
|
178
|
+
...outboxOptions(timeoutMs, publication.publisher, config),
|
|
147
179
|
...lifecycle,
|
|
148
|
-
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publisher, entries, timeoutMs),
|
|
180
|
+
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_publishSnapshots).call(this, publication.publisher, entries, timeoutMs),
|
|
149
181
|
}), "f");
|
|
150
182
|
}
|
|
151
|
-
const
|
|
152
|
-
for (const [name, channel] of Object.entries(channels)) {
|
|
153
|
-
if (!name || typeof channel.deliver !== 'function') {
|
|
154
|
-
throw new TopologyError(`Invalid topology channel "${name}"`);
|
|
155
|
-
}
|
|
156
|
-
const timeoutMs = timeoutOf(channel, config);
|
|
183
|
+
for (const { name, channel, timeoutMs } of channels) {
|
|
157
184
|
__classPrivateFieldGet(this, _DurabilityTopology_channels, "f").set(name, {
|
|
158
185
|
role: channel.members ?? 'write',
|
|
159
186
|
outbox: new DurabilityOutbox({
|
|
160
187
|
scheduler,
|
|
161
|
-
channel:
|
|
188
|
+
channel: stateChannelFor(ownerChannel, name),
|
|
162
189
|
...outboxOptions(timeoutMs, channel, config),
|
|
163
190
|
...lifecycle,
|
|
164
191
|
publish: (entries) => __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_deliver).call(this, name, channel, entries, timeoutMs),
|
|
@@ -188,10 +215,10 @@ export class DurabilityTopology {
|
|
|
188
215
|
async update(group, next) {
|
|
189
216
|
assertGroup(group);
|
|
190
217
|
const snapshot = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
191
|
-
const row = readGroup(sql, group);
|
|
218
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
192
219
|
const live = row?.deleted === 0 ? row : undefined;
|
|
193
220
|
const before = live ? parseMembers(live) : [];
|
|
194
|
-
const stateRows = readStates(sql, group);
|
|
221
|
+
const stateRows = readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
195
222
|
const states = Object.fromEntries(stateRows.map((state) => [state.channel, deserialize(state.value)]));
|
|
196
223
|
// A copy, so an updater that mutates `current` cannot make a change look like a no-op.
|
|
197
224
|
const draft = next({ members: structuredClone(before), states });
|
|
@@ -215,13 +242,13 @@ export class DurabilityTopology {
|
|
|
215
242
|
};
|
|
216
243
|
if (changed) {
|
|
217
244
|
sql.exec(`INSERT INTO durability_topology_groups
|
|
218
|
-
(group_id, version, members, deleted, updated_at)
|
|
219
|
-
VALUES (?, ?, ?, 0, ?)
|
|
220
|
-
ON CONFLICT(group_id) DO UPDATE SET
|
|
245
|
+
(owner_channel, group_id, version, members, deleted, updated_at)
|
|
246
|
+
VALUES (?, ?, ?, ?, 0, ?)
|
|
247
|
+
ON CONFLICT(owner_channel, group_id) DO UPDATE SET
|
|
221
248
|
version = excluded.version,
|
|
222
249
|
members = excluded.members,
|
|
223
250
|
deleted = 0,
|
|
224
|
-
updated_at = excluded.updated_at`, group, version, JSON.stringify(after), now);
|
|
251
|
+
updated_at = excluded.updated_at`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, version, JSON.stringify(after), now);
|
|
225
252
|
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stagePublication).call(this, sql, group, { snapshot: current });
|
|
226
253
|
}
|
|
227
254
|
const changedChannels = __classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_writeStates).call(this, sql, group, changes, stateRows);
|
|
@@ -239,15 +266,16 @@ export class DurabilityTopology {
|
|
|
239
266
|
async delete(group) {
|
|
240
267
|
assertGroup(group);
|
|
241
268
|
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
242
|
-
const row = readGroup(sql, group);
|
|
269
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
243
270
|
if (row?.deleted !== 0) {
|
|
244
271
|
return;
|
|
245
272
|
}
|
|
246
273
|
const version = row.version + 1;
|
|
247
274
|
sql.exec(`UPDATE durability_topology_groups
|
|
248
275
|
SET version = ?, members = '[]', deleted = 1, updated_at = ?
|
|
249
|
-
WHERE group_id = ?`, version, Date.now(), group);
|
|
250
|
-
sql.exec(
|
|
276
|
+
WHERE owner_channel = ? AND group_id = ?`, version, Date.now(), __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
277
|
+
sql.exec(`DELETE FROM durability_topology_states
|
|
278
|
+
WHERE owner_channel = ? AND group_id = ?`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
251
279
|
for (const member of parseMembers(row)) {
|
|
252
280
|
for (const channel of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f").values()) {
|
|
253
281
|
discardWithin(channel.outbox, sql, deliveryKey(group, member.id));
|
|
@@ -260,7 +288,7 @@ export class DurabilityTopology {
|
|
|
260
288
|
/** Coordinator-local read. Readers elsewhere use the published copy. */
|
|
261
289
|
snapshot(group) {
|
|
262
290
|
assertGroup(group);
|
|
263
|
-
const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, group);
|
|
291
|
+
const row = readGroup(__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
264
292
|
if (row?.deleted !== 0) {
|
|
265
293
|
return undefined;
|
|
266
294
|
}
|
|
@@ -272,7 +300,7 @@ export class DurabilityTopology {
|
|
|
272
300
|
};
|
|
273
301
|
}
|
|
274
302
|
}
|
|
275
|
-
_DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
|
|
303
|
+
_DurabilityTopology_engine = new WeakMap(), _DurabilityTopology_ownerChannel = new WeakMap(), _DurabilityTopology_coordinator = new WeakMap(), _DurabilityTopology_publication = new WeakMap(), _DurabilityTopology_channels = new WeakMap(), _DurabilityTopology_instances = new WeakSet(), _DurabilityTopology_publishNow = function _DurabilityTopology_publishNow(group) {
|
|
276
304
|
return __classPrivateFieldGet(this, _DurabilityTopology_publication, "f")
|
|
277
305
|
? publishNow(__classPrivateFieldGet(this, _DurabilityTopology_publication, "f"), group)
|
|
278
306
|
: Promise.resolve(true);
|
|
@@ -296,8 +324,9 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
296
324
|
}));
|
|
297
325
|
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
298
326
|
for (const { entry, id } of writes) {
|
|
299
|
-
sql.exec(`INSERT INTO durability_topology_writes
|
|
300
|
-
|
|
327
|
+
sql.exec(`INSERT INTO durability_topology_writes
|
|
328
|
+
(owner_channel, id, group_id, land_by)
|
|
329
|
+
VALUES (?, ?, ?, ?)`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id, entry.key, landBy);
|
|
301
330
|
}
|
|
302
331
|
});
|
|
303
332
|
await Promise.all(writes.map(async ({ entry, id }) => {
|
|
@@ -314,15 +343,15 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
314
343
|
}
|
|
315
344
|
else {
|
|
316
345
|
// A rejected write settled before any newer write of this group started.
|
|
317
|
-
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, id));
|
|
346
|
+
await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id));
|
|
318
347
|
}
|
|
319
348
|
reportFailure('durability.topology.publish_failed', { group: entry.key, attempt: entry.attempt }, error);
|
|
320
349
|
entry.retry();
|
|
321
350
|
return;
|
|
322
351
|
}
|
|
323
352
|
const outstandingUntil = await __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
324
|
-
forgetWrite(sql, id);
|
|
325
|
-
return outstandingWritesUntil(sql, entry.key, Date.now());
|
|
353
|
+
forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
|
|
354
|
+
return outstandingWritesUntil(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), entry.key, Date.now());
|
|
326
355
|
});
|
|
327
356
|
if (outstandingUntil !== undefined) {
|
|
328
357
|
entry.retry(Math.min(outstandingUntil - Date.now(), maxTimeoutMs));
|
|
@@ -333,13 +362,13 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
333
362
|
const settled = write
|
|
334
363
|
.then(() => undefined, () => undefined)
|
|
335
364
|
.then(() => __classPrivateFieldGet(this, _DurabilityTopology_engine, "f").transaction((sql) => {
|
|
336
|
-
forgetWrite(sql, id);
|
|
365
|
+
forgetWrite(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), id);
|
|
337
366
|
__classPrivateFieldGet(this, _DurabilityTopology_instances, "m", _DurabilityTopology_stageLatest).call(this, sql, group);
|
|
338
367
|
}))
|
|
339
368
|
.catch(onError);
|
|
340
369
|
__classPrivateFieldGet(this, _DurabilityTopology_engine, "f").waitUntil(settled, onError);
|
|
341
370
|
}, _DurabilityTopology_stageLatest = function _DurabilityTopology_stageLatest(sql, group) {
|
|
342
|
-
const row = readGroup(sql, group);
|
|
371
|
+
const row = readGroup(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group);
|
|
343
372
|
if (!row) {
|
|
344
373
|
return;
|
|
345
374
|
}
|
|
@@ -430,14 +459,19 @@ async function _DurabilityTopology_publishSnapshots(publisher, entries, timeoutM
|
|
|
430
459
|
if (previous !== undefined && canonical(previous) === canonical(stored)) {
|
|
431
460
|
continue;
|
|
432
461
|
}
|
|
433
|
-
sql.exec(`INSERT INTO durability_topology_states
|
|
434
|
-
|
|
435
|
-
|
|
462
|
+
sql.exec(`INSERT INTO durability_topology_states
|
|
463
|
+
(owner_channel, group_id, channel, value)
|
|
464
|
+
VALUES (?, ?, ?, ?)
|
|
465
|
+
ON CONFLICT(owner_channel, group_id, channel) DO UPDATE SET
|
|
466
|
+
value = excluded.value`, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group, channel, stored);
|
|
436
467
|
changed.add(channel);
|
|
437
468
|
}
|
|
438
469
|
return changed;
|
|
439
470
|
}, _DurabilityTopology_stageDeliveries = function _DurabilityTopology_stageDeliveries(sql, group, topologyVersion, before, after, changed) {
|
|
440
|
-
const values = new Map(readStates(sql, group).map((row) => [
|
|
471
|
+
const values = new Map(readStates(sql, __classPrivateFieldGet(this, _DurabilityTopology_ownerChannel, "f"), group).map((row) => [
|
|
472
|
+
row.channel,
|
|
473
|
+
row.value,
|
|
474
|
+
]));
|
|
441
475
|
for (const [name, channel] of __classPrivateFieldGet(this, _DurabilityTopology_channels, "f")) {
|
|
442
476
|
const previous = new Set(before
|
|
443
477
|
.filter((member) => inRole(member, channel.role))
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for the experimental topology coordinator.
|
|
3
|
+
* Publication and sync ride on outbox channels. Applied automatically on
|
|
4
|
+
* construction.
|
|
5
|
+
*/
|
|
6
|
+
export declare const durabilityTopologyMigrations: {
|
|
7
|
+
name: string;
|
|
8
|
+
up: string;
|
|
9
|
+
down: string;
|
|
10
|
+
}[];
|