durability 4.2.0 → 4.3.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 +203 -2
- package/dist/dev.d.ts +22 -0
- package/dist/dev.js +149 -0
- package/dist/durability-alarms.d.ts +1 -1
- package/dist/durability-alarms.js +6 -4
- package/dist/durability.d.ts +1 -1
- package/dist/durability.js +8 -5
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +22 -0
- package/dist/experimental.d.ts +5 -0
- package/dist/experimental.js +3 -0
- package/dist/fanout.js +14 -9
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/lifecycle.js +3 -2
- package/dist/log.js +26 -4
- package/dist/migrations.d.ts +47 -2
- package/dist/migrations.js +132 -36
- package/dist/outbox.d.ts +13 -0
- package/dist/outbox.js +78 -30
- package/dist/scheduler.d.ts +7 -0
- package/dist/scheduler.js +26 -7
- package/dist/semaphore-leases.js +15 -9
- package/dist/semaphore-queue-store.d.ts +12 -2
- package/dist/semaphore-queue-store.js +32 -5
- package/dist/semaphore-queue.js +109 -43
- package/dist/semaphore-server.js +7 -9
- package/dist/socket.js +19 -10
- package/dist/topology-client.d.ts +40 -0
- package/dist/topology-client.js +180 -0
- package/dist/topology-snapshot.d.ts +70 -0
- package/dist/topology-snapshot.js +48 -0
- package/dist/topology.d.ts +94 -0
- package/dist/topology.js +468 -0
- package/dist/tracker.js +2 -0
- package/package.json +2 -2
package/dist/outbox.js
CHANGED
|
@@ -9,9 +9,10 @@ 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 _DurabilityOutbox_instances, _DurabilityOutbox_engine, _DurabilityOutbox_channel, _DurabilityOutbox_publish, _DurabilityOutbox_maxBatchSize, _DurabilityOutbox_batchDelayMs, _DurabilityOutbox_attemptTimeoutMs, _DurabilityOutbox_delay, _DurabilityOutbox_emit, _DurabilityOutbox_running, _DurabilityOutbox_stageIn, _DurabilityOutbox_emitRegistered, _DurabilityOutbox_attach, _DurabilityOutbox_firstAttemptAt, _DurabilityOutbox_runDue, _DurabilityOutbox_runBatch, _DurabilityOutbox_publishBatch, _DurabilityOutbox_retryDelay, _DurabilityOutbox_deadline;
|
|
12
|
+
var _DurabilityOutbox_instances, _DurabilityOutbox_engine, _DurabilityOutbox_channel, _DurabilityOutbox_publish, _DurabilityOutbox_maxBatchSize, _DurabilityOutbox_batchDelayMs, _DurabilityOutbox_attemptTimeoutMs, _DurabilityOutbox_delay, _DurabilityOutbox_emit, _DurabilityOutbox_running, _DurabilityOutbox_stageIn, _DurabilityOutbox_emitRegistered, _DurabilityOutbox_attach, _DurabilityOutbox_firstAttemptAt, _DurabilityOutbox_runDue, _DurabilityOutbox_publishNow, _DurabilityOutbox_exclusive, _DurabilityOutbox_runBatch, _DurabilityOutbox_publishBatch, _DurabilityOutbox_retryDelay, _DurabilityOutbox_deadline;
|
|
13
|
+
import { traceLifecycle, traceMethods } from './dev.js';
|
|
13
14
|
import { OutboxTimeoutError, reportFailure, serializeError, } from './errors.js';
|
|
14
|
-
import { durabilityOutboxMigrations,
|
|
15
|
+
import { durabilityOutboxMigrations, migrateCapability, } from './migrations.js';
|
|
15
16
|
import { assertPositiveInteger, } from './policy.js';
|
|
16
17
|
import { engineFor, } from './scheduler.js';
|
|
17
18
|
import { deserialize, serialize } from './serialization.js';
|
|
@@ -35,11 +36,12 @@ const outboxRowSchema = z.object({
|
|
|
35
36
|
});
|
|
36
37
|
const versionRowSchema = z.object({ version: z.number().int().positive() });
|
|
37
38
|
const channelRowSchema = z.object({ channel: z.string() });
|
|
38
|
-
const listDueForChannel = (sql, channel, now, limit) => outboxRowSchema.array().parse(sql
|
|
39
|
+
const listDueForChannel = (sql, channel, now, limit, key) => outboxRowSchema.array().parse(sql
|
|
39
40
|
.exec(`SELECT * FROM durability_outbox
|
|
40
41
|
WHERE channel = ? AND status = 'pending' AND next_attempt_at <= ?
|
|
42
|
+
AND (? IS NULL OR entry_key = ?)
|
|
41
43
|
ORDER BY next_attempt_at ASC, version ASC
|
|
42
|
-
LIMIT ?`, channel, now, limit)
|
|
44
|
+
LIMIT ?`, channel, now, key ?? null, key ?? null, limit)
|
|
43
45
|
.toArray());
|
|
44
46
|
/** Allocates a version from a durable counter that survives row deletion. */
|
|
45
47
|
const nextVersion = (sql) => {
|
|
@@ -60,6 +62,19 @@ const nextVersion = (sql) => {
|
|
|
60
62
|
* the class's static block.
|
|
61
63
|
*/
|
|
62
64
|
export let stageWithin;
|
|
65
|
+
/**
|
|
66
|
+
* Package-internal: drops a key's pending version inside a held transaction.
|
|
67
|
+
* An in-flight publication of it still completes, but its acknowledgement and
|
|
68
|
+
* retry both miss.
|
|
69
|
+
*/
|
|
70
|
+
export let discardWithin;
|
|
71
|
+
/**
|
|
72
|
+
* Package-internal: publishes a key's pending version now, even one waiting
|
|
73
|
+
* out a retry delay or batch window, serialized with the channel's other
|
|
74
|
+
* publish runs, and reports whether nothing is left pending for it. Takes no
|
|
75
|
+
* scheduler permit, so a caller holding the last one cannot deadlock.
|
|
76
|
+
*/
|
|
77
|
+
export let publishNow;
|
|
63
78
|
const infrastructureRetryMs = 60000;
|
|
64
79
|
// setTimeout overflows past 2^31 - 1 ms, and it keeps every derived timestamp
|
|
65
80
|
// far inside the safe-integer range the storage indexes accept.
|
|
@@ -117,6 +132,7 @@ export class DurabilityOutbox {
|
|
|
117
132
|
_DurabilityOutbox_attemptTimeoutMs.set(this, void 0);
|
|
118
133
|
_DurabilityOutbox_delay.set(this, void 0);
|
|
119
134
|
_DurabilityOutbox_emit.set(this, void 0);
|
|
135
|
+
/** The channel's one publish run, never one still waiting for a permit. */
|
|
120
136
|
_DurabilityOutbox_running.set(this, void 0);
|
|
121
137
|
__classPrivateFieldSet(this, _DurabilityOutbox_channel, config.channel ?? 'default', "f");
|
|
122
138
|
if (typeof __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f") !== 'string' || __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f").length === 0) {
|
|
@@ -142,12 +158,12 @@ export class DurabilityOutbox {
|
|
|
142
158
|
__classPrivateFieldSet(this, _DurabilityOutbox_delay, config.retries?.delay ?? ((attempt) => jitter(exponential(attempt))), "f");
|
|
143
159
|
__classPrivateFieldSet(this, _DurabilityOutbox_publish, config.publish, "f");
|
|
144
160
|
__classPrivateFieldSet(this, _DurabilityOutbox_engine, engineFor(config), "f");
|
|
145
|
-
__classPrivateFieldSet(this, _DurabilityOutbox_emit, __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").emitter(config.onLifecycleEvent), "f");
|
|
161
|
+
__classPrivateFieldSet(this, _DurabilityOutbox_emit, traceLifecycle(__classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").emitter(config.onLifecycleEvent)), "f");
|
|
146
162
|
__classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").migrateSchema('outbox', durabilityOutboxMigrations);
|
|
147
163
|
__classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_attach).call(this);
|
|
148
164
|
}
|
|
149
165
|
static migrate(context, target) {
|
|
150
|
-
return
|
|
166
|
+
return migrateCapability(context.storage, 'outbox', durabilityOutboxMigrations, target);
|
|
151
167
|
}
|
|
152
168
|
alarm(info) {
|
|
153
169
|
return __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").alarm(info);
|
|
@@ -222,42 +238,69 @@ _DurabilityOutbox_engine = new WeakMap(), _DurabilityOutbox_channel = new WeakMa
|
|
|
222
238
|
return createdAt;
|
|
223
239
|
}
|
|
224
240
|
return Math.ceil(createdAt / __classPrivateFieldGet(this, _DurabilityOutbox_batchDelayMs, "f")) * __classPrivateFieldGet(this, _DurabilityOutbox_batchDelayMs, "f");
|
|
225
|
-
}, _DurabilityOutbox_runDue = function _DurabilityOutbox_runDue() {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
return __classPrivateFieldGet(this, _DurabilityOutbox_running, "f");
|
|
230
|
-
}, _DurabilityOutbox_runBatch = async function _DurabilityOutbox_runBatch() {
|
|
241
|
+
}, _DurabilityOutbox_runDue = async function _DurabilityOutbox_runDue() {
|
|
242
|
+
if (__classPrivateFieldGet(this, _DurabilityOutbox_running, "f")) {
|
|
243
|
+
return __classPrivateFieldGet(this, _DurabilityOutbox_running, "f");
|
|
244
|
+
}
|
|
231
245
|
// Every transaction reconciles the alarm; one per idle run keeps re-arming
|
|
232
246
|
// it while anything else on the scheduler is due, such as a running operation.
|
|
233
247
|
if (listDueForChannel(__classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").sql, __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"), Date.now(), 1)
|
|
234
248
|
.length === 0) {
|
|
235
|
-
return;
|
|
249
|
+
return undefined;
|
|
236
250
|
}
|
|
251
|
+
// The permit is taken before claiming the channel, so nothing that holds
|
|
252
|
+
// the channel ever waits for a permit.
|
|
237
253
|
const release = await __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").acquirePermit();
|
|
238
254
|
try {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const deadline = Date.now() + __classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f");
|
|
242
|
-
const claimed = [];
|
|
243
|
-
for (const row of due) {
|
|
244
|
-
const attempt = claimAttempt(sql, outboxTable, row.id, row.generation_id, Number.MAX_SAFE_INTEGER);
|
|
245
|
-
if (attempt === undefined) {
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
// A crash mid-publication leaves the claim due again at the deadline.
|
|
249
|
-
updateAttempt(sql, outboxTable, { key: row.id, generation: row.generation_id, attempt }, 'next_attempt_at = ?', deadline);
|
|
250
|
-
claimed.push({ ...row, attempt });
|
|
251
|
-
}
|
|
252
|
-
return claimed;
|
|
253
|
-
});
|
|
254
|
-
if (claims.length > 0) {
|
|
255
|
-
await __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_publishBatch).call(this, claims);
|
|
255
|
+
if (__classPrivateFieldGet(this, _DurabilityOutbox_running, "f")) {
|
|
256
|
+
return await __classPrivateFieldGet(this, _DurabilityOutbox_running, "f");
|
|
256
257
|
}
|
|
258
|
+
return await __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_exclusive).call(this, () => __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_runBatch).call(this));
|
|
257
259
|
}
|
|
258
260
|
finally {
|
|
259
261
|
release();
|
|
260
262
|
}
|
|
263
|
+
}, _DurabilityOutbox_publishNow = async function _DurabilityOutbox_publishNow(key) {
|
|
264
|
+
const pending = () => __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").sql
|
|
265
|
+
.exec('SELECT 1 FROM durability_outbox WHERE id = ?', rowId(__classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"), key))
|
|
266
|
+
.toArray().length > 0;
|
|
267
|
+
while (__classPrivateFieldGet(this, _DurabilityOutbox_running, "f")) {
|
|
268
|
+
// eslint-disable-next-line no-await-in-loop
|
|
269
|
+
await __classPrivateFieldGet(this, _DurabilityOutbox_running, "f").catch(() => undefined);
|
|
270
|
+
}
|
|
271
|
+
if (!pending()) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
await __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_exclusive).call(this, () => __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_runBatch).call(this, key));
|
|
275
|
+
return !pending();
|
|
276
|
+
}, _DurabilityOutbox_exclusive = function _DurabilityOutbox_exclusive(run) {
|
|
277
|
+
const running = run().finally(() => {
|
|
278
|
+
if (__classPrivateFieldGet(this, _DurabilityOutbox_running, "f") === running) {
|
|
279
|
+
__classPrivateFieldSet(this, _DurabilityOutbox_running, undefined, "f");
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
__classPrivateFieldSet(this, _DurabilityOutbox_running, running, "f");
|
|
283
|
+
return running;
|
|
284
|
+
}, _DurabilityOutbox_runBatch = async function _DurabilityOutbox_runBatch(key) {
|
|
285
|
+
const claims = await __classPrivateFieldGet(this, _DurabilityOutbox_engine, "f").transaction((sql) => {
|
|
286
|
+
// No run is in flight here, so a key's future attempt time is only a delay to skip.
|
|
287
|
+
const due = listDueForChannel(sql, __classPrivateFieldGet(this, _DurabilityOutbox_channel, "f"), key === undefined ? Date.now() : Number.MAX_SAFE_INTEGER, __classPrivateFieldGet(this, _DurabilityOutbox_maxBatchSize, "f"), key);
|
|
288
|
+
const deadline = Date.now() + __classPrivateFieldGet(this, _DurabilityOutbox_attemptTimeoutMs, "f");
|
|
289
|
+
const claimed = [];
|
|
290
|
+
for (const row of due) {
|
|
291
|
+
const attempt = claimAttempt(sql, outboxTable, row.id, row.generation_id, Number.MAX_SAFE_INTEGER);
|
|
292
|
+
if (attempt === undefined) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
// A crash mid-publication leaves the claim due again at the deadline.
|
|
296
|
+
updateAttempt(sql, outboxTable, { key: row.id, generation: row.generation_id, attempt }, 'next_attempt_at = ?', deadline);
|
|
297
|
+
claimed.push({ ...row, attempt });
|
|
298
|
+
}
|
|
299
|
+
return claimed;
|
|
300
|
+
});
|
|
301
|
+
if (claims.length > 0) {
|
|
302
|
+
await __classPrivateFieldGet(this, _DurabilityOutbox_instances, "m", _DurabilityOutbox_publishBatch).call(this, claims);
|
|
303
|
+
}
|
|
261
304
|
}, _DurabilityOutbox_publishBatch = async function _DurabilityOutbox_publishBatch(claims) {
|
|
262
305
|
const startedAt = Date.now();
|
|
263
306
|
const retries = new Map();
|
|
@@ -382,4 +425,9 @@ _DurabilityOutbox_engine = new WeakMap(), _DurabilityOutbox_channel = new WeakMa
|
|
|
382
425
|
__classPrivateFieldGet(outbox, _DurabilityOutbox_instances, "m", _DurabilityOutbox_emitRegistered).call(outbox, row);
|
|
383
426
|
return row.version;
|
|
384
427
|
};
|
|
428
|
+
discardWithin = (outbox, sql, key) => {
|
|
429
|
+
sql.exec('DELETE FROM durability_outbox WHERE id = ?', rowId(__classPrivateFieldGet(outbox, _DurabilityOutbox_channel, "f"), key));
|
|
430
|
+
};
|
|
431
|
+
publishNow = (outbox, key) => __classPrivateFieldGet(outbox, _DurabilityOutbox_instances, "m", _DurabilityOutbox_publishNow).call(outbox, key);
|
|
385
432
|
})();
|
|
433
|
+
traceMethods(DurabilityOutbox.prototype, 'DurabilityOutbox');
|
package/dist/scheduler.d.ts
CHANGED
|
@@ -79,10 +79,12 @@ export type ExecutionSpec = {
|
|
|
79
79
|
export declare class Engine {
|
|
80
80
|
readonly sql: SqlStorage;
|
|
81
81
|
private readonly context;
|
|
82
|
+
private readonly storage;
|
|
82
83
|
private readonly alarmConcurrency;
|
|
83
84
|
private readonly alarmMinDelayMs;
|
|
84
85
|
private readonly alarmHandoffMs;
|
|
85
86
|
private readonly participants;
|
|
87
|
+
private readonly migrated;
|
|
86
88
|
private availablePermits;
|
|
87
89
|
private readonly permitWaiters;
|
|
88
90
|
private alarmRefresh;
|
|
@@ -91,6 +93,11 @@ export declare class Engine {
|
|
|
91
93
|
constructor(context: DurabilityContext, options: DurabilitySchedulerOptions);
|
|
92
94
|
register(participant: Participant): void;
|
|
93
95
|
waitUntil(promise: Promise<unknown>, onError: (error: unknown) => void): void;
|
|
96
|
+
/**
|
|
97
|
+
* The owning Durable Object's ID. The public context type omits it, but the
|
|
98
|
+
* `DurableObjectState` applications pass at runtime carries it.
|
|
99
|
+
*/
|
|
100
|
+
objectId(): string | undefined;
|
|
94
101
|
emitter<Event extends {
|
|
95
102
|
entityKind: string;
|
|
96
103
|
type: string;
|
package/dist/scheduler.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { trace, traceMethods, traceStorage } from './dev.js';
|
|
2
3
|
import { DurableRetryPolicyError, isErrorInstance, isNonRetryable, reportFailure, serializeError, } from './errors.js';
|
|
3
4
|
import { createEmitter, } from './lifecycle.js';
|
|
4
|
-
import {
|
|
5
|
+
import { migrateCapability } from './migrations.js';
|
|
5
6
|
import { assertPositiveInteger } from './policy.js';
|
|
6
7
|
import { claimAttempt, exhaustAttempts, updateAttempt, } from './storage.js';
|
|
7
8
|
const minAlarmDelayMs = 1000;
|
|
@@ -55,10 +56,12 @@ const decideRetry = (spec, attempt, error) => {
|
|
|
55
56
|
export class Engine {
|
|
56
57
|
constructor(context, options) {
|
|
57
58
|
this.participants = [];
|
|
59
|
+
this.migrated = new Set();
|
|
58
60
|
this.permitWaiters = [];
|
|
59
61
|
this.watchers = new Map();
|
|
60
62
|
this.context = context;
|
|
61
|
-
this.
|
|
63
|
+
this.storage = traceStorage(context.storage);
|
|
64
|
+
this.sql = this.storage.sql;
|
|
62
65
|
this.alarmConcurrency = options.alarmConcurrency ?? 10;
|
|
63
66
|
this.alarmMinDelayMs = options.alarmMinDelayMs ?? 15000;
|
|
64
67
|
this.alarmHandoffMs = options.alarmHandoffMs ?? 14 * 60000;
|
|
@@ -89,16 +92,30 @@ export class Engine {
|
|
|
89
92
|
onError(error);
|
|
90
93
|
}
|
|
91
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The owning Durable Object's ID. The public context type omits it, but the
|
|
97
|
+
* `DurableObjectState` applications pass at runtime carries it.
|
|
98
|
+
*/
|
|
99
|
+
objectId() {
|
|
100
|
+
const id = Reflect.get(this.context, 'id');
|
|
101
|
+
return id === undefined || id === null ? undefined : String(id);
|
|
102
|
+
}
|
|
92
103
|
emitter(hook) {
|
|
93
104
|
return createEmitter(hook, (promise) => this.context.waitUntil?.(promise));
|
|
94
105
|
}
|
|
95
106
|
/** Applies a helper's SQLite migrations. */
|
|
96
107
|
migrateSchema(capability, migrations) {
|
|
97
|
-
|
|
108
|
+
// Helpers of one kind on one scheduler share their tables; checking the
|
|
109
|
+
// schema again for each would only add statements to every construction.
|
|
110
|
+
if (this.migrated.has(capability)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
migrateCapability(this.storage, capability, migrations, undefined);
|
|
114
|
+
this.migrated.add(capability);
|
|
98
115
|
}
|
|
99
116
|
/** Runs synchronous writes atomically without touching the physical alarm. */
|
|
100
117
|
transactionSync(write) {
|
|
101
|
-
return this.
|
|
118
|
+
return this.storage.transactionSync(() => write(this.sql));
|
|
102
119
|
}
|
|
103
120
|
/**
|
|
104
121
|
* Runs the writes and reconciles the physical alarm in one storage
|
|
@@ -106,7 +123,7 @@ export class Engine {
|
|
|
106
123
|
*/
|
|
107
124
|
async transaction(write) {
|
|
108
125
|
let nextPendingAt;
|
|
109
|
-
const result = await this.
|
|
126
|
+
const result = await this.storage.transaction(async (transaction) => {
|
|
110
127
|
const value = write(this.sql);
|
|
111
128
|
nextPendingAt = await this.reconcile(transaction);
|
|
112
129
|
return value;
|
|
@@ -165,6 +182,7 @@ export class Engine {
|
|
|
165
182
|
return;
|
|
166
183
|
}
|
|
167
184
|
this.earlyAlarm = undefined;
|
|
185
|
+
trace('scheduler.early_alarm', { scheduledTime: timestamp });
|
|
168
186
|
const onError = (error) => reportFailure('durability.background_execution.failed', { scheduledTime: timestamp }, error);
|
|
169
187
|
const execution = this.alarm().catch(onError);
|
|
170
188
|
this.waitUntil(execution, onError);
|
|
@@ -326,7 +344,7 @@ export class Engine {
|
|
|
326
344
|
/** Arms the physical alarm directly, outside any transaction; the next run reconciles properly. */
|
|
327
345
|
async armFallbackAlarm() {
|
|
328
346
|
try {
|
|
329
|
-
await this.
|
|
347
|
+
await this.storage.setAlarm(Date.now() + this.alarmMinDelayMs);
|
|
330
348
|
return true;
|
|
331
349
|
}
|
|
332
350
|
catch {
|
|
@@ -334,7 +352,7 @@ export class Engine {
|
|
|
334
352
|
}
|
|
335
353
|
}
|
|
336
354
|
armForHandoff(timestamp) {
|
|
337
|
-
return this.
|
|
355
|
+
return this.storage.transaction(async (physicalAlarm) => {
|
|
338
356
|
const currentAlarm = await physicalAlarm.getAlarm();
|
|
339
357
|
const target = Math.max(timestamp, Date.now() + this.alarmMinDelayMs);
|
|
340
358
|
if (currentAlarm === null || currentAlarm > target) {
|
|
@@ -519,6 +537,7 @@ export class DurabilityScheduler {
|
|
|
519
537
|
return engineFor({ scheduler: this }).alarm(info);
|
|
520
538
|
}
|
|
521
539
|
}
|
|
540
|
+
traceMethods(DurabilityScheduler.prototype, 'DurabilityScheduler');
|
|
522
541
|
/**
|
|
523
542
|
* Resolves the engine a helper should attach to: the shared one behind a
|
|
524
543
|
* scheduler, or a private one built from the Durable Object context.
|
package/dist/semaphore-leases.js
CHANGED
|
@@ -75,7 +75,7 @@ export const applyLeaseReports = (store, policy, clientId, reports, now) => {
|
|
|
75
75
|
};
|
|
76
76
|
for (const report of reports) {
|
|
77
77
|
const lease = store.getLease(report.leaseId);
|
|
78
|
-
if (!lease || lease.client_id !== clientId
|
|
78
|
+
if (!lease || lease.client_id !== clientId) {
|
|
79
79
|
outcome.echoes.push({
|
|
80
80
|
leaseId: report.leaseId,
|
|
81
81
|
group: lease?.grp ?? '',
|
|
@@ -90,13 +90,19 @@ export const applyLeaseReports = (store, policy, clientId, reports, now) => {
|
|
|
90
90
|
const drawn = Math.min(Math.max(0, report.drawn), lease.permits);
|
|
91
91
|
outcome.drawn.set(lease.lease_id, drawn);
|
|
92
92
|
const released = Math.max(lease.released, Math.min(Math.max(0, report.released), drawn));
|
|
93
|
+
// An expired lease is retired, not forgotten: its permits may be running.
|
|
93
94
|
// Only the client's own retired report shrinks a lease to what it drew:
|
|
94
95
|
// until then it may still be drawing, whoever asked it to stop.
|
|
95
|
-
const retired = lease.retired === 1 || report.retired === true;
|
|
96
|
+
const retired = lease.retired === 1 || report.retired === true || lease.expires_at <= now;
|
|
96
97
|
const permits = report.retired === true ? Math.max(released, drawn) : lease.permits;
|
|
97
98
|
if (released >= permits) {
|
|
98
99
|
store.removeLease(lease.lease_id);
|
|
99
|
-
|
|
100
|
+
// Echoed as settled (permits = released) so the client can drop the lease.
|
|
101
|
+
outcome.echoes.push({
|
|
102
|
+
...toLeaseGrant(lease, now),
|
|
103
|
+
permits: released,
|
|
104
|
+
expiresAt: now,
|
|
105
|
+
});
|
|
100
106
|
continue;
|
|
101
107
|
}
|
|
102
108
|
const refreshed = {
|
|
@@ -104,7 +110,7 @@ export const applyLeaseReports = (store, policy, clientId, reports, now) => {
|
|
|
104
110
|
permits,
|
|
105
111
|
released,
|
|
106
112
|
retired: retired ? 1 : 0,
|
|
107
|
-
expires_at: now + policy.ttlMs,
|
|
113
|
+
expires_at: retired ? lease.expires_at : now + policy.ttlMs,
|
|
108
114
|
};
|
|
109
115
|
store.updateLease(refreshed);
|
|
110
116
|
outcome.echoes.push(toLeaseGrant(refreshed, now));
|
|
@@ -127,13 +133,13 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
127
133
|
const groupFootprint = new Map();
|
|
128
134
|
const usable = new Set();
|
|
129
135
|
for (const lease of store.listLeases()) {
|
|
130
|
-
if (lease.expires_at <= now) {
|
|
131
|
-
store.removeLease(lease.lease_id);
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
136
|
const group = input.policies.get(lease.grp) ?? {};
|
|
135
137
|
let current = lease;
|
|
136
|
-
|
|
138
|
+
// An unreported lease is retired, not swept: its client may be cut off
|
|
139
|
+
// with work still running, so its footprint stays until it reports, as a
|
|
140
|
+
// claimed grant permit stays until it is released.
|
|
141
|
+
if (!lease.retired &&
|
|
142
|
+
(lease.expires_at <= now || !leaseEligible(policy, group))) {
|
|
137
143
|
// Stop draws now; the full footprint stays until the client confirms.
|
|
138
144
|
current = { ...lease, retired: 1 };
|
|
139
145
|
store.updateLease(current);
|
|
@@ -45,12 +45,16 @@ export declare const queueLeaseRowSchema: z.ZodObject<{
|
|
|
45
45
|
released: z.ZodNumber;
|
|
46
46
|
created_at: z.ZodNumber;
|
|
47
47
|
expires_at: z.ZodNumber;
|
|
48
|
+
renew_at: z.ZodNumber;
|
|
49
|
+
renew_ms: z.ZodNumber;
|
|
50
|
+
confirmed: z.ZodNumber;
|
|
48
51
|
}, z.core.$strip>;
|
|
49
52
|
export type QueueLeaseRow = z.infer<typeof queueLeaseRowSchema>;
|
|
50
53
|
/**
|
|
51
54
|
* A semaphore client's queue: items, the grants it holds per key, and per-key
|
|
52
55
|
* request pacing. Due work is a key whose request time has passed with items
|
|
53
|
-
* queued,
|
|
56
|
+
* queued, an admitted item whose `admit()` notification is outstanding, or a
|
|
57
|
+
* lease the holder must hear about again.
|
|
54
58
|
*/
|
|
55
59
|
export declare class QueueStore {
|
|
56
60
|
private readonly sql;
|
|
@@ -73,7 +77,13 @@ export declare class QueueStore {
|
|
|
73
77
|
getLease(leaseId: string): QueueLeaseRow | undefined;
|
|
74
78
|
insertLease(row: QueueLeaseRow): boolean;
|
|
75
79
|
updateLease(row: QueueLeaseRow): void;
|
|
76
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Leases the holder must hear about again: those with permits in use, and
|
|
82
|
+
* stopped ones it has not settled. Both stay counted at the holder until
|
|
83
|
+
* reported, so a lost report must be repeated.
|
|
84
|
+
*/
|
|
85
|
+
listRenewalsDue(now: number, limit: number): QueueLeaseRow[];
|
|
86
|
+
/** Drops settled leases that stopped drawing before `before`. */
|
|
77
87
|
removeSpentLeases(before: number): void;
|
|
78
88
|
listLeases(key: string): QueueLeaseRow[];
|
|
79
89
|
getKey(key: string): QueueKeyRow | undefined;
|
|
@@ -45,6 +45,12 @@ export const queueLeaseRowSchema = z.object({
|
|
|
45
45
|
released: z.number().int().nonnegative(),
|
|
46
46
|
created_at: z.number().int().nonnegative(),
|
|
47
47
|
expires_at: z.number().int().nonnegative(),
|
|
48
|
+
/** When to report the lease again while it has permits in use, so the holder keeps it. */
|
|
49
|
+
renew_at: z.number().int().nonnegative(),
|
|
50
|
+
/** Half the lease's lifetime at the holder, from its last refresh. */
|
|
51
|
+
renew_ms: z.number().int().positive(),
|
|
52
|
+
/** 1 once the holder has settled the stopped lease; until then it is reported again. */
|
|
53
|
+
confirmed: z.number().int().min(0).max(1),
|
|
48
54
|
});
|
|
49
55
|
const queueItemColumns = queueItemRowSchema.keyof().options;
|
|
50
56
|
const queueGrantColumns = queueGrantRowSchema.keyof().options;
|
|
@@ -56,7 +62,8 @@ const statusCountSchema = z.object({
|
|
|
56
62
|
/**
|
|
57
63
|
* A semaphore client's queue: items, the grants it holds per key, and per-key
|
|
58
64
|
* request pacing. Due work is a key whose request time has passed with items
|
|
59
|
-
* queued,
|
|
65
|
+
* queued, an admitted item whose `admit()` notification is outstanding, or a
|
|
66
|
+
* lease the holder must hear about again.
|
|
60
67
|
*/
|
|
61
68
|
export class QueueStore {
|
|
62
69
|
constructor(sql) {
|
|
@@ -162,13 +169,27 @@ export class QueueStore {
|
|
|
162
169
|
}
|
|
163
170
|
updateLease(row) {
|
|
164
171
|
this.sql.exec(`UPDATE durability_semaphore_queue_leases
|
|
165
|
-
SET permits = ?, drawn = ?, released = ?, expires_at =
|
|
166
|
-
|
|
172
|
+
SET permits = ?, drawn = ?, released = ?, expires_at = ?,
|
|
173
|
+
renew_at = ?, renew_ms = ?, confirmed = ?
|
|
174
|
+
WHERE lease_id = ?`, row.permits, row.drawn, row.released, row.expires_at, row.renew_at, row.renew_ms, row.confirmed, row.lease_id);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Leases the holder must hear about again: those with permits in use, and
|
|
178
|
+
* stopped ones it has not settled. Both stay counted at the holder until
|
|
179
|
+
* reported, so a lost report must be repeated.
|
|
180
|
+
*/
|
|
181
|
+
listRenewalsDue(now, limit) {
|
|
182
|
+
return queueLeaseRowSchema.array().parse(this.sql
|
|
183
|
+
.exec(`SELECT * FROM durability_semaphore_queue_leases
|
|
184
|
+
WHERE confirmed = 0 AND renew_at <= ?
|
|
185
|
+
AND (released < drawn OR expires_at <= ?)
|
|
186
|
+
ORDER BY renew_at ASC LIMIT ?`, now, now, limit)
|
|
187
|
+
.toArray());
|
|
167
188
|
}
|
|
168
|
-
/** Drops leases that stopped drawing before `before
|
|
189
|
+
/** Drops settled leases that stopped drawing before `before`. */
|
|
169
190
|
removeSpentLeases(before) {
|
|
170
191
|
this.sql.exec(`DELETE FROM durability_semaphore_queue_leases
|
|
171
|
-
WHERE expires_at < ? AND released >= drawn`, before);
|
|
192
|
+
WHERE confirmed = 1 AND expires_at < ? AND released >= drawn`, before);
|
|
172
193
|
}
|
|
173
194
|
listLeases(key) {
|
|
174
195
|
return queueLeaseRowSchema.array().parse(this.sql
|
|
@@ -210,6 +231,12 @@ export class QueueStore {
|
|
|
210
231
|
WHERE status = 'admitted' AND (notified = 0 OR ?)
|
|
211
232
|
ORDER BY next_attempt_at ASC LIMIT 1`, checksFinished ? 1 : 0)
|
|
212
233
|
.toArray()[0]?.['at'],
|
|
234
|
+
// An idle lease is due when it stops at its expiry, not before.
|
|
235
|
+
this.sql
|
|
236
|
+
.exec(`SELECT MIN(CASE WHEN released < drawn THEN renew_at
|
|
237
|
+
ELSE MAX(renew_at, expires_at) END) AS at
|
|
238
|
+
FROM durability_semaphore_queue_leases WHERE confirmed = 0`)
|
|
239
|
+
.toArray()[0]?.['at'],
|
|
213
240
|
].filter((value) => typeof value === 'number');
|
|
214
241
|
return candidates.length === 0 ? undefined : Math.min(...candidates);
|
|
215
242
|
}
|