@nodii/outbox-dispatcher 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -2
- package/dist/cli/migrate-gen.d.ts +3 -0
- package/dist/cli/migrate-gen.d.ts.map +1 -0
- package/dist/cli/migrate-gen.js +97 -0
- package/dist/cli/migrate-gen.js.map +1 -0
- package/dist/define-dispatcher.d.ts +21 -0
- package/dist/define-dispatcher.d.ts.map +1 -0
- package/dist/define-dispatcher.js +65 -0
- package/dist/define-dispatcher.js.map +1 -0
- package/dist/drainer.d.ts +26 -0
- package/dist/drainer.d.ts.map +1 -0
- package/dist/drainer.js +339 -0
- package/dist/drainer.js.map +1 -0
- package/dist/envelope.d.ts +27 -0
- package/dist/envelope.d.ts.map +1 -0
- package/dist/envelope.js +77 -0
- package/dist/envelope.js.map +1 -0
- package/dist/hwm.d.ts +15 -0
- package/dist/hwm.d.ts.map +1 -0
- package/dist/hwm.js +51 -0
- package/dist/hwm.js.map +1 -0
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -1
- package/dist/index.js.map +1 -0
- package/dist/lease.d.ts +27 -0
- package/dist/lease.d.ts.map +1 -0
- package/dist/lease.js +97 -0
- package/dist/lease.js.map +1 -0
- package/dist/migrations/index.d.ts +12 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/migrations/index.js +88 -0
- package/dist/migrations/index.js.map +1 -0
- package/dist/sql-builders.d.ts +20 -0
- package/dist/sql-builders.d.ts.map +1 -0
- package/dist/sql-builders.js +70 -0
- package/dist/sql-builders.js.map +1 -0
- package/dist/start.d.ts +25 -0
- package/dist/start.d.ts.map +1 -0
- package/dist/start.js +74 -0
- package/dist/start.js.map +1 -0
- package/dist/test-doubles/index.d.ts +56 -0
- package/dist/test-doubles/index.d.ts.map +1 -0
- package/dist/test-doubles/index.js +145 -0
- package/dist/test-doubles/index.js.map +1 -0
- package/dist/types.d.ts +195 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +33 -0
- package/dist/types.js.map +1 -0
- package/dist/uuid.d.ts +2 -0
- package/dist/uuid.d.ts.map +1 -0
- package/dist/uuid.js +28 -0
- package/dist/uuid.js.map +1 -0
- package/package.json +40 -3
- package/src/migrations/001-outbox-dispatcher.sql +60 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drainer.js","sourceRoot":"","sources":["../src/drainer.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,EAAE;AACF,qEAAqE;AACrE,0EAA0E;AAC1E,gDAAgD;AAChD,EAAE;AACF,2EAA2E;AAC3E,6EAA6E;AAC7E,yDAAyD;AAEzD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAoB,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,2EAA2E;AAC3E,MAAM,eAAe,GAAG,MAAM,CAAC;AAe/B,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,IAW3C;IACC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;IACnC,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC;IAEnC,MAAM,GAAG,GAAG,IAAI,UAAU,EAAE,CAAC;IAC7B,MAAM,UAAU,GAAa,EAAE,CAAC;IAChC,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,MAAM,QAAQ,GAAG,kBA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AE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAClB,MAAM,CAAC,MAAM,GAAG,CAAC,EACjB,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CACtC,CAAC;IACF,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { OutboxRow, ReplicationEnvelope } from "./types.js";
|
|
2
|
+
/** Default per-stream MAXLEN ~ approximation (doctrine § 3, ~1M trailing). */
|
|
3
|
+
export declare const DEFAULT_STREAM_MAXLEN = 1000000;
|
|
4
|
+
/**
|
|
5
|
+
* Build the canonical envelope from an outbox row. The `committed_at` is
|
|
6
|
+
* derived from `occurred_at` (ms epoch) so the consumer's lag metric is
|
|
7
|
+
* `XADD_time - occurred_at` per § 5.6.
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildEnvelope(row: OutboxRow): ReplicationEnvelope;
|
|
10
|
+
/** Serialize the envelope to the wire form the consumer parses. */
|
|
11
|
+
export declare function serializeEnvelope(env: ReplicationEnvelope): string;
|
|
12
|
+
/**
|
|
13
|
+
* XADD an envelope to a stream via
|
|
14
|
+
* `XADD <stream> MAXLEN ~ <maxlen> * event <json>`.
|
|
15
|
+
*
|
|
16
|
+
* Byte-identical to @nodii/replica-consumer's `publishReplicationEvent`
|
|
17
|
+
* call shape (single `event` field). Returns the generated stream id.
|
|
18
|
+
*/
|
|
19
|
+
export declare function xaddEnvelope(args: {
|
|
20
|
+
redis: {
|
|
21
|
+
xadd(key: string, ...a: (string | number)[]): Promise<string | null>;
|
|
22
|
+
};
|
|
23
|
+
stream: string;
|
|
24
|
+
env: ReplicationEnvelope;
|
|
25
|
+
streamMaxLenApprox?: number;
|
|
26
|
+
}): Promise<string | null>;
|
|
27
|
+
//# sourceMappingURL=envelope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjE,8EAA8E;AAC9E,eAAO,MAAM,qBAAqB,UAAY,CAAC;AAE/C;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,SAAS,GAAG,mBAAmB,CAsBjE;AAED,mEAAmE;AACnE,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,mBAAmB,GAAG,MAAM,CAElE;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE;IACvC,KAAK,EAAE;QACL,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;KACtE,CAAC;IACF,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,mBAAmB,CAAC;IACzB,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAezB"}
|
package/dist/envelope.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The canonical XADD envelope — the load-bearing producer↔consumer contract.
|
|
2
|
+
//
|
|
3
|
+
// Per `09-replication-doctrine § 2.4` (XADD envelope) AND the literal read
|
|
4
|
+
// path of @nodii/replica-consumer (#12): its `publishReplicationEvent`
|
|
5
|
+
// emits, and its worker `parseXReadGroup` reads, a SINGLE Redis-Streams
|
|
6
|
+
// field named `event` whose value is `JSON.stringify(ReplicationEvent)`.
|
|
7
|
+
//
|
|
8
|
+
// This library MUST emit the byte-identical shape so the consumer's
|
|
9
|
+
// `consumeOneEvent` / `toRow` accept it with no schema mismatch. The
|
|
10
|
+
// contract test (tests/contract.test.ts) drives the dispatcher's XADD
|
|
11
|
+
// output back through the consumer's read path to prove round-trip.
|
|
12
|
+
//
|
|
13
|
+
// Field-name mapping note (§ 5.2 of the feature_doc lists individual XADD
|
|
14
|
+
// fields incl. `source_entity_version` + a separate `telemetry` field; the
|
|
15
|
+
// SHIPPED consumer reads a single JSON `event` field keyed `entity_version`).
|
|
16
|
+
// Per the feature_doc's own binding statement — "XADD envelope MUST match
|
|
17
|
+
// what @nodii/replica-consumer #12 reads" — the consumer's read path wins.
|
|
18
|
+
// We therefore map the outbox column `source_entity_version` → envelope
|
|
19
|
+
// `entity_version`, and fold `telemetry_envelope` into the payload under
|
|
20
|
+
// `_telemetry` so it survives the round-trip without breaking the consumer's
|
|
21
|
+
// fixed key set. Drift filed (see report).
|
|
22
|
+
/** Default per-stream MAXLEN ~ approximation (doctrine § 3, ~1M trailing). */
|
|
23
|
+
export const DEFAULT_STREAM_MAXLEN = 1_000_000;
|
|
24
|
+
/**
|
|
25
|
+
* Build the canonical envelope from an outbox row. The `committed_at` is
|
|
26
|
+
* derived from `occurred_at` (ms epoch) so the consumer's lag metric is
|
|
27
|
+
* `XADD_time - occurred_at` per § 5.6.
|
|
28
|
+
*/
|
|
29
|
+
export function buildEnvelope(row) {
|
|
30
|
+
const payload = { ...(row.payload ?? {}) };
|
|
31
|
+
// Fold the producer telemetry envelope into the payload under a reserved
|
|
32
|
+
// key so it round-trips through the consumer's fixed key set without
|
|
33
|
+
// requiring a consumer-side schema change.
|
|
34
|
+
if (row.telemetry_envelope != null) {
|
|
35
|
+
payload._telemetry = row.telemetry_envelope;
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
event_id: row.event_id,
|
|
39
|
+
topic: row.topic,
|
|
40
|
+
aggregate_kind: row.aggregate_kind,
|
|
41
|
+
aggregate_id: row.aggregate_id,
|
|
42
|
+
entity_version: Number(row.source_entity_version),
|
|
43
|
+
payload,
|
|
44
|
+
// The producer does not pre-assign a stream id; the consumer reads the
|
|
45
|
+
// live Redis id off the wire. Empty string mirrors that absence and is
|
|
46
|
+
// a no-op for the consumer (it only treats the literal value
|
|
47
|
+
// "snapshot" specially).
|
|
48
|
+
stream_id: "",
|
|
49
|
+
committed_at: toEpochMs(row.occurred_at),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Serialize the envelope to the wire form the consumer parses. */
|
|
53
|
+
export function serializeEnvelope(env) {
|
|
54
|
+
return JSON.stringify(env);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* XADD an envelope to a stream via
|
|
58
|
+
* `XADD <stream> MAXLEN ~ <maxlen> * event <json>`.
|
|
59
|
+
*
|
|
60
|
+
* Byte-identical to @nodii/replica-consumer's `publishReplicationEvent`
|
|
61
|
+
* call shape (single `event` field). Returns the generated stream id.
|
|
62
|
+
*/
|
|
63
|
+
export async function xaddEnvelope(args) {
|
|
64
|
+
const maxlen = args.streamMaxLenApprox ?? DEFAULT_STREAM_MAXLEN;
|
|
65
|
+
const payload = serializeEnvelope(args.env);
|
|
66
|
+
if (maxlen <= 0) {
|
|
67
|
+
return args.redis.xadd(args.stream, "*", "event", payload);
|
|
68
|
+
}
|
|
69
|
+
return args.redis.xadd(args.stream, "MAXLEN", "~", maxlen, "*", "event", payload);
|
|
70
|
+
}
|
|
71
|
+
function toEpochMs(v) {
|
|
72
|
+
if (v instanceof Date)
|
|
73
|
+
return v.getTime();
|
|
74
|
+
const n = Date.parse(v);
|
|
75
|
+
return Number.isNaN(n) ? Date.now() : n;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=envelope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA,6EAA6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AC,IAOlC;IACC,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,IAAI,qBAAqB,CAAC;IAChE,MAAM,OAAO,GAAG,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAC7D,CAAC;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CACpB,IAAI,CAAC,MAAM,EACX,QAAQ,EACR,GAAG,EACH,MAAM,EACN,GAAG,EACH,OAAO,EACP,OAAO,CACR,CAAC;AACJ,CAAC;AAED,SAAS,SAAS,CAAC,CAAgB;IACjC,IAAI,CAAC,YAAY,IAAI;QAAE,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IAC1C,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACxB,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1C,CAAC"}
|
package/dist/hwm.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class HwmTracker {
|
|
2
|
+
private readonly map;
|
|
3
|
+
/** Record an XADD id for a stream; keeps the lexicographically-greatest. */
|
|
4
|
+
record(stream: string, streamId: string | null): void;
|
|
5
|
+
/** Latest XADD id this process emitted for the stream, or null. */
|
|
6
|
+
current(stream: string): string | null;
|
|
7
|
+
/** Snapshot of all tracked (stream, hwm) pairs — for the `outbox.hwm` gauge. */
|
|
8
|
+
entries(): Array<[string, string]>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Compare two Redis-Streams ids (`<ms>-<seq>`). Returns >0 if a>b, <0 if
|
|
12
|
+
* a<b, 0 if equal. Numeric per-part so `10-0` > `9-0` correctly.
|
|
13
|
+
*/
|
|
14
|
+
export declare function compareStreamIds(a: string, b: string): number;
|
|
15
|
+
//# sourceMappingURL=hwm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hwm.d.ts","sourceRoot":"","sources":["../src/hwm.ts"],"names":[],"mappings":"AAYA,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA6B;IAEjD,4EAA4E;IAC5E,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAQrD,mEAAmE;IACnE,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAItC,gFAAgF;IAChF,OAAO,IAAI,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAGnC;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAM7D"}
|
package/dist/hwm.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// HWM (high-water-mark) tracker — per `09-replication-doctrine § 7`.
|
|
2
|
+
//
|
|
3
|
+
// The dispatcher keeps an in-memory Map<stream, latestXaddId> updated on
|
|
4
|
+
// every successful XADD. `currentHwm(stream)` returns the latest id (or
|
|
5
|
+
// null if no XADDs have been emitted for the stream this process). Snapshot
|
|
6
|
+
// RPC handlers call this at snapshot freeze; the value becomes the final
|
|
7
|
+
// snapshot chunk's `stream_high_water_mark` (the consumer's resume cursor).
|
|
8
|
+
//
|
|
9
|
+
// Redis-Streams ids (`<ms>-<seq>`) are lexicographically AND numerically
|
|
10
|
+
// sortable; we keep the max so out-of-order in-flight completions can't
|
|
11
|
+
// regress the HWM.
|
|
12
|
+
export class HwmTracker {
|
|
13
|
+
map = new Map();
|
|
14
|
+
/** Record an XADD id for a stream; keeps the lexicographically-greatest. */
|
|
15
|
+
record(stream, streamId) {
|
|
16
|
+
if (!streamId)
|
|
17
|
+
return;
|
|
18
|
+
const prev = this.map.get(stream);
|
|
19
|
+
if (prev === undefined || compareStreamIds(streamId, prev) > 0) {
|
|
20
|
+
this.map.set(stream, streamId);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Latest XADD id this process emitted for the stream, or null. */
|
|
24
|
+
current(stream) {
|
|
25
|
+
return this.map.get(stream) ?? null;
|
|
26
|
+
}
|
|
27
|
+
/** Snapshot of all tracked (stream, hwm) pairs — for the `outbox.hwm` gauge. */
|
|
28
|
+
entries() {
|
|
29
|
+
return [...this.map.entries()];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Compare two Redis-Streams ids (`<ms>-<seq>`). Returns >0 if a>b, <0 if
|
|
34
|
+
* a<b, 0 if equal. Numeric per-part so `10-0` > `9-0` correctly.
|
|
35
|
+
*/
|
|
36
|
+
export function compareStreamIds(a, b) {
|
|
37
|
+
const [aMs, aSeq] = splitId(a);
|
|
38
|
+
const [bMs, bSeq] = splitId(b);
|
|
39
|
+
if (aMs !== bMs)
|
|
40
|
+
return aMs < bMs ? -1 : 1;
|
|
41
|
+
if (aSeq !== bSeq)
|
|
42
|
+
return aSeq < bSeq ? -1 : 1;
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
function splitId(id) {
|
|
46
|
+
const dash = id.indexOf("-");
|
|
47
|
+
if (dash < 0)
|
|
48
|
+
return [BigInt(id || "0"), 0n];
|
|
49
|
+
return [BigInt(id.slice(0, dash) || "0"), BigInt(id.slice(dash + 1) || "0")];
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=hwm.js.map
|
package/dist/hwm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hwm.js","sourceRoot":"","sources":["../src/hwm.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,yEAAyE;AACzE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CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,CAAC;AACX,CAAC;AAED,SAAS,OAAO,CAAC,EAAU;IACzB,MAAM,IAAI,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,IAAI,GAAG,CAAC;QAAE,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;IAC7C,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;AAC/E,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
|
-
export declare const LIB_NAME
|
|
2
|
-
export declare const VERSION
|
|
1
|
+
export declare const LIB_NAME = "outbox-dispatcher";
|
|
2
|
+
export declare const VERSION = "0.1.0";
|
|
3
|
+
export { defineDispatcher, DEFAULT_BATCH_SIZE, DEFAULT_DRAIN_DEADLINE_SECONDS, DEFAULT_LEASE_TTL_MS, DEFAULT_POLL_MS, DEFAULT_PUBLISH_MAX_ATTEMPTS, snakeServiceName, } from "./define-dispatcher.js";
|
|
4
|
+
export { type DispatcherRunnerHandle, startDispatcherRunner, } from "./drainer.js";
|
|
5
|
+
export { buildEnvelope, DEFAULT_STREAM_MAXLEN, serializeEnvelope, xaddEnvelope, } from "./envelope.js";
|
|
6
|
+
export { compareStreamIds, HwmTracker } from "./hwm.js";
|
|
7
|
+
export { type LeaseHandle, type LeaseOpts, tryAcquireLease } from "./lease.js";
|
|
8
|
+
export { applyOutboxDispatcherMigrations, getOutboxDispatcherMigrationSQL, } from "./migrations/index.js";
|
|
9
|
+
export { buildClaimBatchSql, buildDeleteOutboxRowSql, buildDepthByTopicSql, buildDlqInsertSql, buildMarkDispatchedSql, buildRecordFailureSql, quoteIdent, } from "./sql-builders.js";
|
|
10
|
+
export { type DispatcherHandle, startDispatcher } from "./start.js";
|
|
11
|
+
export { DispatcherNotWired, InvalidDispatcherConfig, OutboxDispatcherError, } from "./types.js";
|
|
12
|
+
export type { DbRlsLike, DispatcherConfig, OutboxRow, RedisLike, ReplicationEnvelope, ResolvedDispatcher, ResolvedStartConfig, SqlExecutor, SqlPoolExecutor, SqlTxExecutor, StartDispatcherConfig, TelemetryAdapter, } from "./types.js";
|
|
13
|
+
export { uuidv7 } from "./uuid.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,QAAQ,sBAAsB,CAAC;AAC5C,eAAO,MAAM,OAAO,UAAU,CAAC;AAE/B,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,8BAA8B,EAC9B,oBAAoB,EACpB,eAAe,EACf,4BAA4B,EAC5B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,KAAK,sBAAsB,EAC3B,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EACL,+BAA+B,EAC/B,+BAA+B,GAChC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,EACrB,UAAU,GACX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,KAAK,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,SAAS,EACT,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,eAAe,EACf,aAAa,EACb,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
|
+
// @nodii/outbox-dispatcher — v0.1.0 public barrel.
|
|
2
|
+
//
|
|
3
|
+
// Spec: planning hub feature_doc serviceId=nodii-libs docKey=outbox-dispatcher.
|
|
4
|
+
// Producer-side Pattern-3 outbox drainer; symmetric peer of
|
|
5
|
+
// @nodii/replica-consumer (#12). Polyglot ship: TS + Python + Go.
|
|
6
|
+
//
|
|
7
|
+
// Surface:
|
|
8
|
+
// - Declarative entry: defineDispatcher / startDispatcher
|
|
9
|
+
// - Handle: DispatcherHandle (currentHwm / lag / stop / runners)
|
|
10
|
+
// - Migrations: getOutboxDispatcherMigrationSQL /
|
|
11
|
+
// applyOutboxDispatcherMigrations
|
|
12
|
+
// - Envelope (advanced / contract tests): buildEnvelope /
|
|
13
|
+
// serializeEnvelope / xaddEnvelope / DEFAULT_STREAM_MAXLEN
|
|
14
|
+
// - Lease primitive (advanced): tryAcquireLease
|
|
15
|
+
// - HWM tracker (advanced): HwmTracker / compareStreamIds
|
|
16
|
+
// - SQL builders (advanced): buildClaimBatchSql etc.
|
|
17
|
+
// - Errors: OutboxDispatcherError + typed subclasses
|
|
18
|
+
// - uuidv7
|
|
1
19
|
export const LIB_NAME = "outbox-dispatcher";
|
|
2
|
-
export const VERSION = "0.0
|
|
20
|
+
export const VERSION = "0.1.0";
|
|
21
|
+
export { defineDispatcher, DEFAULT_BATCH_SIZE, DEFAULT_DRAIN_DEADLINE_SECONDS, DEFAULT_LEASE_TTL_MS, DEFAULT_POLL_MS, DEFAULT_PUBLISH_MAX_ATTEMPTS, snakeServiceName, } from "./define-dispatcher.js";
|
|
22
|
+
export { startDispatcherRunner, } from "./drainer.js";
|
|
23
|
+
export { buildEnvelope, DEFAULT_STREAM_MAXLEN, serializeEnvelope, xaddEnvelope, } from "./envelope.js";
|
|
24
|
+
export { compareStreamIds, HwmTracker } from "./hwm.js";
|
|
25
|
+
export { tryAcquireLease } from "./lease.js";
|
|
26
|
+
export { applyOutboxDispatcherMigrations, getOutboxDispatcherMigrationSQL, } from "./migrations/index.js";
|
|
27
|
+
export { buildClaimBatchSql, buildDeleteOutboxRowSql, buildDepthByTopicSql, buildDlqInsertSql, buildMarkDispatchedSql, buildRecordFailureSql, quoteIdent, } from "./sql-builders.js";
|
|
28
|
+
export { startDispatcher } from "./start.js";
|
|
29
|
+
export { DispatcherNotWired, InvalidDispatcherConfig, OutboxDispatcherError, } from "./types.js";
|
|
30
|
+
export { uuidv7 } from "./uuid.js";
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,gFAAgF;AAChF,4DAA4D;AAC5D,kEAAkE;AAClE,EAAE;AACF,WAAW;AACX,4DAA4D;AAC5D,mEAAmE;AACnE,oDAAoD;AACpD,kDAAkD;AAClD,4DAA4D;AAC5D,2EAA2E;AAC3E,kDAAkD;AAClD,4DAA4D;AAC5D,uDAAuD;AACvD,uDAAuD;AACvD,aAAa;AAEb,MAAM,CAAC,MAAM,QAAQ,GAAG,mBAAmB,CAAC;AAC5C,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC;AAE/B,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,8BAA8B,EAC9B,oBAAoB,EACpB,eAAe,EACf,4BAA4B,EAC5B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAoC,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EACL,+BAA+B,EAC/B,+BAA+B,GAChC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,EACrB,UAAU,GACX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAyB,eAAe,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAepB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/lease.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { RedisLike, TelemetryAdapter } from "./types.js";
|
|
2
|
+
export interface LeaseOpts {
|
|
3
|
+
redis: RedisLike;
|
|
4
|
+
leaseKey: string;
|
|
5
|
+
leaseTtlMs: number;
|
|
6
|
+
/** Stable per-process id. Defaults to a fresh uuidv7. */
|
|
7
|
+
ownerId?: string;
|
|
8
|
+
telemetry?: TelemetryAdapter;
|
|
9
|
+
serviceName: string;
|
|
10
|
+
}
|
|
11
|
+
export interface LeaseHandle {
|
|
12
|
+
/** Did we acquire the lease on this call? */
|
|
13
|
+
readonly acquired: boolean;
|
|
14
|
+
/** Did the renewal loop detect we lost the lease? */
|
|
15
|
+
readonly lost: boolean;
|
|
16
|
+
/** This holder's owner id. */
|
|
17
|
+
readonly ownerId: string;
|
|
18
|
+
/** Stop the renewal loop + CAS-release (best-effort). */
|
|
19
|
+
release(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Attempt to acquire the single-active lease. If another process holds it,
|
|
23
|
+
* returns an unacquired handle (the caller should poll + retry). On success,
|
|
24
|
+
* starts a renewal timer at ttl/2 cadence.
|
|
25
|
+
*/
|
|
26
|
+
export declare function tryAcquireLease(opts: LeaseOpts): Promise<LeaseHandle>;
|
|
27
|
+
//# sourceMappingURL=lease.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lease.d.ts","sourceRoot":"","sources":["../src/lease.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAiB9D,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,SAAS,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,qDAAqD;IACrD,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,8BAA8B;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,yDAAyD;IACzD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CA6E3E"}
|
package/dist/lease.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// D209 single-active-worker lease — Redis SET key val NX EX <ttl> + periodic
|
|
2
|
+
// renewal via a Lua CAS. Only the lease holder drains the outbox.
|
|
3
|
+
//
|
|
4
|
+
// Per the feature_doc § 5.5:
|
|
5
|
+
// - Acquire: SET leaseKey processId NX EX leaseTtlMs/1000
|
|
6
|
+
// - Renew: every leaseTtlMs/2, Lua CAS-renew (GET==me ? EXPIRE : 0)
|
|
7
|
+
// - Release: Lua CAS-delete on graceful shutdown so a successor claims fast
|
|
8
|
+
// - Crash: key expires after leaseTtlMs; the next poller acquires
|
|
9
|
+
//
|
|
10
|
+
// BullMQ is NOT used (D209 softened the § 9.2 BullMQ wording). This is the
|
|
11
|
+
// canonical primitive. Byte-parity with @nodii/replica-consumer's lease.
|
|
12
|
+
import { uuidv7 } from "./uuid.js";
|
|
13
|
+
const RENEW_LUA = `
|
|
14
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
15
|
+
return redis.call("EXPIRE", KEYS[1], ARGV[2])
|
|
16
|
+
end
|
|
17
|
+
return 0
|
|
18
|
+
`;
|
|
19
|
+
const RELEASE_LUA = `
|
|
20
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
21
|
+
return redis.call("DEL", KEYS[1])
|
|
22
|
+
end
|
|
23
|
+
return 0
|
|
24
|
+
`;
|
|
25
|
+
/**
|
|
26
|
+
* Attempt to acquire the single-active lease. If another process holds it,
|
|
27
|
+
* returns an unacquired handle (the caller should poll + retry). On success,
|
|
28
|
+
* starts a renewal timer at ttl/2 cadence.
|
|
29
|
+
*/
|
|
30
|
+
export async function tryAcquireLease(opts) {
|
|
31
|
+
const ownerId = opts.ownerId ?? uuidv7();
|
|
32
|
+
const ttlSeconds = Math.max(1, Math.floor(opts.leaseTtlMs / 1000));
|
|
33
|
+
const result = await opts.redis.set(opts.leaseKey, ownerId, "EX", ttlSeconds, "NX");
|
|
34
|
+
if (result !== "OK") {
|
|
35
|
+
return {
|
|
36
|
+
acquired: false,
|
|
37
|
+
lost: false,
|
|
38
|
+
ownerId,
|
|
39
|
+
async release() {
|
|
40
|
+
/* nothing held */
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let lost = false;
|
|
45
|
+
const renewMs = Math.max(1_000, Math.floor(opts.leaseTtlMs / 2));
|
|
46
|
+
const renewTimer = setInterval(async () => {
|
|
47
|
+
try {
|
|
48
|
+
const r = (await opts.redis.eval(RENEW_LUA, 1, opts.leaseKey, ownerId, String(ttlSeconds)));
|
|
49
|
+
if (Number(r) === 1) {
|
|
50
|
+
opts.telemetry?.recordMetric({
|
|
51
|
+
kind: "counter",
|
|
52
|
+
name: "outbox.lease_renewals",
|
|
53
|
+
value: 1,
|
|
54
|
+
attributes: { service: opts.serviceName, result: "held" },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
lost = true;
|
|
59
|
+
opts.telemetry?.recordMetric({
|
|
60
|
+
kind: "counter",
|
|
61
|
+
name: "outbox.lease_renewals",
|
|
62
|
+
value: 1,
|
|
63
|
+
attributes: { service: opts.serviceName, result: "lost" },
|
|
64
|
+
});
|
|
65
|
+
clearInterval(renewTimer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
lost = true;
|
|
70
|
+
opts.telemetry?.recordMetric({
|
|
71
|
+
kind: "counter",
|
|
72
|
+
name: "outbox.lease_renewals",
|
|
73
|
+
value: 1,
|
|
74
|
+
attributes: { service: opts.serviceName, result: "error" },
|
|
75
|
+
});
|
|
76
|
+
clearInterval(renewTimer);
|
|
77
|
+
}
|
|
78
|
+
}, renewMs);
|
|
79
|
+
renewTimer.unref?.();
|
|
80
|
+
return {
|
|
81
|
+
acquired: true,
|
|
82
|
+
get lost() {
|
|
83
|
+
return lost;
|
|
84
|
+
},
|
|
85
|
+
ownerId,
|
|
86
|
+
async release() {
|
|
87
|
+
clearInterval(renewTimer);
|
|
88
|
+
try {
|
|
89
|
+
await opts.redis.eval(RELEASE_LUA, 1, opts.leaseKey, ownerId);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
/* swallow — key will TTL-expire anyway */
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=lease.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lease.js","sourceRoot":"","sources":["../src/lease.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,kEAAkE;AAClE,EAAE;AACF,6BAA6B;AAC7B,4DAA4D;AAC5D,wEAAwE;AACxE,8EAA8E;AAC9E,sEAAsE;AACtE,EAAE;AACF,2EAA2E;AAC3E,yEAAyE;AAGzE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,MAAM,SAAS,GAAG;;;;;CAKjB,CAAC;AAEF,MAAM,WAAW,GAAG;;;;;CAKnB,CAAC;AAuBF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,IAAe;IACnD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,MAAM,EAAE,CAAC;IACzC,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC;IACnE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CACjC,IAAI,CAAC,QAAQ,EACb,OAAO,EACP,IAAI,EACJ,UAAU,EACV,IAAI,CACL,CAAC;IACF,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,OAAO;YACL,QAAQ,EAAE,KAAK;YACf,IAAI,EAAE,KAAK;YACX,OAAO;YACP,KAAK,CAAC,OAAO;gBACX,kBAAkB;YACpB,CAAC;SACF,CAAC;IACJ,CAAC;IAED,IAAI,IAAI,GAAG,KAAK,CAAC;IACjB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CA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AC;IACX,UAAgD,CAAC,KAAK,EAAE,EAAE,CAAC;IAE5D,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,IAAI,IAAI;YACN,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO;QACP,KAAK,CAAC,OAAO;YACX,aAAa,CAAC,UAAU,CAAC,CAAC;YAC1B,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAChE,CAAC;YAAC,MAAM,CAAC;gBACP,0CAA0C;YAC5C,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SqlExecutor } from "../types.js";
|
|
2
|
+
/** Generate the outbox + DLQ DDL for a service's outbox + DLQ table names. */
|
|
3
|
+
export declare function getOutboxDispatcherMigrationSQL(args: {
|
|
4
|
+
outboxTable?: string;
|
|
5
|
+
dlqTable: string;
|
|
6
|
+
}): string;
|
|
7
|
+
/** Apply the outbox + DLQ DDL via a SqlExecutor (integration/test/boot). */
|
|
8
|
+
export declare function applyOutboxDispatcherMigrations(exec: SqlExecutor, args: {
|
|
9
|
+
outboxTable?: string;
|
|
10
|
+
dlqTable: string;
|
|
11
|
+
}): Promise<void>;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,8EAA8E;AAC9E,wBAAgB,+BAA+B,CAAC,IAAI,EAAE;IACpD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;CAClB,GAAG,MAAM,CAiET;AAED,4EAA4E;AAC5E,wBAAsB,+BAA+B,CACnD,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/C,OAAO,CAAC,IAAI,CAAC,CAGf"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Migration SQL generators — the `<service>_outbox` + `<service>_outbox_dlq`
|
|
2
|
+
// tables per `09-replication-doctrine § 2.2` + feature_doc § 5.4 / § 5.8.
|
|
3
|
+
//
|
|
4
|
+
// The canonical outbox columns the drain loop reads (per § 5.2):
|
|
5
|
+
// id, event_id, aggregate_kind, aggregate_id, source_entity_version,
|
|
6
|
+
// topic, payload, telemetry_envelope, occurred_at, dispatched_at,
|
|
7
|
+
// stream_id, publish_attempts, last_error, last_attempt_at
|
|
8
|
+
//
|
|
9
|
+
// migrate-gen (src/cli/migrate-gen.ts) writes these out per service. The
|
|
10
|
+
// `applyOutboxDispatcherMigrations` helper is for integration tests +
|
|
11
|
+
// programmatic boot wiring. All DDL is IF NOT EXISTS / idempotent so it is
|
|
12
|
+
// safe to re-run and safe against an existing pre-library outbox table
|
|
13
|
+
// (ALTER ... ADD COLUMN IF NOT EXISTS backfills dispatched_at + stream_id +
|
|
14
|
+
// retry columns onto a legacy table per the feature_doc § 6 migration note).
|
|
15
|
+
import { quoteIdent } from "../sql-builders.js";
|
|
16
|
+
/** Generate the outbox + DLQ DDL for a service's outbox + DLQ table names. */
|
|
17
|
+
export function getOutboxDispatcherMigrationSQL(args) {
|
|
18
|
+
const outbox = quoteIdent(args.outboxTable ?? "outbox");
|
|
19
|
+
const dlq = quoteIdent(args.dlqTable);
|
|
20
|
+
return `-- @nodii/outbox-dispatcher v0.1.0 — producer-side outbox + DLQ tables.
|
|
21
|
+
--
|
|
22
|
+
-- Schema tracks 09-replication-doctrine § 2.2 (outbox) + outbox-dispatcher
|
|
23
|
+
-- feature_doc § 5.4 (DLQ). One outbox per producing service. The producer
|
|
24
|
+
-- writes outbox rows in the SAME business tx as the source-row mutation;
|
|
25
|
+
-- the dispatcher drains them to Redis Streams via XADD.
|
|
26
|
+
|
|
27
|
+
-- ── 1. ${args.outboxTable ?? "outbox"} ──────────────────────────────
|
|
28
|
+
CREATE TABLE IF NOT EXISTS ${outbox} (
|
|
29
|
+
id TEXT PRIMARY KEY,
|
|
30
|
+
event_id TEXT NOT NULL,
|
|
31
|
+
aggregate_kind TEXT NOT NULL,
|
|
32
|
+
aggregate_id TEXT NOT NULL,
|
|
33
|
+
source_entity_version BIGINT NOT NULL,
|
|
34
|
+
topic TEXT NOT NULL,
|
|
35
|
+
payload JSONB NOT NULL,
|
|
36
|
+
telemetry_envelope JSONB NULL,
|
|
37
|
+
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
38
|
+
dispatched_at TIMESTAMPTZ NULL,
|
|
39
|
+
stream_id TEXT NULL,
|
|
40
|
+
publish_attempts INT NOT NULL DEFAULT 0,
|
|
41
|
+
last_error TEXT NULL,
|
|
42
|
+
last_attempt_at TIMESTAMPTZ NULL
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
-- Forward-compat ALTERs for an EXISTING pre-library outbox table (feature_doc
|
|
46
|
+
-- § 6): add the drain-bookkeeping columns if absent. Idempotent + no-op on a
|
|
47
|
+
-- freshly-created table.
|
|
48
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS telemetry_envelope JSONB NULL;
|
|
49
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS dispatched_at TIMESTAMPTZ NULL;
|
|
50
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS stream_id TEXT NULL;
|
|
51
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS publish_attempts INT NOT NULL DEFAULT 0;
|
|
52
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS last_error TEXT NULL;
|
|
53
|
+
ALTER TABLE ${outbox} ADD COLUMN IF NOT EXISTS last_attempt_at TIMESTAMPTZ NULL;
|
|
54
|
+
|
|
55
|
+
-- Partial index on the hot drain predicate (un-dispatched, FIFO by occurred_at).
|
|
56
|
+
CREATE INDEX IF NOT EXISTS idx_${args.outboxTable ?? "outbox"}_pending
|
|
57
|
+
ON ${outbox} (occurred_at)
|
|
58
|
+
WHERE dispatched_at IS NULL;
|
|
59
|
+
|
|
60
|
+
-- ── 2. ${args.dlqTable} ─────────────────────────────────────────────
|
|
61
|
+
-- Quarantine for rows that exhaust the publish-retry budget (§ 5.4). DLQ
|
|
62
|
+
-- rows are NOT auto-retried; ops requeue manually after root-cause fix.
|
|
63
|
+
CREATE TABLE IF NOT EXISTS ${dlq} (
|
|
64
|
+
id TEXT PRIMARY KEY,
|
|
65
|
+
original_outbox_id TEXT NOT NULL,
|
|
66
|
+
topic TEXT NOT NULL,
|
|
67
|
+
aggregate_kind TEXT NOT NULL,
|
|
68
|
+
aggregate_id TEXT NOT NULL,
|
|
69
|
+
event_id TEXT NOT NULL,
|
|
70
|
+
payload JSONB NOT NULL,
|
|
71
|
+
final_error TEXT NOT NULL,
|
|
72
|
+
failed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
73
|
+
publish_attempts INT NOT NULL DEFAULT 0
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
CREATE INDEX IF NOT EXISTS idx_${args.dlqTable}_failed_at
|
|
77
|
+
ON ${dlq} (failed_at DESC);
|
|
78
|
+
|
|
79
|
+
CREATE INDEX IF NOT EXISTS idx_${args.dlqTable}_event_id
|
|
80
|
+
ON ${dlq} (event_id);
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
/** Apply the outbox + DLQ DDL via a SqlExecutor (integration/test/boot). */
|
|
84
|
+
export async function applyOutboxDispatcherMigrations(exec, args) {
|
|
85
|
+
const sql = getOutboxDispatcherMigrationSQL(args);
|
|
86
|
+
await exec.unsafe(sql);
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC1E,EAAE;AACF,iEAAiE;AACjE,uEAAuE;AACvE,oEAAoE;AACpE,6DAA6D;AAC7D,EAAE;AACF,yEAAyE;AACzE,sEAAsE;AACtE,2EAA2E;AAC3E,uEAAuE;AACvE,4EAA4E;AAC5E,6EAA6E;AAE7E,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGhD,8EAA8E;AAC9E,MAAM,UAAU,+BAA+B,CAAC,IAG/C;IACC,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,WAAW,IAAI,QAAQ,CAAC,CAAC;IACxD,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,OAAO;;;;;;;WAOE,IAAI,CAAC,WAAW,IAAI,QAAQ;6BACV,MAAM;;;;;;;;;;;;;;;;;;;;cAoBrB,MAAM;cACN,MAAM;cACN,MAAM;cACN,MAAM;cACN,MAAM;cACN,MAAM;;;iCAGa,IAAI,CAAC,WAAW,IAAI,QAAQ;OACtD,MAAM;;;WAGF,IAAI,CAAC,QAAQ;;;6BAGK,GAAG;;;;;;;;;;;;;iCAaC,IAAI,CAAC,QAAQ;OACvC,GAAG;;iCAEuB,IAAI,CAAC,QAAQ;OACvC,GAAG;CACT,CAAC;AACF,CAAC;AAED,4EAA4E;AAC5E,MAAM,CAAC,KAAK,UAAU,+BAA+B,CACnD,IAAiB,EACjB,IAAgD;IAEhD,MAAM,GAAG,GAAG,+BAA+B,CAAC,IAAI,CAAC,CAAC;IAClD,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare function quoteIdent(name: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* Claim a batch of pending outbox rows for dispatch. Per § 5.2:
|
|
4
|
+
* - dispatched_at IS NULL (not yet published)
|
|
5
|
+
* - under the retry budget, OR cooled-down (backoff = 1 min * attempts)
|
|
6
|
+
* - ORDER BY occurred_at (FIFO), LIMIT batchSize
|
|
7
|
+
* - FOR UPDATE SKIP LOCKED (multi-claimant safe within a tx)
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildClaimBatchSql(outboxTable: string): string;
|
|
10
|
+
/** Mark a row dispatched + record the assigned stream id. Per § 5.2 step 4. */
|
|
11
|
+
export declare function buildMarkDispatchedSql(outboxTable: string): string;
|
|
12
|
+
/** Record a publish failure — bump attempts, stamp error + attempt time. */
|
|
13
|
+
export declare function buildRecordFailureSql(outboxTable: string): string;
|
|
14
|
+
/** Move a row to the DLQ (insert) — § 5.4. */
|
|
15
|
+
export declare function buildDlqInsertSql(dlqTable: string): string;
|
|
16
|
+
/** Delete the DLQ'd row from the outbox (moved, not retried) — § 5.4. */
|
|
17
|
+
export declare function buildDeleteOutboxRowSql(outboxTable: string): string;
|
|
18
|
+
/** Count pending (un-dispatched) rows by topic — `outbox.depth` gauge § 5.6. */
|
|
19
|
+
export declare function buildDepthByTopicSql(outboxTable: string): string;
|
|
20
|
+
//# sourceMappingURL=sql-builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-builders.d.ts","sourceRoot":"","sources":["../src/sql-builders.ts"],"names":[],"mappings":"AASA,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAK/C;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAa9D;AAED,+EAA+E;AAC/E,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAGlE;AAED,4EAA4E;AAC5E,wBAAgB,qBAAqB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAOjE;AAED,8CAA8C;AAC9C,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAM1D;AAED,yEAAyE;AACzE,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAGnE;AAED,gFAAgF;AAChF,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMhE"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// SQL builders — the canonical drain claim + mark + retry + DLQ shapes.
|
|
2
|
+
//
|
|
3
|
+
// Per `01-comms § 9.2` (FOR UPDATE SKIP LOCKED claim) + § 5.2/§5.4 of the
|
|
4
|
+
// feature_doc. Table names are interpolated from the configured serviceName
|
|
5
|
+
// (validated against an identifier allowlist via `quoteIdent`); all values
|
|
6
|
+
// are positional ($1...). Parity with the Python + Go ports.
|
|
7
|
+
const IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
8
|
+
export function quoteIdent(name) {
|
|
9
|
+
if (!IDENT_RE.test(name)) {
|
|
10
|
+
throw new Error(`invalid SQL identifier: ${name}`);
|
|
11
|
+
}
|
|
12
|
+
return `"${name}"`;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Claim a batch of pending outbox rows for dispatch. Per § 5.2:
|
|
16
|
+
* - dispatched_at IS NULL (not yet published)
|
|
17
|
+
* - under the retry budget, OR cooled-down (backoff = 1 min * attempts)
|
|
18
|
+
* - ORDER BY occurred_at (FIFO), LIMIT batchSize
|
|
19
|
+
* - FOR UPDATE SKIP LOCKED (multi-claimant safe within a tx)
|
|
20
|
+
*/
|
|
21
|
+
export function buildClaimBatchSql(outboxTable) {
|
|
22
|
+
const t = quoteIdent(outboxTable);
|
|
23
|
+
return `SELECT id, event_id, aggregate_kind, aggregate_id, source_entity_version,
|
|
24
|
+
topic, payload, telemetry_envelope, occurred_at, dispatched_at,
|
|
25
|
+
stream_id, publish_attempts, last_error, last_attempt_at
|
|
26
|
+
FROM ${t}
|
|
27
|
+
WHERE dispatched_at IS NULL
|
|
28
|
+
AND publish_attempts < $1
|
|
29
|
+
AND (last_attempt_at IS NULL
|
|
30
|
+
OR last_attempt_at < now() - (interval '1 minute' * publish_attempts))
|
|
31
|
+
ORDER BY occurred_at ASC
|
|
32
|
+
LIMIT $2
|
|
33
|
+
FOR UPDATE SKIP LOCKED`;
|
|
34
|
+
}
|
|
35
|
+
/** Mark a row dispatched + record the assigned stream id. Per § 5.2 step 4. */
|
|
36
|
+
export function buildMarkDispatchedSql(outboxTable) {
|
|
37
|
+
const t = quoteIdent(outboxTable);
|
|
38
|
+
return `UPDATE ${t} SET dispatched_at = now(), stream_id = $1 WHERE id = $2`;
|
|
39
|
+
}
|
|
40
|
+
/** Record a publish failure — bump attempts, stamp error + attempt time. */
|
|
41
|
+
export function buildRecordFailureSql(outboxTable) {
|
|
42
|
+
const t = quoteIdent(outboxTable);
|
|
43
|
+
return `UPDATE ${t}
|
|
44
|
+
SET publish_attempts = publish_attempts + 1,
|
|
45
|
+
last_error = $1,
|
|
46
|
+
last_attempt_at = now()
|
|
47
|
+
WHERE id = $2`;
|
|
48
|
+
}
|
|
49
|
+
/** Move a row to the DLQ (insert) — § 5.4. */
|
|
50
|
+
export function buildDlqInsertSql(dlqTable) {
|
|
51
|
+
const t = quoteIdent(dlqTable);
|
|
52
|
+
return `INSERT INTO ${t}
|
|
53
|
+
(id, original_outbox_id, topic, aggregate_kind, aggregate_id, event_id,
|
|
54
|
+
payload, final_error, failed_at, publish_attempts)
|
|
55
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7::jsonb, $8, now(), $9)`;
|
|
56
|
+
}
|
|
57
|
+
/** Delete the DLQ'd row from the outbox (moved, not retried) — § 5.4. */
|
|
58
|
+
export function buildDeleteOutboxRowSql(outboxTable) {
|
|
59
|
+
const t = quoteIdent(outboxTable);
|
|
60
|
+
return `DELETE FROM ${t} WHERE id = $1`;
|
|
61
|
+
}
|
|
62
|
+
/** Count pending (un-dispatched) rows by topic — `outbox.depth` gauge § 5.6. */
|
|
63
|
+
export function buildDepthByTopicSql(outboxTable) {
|
|
64
|
+
const t = quoteIdent(outboxTable);
|
|
65
|
+
return `SELECT topic, COUNT(*)::int AS depth
|
|
66
|
+
FROM ${t}
|
|
67
|
+
WHERE dispatched_at IS NULL
|
|
68
|
+
GROUP BY topic`;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=sql-builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-builders.js","sourceRoot":"","sources":["../src/sql-builders.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,2EAA2E;AAC3E,6DAA6D;AAE7D,MAAM,QAAQ,GAAG,0BAA0B,CAAC;AAE5C,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,2BAA2B,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,IAAI,IAAI,GAAG,CAAC;AACrB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAAmB;IACpD,MAAM,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAClC,OAAO;;;SAGA,CAAC;;;;;;;wBAOc,CAAC;AACzB,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,sBAAsB,CAAC,WAAmB;IACxD,MAAM,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAClC,OAAO,UAAU,CAAC,0DAA0D,CAAC;AAC/E,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,qBAAqB,CAAC,WAAmB;IACvD,MAAM,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IAClC,OAAO,UAAU,CAAC;;;;iBAIH,CAAC;AAClB,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,iBAAiB,CAAC,QAAgB;IAChD,MAAM,CAAC,GAAG,UAAU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