durability 5.1.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 +145 -12
- package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +13 -5
- package/dist/{durability-alarms.js → alarms/index.js} +69 -55
- package/dist/alarms/migrations.d.ts +9 -0
- package/dist/alarms/migrations.js +133 -0
- package/dist/experimental.d.ts +10 -10
- package/dist/experimental.js +7 -7
- package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
- package/dist/{fanout.js → fanout/index.js} +54 -46
- 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 +0 -111
- package/dist/migrations.js +0 -612
- package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
- package/dist/{durability.js → operations/index.js} +59 -47
- 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} +16 -60
- 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.d.ts → schedule/index.d.ts} +16 -8
- package/dist/{schedule.js → schedule/index.js} +65 -49
- package/dist/schedule/migrations.d.ts +9 -0
- package/dist/schedule/migrations.js +118 -0
- package/dist/{scheduler.d.ts → scheduler/index.d.ts} +75 -25
- package/dist/{scheduler.js → scheduler/index.js} +300 -63
- 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} +69 -61
- 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 +4 -0
- package/dist/storage.js +13 -6
- 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/{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
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { assertPositiveInteger } from '
|
|
1
|
+
import { assertPositiveInteger } from '../policy.js';
|
|
2
|
+
/** Map key for a client identity; JSON keeps raw IDs and channels unambiguous. */
|
|
3
|
+
export const clientKey = (clientId, clientChannel) => JSON.stringify([clientId, clientChannel]);
|
|
4
|
+
export const ownedBy = (row, client) => row.client_id === client.clientId &&
|
|
5
|
+
row.client_channel === client.clientChannel;
|
|
2
6
|
const maxDelayMs = 2 ** 31 - 1;
|
|
3
7
|
export const resolveLeasePolicy = (options) => {
|
|
4
8
|
const policy = {
|
|
@@ -67,7 +71,7 @@ export const leaseEligible = (policy, group) => group.blocked === undefined &&
|
|
|
67
71
|
* and charges their reported live footprint so a forgotten lease cannot
|
|
68
72
|
* over-admit for the rest of this round.
|
|
69
73
|
*/
|
|
70
|
-
export const applyLeaseReports = (store, policy,
|
|
74
|
+
export const applyLeaseReports = (store, policy, client, reports, now) => {
|
|
71
75
|
const outcome = {
|
|
72
76
|
echoes: [],
|
|
73
77
|
unknownDebt: 0,
|
|
@@ -75,7 +79,7 @@ export const applyLeaseReports = (store, policy, clientId, reports, now) => {
|
|
|
75
79
|
};
|
|
76
80
|
for (const report of reports) {
|
|
77
81
|
const lease = store.getLease(report.leaseId);
|
|
78
|
-
if (!lease || lease
|
|
82
|
+
if (!lease || !ownedBy(lease, client)) {
|
|
79
83
|
outcome.echoes.push({
|
|
80
84
|
leaseId: report.leaseId,
|
|
81
85
|
group: lease?.grp ?? '',
|
|
@@ -126,8 +130,9 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
126
130
|
const { now } = input;
|
|
127
131
|
const grants = new Map();
|
|
128
132
|
const events = [];
|
|
129
|
-
const echo = (
|
|
130
|
-
|
|
133
|
+
const echo = (lease, grant) => {
|
|
134
|
+
const key = clientKey(lease.client_id, lease.client_channel);
|
|
135
|
+
grants.set(key, [...(grants.get(key) ?? []), grant]);
|
|
131
136
|
};
|
|
132
137
|
let footprint = 0;
|
|
133
138
|
const groupFootprint = new Map();
|
|
@@ -143,13 +148,14 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
143
148
|
// Stop draws now; the full footprint stays until the client confirms.
|
|
144
149
|
current = { ...lease, retired: 1 };
|
|
145
150
|
store.updateLease(current);
|
|
146
|
-
echo(lease
|
|
151
|
+
echo(lease, toLeaseGrant(current, now));
|
|
147
152
|
events.push({
|
|
148
153
|
entityKind: 'semaphore',
|
|
149
154
|
type: 'lease_retired',
|
|
150
155
|
timestamp: now,
|
|
151
156
|
leaseId: lease.lease_id,
|
|
152
157
|
clientId: lease.client_id,
|
|
158
|
+
clientChannel: lease.client_channel,
|
|
153
159
|
group: lease.grp,
|
|
154
160
|
permits: lease.permits - (input.drawn.get(lease.lease_id) ?? lease.permits),
|
|
155
161
|
});
|
|
@@ -160,17 +166,22 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
160
166
|
// An unreported lease is assumed fully drawn, as gatekeeper does.
|
|
161
167
|
if (!current.retired &&
|
|
162
168
|
(input.drawn.get(lease.lease_id) ?? lease.permits) < lease.permits) {
|
|
163
|
-
usable.add(JSON.stringify([lease.client_id, lease.grp]));
|
|
169
|
+
usable.add(JSON.stringify([lease.client_id, lease.client_channel, lease.grp]));
|
|
164
170
|
}
|
|
165
171
|
}
|
|
166
172
|
const byGroup = new Map();
|
|
167
173
|
for (const candidate of input.candidates) {
|
|
168
174
|
if (!leaseEligible(policy, input.policies.get(candidate.group) ?? {}) ||
|
|
169
|
-
usable.has(JSON.stringify([
|
|
175
|
+
usable.has(JSON.stringify([
|
|
176
|
+
candidate.clientId,
|
|
177
|
+
candidate.clientChannel,
|
|
178
|
+
candidate.group,
|
|
179
|
+
]))) {
|
|
170
180
|
continue;
|
|
171
181
|
}
|
|
172
182
|
const existing = byGroup.get(candidate.group) ?? [];
|
|
173
|
-
const prior = existing.find((c) => c.clientId === candidate.clientId
|
|
183
|
+
const prior = existing.find((c) => c.clientId === candidate.clientId &&
|
|
184
|
+
c.clientChannel === candidate.clientChannel);
|
|
174
185
|
if (prior) {
|
|
175
186
|
prior.oldestAt = Math.min(prior.oldestAt, candidate.oldestAt);
|
|
176
187
|
}
|
|
@@ -183,7 +194,9 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
183
194
|
.map(([group, clients]) => ({
|
|
184
195
|
group,
|
|
185
196
|
clients: clients
|
|
186
|
-
.sort((a, b) => a.oldestAt - b.oldestAt ||
|
|
197
|
+
.sort((a, b) => a.oldestAt - b.oldestAt ||
|
|
198
|
+
a.clientId.localeCompare(b.clientId) ||
|
|
199
|
+
a.clientChannel.localeCompare(b.clientChannel))
|
|
187
200
|
.slice(0, policy.maxClientsPerGroup),
|
|
188
201
|
oldestAt: Math.min(...clients.map((c) => c.oldestAt)),
|
|
189
202
|
}))
|
|
@@ -210,13 +223,14 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
210
223
|
const retired = { ...lease, retired: 1 };
|
|
211
224
|
store.updateLease(retired);
|
|
212
225
|
excess -= lease.permits - drawn;
|
|
213
|
-
echo(lease
|
|
226
|
+
echo(lease, toLeaseGrant(retired, now));
|
|
214
227
|
events.push({
|
|
215
228
|
entityKind: 'semaphore',
|
|
216
229
|
type: 'lease_retired',
|
|
217
230
|
timestamp: now,
|
|
218
231
|
leaseId: lease.lease_id,
|
|
219
232
|
clientId: lease.client_id,
|
|
233
|
+
clientChannel: lease.client_channel,
|
|
220
234
|
group: lease.grp,
|
|
221
235
|
permits: lease.permits - drawn,
|
|
222
236
|
});
|
|
@@ -247,6 +261,7 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
247
261
|
const lease = {
|
|
248
262
|
lease_id: crypto.randomUUID(),
|
|
249
263
|
client_id: candidate.clientId,
|
|
264
|
+
client_channel: candidate.clientChannel,
|
|
250
265
|
grp: candidate.group,
|
|
251
266
|
permits,
|
|
252
267
|
released: 0,
|
|
@@ -257,13 +272,14 @@ export const allocateLeases = (store, policy, input) => {
|
|
|
257
272
|
store.insertLease(lease);
|
|
258
273
|
footprint += permits;
|
|
259
274
|
groupFootprint.set(candidate.group, (groupFootprint.get(candidate.group) ?? 0) + permits);
|
|
260
|
-
echo(
|
|
275
|
+
echo(lease, toLeaseGrant(lease, now));
|
|
261
276
|
events.push({
|
|
262
277
|
entityKind: 'semaphore',
|
|
263
278
|
type: 'lease_granted',
|
|
264
279
|
timestamp: now,
|
|
265
280
|
leaseId: lease.lease_id,
|
|
266
281
|
clientId: lease.client_id,
|
|
282
|
+
clientChannel: lease.client_channel,
|
|
267
283
|
group: lease.grp,
|
|
268
284
|
permits,
|
|
269
285
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for semaphore grants. Applied automatically on
|
|
3
|
+
* construction.
|
|
4
|
+
*/
|
|
5
|
+
export declare const durabilitySemaphoreMigrations: {
|
|
6
|
+
name: string;
|
|
7
|
+
up: string;
|
|
8
|
+
down: string;
|
|
9
|
+
}[];
|
|
10
|
+
/**
|
|
11
|
+
* Ordered SQLite migrations for a semaphore client's queue. Applied
|
|
12
|
+
* automatically on construction.
|
|
13
|
+
*/
|
|
14
|
+
export declare const durabilitySemaphoreQueueMigrations: {
|
|
15
|
+
name: string;
|
|
16
|
+
up: string;
|
|
17
|
+
down: string;
|
|
18
|
+
}[];
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered SQLite migrations for semaphore grants. Applied automatically on
|
|
3
|
+
* construction.
|
|
4
|
+
*/
|
|
5
|
+
export const durabilitySemaphoreMigrations = [
|
|
6
|
+
{
|
|
7
|
+
name: 'durability_0007_create_semaphore',
|
|
8
|
+
up: `
|
|
9
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_grants (
|
|
10
|
+
grant_id TEXT PRIMARY KEY,
|
|
11
|
+
client_id TEXT NOT NULL,
|
|
12
|
+
grp TEXT NOT NULL,
|
|
13
|
+
permits INTEGER NOT NULL,
|
|
14
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
15
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
16
|
+
created_at INTEGER NOT NULL,
|
|
17
|
+
expires_at INTEGER NOT NULL
|
|
18
|
+
);
|
|
19
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_grants_expires_idx
|
|
20
|
+
ON durability_semaphore_grants (expires_at) WHERE claimed < permits;
|
|
21
|
+
`,
|
|
22
|
+
down: `
|
|
23
|
+
DROP INDEX IF EXISTS durability_semaphore_grants_expires_idx;
|
|
24
|
+
DROP TABLE IF EXISTS durability_semaphore_grants;
|
|
25
|
+
`,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'durability_0009_create_semaphore_leases',
|
|
29
|
+
up: `
|
|
30
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_leases (
|
|
31
|
+
lease_id TEXT PRIMARY KEY,
|
|
32
|
+
client_id TEXT NOT NULL,
|
|
33
|
+
grp TEXT NOT NULL,
|
|
34
|
+
permits INTEGER NOT NULL,
|
|
35
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
36
|
+
created_at INTEGER NOT NULL,
|
|
37
|
+
expires_at INTEGER NOT NULL,
|
|
38
|
+
retired INTEGER NOT NULL DEFAULT 0
|
|
39
|
+
);
|
|
40
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_leases_expires_idx
|
|
41
|
+
ON durability_semaphore_leases (expires_at);
|
|
42
|
+
`,
|
|
43
|
+
down: `
|
|
44
|
+
DROP INDEX IF EXISTS durability_semaphore_leases_expires_idx;
|
|
45
|
+
DROP TABLE IF EXISTS durability_semaphore_leases;
|
|
46
|
+
`,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'durability_0020_semaphore_channels',
|
|
50
|
+
up: `
|
|
51
|
+
ALTER TABLE durability_semaphore_grants RENAME TO durability_semaphore_grants_before_channels;
|
|
52
|
+
CREATE TABLE durability_semaphore_grants (
|
|
53
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
54
|
+
grant_id TEXT NOT NULL,
|
|
55
|
+
client_id TEXT NOT NULL,
|
|
56
|
+
client_channel TEXT NOT NULL DEFAULT 'default',
|
|
57
|
+
grp TEXT NOT NULL,
|
|
58
|
+
permits INTEGER NOT NULL,
|
|
59
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
60
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
61
|
+
created_at INTEGER NOT NULL,
|
|
62
|
+
expires_at INTEGER NOT NULL,
|
|
63
|
+
PRIMARY KEY (channel, grant_id)
|
|
64
|
+
);
|
|
65
|
+
INSERT INTO durability_semaphore_grants
|
|
66
|
+
(channel, grant_id, client_id, client_channel, grp, permits, claimed,
|
|
67
|
+
released, created_at, expires_at)
|
|
68
|
+
SELECT 'default', grant_id, client_id, 'default', grp, permits, claimed,
|
|
69
|
+
released, created_at, expires_at
|
|
70
|
+
FROM durability_semaphore_grants_before_channels;
|
|
71
|
+
DROP TABLE durability_semaphore_grants_before_channels;
|
|
72
|
+
CREATE INDEX durability_semaphore_grants_expires_idx
|
|
73
|
+
ON durability_semaphore_grants (channel, expires_at) WHERE claimed < permits;
|
|
74
|
+
|
|
75
|
+
ALTER TABLE durability_semaphore_leases RENAME TO durability_semaphore_leases_before_channels;
|
|
76
|
+
CREATE TABLE durability_semaphore_leases (
|
|
77
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
78
|
+
lease_id TEXT NOT NULL,
|
|
79
|
+
client_id TEXT NOT NULL,
|
|
80
|
+
client_channel TEXT NOT NULL DEFAULT 'default',
|
|
81
|
+
grp TEXT NOT NULL,
|
|
82
|
+
permits INTEGER NOT NULL,
|
|
83
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
84
|
+
created_at INTEGER NOT NULL,
|
|
85
|
+
expires_at INTEGER NOT NULL,
|
|
86
|
+
retired INTEGER NOT NULL DEFAULT 0,
|
|
87
|
+
PRIMARY KEY (channel, lease_id)
|
|
88
|
+
);
|
|
89
|
+
INSERT INTO durability_semaphore_leases
|
|
90
|
+
(channel, lease_id, client_id, client_channel, grp, permits, released,
|
|
91
|
+
created_at, expires_at, retired)
|
|
92
|
+
SELECT 'default', lease_id, client_id, 'default', grp, permits, released,
|
|
93
|
+
created_at, expires_at, retired
|
|
94
|
+
FROM durability_semaphore_leases_before_channels;
|
|
95
|
+
DROP TABLE durability_semaphore_leases_before_channels;
|
|
96
|
+
CREATE INDEX durability_semaphore_leases_expires_idx
|
|
97
|
+
ON durability_semaphore_leases (channel, expires_at);
|
|
98
|
+
`,
|
|
99
|
+
down: `
|
|
100
|
+
CREATE TABLE durability_semaphore_channel_guard (
|
|
101
|
+
channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
|
|
102
|
+
);
|
|
103
|
+
INSERT INTO durability_semaphore_channel_guard
|
|
104
|
+
SELECT channel FROM durability_semaphore_grants
|
|
105
|
+
UNION SELECT client_channel FROM durability_semaphore_grants
|
|
106
|
+
UNION SELECT channel FROM durability_semaphore_leases
|
|
107
|
+
UNION SELECT client_channel FROM durability_semaphore_leases;
|
|
108
|
+
DROP TABLE durability_semaphore_channel_guard;
|
|
109
|
+
|
|
110
|
+
ALTER TABLE durability_semaphore_grants RENAME TO durability_semaphore_grants_with_channels;
|
|
111
|
+
CREATE TABLE durability_semaphore_grants (
|
|
112
|
+
grant_id TEXT PRIMARY KEY,
|
|
113
|
+
client_id TEXT NOT NULL,
|
|
114
|
+
grp TEXT NOT NULL,
|
|
115
|
+
permits INTEGER NOT NULL,
|
|
116
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
117
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
118
|
+
created_at INTEGER NOT NULL,
|
|
119
|
+
expires_at INTEGER NOT NULL
|
|
120
|
+
);
|
|
121
|
+
INSERT INTO durability_semaphore_grants
|
|
122
|
+
SELECT grant_id, client_id, grp, permits, claimed, released, created_at,
|
|
123
|
+
expires_at
|
|
124
|
+
FROM durability_semaphore_grants_with_channels;
|
|
125
|
+
DROP TABLE durability_semaphore_grants_with_channels;
|
|
126
|
+
CREATE INDEX durability_semaphore_grants_expires_idx
|
|
127
|
+
ON durability_semaphore_grants (expires_at) WHERE claimed < permits;
|
|
128
|
+
|
|
129
|
+
ALTER TABLE durability_semaphore_leases RENAME TO durability_semaphore_leases_with_channels;
|
|
130
|
+
CREATE TABLE durability_semaphore_leases (
|
|
131
|
+
lease_id TEXT PRIMARY KEY,
|
|
132
|
+
client_id TEXT NOT NULL,
|
|
133
|
+
grp TEXT NOT NULL,
|
|
134
|
+
permits INTEGER NOT NULL,
|
|
135
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
136
|
+
created_at INTEGER NOT NULL,
|
|
137
|
+
expires_at INTEGER NOT NULL,
|
|
138
|
+
retired INTEGER NOT NULL DEFAULT 0
|
|
139
|
+
);
|
|
140
|
+
INSERT INTO durability_semaphore_leases
|
|
141
|
+
SELECT lease_id, client_id, grp, permits, released, created_at,
|
|
142
|
+
expires_at, retired
|
|
143
|
+
FROM durability_semaphore_leases_with_channels;
|
|
144
|
+
DROP TABLE durability_semaphore_leases_with_channels;
|
|
145
|
+
CREATE INDEX durability_semaphore_leases_expires_idx
|
|
146
|
+
ON durability_semaphore_leases (expires_at);
|
|
147
|
+
`,
|
|
148
|
+
},
|
|
149
|
+
];
|
|
150
|
+
/**
|
|
151
|
+
* Ordered SQLite migrations for a semaphore client's queue. Applied
|
|
152
|
+
* automatically on construction.
|
|
153
|
+
*/
|
|
154
|
+
export const durabilitySemaphoreQueueMigrations = [
|
|
155
|
+
{
|
|
156
|
+
name: 'durability_0008_create_semaphore_queue',
|
|
157
|
+
up: `
|
|
158
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_queue_items (
|
|
159
|
+
id TEXT PRIMARY KEY,
|
|
160
|
+
key TEXT NOT NULL,
|
|
161
|
+
grp TEXT NOT NULL,
|
|
162
|
+
payload TEXT NOT NULL,
|
|
163
|
+
status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
|
|
164
|
+
grant_id TEXT,
|
|
165
|
+
created_at INTEGER NOT NULL,
|
|
166
|
+
admitted_at INTEGER,
|
|
167
|
+
notified INTEGER NOT NULL DEFAULT 0,
|
|
168
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
169
|
+
next_attempt_at INTEGER NOT NULL
|
|
170
|
+
);
|
|
171
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_key_idx
|
|
172
|
+
ON durability_semaphore_queue_items (key, status, created_at, id);
|
|
173
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_grant_idx
|
|
174
|
+
ON durability_semaphore_queue_items (grant_id, created_at, id);
|
|
175
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_items_notify_idx
|
|
176
|
+
ON durability_semaphore_queue_items (next_attempt_at)
|
|
177
|
+
WHERE status = 'admitted' AND notified = 0;
|
|
178
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_queue_grants (
|
|
179
|
+
grant_id TEXT PRIMARY KEY,
|
|
180
|
+
key TEXT NOT NULL,
|
|
181
|
+
grp TEXT NOT NULL,
|
|
182
|
+
permits INTEGER NOT NULL,
|
|
183
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
184
|
+
confirmed INTEGER NOT NULL DEFAULT 0,
|
|
185
|
+
admitted INTEGER NOT NULL DEFAULT 0,
|
|
186
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
187
|
+
created_at INTEGER NOT NULL
|
|
188
|
+
);
|
|
189
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_grants_key_idx
|
|
190
|
+
ON durability_semaphore_queue_grants (key, created_at);
|
|
191
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_queue_keys (
|
|
192
|
+
key TEXT PRIMARY KEY,
|
|
193
|
+
next_request_at INTEGER NOT NULL,
|
|
194
|
+
queued INTEGER NOT NULL
|
|
195
|
+
);
|
|
196
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_keys_due_idx
|
|
197
|
+
ON durability_semaphore_queue_keys (next_request_at) WHERE queued > 0;
|
|
198
|
+
`,
|
|
199
|
+
down: `
|
|
200
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_keys_due_idx;
|
|
201
|
+
DROP TABLE IF EXISTS durability_semaphore_queue_keys;
|
|
202
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_grants_key_idx;
|
|
203
|
+
DROP TABLE IF EXISTS durability_semaphore_queue_grants;
|
|
204
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_items_notify_idx;
|
|
205
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_items_grant_idx;
|
|
206
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_items_key_idx;
|
|
207
|
+
DROP TABLE IF EXISTS durability_semaphore_queue_items;
|
|
208
|
+
`,
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: 'durability_0010_add_semaphore_queue_leases',
|
|
212
|
+
up: `
|
|
213
|
+
ALTER TABLE durability_semaphore_queue_items ADD COLUMN lease_id TEXT;
|
|
214
|
+
CREATE TABLE IF NOT EXISTS durability_semaphore_queue_leases (
|
|
215
|
+
lease_id TEXT PRIMARY KEY,
|
|
216
|
+
key TEXT NOT NULL,
|
|
217
|
+
grp TEXT NOT NULL,
|
|
218
|
+
permits INTEGER NOT NULL,
|
|
219
|
+
drawn INTEGER NOT NULL DEFAULT 0,
|
|
220
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
221
|
+
created_at INTEGER NOT NULL,
|
|
222
|
+
expires_at INTEGER NOT NULL
|
|
223
|
+
);
|
|
224
|
+
CREATE INDEX IF NOT EXISTS durability_semaphore_queue_leases_key_idx
|
|
225
|
+
ON durability_semaphore_queue_leases (key, created_at);
|
|
226
|
+
`,
|
|
227
|
+
down: `
|
|
228
|
+
DROP INDEX IF EXISTS durability_semaphore_queue_leases_key_idx;
|
|
229
|
+
DROP TABLE IF EXISTS durability_semaphore_queue_leases;
|
|
230
|
+
ALTER TABLE durability_semaphore_queue_items DROP COLUMN lease_id;
|
|
231
|
+
`,
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: 'durability_0011_renew_semaphore_queue_leases',
|
|
235
|
+
up: `
|
|
236
|
+
ALTER TABLE durability_semaphore_queue_leases ADD COLUMN renew_at INTEGER NOT NULL DEFAULT 0;
|
|
237
|
+
ALTER TABLE durability_semaphore_queue_leases ADD COLUMN renew_ms INTEGER NOT NULL DEFAULT 30000;
|
|
238
|
+
ALTER TABLE durability_semaphore_queue_leases ADD COLUMN confirmed INTEGER NOT NULL DEFAULT 0;
|
|
239
|
+
`,
|
|
240
|
+
down: `
|
|
241
|
+
ALTER TABLE durability_semaphore_queue_leases DROP COLUMN confirmed;
|
|
242
|
+
ALTER TABLE durability_semaphore_queue_leases DROP COLUMN renew_ms;
|
|
243
|
+
ALTER TABLE durability_semaphore_queue_leases DROP COLUMN renew_at;
|
|
244
|
+
`,
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
name: 'durability_0021_semaphore_queue_channels',
|
|
248
|
+
up: `
|
|
249
|
+
ALTER TABLE durability_semaphore_queue_items RENAME TO durability_semaphore_queue_items_before_channels;
|
|
250
|
+
CREATE TABLE durability_semaphore_queue_items (
|
|
251
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
252
|
+
id TEXT NOT NULL,
|
|
253
|
+
key TEXT NOT NULL,
|
|
254
|
+
grp TEXT NOT NULL,
|
|
255
|
+
payload TEXT NOT NULL,
|
|
256
|
+
status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
|
|
257
|
+
grant_id TEXT,
|
|
258
|
+
lease_id TEXT,
|
|
259
|
+
created_at INTEGER NOT NULL,
|
|
260
|
+
admitted_at INTEGER,
|
|
261
|
+
notified INTEGER NOT NULL DEFAULT 0,
|
|
262
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
263
|
+
next_attempt_at INTEGER NOT NULL,
|
|
264
|
+
PRIMARY KEY (channel, id)
|
|
265
|
+
);
|
|
266
|
+
INSERT INTO durability_semaphore_queue_items
|
|
267
|
+
(channel, id, key, grp, payload, status, grant_id, lease_id, created_at,
|
|
268
|
+
admitted_at, notified, attempt, next_attempt_at)
|
|
269
|
+
SELECT 'default', id, key, grp, payload, status, grant_id, lease_id,
|
|
270
|
+
created_at, admitted_at, notified, attempt, next_attempt_at
|
|
271
|
+
FROM durability_semaphore_queue_items_before_channels;
|
|
272
|
+
DROP TABLE durability_semaphore_queue_items_before_channels;
|
|
273
|
+
CREATE INDEX durability_semaphore_queue_items_key_idx
|
|
274
|
+
ON durability_semaphore_queue_items (channel, key, status, created_at, id);
|
|
275
|
+
CREATE INDEX durability_semaphore_queue_items_grant_idx
|
|
276
|
+
ON durability_semaphore_queue_items (channel, grant_id, created_at, id);
|
|
277
|
+
CREATE INDEX durability_semaphore_queue_items_notify_idx
|
|
278
|
+
ON durability_semaphore_queue_items (channel, next_attempt_at)
|
|
279
|
+
WHERE status = 'admitted' AND notified = 0;
|
|
280
|
+
|
|
281
|
+
ALTER TABLE durability_semaphore_queue_grants RENAME TO durability_semaphore_queue_grants_before_channels;
|
|
282
|
+
CREATE TABLE durability_semaphore_queue_grants (
|
|
283
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
284
|
+
grant_id TEXT NOT NULL,
|
|
285
|
+
key TEXT NOT NULL,
|
|
286
|
+
grp TEXT NOT NULL,
|
|
287
|
+
permits INTEGER NOT NULL,
|
|
288
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
289
|
+
confirmed INTEGER NOT NULL DEFAULT 0,
|
|
290
|
+
admitted INTEGER NOT NULL DEFAULT 0,
|
|
291
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
292
|
+
created_at INTEGER NOT NULL,
|
|
293
|
+
PRIMARY KEY (channel, grant_id)
|
|
294
|
+
);
|
|
295
|
+
INSERT INTO durability_semaphore_queue_grants
|
|
296
|
+
(channel, grant_id, key, grp, permits, claimed, confirmed, admitted,
|
|
297
|
+
released, created_at)
|
|
298
|
+
SELECT 'default', grant_id, key, grp, permits, claimed, confirmed,
|
|
299
|
+
admitted, released, created_at
|
|
300
|
+
FROM durability_semaphore_queue_grants_before_channels;
|
|
301
|
+
DROP TABLE durability_semaphore_queue_grants_before_channels;
|
|
302
|
+
CREATE INDEX durability_semaphore_queue_grants_key_idx
|
|
303
|
+
ON durability_semaphore_queue_grants (channel, key, created_at);
|
|
304
|
+
|
|
305
|
+
ALTER TABLE durability_semaphore_queue_keys RENAME TO durability_semaphore_queue_keys_before_channels;
|
|
306
|
+
CREATE TABLE durability_semaphore_queue_keys (
|
|
307
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
308
|
+
key TEXT NOT NULL,
|
|
309
|
+
next_request_at INTEGER NOT NULL,
|
|
310
|
+
queued INTEGER NOT NULL,
|
|
311
|
+
PRIMARY KEY (channel, key)
|
|
312
|
+
);
|
|
313
|
+
INSERT INTO durability_semaphore_queue_keys
|
|
314
|
+
(channel, key, next_request_at, queued)
|
|
315
|
+
SELECT 'default', key, next_request_at, queued
|
|
316
|
+
FROM durability_semaphore_queue_keys_before_channels;
|
|
317
|
+
DROP TABLE durability_semaphore_queue_keys_before_channels;
|
|
318
|
+
CREATE INDEX durability_semaphore_queue_keys_due_idx
|
|
319
|
+
ON durability_semaphore_queue_keys (channel, next_request_at) WHERE queued > 0;
|
|
320
|
+
|
|
321
|
+
ALTER TABLE durability_semaphore_queue_leases RENAME TO durability_semaphore_queue_leases_before_channels;
|
|
322
|
+
CREATE TABLE durability_semaphore_queue_leases (
|
|
323
|
+
channel TEXT NOT NULL DEFAULT 'default',
|
|
324
|
+
lease_id TEXT NOT NULL,
|
|
325
|
+
key TEXT NOT NULL,
|
|
326
|
+
grp TEXT NOT NULL,
|
|
327
|
+
permits INTEGER NOT NULL,
|
|
328
|
+
drawn INTEGER NOT NULL DEFAULT 0,
|
|
329
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
330
|
+
created_at INTEGER NOT NULL,
|
|
331
|
+
expires_at INTEGER NOT NULL,
|
|
332
|
+
renew_at INTEGER NOT NULL DEFAULT 0,
|
|
333
|
+
renew_ms INTEGER NOT NULL DEFAULT 30000,
|
|
334
|
+
confirmed INTEGER NOT NULL DEFAULT 0,
|
|
335
|
+
PRIMARY KEY (channel, lease_id)
|
|
336
|
+
);
|
|
337
|
+
INSERT INTO durability_semaphore_queue_leases
|
|
338
|
+
(channel, lease_id, key, grp, permits, drawn, released, created_at,
|
|
339
|
+
expires_at, renew_at, renew_ms, confirmed)
|
|
340
|
+
SELECT 'default', lease_id, key, grp, permits, drawn, released,
|
|
341
|
+
created_at, expires_at, renew_at, renew_ms, confirmed
|
|
342
|
+
FROM durability_semaphore_queue_leases_before_channels;
|
|
343
|
+
DROP TABLE durability_semaphore_queue_leases_before_channels;
|
|
344
|
+
CREATE INDEX durability_semaphore_queue_leases_key_idx
|
|
345
|
+
ON durability_semaphore_queue_leases (channel, key, created_at);
|
|
346
|
+
`,
|
|
347
|
+
down: `
|
|
348
|
+
CREATE TABLE durability_semaphore_queue_channel_guard (
|
|
349
|
+
channel TEXT CONSTRAINT cannot_downgrade_nondefault_channels CHECK (channel = 'default')
|
|
350
|
+
);
|
|
351
|
+
INSERT INTO durability_semaphore_queue_channel_guard
|
|
352
|
+
SELECT channel FROM durability_semaphore_queue_items
|
|
353
|
+
UNION SELECT channel FROM durability_semaphore_queue_grants
|
|
354
|
+
UNION SELECT channel FROM durability_semaphore_queue_keys
|
|
355
|
+
UNION SELECT channel FROM durability_semaphore_queue_leases;
|
|
356
|
+
DROP TABLE durability_semaphore_queue_channel_guard;
|
|
357
|
+
|
|
358
|
+
ALTER TABLE durability_semaphore_queue_items RENAME TO durability_semaphore_queue_items_with_channels;
|
|
359
|
+
CREATE TABLE durability_semaphore_queue_items (
|
|
360
|
+
id TEXT PRIMARY KEY,
|
|
361
|
+
key TEXT NOT NULL,
|
|
362
|
+
grp TEXT NOT NULL,
|
|
363
|
+
payload TEXT NOT NULL,
|
|
364
|
+
status TEXT NOT NULL CHECK (status IN ('queued', 'claiming', 'admitted')),
|
|
365
|
+
grant_id TEXT,
|
|
366
|
+
created_at INTEGER NOT NULL,
|
|
367
|
+
admitted_at INTEGER,
|
|
368
|
+
notified INTEGER NOT NULL DEFAULT 0,
|
|
369
|
+
attempt INTEGER NOT NULL DEFAULT 0,
|
|
370
|
+
next_attempt_at INTEGER NOT NULL,
|
|
371
|
+
lease_id TEXT
|
|
372
|
+
);
|
|
373
|
+
INSERT INTO durability_semaphore_queue_items
|
|
374
|
+
(id, key, grp, payload, status, grant_id, created_at, admitted_at,
|
|
375
|
+
notified, attempt, next_attempt_at, lease_id)
|
|
376
|
+
SELECT id, key, grp, payload, status, grant_id, created_at, admitted_at,
|
|
377
|
+
notified, attempt, next_attempt_at, lease_id
|
|
378
|
+
FROM durability_semaphore_queue_items_with_channels;
|
|
379
|
+
DROP TABLE durability_semaphore_queue_items_with_channels;
|
|
380
|
+
CREATE INDEX durability_semaphore_queue_items_key_idx
|
|
381
|
+
ON durability_semaphore_queue_items (key, status, created_at, id);
|
|
382
|
+
CREATE INDEX durability_semaphore_queue_items_grant_idx
|
|
383
|
+
ON durability_semaphore_queue_items (grant_id, created_at, id);
|
|
384
|
+
CREATE INDEX durability_semaphore_queue_items_notify_idx
|
|
385
|
+
ON durability_semaphore_queue_items (next_attempt_at)
|
|
386
|
+
WHERE status = 'admitted' AND notified = 0;
|
|
387
|
+
|
|
388
|
+
ALTER TABLE durability_semaphore_queue_grants RENAME TO durability_semaphore_queue_grants_with_channels;
|
|
389
|
+
CREATE TABLE durability_semaphore_queue_grants (
|
|
390
|
+
grant_id TEXT PRIMARY KEY,
|
|
391
|
+
key TEXT NOT NULL,
|
|
392
|
+
grp TEXT NOT NULL,
|
|
393
|
+
permits INTEGER NOT NULL,
|
|
394
|
+
claimed INTEGER NOT NULL DEFAULT 0,
|
|
395
|
+
confirmed INTEGER NOT NULL DEFAULT 0,
|
|
396
|
+
admitted INTEGER NOT NULL DEFAULT 0,
|
|
397
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
398
|
+
created_at INTEGER NOT NULL
|
|
399
|
+
);
|
|
400
|
+
INSERT INTO durability_semaphore_queue_grants
|
|
401
|
+
SELECT grant_id, key, grp, permits, claimed, confirmed, admitted,
|
|
402
|
+
released, created_at
|
|
403
|
+
FROM durability_semaphore_queue_grants_with_channels;
|
|
404
|
+
DROP TABLE durability_semaphore_queue_grants_with_channels;
|
|
405
|
+
CREATE INDEX durability_semaphore_queue_grants_key_idx
|
|
406
|
+
ON durability_semaphore_queue_grants (key, created_at);
|
|
407
|
+
|
|
408
|
+
ALTER TABLE durability_semaphore_queue_keys RENAME TO durability_semaphore_queue_keys_with_channels;
|
|
409
|
+
CREATE TABLE durability_semaphore_queue_keys (
|
|
410
|
+
key TEXT PRIMARY KEY,
|
|
411
|
+
next_request_at INTEGER NOT NULL,
|
|
412
|
+
queued INTEGER NOT NULL
|
|
413
|
+
);
|
|
414
|
+
INSERT INTO durability_semaphore_queue_keys
|
|
415
|
+
SELECT key, next_request_at, queued
|
|
416
|
+
FROM durability_semaphore_queue_keys_with_channels;
|
|
417
|
+
DROP TABLE durability_semaphore_queue_keys_with_channels;
|
|
418
|
+
CREATE INDEX durability_semaphore_queue_keys_due_idx
|
|
419
|
+
ON durability_semaphore_queue_keys (next_request_at) WHERE queued > 0;
|
|
420
|
+
|
|
421
|
+
ALTER TABLE durability_semaphore_queue_leases RENAME TO durability_semaphore_queue_leases_with_channels;
|
|
422
|
+
CREATE TABLE durability_semaphore_queue_leases (
|
|
423
|
+
lease_id TEXT PRIMARY KEY,
|
|
424
|
+
key TEXT NOT NULL,
|
|
425
|
+
grp TEXT NOT NULL,
|
|
426
|
+
permits INTEGER NOT NULL,
|
|
427
|
+
drawn INTEGER NOT NULL DEFAULT 0,
|
|
428
|
+
released INTEGER NOT NULL DEFAULT 0,
|
|
429
|
+
created_at INTEGER NOT NULL,
|
|
430
|
+
expires_at INTEGER NOT NULL,
|
|
431
|
+
renew_at INTEGER NOT NULL DEFAULT 0,
|
|
432
|
+
renew_ms INTEGER NOT NULL DEFAULT 30000,
|
|
433
|
+
confirmed INTEGER NOT NULL DEFAULT 0
|
|
434
|
+
);
|
|
435
|
+
INSERT INTO durability_semaphore_queue_leases
|
|
436
|
+
SELECT lease_id, key, grp, permits, drawn, released, created_at,
|
|
437
|
+
expires_at, renew_at, renew_ms, confirmed
|
|
438
|
+
FROM durability_semaphore_queue_leases_with_channels;
|
|
439
|
+
DROP TABLE durability_semaphore_queue_leases_with_channels;
|
|
440
|
+
CREATE INDEX durability_semaphore_queue_leases_key_idx
|
|
441
|
+
ON durability_semaphore_queue_leases (key, created_at);
|
|
442
|
+
`,
|
|
443
|
+
},
|
|
444
|
+
];
|