@voltro/plugin-cdc-out 0.84.0 → 0.86.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/CHANGELOG.md +1215 -0
- package/THIRD-PARTY-NOTICES.md +118 -600
- package/dist/engine.js +156 -0
- package/dist/errors.js +12 -0
- package/dist/handoff.js +37 -0
- package/dist/index.d.ts +8 -11
- package/dist/index.js +183 -477
- package/dist/outbox.js +72 -0
- package/dist/sinks.js +46 -0
- package/package.json +5 -5
package/dist/engine.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { CdcDeliveryError as e } from "./errors.js";
|
|
2
|
+
import { CDCOUT_OUTBOX_TABLE as t, toMs as n } from "./outbox.js";
|
|
3
|
+
import { Duration as r, Effect as i, Result as a } from "effect";
|
|
4
|
+
import { and as o, eq as s, inSet as c, lt as l } from "@voltro/database";
|
|
5
|
+
import { withServerTracer as u } from "@voltro/runtime";
|
|
6
|
+
//#region src/engine.ts
|
|
7
|
+
var d = (t, n, a) => i.suspend(() => {
|
|
8
|
+
let o = new AbortController();
|
|
9
|
+
return i.try(() => t.deliver(n, { signal: o.signal })).pipe(i.flatMap((e) => i.isEffect(e) ? e : i.tryPromise(() => e))).pipe(i.timeoutOrElse({
|
|
10
|
+
duration: r.millis(a.timeoutMs),
|
|
11
|
+
orElse: () => i.fail(new e({
|
|
12
|
+
sink: t.name,
|
|
13
|
+
reason: `delivery timed out after ${a.timeoutMs}ms`
|
|
14
|
+
}))
|
|
15
|
+
}), i.onError(() => i.sync(() => o.abort())), i.mapError((n) => n instanceof e ? n : f(t.name, n)), i.withSpan("cdcOut.deliver", { attributes: {
|
|
16
|
+
pipe: a.pipe,
|
|
17
|
+
sink: t.name,
|
|
18
|
+
records: n.length
|
|
19
|
+
} }));
|
|
20
|
+
}), f = (t, n) => {
|
|
21
|
+
let r = typeof n == "object" && n && "cause" in n && n.cause !== void 0 ? n.cause : n, i = r instanceof Error ? r.message : String(r);
|
|
22
|
+
return new e({
|
|
23
|
+
sink: t,
|
|
24
|
+
reason: i
|
|
25
|
+
});
|
|
26
|
+
}, p = (e, t) => {
|
|
27
|
+
let n = Math.min(t.baseMs * 2 ** Math.max(0, e - 1), t.maxMs);
|
|
28
|
+
return Math.round(n * (.5 + .5 * (t.random ?? Math.random)()));
|
|
29
|
+
}, m = class {
|
|
30
|
+
opts;
|
|
31
|
+
now;
|
|
32
|
+
stuckClaimMs;
|
|
33
|
+
constructor(e) {
|
|
34
|
+
this.opts = e, this.now = e.now ?? Date.now, this.stuckClaimMs = Math.max(2 * e.deliveryTimeoutMs, 1e4);
|
|
35
|
+
}
|
|
36
|
+
async tick(e) {
|
|
37
|
+
this.opts.isLeader() && await Promise.all(this.opts.pipes.map(async (t) => {
|
|
38
|
+
if (!t.busy) {
|
|
39
|
+
t.busy = !0;
|
|
40
|
+
try {
|
|
41
|
+
await this.pipeTick(e, t);
|
|
42
|
+
} finally {
|
|
43
|
+
t.busy = !1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
async pipeTick(e, r) {
|
|
49
|
+
let f = this.now();
|
|
50
|
+
if (await e.updateMany(t, {
|
|
51
|
+
status: "pending",
|
|
52
|
+
claimedBy: null,
|
|
53
|
+
claimedAt: null
|
|
54
|
+
}, { where: o(s("pipe", r.key), s("status", "delivering"), l("claimedAt", new Date(f - this.stuckClaimMs))) }), (await e.query({
|
|
55
|
+
table: "_voltro_cdcout_outbox",
|
|
56
|
+
predicate: o(s("pipe", r.key), s("status", "delivering")),
|
|
57
|
+
order: [],
|
|
58
|
+
take: 1,
|
|
59
|
+
skip: void 0,
|
|
60
|
+
projection: ["id"]
|
|
61
|
+
})).length > 0) return;
|
|
62
|
+
let m = await e.query({
|
|
63
|
+
table: t,
|
|
64
|
+
predicate: o(s("pipe", r.key), s("status", "pending")),
|
|
65
|
+
order: [{
|
|
66
|
+
column: "id",
|
|
67
|
+
direction: "asc"
|
|
68
|
+
}],
|
|
69
|
+
take: r.batchSize,
|
|
70
|
+
skip: void 0,
|
|
71
|
+
projection: void 0
|
|
72
|
+
});
|
|
73
|
+
if (m.length === 0) return;
|
|
74
|
+
let h = [];
|
|
75
|
+
for (let e of m) {
|
|
76
|
+
if (n(e.nextAttemptAt) > f) break;
|
|
77
|
+
h.push(e);
|
|
78
|
+
}
|
|
79
|
+
if (h.length === 0) return;
|
|
80
|
+
let g = h.map((e) => e.id);
|
|
81
|
+
if (await e.updateMany("_voltro_cdcout_outbox", {
|
|
82
|
+
status: "delivering",
|
|
83
|
+
claimedBy: this.opts.replicaId,
|
|
84
|
+
claimedAt: new Date(f)
|
|
85
|
+
}, { where: o(s("pipe", r.key), c("id", g), s("status", "pending")) }) === 0) return;
|
|
86
|
+
let _ = await e.query({
|
|
87
|
+
table: t,
|
|
88
|
+
predicate: o(s("pipe", r.key), c("id", g), s("status", "delivering"), s("claimedBy", this.opts.replicaId)),
|
|
89
|
+
order: [{
|
|
90
|
+
column: "id",
|
|
91
|
+
direction: "asc"
|
|
92
|
+
}],
|
|
93
|
+
take: void 0,
|
|
94
|
+
skip: void 0,
|
|
95
|
+
projection: void 0
|
|
96
|
+
});
|
|
97
|
+
if (_.length === 0) return;
|
|
98
|
+
let v = _.map((e) => ({
|
|
99
|
+
table: e.sourceTable,
|
|
100
|
+
op: e.op,
|
|
101
|
+
key: e.key,
|
|
102
|
+
data: e.payload,
|
|
103
|
+
deliveryKey: e.id
|
|
104
|
+
})), y = await i.runPromise(u(i.result(d(r.sink, v, {
|
|
105
|
+
timeoutMs: this.opts.deliveryTimeoutMs,
|
|
106
|
+
pipe: r.key
|
|
107
|
+
})))), b = this.now();
|
|
108
|
+
if (a.isSuccess(y)) {
|
|
109
|
+
await e.updateMany(t, {
|
|
110
|
+
status: "delivered",
|
|
111
|
+
deliveredAt: new Date(b),
|
|
112
|
+
claimedBy: null,
|
|
113
|
+
claimedAt: null,
|
|
114
|
+
lastError: null
|
|
115
|
+
}, { where: o(s("pipe", r.key), c("id", _.map((e) => e.id)), s("status", "delivering"), s("claimedBy", this.opts.replicaId)) }), r.stats.delivered += _.length, r.stats.lastDeliveryAtMs = b;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
let x = y.failure.message;
|
|
119
|
+
for (let n of _) {
|
|
120
|
+
let i = n.attempts + 1;
|
|
121
|
+
if (i >= this.opts.maxAttempts) await e.update(t, n.id, {
|
|
122
|
+
status: "dead",
|
|
123
|
+
attempts: i,
|
|
124
|
+
claimedBy: null,
|
|
125
|
+
claimedAt: null,
|
|
126
|
+
lastError: x
|
|
127
|
+
}), r.stats.dead += 1;
|
|
128
|
+
else {
|
|
129
|
+
let r = p(i, {
|
|
130
|
+
baseMs: this.opts.backoffBaseMs,
|
|
131
|
+
maxMs: this.opts.backoffMaxMs,
|
|
132
|
+
...this.opts.random === void 0 ? {} : { random: this.opts.random }
|
|
133
|
+
});
|
|
134
|
+
await e.update(t, n.id, {
|
|
135
|
+
status: "pending",
|
|
136
|
+
attempts: i,
|
|
137
|
+
nextAttemptAt: new Date(b + r),
|
|
138
|
+
claimedBy: null,
|
|
139
|
+
claimedAt: null,
|
|
140
|
+
lastError: x
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
r.stats.failed += 1;
|
|
144
|
+
}
|
|
145
|
+
this.opts.warn?.("cdc-out delivery failed (durable retry scheduled)", {
|
|
146
|
+
pipe: r.key,
|
|
147
|
+
sink: r.sink.name,
|
|
148
|
+
records: _.length,
|
|
149
|
+
attempts: _[0] === void 0 ? 1 : _[0].attempts + 1,
|
|
150
|
+
maxAttempts: this.opts.maxAttempts,
|
|
151
|
+
reason: x
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
//#endregion
|
|
156
|
+
export { m as CdcOutEngine, d as deliverOnce, p as retryDelayMs };
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Schema as e } from "effect";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
var t = class extends e.TaggedError()("CdcConfigError", { message: e.String }) {}, n = class extends e.TaggedError()("CdcDeliveryError", {
|
|
4
|
+
sink: e.String,
|
|
5
|
+
reason: e.String
|
|
6
|
+
}) {
|
|
7
|
+
get message() {
|
|
8
|
+
return `cdc-out delivery to '${this.sink}' failed: ${this.reason}`;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { t as CdcConfigError, n as CdcDeliveryError };
|
package/dist/handoff.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/handoff.ts
|
|
2
|
+
var e = class {
|
|
3
|
+
#e = [];
|
|
4
|
+
#t = 0;
|
|
5
|
+
#n;
|
|
6
|
+
#r;
|
|
7
|
+
#i = 0;
|
|
8
|
+
constructor(e) {
|
|
9
|
+
this.#n = e.windowMs, this.#r = Math.max(1, e.maxEntries);
|
|
10
|
+
}
|
|
11
|
+
get size() {
|
|
12
|
+
return this.#e.length;
|
|
13
|
+
}
|
|
14
|
+
get droppedPending() {
|
|
15
|
+
return this.#i;
|
|
16
|
+
}
|
|
17
|
+
append(e) {
|
|
18
|
+
for (this.#e.push(e); this.#e.length > this.#r;) {
|
|
19
|
+
let e = this.#e.shift();
|
|
20
|
+
e !== void 0 && !e.enqueued && (this.#i += 1), this.#t > 0 && --this.#t;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
nextPending() {
|
|
24
|
+
for (; this.#t < this.#e.length && this.#e[this.#t].enqueued;) this.#t += 1;
|
|
25
|
+
return this.#e[this.#t];
|
|
26
|
+
}
|
|
27
|
+
pendingCount() {
|
|
28
|
+
return this.#e.reduce((e, t) => t.enqueued ? e : e + 1, 0);
|
|
29
|
+
}
|
|
30
|
+
prune(e) {
|
|
31
|
+
let t = e - this.#n, n = [], r = 0;
|
|
32
|
+
for (let [e, i] of this.#e.entries()) i.observedAtMs >= t ? (e < this.#t && (r += 1), n.push(i)) : i.enqueued || (this.#i += 1);
|
|
33
|
+
this.#e = n, this.#t = r;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { e as HandoffBuffer };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { Row } from '@voltro/database';
|
|
|
5
5
|
import { Schema } from 'effect';
|
|
6
6
|
import { TableLike } from '@voltro/database';
|
|
7
7
|
import { VoltroPlugin } from '@voltro/protocol';
|
|
8
|
+
import { YieldableError } from 'effect/Cause';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Thrown at plugin-construction time (= `app.config.ts` load = boot) when the
|
|
@@ -15,12 +16,10 @@ import { VoltroPlugin } from '@voltro/protocol';
|
|
|
15
16
|
export declare class CdcConfigError extends CdcConfigError_base {
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
declare const CdcConfigError_base: Schema.
|
|
19
|
-
readonly _tag: Schema.tag<"CdcConfigError">;
|
|
20
|
-
} & {
|
|
19
|
+
declare const CdcConfigError_base: Schema.Class<CdcConfigError, Schema.TaggedStruct<"CdcConfigError", {
|
|
21
20
|
/** Human-readable detail, naming the offending `sinks[i]` / option / value. */
|
|
22
|
-
message:
|
|
23
|
-
}>;
|
|
21
|
+
readonly message: Schema.String;
|
|
22
|
+
}>, YieldableError>;
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
25
|
* A single delivery attempt to a sink failed — a thrown sink error, a non-2xx
|
|
@@ -32,14 +31,12 @@ export declare class CdcDeliveryError extends CdcDeliveryError_base {
|
|
|
32
31
|
get message(): string;
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
declare const CdcDeliveryError_base: Schema.
|
|
36
|
-
readonly _tag: Schema.tag<"CdcDeliveryError">;
|
|
37
|
-
} & {
|
|
34
|
+
declare const CdcDeliveryError_base: Schema.Class<CdcDeliveryError, Schema.TaggedStruct<"CdcDeliveryError", {
|
|
38
35
|
/** The sink's `name`. */
|
|
39
|
-
sink:
|
|
36
|
+
readonly sink: Schema.String;
|
|
40
37
|
/** Human-readable failure detail (status line, timeout, thrown message). */
|
|
41
|
-
reason:
|
|
42
|
-
}>;
|
|
38
|
+
readonly reason: Schema.String;
|
|
39
|
+
}>, YieldableError>;
|
|
43
40
|
|
|
44
41
|
/**
|
|
45
42
|
* One instance-scoped lease, heartbeat-renewed. `isLeader` is the in-memory
|