@relaybus/postgres 0.1.0 → 0.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 +177 -48
- package/dist/drizzle.cjs +1 -0
- package/dist/drizzle.cjs.map +1 -1
- package/dist/drizzle.d.cts +1 -1
- package/dist/drizzle.d.ts +1 -1
- package/dist/drizzle.js +1 -0
- package/dist/drizzle.js.map +1 -1
- package/dist/executor-CfJgktC6.d.cts +51 -0
- package/dist/executor-CfJgktC6.d.ts +51 -0
- package/dist/index.cjs +2797 -661
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +176 -35
- package/dist/index.d.ts +176 -35
- package/dist/index.js +2799 -664
- package/dist/index.js.map +1 -1
- package/dist/knex.cjs +1 -0
- package/dist/knex.cjs.map +1 -1
- package/dist/knex.d.cts +1 -1
- package/dist/knex.d.ts +1 -1
- package/dist/knex.js +1 -0
- package/dist/knex.js.map +1 -1
- package/dist/kysely.cjs +1 -0
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +1 -1
- package/dist/kysely.d.ts +1 -1
- package/dist/kysely.js +1 -0
- package/dist/kysely.js.map +1 -1
- package/dist/prisma.cjs +1 -0
- package/dist/prisma.cjs.map +1 -1
- package/dist/prisma.d.cts +1 -1
- package/dist/prisma.d.ts +1 -1
- package/dist/prisma.js +1 -0
- package/dist/prisma.js.map +1 -1
- package/dist/typeorm.cjs +1 -0
- package/dist/typeorm.cjs.map +1 -1
- package/dist/typeorm.d.cts +1 -1
- package/dist/typeorm.d.ts +1 -1
- package/dist/typeorm.js +1 -0
- package/dist/typeorm.js.map +1 -1
- package/package.json +24 -11
- package/dist/executor-CSM7ZOhz.d.cts +0 -24
- package/dist/executor-CSM7ZOhz.d.ts +0 -24
package/dist/index.js
CHANGED
|
@@ -1,36 +1,170 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { ConfigurationError, RelayBusError, systemClock } from '@relaybus/core';
|
|
3
|
+
import { assertPositiveInteger, schedule } from '@relaybus/core/internal';
|
|
4
|
+
import { Client, Pool } from 'pg';
|
|
3
5
|
|
|
4
6
|
// src/pg-executor.ts
|
|
7
|
+
|
|
8
|
+
// src/transaction-timeouts.ts
|
|
9
|
+
function budgetSettingsSql({ value, sessionTimeouts, scope }) {
|
|
10
|
+
const local = scope === "transaction";
|
|
11
|
+
const statementTimeout = `SELECT set_config('statement_timeout', ${value}, ${local})`;
|
|
12
|
+
if (!sessionTimeouts) {
|
|
13
|
+
return statementTimeout;
|
|
14
|
+
}
|
|
15
|
+
return `${statementTimeout}, set_config('idle_in_transaction_session_timeout', ${value}, ${local}), CASE WHEN current_setting('server_version_num')::int >= 170000 THEN set_config('transaction_timeout', ${value}, ${local}) END`;
|
|
16
|
+
}
|
|
17
|
+
var SESSION_DEFAULTS_SQL = "SELECT set_config(name, reset_val, false) FROM pg_settings WHERE name IN ('statement_timeout', 'idle_in_transaction_session_timeout', 'transaction_timeout')";
|
|
18
|
+
function budgetValue(timeoutMs) {
|
|
19
|
+
return `${Math.ceil(timeoutMs)}ms`;
|
|
20
|
+
}
|
|
21
|
+
function inBudgetedTransaction(state, {
|
|
22
|
+
work,
|
|
23
|
+
timeoutMs
|
|
24
|
+
}) {
|
|
25
|
+
const { executor } = state;
|
|
26
|
+
if (executor.transactionWithBudget !== void 0) {
|
|
27
|
+
return executor.transactionWithBudget(work, timeoutMs);
|
|
28
|
+
}
|
|
29
|
+
const sql = budgetSettingsSql({
|
|
30
|
+
value: "$1",
|
|
31
|
+
sessionTimeouts: executor.sessionTimeouts === true,
|
|
32
|
+
scope: "transaction"
|
|
33
|
+
});
|
|
34
|
+
return executor.transaction(async (transaction) => {
|
|
35
|
+
await state.query({ sql, parameters: [budgetValue(timeoutMs)], transaction });
|
|
36
|
+
return work(transaction);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/pg-executor.ts
|
|
41
|
+
var FOREIGN_SESSION_ERRORS = /* @__PURE__ */ new Set(["26000", "42P05"]);
|
|
42
|
+
var STATEMENT_NAME_DIGITS = 24;
|
|
5
43
|
var PgExecutor = class {
|
|
6
44
|
#pool;
|
|
7
|
-
|
|
45
|
+
#logger;
|
|
46
|
+
#names = /* @__PURE__ */ new Map();
|
|
47
|
+
#preparing = true;
|
|
48
|
+
#sessionBudget;
|
|
49
|
+
/** The budget each connection keeps at session level (`sessionBudget`); absent for the defaults. */
|
|
50
|
+
#sessionValues = /* @__PURE__ */ new WeakMap();
|
|
51
|
+
constructor(pool, options = {}) {
|
|
8
52
|
this.#pool = pool;
|
|
53
|
+
this.#logger = options.logger ?? console;
|
|
54
|
+
this.#sessionBudget = options.sessionBudget === true;
|
|
55
|
+
}
|
|
56
|
+
/** The connection listens for `error` during the transaction, so a server-ended session fails the transaction instead of the process. */
|
|
57
|
+
sessionTimeouts = true;
|
|
58
|
+
transaction(work) {
|
|
59
|
+
return this.#transaction(
|
|
60
|
+
work,
|
|
61
|
+
(client) => this.#sessionBudget && (!this.#preparing || this.#sessionValues.has(client)) ? { begin: `BEGIN; ${SESSION_DEFAULTS_SQL}`, sessionValue: null } : { begin: "BEGIN" }
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Sends `BEGIN` and the budget settings as one simple query (the value as a literal: the simple
|
|
66
|
+
* query protocol has no parameters), so the budget costs no round trip. With `sessionBudget` a
|
|
67
|
+
* connection that already keeps this budget only gets `BEGIN`.
|
|
68
|
+
*/
|
|
69
|
+
transactionWithBudget(work, timeoutMs) {
|
|
70
|
+
const value = budgetValue(timeoutMs);
|
|
71
|
+
const settings = (scope) => budgetSettingsSql({ value: `'${value}'`, sessionTimeouts: true, scope });
|
|
72
|
+
return this.#transaction(work, (client) => {
|
|
73
|
+
if (!this.#keepsSessionBudget()) {
|
|
74
|
+
return { begin: `BEGIN; ${settings("transaction")}` };
|
|
75
|
+
}
|
|
76
|
+
return this.#sessionValues.get(client) === value ? { begin: "BEGIN" } : { begin: `BEGIN; ${settings("session")}`, sessionValue: value };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
#recordSession(client, sessionValue) {
|
|
80
|
+
if (sessionValue === null) {
|
|
81
|
+
this.#sessionValues.delete(client);
|
|
82
|
+
} else if (sessionValue !== void 0) {
|
|
83
|
+
this.#sessionValues.set(client, sessionValue);
|
|
84
|
+
}
|
|
9
85
|
}
|
|
10
|
-
|
|
86
|
+
/** A pooler that hands out other server sessions (noticed by prepared statements) shares session settings. */
|
|
87
|
+
#keepsSessionBudget() {
|
|
88
|
+
return this.#sessionBudget && this.#preparing;
|
|
89
|
+
}
|
|
90
|
+
async #transaction(work, start) {
|
|
11
91
|
const client = await this.#pool.connect();
|
|
92
|
+
let released = false;
|
|
93
|
+
const release = (destroy) => {
|
|
94
|
+
if (released) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
released = true;
|
|
98
|
+
client.removeListener("error", onSessionEnded);
|
|
99
|
+
if (destroy) {
|
|
100
|
+
client.release(true);
|
|
101
|
+
} else {
|
|
102
|
+
client.release();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const onSessionEnded = (error) => {
|
|
106
|
+
this.#logger.warn(
|
|
107
|
+
`[PgExecutor](transaction) The server ended the session of an open transaction, the transaction fails | error=${error.message}`
|
|
108
|
+
);
|
|
109
|
+
release(true);
|
|
110
|
+
};
|
|
111
|
+
client.on("error", onSessionEnded);
|
|
112
|
+
const { begin, sessionValue } = start(client);
|
|
12
113
|
try {
|
|
13
|
-
await client.query(
|
|
114
|
+
await client.query(begin);
|
|
14
115
|
const result = await work(client);
|
|
15
|
-
await client.query("COMMIT");
|
|
16
|
-
|
|
116
|
+
const { command } = await client.query("COMMIT");
|
|
117
|
+
if (command === "COMMIT") {
|
|
118
|
+
this.#recordSession(client, sessionValue);
|
|
119
|
+
}
|
|
120
|
+
release(false);
|
|
17
121
|
return result;
|
|
18
122
|
} catch (error) {
|
|
19
|
-
await this.#rollbackAndRelease(client);
|
|
123
|
+
await this.#rollbackAndRelease(client, release);
|
|
20
124
|
throw error;
|
|
21
125
|
}
|
|
22
126
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
127
|
+
// eslint-disable-next-line relaybus/max-two-params -- PostgresExecutor port signature
|
|
128
|
+
async query(sql, parameters, transaction, options) {
|
|
129
|
+
const queryable = transaction ?? this.#pool;
|
|
130
|
+
const values = [...parameters];
|
|
131
|
+
if (options?.prepare !== true || !this.#preparing) {
|
|
132
|
+
return (await queryable.query(sql, values)).rows;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
return (await queryable.query({ name: this.#nameOf(sql), text: sql, values })).rows;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (!FOREIGN_SESSION_ERRORS.has(error.code)) {
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
if (this.#preparing) {
|
|
141
|
+
this.#preparing = false;
|
|
142
|
+
this.#logger.warn(
|
|
143
|
+
`[PgExecutor](query) The server does not keep prepared statements per connection, statements are sent unprepared from now on; set preparedStatements: false to skip this check | code=${String(error.code)}`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (transaction !== void 0) {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
return (await queryable.query(sql, values)).rows;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** `relaybus_` and 24 hex digits of the SHA-256 of the text: well under the 63 bytes of an identifier. */
|
|
153
|
+
#nameOf(sql) {
|
|
154
|
+
let name = this.#names.get(sql);
|
|
155
|
+
if (name === void 0) {
|
|
156
|
+
name = `relaybus_${createHash("sha256").update(sql).digest("hex").slice(0, STATEMENT_NAME_DIGITS)}`;
|
|
157
|
+
this.#names.set(sql, name);
|
|
158
|
+
}
|
|
159
|
+
return name;
|
|
26
160
|
}
|
|
27
161
|
/** A client whose rollback fails is in an unknown state, so it is destroyed instead of reused. */
|
|
28
|
-
async #rollbackAndRelease(client) {
|
|
162
|
+
async #rollbackAndRelease(client, release) {
|
|
29
163
|
try {
|
|
30
164
|
await client.query("ROLLBACK");
|
|
31
|
-
|
|
165
|
+
release(false);
|
|
32
166
|
} catch {
|
|
33
|
-
|
|
167
|
+
release(true);
|
|
34
168
|
}
|
|
35
169
|
}
|
|
36
170
|
};
|
|
@@ -48,7 +182,9 @@ function quoteIdentifier(value) {
|
|
|
48
182
|
}
|
|
49
183
|
|
|
50
184
|
// src/names.ts
|
|
185
|
+
var LANE_COUNT = 256;
|
|
51
186
|
var DEFAULT_SCHEMA = "public";
|
|
187
|
+
var MAXIMUM_CHANNEL_LENGTH = 63;
|
|
52
188
|
var DEFAULT_TABLE_PREFIX = "relaybus_";
|
|
53
189
|
var TABLE_PREFIX_PATTERN = /^[a-z0-9_]*$/;
|
|
54
190
|
function resolvePostgresNames(options = {}) {
|
|
@@ -74,35 +210,61 @@ function resolvePostgresNames(options = {}) {
|
|
|
74
210
|
events: table("events"),
|
|
75
211
|
deliveries: table("deliveries"),
|
|
76
212
|
keySequences: table("key_sequences"),
|
|
213
|
+
lanes: table("lanes"),
|
|
214
|
+
lanesTableName: name("lanes"),
|
|
215
|
+
eventCompletions: table("event_completions"),
|
|
216
|
+
completions: table("completed_deliveries"),
|
|
217
|
+
completionHistory: options.completionHistory ?? false,
|
|
218
|
+
notificationChannel: channelName(`${schema}.${tablePrefix}deliveries`),
|
|
77
219
|
indexes: {
|
|
78
220
|
deliveryEventHandler: index("deliveries_event_handler_unique"),
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
deliveryHandlerCreated: index("deliveries_handler_created_idx"),
|
|
221
|
+
deliveryLaneDue: index("deliveries_lane_due_idx"),
|
|
222
|
+
deliveryKeyPending: index("deliveries_key_pending_idx"),
|
|
82
223
|
eventCreated: index("events_created_idx"),
|
|
83
|
-
deliveryKeyUnfinished: index("deliveries_key_unfinished_idx"),
|
|
84
|
-
deliveryKeyProcessing: index("deliveries_key_processing_idx"),
|
|
85
|
-
deliveryStatusCreated: index("deliveries_status_created_idx"),
|
|
86
224
|
deliveryCompleted: index("deliveries_completed_idx"),
|
|
87
|
-
deliveryDead: index("deliveries_dead_idx")
|
|
225
|
+
deliveryDead: index("deliveries_dead_idx"),
|
|
226
|
+
completionCompleted: index("completed_deliveries_completed_idx"),
|
|
227
|
+
eventCompletionsCompleted: index("event_completions_completed_idx"),
|
|
228
|
+
obsolete: [
|
|
229
|
+
index("deliveries_due_idx"),
|
|
230
|
+
index("deliveries_due_unkeyed_idx"),
|
|
231
|
+
index("deliveries_handler_created_idx"),
|
|
232
|
+
index("deliveries_key_unfinished_idx"),
|
|
233
|
+
index("deliveries_key_processing_idx"),
|
|
234
|
+
index("deliveries_status_created_idx")
|
|
235
|
+
]
|
|
88
236
|
}
|
|
89
237
|
};
|
|
90
238
|
}
|
|
239
|
+
var CHANNEL_HASH_DIGITS = 32;
|
|
240
|
+
function channelName(readable) {
|
|
241
|
+
return readable.length <= MAXIMUM_CHANNEL_LENGTH ? readable : `relaybus_${createHash("sha256").update(readable).digest("hex").slice(0, CHANNEL_HASH_DIGITS)}`;
|
|
242
|
+
}
|
|
91
243
|
|
|
92
244
|
// src/sql-fragments.ts
|
|
93
|
-
function dueAtExpression(alias) {
|
|
94
|
-
const prefix = alias === void 0 ? "" : `${alias}.`;
|
|
95
|
-
return `CASE WHEN ${prefix}status = 'pending' THEN ${prefix}available_at ELSE ${prefix}lease_expires_at END`;
|
|
96
|
-
}
|
|
97
245
|
function isoText(expression) {
|
|
98
246
|
return `to_char(${expression} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`;
|
|
99
247
|
}
|
|
248
|
+
function laneExpression(keyExpression, eventIdExpression) {
|
|
249
|
+
return `(hashtextextended(COALESCE(${keyExpression}, ${eventIdExpression}), 0) & ${LANE_COUNT - 1})::smallint`;
|
|
250
|
+
}
|
|
100
251
|
|
|
101
252
|
// src/schema.ts
|
|
253
|
+
var SCHEMA_MARKER_DIGITS = 32;
|
|
102
254
|
function createPostgresSchemaStatements(options = {}) {
|
|
103
255
|
const names = resolvePostgresNames(options);
|
|
104
|
-
const
|
|
105
|
-
|
|
256
|
+
const statements = [...eventAndDeliveryStatements(names), ...laneAndCompletionStatements(names)];
|
|
257
|
+
if (names.completionHistory) {
|
|
258
|
+
statements.push(...completionHistoryStatements(names));
|
|
259
|
+
}
|
|
260
|
+
if (names.schema !== "public") {
|
|
261
|
+
statements.unshift(`CREATE SCHEMA IF NOT EXISTS ${names.quotedSchema}`);
|
|
262
|
+
}
|
|
263
|
+
statements.push(`COMMENT ON TABLE ${names.lanes} IS '${postgresSchemaMarker(statements)}'`);
|
|
264
|
+
return statements;
|
|
265
|
+
}
|
|
266
|
+
function eventAndDeliveryStatements(names) {
|
|
267
|
+
return [
|
|
106
268
|
`CREATE TABLE IF NOT EXISTS ${names.events} (
|
|
107
269
|
id text PRIMARY KEY,
|
|
108
270
|
type text NOT NULL,
|
|
@@ -114,7 +276,8 @@ function createPostgresSchemaStatements(options = {}) {
|
|
|
114
276
|
correlation_id text,
|
|
115
277
|
causation_id text,
|
|
116
278
|
occurred_at timestamptz NOT NULL,
|
|
117
|
-
created_at timestamptz NOT NULL
|
|
279
|
+
created_at timestamptz NOT NULL,
|
|
280
|
+
handlers text[] NOT NULL DEFAULT '{}'
|
|
118
281
|
)`,
|
|
119
282
|
`CREATE INDEX IF NOT EXISTS ${names.indexes.eventCreated} ON ${names.events} (created_at, id)`,
|
|
120
283
|
`CREATE TABLE IF NOT EXISTS ${names.keySequences} (
|
|
@@ -125,12 +288,11 @@ function createPostgresSchemaStatements(options = {}) {
|
|
|
125
288
|
id text COLLATE "C" PRIMARY KEY,
|
|
126
289
|
event_id text NOT NULL REFERENCES ${names.events} (id) ON DELETE CASCADE,
|
|
127
290
|
handler text NOT NULL,
|
|
291
|
+
lane smallint NOT NULL,
|
|
292
|
+
claims integer NOT NULL DEFAULT 0,
|
|
128
293
|
status text NOT NULL CHECK (status IN ('pending', 'processing', 'completed', 'dead')),
|
|
129
294
|
attempts integer NOT NULL DEFAULT 0,
|
|
130
295
|
available_at timestamptz NOT NULL,
|
|
131
|
-
lease_owner text,
|
|
132
|
-
lease_token text,
|
|
133
|
-
lease_expires_at timestamptz,
|
|
134
296
|
last_error jsonb,
|
|
135
297
|
key text,
|
|
136
298
|
sequence bigint,
|
|
@@ -138,25 +300,179 @@ function createPostgresSchemaStatements(options = {}) {
|
|
|
138
300
|
updated_at timestamptz NOT NULL,
|
|
139
301
|
completed_at timestamptz,
|
|
140
302
|
CONSTRAINT ${names.indexes.deliveryEventHandler} UNIQUE (event_id, handler)
|
|
141
|
-
)`,
|
|
142
|
-
|
|
143
|
-
`
|
|
144
|
-
`
|
|
145
|
-
`
|
|
146
|
-
`CREATE INDEX IF NOT EXISTS ${names.indexes.
|
|
147
|
-
`CREATE INDEX IF NOT EXISTS ${names.indexes.
|
|
303
|
+
) WITH (autovacuum_vacuum_scale_factor = 0.02, autovacuum_analyze_scale_factor = 0.05)`,
|
|
304
|
+
upgradeFromVersion010(names),
|
|
305
|
+
`ALTER TABLE ${names.deliveries} ADD COLUMN IF NOT EXISTS claims integer NOT NULL DEFAULT 0`,
|
|
306
|
+
`ALTER TABLE ${names.deliveries} SET (autovacuum_vacuum_scale_factor = 0.02, autovacuum_analyze_scale_factor = 0.05)`,
|
|
307
|
+
...names.indexes.obsolete.map((index) => `DROP INDEX IF EXISTS ${names.quotedSchema}.${index}`),
|
|
308
|
+
`CREATE INDEX IF NOT EXISTS ${names.indexes.deliveryLaneDue} ON ${names.deliveries} (lane, available_at, id) WHERE status = 'pending'`,
|
|
309
|
+
`CREATE INDEX IF NOT EXISTS ${names.indexes.deliveryKeyPending} ON ${names.deliveries} (lane, handler, key, sequence) WHERE key IS NOT NULL AND status = 'pending'`,
|
|
148
310
|
`CREATE INDEX IF NOT EXISTS ${names.indexes.deliveryCompleted} ON ${names.deliveries} (completed_at, id) WHERE status = 'completed'`,
|
|
149
311
|
`CREATE INDEX IF NOT EXISTS ${names.indexes.deliveryDead} ON ${names.deliveries} (updated_at, id) WHERE status = 'dead'`
|
|
150
312
|
];
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
313
|
+
}
|
|
314
|
+
function laneAndCompletionStatements(names) {
|
|
315
|
+
return [
|
|
316
|
+
`CREATE TABLE IF NOT EXISTS ${names.lanes} (
|
|
317
|
+
lane smallint PRIMARY KEY CHECK (lane >= 0 AND lane < ${LANE_COUNT}),
|
|
318
|
+
epoch bigint NOT NULL DEFAULT 0,
|
|
319
|
+
lease_owner text,
|
|
320
|
+
lease_expires_at timestamptz,
|
|
321
|
+
in_flight text[] NOT NULL DEFAULT '{}',
|
|
322
|
+
claim_floor bigint NOT NULL DEFAULT 0
|
|
323
|
+
) WITH (fillfactor = 50)`,
|
|
324
|
+
`ALTER TABLE ${names.lanes} ADD COLUMN IF NOT EXISTS claim_floor bigint NOT NULL DEFAULT 0`,
|
|
325
|
+
`CREATE TABLE IF NOT EXISTS ${names.eventCompletions} (
|
|
326
|
+
event_id text PRIMARY KEY REFERENCES ${names.events} (id) ON DELETE CASCADE,
|
|
327
|
+
completed_at timestamptz NOT NULL
|
|
328
|
+
)`,
|
|
329
|
+
`CREATE INDEX IF NOT EXISTS ${names.indexes.eventCompletionsCompleted} ON ${names.eventCompletions} (completed_at, event_id)`,
|
|
330
|
+
`INSERT INTO ${names.lanes} (lane) SELECT generate_series(0, ${LANE_COUNT - 1})::smallint ON CONFLICT (lane) DO NOTHING`
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
function completionHistoryStatements(names) {
|
|
334
|
+
return [
|
|
335
|
+
`CREATE TABLE IF NOT EXISTS ${names.completions} (
|
|
336
|
+
id text COLLATE "C" PRIMARY KEY,
|
|
337
|
+
event_id text NOT NULL REFERENCES ${names.events} (id) ON DELETE CASCADE,
|
|
338
|
+
handler text NOT NULL,
|
|
339
|
+
attempts integer NOT NULL,
|
|
340
|
+
available_at timestamptz NOT NULL,
|
|
341
|
+
created_at timestamptz NOT NULL,
|
|
342
|
+
completed_at timestamptz NOT NULL
|
|
343
|
+
)`,
|
|
344
|
+
`CREATE INDEX IF NOT EXISTS ${names.indexes.completionCompleted} ON ${names.completions} (completed_at, id)`
|
|
345
|
+
];
|
|
346
|
+
}
|
|
347
|
+
function postgresSchemaMarker(statements) {
|
|
348
|
+
return `relaybus schema ${createHash("sha256").update(statements.join(";")).digest("hex").slice(0, SCHEMA_MARKER_DIGITS)}`;
|
|
349
|
+
}
|
|
350
|
+
function upgradeFromVersion010(names) {
|
|
351
|
+
return `DO $relaybus_upgrade$
|
|
352
|
+
BEGIN
|
|
353
|
+
IF EXISTS (
|
|
354
|
+
SELECT 1 FROM pg_attribute
|
|
355
|
+
WHERE attrelid = '${names.deliveries}'::regclass AND attname = 'lease_token' AND NOT attisdropped
|
|
356
|
+
) THEN
|
|
357
|
+
ALTER TABLE ${names.events} ADD COLUMN IF NOT EXISTS handlers text[] NOT NULL DEFAULT '{}';
|
|
358
|
+
UPDATE ${names.events} AS event SET handlers = known.handlers
|
|
359
|
+
FROM (
|
|
360
|
+
SELECT event_id, array_agg(handler) AS handlers FROM ${names.deliveries} GROUP BY event_id
|
|
361
|
+
) AS known
|
|
362
|
+
WHERE event.id = known.event_id;
|
|
363
|
+
ALTER TABLE ${names.deliveries} ADD COLUMN IF NOT EXISTS lane smallint;
|
|
364
|
+
UPDATE ${names.deliveries} SET lane = ${laneExpression("key", "event_id")} WHERE lane IS NULL;
|
|
365
|
+
ALTER TABLE ${names.deliveries} ALTER COLUMN lane SET NOT NULL;
|
|
366
|
+
UPDATE ${names.deliveries} SET status = 'pending' WHERE status = 'processing';
|
|
367
|
+
ALTER TABLE ${names.deliveries}
|
|
368
|
+
DROP COLUMN lease_owner, DROP COLUMN lease_token, DROP COLUMN lease_expires_at;
|
|
369
|
+
END IF;
|
|
370
|
+
END
|
|
371
|
+
$relaybus_upgrade$`;
|
|
155
372
|
}
|
|
156
373
|
function createPostgresSchemaSql(options = {}) {
|
|
157
374
|
return `${createPostgresSchemaStatements(options).join(";\n\n")};
|
|
158
375
|
`;
|
|
159
376
|
}
|
|
377
|
+
var RECONNECT_DELAY_MS = 1e3;
|
|
378
|
+
var NotificationListener = class {
|
|
379
|
+
#connect;
|
|
380
|
+
#channel;
|
|
381
|
+
#listener;
|
|
382
|
+
#logger;
|
|
383
|
+
/** Ends the current connection, listening or still starting; undefined while there is none. */
|
|
384
|
+
#drop;
|
|
385
|
+
#reconnectTimer;
|
|
386
|
+
#closed = false;
|
|
387
|
+
constructor(connect2, channel, listener, logger) {
|
|
388
|
+
this.#connect = connect2;
|
|
389
|
+
this.#channel = channel;
|
|
390
|
+
this.#listener = listener;
|
|
391
|
+
this.#logger = logger;
|
|
392
|
+
}
|
|
393
|
+
/** Connects and listens. Rejects when that fails; a connection lost later is replaced. */
|
|
394
|
+
async start() {
|
|
395
|
+
const connection = await this.#connect();
|
|
396
|
+
let ended = false;
|
|
397
|
+
let ending = Promise.resolve();
|
|
398
|
+
const end = () => {
|
|
399
|
+
if (!ended) {
|
|
400
|
+
ended = true;
|
|
401
|
+
ending = connection.end().catch(() => void 0);
|
|
402
|
+
}
|
|
403
|
+
return ending;
|
|
404
|
+
};
|
|
405
|
+
if (this.#closed) {
|
|
406
|
+
await end();
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
this.#drop = end;
|
|
410
|
+
let listening = false;
|
|
411
|
+
const onLost = (error) => {
|
|
412
|
+
if (!listening || ended) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
void end();
|
|
416
|
+
this.#drop = void 0;
|
|
417
|
+
this.#logger.warn(
|
|
418
|
+
`[NotificationListener](start) Listening connection lost, opening another | channel=${this.#channel} | error=${error?.message ?? "connection ended"}`
|
|
419
|
+
);
|
|
420
|
+
this.#scheduleReconnect();
|
|
421
|
+
};
|
|
422
|
+
connection.on("error", onLost);
|
|
423
|
+
connection.on("end", () => onLost());
|
|
424
|
+
connection.on("notification", () => this.#listener());
|
|
425
|
+
try {
|
|
426
|
+
await connection.query(`LISTEN "${this.#channel}"`);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (this.#drop === end) {
|
|
429
|
+
this.#drop = void 0;
|
|
430
|
+
}
|
|
431
|
+
await end();
|
|
432
|
+
if (this.#closed) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
throw error;
|
|
436
|
+
}
|
|
437
|
+
if (this.#closed) {
|
|
438
|
+
await end();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
listening = true;
|
|
442
|
+
}
|
|
443
|
+
#scheduleReconnect() {
|
|
444
|
+
this.#reconnectTimer = schedule(systemClock, {
|
|
445
|
+
run: () => {
|
|
446
|
+
this.#reconnectTimer = void 0;
|
|
447
|
+
this.start().then(
|
|
448
|
+
() => {
|
|
449
|
+
if (!this.#closed) {
|
|
450
|
+
this.#listener();
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
(error) => {
|
|
454
|
+
this.#logger.warn(
|
|
455
|
+
`[NotificationListener](scheduleReconnect) Listening again failed, retrying | channel=${this.#channel} | error=${error instanceof Error ? error.message : String(error)}`
|
|
456
|
+
);
|
|
457
|
+
if (!this.#closed) {
|
|
458
|
+
this.#scheduleReconnect();
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
);
|
|
462
|
+
},
|
|
463
|
+
delayMs: RECONNECT_DELAY_MS,
|
|
464
|
+
unref: true
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
/** Stops listening for good and ends the connection. */
|
|
468
|
+
async close() {
|
|
469
|
+
this.#closed = true;
|
|
470
|
+
this.#reconnectTimer?.();
|
|
471
|
+
const drop = this.#drop;
|
|
472
|
+
this.#drop = void 0;
|
|
473
|
+
await drop?.();
|
|
474
|
+
}
|
|
475
|
+
};
|
|
160
476
|
|
|
161
477
|
// src/row-mapping.ts
|
|
162
478
|
function text(row, column) {
|
|
@@ -185,12 +501,26 @@ function mapEventEnvelope(row) {
|
|
|
185
501
|
occurredAt: new Date(text(row, "event_occurred_at"))
|
|
186
502
|
};
|
|
187
503
|
}
|
|
504
|
+
function formatLeaseToken(lane, { epoch, claims }) {
|
|
505
|
+
return `${lane}:${epoch}:${claims}`;
|
|
506
|
+
}
|
|
507
|
+
var LEASE_TOKEN_PATTERN = /^(\d{1,3}):(\d{1,18}):(\d{1,9})$/;
|
|
508
|
+
function parseLeaseToken(token) {
|
|
509
|
+
const match = LEASE_TOKEN_PATTERN.exec(token);
|
|
510
|
+
if (match === null || Number(match[1]) >= LANE_COUNT) {
|
|
511
|
+
return void 0;
|
|
512
|
+
}
|
|
513
|
+
return { lane: Number(match[1]), epoch: match[2], claims: Number(match[3]) };
|
|
514
|
+
}
|
|
188
515
|
function mapClaimedDelivery(row, leaseExpiresAt) {
|
|
189
516
|
return {
|
|
190
517
|
deliveryId: text(row, "delivery_id"),
|
|
191
|
-
leaseToken:
|
|
518
|
+
leaseToken: formatLeaseToken(readCount(row, "lane"), {
|
|
519
|
+
epoch: text(row, "epoch"),
|
|
520
|
+
claims: readCount(row, "claims")
|
|
521
|
+
}),
|
|
192
522
|
handler: text(row, "handler"),
|
|
193
|
-
attempt: readCount(row, "
|
|
523
|
+
attempt: readCount(row, "attempt"),
|
|
194
524
|
leaseExpiresAt,
|
|
195
525
|
event: mapEventEnvelope(row)
|
|
196
526
|
};
|
|
@@ -213,354 +543,1999 @@ function mapDeliveryRecord(row) {
|
|
|
213
543
|
};
|
|
214
544
|
}
|
|
215
545
|
function mapOldestDueAt(rows) {
|
|
216
|
-
return
|
|
546
|
+
return nullableDate(rows[0], "oldest_due_at");
|
|
217
547
|
}
|
|
218
548
|
|
|
219
|
-
// src/
|
|
220
|
-
var
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
${alias}.key AS event_key,
|
|
226
|
-
${alias}.payload::text AS event_payload,
|
|
227
|
-
${alias}.headers::text AS event_headers,
|
|
228
|
-
${alias}.correlation_id AS event_correlation_id,
|
|
229
|
-
${alias}.causation_id AS event_causation_id,
|
|
230
|
-
${isoText(`${alias}.occurred_at`)} AS event_occurred_at`;
|
|
231
|
-
}
|
|
232
|
-
function countChanged(dml) {
|
|
233
|
-
return `WITH changed AS (${dml} RETURNING 1) SELECT count(*)::int AS count FROM changed`;
|
|
234
|
-
}
|
|
235
|
-
var CLAIM_SCAN_WINDOW = 256;
|
|
236
|
-
var UNFINISHED_PREDICATE = (alias) => `${alias}.status IN ('pending', 'processing')`;
|
|
237
|
-
var HANDLER_PREDICATE = `delivery.handler = ANY (ARRAY(SELECT jsonb_array_elements_text($2::jsonb)))`;
|
|
238
|
-
var CLEAR_LEASE = "lease_owner = NULL, lease_token = NULL, lease_expires_at = NULL";
|
|
239
|
-
var PostgresStatements = class {
|
|
240
|
-
#names;
|
|
241
|
-
constructor(names) {
|
|
242
|
-
this.#names = names;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Lock advisory key text for {@link setupLock}. It is derived from the schema
|
|
246
|
-
* name only, so stores with different table prefixes in one schema also
|
|
247
|
-
* serialize their `CREATE SCHEMA IF NOT EXISTS`, which is not race-free.
|
|
248
|
-
*/
|
|
249
|
-
get setupLockKey() {
|
|
250
|
-
return `relaybus:${this.#names.schema}`;
|
|
251
|
-
}
|
|
252
|
-
get setupLock() {
|
|
253
|
-
return "SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))";
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Reserves sequence numbers per key. Parameters: `[{ key, count }]` as JSON.
|
|
257
|
-
* Keys are locked in sorted order, so publishers cannot deadlock within one
|
|
258
|
-
* call (across calls of one transaction they can, see the README); each row lock is held until the publishing transaction ends, which makes
|
|
259
|
-
* sequence order equal commit order for a key.
|
|
260
|
-
*/
|
|
261
|
-
get reserveSequences() {
|
|
262
|
-
return `INSERT INTO ${this.#names.keySequences} AS reserved (key, sequence)
|
|
263
|
-
SELECT requested.key, requested.count
|
|
264
|
-
FROM jsonb_to_recordset($1::jsonb) AS requested (key text, count bigint)
|
|
265
|
-
ORDER BY requested.key
|
|
266
|
-
ON CONFLICT (key) DO UPDATE SET sequence = reserved.sequence + EXCLUDED.sequence
|
|
267
|
-
RETURNING reserved.key AS key, reserved.sequence::text AS sequence`;
|
|
549
|
+
// src/postgres-append.ts
|
|
550
|
+
var NOTIFICATION_INTERVAL_MS = 10;
|
|
551
|
+
var PostgresAppend = class {
|
|
552
|
+
#state;
|
|
553
|
+
constructor(state) {
|
|
554
|
+
this.#state = state;
|
|
268
555
|
}
|
|
269
|
-
/**
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
556
|
+
/** Implements {@link PostgresEventStore.append}. */
|
|
557
|
+
async append(transaction, { entries, now }) {
|
|
558
|
+
const plan = planAppend(entries);
|
|
559
|
+
if (plan.events.length === 0) {
|
|
560
|
+
return { appendedEventIds: [], duplicateEventIds: [], appendedDeliveryCount: 0 };
|
|
561
|
+
}
|
|
562
|
+
const createdAt = now.toISOString();
|
|
563
|
+
const wakeAt = this.#wakeAt(plan.deliveries, { now, transaction });
|
|
564
|
+
const handlersByEvent = handlersByEventOf(plan.deliveries);
|
|
565
|
+
const keyCounts = keyCountsOf(plan.events);
|
|
566
|
+
const fresh = await this.#state.query({
|
|
567
|
+
sql: this.#state.statements.append.insertFreshEvents,
|
|
568
|
+
parameters: [
|
|
569
|
+
JSON.stringify(
|
|
570
|
+
plan.events.map((event, ordinal) => ({
|
|
571
|
+
ordinal,
|
|
572
|
+
...toEventRow(event, { sequence: null, handlers: handlersByEvent.get(event.id) ?? [] })
|
|
573
|
+
}))
|
|
574
|
+
),
|
|
575
|
+
createdAt,
|
|
576
|
+
JSON.stringify(plan.deliveries),
|
|
577
|
+
JSON.stringify([...keyCounts].map(([key, count]) => ({ key, count }))),
|
|
578
|
+
wakeAt
|
|
579
|
+
],
|
|
580
|
+
transaction
|
|
581
|
+
});
|
|
582
|
+
const insertedEventIds = new Set(
|
|
583
|
+
fresh.filter((row) => row["inserted"] === true).map((row) => row["id"])
|
|
584
|
+
);
|
|
585
|
+
let appendedDeliveryCount = readCount(fresh[0], "deliveries");
|
|
586
|
+
const remaining = plan.deliveries.filter(
|
|
587
|
+
(delivery) => !insertedEventIds.has(delivery.event_id)
|
|
588
|
+
);
|
|
589
|
+
if (remaining.length > 0) {
|
|
590
|
+
appendedDeliveryCount += await this.#appendToExistingEvents(transaction, {
|
|
591
|
+
plan,
|
|
592
|
+
fresh,
|
|
593
|
+
deliveries: remaining,
|
|
594
|
+
handlersByEvent,
|
|
595
|
+
insertedEventIds,
|
|
596
|
+
createdAt,
|
|
597
|
+
wakeAt
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
return { ...splitOutcomes(plan, insertedEventIds), appendedDeliveryCount };
|
|
282
601
|
}
|
|
283
602
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
603
|
+
* The time the publishing statements notify the dispatchers for (deliveries due by then wake them),
|
|
604
|
+
* or `null` for no notification. An append in a transaction of the caller's always notifies, at its
|
|
605
|
+
* commit. In a transaction of this store, only the first append within {@link NOTIFICATION_INTERVAL_MS}
|
|
606
|
+
* does; the others leave the wake-up to {@link #wakeAfterCommit}, which runs once their transaction
|
|
607
|
+
* committed, so no committed delivery waits for the poll.
|
|
287
608
|
*/
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
609
|
+
#wakeAt(deliveries, { now, transaction }) {
|
|
610
|
+
if (!this.#state.notifies || !deliveries.some((delivery) => Date.parse(delivery.available_at) <= now.getTime())) {
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
const own = this.#state.ownTransactions.get(transaction);
|
|
614
|
+
if (own !== void 0 && Date.now() - this.#state.lastNotificationAt < NOTIFICATION_INTERVAL_MS) {
|
|
615
|
+
own.wake = true;
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
this.#state.lastNotificationAt = Date.now();
|
|
619
|
+
return now.toISOString();
|
|
298
620
|
}
|
|
299
621
|
/**
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
* `
|
|
304
|
-
*
|
|
305
|
-
* Always returns one row per leased delivery, or a single row whose
|
|
306
|
-
* `delivery_id` is null when nothing was leased. `window_saturated` says
|
|
307
|
-
* whether the window was full: when it is and fewer than `limit` deliveries
|
|
308
|
-
* were leased, deliveries beyond the window may still be claimable and the
|
|
309
|
-
* caller runs {@link claimBehindBlockedKeys}. When the window is not
|
|
310
|
-
* saturated every due delivery was examined, so the result is complete.
|
|
311
|
-
*
|
|
312
|
-
* Candidates are chosen with `FOR UPDATE SKIP LOCKED`, so two claimers never
|
|
313
|
-
* lock the same row. A keyed delivery is a candidate only when it is the head
|
|
314
|
-
* of its (handler, key), that is (a) no earlier delivery of that handler and
|
|
315
|
-
* key is pending or processing, which is the ordering guarantee, and (b) no
|
|
316
|
-
* other delivery of that handler and key holds a live lease. Rule b covers a
|
|
317
|
-
* delivery that re-enters the key behind a running one (requeue, or a handler
|
|
318
|
-
* added to an old event); an expired lease does not count, because its worker
|
|
319
|
-
* is presumed dead and the key must not stall behind it.
|
|
320
|
-
*
|
|
321
|
-
* Why SKIP LOCKED alone is enough for ordering: only the head of a key can
|
|
322
|
-
* ever be a candidate, and the head is a single row. Two claimers racing
|
|
323
|
-
* for it either skip it (the other holds the row lock) or, if the other
|
|
324
|
-
* already committed, re-check the row's new version (now processing and not
|
|
325
|
-
* due), which excludes it. Followers stay blocked because their predecessor
|
|
326
|
-
* remains pending or processing in every snapshot until it completes or is
|
|
327
|
-
* buried, and that commit happens-before the next claim that sees it.
|
|
622
|
+
* Wakes the dispatchers for a committed transaction whose appends sent no notification: at once when
|
|
623
|
+
* none was sent within {@link NOTIFICATION_INTERVAL_MS}, otherwise once when the interval ends. A
|
|
624
|
+
* wake-up already scheduled covers this transaction too, since it is sent after this commit. Nothing is
|
|
625
|
+
* sent once the store is closed or the `pg` pool behind it is ending: no dispatcher can be woken through
|
|
626
|
+
* it then, and the deliveries are found by polling.
|
|
328
627
|
*/
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
EXISTS (
|
|
349
|
-
SELECT 1 FROM ${deliveries} AS earlier
|
|
350
|
-
WHERE earlier.key IS NOT NULL AND earlier.handler = delivery.handler AND earlier.key = delivery.key
|
|
351
|
-
AND ${UNFINISHED_PREDICATE("earlier")}
|
|
352
|
-
AND earlier.sequence < delivery.sequence
|
|
353
|
-
)
|
|
354
|
-
OR ${this.#liveLeaseElsewhere()}
|
|
355
|
-
)
|
|
356
|
-
)
|
|
357
|
-
ORDER BY due_at, delivery.id
|
|
358
|
-
LIMIT $3::int
|
|
359
|
-
FOR UPDATE OF delivery SKIP LOCKED
|
|
360
|
-
), ${this.#leaseCandidates()}
|
|
361
|
-
SELECT (SELECT count(*) FROM due_window) >= ${CLAIM_SCAN_WINDOW} AS window_saturated, leased.*
|
|
362
|
-
FROM (SELECT 1) AS anchor
|
|
363
|
-
LEFT JOIN (${this.#leasedRows()}) AS leased ON true
|
|
364
|
-
ORDER BY leased.due_at, leased.delivery_id`;
|
|
628
|
+
wakeAfterCommit() {
|
|
629
|
+
if (this.#state.trailingNotification !== void 0 || this.#state.closed) {
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
this.#state.trailingNotification = schedule(systemClock, {
|
|
633
|
+
run: () => {
|
|
634
|
+
this.#state.trailingNotification = void 0;
|
|
635
|
+
if (this.#state.pool?.ending === true) {
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
this.#state.lastNotificationAt = Date.now();
|
|
639
|
+
this.#state.query({ sql: this.#state.statements.append.notify, parameters: [] }).catch((error) => {
|
|
640
|
+
this.#state.logger.error(
|
|
641
|
+
`[PostgresAppend](wakeAfterCommit) Waking the dispatchers failed, they find the deliveries when they poll | error=${error instanceof Error ? error.message : String(error)}`
|
|
642
|
+
);
|
|
643
|
+
});
|
|
644
|
+
},
|
|
645
|
+
delayMs: Math.max(0, this.#state.lastNotificationAt + NOTIFICATION_INTERVAL_MS - Date.now())
|
|
646
|
+
});
|
|
365
647
|
}
|
|
366
648
|
/**
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
649
|
+
* Adds the deliveries of events the publishing statement found already stored. An event that already
|
|
650
|
+
* exists may be getting the same new handler from another transaction at this very moment. Whoever
|
|
651
|
+
* holds the event decides, so the freshness check of the insert sees what the other transaction
|
|
652
|
+
* committed, even if its delivery has completed since. An existing event that a purge removed
|
|
653
|
+
* meanwhile is inserted again (with the sequence reserved for it) instead of being skipped; its id is
|
|
654
|
+
* added to `insertedEventIds`. Returns how many deliveries were inserted.
|
|
373
655
|
*/
|
|
374
|
-
|
|
375
|
-
const deliveries =
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
)
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
)
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
LEFT JOIN (${this.#leasedRows()}) AS leased ON true
|
|
415
|
-
ORDER BY leased.due_at, leased.delivery_id`;
|
|
656
|
+
async #appendToExistingEvents(transaction, input) {
|
|
657
|
+
const { plan, fresh, deliveries, handlersByEvent, insertedEventIds, createdAt, wakeAt } = input;
|
|
658
|
+
const sequences = new Map(
|
|
659
|
+
fresh.map((row) => [row["id"], row["sequence"]])
|
|
660
|
+
);
|
|
661
|
+
const reinserted = [];
|
|
662
|
+
let existing = plan.events.filter((event) => !insertedEventIds.has(event.id)).map(
|
|
663
|
+
(event) => toEventRow(event, {
|
|
664
|
+
sequence: sequences.get(event.id),
|
|
665
|
+
handlers: handlersByEvent.get(event.id) ?? []
|
|
666
|
+
})
|
|
667
|
+
);
|
|
668
|
+
while (existing.length > 0) {
|
|
669
|
+
const locked = new Set(
|
|
670
|
+
(await this.#state.query({
|
|
671
|
+
sql: this.#state.statements.append.lockExistingEvents,
|
|
672
|
+
parameters: [JSON.stringify(existing.map((row) => row.id).sort())],
|
|
673
|
+
transaction
|
|
674
|
+
})).map((row) => row["id"])
|
|
675
|
+
);
|
|
676
|
+
const gone = existing.filter((row) => !locked.has(row.id));
|
|
677
|
+
if (gone.length > 0) {
|
|
678
|
+
const inserted2 = await this.#state.query({
|
|
679
|
+
sql: this.#state.statements.append.insertEvents,
|
|
680
|
+
parameters: [JSON.stringify(gone), createdAt],
|
|
681
|
+
transaction
|
|
682
|
+
});
|
|
683
|
+
for (const row of inserted2) {
|
|
684
|
+
insertedEventIds.add(row["id"]);
|
|
685
|
+
reinserted.push(row["id"]);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
existing = gone.filter((row) => !insertedEventIds.has(row.id));
|
|
689
|
+
}
|
|
690
|
+
const inserted = await this.#state.query({
|
|
691
|
+
sql: this.#state.statements.append.insertDeliveries,
|
|
692
|
+
parameters: [JSON.stringify(deliveries), createdAt, JSON.stringify(reinserted), wakeAt],
|
|
693
|
+
transaction
|
|
694
|
+
});
|
|
695
|
+
return inserted.length;
|
|
416
696
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
)
|
|
697
|
+
};
|
|
698
|
+
function planAppend(entries) {
|
|
699
|
+
const events = [];
|
|
700
|
+
const deliveries = [];
|
|
701
|
+
const outcomes = [];
|
|
702
|
+
const seenEventIds = /* @__PURE__ */ new Set();
|
|
703
|
+
const seenDeliveries = /* @__PURE__ */ new Set();
|
|
704
|
+
for (const { event, deliveries: newDeliveries } of entries) {
|
|
705
|
+
const repeated = seenEventIds.has(event.id);
|
|
706
|
+
seenEventIds.add(event.id);
|
|
707
|
+
outcomes.push({ eventId: event.id, repeated });
|
|
708
|
+
if (!repeated) {
|
|
709
|
+
events.push(event);
|
|
710
|
+
}
|
|
711
|
+
for (const delivery of newDeliveries) {
|
|
712
|
+
const identity = `${event.id}\0${delivery.handler}`;
|
|
713
|
+
if (!seenDeliveries.has(identity)) {
|
|
714
|
+
seenDeliveries.add(identity);
|
|
715
|
+
deliveries.push({
|
|
716
|
+
id: delivery.id,
|
|
717
|
+
event_id: event.id,
|
|
718
|
+
handler: delivery.handler,
|
|
719
|
+
available_at: delivery.availableAt.toISOString()
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
}
|
|
441
723
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
724
|
+
return { events, deliveries, outcomes };
|
|
725
|
+
}
|
|
726
|
+
function toEventRow(event, {
|
|
727
|
+
sequence,
|
|
728
|
+
handlers
|
|
729
|
+
}) {
|
|
730
|
+
return {
|
|
731
|
+
id: event.id,
|
|
732
|
+
type: event.type,
|
|
733
|
+
source: event.source,
|
|
734
|
+
key: event.key,
|
|
735
|
+
sequence,
|
|
736
|
+
payload: event.payload,
|
|
737
|
+
headers: event.headers,
|
|
738
|
+
correlation_id: event.correlationId,
|
|
739
|
+
causation_id: event.causationId,
|
|
740
|
+
occurred_at: event.occurredAt.toISOString(),
|
|
741
|
+
handlers
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
function handlersByEventOf(deliveries) {
|
|
745
|
+
const handlersByEvent = /* @__PURE__ */ new Map();
|
|
746
|
+
for (const delivery of deliveries) {
|
|
747
|
+
handlersByEvent.set(delivery.event_id, [
|
|
748
|
+
...handlersByEvent.get(delivery.event_id) ?? [],
|
|
749
|
+
delivery.handler
|
|
750
|
+
]);
|
|
448
751
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
752
|
+
return handlersByEvent;
|
|
753
|
+
}
|
|
754
|
+
function keyCountsOf(events) {
|
|
755
|
+
const keyCounts = /* @__PURE__ */ new Map();
|
|
756
|
+
for (const event of events) {
|
|
757
|
+
if (event.key !== null) {
|
|
758
|
+
keyCounts.set(event.key, (keyCounts.get(event.key) ?? 0) + 1);
|
|
759
|
+
}
|
|
454
760
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
761
|
+
return keyCounts;
|
|
762
|
+
}
|
|
763
|
+
function splitOutcomes(plan, insertedEventIds) {
|
|
764
|
+
const appendedEventIds = [];
|
|
765
|
+
const duplicateEventIds = [];
|
|
766
|
+
for (const { eventId, repeated } of plan.outcomes) {
|
|
767
|
+
(!repeated && insertedEventIds.has(eventId) ? appendedEventIds : duplicateEventIds).push(
|
|
768
|
+
eventId
|
|
769
|
+
);
|
|
460
770
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
771
|
+
return { appendedEventIds, duplicateEventIds };
|
|
772
|
+
}
|
|
773
|
+
var ReconcileFailedError = class extends RelayBusError {
|
|
774
|
+
name = "ReconcileFailedError";
|
|
775
|
+
code = "RELAYBUS_POSTGRES_RECONCILE_FAILED";
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// src/postgres-store-state.ts
|
|
779
|
+
function handedOutKey(deliveryId, leaseToken) {
|
|
780
|
+
return `${deliveryId}\0${leaseToken}`;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// src/postgres-claim.ts
|
|
784
|
+
var INITIAL_LANES_PER_ROUND = 4;
|
|
785
|
+
var MAXIMUM_STALE_CLAIM_ATTEMPTS = 3;
|
|
786
|
+
var PostgresClaims = class {
|
|
787
|
+
#state;
|
|
788
|
+
constructor(state) {
|
|
789
|
+
this.#state = state;
|
|
466
790
|
}
|
|
467
|
-
/**
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
791
|
+
/** Implements {@link PostgresEventStore.claim}. */
|
|
792
|
+
claim(request) {
|
|
793
|
+
if (request.handlers.length === 0 || !(request.limit > 0)) {
|
|
794
|
+
return Promise.resolve([]);
|
|
795
|
+
}
|
|
796
|
+
return this.#serialized(request.leaseOwner, () => this.#claim(request));
|
|
472
797
|
}
|
|
473
798
|
/**
|
|
474
|
-
*
|
|
475
|
-
*
|
|
799
|
+
* Runs `work` after the earlier claims of the same owner on this store settled. The
|
|
800
|
+
* bookkeeping of what was handed out and the reconciliation of a failed claim need
|
|
801
|
+
* one claim at a time per owner (the dispatcher claims one batch at a time anyway;
|
|
802
|
+
* `runOnce` next to a running dispatcher is what this guards against).
|
|
476
803
|
*/
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
804
|
+
#serialized(owner, work) {
|
|
805
|
+
const previous = this.#state.ownerQueues.get(owner) ?? Promise.resolve();
|
|
806
|
+
const run = previous.then(work, work);
|
|
807
|
+
const tail = run.then(
|
|
808
|
+
() => void 0,
|
|
809
|
+
() => void 0
|
|
810
|
+
);
|
|
811
|
+
this.#state.ownerQueues.set(owner, tail);
|
|
812
|
+
void tail.then(() => {
|
|
813
|
+
if (this.#state.ownerQueues.get(owner) === tail) {
|
|
814
|
+
this.#state.ownerQueues.delete(owner);
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
return run;
|
|
487
818
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
const
|
|
493
|
-
|
|
494
|
-
|
|
819
|
+
async #claim(request) {
|
|
820
|
+
await this.#assertReconciled(request.leaseOwner);
|
|
821
|
+
const heldFirst = this.#state.drainingOwners.has(request.leaseOwner);
|
|
822
|
+
let rows = await this.#claimFirst(request, heldFirst);
|
|
823
|
+
const heldLanes = new Set(JSON.parse(rows[0]["examined_lanes"]));
|
|
824
|
+
const claimed = this.#takeClaimed(rows, request.leaseOwner);
|
|
825
|
+
if (heldFirst && claimed.length < request.limit && rows[0]["stale"] !== true) {
|
|
826
|
+
const freeRows = await this.#claimFreeLanesAfterHeld(request, claimed);
|
|
827
|
+
if (freeRows === void 0) {
|
|
828
|
+
return this.#handOutInDueOrder(claimed, { request, heldLanes });
|
|
829
|
+
}
|
|
830
|
+
rows = freeRows;
|
|
831
|
+
}
|
|
832
|
+
if (claimed.length < request.limit && leftWorkBehind(rows[0])) {
|
|
833
|
+
await this.#claimMoreLanes(request, claimed);
|
|
834
|
+
}
|
|
835
|
+
return this.#handOutInDueOrder(claimed, { request, heldLanes });
|
|
836
|
+
}
|
|
837
|
+
/** Reconciles first when an earlier reconciliation of `owner` failed; refuses to claim while it still fails. */
|
|
838
|
+
async #assertReconciled(owner) {
|
|
839
|
+
if (this.#state.unreconciled.has(owner) && !await this.#reconcile(owner)) {
|
|
840
|
+
throw new ReconcileFailedError(
|
|
841
|
+
`[PostgresEventStore](claim) Giving back unreturned deliveries still fails, so no claim is made | owner=${owner}`
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
/** The first claim statement: from the held lanes only when `heldFirst`, else from held and free lanes. */
|
|
846
|
+
async #claimFirst(request, heldFirst) {
|
|
847
|
+
try {
|
|
848
|
+
return await this.#claimInOneStatement(
|
|
849
|
+
request,
|
|
850
|
+
heldFirst ? this.#state.statements.lanes.claimHeldLanes : this.#state.statements.lanes.claimLanes
|
|
851
|
+
);
|
|
852
|
+
} catch (error) {
|
|
853
|
+
await this.#reconcile(request.leaseOwner);
|
|
854
|
+
throw error;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* After a held-lanes claim came up short: claims the rest from held and free lanes and adds it to
|
|
859
|
+
* `claimed`. Returns the statement's rows, or undefined when it failed after something was claimed
|
|
860
|
+
* already, which is then handed out as it is.
|
|
861
|
+
*/
|
|
862
|
+
async #claimFreeLanesAfterHeld(request, claimed) {
|
|
863
|
+
let rows;
|
|
864
|
+
try {
|
|
865
|
+
rows = await this.#claimInOneStatement(
|
|
866
|
+
{ ...request, limit: request.limit - claimed.length },
|
|
867
|
+
this.#state.statements.lanes.claimLanes
|
|
868
|
+
);
|
|
869
|
+
} catch (error) {
|
|
870
|
+
await this.#reconcile(request.leaseOwner);
|
|
871
|
+
if (claimed.length === 0) {
|
|
872
|
+
this.#state.drainingOwners.delete(request.leaseOwner);
|
|
873
|
+
throw error;
|
|
874
|
+
}
|
|
875
|
+
this.#state.logger.error(
|
|
876
|
+
`[PostgresClaims](claimFreeLanesAfterHeld) Claiming from free lanes failed, returning the deliveries already claimed | claimed=${claimed.length} | error=${error instanceof Error ? error.message : String(error)}`
|
|
877
|
+
);
|
|
878
|
+
return void 0;
|
|
879
|
+
}
|
|
880
|
+
claimed.push(...this.#takeClaimed(rows, request.leaseOwner));
|
|
881
|
+
return rows;
|
|
882
|
+
}
|
|
883
|
+
/** Takes more lanes round by round and adds what they give to `claimed`; keeps what was claimed when that fails. */
|
|
884
|
+
async #claimMoreLanes(request, claimed) {
|
|
885
|
+
try {
|
|
886
|
+
const more = await this.#claimMore(request, claimed.length);
|
|
887
|
+
this.#handOut(more, request.leaseOwner);
|
|
888
|
+
claimed.push(...more);
|
|
889
|
+
} catch (error) {
|
|
890
|
+
await this.#reconcile(request.leaseOwner);
|
|
891
|
+
if (claimed.length === 0) {
|
|
892
|
+
throw error;
|
|
893
|
+
}
|
|
894
|
+
this.#state.logger.error(
|
|
895
|
+
`[PostgresClaims](claimMoreLanes) Taking more lanes failed, returning the deliveries already claimed | claimed=${claimed.length} | error=${error instanceof Error ? error.message : String(error)}`
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
/** The deliveries a claim statement picked, registered as handed out to `leaseOwner`. */
|
|
900
|
+
#takeClaimed(rows, leaseOwner) {
|
|
901
|
+
const claimed = rows.filter((row) => row["delivery_id"] !== null);
|
|
902
|
+
this.#forgetFinished(rows[0], { leaseOwner, claimed });
|
|
903
|
+
this.#handOut(claimed, leaseOwner);
|
|
904
|
+
return claimed;
|
|
905
|
+
}
|
|
906
|
+
#handOutInDueOrder(claimed, {
|
|
907
|
+
request,
|
|
908
|
+
heldLanes
|
|
909
|
+
}) {
|
|
910
|
+
if (claimed.length >= request.limit && claimed.every((row) => heldLanes.has(readCount(row, "lane")))) {
|
|
911
|
+
this.#state.drainingOwners.add(request.leaseOwner);
|
|
912
|
+
} else {
|
|
913
|
+
this.#state.drainingOwners.delete(request.leaseOwner);
|
|
914
|
+
}
|
|
915
|
+
claimed.sort(compareDue);
|
|
916
|
+
return claimed.map((row) => mapClaimedDelivery(row, request.leaseExpiresAt));
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Runs `statement` (`LaneStatements.claimLanes` or its held-lane form), again when it reports that it waited for a lane lock and
|
|
920
|
+
* read deliveries with an older snapshot than the lane it got. A held lane is only locked by an
|
|
921
|
+
* operation of its own owner (another claim, a reschedule), so a few attempts are enough.
|
|
922
|
+
*/
|
|
923
|
+
async #claimInOneStatement(request, statement) {
|
|
924
|
+
const parameters = [
|
|
925
|
+
request.now.toISOString(),
|
|
926
|
+
JSON.stringify(request.handlers),
|
|
927
|
+
request.limit,
|
|
928
|
+
request.leaseOwner,
|
|
929
|
+
request.leaseExpiresAt.toISOString(),
|
|
930
|
+
this.#generation(request.leaseOwner).toString()
|
|
931
|
+
];
|
|
932
|
+
let rows = await this.#state.query({ sql: statement, parameters });
|
|
933
|
+
for (let attempt = 1; attempt < MAXIMUM_STALE_CLAIM_ATTEMPTS && rows[0]["stale"] === true; attempt += 1) {
|
|
934
|
+
rows = await this.#state.query({ sql: statement, parameters });
|
|
935
|
+
}
|
|
936
|
+
return rows;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Second step of {@link PostgresEventStore.claim}, in one transaction: takes free lanes a few at a
|
|
940
|
+
* time, lanes to take over included (their lost attempts are charged first), and records the batch.
|
|
941
|
+
* `alreadyClaimed` deliveries of the first step are in flight, so they are not picked again.
|
|
942
|
+
*/
|
|
943
|
+
async #claimMore(request, alreadyClaimed) {
|
|
944
|
+
const now = request.now.toISOString();
|
|
945
|
+
const leaseExpiresAt = request.leaseExpiresAt.toISOString();
|
|
946
|
+
const handlers = JSON.stringify(request.handlers);
|
|
947
|
+
const generation = this.#generation(request.leaseOwner).toString();
|
|
948
|
+
return this.#state.executor.transaction(async (transaction) => {
|
|
949
|
+
const claimed = [];
|
|
950
|
+
const remaining = () => request.limit - alreadyClaimed - claimed.length;
|
|
951
|
+
const takenLanes = /* @__PURE__ */ new Set();
|
|
952
|
+
for (let laneCount = INITIAL_LANES_PER_ROUND; remaining() > 0; ) {
|
|
953
|
+
const taken = await this.#state.query({
|
|
954
|
+
sql: this.#state.statements.lanes.takeFreeLanes,
|
|
955
|
+
parameters: [now, handlers, laneCount, request.leaseOwner, leaseExpiresAt, generation],
|
|
956
|
+
transaction
|
|
957
|
+
});
|
|
958
|
+
if (taken.length === 0) {
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
const lanes = taken.map((row) => readCount(row, "lane"));
|
|
962
|
+
for (const lane of lanes) {
|
|
963
|
+
takenLanes.add(lane);
|
|
964
|
+
}
|
|
965
|
+
claimed.push(
|
|
966
|
+
...await this.#pickDeliveries(
|
|
967
|
+
[now, handlers, remaining(), JSON.stringify(lanes)],
|
|
968
|
+
transaction
|
|
969
|
+
)
|
|
970
|
+
);
|
|
971
|
+
laneCount *= 2;
|
|
972
|
+
}
|
|
973
|
+
await this.#recordClaim(claimed, { lanes: takenLanes, request, transaction });
|
|
974
|
+
return claimed;
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* The generation of the owner's claim statements. It only grows, also across restarts (it starts at the
|
|
979
|
+
* clock), and a reconciliation moves it up, which fences off every earlier statement still in flight.
|
|
980
|
+
*/
|
|
981
|
+
#generation(owner, advance = false) {
|
|
982
|
+
const current = this.#state.generations.get(owner) ?? BigInt(Date.now());
|
|
983
|
+
const next = advance ? current + GENERATION_STEP > BigInt(Date.now()) ? current + GENERATION_STEP : BigInt(Date.now()) : current;
|
|
984
|
+
this.#state.generations.set(owner, next);
|
|
985
|
+
return next;
|
|
986
|
+
}
|
|
987
|
+
/** Remembers the claims handed to the caller of `claim` until they are settled. */
|
|
988
|
+
#handOut(rows, leaseOwner) {
|
|
989
|
+
for (const row of rows) {
|
|
990
|
+
const claim = mapClaimedDelivery(row, /* @__PURE__ */ new Date(0));
|
|
991
|
+
this.#state.handedOut.set(handedOutKey(claim.deliveryId, claim.leaseToken), {
|
|
992
|
+
owner: leaseOwner,
|
|
993
|
+
deliveryId: claim.deliveryId,
|
|
994
|
+
...parseLeaseToken(claim.leaseToken)
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Drops the registrations of the owner's held lanes whose delivery no longer runs there
|
|
1000
|
+
* (completed inside the caller's transaction, deleted, or listed no more). Registrations of
|
|
1001
|
+
* lanes this claim did not see stay: their tokens may still be valid.
|
|
1002
|
+
*/
|
|
1003
|
+
#forgetFinished(row, {
|
|
1004
|
+
leaseOwner,
|
|
1005
|
+
claimed
|
|
1006
|
+
}) {
|
|
1007
|
+
const heldLanes = new Set(JSON.parse(row["examined_lanes"]));
|
|
1008
|
+
const running = /* @__PURE__ */ new Set([
|
|
1009
|
+
...JSON.parse(row["running_ids"]),
|
|
1010
|
+
...claimed.map((picked) => picked["delivery_id"])
|
|
1011
|
+
]);
|
|
1012
|
+
for (const [key, entry] of this.#state.handedOut) {
|
|
1013
|
+
if (entry.owner === leaseOwner && heldLanes.has(entry.lane) && !running.has(entry.deliveryId)) {
|
|
1014
|
+
this.#state.handedOut.delete(key);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* After a claim failed, possibly after the database recorded its batch: gives back the recorded
|
|
1020
|
+
* deliveries that were never handed out, keeping exactly the claims handed out and not settled.
|
|
1021
|
+
* A failure is remembered and retried before the owner's next claim: other deliveries keep
|
|
1022
|
+
* renewing the lane, so an unreturned one would otherwise never be recovered.
|
|
1023
|
+
*/
|
|
1024
|
+
async #reconcile(leaseOwner) {
|
|
1025
|
+
const generation = this.#generation(leaseOwner, true);
|
|
1026
|
+
const handedOut = [...this.#state.handedOut.values()].filter((entry) => entry.owner === leaseOwner).map(({ deliveryId, lane, epoch, claims }) => ({ id: deliveryId, lane, epoch, claims }));
|
|
1027
|
+
try {
|
|
1028
|
+
await this.#state.query({
|
|
1029
|
+
sql: this.#state.statements.claim.reconcileClaims,
|
|
1030
|
+
parameters: [leaseOwner, JSON.stringify(handedOut), generation.toString()]
|
|
1031
|
+
});
|
|
1032
|
+
this.#state.unreconciled.delete(leaseOwner);
|
|
1033
|
+
return true;
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
this.#state.unreconciled.add(leaseOwner);
|
|
1036
|
+
this.#state.logger.error(
|
|
1037
|
+
`[PostgresClaims](reconcile) Giving back unreturned deliveries failed, it is retried before the next claim | owner=${leaseOwner} | error=${error instanceof Error ? error.message : String(error)}`
|
|
1038
|
+
);
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
/** Fast claim, then the slow one when the fast window was full of blocked followers. */
|
|
1043
|
+
async #pickDeliveries(parameters, transaction) {
|
|
1044
|
+
const limit = parameters[2];
|
|
1045
|
+
const fastRows = await this.#state.query({
|
|
1046
|
+
sql: this.#state.statements.claim.claim,
|
|
1047
|
+
parameters,
|
|
1048
|
+
transaction
|
|
1049
|
+
});
|
|
1050
|
+
const picked = fastRows.filter((row) => row["delivery_id"] !== null);
|
|
1051
|
+
if (picked.length < limit && fastRows[0]["window_saturated"] === true) {
|
|
1052
|
+
const slowRows = await this.#state.query({
|
|
1053
|
+
sql: this.#state.statements.claim.claimBehindBlockedKeys,
|
|
1054
|
+
parameters,
|
|
1055
|
+
transaction
|
|
1056
|
+
});
|
|
1057
|
+
const pickedIds = new Set(picked.map((row) => row["delivery_id"]));
|
|
1058
|
+
picked.push(
|
|
1059
|
+
...slowRows.filter(
|
|
1060
|
+
(row) => row["delivery_id"] !== null && !pickedIds.has(row["delivery_id"])
|
|
1061
|
+
)
|
|
1062
|
+
);
|
|
1063
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
1064
|
+
const kept = [...picked].sort((left, right) => compareSequence(left, right)).filter((row) => {
|
|
1065
|
+
if (row["delivery_key"] === null) {
|
|
1066
|
+
return true;
|
|
1067
|
+
}
|
|
1068
|
+
const group = `${row["handler"]}\0${row["delivery_key"]}`;
|
|
1069
|
+
return !seenKeys.has(group) && Boolean(seenKeys.add(group));
|
|
1070
|
+
});
|
|
1071
|
+
kept.sort(compareDue);
|
|
1072
|
+
return kept.slice(0, limit);
|
|
1073
|
+
}
|
|
1074
|
+
return picked;
|
|
1075
|
+
}
|
|
1076
|
+
/** Writes the picked ids into their lane rows and frees the lanes that picked nothing. */
|
|
1077
|
+
async #recordClaim(claimed, {
|
|
1078
|
+
lanes,
|
|
1079
|
+
request,
|
|
1080
|
+
transaction
|
|
1081
|
+
}) {
|
|
1082
|
+
const idsByLane = /* @__PURE__ */ new Map();
|
|
1083
|
+
for (const row of claimed) {
|
|
1084
|
+
const lane = readCount(row, "lane");
|
|
1085
|
+
idsByLane.set(lane, [...idsByLane.get(lane) ?? [], row["delivery_id"]]);
|
|
1086
|
+
}
|
|
1087
|
+
const idleLanes = [...lanes].filter((lane) => !idsByLane.has(lane));
|
|
1088
|
+
if (idsByLane.size === 0 && idleLanes.length === 0) {
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
await this.#state.query({
|
|
1092
|
+
sql: this.#state.statements.claim.recordClaim,
|
|
1093
|
+
parameters: [
|
|
1094
|
+
JSON.stringify([...idsByLane].map(([lane, ids]) => ({ lane, ids }))),
|
|
1095
|
+
JSON.stringify(idleLanes),
|
|
1096
|
+
request.leaseOwner,
|
|
1097
|
+
request.leaseExpiresAt.toISOString()
|
|
1098
|
+
],
|
|
1099
|
+
transaction
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
1103
|
+
var GENERATION_STEP = 1n;
|
|
1104
|
+
function leftWorkBehind(row) {
|
|
1105
|
+
return row["takeover_waiting"] === true || row["unexamined_free_lanes"] === true || row["stale"] === true;
|
|
1106
|
+
}
|
|
1107
|
+
function compareSequence(left, right) {
|
|
1108
|
+
const first = sequenceOf(left);
|
|
1109
|
+
const second = sequenceOf(right);
|
|
1110
|
+
return Number(first > second) - Number(first < second);
|
|
1111
|
+
}
|
|
1112
|
+
function sequenceOf(row) {
|
|
1113
|
+
return BigInt(row["sequence"] ?? 0);
|
|
1114
|
+
}
|
|
1115
|
+
function compareDue(left, right) {
|
|
1116
|
+
return compareText(left["due_at"], right["due_at"]) || compareText(left["delivery_id"], right["delivery_id"]);
|
|
1117
|
+
}
|
|
1118
|
+
function compareText(left, right) {
|
|
1119
|
+
return Number(left > right) - Number(left < right);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
// src/postgres-completion.ts
|
|
1123
|
+
var PostgresCompletion = class {
|
|
1124
|
+
#state;
|
|
1125
|
+
constructor(state) {
|
|
1126
|
+
this.#state = state;
|
|
1127
|
+
}
|
|
1128
|
+
/** Implements {@link PostgresEventStore.extendLease}. */
|
|
1129
|
+
extendLease(lease, leaseExpiresAt) {
|
|
1130
|
+
return this.#whenIssued(
|
|
1131
|
+
lease,
|
|
1132
|
+
(held) => this.#changed(this.#state.statements.completion.extendLease, [
|
|
1133
|
+
lease.deliveryId,
|
|
1134
|
+
held.lane,
|
|
1135
|
+
held.epoch,
|
|
1136
|
+
held.claims,
|
|
1137
|
+
leaseExpiresAt.toISOString()
|
|
1138
|
+
])
|
|
1139
|
+
);
|
|
1140
|
+
}
|
|
1141
|
+
/** Implements {@link PostgresEventStore.complete}. */
|
|
1142
|
+
async complete(lease, { now, transaction }) {
|
|
1143
|
+
return (await this.#completeLeases([lease], { now, transaction }))[0];
|
|
1144
|
+
}
|
|
1145
|
+
/** Implements {@link PostgresEventStore.completeMany}. */
|
|
1146
|
+
completeMany(leases, now) {
|
|
1147
|
+
return this.#completeLeases(leases, { now, transaction: void 0 });
|
|
1148
|
+
}
|
|
1149
|
+
/** Implements {@link PostgresEventStore.reschedule}. */
|
|
1150
|
+
reschedule(lease, {
|
|
1151
|
+
availableAt,
|
|
1152
|
+
error,
|
|
1153
|
+
now
|
|
1154
|
+
}) {
|
|
1155
|
+
return this.#settled(
|
|
1156
|
+
lease,
|
|
1157
|
+
this.#whenIssued(
|
|
1158
|
+
lease,
|
|
1159
|
+
(held) => this.#changed(this.#state.statements.completion.reschedule, [
|
|
1160
|
+
lease.deliveryId,
|
|
1161
|
+
held.lane,
|
|
1162
|
+
held.epoch,
|
|
1163
|
+
held.claims,
|
|
1164
|
+
availableAt.toISOString(),
|
|
1165
|
+
JSON.stringify(error),
|
|
1166
|
+
now.toISOString()
|
|
1167
|
+
])
|
|
1168
|
+
)
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
/** Implements {@link PostgresEventStore.bury}. */
|
|
1172
|
+
bury(lease, { error, now }) {
|
|
1173
|
+
return this.#settled(
|
|
1174
|
+
lease,
|
|
1175
|
+
this.#whenIssued(
|
|
1176
|
+
lease,
|
|
1177
|
+
(held) => this.#changed(this.#state.statements.completion.bury, [
|
|
1178
|
+
lease.deliveryId,
|
|
1179
|
+
held.lane,
|
|
1180
|
+
held.epoch,
|
|
1181
|
+
held.claims,
|
|
1182
|
+
JSON.stringify(error),
|
|
1183
|
+
now.toISOString()
|
|
1184
|
+
])
|
|
1185
|
+
)
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
/** Implements {@link PostgresEventStore.release}. */
|
|
1189
|
+
release(lease, now) {
|
|
1190
|
+
return this.#settled(
|
|
1191
|
+
lease,
|
|
1192
|
+
this.#whenIssued(
|
|
1193
|
+
lease,
|
|
1194
|
+
(held) => this.#changed(this.#state.statements.completion.release, [
|
|
1195
|
+
lease.deliveryId,
|
|
1196
|
+
held.lane,
|
|
1197
|
+
held.epoch,
|
|
1198
|
+
held.claims,
|
|
1199
|
+
now.toISOString()
|
|
1200
|
+
])
|
|
1201
|
+
)
|
|
1202
|
+
);
|
|
1203
|
+
}
|
|
1204
|
+
/** Forgets a handed-out lease once the store answered about it (any answer: the claim is over or fenced out). */
|
|
1205
|
+
async #settled(lease, work) {
|
|
1206
|
+
const result = await work;
|
|
1207
|
+
this.#state.handedOut.delete(handedOutKey(lease.deliveryId, lease.leaseToken));
|
|
1208
|
+
return result;
|
|
1209
|
+
}
|
|
1210
|
+
async #changed(sql, parameters) {
|
|
1211
|
+
const rows = await this.#state.query({ sql, parameters });
|
|
1212
|
+
return readCount(rows[0], "count") > 0;
|
|
1213
|
+
}
|
|
1214
|
+
/** Runs `operation` for a lease this store issued; any other token cannot be held. */
|
|
1215
|
+
async #whenIssued(lease, operation) {
|
|
1216
|
+
const held = parseLeaseToken(lease.leaseToken);
|
|
1217
|
+
return held !== void 0 && await operation(held);
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Deletes the delivery of every lease that is still the current claim of its
|
|
1221
|
+
* delivery. Returns one flag per lease, in order. Results are matched to leases
|
|
1222
|
+
* by position, so two leases of one delivery (a stale and a current one) get
|
|
1223
|
+
* their own answers; a lease listed twice completes once, at its first position.
|
|
1224
|
+
*/
|
|
1225
|
+
async #completeLeases(leases, { now, transaction }) {
|
|
1226
|
+
const issued = [];
|
|
1227
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1228
|
+
leases.forEach((lease, ord) => {
|
|
1229
|
+
const held = parseLeaseToken(lease.leaseToken);
|
|
1230
|
+
const identity = `${lease.deliveryId}\0${lease.leaseToken}`;
|
|
1231
|
+
if (held !== void 0 && !seen.has(identity)) {
|
|
1232
|
+
seen.add(identity);
|
|
1233
|
+
issued.push({ ord, id: lease.deliveryId, ...held });
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
const completed = /* @__PURE__ */ new Set();
|
|
1237
|
+
if (issued.length > 0) {
|
|
1238
|
+
const rows = await this.#state.query({
|
|
1239
|
+
sql: this.#state.statements.completion.complete,
|
|
1240
|
+
parameters: [JSON.stringify(issued), now.toISOString()],
|
|
1241
|
+
transaction
|
|
1242
|
+
});
|
|
1243
|
+
for (const row of rows) {
|
|
1244
|
+
completed.add(readCount(row, "ord"));
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
if (transaction === void 0) {
|
|
1248
|
+
for (const lease of leases) {
|
|
1249
|
+
this.#state.handedOut.delete(handedOutKey(lease.deliveryId, lease.leaseToken));
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
return leases.map((_, ord) => completed.has(ord));
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
var OwnedConnections = class {
|
|
1256
|
+
#clients = /* @__PURE__ */ new Set();
|
|
1257
|
+
/**
|
|
1258
|
+
* The `Client` class for a pool the store creates: every connection is tracked from the moment it
|
|
1259
|
+
* is created, so one still connecting or authenticating is destroyed too.
|
|
1260
|
+
*/
|
|
1261
|
+
clientClass() {
|
|
1262
|
+
const owned = this;
|
|
1263
|
+
return class OwnedClient extends Client {
|
|
1264
|
+
constructor(config) {
|
|
1265
|
+
super(config);
|
|
1266
|
+
owned.track(this);
|
|
1267
|
+
}
|
|
495
1268
|
};
|
|
496
|
-
|
|
497
|
-
|
|
1269
|
+
}
|
|
1270
|
+
/** Tracks one connection until it ends. */
|
|
1271
|
+
track(client) {
|
|
1272
|
+
this.#clients.add(client);
|
|
1273
|
+
client.once("end", () => this.#clients.delete(client));
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Destroys the socket of every tracked connection. Its queries fail with "Connection terminated"
|
|
1277
|
+
* and the pool drops it. The server ends the session when it next talks to the client.
|
|
1278
|
+
*/
|
|
1279
|
+
destroyAll() {
|
|
1280
|
+
for (const client of this.#clients) {
|
|
1281
|
+
client.on("error", () => void 0);
|
|
1282
|
+
client.connection.stream.destroy();
|
|
498
1283
|
}
|
|
499
|
-
|
|
500
|
-
|
|
1284
|
+
this.#clients.clear();
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
|
|
1288
|
+
// src/postgres-maintenance.ts
|
|
1289
|
+
var PostgresMaintenance = class {
|
|
1290
|
+
#state;
|
|
1291
|
+
constructor(state) {
|
|
1292
|
+
this.#state = state;
|
|
1293
|
+
}
|
|
1294
|
+
/** Implements {@link PostgresEventStore.relinquish}. */
|
|
1295
|
+
async relinquish(leaseOwner) {
|
|
1296
|
+
await this.#state.query({
|
|
1297
|
+
sql: this.#state.statements.claim.relinquish,
|
|
1298
|
+
parameters: [leaseOwner]
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
/** Implements {@link PostgresEventStore.listDeliveries}. */
|
|
1302
|
+
async listDeliveries(query) {
|
|
1303
|
+
const statement = this.#state.statements.queries.listDeliveries(query);
|
|
1304
|
+
const rows = await this.#state.executor.query(statement.sql, statement.parameters);
|
|
1305
|
+
return rows.map(mapDeliveryRecord);
|
|
1306
|
+
}
|
|
1307
|
+
/** Implements {@link PostgresEventStore.requeue}. */
|
|
1308
|
+
async requeue(deliveryIds, now) {
|
|
1309
|
+
const rows = await this.#state.query({
|
|
1310
|
+
sql: this.#state.statements.queries.requeue,
|
|
1311
|
+
parameters: [JSON.stringify(deliveryIds), now.toISOString()]
|
|
1312
|
+
});
|
|
1313
|
+
return readCount(rows[0], "count");
|
|
1314
|
+
}
|
|
1315
|
+
/** Implements {@link PostgresEventStore.statistics}. */
|
|
1316
|
+
async statistics(now) {
|
|
1317
|
+
const stored = { pending: 0, dead: 0, completed: 0 };
|
|
1318
|
+
for (const row of await this.#state.query({
|
|
1319
|
+
sql: this.#state.statements.queries.countByStatus,
|
|
1320
|
+
parameters: []
|
|
1321
|
+
})) {
|
|
1322
|
+
stored[row["status"]] = readCount(row, "count");
|
|
501
1323
|
}
|
|
502
|
-
|
|
503
|
-
|
|
1324
|
+
const [running] = await this.#state.query({
|
|
1325
|
+
sql: this.#state.statements.queries.countRunning,
|
|
1326
|
+
parameters: []
|
|
1327
|
+
});
|
|
1328
|
+
const processing = readCount(running, "count");
|
|
1329
|
+
let completed = stored["completed"];
|
|
1330
|
+
if (this.#state.completionHistory) {
|
|
1331
|
+
const [history] = await this.#state.query({
|
|
1332
|
+
sql: this.#state.statements.queries.countCompletionHistory,
|
|
1333
|
+
parameters: []
|
|
1334
|
+
});
|
|
1335
|
+
completed += readCount(history, "count");
|
|
504
1336
|
}
|
|
505
|
-
|
|
506
|
-
|
|
1337
|
+
const oldestDue = await this.#state.query({
|
|
1338
|
+
sql: this.#state.statements.queries.oldestDue,
|
|
1339
|
+
parameters: [now.toISOString()]
|
|
1340
|
+
});
|
|
1341
|
+
return {
|
|
1342
|
+
// The counts come from separate statements; a delivery that finishes in between must not make this negative.
|
|
1343
|
+
pending: Math.max(0, stored["pending"] - processing),
|
|
1344
|
+
processing,
|
|
1345
|
+
completed,
|
|
1346
|
+
dead: stored["dead"],
|
|
1347
|
+
oldestDueAt: mapOldestDueAt(oldestDue)
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
/** Implements {@link PostgresEventStore.purge}. */
|
|
1351
|
+
async purge(request) {
|
|
1352
|
+
const total = { deletedDeliveries: 0, deletedEvents: 0 };
|
|
1353
|
+
const batches = [
|
|
1354
|
+
[
|
|
1355
|
+
this.#state.statements.purge.purgeCompletedBatch(),
|
|
1356
|
+
request.completedBefore,
|
|
1357
|
+
this.#state.statements.purge.deleteOrphanEventsWithoutCompletion
|
|
1358
|
+
],
|
|
1359
|
+
[
|
|
1360
|
+
this.#state.statements.purge.purgeDeadBatch(),
|
|
1361
|
+
request.deadBefore,
|
|
1362
|
+
this.#state.statements.purge.deleteOrphanEventsWithoutCompletion
|
|
1363
|
+
]
|
|
1364
|
+
];
|
|
1365
|
+
if (this.#state.completionHistory) {
|
|
1366
|
+
batches.push([
|
|
1367
|
+
this.#state.statements.purge.purgeCompletionHistoryBatch(),
|
|
1368
|
+
request.completedBefore,
|
|
1369
|
+
this.#state.statements.purge.deleteOrphanEventsWithoutCompletion
|
|
1370
|
+
]);
|
|
507
1371
|
}
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
1372
|
+
for (const [sql, cutoff, deleteOrphans] of batches) {
|
|
1373
|
+
if (cutoff === void 0) {
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
let deletedInBatch = this.#state.purgeBatchSize;
|
|
1377
|
+
while (deletedInBatch === this.#state.purgeBatchSize) {
|
|
1378
|
+
const batch = await this.#purgeBatch(sql, { cutoff, deleteOrphans });
|
|
1379
|
+
deletedInBatch = batch.deletedDeliveries;
|
|
1380
|
+
total.deletedDeliveries += batch.deletedDeliveries;
|
|
1381
|
+
total.deletedEvents += batch.deletedEvents;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
const orphanCutoff = [request.completedBefore, request.deadBefore].filter((cutoff) => cutoff !== void 0).sort((first, second) => first.getTime() - second.getTime())[0];
|
|
1385
|
+
if (orphanCutoff !== void 0) {
|
|
1386
|
+
total.deletedEvents += await this.#purgeOrphanEvents(
|
|
1387
|
+
this.#state.statements.purge.lockOrphanEventsBatch,
|
|
1388
|
+
{
|
|
1389
|
+
deleteStatement: this.#state.statements.purge.deleteOrphanEventsWithoutCompletion,
|
|
1390
|
+
cutoff: orphanCutoff,
|
|
1391
|
+
recheckCutoff: false
|
|
1392
|
+
}
|
|
1393
|
+
);
|
|
1394
|
+
}
|
|
1395
|
+
if (request.completedBefore !== void 0) {
|
|
1396
|
+
total.deletedEvents += await this.#purgeOrphanEvents(
|
|
1397
|
+
this.#state.statements.purge.lockCompletedOrphanEventsBatch,
|
|
1398
|
+
{
|
|
1399
|
+
deleteStatement: this.#state.statements.purge.deleteExpiredCompletedOrphanEvents,
|
|
1400
|
+
cutoff: request.completedBefore,
|
|
1401
|
+
recheckCutoff: true
|
|
1402
|
+
}
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
return total;
|
|
525
1406
|
}
|
|
526
|
-
|
|
527
|
-
|
|
1407
|
+
async #purgeOrphanEvents(lockStatement, {
|
|
1408
|
+
deleteStatement,
|
|
1409
|
+
cutoff,
|
|
1410
|
+
recheckCutoff
|
|
1411
|
+
}) {
|
|
1412
|
+
let deletedEvents = 0;
|
|
1413
|
+
let removedInBatch = this.#state.purgeBatchSize;
|
|
1414
|
+
while (removedInBatch === this.#state.purgeBatchSize) {
|
|
1415
|
+
removedInBatch = await this.#state.executor.transaction(async (transaction) => {
|
|
1416
|
+
const locked = await this.#state.query({
|
|
1417
|
+
sql: lockStatement,
|
|
1418
|
+
parameters: [cutoff.toISOString(), this.#state.purgeBatchSize],
|
|
1419
|
+
transaction
|
|
1420
|
+
});
|
|
1421
|
+
const ids = JSON.stringify(locked.map((row) => row["id"]));
|
|
1422
|
+
const removed = await this.#state.query({
|
|
1423
|
+
sql: deleteStatement,
|
|
1424
|
+
parameters: recheckCutoff ? [ids, cutoff.toISOString()] : [ids],
|
|
1425
|
+
transaction
|
|
1426
|
+
});
|
|
1427
|
+
return readCount(removed[0], "count");
|
|
1428
|
+
});
|
|
1429
|
+
deletedEvents += removedInBatch;
|
|
1430
|
+
}
|
|
1431
|
+
return deletedEvents;
|
|
1432
|
+
}
|
|
1433
|
+
async #purgeBatch(sql, { cutoff, deleteOrphans }) {
|
|
1434
|
+
return this.#state.executor.transaction(async (transaction) => {
|
|
1435
|
+
const deleted = await this.#state.query({
|
|
1436
|
+
sql,
|
|
1437
|
+
parameters: [cutoff.toISOString(), this.#state.purgeBatchSize],
|
|
1438
|
+
transaction
|
|
1439
|
+
});
|
|
1440
|
+
if (deleted.length === 0) {
|
|
1441
|
+
return { deletedDeliveries: 0, deletedEvents: 0 };
|
|
1442
|
+
}
|
|
1443
|
+
const eventIds = JSON.stringify([
|
|
1444
|
+
...new Set(deleted.map((row) => row["event_id"]))
|
|
1445
|
+
]);
|
|
1446
|
+
const locked = await this.#state.query({
|
|
1447
|
+
sql: this.#state.statements.purge.lockEvents,
|
|
1448
|
+
parameters: [eventIds],
|
|
1449
|
+
transaction
|
|
1450
|
+
});
|
|
1451
|
+
const removed = await this.#state.query({
|
|
1452
|
+
sql: deleteOrphans,
|
|
1453
|
+
parameters: [JSON.stringify(locked.map((row) => row["id"]))],
|
|
1454
|
+
transaction
|
|
1455
|
+
});
|
|
1456
|
+
return { deletedDeliveries: deleted.length, deletedEvents: readCount(removed[0], "count") };
|
|
1457
|
+
});
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
|
|
1461
|
+
// src/statements-append.ts
|
|
1462
|
+
var AppendStatements = class {
|
|
1463
|
+
#names;
|
|
1464
|
+
constructor(names) {
|
|
1465
|
+
this.#names = names;
|
|
1466
|
+
}
|
|
1467
|
+
/**
|
|
1468
|
+
* Publishes in one statement: reserves the sequences of the keys (`[{ key, count }]` in `$4`, locked in
|
|
1469
|
+
* key order: each row lock is held until the publishing transaction ends, which makes sequence order equal commit order for a key), numbers the keyed events in entry order (`ordinal`), inserts
|
|
1470
|
+
* the events of `$1` that do not exist yet and the deliveries of `$3` that belong to them, and sends one
|
|
1471
|
+
* wake-up notification when a delivery it inserted is due at `$5`. Parameters:
|
|
1472
|
+
* `[eventsJson, createdAt, deliveriesJson, keyCountsJson, now]`. Returns one row per event of `$1`: its
|
|
1473
|
+
* id, its sequence, whether this statement inserted it, and (on every row) how many deliveries it
|
|
1474
|
+
* inserted. Deliveries of events that already existed are left to {@link insertDeliveries}.
|
|
1475
|
+
*/
|
|
1476
|
+
get insertFreshEvents() {
|
|
1477
|
+
const events = this.#names.events;
|
|
1478
|
+
return `WITH reserved AS (
|
|
1479
|
+
INSERT INTO ${this.#names.keySequences} AS reserved (key, sequence)
|
|
1480
|
+
SELECT requested.key, requested.count
|
|
1481
|
+
FROM jsonb_to_recordset($4::jsonb) AS requested (key text, count bigint)
|
|
1482
|
+
ORDER BY requested.key
|
|
1483
|
+
ON CONFLICT (key) DO UPDATE SET sequence = reserved.sequence + EXCLUDED.sequence
|
|
1484
|
+
RETURNING reserved.key AS key, reserved.sequence AS sequence
|
|
1485
|
+
), incoming AS (
|
|
1486
|
+
SELECT incoming.*,
|
|
1487
|
+
CASE WHEN incoming.key IS NULL THEN NULL ELSE
|
|
1488
|
+
reserved.sequence - count(*) OVER (PARTITION BY incoming.key)
|
|
1489
|
+
+ row_number() OVER (PARTITION BY incoming.key ORDER BY incoming.ordinal)
|
|
1490
|
+
END AS sequence
|
|
1491
|
+
FROM jsonb_to_recordset($1::jsonb) AS incoming (
|
|
1492
|
+
ordinal int, id text, type text, source text, key text, payload jsonb, headers jsonb,
|
|
1493
|
+
correlation_id text, causation_id text, occurred_at timestamptz, handlers text[])
|
|
1494
|
+
LEFT JOIN reserved ON reserved.key = incoming.key
|
|
1495
|
+
), inserted_events AS (
|
|
1496
|
+
INSERT INTO ${events}
|
|
1497
|
+
(id, type, source, key, sequence, payload, headers, correlation_id, causation_id, occurred_at, created_at, handlers)
|
|
1498
|
+
SELECT incoming.id, incoming.type, incoming.source, incoming.key, incoming.sequence,
|
|
1499
|
+
COALESCE(incoming.payload, 'null'::jsonb), COALESCE(incoming.headers, '{}'::jsonb),
|
|
1500
|
+
incoming.correlation_id, incoming.causation_id, incoming.occurred_at, $2::timestamptz,
|
|
1501
|
+
COALESCE(incoming.handlers, '{}')
|
|
1502
|
+
FROM incoming
|
|
1503
|
+
ORDER BY incoming.id
|
|
1504
|
+
ON CONFLICT (id) DO NOTHING
|
|
1505
|
+
RETURNING id, key, sequence
|
|
1506
|
+
), inserted_deliveries AS (
|
|
1507
|
+
INSERT INTO ${this.#names.deliveries}
|
|
1508
|
+
(id, event_id, handler, lane, status, attempts, available_at, key, sequence, created_at, updated_at)
|
|
1509
|
+
SELECT delivery.id, delivery.event_id, delivery.handler, ${laneExpression("event.key", "event.id")}, 'pending', 0,
|
|
1510
|
+
delivery.available_at, event.key, event.sequence, $2::timestamptz, $2::timestamptz
|
|
1511
|
+
FROM jsonb_to_recordset($3::jsonb) AS delivery (id text, event_id text, handler text, available_at timestamptz)
|
|
1512
|
+
JOIN inserted_events AS event ON event.id = delivery.event_id
|
|
1513
|
+
ORDER BY delivery.event_id, delivery.handler
|
|
1514
|
+
ON CONFLICT (event_id, handler) DO NOTHING
|
|
1515
|
+
RETURNING available_at
|
|
1516
|
+
), ${this.#wakeUp("inserted_deliveries", "$5")}
|
|
1517
|
+
SELECT incoming.id, incoming.sequence::text AS sequence,
|
|
1518
|
+
incoming.id IN (SELECT inserted_events.id FROM inserted_events) AS inserted,
|
|
1519
|
+
(SELECT count(*) FROM inserted_deliveries)::int AS deliveries
|
|
1520
|
+
FROM incoming
|
|
1521
|
+
WHERE (SELECT count(*) FROM woken) >= 0`;
|
|
1522
|
+
}
|
|
1523
|
+
/** One wake-up notification on {@link PostgresNames.notificationChannel}, outside any publishing transaction. */
|
|
1524
|
+
get notify() {
|
|
1525
|
+
return `SELECT pg_notify('${this.#names.notificationChannel}', '')`;
|
|
1526
|
+
}
|
|
1527
|
+
/**
|
|
1528
|
+
* A CTE `woken` that sends one notification on {@link PostgresNames.notificationChannel} when a row of
|
|
1529
|
+
* `inserted` is due at `now`. PostgreSQL delivers it when the transaction commits, merges the same
|
|
1530
|
+
* notification sent twice in one transaction, and drops it on rollback. The statement must reference
|
|
1531
|
+
* `woken` for it to run.
|
|
1532
|
+
*/
|
|
1533
|
+
#wakeUp(inserted, now) {
|
|
1534
|
+
return `woken AS (
|
|
1535
|
+
SELECT pg_notify('${this.#names.notificationChannel}', '') FROM (
|
|
1536
|
+
SELECT 1 FROM ${inserted} WHERE ${inserted}.available_at <= ${now}::timestamptz LIMIT 1
|
|
1537
|
+
) AS due
|
|
1538
|
+
)`;
|
|
1539
|
+
}
|
|
1540
|
+
/**
|
|
1541
|
+
* Parameters: `[eventsJson, createdAt]`. Returns the ids that were inserted.
|
|
1542
|
+
* `handlers` is the list of handlers the event gets deliveries for: it
|
|
1543
|
+
* outlives the deliveries, which are deleted when they complete.
|
|
1544
|
+
*/
|
|
1545
|
+
get insertEvents() {
|
|
1546
|
+
return `INSERT INTO ${this.#names.events}
|
|
1547
|
+
(id, type, source, key, sequence, payload, headers, correlation_id, causation_id, occurred_at, created_at, handlers)
|
|
1548
|
+
SELECT incoming.id, incoming.type, incoming.source, incoming.key, incoming.sequence,
|
|
1549
|
+
COALESCE(incoming.payload, 'null'::jsonb), COALESCE(incoming.headers, '{}'::jsonb),
|
|
1550
|
+
incoming.correlation_id, incoming.causation_id, incoming.occurred_at, $2::timestamptz,
|
|
1551
|
+
COALESCE(incoming.handlers, '{}')
|
|
1552
|
+
FROM jsonb_to_recordset($1::jsonb) AS incoming (
|
|
1553
|
+
id text, type text, source text, key text, sequence bigint, payload jsonb, headers jsonb,
|
|
1554
|
+
correlation_id text, causation_id text, occurred_at timestamptz, handlers text[])
|
|
1555
|
+
ORDER BY incoming.id
|
|
1556
|
+
ON CONFLICT (id) DO NOTHING
|
|
1557
|
+
RETURNING id`;
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Locks events that were not inserted by the current append, in id order, so
|
|
1561
|
+
* that appends adding handlers to the same event run one after the other.
|
|
1562
|
+
* Parameters: `[eventIdsJson]`. `FOR NO KEY UPDATE` conflicts with itself and
|
|
1563
|
+
* not with the foreign key checks of other inserts.
|
|
1564
|
+
*/
|
|
1565
|
+
get lockExistingEvents() {
|
|
1566
|
+
return `SELECT id FROM ${this.#names.events}
|
|
1567
|
+
WHERE id = ANY (ARRAY(SELECT jsonb_array_elements_text($1::jsonb)))
|
|
1568
|
+
ORDER BY id
|
|
1569
|
+
FOR NO KEY UPDATE`;
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* Parameters: `[deliveriesJson, createdAt, insertedEventIdsJson, now]`. Key and
|
|
1573
|
+
* sequence are copied from the stored event, so a delivery added later to an
|
|
1574
|
+
* existing event keeps the event's position in its key and lands in the same
|
|
1575
|
+
* lane. Returns the ids that were inserted.
|
|
1576
|
+
*
|
|
1577
|
+
* The deliveries of events this call just inserted are all new. For an event
|
|
1578
|
+
* that already existed, a handler gets a delivery only if the event does not
|
|
1579
|
+
* list it yet: a completed delivery is deleted, but its handler stays listed,
|
|
1580
|
+
* so a redelivered or republished event never runs a handler twice. The
|
|
1581
|
+
* handlers added to an existing event are appended to its list.
|
|
1582
|
+
*/
|
|
1583
|
+
get insertDeliveries() {
|
|
1584
|
+
const events = this.#names.events;
|
|
1585
|
+
const insertedEvents = `ARRAY(SELECT jsonb_array_elements_text($3::jsonb))`;
|
|
1586
|
+
return `WITH incoming AS (
|
|
1587
|
+
SELECT incoming.id, incoming.event_id, incoming.handler, incoming.available_at
|
|
1588
|
+
FROM jsonb_to_recordset($1::jsonb) AS incoming (id text, event_id text, handler text, available_at timestamptz)
|
|
1589
|
+
), fresh AS (
|
|
1590
|
+
SELECT incoming.*, event.key, event.sequence, ${laneExpression("event.key", "event.id")} AS lane
|
|
1591
|
+
FROM incoming
|
|
1592
|
+
JOIN ${events} AS event ON event.id = incoming.event_id
|
|
1593
|
+
WHERE event.id = ANY (${insertedEvents}) OR NOT incoming.handler = ANY (event.handlers)
|
|
1594
|
+
), inserted AS (
|
|
1595
|
+
INSERT INTO ${this.#names.deliveries}
|
|
1596
|
+
(id, event_id, handler, lane, status, attempts, available_at, key, sequence, created_at, updated_at)
|
|
1597
|
+
SELECT fresh.id, fresh.event_id, fresh.handler, fresh.lane, 'pending', 0, fresh.available_at,
|
|
1598
|
+
fresh.key, fresh.sequence, $2::timestamptz, $2::timestamptz
|
|
1599
|
+
FROM fresh
|
|
1600
|
+
ORDER BY fresh.event_id, fresh.handler
|
|
1601
|
+
ON CONFLICT (event_id, handler) DO NOTHING
|
|
1602
|
+
RETURNING id, event_id, handler, available_at
|
|
1603
|
+
), extended AS (
|
|
1604
|
+
UPDATE ${events} AS event SET handlers = event.handlers || added.handlers
|
|
1605
|
+
FROM (
|
|
1606
|
+
SELECT inserted.event_id, array_agg(inserted.handler) AS handlers FROM inserted
|
|
1607
|
+
WHERE inserted.event_id <> ALL (${insertedEvents}) GROUP BY inserted.event_id
|
|
1608
|
+
) AS added
|
|
1609
|
+
WHERE event.id = added.event_id
|
|
1610
|
+
RETURNING event.id
|
|
1611
|
+
), ${this.#wakeUp("inserted", "$4")}
|
|
1612
|
+
SELECT id FROM inserted WHERE (SELECT count(*) FROM woken) >= 0`;
|
|
1613
|
+
}
|
|
1614
|
+
};
|
|
1615
|
+
|
|
1616
|
+
// src/statement-fragments.ts
|
|
1617
|
+
var CLAIM_SCAN_WINDOW = 256;
|
|
1618
|
+
var LANE_SCAN_WINDOW = 64;
|
|
1619
|
+
var LANE_DEPTH_CAP = 16;
|
|
1620
|
+
var INITIAL_FREE_LANES = 4;
|
|
1621
|
+
var HANDLER_PREDICATE = `delivery.handler = ANY (ARRAY(SELECT jsonb_array_elements_text($2::jsonb)))`;
|
|
1622
|
+
function eventColumns(alias) {
|
|
1623
|
+
return `${alias}.id AS event_id,
|
|
1624
|
+
${alias}.type AS event_type,
|
|
1625
|
+
${alias}.source AS event_source,
|
|
1626
|
+
${alias}.key AS event_key,
|
|
1627
|
+
${alias}.payload::text AS event_payload,
|
|
1628
|
+
${alias}.headers::text AS event_headers,
|
|
1629
|
+
${alias}.correlation_id AS event_correlation_id,
|
|
1630
|
+
${alias}.causation_id AS event_causation_id,
|
|
1631
|
+
${isoText(`${alias}.occurred_at`)} AS event_occurred_at`;
|
|
1632
|
+
}
|
|
1633
|
+
function countChanged(dml) {
|
|
1634
|
+
return `WITH changed AS (${dml} RETURNING 1) SELECT count(*)::int AS count FROM changed`;
|
|
1635
|
+
}
|
|
1636
|
+
function keyHeads(names, scope) {
|
|
1637
|
+
const deliveries = names.deliveries;
|
|
1638
|
+
const pending = `candidate.lane = ${scope}.lane AND candidate.status = 'pending'`;
|
|
1639
|
+
const nextHandler = (after) => `(SELECT min(wanted.handler) FROM jsonb_array_elements_text($2::jsonb) AS wanted (handler)${after === "" ? "" : ` WHERE wanted.handler > ${after}`})`;
|
|
1640
|
+
return `
|
|
1641
|
+
WITH RECURSIVE hops (handler, key) AS (
|
|
1642
|
+
SELECT ${nextHandler("")}, NULL::text
|
|
1643
|
+
UNION ALL
|
|
1644
|
+
SELECT CASE WHEN next.key IS NULL THEN ${nextHandler("hops.handler")} ELSE hops.handler END, next.key
|
|
1645
|
+
FROM hops
|
|
1646
|
+
CROSS JOIN LATERAL (
|
|
1647
|
+
SELECT CASE WHEN hops.key IS NULL
|
|
1648
|
+
THEN (SELECT candidate.key FROM ${deliveries} AS candidate
|
|
1649
|
+
WHERE ${pending} AND candidate.handler = hops.handler AND candidate.key IS NOT NULL
|
|
1650
|
+
ORDER BY candidate.key LIMIT 1)
|
|
1651
|
+
ELSE (SELECT candidate.key FROM ${deliveries} AS candidate
|
|
1652
|
+
WHERE ${pending} AND candidate.handler = hops.handler AND candidate.key > hops.key
|
|
1653
|
+
ORDER BY candidate.key LIMIT 1)
|
|
1654
|
+
END AS key
|
|
1655
|
+
OFFSET 0
|
|
1656
|
+
) AS next
|
|
1657
|
+
WHERE hops.handler IS NOT NULL
|
|
1658
|
+
)
|
|
1659
|
+
SELECT lowest.id, lowest.lane, lowest.available_at, lowest.handler, lowest.key
|
|
1660
|
+
FROM hops
|
|
1661
|
+
CROSS JOIN LATERAL (
|
|
1662
|
+
SELECT candidate.id, candidate.lane, candidate.available_at, candidate.handler, candidate.key
|
|
1663
|
+
FROM ${deliveries} AS candidate
|
|
1664
|
+
WHERE ${pending} AND candidate.handler = hops.handler AND candidate.key = hops.key
|
|
1665
|
+
ORDER BY candidate.sequence LIMIT 1
|
|
1666
|
+
) AS lowest
|
|
1667
|
+
WHERE hops.key IS NOT NULL
|
|
1668
|
+
`;
|
|
1669
|
+
}
|
|
1670
|
+
function noRunningDelivery(names, laneAlias) {
|
|
1671
|
+
return `NOT EXISTS (
|
|
1672
|
+
SELECT 1 FROM unnest(${laneAlias}.in_flight) AS flying (id)
|
|
1673
|
+
JOIN ${names.deliveries} AS delivery ON delivery.id = flying.id AND delivery.status = 'pending'
|
|
1674
|
+
)`;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
// src/statements-claim.ts
|
|
1678
|
+
var LANE_LIST = `ARRAY(SELECT jsonb_array_elements_text($4::jsonb)::smallint)`;
|
|
1679
|
+
var ClaimStatements = class {
|
|
1680
|
+
#names;
|
|
1681
|
+
constructor(names) {
|
|
1682
|
+
this.#names = names;
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Fast claim: picks from the first {@link CLAIM_SCAN_WINDOW} due deliveries of
|
|
1686
|
+
* the given lanes in due order, which is everything the claim needs while the
|
|
1687
|
+
* front of the queue is not clogged with blocked followers of busy keys.
|
|
1688
|
+
* Parameters: `[now, handlersJson, limit, lanesJson]`. The lanes must be held
|
|
1689
|
+
* and locked by the caller.
|
|
1690
|
+
*
|
|
1691
|
+
* Always returns one row per picked delivery, or a single row whose
|
|
1692
|
+
* `delivery_id` is null when nothing was picked. `window_saturated` says
|
|
1693
|
+
* whether the window was full: when it is and fewer than `limit` deliveries
|
|
1694
|
+
* were picked, deliveries beyond the window may still be claimable and the
|
|
1695
|
+
* caller runs {@link claimBehindBlockedKeys}. When the window is not
|
|
1696
|
+
* saturated every due delivery was examined, so the result is complete.
|
|
1697
|
+
*
|
|
1698
|
+
* A delivery is a candidate when it is pending, due, and not in flight. A
|
|
1699
|
+
* keyed delivery is a candidate only when it is the head of its (handler,
|
|
1700
|
+
* key), that is (a) no earlier delivery of that handler and key is pending
|
|
1701
|
+
* (an earlier one in flight is still a pending row, which is the ordering
|
|
1702
|
+
* guarantee), and (b) no other delivery of that handler and key is in
|
|
1703
|
+
* flight, which covers a delivery that re-enters the key behind a running one
|
|
1704
|
+
* (requeue, or a handler added to an old event).
|
|
1705
|
+
*
|
|
1706
|
+
* No row is locked or written here: the lanes' exclusive leases are the
|
|
1707
|
+
* mutual exclusion, so two claimers never see the same lane.
|
|
1708
|
+
*/
|
|
1709
|
+
get claim() {
|
|
1710
|
+
return `WITH ${this.#scopeAndRunning(`SELECT lane.lane, lane.epoch, lane.in_flight FROM ${this.#names.lanes} AS lane WHERE lane.lane = ANY (${LANE_LIST})`)},
|
|
1711
|
+
${this.#fastWindow()}
|
|
1712
|
+
)
|
|
1713
|
+
${this.#pickedRows(`((SELECT count(*) FROM due_window) >= ${CLAIM_SCAN_WINDOW} OR EXISTS (SELECT 1 FROM due_window GROUP BY lane HAVING count(*) >= ${LANE_SCAN_WINDOW}))`)}`;
|
|
1714
|
+
}
|
|
1715
|
+
/** The `due_window` and `candidates` CTEs of the fast claim, up to (not including) the closing parenthesis of `candidates`. */
|
|
1716
|
+
#fastWindow() {
|
|
1717
|
+
const deliveries = this.#names.deliveries;
|
|
1718
|
+
return `due_window AS MATERIALIZED (
|
|
1719
|
+
SELECT recent.id, recent.lane, recent.available_at
|
|
1720
|
+
FROM scope
|
|
1721
|
+
CROSS JOIN LATERAL (
|
|
1722
|
+
SELECT delivery.id, delivery.lane, delivery.available_at
|
|
1723
|
+
FROM ${deliveries} AS delivery
|
|
1724
|
+
WHERE delivery.lane = scope.lane AND delivery.status = 'pending'
|
|
1725
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
1726
|
+
AND delivery.id <> ALL (scope.in_flight)
|
|
1727
|
+
ORDER BY delivery.available_at, delivery.id
|
|
1728
|
+
LIMIT ${LANE_SCAN_WINDOW}
|
|
1729
|
+
) AS recent
|
|
1730
|
+
ORDER BY recent.available_at, recent.id
|
|
1731
|
+
LIMIT ${CLAIM_SCAN_WINDOW}
|
|
1732
|
+
), candidates AS MATERIALIZED (
|
|
1733
|
+
SELECT delivery.id, delivery.lane, delivery.available_at
|
|
1734
|
+
FROM due_window
|
|
1735
|
+
JOIN ${deliveries} AS delivery ON delivery.id = due_window.id
|
|
1736
|
+
WHERE delivery.key IS NULL OR NOT (${this.#blockedKey("delivery")})
|
|
1737
|
+
ORDER BY delivery.available_at, delivery.id
|
|
1738
|
+
LIMIT $3::int`;
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Slow claim, run when {@link claim} picked too few deliveries from a full
|
|
1742
|
+
* window: the front of the due queue is mostly followers of blocked keys.
|
|
1743
|
+
* Same parameters and rules as {@link claim}, but heads are found with
|
|
1744
|
+
* a loose index scan over the pending-key index of each lane (it hops from key
|
|
1745
|
+
* to key), so the cost follows the number of pending keys in the lanes
|
|
1746
|
+
* instead of one probe per due follower.
|
|
1747
|
+
*/
|
|
1748
|
+
get claimBehindBlockedKeys() {
|
|
1749
|
+
const deliveries = this.#names.deliveries;
|
|
1750
|
+
return `WITH ${this.#scopeAndRunning(`SELECT lane.lane, lane.epoch, lane.in_flight FROM ${this.#names.lanes} AS lane WHERE lane.lane = ANY (${LANE_LIST})`)},
|
|
1751
|
+
heads AS MATERIALIZED (
|
|
1752
|
+
SELECT head.id, head.lane, head.available_at, head.handler, head.key
|
|
1753
|
+
FROM scope
|
|
1754
|
+
CROSS JOIN LATERAL (${keyHeads(this.#names, "scope")}) AS head
|
|
1755
|
+
), keyed AS MATERIALIZED (
|
|
1756
|
+
SELECT heads.id, heads.lane, heads.available_at
|
|
1757
|
+
FROM heads
|
|
1758
|
+
JOIN scope ON scope.lane = heads.lane
|
|
1759
|
+
WHERE heads.available_at <= $1::timestamptz AND heads.id <> ALL (scope.in_flight)
|
|
1760
|
+
AND NOT EXISTS (SELECT 1 FROM running WHERE running.handler = heads.handler AND running.key = heads.key)
|
|
1761
|
+
ORDER BY heads.available_at, heads.id
|
|
1762
|
+
LIMIT $3::int
|
|
1763
|
+
), unkeyed AS MATERIALIZED (
|
|
1764
|
+
SELECT recent.id, recent.lane, recent.available_at
|
|
1765
|
+
FROM scope
|
|
1766
|
+
CROSS JOIN LATERAL (
|
|
1767
|
+
SELECT delivery.id, delivery.lane, delivery.available_at
|
|
1768
|
+
FROM ${deliveries} AS delivery
|
|
1769
|
+
WHERE delivery.lane = scope.lane AND delivery.key IS NULL AND delivery.status = 'pending'
|
|
1770
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
1771
|
+
AND delivery.id <> ALL (scope.in_flight)
|
|
1772
|
+
ORDER BY delivery.available_at, delivery.id
|
|
1773
|
+
LIMIT $3::int
|
|
1774
|
+
) AS recent
|
|
1775
|
+
ORDER BY recent.available_at, recent.id
|
|
1776
|
+
LIMIT $3::int
|
|
1777
|
+
), candidates AS MATERIALIZED (
|
|
1778
|
+
SELECT id, lane, available_at FROM (
|
|
1779
|
+
SELECT id, lane, available_at FROM keyed
|
|
1780
|
+
UNION ALL
|
|
1781
|
+
SELECT id, lane, available_at FROM unkeyed
|
|
1782
|
+
) AS both_sets
|
|
1783
|
+
ORDER BY available_at, id
|
|
1784
|
+
LIMIT $3::int
|
|
1785
|
+
)
|
|
1786
|
+
${this.#pickedRows("false")}`;
|
|
1787
|
+
}
|
|
1788
|
+
/** The held lanes with their epochs and in-flight ids, and the keyed deliveries running in them. */
|
|
1789
|
+
#scopeAndRunning(scopeQuery) {
|
|
1790
|
+
const deliveries = this.#names.deliveries;
|
|
1791
|
+
return `scope AS MATERIALIZED (
|
|
1792
|
+
${scopeQuery}
|
|
1793
|
+
), running AS MATERIALIZED (
|
|
1794
|
+
SELECT delivery.handler, delivery.key
|
|
1795
|
+
FROM scope
|
|
1796
|
+
CROSS JOIN LATERAL unnest(scope.in_flight) AS flying (id)
|
|
1797
|
+
JOIN ${deliveries} AS delivery ON delivery.id = flying.id
|
|
1798
|
+
WHERE delivery.status = 'pending' AND delivery.key IS NOT NULL
|
|
1799
|
+
)`;
|
|
1800
|
+
}
|
|
1801
|
+
/** True when an earlier delivery of the candidate's handler and key is pending, or another one is running. */
|
|
1802
|
+
#blockedKey(alias) {
|
|
1803
|
+
return `EXISTS (
|
|
1804
|
+
SELECT 1 FROM ${this.#names.deliveries} AS earlier
|
|
1805
|
+
WHERE earlier.lane = ${alias}.lane AND earlier.handler = ${alias}.handler AND earlier.key = ${alias}.key
|
|
1806
|
+
AND earlier.status = 'pending' AND earlier.key IS NOT NULL
|
|
1807
|
+
AND earlier.sequence < ${alias}.sequence
|
|
1808
|
+
) OR ${this.#runningElsewhere(alias)}`;
|
|
1809
|
+
}
|
|
1810
|
+
/** True when a delivery of the candidate's handler and key is in flight. The candidate itself never is. */
|
|
1811
|
+
#runningElsewhere(alias) {
|
|
1812
|
+
return `EXISTS (
|
|
1813
|
+
SELECT 1 FROM running
|
|
1814
|
+
WHERE running.handler = ${alias}.handler AND running.key = ${alias}.key
|
|
1815
|
+
)`;
|
|
1816
|
+
}
|
|
1817
|
+
/** Final SELECT shared by both claims: the picked deliveries with their lane epoch and event. */
|
|
1818
|
+
#pickedRows(saturated) {
|
|
1819
|
+
return `SELECT ${saturated} AS window_saturated, picked.*
|
|
1820
|
+
FROM (SELECT 1) AS anchor
|
|
1821
|
+
LEFT JOIN (
|
|
1822
|
+
${this.#pickedRowsSelect()}
|
|
1823
|
+
) AS picked ON true
|
|
1824
|
+
ORDER BY picked.due_at, picked.delivery_id`;
|
|
1825
|
+
}
|
|
1826
|
+
/** The picked deliveries joined with the lane epoch and their event, as the claim result columns. */
|
|
1827
|
+
#pickedRowsSelect() {
|
|
1828
|
+
return `SELECT candidates.available_at AS due_at, delivery.id AS delivery_id, delivery.lane::int AS lane,
|
|
1829
|
+
scope.epoch::text AS epoch, delivery.claims AS claims, delivery.handler, delivery.key AS delivery_key, delivery.sequence::text AS sequence, (delivery.attempts + 1)::int AS attempt,
|
|
1830
|
+
${eventColumns("event")}
|
|
1831
|
+
FROM candidates
|
|
1832
|
+
JOIN scope ON scope.lane = candidates.lane
|
|
1833
|
+
JOIN ${this.#names.deliveries} AS delivery ON delivery.id = candidates.id
|
|
1834
|
+
JOIN ${this.#names.events} AS event ON event.id = delivery.event_id`;
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* Records the deliveries a claim picked in their lanes and frees the lanes it
|
|
1838
|
+
* took or held for nothing. Parameters:
|
|
1839
|
+
* `[pickedJson, idleLanesJson, leaseOwner, leaseExpiresAt]` with
|
|
1840
|
+
* `pickedJson = [{ lane, ids }]`.
|
|
1841
|
+
*
|
|
1842
|
+
* A picked lane lists the ids that are still in flight (listed ids whose row
|
|
1843
|
+
* still exists) plus the new ones, and its lease reaches `leaseExpiresAt`.
|
|
1844
|
+
* An idle lane is freed when no delivery of it is in flight, so other workers
|
|
1845
|
+
* need not wait for the lease to expire.
|
|
1846
|
+
*/
|
|
1847
|
+
get recordClaim() {
|
|
1848
|
+
const deliveries = this.#names.deliveries;
|
|
1849
|
+
const lanes = this.#names.lanes;
|
|
1850
|
+
return `WITH picked AS (
|
|
1851
|
+
SELECT recorded.lane, recorded.ids
|
|
1852
|
+
FROM jsonb_to_recordset($1::jsonb) AS recorded (lane smallint, ids text[])
|
|
1853
|
+
), still_running AS (
|
|
1854
|
+
SELECT lane.lane, array_agg(delivery.id) AS ids
|
|
1855
|
+
FROM picked
|
|
1856
|
+
JOIN ${lanes} AS lane ON lane.lane = picked.lane
|
|
1857
|
+
CROSS JOIN LATERAL unnest(lane.in_flight) AS flying (id)
|
|
1858
|
+
JOIN ${deliveries} AS delivery ON delivery.id = flying.id AND delivery.status = 'pending'
|
|
1859
|
+
GROUP BY lane.lane
|
|
1860
|
+
), extended AS (
|
|
1861
|
+
UPDATE ${lanes} AS lane
|
|
1862
|
+
SET in_flight = COALESCE(still_running.ids, '{}') || picked.ids,
|
|
1863
|
+
lease_expires_at = GREATEST(lane.lease_expires_at, $4::timestamptz)
|
|
1864
|
+
FROM picked
|
|
1865
|
+
LEFT JOIN still_running ON still_running.lane = picked.lane
|
|
1866
|
+
WHERE lane.lane = picked.lane
|
|
1867
|
+
RETURNING lane.lane
|
|
1868
|
+
), freed AS (
|
|
1869
|
+
UPDATE ${lanes} AS lane
|
|
1870
|
+
SET lease_owner = NULL, lease_expires_at = NULL, in_flight = '{}'
|
|
1871
|
+
WHERE lane.lane = ANY (ARRAY(SELECT jsonb_array_elements_text($2::jsonb)::smallint))
|
|
1872
|
+
AND lane.lease_owner = $3::text
|
|
1873
|
+
AND ${noRunningDelivery(this.#names, "lane")}
|
|
1874
|
+
RETURNING lane.lane
|
|
1875
|
+
)
|
|
1876
|
+
SELECT (SELECT count(*) FROM extended)::int AS extended, (SELECT count(*) FROM freed)::int AS freed`;
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Keeps in the in-flight lists of the lanes `$1` holds only the claims in `$2`
|
|
1880
|
+
* (`[{ id, lane, epoch, claims }]`, the ones this store handed out and nobody
|
|
1881
|
+
* settled yet) and takes every other delivery out. That is a batch a claim
|
|
1882
|
+
* recorded but whose answer never arrived: nothing runs it and the lane lease,
|
|
1883
|
+
* renewed by the owner's other deliveries, would keep it away from everybody for
|
|
1884
|
+
* ever. A claim matches only with the lane epoch and the delivery generation it was
|
|
1885
|
+
* handed out with. Parameters: `[leaseOwner, handedOutJson, generation]`; a lane whose `claim_floor` is above
|
|
1886
|
+
* the generation was reconciled by a newer call, so an older (late) reconciliation leaves it alone.
|
|
1887
|
+
* The lanes are locked in lane order first (see the lock order on {@link PostgresStatements}).
|
|
1888
|
+
*/
|
|
1889
|
+
get reconcileClaims() {
|
|
1890
|
+
const deliveries = this.#names.deliveries;
|
|
1891
|
+
const handedOut = `jsonb_to_recordset($2::jsonb) AS handed (id text, lane smallint, epoch bigint, claims int)`;
|
|
1892
|
+
const kept = `SELECT 1 FROM ${handedOut} JOIN ${deliveries} AS delivery ON delivery.id = handed.id
|
|
1893
|
+
WHERE handed.id = flying.id AND handed.lane = lane.lane AND handed.epoch = lane.epoch AND delivery.claims = handed.claims`;
|
|
1894
|
+
return `WITH owned AS MATERIALIZED (
|
|
1895
|
+
SELECT lane.lane FROM ${this.#names.lanes} AS lane
|
|
1896
|
+
WHERE lane.lease_owner = $1::text AND lane.claim_floor <= $3::bigint
|
|
1897
|
+
ORDER BY lane.lane
|
|
1898
|
+
FOR NO KEY UPDATE
|
|
1899
|
+
)
|
|
1900
|
+
UPDATE ${this.#names.lanes} AS lane
|
|
1901
|
+
SET in_flight = ARRAY(SELECT flying.id FROM unnest(lane.in_flight) AS flying (id) WHERE EXISTS (${kept})),
|
|
1902
|
+
claim_floor = $3::bigint
|
|
1903
|
+
FROM owned
|
|
1904
|
+
WHERE lane.lane = owned.lane AND lane.lease_owner = $1::text AND lane.claim_floor <= $3::bigint`;
|
|
1905
|
+
}
|
|
1906
|
+
/**
|
|
1907
|
+
* Frees every lane `$1` holds that has no running delivery, locking them in lane order first.
|
|
1908
|
+
* Parameters: `[leaseOwner]`.
|
|
1909
|
+
*/
|
|
1910
|
+
get relinquish() {
|
|
1911
|
+
return `WITH owned AS MATERIALIZED (
|
|
1912
|
+
SELECT lane.lane FROM ${this.#names.lanes} AS lane
|
|
1913
|
+
WHERE lane.lease_owner = $1::text
|
|
1914
|
+
ORDER BY lane.lane
|
|
1915
|
+
FOR NO KEY UPDATE
|
|
1916
|
+
)
|
|
1917
|
+
UPDATE ${this.#names.lanes} AS lane
|
|
1918
|
+
SET lease_owner = NULL, lease_expires_at = NULL, in_flight = '{}'
|
|
1919
|
+
FROM owned
|
|
1920
|
+
WHERE lane.lane = owned.lane AND lane.lease_owner = $1::text AND ${noRunningDelivery(this.#names, "lane")}`;
|
|
1921
|
+
}
|
|
1922
|
+
};
|
|
1923
|
+
|
|
1924
|
+
// src/statements-completion.ts
|
|
1925
|
+
var CompletionStatements = class {
|
|
1926
|
+
#names;
|
|
1927
|
+
constructor(names) {
|
|
1928
|
+
this.#names = names;
|
|
1929
|
+
}
|
|
1930
|
+
/**
|
|
1931
|
+
* Extends the lease of the lane a running delivery belongs to; the lane lease
|
|
1932
|
+
* never shrinks. Parameters: `[deliveryId, lane, epoch, claims, leaseExpiresAt]`.
|
|
1933
|
+
*/
|
|
1934
|
+
get extendLease() {
|
|
1935
|
+
return countChanged(`UPDATE ${this.#names.lanes} AS lane
|
|
1936
|
+
SET lease_expires_at = GREATEST(lane.lease_expires_at, $5::timestamptz)
|
|
1937
|
+
WHERE lane.lane = $2::smallint AND lane.epoch = $3::bigint AND $1::text = ANY (lane.in_flight)
|
|
1938
|
+
AND EXISTS (
|
|
1939
|
+
SELECT 1 FROM ${this.#names.deliveries} AS delivery
|
|
1940
|
+
WHERE delivery.id = $1::text AND delivery.status = 'pending' AND delivery.claims = $4::int
|
|
1941
|
+
)`);
|
|
1942
|
+
}
|
|
1943
|
+
/**
|
|
1944
|
+
* Deletes the running deliveries whose lane lease is still the one they were
|
|
1945
|
+
* claimed under. Parameters: `[leasesJson, now]` with
|
|
1946
|
+
* `leasesJson = [{ ord, id, lane, epoch, claims }]`. Returns the `ord` of every lease that was completed, so
|
|
1947
|
+
* results are matched to leases and not to delivery ids. A delivery is only deleted by the claim that
|
|
1948
|
+
* is still its current one (`claims`), which makes a stale lease harmless even when the same owner re-claimed
|
|
1949
|
+
* the delivery in the same lane epoch. The event gets a completion time for retention.
|
|
1950
|
+
*
|
|
1951
|
+
* The lane rows are locked `FOR KEY SHARE` first, in lane order, and the lock
|
|
1952
|
+
* is held until the surrounding transaction ends. A claimer that takes the
|
|
1953
|
+
* lane over locks it `FOR UPDATE`, which conflicts with that lock, so it
|
|
1954
|
+
* skips the lane until this completion commits or rolls back: a completion
|
|
1955
|
+
* and a takeover cannot both succeed for one delivery, which is what makes a
|
|
1956
|
+
* transactional handler's writes effectively-once. The weakest lock that
|
|
1957
|
+
* conflicts with a takeover is used on purpose: it does not conflict with the
|
|
1958
|
+
* owner's own claim and heartbeats (plain updates of the lane row), which
|
|
1959
|
+
* would otherwise wait for every completion in progress. With a completion history the deleted rows are copied to
|
|
1960
|
+
* it in the same statement.
|
|
1961
|
+
*/
|
|
1962
|
+
get complete() {
|
|
1963
|
+
const deliveries = this.#names.deliveries;
|
|
1964
|
+
const history = this.#names.completionHistory;
|
|
1965
|
+
return `WITH leases AS (
|
|
1966
|
+
SELECT lease.ord, lease.id, lease.lane, lease.epoch, lease.claims
|
|
1967
|
+
FROM jsonb_to_recordset($1::jsonb) AS lease (ord int, id text, lane smallint, epoch bigint, claims int)
|
|
1968
|
+
), fence AS MATERIALIZED (
|
|
1969
|
+
SELECT lane.lane, lane.epoch, lane.in_flight
|
|
1970
|
+
FROM ${this.#names.lanes} AS lane
|
|
1971
|
+
WHERE lane.lane IN (SELECT lane FROM leases)
|
|
1972
|
+
ORDER BY lane.lane
|
|
1973
|
+
FOR KEY SHARE
|
|
1974
|
+
), completed AS (
|
|
1975
|
+
DELETE FROM ${deliveries} AS delivery
|
|
1976
|
+
USING leases JOIN fence ON fence.lane = leases.lane AND fence.epoch = leases.epoch
|
|
1977
|
+
WHERE delivery.id = leases.id AND delivery.lane = leases.lane AND delivery.status = 'pending'
|
|
1978
|
+
AND delivery.claims = leases.claims AND leases.id = ANY (fence.in_flight)
|
|
1979
|
+
RETURNING leases.ord, delivery.id, delivery.event_id, delivery.handler, delivery.attempts, delivery.available_at, delivery.created_at
|
|
1980
|
+
), stamped AS (
|
|
1981
|
+
INSERT INTO ${this.#names.eventCompletions} (event_id, completed_at)
|
|
1982
|
+
SELECT DISTINCT completed.event_id, $2::timestamptz FROM completed
|
|
1983
|
+
-- Event id order: completions of other handlers of the same events lock these rows too.
|
|
1984
|
+
ORDER BY completed.event_id
|
|
1985
|
+
ON CONFLICT (event_id) DO UPDATE SET completed_at = GREATEST(${this.#names.eventCompletions}.completed_at, EXCLUDED.completed_at)
|
|
1986
|
+
RETURNING event_id
|
|
1987
|
+
)${history ? `, archived AS (
|
|
1988
|
+
INSERT INTO ${this.#names.completions} (id, event_id, handler, attempts, available_at, created_at, completed_at)
|
|
1989
|
+
SELECT id, event_id, handler, attempts + 1, available_at, created_at, $2::timestamptz FROM completed
|
|
1990
|
+
RETURNING id
|
|
1991
|
+
)` : ""}
|
|
1992
|
+
SELECT ord FROM completed`;
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Returns a running delivery to pending, due at `availableAt`, and takes it
|
|
1996
|
+
* out of the lane's in-flight list. Parameters:
|
|
1997
|
+
* `[deliveryId, lane, epoch, claims, availableAt, errorJson, now]`. The attempt that
|
|
1998
|
+
* just ended is counted.
|
|
1999
|
+
*/
|
|
2000
|
+
get reschedule() {
|
|
2001
|
+
return this.#leaveFlight(
|
|
2002
|
+
`SET available_at = $5::timestamptz, last_error = $6::jsonb, attempts = delivery.attempts + 1, claims = delivery.claims + 1, updated_at = $7::timestamptz`
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
2005
|
+
/** Parameters: `[deliveryId, lane, epoch, claims, errorJson, now]`. The attempt that just ended is counted. */
|
|
2006
|
+
get bury() {
|
|
2007
|
+
return this.#leaveFlight(
|
|
2008
|
+
`SET status = 'dead', last_error = $5::jsonb, attempts = delivery.attempts + 1, claims = delivery.claims + 1, updated_at = $6::timestamptz`
|
|
2009
|
+
);
|
|
2010
|
+
}
|
|
2011
|
+
/**
|
|
2012
|
+
* Gives a running delivery back without spending its attempt (an attempt is
|
|
2013
|
+
* only recorded when it ends in failure or a lost lease): pending, due `now`.
|
|
2014
|
+
* Parameters: `[deliveryId, lane, epoch, claims, now]`.
|
|
2015
|
+
*/
|
|
2016
|
+
get release() {
|
|
2017
|
+
return this.#leaveFlight(
|
|
2018
|
+
`SET available_at = $5::timestamptz, claims = delivery.claims + 1, updated_at = $5::timestamptz`
|
|
2019
|
+
);
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Applies `assignments` to a running delivery and removes it from its lane's
|
|
2023
|
+
* in-flight list, if the lane still has the caller's epoch. The lane row is
|
|
2024
|
+
* locked first (`FOR NO KEY UPDATE`, which conflicts with a takeover's `FOR UPDATE`), so a takeover cannot interleave. Parameters
|
|
2025
|
+
* `$1`..`$4` are always `[deliveryId, lane, epoch, claims]`; ending a claim bumps `claims`.
|
|
2026
|
+
*/
|
|
2027
|
+
#leaveFlight(assignments) {
|
|
2028
|
+
const lanes = this.#names.lanes;
|
|
2029
|
+
return `WITH fence AS MATERIALIZED (
|
|
2030
|
+
SELECT lane.lane FROM ${lanes} AS lane
|
|
2031
|
+
WHERE lane.lane = $2::smallint AND lane.epoch = $3::bigint AND $1::text = ANY (lane.in_flight)
|
|
2032
|
+
FOR NO KEY UPDATE
|
|
2033
|
+
), changed AS (
|
|
2034
|
+
UPDATE ${this.#names.deliveries} AS delivery ${assignments}
|
|
2035
|
+
FROM fence
|
|
2036
|
+
WHERE delivery.id = $1::text AND delivery.lane = fence.lane AND delivery.status = 'pending'
|
|
2037
|
+
AND delivery.claims = $4::int
|
|
2038
|
+
RETURNING delivery.id
|
|
2039
|
+
), trimmed AS (
|
|
2040
|
+
UPDATE ${lanes} AS lane SET in_flight = array_remove(lane.in_flight, $1::text)
|
|
2041
|
+
FROM changed WHERE lane.lane = $2::smallint
|
|
2042
|
+
RETURNING lane.lane
|
|
2043
|
+
)
|
|
2044
|
+
SELECT count(*)::int AS count FROM changed`;
|
|
2045
|
+
}
|
|
2046
|
+
};
|
|
2047
|
+
|
|
2048
|
+
// src/statements-lanes.ts
|
|
2049
|
+
var FREE_LANE_SURPLUS = 16;
|
|
2050
|
+
function notRunning(running, alias) {
|
|
2051
|
+
return running === null ? "true" : `NOT EXISTS (SELECT 1 FROM ${running} AS running WHERE running.handler = ${alias}.handler AND running.key = ${alias}.key)`;
|
|
2052
|
+
}
|
|
2053
|
+
var freeLane = (alias) => `(${alias}.lease_expires_at IS NULL OR ${alias}.lease_expires_at <= $1::timestamptz
|
|
2054
|
+
OR (${alias}.lease_owner = $4::text AND ${alias}.claim_floor < $6::bigint))`;
|
|
2055
|
+
var NO_FREE_LANES = `), free_lanes AS (
|
|
2056
|
+
SELECT NULL::smallint AS lane, NULL::bigint AS epoch, NULL::text[] AS in_flight, false AS running WHERE false
|
|
2057
|
+
), free_scope AS (
|
|
2058
|
+
SELECT NULL::smallint AS lane, NULL::bigint AS epoch WHERE false
|
|
2059
|
+
), free_candidates AS (
|
|
2060
|
+
SELECT NULL::text AS id, NULL::smallint AS lane, NULL::timestamptz AS available_at WHERE false
|
|
2061
|
+
)`;
|
|
2062
|
+
var LaneStatements = class {
|
|
2063
|
+
#names;
|
|
2064
|
+
constructor(names) {
|
|
2065
|
+
this.#names = names;
|
|
2066
|
+
}
|
|
2067
|
+
/**
|
|
2068
|
+
* A claim in one statement, the only one in the common case. Parameters:
|
|
2069
|
+
* `[now, handlersJson, limit, leaseOwner, leaseExpiresAt, generation]`.
|
|
2070
|
+
*
|
|
2071
|
+
* 1. **Held lanes.** The lanes this owner holds under a live lease whose `claim_floor` is exactly the
|
|
2072
|
+
* generation are locked `FOR NO KEY UPDATE` in lane order. A lane above it was raised by a
|
|
2073
|
+
* reconciliation, so a statement of a claim whose caller gave up (a client timeout does not cancel
|
|
2074
|
+
* the statement on the server) and that reaches the server late records nothing; a lane below it was
|
|
2075
|
+
* taken by such a late statement after the reconciliation, and its listed deliveries were never
|
|
2076
|
+
* handed out. A lane that another transaction of this owner is changing makes the statement wait;
|
|
2077
|
+
* the statement then reports `stale` and does nothing (see the `stale` CTE).
|
|
2078
|
+
* 2. **Free lanes**, only when the held lanes yield fewer than `limit`: the free lanes with a due
|
|
2079
|
+
* delivery (one index probe per free lane) are ranked by their oldest due delivery; the first
|
|
2080
|
+
* {@link INITIAL_FREE_LANES} that can give something now, and further ones until what they can give
|
|
2081
|
+
* (estimated from their first {@link LANE_DEPTH_CAP} due deliveries) covers what is still wanted, are locked
|
|
2082
|
+
* `FOR UPDATE SKIP LOCKED`, so a lane another claimer has is skipped, never waited
|
|
2083
|
+
* for. A lane that still runs deliveries of an expired lease is left to the transaction of
|
|
2084
|
+
* {@link takeFreeLanes}, which charges their lost attempts (`takeover_waiting`); so is any lane whose
|
|
2085
|
+
* locked row differs from what the statement's snapshot saw. A lane that runs nothing and did not
|
|
2086
|
+
* change cannot have had a delivery written since the snapshot, so its deliveries can be read here.
|
|
2087
|
+
* 3. **Picks** follow the rules of `ClaimStatements.claim`: the fast window of each lane, and for lanes whose
|
|
2088
|
+
* window filled up while the batch is not full, the heads of all keys (a loose index scan per lane).
|
|
2089
|
+
* 4. **Writes**: one update per lane. A held lane records its picks; a taken lane gets a new epoch, the
|
|
2090
|
+
* owner, the lease and its picks; a taken lane that yielded nothing is not written. Held lanes that
|
|
2091
|
+
* picked nothing, run nothing and have no full scan window are freed.
|
|
2092
|
+
*
|
|
2093
|
+
* Always returns one row per picked delivery, or one row whose `delivery_id` is null. `running_ids`
|
|
2094
|
+
* (JSON text) lists the deliveries the held lanes still run, `examined_lanes` the held lanes.
|
|
2095
|
+
*/
|
|
2096
|
+
get claimLanes() {
|
|
2097
|
+
return this.#claimLanes(true);
|
|
2098
|
+
}
|
|
2099
|
+
/**
|
|
2100
|
+
* {@link claimLanes} limited to the lanes the owner holds: the same parameters, rows and writes, without
|
|
2101
|
+
* looking at free lanes (`takeover_waiting` and `unexamined_free_lanes` are false). A worker draining a
|
|
2102
|
+
* backlog finds its batch in the lanes it holds; this statement is about half as costly to plan, and
|
|
2103
|
+
* the caller runs {@link claimLanes} after it only when it came up short.
|
|
2104
|
+
*/
|
|
2105
|
+
get claimHeldLanes() {
|
|
2106
|
+
return this.#claimLanes(false);
|
|
2107
|
+
}
|
|
2108
|
+
#claimLanes(includeFreeLanes) {
|
|
2109
|
+
const free = includeFreeLanes ? this.#freeLaneChoice() + this.#freeLaneDepth() + this.#freeLaneTake() : NO_FREE_LANES;
|
|
2110
|
+
return this.#heldLanes() + free + this.#claimPicks() + this.#claimRecord() + this.#claimRelease();
|
|
2111
|
+
}
|
|
2112
|
+
/** Locks the held lanes, detects a stale snapshot and picks from the held lanes; ends with `wanted`. */
|
|
2113
|
+
#heldLanes() {
|
|
2114
|
+
const deliveries = this.#names.deliveries;
|
|
2115
|
+
const lanes = this.#names.lanes;
|
|
2116
|
+
return `WITH held AS MATERIALIZED (
|
|
2117
|
+
SELECT lane.lane, lane.epoch, lane.in_flight
|
|
2118
|
+
FROM ${lanes} AS lane
|
|
2119
|
+
WHERE lane.lease_owner = $4::text AND lane.lease_expires_at > $1::timestamptz
|
|
2120
|
+
AND lane.claim_floor = $6::bigint
|
|
2121
|
+
ORDER BY lane.lane
|
|
2122
|
+
FOR NO KEY UPDATE
|
|
2123
|
+
), stale AS (
|
|
2124
|
+
-- A claim that waited for a lane lock sees the new lane row but reads deliveries with the snapshot it took
|
|
2125
|
+
-- before the wait: whatever changed the lane meanwhile (a reschedule, a release) may also have changed
|
|
2126
|
+
-- deliveries it cannot see. The statement then does nothing and reports it, and the caller runs it again.
|
|
2127
|
+
SELECT EXISTS (
|
|
2128
|
+
SELECT 1 FROM held
|
|
2129
|
+
JOIN ${lanes} AS before ON before.lane = held.lane
|
|
2130
|
+
WHERE before.epoch <> held.epoch OR before.in_flight IS DISTINCT FROM held.in_flight
|
|
2131
|
+
) AS value
|
|
2132
|
+
), held_scope AS MATERIALIZED (
|
|
2133
|
+
SELECT held.lane, held.epoch, held.in_flight FROM held WHERE NOT (SELECT value FROM stale)
|
|
2134
|
+
), held_running AS MATERIALIZED (
|
|
2135
|
+
SELECT delivery.handler, delivery.key
|
|
2136
|
+
FROM held_scope
|
|
2137
|
+
CROSS JOIN LATERAL unnest(held_scope.in_flight) AS flying (id)
|
|
2138
|
+
JOIN ${deliveries} AS delivery ON delivery.id = flying.id
|
|
2139
|
+
WHERE delivery.status = 'pending' AND delivery.key IS NOT NULL
|
|
2140
|
+
), ${this.#candidates("held", { scope: "held_scope", running: "held_running", limit: "$3::int" })}, wanted AS MATERIALIZED (
|
|
2141
|
+
SELECT CASE WHEN (SELECT value FROM stale) THEN 0
|
|
2142
|
+
ELSE $3::int - (SELECT count(*) FROM held_candidates)::int END AS count
|
|
2143
|
+
`;
|
|
2144
|
+
}
|
|
2145
|
+
/** The oldest due delivery of each free lane, at most the number still wanted plus a surplus. */
|
|
2146
|
+
#freeLaneChoice() {
|
|
2147
|
+
const deliveries = this.#names.deliveries;
|
|
2148
|
+
const lanes = this.#names.lanes;
|
|
2149
|
+
return `), free_lanes AS MATERIALIZED (
|
|
2150
|
+
-- The oldest due delivery of each free lane: one index probe per free lane, bounded by the due time,
|
|
2151
|
+
-- so deliveries scheduled for later are never read.
|
|
2152
|
+
SELECT lane.lane, lane.epoch, lane.in_flight, lane.lease_owner, lane.lease_expires_at,
|
|
2153
|
+
oldest.available_at, oldest.id, NOT ${noRunningDelivery(this.#names, "lane")} AS running
|
|
2154
|
+
FROM ${lanes} AS lane
|
|
2155
|
+
CROSS JOIN LATERAL (
|
|
2156
|
+
SELECT delivery.available_at, delivery.id
|
|
2157
|
+
FROM ${deliveries} AS delivery
|
|
2158
|
+
WHERE delivery.lane = lane.lane AND delivery.status = 'pending'
|
|
2159
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
2160
|
+
ORDER BY delivery.available_at, delivery.id
|
|
2161
|
+
LIMIT 1
|
|
2162
|
+
) AS oldest
|
|
2163
|
+
WHERE (SELECT count FROM wanted) > 0 AND ${freeLane("lane")}
|
|
2164
|
+
ORDER BY oldest.available_at, oldest.id
|
|
2165
|
+
LIMIT (SELECT count FROM wanted) + ${FREE_LANE_SURPLUS}
|
|
2166
|
+
`;
|
|
2167
|
+
}
|
|
2168
|
+
/** What each free candidate lane can give now, and their order. */
|
|
2169
|
+
#freeLaneDepth() {
|
|
2170
|
+
const deliveries = this.#names.deliveries;
|
|
2171
|
+
return `), free_depth AS MATERIALIZED (
|
|
2172
|
+
-- What each candidate lane can give now, from its first due deliveries: unkeyed ones, and of a key its
|
|
2173
|
+
-- lowest one when nothing earlier of the key is pending. Lanes that can give nothing are passed over;
|
|
2174
|
+
-- of the others, the first ${INITIAL_FREE_LANES} are taken, and further ones until they cover what is
|
|
2175
|
+
-- still wanted, so the earliest due work of the free lanes comes first.
|
|
2176
|
+
SELECT runnable.lane,
|
|
2177
|
+
row_number() OVER (ORDER BY runnable.available_at, runnable.id) AS rank,
|
|
2178
|
+
sum(runnable.count) OVER (ORDER BY runnable.available_at, runnable.id ROWS UNBOUNDED PRECEDING) - runnable.count AS due_before
|
|
2179
|
+
FROM (
|
|
2180
|
+
SELECT lane_candidate.lane, lane_candidate.available_at, lane_candidate.id, depth.count
|
|
2181
|
+
FROM free_lanes AS lane_candidate
|
|
2182
|
+
CROSS JOIN LATERAL (
|
|
2183
|
+
-- A full window that shows nothing runnable may hide runnable work behind it: such a lane counts
|
|
2184
|
+
-- once, and the claim looks behind its blocked deliveries.
|
|
2185
|
+
SELECT GREATEST(count(*) FILTER (
|
|
2186
|
+
WHERE due.key IS NULL OR (due.position = 1 AND NOT EXISTS (
|
|
2187
|
+
SELECT 1 FROM ${deliveries} AS earlier
|
|
2188
|
+
WHERE earlier.lane = due.lane AND earlier.handler = due.handler AND earlier.key = due.key
|
|
2189
|
+
AND earlier.status = 'pending' AND earlier.key IS NOT NULL AND earlier.sequence < due.sequence
|
|
2190
|
+
))
|
|
2191
|
+
), CASE WHEN count(*) >= ${LANE_DEPTH_CAP} THEN 1 ELSE 0 END) AS count
|
|
2192
|
+
FROM (
|
|
2193
|
+
SELECT window_rows.*,
|
|
2194
|
+
row_number() OVER (PARTITION BY window_rows.handler, window_rows.key ORDER BY window_rows.sequence) AS position
|
|
2195
|
+
FROM (
|
|
2196
|
+
SELECT delivery.lane, delivery.handler, delivery.key, delivery.sequence FROM ${deliveries} AS delivery
|
|
2197
|
+
WHERE delivery.lane = lane_candidate.lane AND delivery.status = 'pending'
|
|
2198
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
2199
|
+
ORDER BY delivery.available_at, delivery.id
|
|
2200
|
+
LIMIT ${LANE_DEPTH_CAP}
|
|
2201
|
+
) AS window_rows
|
|
2202
|
+
) AS due
|
|
2203
|
+
) AS depth
|
|
2204
|
+
WHERE NOT lane_candidate.running AND depth.count > 0
|
|
2205
|
+
) AS runnable
|
|
2206
|
+
`;
|
|
2207
|
+
}
|
|
2208
|
+
/** Locks the chosen free lanes that did not change hands and picks from them. */
|
|
2209
|
+
#freeLaneTake() {
|
|
2210
|
+
const lanes = this.#names.lanes;
|
|
2211
|
+
return `), free_locked AS MATERIALIZED (
|
|
2212
|
+
SELECT lane.lane, lane.epoch, lane.in_flight, lane.lease_owner, lane.lease_expires_at
|
|
2213
|
+
FROM ${lanes} AS lane
|
|
2214
|
+
WHERE lane.lane IN (
|
|
2215
|
+
SELECT chosen.lane FROM free_depth AS chosen
|
|
2216
|
+
WHERE chosen.rank <= ${INITIAL_FREE_LANES} OR chosen.due_before < (SELECT count FROM wanted)
|
|
2217
|
+
)
|
|
2218
|
+
AND ${freeLane("lane")}
|
|
2219
|
+
ORDER BY lane.lane
|
|
2220
|
+
FOR UPDATE SKIP LOCKED
|
|
2221
|
+
), free_scope AS MATERIALIZED (
|
|
2222
|
+
-- A lane whose locked row is not the one the snapshot saw changed hands meanwhile: left for a later claim.
|
|
2223
|
+
SELECT locked.lane, locked.epoch + 1 AS epoch, '{}'::text[] AS in_flight
|
|
2224
|
+
FROM free_locked AS locked
|
|
2225
|
+
JOIN free_lanes AS seen ON seen.lane = locked.lane
|
|
2226
|
+
WHERE locked.epoch = seen.epoch AND locked.in_flight = seen.in_flight
|
|
2227
|
+
AND locked.lease_owner IS NOT DISTINCT FROM seen.lease_owner
|
|
2228
|
+
AND locked.lease_expires_at IS NOT DISTINCT FROM seen.lease_expires_at
|
|
2229
|
+
), ${this.#candidates("free", { scope: "free_scope", running: null, limit: "(SELECT count FROM wanted)" })}`;
|
|
2230
|
+
}
|
|
2231
|
+
/** The picked deliveries of held and taken lanes with their events, and the picks per lane. */
|
|
2232
|
+
#claimPicks() {
|
|
2233
|
+
const deliveries = this.#names.deliveries;
|
|
2234
|
+
return `, claim_scope AS MATERIALIZED (
|
|
2235
|
+
SELECT held_scope.lane, held_scope.epoch FROM held_scope
|
|
2236
|
+
UNION ALL
|
|
2237
|
+
SELECT free_scope.lane, free_scope.epoch FROM free_scope
|
|
2238
|
+
), picked_rows AS MATERIALIZED (
|
|
2239
|
+
SELECT candidates.available_at AS due_at, delivery.id AS delivery_id, delivery.lane::int AS lane,
|
|
2240
|
+
scope.epoch::text AS epoch, delivery.claims AS claims, delivery.handler, delivery.key AS delivery_key, delivery.sequence::text AS sequence, (delivery.attempts + 1)::int AS attempt,
|
|
2241
|
+
${eventColumns("event")}
|
|
2242
|
+
FROM (
|
|
2243
|
+
SELECT held_candidates.id, held_candidates.lane, held_candidates.available_at FROM held_candidates
|
|
2244
|
+
UNION ALL
|
|
2245
|
+
SELECT free_candidates.id, free_candidates.lane, free_candidates.available_at FROM free_candidates
|
|
2246
|
+
) AS candidates
|
|
2247
|
+
JOIN claim_scope AS scope ON scope.lane = candidates.lane
|
|
2248
|
+
JOIN ${deliveries} AS delivery ON delivery.id = candidates.id
|
|
2249
|
+
JOIN ${this.#names.events} AS event ON event.id = delivery.event_id
|
|
2250
|
+
), by_lane AS (
|
|
2251
|
+
SELECT picked_rows.lane, array_agg(picked_rows.delivery_id) AS ids FROM picked_rows GROUP BY picked_rows.lane
|
|
2252
|
+
`;
|
|
2253
|
+
}
|
|
2254
|
+
/** Records the picks in the held lanes and takes the free lanes that yielded picks. */
|
|
2255
|
+
#claimRecord() {
|
|
2256
|
+
const deliveries = this.#names.deliveries;
|
|
2257
|
+
const lanes = this.#names.lanes;
|
|
2258
|
+
return `), still_running AS (
|
|
2259
|
+
-- The retained ids come from the locked \`held\` rows, not from the lane table: a claim that waited for the lock
|
|
2260
|
+
-- must see the in-flight list the claim before it recorded, which the statement snapshot may not.
|
|
2261
|
+
SELECT held.lane, array_agg(delivery.id) AS ids
|
|
2262
|
+
FROM by_lane
|
|
2263
|
+
JOIN held ON held.lane = by_lane.lane
|
|
2264
|
+
CROSS JOIN LATERAL unnest(held.in_flight) AS flying (id)
|
|
2265
|
+
JOIN ${deliveries} AS delivery ON delivery.id = flying.id AND delivery.status = 'pending'
|
|
2266
|
+
GROUP BY held.lane
|
|
2267
|
+
), extended AS (
|
|
2268
|
+
UPDATE ${lanes} AS lane
|
|
2269
|
+
SET in_flight = COALESCE(still_running.ids, '{}') || by_lane.ids,
|
|
2270
|
+
lease_expires_at = GREATEST(lane.lease_expires_at, $5::timestamptz)
|
|
2271
|
+
FROM by_lane
|
|
2272
|
+
JOIN held_scope ON held_scope.lane = by_lane.lane
|
|
2273
|
+
LEFT JOIN still_running ON still_running.lane = by_lane.lane
|
|
2274
|
+
WHERE lane.lane = by_lane.lane
|
|
2275
|
+
RETURNING lane.lane
|
|
2276
|
+
), taken AS (
|
|
2277
|
+
UPDATE ${lanes} AS lane
|
|
2278
|
+
SET epoch = free_scope.epoch, lease_owner = $4::text, lease_expires_at = $5::timestamptz,
|
|
2279
|
+
in_flight = by_lane.ids, claim_floor = $6::bigint
|
|
2280
|
+
FROM free_scope
|
|
2281
|
+
JOIN by_lane ON by_lane.lane = free_scope.lane
|
|
2282
|
+
WHERE lane.lane = free_scope.lane
|
|
2283
|
+
RETURNING lane.lane
|
|
2284
|
+
`;
|
|
2285
|
+
}
|
|
2286
|
+
/** Frees the held lanes that yielded nothing and run nothing, and selects the result rows. */
|
|
2287
|
+
#claimRelease() {
|
|
2288
|
+
const deliveries = this.#names.deliveries;
|
|
2289
|
+
const lanes = this.#names.lanes;
|
|
2290
|
+
return `), saturated_lanes AS (
|
|
2291
|
+
SELECT held_window.lane FROM held_window GROUP BY held_window.lane HAVING count(*) >= ${LANE_SCAN_WINDOW}
|
|
2292
|
+
), freed AS (
|
|
2293
|
+
UPDATE ${lanes} AS lane
|
|
2294
|
+
SET lease_owner = NULL, lease_expires_at = NULL, in_flight = '{}'
|
|
2295
|
+
FROM held
|
|
2296
|
+
WHERE lane.lane = held.lane
|
|
2297
|
+
AND lane.lane NOT IN (SELECT lane FROM by_lane)
|
|
2298
|
+
AND lane.lane NOT IN (SELECT lane FROM saturated_lanes)
|
|
2299
|
+
AND (SELECT count(*) FROM held_window) < ${CLAIM_SCAN_WINDOW}
|
|
2300
|
+
AND NOT (SELECT value FROM stale)
|
|
2301
|
+
AND ${noRunningDelivery(this.#names, "lane")}
|
|
2302
|
+
RETURNING lane.lane
|
|
2303
|
+
)
|
|
2304
|
+
SELECT (SELECT value FROM stale) AS stale,
|
|
2305
|
+
EXISTS (SELECT 1 FROM free_lanes WHERE free_lanes.running) AS takeover_waiting,
|
|
2306
|
+
(SELECT count(*) FROM free_lanes) = (SELECT count FROM wanted) + ${FREE_LANE_SURPLUS} AS unexamined_free_lanes,
|
|
2307
|
+
(
|
|
2308
|
+
SELECT COALESCE(jsonb_agg(delivery.id), '[]'::jsonb)
|
|
2309
|
+
FROM held
|
|
2310
|
+
CROSS JOIN LATERAL unnest(held.in_flight) AS flying (id)
|
|
2311
|
+
JOIN ${deliveries} AS delivery ON delivery.id = flying.id AND delivery.status = 'pending'
|
|
2312
|
+
)::text AS running_ids,
|
|
2313
|
+
(SELECT COALESCE(jsonb_agg(held.lane), '[]'::jsonb) FROM held)::text AS examined_lanes,
|
|
2314
|
+
picked.*
|
|
2315
|
+
FROM (SELECT 1) AS anchor
|
|
2316
|
+
LEFT JOIN picked_rows AS picked ON true
|
|
2317
|
+
ORDER BY picked.due_at, picked.delivery_id`;
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* CTEs that pick up to `limit` claimable deliveries from the lanes of the CTE `scope` (columns `lane`,
|
|
2321
|
+
* `epoch`, `in_flight`), named after `prefix`; the result is `<prefix>_candidates (id, lane, available_at)`.
|
|
2322
|
+
* The rules are those of `ClaimStatements.claim`. The fast window reads the first {@link LANE_SCAN_WINDOW} due
|
|
2323
|
+
* deliveries of each lane (at most {@link CLAIM_SCAN_WINDOW} together); of a key, only the lowest sequence
|
|
2324
|
+
* in the window can be its head, so only that one is checked for an earlier pending delivery. Only when
|
|
2325
|
+
* that yields fewer than `limit` are the lanes whose window filled up looked at again with a loose index
|
|
2326
|
+
* scan over the heads of all their keys. `running` names the CTE of the keyed deliveries running in the
|
|
2327
|
+
* lanes, or is null when nothing runs there.
|
|
2328
|
+
*/
|
|
2329
|
+
#candidates(prefix, {
|
|
2330
|
+
scope,
|
|
2331
|
+
running,
|
|
2332
|
+
limit
|
|
2333
|
+
}) {
|
|
2334
|
+
const spec = { prefix, scope, running, limit };
|
|
2335
|
+
return this.#fastCandidates(spec) + this.#slowCandidates(spec);
|
|
2336
|
+
}
|
|
2337
|
+
/** The fast window of each lane and the claimable deliveries in it (`<prefix>_window`, `<prefix>_fast`). */
|
|
2338
|
+
#fastCandidates({ prefix, scope, running, limit }) {
|
|
2339
|
+
const deliveries = this.#names.deliveries;
|
|
2340
|
+
return `${prefix}_window AS MATERIALIZED (
|
|
2341
|
+
SELECT recent.id, recent.lane, recent.available_at, recent.handler, recent.key, recent.sequence
|
|
2342
|
+
FROM ${scope}
|
|
2343
|
+
CROSS JOIN LATERAL (
|
|
2344
|
+
SELECT delivery.id, delivery.lane, delivery.available_at, delivery.handler, delivery.key, delivery.sequence
|
|
2345
|
+
FROM ${deliveries} AS delivery
|
|
2346
|
+
WHERE delivery.lane = ${scope}.lane AND delivery.status = 'pending'
|
|
2347
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
2348
|
+
AND delivery.id <> ALL (${scope}.in_flight)
|
|
2349
|
+
ORDER BY delivery.available_at, delivery.id
|
|
2350
|
+
LIMIT ${LANE_SCAN_WINDOW}
|
|
2351
|
+
) AS recent
|
|
2352
|
+
ORDER BY recent.available_at, recent.id
|
|
2353
|
+
LIMIT ${CLAIM_SCAN_WINDOW}
|
|
2354
|
+
), ${prefix}_fast AS MATERIALIZED (
|
|
2355
|
+
SELECT picked.id, picked.lane, picked.available_at FROM (
|
|
2356
|
+
SELECT unkeyed.id, unkeyed.lane, unkeyed.available_at FROM ${prefix}_window AS unkeyed WHERE unkeyed.key IS NULL
|
|
2357
|
+
UNION ALL
|
|
2358
|
+
SELECT head.id, head.lane, head.available_at
|
|
2359
|
+
FROM (
|
|
2360
|
+
SELECT DISTINCT ON (keyed.handler, keyed.key) keyed.id, keyed.lane, keyed.available_at, keyed.handler, keyed.key, keyed.sequence
|
|
2361
|
+
FROM ${prefix}_window AS keyed
|
|
2362
|
+
WHERE keyed.key IS NOT NULL
|
|
2363
|
+
ORDER BY keyed.handler, keyed.key, keyed.sequence
|
|
2364
|
+
) AS head
|
|
2365
|
+
WHERE NOT EXISTS (
|
|
2366
|
+
SELECT 1 FROM ${deliveries} AS earlier
|
|
2367
|
+
WHERE earlier.lane = head.lane AND earlier.handler = head.handler AND earlier.key = head.key
|
|
2368
|
+
AND earlier.status = 'pending' AND earlier.key IS NOT NULL AND earlier.sequence < head.sequence
|
|
2369
|
+
) AND ${notRunning(running, "head")}
|
|
2370
|
+
) AS picked
|
|
2371
|
+
ORDER BY picked.available_at, picked.id
|
|
2372
|
+
LIMIT ${limit}
|
|
2373
|
+
`;
|
|
2374
|
+
}
|
|
2375
|
+
/**
|
|
2376
|
+
* For lanes whose window filled up while the fast pick came up short: the heads of all their keys and
|
|
2377
|
+
* their unkeyed deliveries, then the union with the fast pick (`<prefix>_candidates`).
|
|
2378
|
+
*/
|
|
2379
|
+
#slowCandidates({ prefix, scope, running, limit }) {
|
|
2380
|
+
const deliveries = this.#names.deliveries;
|
|
2381
|
+
return `), ${prefix}_saturated AS MATERIALIZED (
|
|
2382
|
+
SELECT ${scope}.lane, ${scope}.in_flight FROM ${scope}
|
|
2383
|
+
WHERE (SELECT count(*) FROM ${prefix}_fast) < ${limit}
|
|
2384
|
+
AND ((SELECT count(*) FROM ${prefix}_window) >= ${CLAIM_SCAN_WINDOW}
|
|
2385
|
+
OR ${scope}.lane IN (
|
|
2386
|
+
SELECT full_lane.lane FROM ${prefix}_window AS full_lane
|
|
2387
|
+
GROUP BY full_lane.lane HAVING count(*) >= ${LANE_SCAN_WINDOW}
|
|
2388
|
+
))
|
|
2389
|
+
), ${prefix}_heads AS MATERIALIZED (
|
|
2390
|
+
SELECT head.id, head.lane, head.available_at, head.handler, head.key
|
|
2391
|
+
FROM ${prefix}_saturated AS scope
|
|
2392
|
+
CROSS JOIN LATERAL (${keyHeads(this.#names, "scope")}) AS head
|
|
2393
|
+
), ${prefix}_keyed AS MATERIALIZED (
|
|
2394
|
+
SELECT heads.id, heads.lane, heads.available_at
|
|
2395
|
+
FROM ${prefix}_heads AS heads
|
|
2396
|
+
JOIN ${prefix}_saturated AS scope ON scope.lane = heads.lane
|
|
2397
|
+
WHERE heads.available_at <= $1::timestamptz AND heads.id <> ALL (scope.in_flight) AND ${notRunning(running, "heads")}
|
|
2398
|
+
ORDER BY heads.available_at, heads.id
|
|
2399
|
+
LIMIT ${limit}
|
|
2400
|
+
), ${prefix}_unkeyed AS MATERIALIZED (
|
|
2401
|
+
SELECT recent.id, recent.lane, recent.available_at
|
|
2402
|
+
FROM ${prefix}_saturated AS scope
|
|
2403
|
+
CROSS JOIN LATERAL (
|
|
2404
|
+
SELECT delivery.id, delivery.lane, delivery.available_at
|
|
2405
|
+
FROM ${deliveries} AS delivery
|
|
2406
|
+
WHERE delivery.lane = scope.lane AND delivery.key IS NULL AND delivery.status = 'pending'
|
|
2407
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
2408
|
+
AND delivery.id <> ALL (scope.in_flight)
|
|
2409
|
+
ORDER BY delivery.available_at, delivery.id
|
|
2410
|
+
LIMIT ${limit}
|
|
2411
|
+
) AS recent
|
|
2412
|
+
ORDER BY recent.available_at, recent.id
|
|
2413
|
+
LIMIT ${limit}
|
|
2414
|
+
), ${prefix}_candidates AS MATERIALIZED (
|
|
2415
|
+
SELECT chosen.id, chosen.lane, chosen.available_at FROM (
|
|
2416
|
+
SELECT fast.id, fast.lane, fast.available_at FROM ${prefix}_fast AS fast
|
|
2417
|
+
UNION
|
|
2418
|
+
SELECT keyed.id, keyed.lane, keyed.available_at FROM ${prefix}_keyed AS keyed
|
|
2419
|
+
UNION
|
|
2420
|
+
SELECT unkeyed.id, unkeyed.lane, unkeyed.available_at FROM ${prefix}_unkeyed AS unkeyed
|
|
2421
|
+
) AS chosen
|
|
2422
|
+
ORDER BY chosen.available_at, chosen.id
|
|
2423
|
+
LIMIT ${limit}
|
|
2424
|
+
)`;
|
|
2425
|
+
}
|
|
2426
|
+
/**
|
|
2427
|
+
* Takes up to `$3` free lanes (no lease, or an expired one) that have a due
|
|
2428
|
+
* delivery of the requested handlers, the ones with the oldest due delivery
|
|
2429
|
+
* first. Parameters: `[now, handlersJson, laneCount, leaseOwner, leaseExpiresAt, generation]`.
|
|
2430
|
+
* The lane gets the owner's `generation` as its `claim_floor`. A lane the owner itself holds under a live
|
|
2431
|
+
* lease with a lower floor was taken by a claim statement that reached the server after its caller gave up
|
|
2432
|
+
* and the owner reconciled (or by an earlier incarnation of the same owner): it is taken over like the lane of a
|
|
2433
|
+
* crashed worker, so its listed deliveries are charged an attempt.
|
|
2434
|
+
*
|
|
2435
|
+
* A taken lane gets a new epoch, which fences everything the previous owner
|
|
2436
|
+
* still holds. Deliveries the previous owner had in flight are the ones whose
|
|
2437
|
+
* attempt was never recorded (a claim writes no delivery row), so each of
|
|
2438
|
+
* them is charged one attempt here: a worker that crashes with a poison
|
|
2439
|
+
* message still runs it into the retry limit. Lanes another claimer has locked
|
|
2440
|
+
* are skipped, never waited for.
|
|
2441
|
+
*/
|
|
2442
|
+
get takeFreeLanes() {
|
|
2443
|
+
const deliveries = this.#names.deliveries;
|
|
2444
|
+
const lanes = this.#names.lanes;
|
|
2445
|
+
return `WITH picked AS (
|
|
2446
|
+
-- in_flight is read here, from the locked version of the lane: a settlement that committed after the
|
|
2447
|
+
-- statement took its snapshot has taken its delivery out of the list already and must not be charged again.
|
|
2448
|
+
SELECT free.lane, free.in_flight
|
|
2449
|
+
FROM ${lanes} AS free
|
|
2450
|
+
CROSS JOIN LATERAL (
|
|
2451
|
+
SELECT delivery.available_at, delivery.id
|
|
2452
|
+
FROM ${deliveries} AS delivery
|
|
2453
|
+
WHERE delivery.lane = free.lane AND delivery.status = 'pending'
|
|
2454
|
+
AND delivery.available_at <= $1::timestamptz AND ${HANDLER_PREDICATE}
|
|
2455
|
+
ORDER BY delivery.available_at, delivery.id
|
|
2456
|
+
LIMIT 1
|
|
2457
|
+
) AS oldest
|
|
2458
|
+
WHERE free.lease_expires_at IS NULL OR free.lease_expires_at <= $1::timestamptz
|
|
2459
|
+
OR (free.lease_owner = $4::text AND free.claim_floor < $6::bigint)
|
|
2460
|
+
ORDER BY oldest.available_at, oldest.id
|
|
2461
|
+
LIMIT $3::int
|
|
2462
|
+
FOR UPDATE OF free SKIP LOCKED
|
|
2463
|
+
), charged AS (
|
|
2464
|
+
UPDATE ${deliveries} AS delivery SET attempts = delivery.attempts + 1, claims = delivery.claims + 1
|
|
2465
|
+
FROM (
|
|
2466
|
+
SELECT unnest(picked.in_flight) AS id FROM picked
|
|
2467
|
+
) AS lost
|
|
2468
|
+
WHERE delivery.id = lost.id AND delivery.status = 'pending'
|
|
2469
|
+
RETURNING delivery.id
|
|
2470
|
+
), taken AS (
|
|
2471
|
+
UPDATE ${lanes} AS lane
|
|
2472
|
+
SET epoch = lane.epoch + 1, lease_owner = $4::text, lease_expires_at = $5::timestamptz, in_flight = '{}',
|
|
2473
|
+
claim_floor = $6::bigint
|
|
2474
|
+
FROM picked WHERE lane.lane = picked.lane
|
|
2475
|
+
RETURNING lane.lane, lane.epoch
|
|
2476
|
+
)
|
|
2477
|
+
SELECT lane::int AS lane, epoch::text AS epoch FROM taken ORDER BY lane`;
|
|
528
2478
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
LIMIT 1
|
|
537
|
-
) AS oldest`;
|
|
2479
|
+
};
|
|
2480
|
+
|
|
2481
|
+
// src/statements-purge.ts
|
|
2482
|
+
var PurgeStatements = class {
|
|
2483
|
+
#names;
|
|
2484
|
+
constructor(names) {
|
|
2485
|
+
this.#names = names;
|
|
538
2486
|
}
|
|
539
2487
|
/**
|
|
540
|
-
* Deletes one bounded batch of purgeable deliveries
|
|
541
|
-
* of each deleted
|
|
542
|
-
* `FOR UPDATE SKIP LOCKED` keeps purge from waiting
|
|
2488
|
+
* Deletes one bounded batch of purgeable deliveries (or of the completion
|
|
2489
|
+
* history) and returns the event id of each deleted row. Parameters:
|
|
2490
|
+
* `[cutoff, batchSize]`. `FOR UPDATE SKIP LOCKED` keeps purge from waiting
|
|
2491
|
+
* on, or being waited on by, anything.
|
|
543
2492
|
*/
|
|
544
2493
|
purgeCompletedBatch() {
|
|
545
|
-
return this.#purgeBatch(
|
|
546
|
-
`status = 'completed' AND completed_at < $1::timestamptz`,
|
|
547
|
-
"completed_at"
|
|
548
|
-
);
|
|
2494
|
+
return this.#purgeBatch(this.#names.deliveries, {
|
|
2495
|
+
condition: `status = 'completed' AND completed_at < $1::timestamptz`,
|
|
2496
|
+
orderColumn: "completed_at"
|
|
2497
|
+
});
|
|
2498
|
+
}
|
|
2499
|
+
purgeCompletionHistoryBatch() {
|
|
2500
|
+
return this.#purgeBatch(this.#names.completions, {
|
|
2501
|
+
condition: `completed_at < $1::timestamptz`,
|
|
2502
|
+
orderColumn: "completed_at"
|
|
2503
|
+
});
|
|
549
2504
|
}
|
|
550
2505
|
purgeDeadBatch() {
|
|
551
|
-
return this.#purgeBatch(
|
|
2506
|
+
return this.#purgeBatch(this.#names.deliveries, {
|
|
2507
|
+
condition: `status = 'dead' AND updated_at < $1::timestamptz`,
|
|
2508
|
+
orderColumn: "updated_at"
|
|
2509
|
+
});
|
|
552
2510
|
}
|
|
553
2511
|
/**
|
|
554
2512
|
* Selects and locks one bounded batch of events created before the cutoff
|
|
555
|
-
* that no delivery references
|
|
556
|
-
*
|
|
2513
|
+
* that no delivery references and that never had a delivery complete (those
|
|
2514
|
+
* are measured from their completion, see {@link lockCompletedOrphanEventsBatch}).
|
|
2515
|
+
* Parameters: `[cutoff, batchSize]`. The ids go through
|
|
2516
|
+
* {@link deleteOrphanEventsWithoutCompletion} or {@link deleteExpiredCompletedOrphanEvents}, which re-check under a fresh snapshot.
|
|
557
2517
|
*/
|
|
558
2518
|
get lockOrphanEventsBatch() {
|
|
559
2519
|
return `SELECT event.id FROM ${this.#names.events} AS event
|
|
560
2520
|
WHERE event.created_at < $1::timestamptz
|
|
561
|
-
AND
|
|
2521
|
+
AND ${this.#unreferenced("event")}
|
|
2522
|
+
AND NOT EXISTS (SELECT 1 FROM ${this.#names.eventCompletions} AS completion WHERE completion.event_id = event.id)
|
|
562
2523
|
ORDER BY event.created_at, event.id
|
|
563
2524
|
LIMIT $2::int
|
|
2525
|
+
FOR UPDATE OF event SKIP LOCKED`;
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Like {@link lockOrphanEventsBatch} for events that had a delivery complete:
|
|
2529
|
+
* they go once their last completion is older than the cutoff, however old the
|
|
2530
|
+
* event itself is. Parameters: `[cutoff, batchSize]`.
|
|
2531
|
+
*/
|
|
2532
|
+
get lockCompletedOrphanEventsBatch() {
|
|
2533
|
+
return `SELECT event.id FROM ${this.#names.events} AS event
|
|
2534
|
+
JOIN ${this.#names.eventCompletions} AS completion ON completion.event_id = event.id
|
|
2535
|
+
WHERE completion.completed_at < $1::timestamptz
|
|
2536
|
+
AND ${this.#unreferenced("event")}
|
|
2537
|
+
ORDER BY completion.completed_at, event.id
|
|
2538
|
+
LIMIT $2::int
|
|
564
2539
|
FOR UPDATE OF event SKIP LOCKED`;
|
|
565
2540
|
}
|
|
566
2541
|
/**
|
|
@@ -574,216 +2549,461 @@ WHERE id = ANY (ARRAY(SELECT jsonb_array_elements_text($1::jsonb)))
|
|
|
574
2549
|
FOR UPDATE SKIP LOCKED`;
|
|
575
2550
|
}
|
|
576
2551
|
/**
|
|
577
|
-
* Deletes
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
* Parameters: `[eventIdsJson]`.
|
|
2552
|
+
* Deletes events that no delivery references and whose last completion is older than
|
|
2553
|
+
* `$2`, checked again under the fresh snapshot of this statement: a completion recorded
|
|
2554
|
+
* after the sweep selected the event moves its time and keeps the event. Parameters:
|
|
2555
|
+
* `[eventIdsJson, cutoff]`.
|
|
582
2556
|
*/
|
|
583
|
-
get
|
|
584
|
-
|
|
585
|
-
return countChanged(`DELETE FROM ${events} AS event
|
|
2557
|
+
get deleteExpiredCompletedOrphanEvents() {
|
|
2558
|
+
return countChanged(`DELETE FROM ${this.#names.events} AS event
|
|
586
2559
|
WHERE event.id = ANY (ARRAY(SELECT jsonb_array_elements_text($1::jsonb)))
|
|
587
|
-
AND
|
|
2560
|
+
AND ${this.#unreferenced("event")}
|
|
2561
|
+
AND EXISTS (
|
|
2562
|
+
SELECT 1 FROM ${this.#names.eventCompletions} AS completion
|
|
2563
|
+
WHERE completion.event_id = event.id AND completion.completed_at < $2::timestamptz
|
|
2564
|
+
)`);
|
|
588
2565
|
}
|
|
589
|
-
|
|
590
|
-
|
|
2566
|
+
/**
|
|
2567
|
+
* Deletes locked events that no delivery references and that never had a delivery complete.
|
|
2568
|
+
* It must run as a statement of its own after {@link lockEvents}: it then takes a fresh
|
|
2569
|
+
* snapshot that includes every delivery committed before the lock was granted, so an event
|
|
2570
|
+
* that just gained a delivery is never cascaded away. Keeps events that had a delivery complete:
|
|
2571
|
+
* deleting dead letters must not shorten the retention of completed work.
|
|
2572
|
+
*/
|
|
2573
|
+
get deleteOrphanEventsWithoutCompletion() {
|
|
2574
|
+
return countChanged(`DELETE FROM ${this.#names.events} AS event
|
|
2575
|
+
WHERE event.id = ANY (ARRAY(SELECT jsonb_array_elements_text($1::jsonb)))
|
|
2576
|
+
AND ${this.#unreferenced("event")}
|
|
2577
|
+
AND NOT EXISTS (SELECT 1 FROM ${this.#names.eventCompletions} AS completion WHERE completion.event_id = event.id)`);
|
|
2578
|
+
}
|
|
2579
|
+
/** True when no delivery and no completion history record references the event. */
|
|
2580
|
+
#unreferenced(eventAlias) {
|
|
2581
|
+
const history = this.#names.completionHistory ? ` AND NOT EXISTS (SELECT 1 FROM ${this.#names.completions} AS done WHERE done.event_id = ${eventAlias}.id)` : "";
|
|
2582
|
+
return `NOT EXISTS (SELECT 1 FROM ${this.#names.deliveries} AS delivery WHERE delivery.event_id = ${eventAlias}.id)${history}`;
|
|
2583
|
+
}
|
|
2584
|
+
#purgeBatch(table, { condition, orderColumn }) {
|
|
591
2585
|
return `WITH batch AS MATERIALIZED (
|
|
592
|
-
SELECT id FROM ${
|
|
2586
|
+
SELECT id FROM ${table}
|
|
593
2587
|
WHERE ${condition}
|
|
594
2588
|
ORDER BY ${orderColumn}, id
|
|
595
2589
|
LIMIT $2::int
|
|
596
2590
|
FOR UPDATE SKIP LOCKED
|
|
597
2591
|
), deleted AS (
|
|
598
|
-
DELETE FROM ${
|
|
2592
|
+
DELETE FROM ${table} AS delivery USING batch WHERE delivery.id = batch.id RETURNING delivery.event_id
|
|
599
2593
|
)
|
|
600
2594
|
SELECT event_id FROM deleted`;
|
|
601
2595
|
}
|
|
602
2596
|
};
|
|
603
2597
|
|
|
2598
|
+
// src/statements-queries.ts
|
|
2599
|
+
var DEFAULT_LIST_LIMIT = 100;
|
|
2600
|
+
function bindListFilters(query) {
|
|
2601
|
+
const parameters = [];
|
|
2602
|
+
const bind = (value) => {
|
|
2603
|
+
parameters.push(value);
|
|
2604
|
+
return `$${parameters.length}::${typeof value === "number" ? "int" : "text"}`;
|
|
2605
|
+
};
|
|
2606
|
+
const filters = {
|
|
2607
|
+
handler: query.handler === void 0 ? void 0 : bind(query.handler),
|
|
2608
|
+
eventType: query.eventType === void 0 ? void 0 : bind(query.eventType),
|
|
2609
|
+
eventId: query.eventId === void 0 ? void 0 : bind(query.eventId),
|
|
2610
|
+
status: query.status === void 0 ? void 0 : bind(query.status)
|
|
2611
|
+
};
|
|
2612
|
+
const limit = bind(query.limit ?? DEFAULT_LIST_LIMIT);
|
|
2613
|
+
return { parameters, filters, limit };
|
|
2614
|
+
}
|
|
2615
|
+
function listConditions(filters, alias) {
|
|
2616
|
+
return [
|
|
2617
|
+
...filters.handler === void 0 ? [] : [`${alias}.handler = ${filters.handler}`],
|
|
2618
|
+
...filters.eventType === void 0 ? [] : [`event.type = ${filters.eventType}`],
|
|
2619
|
+
...filters.eventId === void 0 ? [] : [`${alias}.event_id = ${filters.eventId}`]
|
|
2620
|
+
];
|
|
2621
|
+
}
|
|
2622
|
+
var QueryStatements = class {
|
|
2623
|
+
#names;
|
|
2624
|
+
constructor(names) {
|
|
2625
|
+
this.#names = names;
|
|
2626
|
+
}
|
|
2627
|
+
/** Parameters: `[deliveryIdsJson, now]`. The dead rows are locked in id order first. */
|
|
2628
|
+
get requeue() {
|
|
2629
|
+
const deliveries = this.#names.deliveries;
|
|
2630
|
+
return countChanged(`WITH dead AS MATERIALIZED (
|
|
2631
|
+
SELECT delivery.id FROM ${deliveries} AS delivery
|
|
2632
|
+
WHERE delivery.id = ANY (ARRAY(SELECT jsonb_array_elements_text($1::jsonb))) AND delivery.status = 'dead'
|
|
2633
|
+
ORDER BY delivery.id
|
|
2634
|
+
FOR NO KEY UPDATE
|
|
2635
|
+
)
|
|
2636
|
+
UPDATE ${deliveries} AS delivery
|
|
2637
|
+
SET status = 'pending', attempts = 0, available_at = $2::timestamptz, updated_at = $2::timestamptz
|
|
2638
|
+
FROM dead
|
|
2639
|
+
WHERE delivery.id = dead.id AND delivery.status = 'dead'`);
|
|
2640
|
+
}
|
|
2641
|
+
/**
|
|
2642
|
+
* Builds the filtered, creation-ordered listing. A delivery is `processing`
|
|
2643
|
+
* while its row exists as `pending` and its lane lists its id; its attempt
|
|
2644
|
+
* count, lease owner and lease expiry then come from the claim and the lane.
|
|
2645
|
+
* Records of the completion history (when kept) are listed as `completed`.
|
|
2646
|
+
*/
|
|
2647
|
+
listDeliveries(query) {
|
|
2648
|
+
const { parameters, filters, limit } = bindListFilters(query);
|
|
2649
|
+
const current = this.#currentDeliveries(filters);
|
|
2650
|
+
const includeHistory = this.#names.completionHistory && (query.status === void 0 || query.status === "completed");
|
|
2651
|
+
if (!includeHistory) {
|
|
2652
|
+
return {
|
|
2653
|
+
sql: `${current}
|
|
2654
|
+
ORDER BY delivery.created_at, delivery.id
|
|
2655
|
+
LIMIT ${limit}`,
|
|
2656
|
+
parameters
|
|
2657
|
+
};
|
|
2658
|
+
}
|
|
2659
|
+
const history = this.#completionHistory(filters);
|
|
2660
|
+
return {
|
|
2661
|
+
sql: `SELECT * FROM (
|
|
2662
|
+
${current}
|
|
2663
|
+
UNION ALL
|
|
2664
|
+
${history}
|
|
2665
|
+
) AS listed
|
|
2666
|
+
ORDER BY created_at, delivery_id
|
|
2667
|
+
LIMIT ${limit}`,
|
|
2668
|
+
parameters
|
|
2669
|
+
};
|
|
2670
|
+
}
|
|
2671
|
+
/** The deliveries that still exist, with a running one reported as `processing`. */
|
|
2672
|
+
#currentDeliveries(filters) {
|
|
2673
|
+
const inFlight = `(delivery.status = 'pending' AND delivery.id = ANY (lane.in_flight))`;
|
|
2674
|
+
const whereCurrent = [
|
|
2675
|
+
...listConditions(filters, "delivery"),
|
|
2676
|
+
...filters.status === void 0 ? [] : [
|
|
2677
|
+
`(CASE WHEN ${inFlight} THEN 'processing' ELSE delivery.status END) = ${filters.status}`
|
|
2678
|
+
]
|
|
2679
|
+
];
|
|
2680
|
+
return `SELECT delivery.id AS delivery_id, delivery.handler,
|
|
2681
|
+
CASE WHEN ${inFlight} THEN 'processing' ELSE delivery.status END AS status,
|
|
2682
|
+
(delivery.attempts + CASE WHEN ${inFlight} THEN 1 ELSE 0 END)::int AS attempts,
|
|
2683
|
+
${isoText("delivery.available_at")} AS available_at,
|
|
2684
|
+
CASE WHEN ${inFlight} THEN lane.lease_owner END AS lease_owner,
|
|
2685
|
+
CASE WHEN ${inFlight} THEN ${isoText("lane.lease_expires_at")} END AS lease_expires_at,
|
|
2686
|
+
delivery.last_error::text AS last_error,
|
|
2687
|
+
${isoText("delivery.created_at")} AS created_at,
|
|
2688
|
+
${isoText("delivery.updated_at")} AS updated_at,
|
|
2689
|
+
${isoText("delivery.completed_at")} AS completed_at,
|
|
2690
|
+
${eventColumns("event")}
|
|
2691
|
+
FROM ${this.#names.deliveries} AS delivery
|
|
2692
|
+
JOIN ${this.#names.lanes} AS lane ON lane.lane = delivery.lane
|
|
2693
|
+
JOIN ${this.#names.events} AS event ON event.id = delivery.event_id
|
|
2694
|
+
${whereCurrent.length === 0 ? "" : `WHERE ${whereCurrent.join(" AND ")}`}`;
|
|
2695
|
+
}
|
|
2696
|
+
/** The completion history records, listed as `completed`. */
|
|
2697
|
+
#completionHistory(filters) {
|
|
2698
|
+
const common = (alias) => listConditions(filters, alias);
|
|
2699
|
+
return `SELECT done.id AS delivery_id, done.handler, 'completed' AS status, done.attempts,
|
|
2700
|
+
${isoText("done.available_at")} AS available_at,
|
|
2701
|
+
NULL AS lease_owner,
|
|
2702
|
+
NULL AS lease_expires_at,
|
|
2703
|
+
NULL AS last_error,
|
|
2704
|
+
${isoText("done.created_at")} AS created_at,
|
|
2705
|
+
${isoText("done.completed_at")} AS updated_at,
|
|
2706
|
+
${isoText("done.completed_at")} AS completed_at,
|
|
2707
|
+
${eventColumns("event")}
|
|
2708
|
+
FROM ${this.#names.completions} AS done
|
|
2709
|
+
JOIN ${this.#names.events} AS event ON event.id = done.event_id
|
|
2710
|
+
${common("done").length === 0 ? "" : `WHERE ${common("done").join(" AND ")}`}`;
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
* Counts of rows by stored status (`pending` includes running deliveries;
|
|
2714
|
+
* `completed` rows exist only after an upgrade from 0.1.0). Each count is
|
|
2715
|
+
* answered from a partial index.
|
|
2716
|
+
*/
|
|
2717
|
+
get countByStatus() {
|
|
2718
|
+
const deliveries = this.#names.deliveries;
|
|
2719
|
+
return ["pending", "dead", "completed"].map(
|
|
2720
|
+
(status) => `SELECT '${status}' AS status, count(*)::text AS count FROM ${deliveries} WHERE status = '${status}'`
|
|
2721
|
+
).join("\nUNION ALL\n");
|
|
2722
|
+
}
|
|
2723
|
+
/** Deliveries that are running: listed by a lane and still pending as rows. */
|
|
2724
|
+
get countRunning() {
|
|
2725
|
+
return `SELECT count(*)::text AS count
|
|
2726
|
+
FROM ${this.#names.lanes} AS lane
|
|
2727
|
+
CROSS JOIN LATERAL unnest(lane.in_flight) AS flying (id)
|
|
2728
|
+
JOIN ${this.#names.deliveries} AS delivery ON delivery.id = flying.id AND delivery.status = 'pending'`;
|
|
2729
|
+
}
|
|
2730
|
+
get countCompletionHistory() {
|
|
2731
|
+
return `SELECT count(*)::text AS count FROM ${this.#names.completions}`;
|
|
2732
|
+
}
|
|
2733
|
+
/**
|
|
2734
|
+
* Oldest instant at which a delivery became claimable: `available_at` of a
|
|
2735
|
+
* pending delivery that nobody runs, or the lease expiry of a lane that runs
|
|
2736
|
+
* deliveries. Parameters: `[now]`.
|
|
2737
|
+
*/
|
|
2738
|
+
get oldestDue() {
|
|
2739
|
+
const deliveries = this.#names.deliveries;
|
|
2740
|
+
const lanes = this.#names.lanes;
|
|
2741
|
+
return `SELECT ${isoText("min(due.due_at)")} AS oldest_due_at FROM (
|
|
2742
|
+
SELECT waiting.available_at AS due_at
|
|
2743
|
+
FROM ${lanes} AS lane
|
|
2744
|
+
CROSS JOIN LATERAL (
|
|
2745
|
+
SELECT delivery.available_at FROM ${deliveries} AS delivery
|
|
2746
|
+
WHERE delivery.lane = lane.lane AND delivery.status = 'pending'
|
|
2747
|
+
AND delivery.available_at <= $1::timestamptz AND delivery.id <> ALL (lane.in_flight)
|
|
2748
|
+
ORDER BY delivery.available_at LIMIT 1
|
|
2749
|
+
) AS waiting
|
|
2750
|
+
UNION ALL
|
|
2751
|
+
SELECT lane.lease_expires_at AS due_at
|
|
2752
|
+
FROM ${lanes} AS lane
|
|
2753
|
+
WHERE lane.lease_expires_at <= $1::timestamptz AND NOT ${noRunningDelivery(this.#names, "lane")}
|
|
2754
|
+
) AS due`;
|
|
2755
|
+
}
|
|
2756
|
+
};
|
|
2757
|
+
|
|
2758
|
+
// src/statements-setup.ts
|
|
2759
|
+
var SetupStatements = class {
|
|
2760
|
+
#names;
|
|
2761
|
+
constructor(names) {
|
|
2762
|
+
this.#names = names;
|
|
2763
|
+
}
|
|
2764
|
+
/**
|
|
2765
|
+
* Lock advisory key text for {@link setupLock}. It is derived from the schema
|
|
2766
|
+
* name only, so stores with different table prefixes in one schema also
|
|
2767
|
+
* serialize their `CREATE SCHEMA IF NOT EXISTS`, which is not race-free.
|
|
2768
|
+
*/
|
|
2769
|
+
get setupLockKey() {
|
|
2770
|
+
return `relaybus:${this.#names.schema}`;
|
|
2771
|
+
}
|
|
2772
|
+
/**
|
|
2773
|
+
* Whether the lane table carries `marker` as its comment, that is whether the schema statements
|
|
2774
|
+
* that produce that marker already ran. Takes no table lock. Parameters: `[marker]`.
|
|
2775
|
+
*/
|
|
2776
|
+
get schemaCurrent() {
|
|
2777
|
+
return `SELECT EXISTS (
|
|
2778
|
+
SELECT 1 FROM pg_catalog.pg_class AS c
|
|
2779
|
+
JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
|
|
2780
|
+
JOIN pg_catalog.pg_description AS d
|
|
2781
|
+
ON d.objoid = c.oid AND d.classoid = 'pg_catalog.pg_class'::regclass AND d.objsubid = 0
|
|
2782
|
+
WHERE n.nspname = '${this.#names.schema}' AND c.relname = '${this.#names.lanesTableName}' AND d.description = $1::text
|
|
2783
|
+
) AS current`;
|
|
2784
|
+
}
|
|
2785
|
+
get setupLock() {
|
|
2786
|
+
return "SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))";
|
|
2787
|
+
}
|
|
2788
|
+
};
|
|
2789
|
+
|
|
2790
|
+
// src/statements.ts
|
|
2791
|
+
var PostgresStatements = class {
|
|
2792
|
+
setup;
|
|
2793
|
+
append;
|
|
2794
|
+
lanes;
|
|
2795
|
+
claim;
|
|
2796
|
+
completion;
|
|
2797
|
+
queries;
|
|
2798
|
+
purge;
|
|
2799
|
+
constructor(names) {
|
|
2800
|
+
this.setup = new SetupStatements(names);
|
|
2801
|
+
this.append = new AppendStatements(names);
|
|
2802
|
+
this.lanes = new LaneStatements(names);
|
|
2803
|
+
this.claim = new ClaimStatements(names);
|
|
2804
|
+
this.completion = new CompletionStatements(names);
|
|
2805
|
+
this.queries = new QueryStatements(names);
|
|
2806
|
+
this.purge = new PurgeStatements(names);
|
|
2807
|
+
}
|
|
2808
|
+
};
|
|
2809
|
+
|
|
604
2810
|
// src/postgres-event-store.ts
|
|
2811
|
+
var WAKE_UPS = ["notify", "poll"];
|
|
605
2812
|
var DEFAULT_PURGE_BATCH_SIZE = 1e3;
|
|
606
2813
|
var PostgresEventStore = class {
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
#
|
|
610
|
-
#
|
|
611
|
-
#
|
|
612
|
-
#
|
|
2814
|
+
#state;
|
|
2815
|
+
#appender;
|
|
2816
|
+
#claims;
|
|
2817
|
+
#completion;
|
|
2818
|
+
#maintenance;
|
|
2819
|
+
#owned = new OwnedConnections();
|
|
613
2820
|
constructor(options) {
|
|
614
|
-
const
|
|
615
|
-
(source) => source !== void 0
|
|
616
|
-
);
|
|
617
|
-
if (sources.length !== 1) {
|
|
618
|
-
throw new ConfigurationError(
|
|
619
|
-
"PostgresEventStore needs exactly one of pool, connectionString or executor"
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
const purgeBatchSize = options.purgeBatchSize ?? DEFAULT_PURGE_BATCH_SIZE;
|
|
623
|
-
if (!Number.isInteger(purgeBatchSize) || purgeBatchSize < 1) {
|
|
624
|
-
throw new ConfigurationError(
|
|
625
|
-
`purgeBatchSize must be a positive integer, received ${String(purgeBatchSize)}`
|
|
626
|
-
);
|
|
627
|
-
}
|
|
2821
|
+
const { purgeBatchSize, wakeUp, queryOptions } = resolveStoreSettings(options);
|
|
628
2822
|
const names = resolvePostgresNames(options);
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
this.#
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
2823
|
+
const statements = new PostgresStatements(names);
|
|
2824
|
+
const schemaStatements = createPostgresSchemaStatements(options);
|
|
2825
|
+
const logger = options.logger ?? console;
|
|
2826
|
+
const { executor, pool, ownedPool } = connect(options, { logger, owned: this.#owned });
|
|
2827
|
+
const state = {
|
|
2828
|
+
executor,
|
|
2829
|
+
statements,
|
|
2830
|
+
schemaStatements,
|
|
2831
|
+
setupLockKey: statements.setup.setupLockKey,
|
|
2832
|
+
schemaMarker: postgresSchemaMarker(schemaStatements.slice(0, -1)),
|
|
2833
|
+
purgeBatchSize,
|
|
2834
|
+
completionHistory: names.completionHistory,
|
|
2835
|
+
logger,
|
|
2836
|
+
queryOptions,
|
|
2837
|
+
notifies: wakeUp === "notify",
|
|
2838
|
+
query: ({ sql, parameters, transaction }) => executor.query(sql, parameters, transaction, queryOptions),
|
|
2839
|
+
handedOut: /* @__PURE__ */ new Map(),
|
|
2840
|
+
unreconciled: /* @__PURE__ */ new Set(),
|
|
2841
|
+
generations: /* @__PURE__ */ new Map(),
|
|
2842
|
+
ownerQueues: /* @__PURE__ */ new Map(),
|
|
2843
|
+
drainingOwners: /* @__PURE__ */ new Set(),
|
|
2844
|
+
ownedPool,
|
|
2845
|
+
pool,
|
|
2846
|
+
notificationChannel: names.notificationChannel,
|
|
2847
|
+
lastNotificationAt: Number.NEGATIVE_INFINITY,
|
|
2848
|
+
trailingNotification: void 0,
|
|
2849
|
+
closed: false,
|
|
2850
|
+
ownTransactions: /* @__PURE__ */ new Map()
|
|
2851
|
+
};
|
|
2852
|
+
this.#state = state;
|
|
2853
|
+
this.#appender = new PostgresAppend(state);
|
|
2854
|
+
this.#claims = new PostgresClaims(state);
|
|
2855
|
+
this.#completion = new PostgresCompletion(state);
|
|
2856
|
+
this.#maintenance = new PostgresMaintenance(state);
|
|
648
2857
|
}
|
|
649
2858
|
/**
|
|
650
2859
|
* Creates the schema, tables and indexes if they do not exist. Safe to call
|
|
651
2860
|
* from many processes at once: a transaction-scoped advisory lock serializes
|
|
652
2861
|
* the DDL, so racing `CREATE TABLE IF NOT EXISTS` statements cannot collide.
|
|
2862
|
+
* When the lane table already carries the marker of the current statements
|
|
2863
|
+
* ({@link postgresSchemaMarker}) nothing runs and no table is locked, so instances
|
|
2864
|
+
* that start next to running dispatchers cannot deadlock with their claims.
|
|
653
2865
|
* Skip it (`setupStore: false`) when a migration tool applies
|
|
654
2866
|
* `createPostgresSchemaSql()` instead.
|
|
655
2867
|
*/
|
|
656
2868
|
async setup() {
|
|
657
|
-
await this.#
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
2869
|
+
if (await this.#schemaIsCurrent()) {
|
|
2870
|
+
return;
|
|
2871
|
+
}
|
|
2872
|
+
await this.#state.executor.transaction(async (transaction) => {
|
|
2873
|
+
await this.#state.executor.query(
|
|
2874
|
+
this.#state.statements.setup.setupLock,
|
|
2875
|
+
[this.#state.setupLockKey],
|
|
2876
|
+
transaction
|
|
2877
|
+
);
|
|
2878
|
+
if (await this.#schemaIsCurrent(transaction)) {
|
|
2879
|
+
return;
|
|
2880
|
+
}
|
|
2881
|
+
for (const statement of this.#state.schemaStatements) {
|
|
2882
|
+
await this.#state.executor.query(statement, [], transaction);
|
|
661
2883
|
}
|
|
662
2884
|
});
|
|
663
2885
|
}
|
|
664
|
-
transaction
|
|
665
|
-
|
|
2886
|
+
async #schemaIsCurrent(transaction) {
|
|
2887
|
+
const rows = await this.#state.executor.query(
|
|
2888
|
+
this.#state.statements.setup.schemaCurrent,
|
|
2889
|
+
[this.#state.schemaMarker],
|
|
2890
|
+
transaction
|
|
2891
|
+
);
|
|
2892
|
+
return rows[0]["current"] === true;
|
|
2893
|
+
}
|
|
2894
|
+
/**
|
|
2895
|
+
* Runs `work` in a transaction of the executor. With `options.timeoutMs` (from `bus.transaction`)
|
|
2896
|
+
* the server holds the transaction to the budget: `statement_timeout`, and for executors with
|
|
2897
|
+
* `sessionTimeouts` (`pg`) also `idle_in_transaction_session_timeout` and, on PostgreSQL 17+,
|
|
2898
|
+
* `transaction_timeout`, all scoped to the transaction. An executor with `transactionWithBudget`
|
|
2899
|
+
* (`PgExecutor`) sets them with `BEGIN`, so the budget costs no round trip; on a pool the store
|
|
2900
|
+
* created they are kept per connection and sent only when the connection does not have them yet.
|
|
2901
|
+
*/
|
|
2902
|
+
async transaction(work, options) {
|
|
2903
|
+
const own = { wake: false };
|
|
2904
|
+
const tracked = async (transaction) => {
|
|
2905
|
+
this.#state.ownTransactions.set(transaction, own);
|
|
2906
|
+
try {
|
|
2907
|
+
return await work(transaction);
|
|
2908
|
+
} finally {
|
|
2909
|
+
this.#state.ownTransactions.delete(transaction);
|
|
2910
|
+
}
|
|
2911
|
+
};
|
|
2912
|
+
const result = await (options === void 0 ? this.#state.executor.transaction(tracked) : inBudgetedTransaction(this.#state, { work: tracked, timeoutMs: options.timeoutMs }));
|
|
2913
|
+
if (own.wake) {
|
|
2914
|
+
this.#appender.wakeAfterCommit();
|
|
2915
|
+
}
|
|
2916
|
+
return result;
|
|
666
2917
|
}
|
|
667
2918
|
/**
|
|
668
|
-
* Writes the entries in `transaction
|
|
669
|
-
*
|
|
670
|
-
* deadlock inside one call), then
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
*
|
|
2919
|
+
* Writes the entries in `transaction`, normally with one statement
|
|
2920
|
+
* (`AppendStatements.insertFreshEvents`): keyed events reserve their sequence numbers (locking
|
|
2921
|
+
* the key rows in sorted order, so publishers cannot deadlock inside one call), then the new events and
|
|
2922
|
+
* their deliveries are inserted, ordered by primary key, and one notification wakes the dispatchers
|
|
2923
|
+
* when a delivery is due now. Events that already existed get their missing handlers' deliveries in
|
|
2924
|
+
* two more statements. Two transactions that append keys in opposite order across several calls can
|
|
2925
|
+
* still deadlock (40P01); the database rolls one back and the caller retries.
|
|
674
2926
|
* A duplicate event id still consumes a sequence number; gaps are harmless
|
|
675
2927
|
* because only the order of sequences matters.
|
|
676
2928
|
*/
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
const sequences = await this.#reserveSequences(plan.events, transaction);
|
|
681
|
-
const insertedEventIds = new Set(
|
|
682
|
-
plan.events.length === 0 ? [] : (await this.#executor.query(
|
|
683
|
-
this.#statements.insertEvents,
|
|
684
|
-
[JSON.stringify(toEventRows(plan.events, sequences)), createdAt],
|
|
685
|
-
transaction
|
|
686
|
-
)).map((row) => row["id"])
|
|
687
|
-
);
|
|
688
|
-
const insertedDeliveries = plan.deliveries.length === 0 ? [] : await this.#executor.query(
|
|
689
|
-
this.#statements.insertDeliveries,
|
|
690
|
-
[JSON.stringify(plan.deliveries), createdAt],
|
|
691
|
-
transaction
|
|
692
|
-
);
|
|
693
|
-
const appendedEventIds = [];
|
|
694
|
-
const duplicateEventIds = [];
|
|
695
|
-
for (const { eventId, repeated } of plan.outcomes) {
|
|
696
|
-
(!repeated && insertedEventIds.has(eventId) ? appendedEventIds : duplicateEventIds).push(
|
|
697
|
-
eventId
|
|
698
|
-
);
|
|
699
|
-
}
|
|
700
|
-
return {
|
|
701
|
-
appendedEventIds,
|
|
702
|
-
duplicateEventIds,
|
|
703
|
-
appendedDeliveryCount: insertedDeliveries.length
|
|
704
|
-
};
|
|
2929
|
+
// eslint-disable-next-line relaybus/max-two-params -- EventStore port signature
|
|
2930
|
+
append(transaction, entries, now) {
|
|
2931
|
+
return this.#appender.append(transaction, { entries, now });
|
|
705
2932
|
}
|
|
706
2933
|
/**
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
2934
|
+
* Claims up to `limit` due deliveries, normally with one statement
|
|
2935
|
+
* (`LaneStatements.claimLanes`): from the lanes this owner already holds, then from free lanes
|
|
2936
|
+
* it takes in the same statement, recording the batch in the lane rows. A claim that would have to take
|
|
2937
|
+
* over a lane whose previous holder still runs deliveries under an expired lease continues in a
|
|
2938
|
+
* transaction that charges their lost attempts first, taking free lanes a few at a time (4, 8, 16, ...);
|
|
2939
|
+
* so does a claim whose statement kept seeing a lane it holds change under it, and one that came up
|
|
2940
|
+
* short with more free lanes left than it looked at. The step that fails after
|
|
2941
|
+
* the first one has recorded deliveries returns those instead of failing: a recorded delivery nobody
|
|
2942
|
+
* received would be held for a whole lease. See `ClaimStatements.claim` for the rules a delivery
|
|
2943
|
+
* must satisfy.
|
|
712
2944
|
*/
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
return [];
|
|
716
|
-
}
|
|
717
|
-
const parameters = (limit) => [
|
|
718
|
-
request.now.toISOString(),
|
|
719
|
-
JSON.stringify(request.handlers),
|
|
720
|
-
limit,
|
|
721
|
-
request.leaseOwner,
|
|
722
|
-
request.leaseExpiresAt.toISOString()
|
|
723
|
-
];
|
|
724
|
-
const fastRows = await this.#executor.query(this.#statements.claim, parameters(request.limit));
|
|
725
|
-
const leased = fastRows.filter((row) => row["delivery_id"] !== null);
|
|
726
|
-
if (leased.length < request.limit && fastRows[0]["window_saturated"] === true) {
|
|
727
|
-
leased.push(
|
|
728
|
-
...(await this.#executor.query(
|
|
729
|
-
this.#statements.claimBehindBlockedKeys,
|
|
730
|
-
parameters(request.limit - leased.length)
|
|
731
|
-
)).filter((row) => row["delivery_id"] !== null)
|
|
732
|
-
);
|
|
733
|
-
}
|
|
734
|
-
return leased.map((row) => mapClaimedDelivery(row, request.leaseExpiresAt));
|
|
2945
|
+
claim(request) {
|
|
2946
|
+
return this.#claims.claim(request);
|
|
735
2947
|
}
|
|
736
|
-
extendLease(lease, leaseExpiresAt
|
|
737
|
-
return this.#
|
|
738
|
-
lease.deliveryId,
|
|
739
|
-
lease.leaseToken,
|
|
740
|
-
leaseExpiresAt.toISOString(),
|
|
741
|
-
now.toISOString()
|
|
742
|
-
]);
|
|
2948
|
+
extendLease(lease, leaseExpiresAt) {
|
|
2949
|
+
return this.#completion.extendLease(lease, leaseExpiresAt);
|
|
743
2950
|
}
|
|
2951
|
+
// eslint-disable-next-line relaybus/max-two-params -- EventStore port signature
|
|
744
2952
|
complete(lease, now, transaction) {
|
|
745
|
-
return this.#
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
);
|
|
2953
|
+
return this.#completion.complete(lease, { now, transaction });
|
|
2954
|
+
}
|
|
2955
|
+
completeMany(leases, now) {
|
|
2956
|
+
return this.#completion.completeMany(leases, now);
|
|
750
2957
|
}
|
|
2958
|
+
// eslint-disable-next-line relaybus/max-two-params -- EventStore port signature
|
|
751
2959
|
reschedule(lease, availableAt, error, now) {
|
|
752
|
-
return this.#
|
|
753
|
-
lease.deliveryId,
|
|
754
|
-
lease.leaseToken,
|
|
755
|
-
availableAt.toISOString(),
|
|
756
|
-
JSON.stringify(error),
|
|
757
|
-
now.toISOString()
|
|
758
|
-
]);
|
|
2960
|
+
return this.#completion.reschedule(lease, { availableAt, error, now });
|
|
759
2961
|
}
|
|
2962
|
+
// eslint-disable-next-line relaybus/max-two-params -- EventStore port signature
|
|
760
2963
|
bury(lease, error, now) {
|
|
761
|
-
return this.#
|
|
762
|
-
lease.deliveryId,
|
|
763
|
-
lease.leaseToken,
|
|
764
|
-
JSON.stringify(error),
|
|
765
|
-
now.toISOString()
|
|
766
|
-
]);
|
|
2964
|
+
return this.#completion.bury(lease, { error, now });
|
|
767
2965
|
}
|
|
768
2966
|
/** Returns a leased delivery to pending, due now, without counting the attempt. */
|
|
769
2967
|
release(lease, now) {
|
|
770
|
-
return this.#
|
|
771
|
-
lease.deliveryId,
|
|
772
|
-
lease.leaseToken,
|
|
773
|
-
now.toISOString()
|
|
774
|
-
]);
|
|
2968
|
+
return this.#completion.release(lease, now);
|
|
775
2969
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
2970
|
+
/**
|
|
2971
|
+
* Calls `listener` soon after a transaction that published deliveries due at once commits, in any
|
|
2972
|
+
* process: `append` sends one `pg_notify` per call (merged per transaction, none for delayed
|
|
2973
|
+
* deliveries), and this listens with `LISTEN` on a connection of its own, opened with the settings of
|
|
2974
|
+
* the store's pool (one connection more than the pool's `max`). A lost connection is replaced after a
|
|
2975
|
+
* second. Resolves with `undefined` for a store built on an `executor`, which has no connection of its
|
|
2976
|
+
* own to listen on, and with `wakeUp: 'poll'`: polling then finds new deliveries (and the bus still
|
|
2977
|
+
* wakes its own dispatcher).
|
|
2978
|
+
*/
|
|
2979
|
+
async watchNewDeliveries(listener) {
|
|
2980
|
+
if (this.#state.pool === void 0 || !this.#state.notifies) {
|
|
2981
|
+
return void 0;
|
|
2982
|
+
}
|
|
2983
|
+
const options = this.#state.pool.options;
|
|
2984
|
+
const watcher = new NotificationListener(
|
|
2985
|
+
async () => {
|
|
2986
|
+
const client = new Client(options);
|
|
2987
|
+
this.#owned.track(client);
|
|
2988
|
+
await client.connect();
|
|
2989
|
+
return client;
|
|
2990
|
+
},
|
|
2991
|
+
this.#state.notificationChannel,
|
|
2992
|
+
listener,
|
|
2993
|
+
this.#state.logger
|
|
2994
|
+
);
|
|
2995
|
+
await watcher.start();
|
|
2996
|
+
return () => watcher.close();
|
|
780
2997
|
}
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
return
|
|
2998
|
+
/** Frees the lanes `leaseOwner` holds that run no delivery, so other workers need not wait for the lease to expire. */
|
|
2999
|
+
relinquish(leaseOwner) {
|
|
3000
|
+
return this.#maintenance.relinquish(leaseOwner);
|
|
3001
|
+
}
|
|
3002
|
+
listDeliveries(query) {
|
|
3003
|
+
return this.#maintenance.listDeliveries(query);
|
|
3004
|
+
}
|
|
3005
|
+
requeue(deliveryIds, now) {
|
|
3006
|
+
return this.#maintenance.requeue(deliveryIds, now);
|
|
787
3007
|
}
|
|
788
3008
|
/**
|
|
789
3009
|
* Deletes purgeable deliveries in batches of `purgeBatchSize`, one short
|
|
@@ -794,176 +3014,91 @@ var PostgresEventStore = class {
|
|
|
794
3014
|
* was subscribed. Events that a concurrent publish is attaching a delivery
|
|
795
3015
|
* to are skipped and kept.
|
|
796
3016
|
*/
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
const batches = [
|
|
800
|
-
[this.#statements.purgeCompletedBatch(), request.completedBefore],
|
|
801
|
-
[this.#statements.purgeDeadBatch(), request.deadBefore]
|
|
802
|
-
];
|
|
803
|
-
for (const [sql, cutoff] of batches) {
|
|
804
|
-
if (cutoff === void 0) {
|
|
805
|
-
continue;
|
|
806
|
-
}
|
|
807
|
-
let deletedInBatch = this.#purgeBatchSize;
|
|
808
|
-
while (deletedInBatch === this.#purgeBatchSize) {
|
|
809
|
-
const batch = await this.#purgeBatch(sql, cutoff);
|
|
810
|
-
deletedInBatch = batch.deletedDeliveries;
|
|
811
|
-
total.deletedDeliveries += batch.deletedDeliveries;
|
|
812
|
-
total.deletedEvents += batch.deletedEvents;
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
const orphanCutoff = [request.completedBefore, request.deadBefore].filter((cutoff) => cutoff !== void 0).sort((first, second) => first.getTime() - second.getTime())[0];
|
|
816
|
-
if (orphanCutoff !== void 0) {
|
|
817
|
-
total.deletedEvents += await this.#purgeOrphanEvents(orphanCutoff);
|
|
818
|
-
}
|
|
819
|
-
return total;
|
|
3017
|
+
purge(request) {
|
|
3018
|
+
return this.#maintenance.purge(request);
|
|
820
3019
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
3020
|
+
statistics(now) {
|
|
3021
|
+
return this.#maintenance.statistics(now);
|
|
3022
|
+
}
|
|
3023
|
+
/**
|
|
3024
|
+
* Ends the pool this store created from `connectionString`. Pools and executors you passed in stay
|
|
3025
|
+
* open. Once `options.signal` aborts (the shutdown deadline passed), the connections the store opened
|
|
3026
|
+
* itself are destroyed: queries still running on them fail at once instead of holding the shutdown.
|
|
3027
|
+
*/
|
|
3028
|
+
async close(options = {}) {
|
|
3029
|
+
this.#state.closed = true;
|
|
3030
|
+
this.#state.trailingNotification?.();
|
|
3031
|
+
this.#state.trailingNotification = void 0;
|
|
3032
|
+
const pool = this.#state.ownedPool;
|
|
3033
|
+
this.#state.ownedPool = void 0;
|
|
3034
|
+
const { signal } = options;
|
|
3035
|
+
const force = () => this.#owned.destroyAll();
|
|
3036
|
+
if (signal?.aborted === true) {
|
|
3037
|
+
force();
|
|
3038
|
+
}
|
|
3039
|
+
signal?.addEventListener("abort", force, { once: true });
|
|
3040
|
+
try {
|
|
3041
|
+
await pool?.end();
|
|
3042
|
+
} finally {
|
|
3043
|
+
signal?.removeEventListener("abort", force);
|
|
830
3044
|
}
|
|
831
|
-
const oldestDue = await this.#executor.query(this.#statements.oldestDue, [now.toISOString()]);
|
|
832
|
-
return { ...counts, oldestDueAt: mapOldestDueAt(oldestDue) };
|
|
833
3045
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
3046
|
+
};
|
|
3047
|
+
function resolveStoreSettings(options) {
|
|
3048
|
+
const sources = [options.pool, options.connectionString, options.executor].filter(
|
|
3049
|
+
(source) => source !== void 0
|
|
3050
|
+
);
|
|
3051
|
+
if (sources.length !== 1) {
|
|
3052
|
+
throw new ConfigurationError(
|
|
3053
|
+
"PostgresEventStore needs exactly one of pool, connectionString or executor"
|
|
3054
|
+
);
|
|
839
3055
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
3056
|
+
const purgeBatchSize = assertPositiveInteger(
|
|
3057
|
+
options.purgeBatchSize ?? DEFAULT_PURGE_BATCH_SIZE,
|
|
3058
|
+
"purgeBatchSize"
|
|
3059
|
+
);
|
|
3060
|
+
const wakeUp = options.wakeUp ?? "notify";
|
|
3061
|
+
if (!WAKE_UPS.includes(wakeUp)) {
|
|
3062
|
+
throw new ConfigurationError(`wakeUp must be 'notify' or 'poll', received ${String(wakeUp)}`);
|
|
843
3063
|
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
counts.set(event.key, (counts.get(event.key) ?? 0) + 1);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
const firstSequences = /* @__PURE__ */ new Map();
|
|
852
|
-
if (counts.size === 0) {
|
|
853
|
-
return firstSequences;
|
|
854
|
-
}
|
|
855
|
-
const requested = [...counts].map(([key, count]) => ({ key, count }));
|
|
856
|
-
const rows = await this.#executor.query(
|
|
857
|
-
this.#statements.reserveSequences,
|
|
858
|
-
[JSON.stringify(requested)],
|
|
859
|
-
transaction
|
|
3064
|
+
const preparedStatements = options.preparedStatements ?? true;
|
|
3065
|
+
if (typeof preparedStatements !== "boolean") {
|
|
3066
|
+
throw new ConfigurationError(
|
|
3067
|
+
`preparedStatements must be a boolean, received ${String(preparedStatements)}`
|
|
860
3068
|
);
|
|
861
|
-
for (const row of rows) {
|
|
862
|
-
const key = row["key"];
|
|
863
|
-
firstSequences.set(key, BigInt(row["sequence"]) - BigInt(counts.get(key)) + 1n);
|
|
864
|
-
}
|
|
865
|
-
return firstSequences;
|
|
866
3069
|
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
);
|
|
877
|
-
const removed = await this.#executor.query(
|
|
878
|
-
this.#statements.deleteOrphanEvents,
|
|
879
|
-
[JSON.stringify(locked.map((row) => row["id"]))],
|
|
880
|
-
transaction
|
|
881
|
-
);
|
|
882
|
-
return readCount(removed[0], "count");
|
|
883
|
-
});
|
|
884
|
-
deletedEvents += removedInBatch;
|
|
885
|
-
}
|
|
886
|
-
return deletedEvents;
|
|
3070
|
+
return {
|
|
3071
|
+
purgeBatchSize,
|
|
3072
|
+
wakeUp,
|
|
3073
|
+
queryOptions: preparedStatements ? { prepare: true } : void 0
|
|
3074
|
+
};
|
|
3075
|
+
}
|
|
3076
|
+
function connect(options, { logger, owned }) {
|
|
3077
|
+
if (options.executor !== void 0) {
|
|
3078
|
+
return { executor: options.executor, pool: void 0, ownedPool: void 0 };
|
|
887
3079
|
}
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
...new Set(deleted.map((row) => row["event_id"]))
|
|
900
|
-
]);
|
|
901
|
-
const locked = await this.#executor.query(
|
|
902
|
-
this.#statements.lockEvents,
|
|
903
|
-
[eventIds],
|
|
904
|
-
transaction
|
|
905
|
-
);
|
|
906
|
-
const removed = await this.#executor.query(
|
|
907
|
-
this.#statements.deleteOrphanEvents,
|
|
908
|
-
[JSON.stringify(locked.map((row) => row["id"]))],
|
|
909
|
-
transaction
|
|
3080
|
+
const pool = options.pool ?? new Pool({
|
|
3081
|
+
...options.poolConfig,
|
|
3082
|
+
connectionString: options.connectionString,
|
|
3083
|
+
Client: owned.clientClass()
|
|
3084
|
+
});
|
|
3085
|
+
let ownedPool;
|
|
3086
|
+
if (options.pool === void 0) {
|
|
3087
|
+
const poolLogger = options.logger ?? console;
|
|
3088
|
+
pool.on("error", (error) => {
|
|
3089
|
+
poolLogger.error(
|
|
3090
|
+
`[PostgresEventStore](connect) Idle connection failed, the pool replaces it | error=${error.message}`
|
|
910
3091
|
);
|
|
911
|
-
return { deletedDeliveries: deleted.length, deletedEvents: readCount(removed[0], "count") };
|
|
912
3092
|
});
|
|
3093
|
+
ownedPool = pool;
|
|
913
3094
|
}
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
const deliveries = [];
|
|
918
|
-
const outcomes = [];
|
|
919
|
-
const seenEventIds = /* @__PURE__ */ new Set();
|
|
920
|
-
const seenDeliveries = /* @__PURE__ */ new Set();
|
|
921
|
-
for (const { event, deliveries: newDeliveries } of entries) {
|
|
922
|
-
const repeated = seenEventIds.has(event.id);
|
|
923
|
-
seenEventIds.add(event.id);
|
|
924
|
-
outcomes.push({ eventId: event.id, repeated });
|
|
925
|
-
if (!repeated) {
|
|
926
|
-
events.push(event);
|
|
927
|
-
}
|
|
928
|
-
for (const delivery of newDeliveries) {
|
|
929
|
-
const identity = `${event.id}\0${delivery.handler}`;
|
|
930
|
-
if (!seenDeliveries.has(identity)) {
|
|
931
|
-
seenDeliveries.add(identity);
|
|
932
|
-
deliveries.push({
|
|
933
|
-
id: delivery.id,
|
|
934
|
-
event_id: event.id,
|
|
935
|
-
handler: delivery.handler,
|
|
936
|
-
available_at: delivery.availableAt.toISOString()
|
|
937
|
-
});
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
return { events, deliveries, outcomes };
|
|
942
|
-
}
|
|
943
|
-
function toEventRows(events, firstSequences) {
|
|
944
|
-
const nextSequences = new Map(firstSequences);
|
|
945
|
-
return events.map((event) => {
|
|
946
|
-
let sequence = null;
|
|
947
|
-
if (event.key !== null) {
|
|
948
|
-
const next = nextSequences.get(event.key);
|
|
949
|
-
nextSequences.set(event.key, next + 1n);
|
|
950
|
-
sequence = next.toString();
|
|
951
|
-
}
|
|
952
|
-
return {
|
|
953
|
-
id: event.id,
|
|
954
|
-
type: event.type,
|
|
955
|
-
source: event.source,
|
|
956
|
-
key: event.key,
|
|
957
|
-
sequence,
|
|
958
|
-
payload: event.payload,
|
|
959
|
-
headers: event.headers,
|
|
960
|
-
correlation_id: event.correlationId,
|
|
961
|
-
causation_id: event.causationId,
|
|
962
|
-
occurred_at: event.occurredAt.toISOString()
|
|
963
|
-
};
|
|
3095
|
+
const executor = new PgExecutor(pool, {
|
|
3096
|
+
logger,
|
|
3097
|
+
sessionBudget: ownedPool !== void 0 && options.preparedStatements !== false
|
|
964
3098
|
});
|
|
3099
|
+
return { executor, pool, ownedPool };
|
|
965
3100
|
}
|
|
966
3101
|
|
|
967
|
-
export { PgExecutor, PostgresEventStore, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
|
|
3102
|
+
export { PgExecutor, PostgresEventStore, ReconcileFailedError, createPostgresSchemaSql, createPostgresSchemaStatements, resolvePostgresNames };
|
|
968
3103
|
//# sourceMappingURL=index.js.map
|
|
969
3104
|
//# sourceMappingURL=index.js.map
|