@smthrs/journal 1.0.0-rc.16 → 1.0.0-rc.18
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 +20 -0
- package/README.md +26 -8
- package/dist/cjs/CallFact.d.ts +8 -8
- package/dist/cjs/Consensus.d.ts +26 -6
- package/dist/cjs/Consensus.d.ts.map +1 -1
- package/dist/cjs/Consensus.js +63 -22
- package/dist/cjs/Consensus.js.map +2 -2
- package/dist/cjs/EngineEvent.d.ts +23 -23
- package/dist/cjs/EngineEvent.d.ts.map +1 -1
- package/dist/cjs/EngineEvent.js +3 -2
- package/dist/cjs/EngineEvent.js.map +2 -2
- package/dist/cjs/ExecutionFact.d.ts +5 -5
- package/dist/cjs/Journal.d.ts +77 -2
- package/dist/cjs/Journal.d.ts.map +1 -1
- package/dist/cjs/Journal.js +1 -0
- package/dist/cjs/Journal.js.map +2 -2
- package/dist/cjs/JournalGeneration.d.ts +1 -1
- package/dist/cjs/JournalGeneration.d.ts.map +1 -1
- package/dist/cjs/JournalGeneration.js +10 -1
- package/dist/cjs/JournalGeneration.js.map +2 -2
- package/dist/cjs/JournalMetrics.d.ts +41 -5
- package/dist/cjs/JournalMetrics.d.ts.map +1 -1
- package/dist/cjs/JournalMetrics.js +10 -1
- package/dist/cjs/JournalMetrics.js.map +2 -2
- package/dist/cjs/RedactedLogger.d.ts.map +1 -1
- package/dist/cjs/RedactedLogger.js +5 -3
- package/dist/cjs/RedactedLogger.js.map +2 -2
- package/dist/cjs/Redaction.d.ts +18 -1
- package/dist/cjs/Redaction.d.ts.map +1 -1
- package/dist/cjs/Redaction.js +142 -12
- package/dist/cjs/Redaction.js.map +3 -3
- package/dist/cjs/SqlConsensus.d.ts +4 -3
- package/dist/cjs/SqlConsensus.d.ts.map +1 -1
- package/dist/cjs/SqlConsensus.js +92 -59
- package/dist/cjs/SqlConsensus.js.map +2 -2
- package/dist/cjs/SqlJournal.d.ts.map +1 -1
- package/dist/cjs/SqlJournal.js +370 -131
- package/dist/cjs/SqlJournal.js.map +3 -3
- package/dist/cjs/StepFact.d.ts +1 -1
- package/dist/cjs/TelemetryLog.d.ts +241 -0
- package/dist/cjs/TelemetryLog.d.ts.map +1 -0
- package/dist/cjs/TelemetryLog.js +349 -0
- package/dist/cjs/TelemetryLog.js.map +7 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/internal/EventTally.d.ts +31 -0
- package/dist/cjs/internal/EventTally.d.ts.map +1 -0
- package/dist/cjs/internal/EventTally.js +32 -0
- package/dist/cjs/internal/EventTally.js.map +7 -0
- package/dist/cjs/internal/JournalDigest.d.ts +24 -0
- package/dist/cjs/internal/JournalDigest.d.ts.map +1 -0
- package/dist/cjs/internal/JournalDigest.js +44 -0
- package/dist/cjs/internal/JournalDigest.js.map +7 -0
- package/dist/cjs/internal/JournalTestObserver.d.ts +41 -0
- package/dist/cjs/internal/JournalTestObserver.d.ts.map +1 -0
- package/dist/cjs/internal/JournalTestObserver.js +38 -0
- package/dist/cjs/internal/JournalTestObserver.js.map +7 -0
- package/dist/cjs/internal/TelemetryText.d.ts +28 -0
- package/dist/cjs/internal/TelemetryText.d.ts.map +1 -0
- package/dist/cjs/internal/TelemetryText.js +44 -0
- package/dist/cjs/internal/TelemetryText.js.map +7 -0
- package/dist/esm/CallFact.d.ts +8 -8
- package/dist/esm/Consensus.d.ts +26 -6
- package/dist/esm/Consensus.d.ts.map +1 -1
- package/dist/esm/Consensus.js +102 -21
- package/dist/esm/Consensus.js.map +1 -1
- package/dist/esm/EngineEvent.d.ts +23 -23
- package/dist/esm/EngineEvent.d.ts.map +1 -1
- package/dist/esm/EngineEvent.js +4 -2
- package/dist/esm/EngineEvent.js.map +1 -1
- package/dist/esm/ExecutionFact.d.ts +5 -5
- package/dist/esm/Journal.d.ts +77 -2
- package/dist/esm/Journal.d.ts.map +1 -1
- package/dist/esm/Journal.js +4 -1
- package/dist/esm/Journal.js.map +1 -1
- package/dist/esm/JournalGeneration.d.ts +1 -1
- package/dist/esm/JournalGeneration.d.ts.map +1 -1
- package/dist/esm/JournalGeneration.js +9 -1
- package/dist/esm/JournalGeneration.js.map +1 -1
- package/dist/esm/JournalMetrics.d.ts +41 -5
- package/dist/esm/JournalMetrics.d.ts.map +1 -1
- package/dist/esm/JournalMetrics.js +38 -6
- package/dist/esm/JournalMetrics.js.map +1 -1
- package/dist/esm/RedactedLogger.d.ts.map +1 -1
- package/dist/esm/RedactedLogger.js +9 -5
- package/dist/esm/RedactedLogger.js.map +1 -1
- package/dist/esm/Redaction.d.ts +18 -1
- package/dist/esm/Redaction.d.ts.map +1 -1
- package/dist/esm/Redaction.js +199 -16
- package/dist/esm/Redaction.js.map +1 -1
- package/dist/esm/SqlConsensus.d.ts +4 -3
- package/dist/esm/SqlConsensus.d.ts.map +1 -1
- package/dist/esm/SqlConsensus.js +34 -22
- package/dist/esm/SqlConsensus.js.map +1 -1
- package/dist/esm/SqlJournal.d.ts.map +1 -1
- package/dist/esm/SqlJournal.js +410 -108
- package/dist/esm/SqlJournal.js.map +1 -1
- package/dist/esm/StepFact.d.ts +1 -1
- package/dist/esm/TelemetryLog.d.ts +241 -0
- package/dist/esm/TelemetryLog.d.ts.map +1 -0
- package/dist/esm/TelemetryLog.js +519 -0
- package/dist/esm/TelemetryLog.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/EventTally.d.ts +31 -0
- package/dist/esm/internal/EventTally.d.ts.map +1 -0
- package/dist/esm/internal/EventTally.js +27 -0
- package/dist/esm/internal/EventTally.js.map +1 -0
- package/dist/esm/internal/JournalDigest.d.ts +24 -0
- package/dist/esm/internal/JournalDigest.d.ts.map +1 -0
- package/dist/esm/internal/JournalDigest.js +27 -0
- package/dist/esm/internal/JournalDigest.js.map +1 -0
- package/dist/esm/internal/JournalTestObserver.d.ts +41 -0
- package/dist/esm/internal/JournalTestObserver.d.ts.map +1 -0
- package/dist/esm/internal/JournalTestObserver.js +17 -0
- package/dist/esm/internal/JournalTestObserver.js.map +1 -0
- package/dist/esm/internal/TelemetryText.d.ts +28 -0
- package/dist/esm/internal/TelemetryText.d.ts.map +1 -0
- package/dist/esm/internal/TelemetryText.js +28 -0
- package/dist/esm/internal/TelemetryText.js.map +1 -0
- package/docs/README.md +6 -5
- package/docs/api.md +244 -49
- package/docs/concepts/owner-fence.md +4 -1
- package/docs/concepts/redaction.md +45 -2
- package/docs/concepts/state-event-authority.md +5 -4
- package/docs/concepts/two-channels.md +3 -2
- package/docs/guides/read-a-run.md +8 -6
- package/docs/guides/redact-log-output.md +4 -2
- package/docs/guides/testing.md +4 -2
- package/docs/installation.md +23 -9
- package/docs/quickstart.md +58 -3
- package/docs/troubleshooting.md +4 -2
- package/package.json +21 -9
- package/src/Consensus.ts +118 -28
- package/src/EngineEvent.ts +6 -2
- package/src/Journal.ts +85 -2
- package/src/JournalGeneration.ts +13 -1
- package/src/JournalMetrics.ts +53 -6
- package/src/RedactedLogger.ts +10 -4
- package/src/Redaction.ts +252 -16
- package/src/SqlConsensus.ts +135 -83
- package/src/SqlJournal.ts +604 -179
- package/src/TelemetryLog.ts +597 -0
- package/src/index.ts +6 -0
- package/src/internal/EventTally.ts +39 -0
- package/src/internal/JournalDigest.ts +30 -0
- package/src/internal/JournalTestObserver.ts +48 -0
- package/src/internal/TelemetryText.ts +31 -0
package/dist/cjs/SqlJournal.js
CHANGED
|
@@ -37,6 +37,7 @@ var Dialect = __toESM(require("@smthrs/database/Dialect"), 1);
|
|
|
37
37
|
var import_DurableWriter = require("@smthrs/database/DurableWriter");
|
|
38
38
|
var Cause = __toESM(require("effect/Cause"), 1);
|
|
39
39
|
var Clock = __toESM(require("effect/Clock"), 1);
|
|
40
|
+
var Context = __toESM(require("effect/Context"), 1);
|
|
40
41
|
var Deferred = __toESM(require("effect/Deferred"), 1);
|
|
41
42
|
var Effect = __toESM(require("effect/Effect"), 1);
|
|
42
43
|
var Exit = __toESM(require("effect/Exit"), 1);
|
|
@@ -52,7 +53,10 @@ var Semaphore = __toESM(require("effect/Semaphore"), 1);
|
|
|
52
53
|
var SqlClient = __toESM(require("effect/sql/SqlClient"), 1);
|
|
53
54
|
var Stream = __toESM(require("effect/Stream"), 1);
|
|
54
55
|
var import_Consensus = require("./Consensus.js");
|
|
56
|
+
var EventTally = __toESM(require("./internal/EventTally.js"), 1);
|
|
57
|
+
var import_JournalDigest = require("./internal/JournalDigest.js");
|
|
55
58
|
var import_JournalInternalFault = require("./internal/JournalInternalFault.js");
|
|
59
|
+
var import_JournalTestObserver = require("./internal/JournalTestObserver.js");
|
|
56
60
|
var import_Journal = require("./Journal.js");
|
|
57
61
|
var import_JournalEvent = require("./JournalEvent.js");
|
|
58
62
|
var JournalGeneration = __toESM(require("./JournalGeneration.js"), 1);
|
|
@@ -89,6 +93,11 @@ const encodeFingerprint = (value, field) => Result.flatMap(encodeJson(value, fie
|
|
|
89
93
|
catch: (cause) => error("invalid_event", `${field} could not be canonicalized`, cause)
|
|
90
94
|
}));
|
|
91
95
|
const unfenced = { _tag: "Unfenced" };
|
|
96
|
+
const guarded = { _tag: "Guarded" };
|
|
97
|
+
const FlushedRuns = Context.Reference("@smthrs/journal/SqlJournal/FlushedRuns", {
|
|
98
|
+
defaultValue: () => /* @__PURE__ */ new Set()
|
|
99
|
+
});
|
|
100
|
+
let journals = 0;
|
|
92
101
|
const decodeOwner = Schema.decodeUnknownResult(import_OwnerId.OwnerId);
|
|
93
102
|
const requireFence = (owner, method) => Result.match(decodeOwner(owner), {
|
|
94
103
|
onFailure: (cause) => Result.fail(error("invalid_event", `${method} requires a well-formed owner fence`, cause)),
|
|
@@ -178,17 +187,18 @@ const make = (options) => Effect.gen(function* () {
|
|
|
178
187
|
const readPageSize = Math.min(batchSize, import_Journal.maxEntriesLimit);
|
|
179
188
|
const sql = yield* Effect.service(SqlClient.SqlClient);
|
|
180
189
|
const writer = yield* import_DurableWriter.DurableWriter;
|
|
190
|
+
const writeRun = (runId, effect) => writer.write(flushing([runId], effect), { partition: runId });
|
|
181
191
|
const consensus = yield* import_Consensus.Consensus;
|
|
192
|
+
const observer = yield* import_JournalTestObserver.JournalTestObserver;
|
|
193
|
+
const digest = yield* import_JournalDigest.JournalDigest;
|
|
182
194
|
yield* JournalGeneration.initialize.pipe(
|
|
183
195
|
Effect.mapError((cause) => error("read_failed", "could not initialize journal generations", cause))
|
|
184
196
|
);
|
|
185
197
|
const byteCounter = new TextEncoder();
|
|
186
|
-
const contentFingerprint = (eventType, payloadJson, metaJson) =>
|
|
187
|
-
|
|
188
|
-
catch: (cause) => error("sink_failed", "could not fingerprint journal content", cause)
|
|
189
|
-
}).pipe(
|
|
198
|
+
const contentFingerprint = (eventType, payloadJson, metaJson) => digest.digest(byteCounter.encode(JSON.stringify([eventType, payloadJson, metaJson]))).pipe(
|
|
199
|
+
Effect.mapError((cause) => error("sink_failed", "could not fingerprint journal content", cause)),
|
|
190
200
|
Effect.map(
|
|
191
|
-
(
|
|
201
|
+
(digest2) => Array.from(new Uint8Array(digest2), (byte) => byte.toString(16).padStart(2, "0")).join("")
|
|
192
202
|
)
|
|
193
203
|
);
|
|
194
204
|
const queue = yield* Queue.dropping(options.capacity);
|
|
@@ -240,6 +250,8 @@ const make = (options) => Effect.gen(function* () {
|
|
|
240
250
|
pendingByRun: /* @__PURE__ */ new Map(),
|
|
241
251
|
sequences: initialized.sequences,
|
|
242
252
|
sourceSequences: initialized.sourceSequences,
|
|
253
|
+
knownSequences: /* @__PURE__ */ new Set(),
|
|
254
|
+
knownSourceSequences: /* @__PURE__ */ new Set(),
|
|
243
255
|
sourceEvents: initialized.sourceEvents,
|
|
244
256
|
flushWaiters: /* @__PURE__ */ new Set()
|
|
245
257
|
};
|
|
@@ -247,11 +259,23 @@ const make = (options) => Effect.gen(function* () {
|
|
|
247
259
|
const maintenanceScope = yield* Scope.make();
|
|
248
260
|
yield* Effect.addFinalizer((exit) => Scope.close(maintenanceScope, exit));
|
|
249
261
|
let pendingMaintenance = 0;
|
|
250
|
-
const
|
|
262
|
+
const policyRunCounts = /* @__PURE__ */ new Map();
|
|
251
263
|
const compactingRuns = /* @__PURE__ */ new Set();
|
|
252
264
|
const runBarriers = /* @__PURE__ */ new Map();
|
|
253
265
|
const runDrainWaiters = /* @__PURE__ */ new Map();
|
|
254
266
|
const activeWritesByRun = /* @__PURE__ */ new Map();
|
|
267
|
+
observer?.opened({
|
|
268
|
+
barrierCount: () => runBarriers.size,
|
|
269
|
+
policyRunCount: () => policyRunCounts.size,
|
|
270
|
+
admission: (runId) => Effect.gen(function* () {
|
|
271
|
+
const barrier = runBarriers.get(runId);
|
|
272
|
+
if (barrier === void 0) return void 0;
|
|
273
|
+
const permitAvailable = yield* barrier.semaphore.withPermitsIfAvailable(1)(Effect.void).pipe(
|
|
274
|
+
Effect.map(Option.isSome)
|
|
275
|
+
);
|
|
276
|
+
return { gateClosed: barrier.compaction !== void 0, permitAvailable };
|
|
277
|
+
})
|
|
278
|
+
});
|
|
255
279
|
const barrierFor = (runId) => {
|
|
256
280
|
const existing = runBarriers.get(runId);
|
|
257
281
|
if (existing !== void 0) return existing;
|
|
@@ -276,7 +300,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
276
300
|
for (const [runId, barrier] of runBarriers) {
|
|
277
301
|
if (barrier.users > 0 || barrier.compaction !== void 0 || state.pendingByRun.has(runId) || activeWritesByRun.has(runId) || runDrainWaiters.has(runId) || readers.has(runId) || wakes.has(runId) || compactingRuns.has(runId)) continue;
|
|
278
302
|
runBarriers.delete(runId);
|
|
279
|
-
|
|
303
|
+
policyRunCounts.delete(runId);
|
|
280
304
|
}
|
|
281
305
|
};
|
|
282
306
|
const withRunAdmission = (runId, effect) => withRunBarrier(runId, (barrier) => barrier.semaphore.withPermit(
|
|
@@ -339,11 +363,14 @@ const make = (options) => Effect.gen(function* () {
|
|
|
339
363
|
const waiters = runDrainWaiters.get(runId) ?? /* @__PURE__ */ new Set();
|
|
340
364
|
waiters.add(waiter);
|
|
341
365
|
runDrainWaiters.set(runId, waiters);
|
|
366
|
+
observer?.drainRegistered(runId);
|
|
342
367
|
return Deferred.await(waiter);
|
|
343
368
|
});
|
|
344
369
|
const withCompactionBarrier = (runId, effect) => Effect.flatMap(Effect.serviceOption(sql.transactionService), (enclosing) => withRunBarrier(runId, (barrier) => {
|
|
345
370
|
const nested = Option.isSome(enclosing);
|
|
346
371
|
const attempt = (nested ? Effect.void : awaitRunDrained(runId)).pipe(
|
|
372
|
+
// Compaction writes no new entry, so it leaves queued ones to
|
|
373
|
+
// the run's next write: they take sequences above its floor.
|
|
347
374
|
Effect.andThen(writer.write(
|
|
348
375
|
barrier.compactionLock.withPermit(Effect.uninterruptibleMask(
|
|
349
376
|
(restore) => barrier.semaphore.withPermit(Effect.sync(() => {
|
|
@@ -358,7 +385,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
358
385
|
if (nested) {
|
|
359
386
|
const outcome = yield* insertBatch([...state.pendingByRun.get(runId) ?? []]);
|
|
360
387
|
for (const { queued, commit } of outcome.commits) {
|
|
361
|
-
yield* settleCommit(queued, commit, restore);
|
|
388
|
+
yield* Effect.asVoid(settleCommit(queued, commit, restore));
|
|
362
389
|
}
|
|
363
390
|
}
|
|
364
391
|
return yield* Effect.asSome(effect);
|
|
@@ -367,7 +394,8 @@ const make = (options) => Effect.gen(function* () {
|
|
|
367
394
|
Deferred.doneUnsafe(gate, Effect.void);
|
|
368
395
|
})))
|
|
369
396
|
))
|
|
370
|
-
))
|
|
397
|
+
)),
|
|
398
|
+
{ partition: runId }
|
|
371
399
|
)),
|
|
372
400
|
Effect.flatMap(
|
|
373
401
|
(result) => Option.isSome(result) ? Effect.succeed(result.value) : attempt
|
|
@@ -403,11 +431,14 @@ const make = (options) => Effect.gen(function* () {
|
|
|
403
431
|
Deferred.doneUnsafe(waiter, exit);
|
|
404
432
|
}
|
|
405
433
|
};
|
|
406
|
-
const
|
|
434
|
+
const reportUnflushedLoss = Effect.suspend(() => {
|
|
407
435
|
if (state.sinkFailure !== void 0 && state.lossEpoch > state.flushedLossEpoch) {
|
|
408
436
|
state.flushedLossEpoch = state.lossEpoch;
|
|
409
437
|
return Effect.fail(state.sinkFailure);
|
|
410
438
|
}
|
|
439
|
+
return Effect.void;
|
|
440
|
+
});
|
|
441
|
+
const awaitQueued = Effect.suspend(() => {
|
|
411
442
|
if (state.status === "closed") {
|
|
412
443
|
return Effect.fail(error("journal_closed", "journal is closed"));
|
|
413
444
|
}
|
|
@@ -421,7 +452,11 @@ const make = (options) => Effect.gen(function* () {
|
|
|
421
452
|
state.flushWaiters.delete(waiter);
|
|
422
453
|
}))
|
|
423
454
|
);
|
|
424
|
-
})
|
|
455
|
+
});
|
|
456
|
+
const flushInternal = reportUnflushedLoss.pipe(
|
|
457
|
+
Effect.andThen(awaitQueued),
|
|
458
|
+
Effect.ensuring(Effect.sync(retireQuiescentRuns))
|
|
459
|
+
);
|
|
425
460
|
const prepare = (input, emittedAtMs) => Result.gen(function* () {
|
|
426
461
|
if (state.status !== "open") {
|
|
427
462
|
return yield* Result.fail(error("journal_closed", "journal is closed"));
|
|
@@ -485,6 +520,14 @@ const make = (options) => Effect.gen(function* () {
|
|
|
485
520
|
const cached = state.sourceEvents.get(sourceEventKey(validated.runId, validated.sourceId, sourceSeq));
|
|
486
521
|
return cached === void 0 ? Result.succeed(void 0) : compareSourceEvent(prepared, sourceSeq, cached);
|
|
487
522
|
};
|
|
523
|
+
const committedDuplicate = (prepared) => {
|
|
524
|
+
const sourceSeq = prepared.validated.sourceSeq;
|
|
525
|
+
if (sourceSeq === void 0) return false;
|
|
526
|
+
const cached = state.sourceEvents.get(
|
|
527
|
+
sourceEventKey(prepared.validated.runId, prepared.validated.sourceId, sourceSeq)
|
|
528
|
+
);
|
|
529
|
+
return cached?.status === "committed" && Result.isSuccess(compareSourceEvent(prepared, sourceSeq, cached));
|
|
530
|
+
};
|
|
488
531
|
const admitQueued = (prepared, floors, emittedAtMs) => Effect.suspend(() => {
|
|
489
532
|
if (state.status !== "open") {
|
|
490
533
|
return Effect.fail(error("journal_closed", "journal is closed"));
|
|
@@ -523,6 +566,8 @@ const make = (options) => Effect.gen(function* () {
|
|
|
523
566
|
const seq = nextSeq;
|
|
524
567
|
raiseSequenceFloor(validated.runId, seq);
|
|
525
568
|
state.sourceSequences.set(key, Math.max(nextSourceSeq, sourceSeq + 1));
|
|
569
|
+
state.knownSequences.add(validated.runId);
|
|
570
|
+
if (validated.sourceSeq === void 0) state.knownSourceSequences.add(key);
|
|
526
571
|
const queued = {
|
|
527
572
|
runId: validated.runId,
|
|
528
573
|
seq,
|
|
@@ -649,6 +694,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
649
694
|
yield* Effect.annotateCurrentSpan({
|
|
650
695
|
runId: options2.runId,
|
|
651
696
|
limit: options2.limit,
|
|
697
|
+
order: options2.order ?? "asc",
|
|
652
698
|
...options2.eventTypes === void 0 ? {} : { eventTypes: options2.eventTypes },
|
|
653
699
|
...options2.after === void 0 ? {} : { after: options2.after }
|
|
654
700
|
});
|
|
@@ -662,14 +708,14 @@ const make = (options) => Effect.gen(function* () {
|
|
|
662
708
|
event_type, payload_json, meta_json
|
|
663
709
|
FROM flows_journal_events
|
|
664
710
|
WHERE run_id = ${options2.runId} AND seq > ${after}
|
|
665
|
-
ORDER BY seq ASC
|
|
711
|
+
ORDER BY seq ${options2.order === "desc" ? sql.literal("DESC") : sql.literal("ASC")}
|
|
666
712
|
LIMIT ${options2.limit + 1}
|
|
667
713
|
` : sql`
|
|
668
714
|
SELECT run_id, seq, event_id, source_id, source_seq, emitted_at_ms,
|
|
669
715
|
event_type, payload_json, meta_json
|
|
670
716
|
FROM flows_journal_events ${Dialect.indexHint(sql, "flows_journal_events_run_event_type_idx")}
|
|
671
717
|
WHERE run_id = ${options2.runId} AND ${sql.in("event_type", options2.eventTypes)} AND seq > ${after}
|
|
672
|
-
ORDER BY seq ASC
|
|
718
|
+
ORDER BY seq ${options2.order === "desc" ? sql.literal("DESC") : sql.literal("ASC")}
|
|
673
719
|
LIMIT ${options2.limit + 1}
|
|
674
720
|
`).pipe(
|
|
675
721
|
Effect.mapError((cause) => error("read_failed", "durable journal read failed", cause))
|
|
@@ -697,21 +743,22 @@ const make = (options) => Effect.gen(function* () {
|
|
|
697
743
|
const subscribeRun = (runId) => Effect.gen(function* () {
|
|
698
744
|
const wake = yield* PubSub.sliding(1);
|
|
699
745
|
const subscription = yield* PubSub.subscribe(wake);
|
|
746
|
+
const follower = { wake, lost: void 0 };
|
|
700
747
|
yield* Effect.acquireRelease(
|
|
701
748
|
Effect.sync(() => {
|
|
702
749
|
const subscribers = wakes.get(runId) ?? /* @__PURE__ */ new Set();
|
|
703
|
-
subscribers.add(
|
|
750
|
+
subscribers.add(follower);
|
|
704
751
|
wakes.set(runId, subscribers);
|
|
705
752
|
}),
|
|
706
753
|
() => Effect.sync(() => {
|
|
707
754
|
const subscribers = wakes.get(runId);
|
|
708
|
-
subscribers?.delete(
|
|
755
|
+
subscribers?.delete(follower);
|
|
709
756
|
if (subscribers?.size === 0) {
|
|
710
757
|
wakes.delete(runId);
|
|
711
758
|
}
|
|
712
759
|
}).pipe(Effect.andThen(PubSub.shutdown(wake)))
|
|
713
760
|
);
|
|
714
|
-
return subscription;
|
|
761
|
+
return { subscription, follower };
|
|
715
762
|
});
|
|
716
763
|
const stream = (streamOptions) => Stream.unwrap(
|
|
717
764
|
Effect.fn("Journal.stream")(function* () {
|
|
@@ -723,7 +770,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
723
770
|
decodeStreamOptions(streamOptions),
|
|
724
771
|
(cause) => error("invalid_event", "stream options violate the journal read contract", cause)
|
|
725
772
|
));
|
|
726
|
-
const wake = yield* subscribeRun(streamOptions.runId);
|
|
773
|
+
const { follower, subscription: wake } = yield* subscribeRun(streamOptions.runId);
|
|
727
774
|
const reader = { cursor: streamOptions.afterSequence ?? -1 };
|
|
728
775
|
yield* Effect.acquireRelease(
|
|
729
776
|
Effect.sync(() => {
|
|
@@ -739,9 +786,8 @@ const make = (options) => Effect.gen(function* () {
|
|
|
739
786
|
}
|
|
740
787
|
})
|
|
741
788
|
);
|
|
742
|
-
const subscribedLossEpoch = state.lossEpoch;
|
|
743
789
|
const readPages = (initialCursor) => Stream.paginate(initialCursor, (cursor) => Effect.suspend(
|
|
744
|
-
() =>
|
|
790
|
+
() => follower.lost !== void 0 ? Effect.fail(follower.lost) : readPage({
|
|
745
791
|
runId: streamOptions.runId,
|
|
746
792
|
...cursor < 0 ? {} : { after: cursor },
|
|
747
793
|
limit: readPageSize
|
|
@@ -761,9 +807,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
761
807
|
const historical = readPages(reader.cursor);
|
|
762
808
|
const live = Stream.fromEffectRepeat(
|
|
763
809
|
Effect.raceFirst(PubSub.take(wake), Effect.sleep("1 second"))
|
|
764
|
-
).pipe(Stream.flatMap(
|
|
765
|
-
() => readPages(reader.cursor)
|
|
766
|
-
));
|
|
810
|
+
).pipe(Stream.flatMap(() => readPages(reader.cursor)));
|
|
767
811
|
return Stream.concat(historical, live);
|
|
768
812
|
})()
|
|
769
813
|
);
|
|
@@ -816,13 +860,13 @@ const make = (options) => Effect.gen(function* () {
|
|
|
816
860
|
const insertOne = (queued, fence, enforceCompactionFloor = false) => Effect.gen(function* () {
|
|
817
861
|
if (fence._tag === "Owned") {
|
|
818
862
|
yield* fenceGuard(queued.runId, fence.owner);
|
|
819
|
-
} else if (!enforceCompactionFloor) {
|
|
863
|
+
} else if (fence._tag === "Unfenced" && !enforceCompactionFloor) {
|
|
820
864
|
const duplicate = yield* selectExisting(queued);
|
|
821
865
|
if (duplicate !== void 0) {
|
|
822
866
|
return duplicate;
|
|
823
867
|
}
|
|
824
868
|
}
|
|
825
|
-
const insert = enforceCompactionFloor ? sql`
|
|
869
|
+
const insert = !enforceCompactionFloor ? sql`
|
|
826
870
|
INSERT INTO flows_journal_events (
|
|
827
871
|
run_id, seq, event_id, source_id, source_seq, emitted_at_ms,
|
|
828
872
|
event_type, payload_json, meta_json
|
|
@@ -839,11 +883,6 @@ const make = (options) => Effect.gen(function* () {
|
|
|
839
883
|
${queued.payloadJson},
|
|
840
884
|
${queued.metaJson}
|
|
841
885
|
WHERE NOT EXISTS (
|
|
842
|
-
SELECT 1 FROM flows_journal_checkpoints
|
|
843
|
-
WHERE run_id = ${queued.runId}
|
|
844
|
-
AND compacted_at_ms IS NOT NULL
|
|
845
|
-
AND seq >= ${queued.seq}
|
|
846
|
-
) AND NOT EXISTS (
|
|
847
886
|
SELECT 1 FROM flows_journal_events
|
|
848
887
|
WHERE run_id = ${queued.runId} AND seq >= ${Number.MAX_SAFE_INTEGER - 1}
|
|
849
888
|
)
|
|
@@ -854,9 +893,11 @@ const make = (options) => Effect.gen(function* () {
|
|
|
854
893
|
INSERT INTO flows_journal_events (
|
|
855
894
|
run_id, seq, event_id, source_id, source_seq, emitted_at_ms,
|
|
856
895
|
event_type, payload_json, meta_json
|
|
857
|
-
)
|
|
896
|
+
)
|
|
897
|
+
SELECT
|
|
858
898
|
${queued.runId},
|
|
859
|
-
${queued.seq},
|
|
899
|
+
(SELECT ${Dialect.greatest(sql)}(${queued.seq}, COALESCE(MAX(seq), -1) + 1)
|
|
900
|
+
FROM flows_journal_events WHERE run_id = ${queued.runId}),
|
|
860
901
|
${queued.eventId},
|
|
861
902
|
${queued.sourceId},
|
|
862
903
|
${queued.sourceSeq},
|
|
@@ -864,6 +905,14 @@ const make = (options) => Effect.gen(function* () {
|
|
|
864
905
|
${queued.eventType},
|
|
865
906
|
${queued.payloadJson},
|
|
866
907
|
${queued.metaJson}
|
|
908
|
+
WHERE NOT EXISTS (
|
|
909
|
+
SELECT 1 FROM flows_journal_checkpoints
|
|
910
|
+
WHERE run_id = ${queued.runId}
|
|
911
|
+
AND compacted_at_ms IS NOT NULL
|
|
912
|
+
AND seq >= ${queued.seq}
|
|
913
|
+
) AND NOT EXISTS (
|
|
914
|
+
SELECT 1 FROM flows_journal_events
|
|
915
|
+
WHERE run_id = ${queued.runId} AND seq >= ${Number.MAX_SAFE_INTEGER - 1}
|
|
867
916
|
)
|
|
868
917
|
ON CONFLICT DO NOTHING
|
|
869
918
|
RETURNING run_id, seq, event_id, source_id, source_seq, emitted_at_ms,
|
|
@@ -871,10 +920,10 @@ const make = (options) => Effect.gen(function* () {
|
|
|
871
920
|
`;
|
|
872
921
|
const inserted = yield* insert;
|
|
873
922
|
if (inserted.length > 0) {
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
};
|
|
923
|
+
const entry = yield* decodeRow(inserted[0]);
|
|
924
|
+
raiseSequenceFloor(entry.runId, entry.seq);
|
|
925
|
+
state.knownSequences.add(entry.runId);
|
|
926
|
+
return { entry, inserted: true };
|
|
878
927
|
}
|
|
879
928
|
const racedDuplicate = yield* selectExisting(queued);
|
|
880
929
|
if (racedDuplicate !== void 0) {
|
|
@@ -917,10 +966,15 @@ const make = (options) => Effect.gen(function* () {
|
|
|
917
966
|
}
|
|
918
967
|
return { commits, losses };
|
|
919
968
|
});
|
|
920
|
-
const persistBatch = (batch) => writer.write(
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
969
|
+
const persistBatch = (batch) => writer.write(
|
|
970
|
+
withRunPermits(
|
|
971
|
+
batch.map((queued) => queued.runId),
|
|
972
|
+
insertBatch(batch)
|
|
973
|
+
),
|
|
974
|
+
// A batch of one run's entries writes that run's partition; a batch
|
|
975
|
+
// across runs serializes against every writer.
|
|
976
|
+
{ partition: batch.every((queued) => queued.runId === batch[0].runId) ? batch[0].runId : void 0 }
|
|
977
|
+
).pipe(
|
|
924
978
|
// Every per-entry `JournalError` is settled inside the transaction
|
|
925
979
|
// above, so the only failure that escapes is the transaction itself:
|
|
926
980
|
// a database outage, which is what `sink_failed` names.
|
|
@@ -936,7 +990,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
936
990
|
Effect.andThen(
|
|
937
991
|
Effect.forEach(
|
|
938
992
|
wakes.get(commit.entry.runId) ?? [],
|
|
939
|
-
(
|
|
993
|
+
(follower) => PubSub.publish(follower.wake, void 0),
|
|
940
994
|
{ discard: true }
|
|
941
995
|
)
|
|
942
996
|
),
|
|
@@ -944,7 +998,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
944
998
|
);
|
|
945
999
|
},
|
|
946
1000
|
{ discard: true }
|
|
947
|
-
);
|
|
1001
|
+
).pipe(Effect.andThen(EventTally.record(commits.filter((commit) => commit.inserted).length)));
|
|
948
1002
|
const rememberCommitted = (queued, seq) => {
|
|
949
1003
|
retain(sourceEventKey(queued.runId, queued.sourceId, queued.sourceSeq), {
|
|
950
1004
|
seq,
|
|
@@ -973,8 +1027,8 @@ const make = (options) => Effect.gen(function* () {
|
|
|
973
1027
|
const ensureFloors = (input) => Effect.suspend(() => {
|
|
974
1028
|
const runId = input.runId;
|
|
975
1029
|
const key = sourceKey(runId, input.sourceId);
|
|
976
|
-
const seq = state.sequences.get(runId);
|
|
977
|
-
const sourceSeq = input.sourceSeq
|
|
1030
|
+
const seq = state.knownSequences.has(runId) ? state.sequences.get(runId) : void 0;
|
|
1031
|
+
const sourceSeq = input.sourceSeq !== void 0 ? 0 : state.knownSourceSequences.has(key) ? state.sourceSequences.get(key) : void 0;
|
|
978
1032
|
if (seq !== void 0 && sourceSeq !== void 0) {
|
|
979
1033
|
return Effect.succeed({ seq, sourceSeq });
|
|
980
1034
|
}
|
|
@@ -1006,12 +1060,52 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1006
1060
|
yield* (0, import_DurableWriter.afterCommit)(mandatory.pipe(Effect.andThen(maintenance)), sql);
|
|
1007
1061
|
return Effect.void;
|
|
1008
1062
|
});
|
|
1009
|
-
const transact = (effect) => writer.write(effect).pipe(
|
|
1063
|
+
const transact = (effect, options2) => writer.write(options2?.partition === void 0 ? effect : flushing([options2.partition], effect), options2).pipe(
|
|
1010
1064
|
Effect.catchIf(
|
|
1011
1065
|
(cause) => cause instanceof import_DurableWriter.DatabaseError,
|
|
1012
1066
|
(cause) => Effect.fail(error("sink_failed", "journal transaction failed", cause))
|
|
1013
1067
|
)
|
|
1014
1068
|
);
|
|
1069
|
+
const reserve = (validated, durableSeq, durableSourceSeq) => allocation.withPermit(Effect.fromResult(Result.gen(function* () {
|
|
1070
|
+
const key = sourceKey(validated.runId, validated.sourceId);
|
|
1071
|
+
const sourceSeq = validated.sourceSeq ?? Math.max(
|
|
1072
|
+
durableSourceSeq,
|
|
1073
|
+
state.sourceSequences.get(key) ?? 0
|
|
1074
|
+
);
|
|
1075
|
+
if (!Number.isSafeInteger(sourceSeq) || sourceSeq < 0 || sourceSeq === Number.MAX_SAFE_INTEGER) {
|
|
1076
|
+
return yield* Result.fail(
|
|
1077
|
+
error("invalid_event", "journal sequence is outside the allocatable safe integer range")
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
const seq = Math.max(
|
|
1081
|
+
durableSeq,
|
|
1082
|
+
state.sequences.get(validated.runId) ?? 0
|
|
1083
|
+
);
|
|
1084
|
+
if (!Number.isSafeInteger(seq) || seq === Number.MAX_SAFE_INTEGER) {
|
|
1085
|
+
return yield* Result.fail(
|
|
1086
|
+
error("invalid_event", "journal sequence is outside the allocatable safe integer range")
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
raiseSequenceFloor(validated.runId, seq);
|
|
1090
|
+
raiseSourceSequenceFloor(validated.runId, validated.sourceId, sourceSeq);
|
|
1091
|
+
if (validated.sourceSeq === void 0) state.knownSourceSequences.add(key);
|
|
1092
|
+
return { seq, sourceSeq };
|
|
1093
|
+
})));
|
|
1094
|
+
const queuedOf = (prepared, emittedAtMs, seq, sourceSeq) => {
|
|
1095
|
+
const { metaJson, payloadJson, validated } = prepared;
|
|
1096
|
+
return {
|
|
1097
|
+
runId: validated.runId,
|
|
1098
|
+
seq,
|
|
1099
|
+
eventId: (0, import_JournalEvent.makeEventId)(validated.runId, validated.sourceId, sourceSeq),
|
|
1100
|
+
sourceId: validated.sourceId,
|
|
1101
|
+
sourceSeq,
|
|
1102
|
+
emittedAtMs,
|
|
1103
|
+
eventType: validated.eventType,
|
|
1104
|
+
payloadJson,
|
|
1105
|
+
metaJson,
|
|
1106
|
+
dedupe: validated.dedupe ?? "content"
|
|
1107
|
+
};
|
|
1108
|
+
};
|
|
1015
1109
|
const writeDurable = (input, fence) => Effect.uninterruptibleMask(
|
|
1016
1110
|
(restoreMaintenance) => restoreMaintenance(Effect.gen(function* () {
|
|
1017
1111
|
yield* Effect.annotateCurrentSpan({
|
|
@@ -1022,54 +1116,21 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1022
1116
|
const emittedAtMs = yield* Clock.currentTimeMillis.pipe(Effect.map(Math.floor));
|
|
1023
1117
|
const prepared = yield* Effect.fromResult(prepare(input, emittedAtMs));
|
|
1024
1118
|
const committed = yield* Effect.gen(function* () {
|
|
1025
|
-
const {
|
|
1119
|
+
const { validated } = prepared;
|
|
1026
1120
|
return yield* Effect.uninterruptibleMask(
|
|
1027
|
-
(restore) => restore(
|
|
1121
|
+
(restore) => restore(writeRun(
|
|
1028
1122
|
prepared.validated.runId,
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
const
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
error("invalid_event", "journal sequence is outside the allocatable safe integer range")
|
|
1041
|
-
);
|
|
1042
|
-
}
|
|
1043
|
-
const seq2 = Math.max(
|
|
1044
|
-
durableSeq,
|
|
1045
|
-
state.sequences.get(validated.runId) ?? 0
|
|
1046
|
-
);
|
|
1047
|
-
if (!Number.isSafeInteger(seq2) || seq2 === Number.MAX_SAFE_INTEGER) {
|
|
1048
|
-
return yield* Result.fail(
|
|
1049
|
-
error("invalid_event", "journal sequence is outside the allocatable safe integer range")
|
|
1050
|
-
);
|
|
1051
|
-
}
|
|
1052
|
-
raiseSequenceFloor(validated.runId, seq2);
|
|
1053
|
-
raiseSourceSequenceFloor(validated.runId, validated.sourceId, sourceSeq2);
|
|
1054
|
-
return { seq: seq2, sourceSeq: sourceSeq2 };
|
|
1055
|
-
})));
|
|
1056
|
-
const { seq, sourceSeq } = reserved;
|
|
1057
|
-
const queued = {
|
|
1058
|
-
runId: validated.runId,
|
|
1059
|
-
seq,
|
|
1060
|
-
eventId: (0, import_JournalEvent.makeEventId)(validated.runId, validated.sourceId, sourceSeq),
|
|
1061
|
-
sourceId: validated.sourceId,
|
|
1062
|
-
sourceSeq,
|
|
1063
|
-
emittedAtMs,
|
|
1064
|
-
eventType: validated.eventType,
|
|
1065
|
-
payloadJson,
|
|
1066
|
-
metaJson,
|
|
1067
|
-
dedupe: validated.dedupe ?? "content"
|
|
1068
|
-
};
|
|
1069
|
-
const commit = yield* insertOne(queued, fence);
|
|
1070
|
-
return { commit, queued, sourceSeq };
|
|
1071
|
-
})
|
|
1072
|
-
))).pipe(
|
|
1123
|
+
withActiveRunWrite(
|
|
1124
|
+
prepared.validated.runId,
|
|
1125
|
+
Effect.gen(function* () {
|
|
1126
|
+
const durableSourceSeq = validated.sourceSeq === void 0 ? yield* nextDurable("source_seq", validated.runId, validated.sourceId) : 0;
|
|
1127
|
+
const { seq, sourceSeq } = yield* reserve(validated, 0, durableSourceSeq);
|
|
1128
|
+
const queued = queuedOf(prepared, emittedAtMs, seq, sourceSeq);
|
|
1129
|
+
const commit = yield* insertOne(queued, fence);
|
|
1130
|
+
return { commit, queued, sourceSeq };
|
|
1131
|
+
})
|
|
1132
|
+
)
|
|
1133
|
+
)).pipe(
|
|
1073
1134
|
/**
|
|
1074
1135
|
* `writer.write` is a retrying transaction: its body replays on
|
|
1075
1136
|
* `SQLITE_BUSY(_SNAPSHOT)` and can still abort at COMMIT after the
|
|
@@ -1121,6 +1182,139 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1121
1182
|
);
|
|
1122
1183
|
const emitDurableUnfenced = Effect.fn("Journal.emitDurableUnfenced")((input) => writeDurable(input, unfenced));
|
|
1123
1184
|
const emitLossy = queuedEmit;
|
|
1185
|
+
const journalKey = `${++journals}:`;
|
|
1186
|
+
const deferred = /* @__PURE__ */ new Map();
|
|
1187
|
+
let deferredFailure;
|
|
1188
|
+
const discard = (runId, drop) => {
|
|
1189
|
+
const kept = (deferred.get(runId) ?? []).filter((entry) => !drop(entry));
|
|
1190
|
+
if (kept.length === 0) deferred.delete(runId);
|
|
1191
|
+
else deferred.set(runId, kept);
|
|
1192
|
+
};
|
|
1193
|
+
const flushRun = (runId) => Effect.gen(function* () {
|
|
1194
|
+
const owners = [];
|
|
1195
|
+
for (const { owner } of deferred.get(runId)) {
|
|
1196
|
+
if (!owners.some((other) => (0, import_Consensus.sameOwner)(other, owner))) owners.push(owner);
|
|
1197
|
+
}
|
|
1198
|
+
for (const owner of owners) {
|
|
1199
|
+
yield* fenceGuard(runId, owner).pipe(
|
|
1200
|
+
Effect.catchIf(
|
|
1201
|
+
(cause) => cause.code === "fence_lost",
|
|
1202
|
+
() => Effect.sync(() => discard(runId, (entry) => (0, import_Consensus.sameOwner)(entry.owner, owner)))
|
|
1203
|
+
)
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1206
|
+
const batch = [...deferred.get(runId) ?? []];
|
|
1207
|
+
if (batch.length === 0) return;
|
|
1208
|
+
for (const entry of batch) {
|
|
1209
|
+
yield* Effect.gen(function* () {
|
|
1210
|
+
const validated = entry.sourceSeq === void 0 ? entry.prepared.validated : { ...entry.prepared.validated, sourceSeq: entry.sourceSeq };
|
|
1211
|
+
const durableSourceSeq = validated.sourceSeq === void 0 ? yield* nextDurable("source_seq", validated.runId, validated.sourceId) : 0;
|
|
1212
|
+
const { seq, sourceSeq } = yield* reserve(validated, 0, durableSourceSeq);
|
|
1213
|
+
entry.sourceSeq = sourceSeq;
|
|
1214
|
+
const queued = queuedOf(entry.prepared, entry.emittedAtMs, seq, sourceSeq);
|
|
1215
|
+
const commit = yield* insertOne(queued, guarded);
|
|
1216
|
+
yield* Effect.asVoid(settleCommit(queued, commit, (effect) => effect));
|
|
1217
|
+
yield* Metric.update(JournalMetrics.durable[commit.inserted ? "Accepted" : "Duplicate"], 1);
|
|
1218
|
+
}).pipe(
|
|
1219
|
+
Effect.catchIf(isJournalError, (cause) => Effect.asVoid((0, import_DurableWriter.afterCommit)(
|
|
1220
|
+
Effect.gen(function* () {
|
|
1221
|
+
if (!deferred.get(runId)?.includes(entry)) return;
|
|
1222
|
+
discard(runId, (queued) => queued === entry);
|
|
1223
|
+
deferredFailure ??= cause;
|
|
1224
|
+
yield* Metric.update(
|
|
1225
|
+
Metric.withAttributes(JournalMetrics.lostEntries, {
|
|
1226
|
+
channel: "deferred",
|
|
1227
|
+
code: cause.code
|
|
1228
|
+
}),
|
|
1229
|
+
1
|
|
1230
|
+
);
|
|
1231
|
+
yield* Effect.logWarning(`journal lost 1 deferred entry (${cause.code}) for run ${runId}`);
|
|
1232
|
+
}),
|
|
1233
|
+
sql
|
|
1234
|
+
)))
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
yield* (0, import_DurableWriter.afterCommit)(Effect.sync(() => discard(runId, (entry) => batch.includes(entry))), sql);
|
|
1238
|
+
});
|
|
1239
|
+
const flushing = (runIds, effect) => Effect.gen(function* () {
|
|
1240
|
+
const flushed = yield* FlushedRuns;
|
|
1241
|
+
const due = runIds.filter((runId) => deferred.has(runId) && !flushed.has(journalKey + runId));
|
|
1242
|
+
if (due.length === 0) return yield* effect;
|
|
1243
|
+
for (const runId of due) yield* flushRun(runId);
|
|
1244
|
+
return yield* Effect.provideService(
|
|
1245
|
+
effect,
|
|
1246
|
+
FlushedRuns,
|
|
1247
|
+
/* @__PURE__ */ new Set([...flushed, ...due.map((runId) => journalKey + runId)])
|
|
1248
|
+
);
|
|
1249
|
+
});
|
|
1250
|
+
const flushDeferred = Effect.suspend(
|
|
1251
|
+
() => Effect.forEach([...deferred.keys()], (runId) => writeRun(runId, Effect.void), { discard: true })
|
|
1252
|
+
).pipe(
|
|
1253
|
+
Effect.mapError(
|
|
1254
|
+
(cause) => isJournalError(cause) ? cause : error("sink_failed", "could not write queued journal entries", cause)
|
|
1255
|
+
)
|
|
1256
|
+
);
|
|
1257
|
+
const reportDeferredFailure = Effect.suspend(() => {
|
|
1258
|
+
const failure = deferredFailure;
|
|
1259
|
+
deferredFailure = void 0;
|
|
1260
|
+
return failure === void 0 ? Effect.void : Effect.fail(failure);
|
|
1261
|
+
});
|
|
1262
|
+
const emitDeferred = Effect.fn("Journal.emitDeferred")(
|
|
1263
|
+
(input, owner) => Effect.gen(function* () {
|
|
1264
|
+
const fence = yield* Effect.fromResult(requireFence(owner, "emitDeferred"));
|
|
1265
|
+
if (Option.isSome(yield* Effect.serviceOption(sql.transactionService))) {
|
|
1266
|
+
return yield* Effect.asVoid(writeDurable(input, fence));
|
|
1267
|
+
}
|
|
1268
|
+
if (state.status !== "open") {
|
|
1269
|
+
return yield* Effect.fail(error("journal_closed", "journal is closed"));
|
|
1270
|
+
}
|
|
1271
|
+
const emittedAtMs = yield* Clock.currentTimeMillis.pipe(Effect.map(Math.floor));
|
|
1272
|
+
const prepared = yield* Effect.fromResult(prepare(input, emittedAtMs));
|
|
1273
|
+
if (committedDuplicate(prepared)) {
|
|
1274
|
+
return yield* Metric.update(JournalMetrics.durable.Duplicate, 1);
|
|
1275
|
+
}
|
|
1276
|
+
const runId = prepared.validated.runId;
|
|
1277
|
+
deferred.set(runId, [...deferred.get(runId) ?? [], {
|
|
1278
|
+
prepared,
|
|
1279
|
+
emittedAtMs,
|
|
1280
|
+
owner: fence.owner
|
|
1281
|
+
}]);
|
|
1282
|
+
})
|
|
1283
|
+
);
|
|
1284
|
+
const recall = Effect.fn("Journal.recall")(
|
|
1285
|
+
(runId) => Effect.gen(function* () {
|
|
1286
|
+
const id = yield* Effect.fromResult(Result.mapError(
|
|
1287
|
+
decodeRunId(runId),
|
|
1288
|
+
(cause) => error("invalid_event", "run id violates the journal contract", cause)
|
|
1289
|
+
));
|
|
1290
|
+
yield* Effect.annotateCurrentSpan({ runId: id });
|
|
1291
|
+
const rows = yield* sql`
|
|
1292
|
+
SELECT run_id, seq, event_id, source_id, source_seq, emitted_at_ms,
|
|
1293
|
+
event_type, payload_json, meta_json
|
|
1294
|
+
FROM flows_journal_events
|
|
1295
|
+
WHERE run_id = ${id}
|
|
1296
|
+
ORDER BY seq DESC
|
|
1297
|
+
LIMIT ${sourceEventCache}
|
|
1298
|
+
`.pipe(Effect.mapError((cause) => error("read_failed", "could not recall the run's journal entries", cause)));
|
|
1299
|
+
const entries = yield* Effect.forEach(rows, decodeRow);
|
|
1300
|
+
for (const entry of entries.reverse()) {
|
|
1301
|
+
retain(sourceEventKey(entry.runId, entry.sourceId, entry.sourceSeq), {
|
|
1302
|
+
seq: entry.seq,
|
|
1303
|
+
eventType: entry.eventType,
|
|
1304
|
+
payloadJson: yield* Effect.fromResult(encodeFingerprint(entry.payload, "payload")),
|
|
1305
|
+
metaJson: yield* Effect.fromResult(encodeFingerprint(entry.meta, "meta")),
|
|
1306
|
+
status: "committed"
|
|
1307
|
+
});
|
|
1308
|
+
}
|
|
1309
|
+
yield* Effect.annotateCurrentSpan({ recalled: entries.length });
|
|
1310
|
+
})
|
|
1311
|
+
);
|
|
1312
|
+
const recorded = (input) => Clock.currentTimeMillis.pipe(
|
|
1313
|
+
Effect.map((now) => {
|
|
1314
|
+
const prepared = prepare(input, Math.floor(now));
|
|
1315
|
+
return Result.isSuccess(prepared) && committedDuplicate(prepared.success);
|
|
1316
|
+
})
|
|
1317
|
+
);
|
|
1124
1318
|
const checkpointInternal = (checkpointOptions, fence) => Effect.gen(function* () {
|
|
1125
1319
|
yield* Effect.annotateCurrentSpan({
|
|
1126
1320
|
runId: checkpointOptions.runId,
|
|
@@ -1138,45 +1332,48 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1138
1332
|
)
|
|
1139
1333
|
);
|
|
1140
1334
|
const createdAtMs = yield* Clock.currentTimeMillis.pipe(Effect.map(Math.floor));
|
|
1141
|
-
return yield*
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1335
|
+
return yield* writeRun(
|
|
1336
|
+
checkpointOptions.runId,
|
|
1337
|
+
Effect.gen(function* () {
|
|
1338
|
+
if (fence._tag === "Owned") {
|
|
1339
|
+
yield* fenceGuard(checkpointOptions.runId, fence.owner);
|
|
1340
|
+
}
|
|
1341
|
+
const target = yield* sql`
|
|
1146
1342
|
SELECT 1 AS ok FROM flows_journal_events
|
|
1147
1343
|
WHERE run_id = ${checkpointOptions.runId} AND seq = ${checkpointOptions.seq}
|
|
1148
1344
|
`;
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1345
|
+
if (target.length === 0) {
|
|
1346
|
+
return yield* Effect.fail(error(
|
|
1347
|
+
"checkpoint_invalid",
|
|
1348
|
+
`checkpoint sequence ${checkpointOptions.seq} names no committed entry of run ${checkpointOptions.runId}`
|
|
1349
|
+
));
|
|
1350
|
+
}
|
|
1351
|
+
const floor = yield* compactionFloor(checkpointOptions.runId);
|
|
1352
|
+
if (floor !== void 0 && checkpointOptions.seq <= floor) {
|
|
1353
|
+
return yield* Effect.fail(
|
|
1354
|
+
new import_Journal.JournalError({
|
|
1355
|
+
code: "checkpoint_invalid",
|
|
1356
|
+
message: `run ${checkpointOptions.runId} is already compacted through sequence ${floor}`,
|
|
1357
|
+
checkpointSeq: floor
|
|
1358
|
+
})
|
|
1359
|
+
);
|
|
1360
|
+
}
|
|
1361
|
+
yield* sql`
|
|
1166
1362
|
INSERT INTO flows_journal_checkpoints (run_id, seq, state_json, created_at_ms)
|
|
1167
1363
|
VALUES (${checkpointOptions.runId}, ${checkpointOptions.seq}, ${stateJson}, ${createdAtMs})
|
|
1168
1364
|
ON CONFLICT (run_id, seq) DO UPDATE SET
|
|
1169
1365
|
state_json = excluded.state_json,
|
|
1170
1366
|
created_at_ms = excluded.created_at_ms
|
|
1171
1367
|
`;
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1368
|
+
return new import_Journal.Checkpoint({
|
|
1369
|
+
runId: checkpointOptions.runId,
|
|
1370
|
+
seq: checkpointOptions.seq,
|
|
1371
|
+
state: receiptState,
|
|
1372
|
+
createdAtMs,
|
|
1373
|
+
compactedAtMs: null
|
|
1374
|
+
});
|
|
1375
|
+
})
|
|
1376
|
+
).pipe(
|
|
1180
1377
|
Effect.mapError(
|
|
1181
1378
|
(cause) => isJournalError(cause) ? cause : error("sink_failed", "durable checkpoint write failed", cause)
|
|
1182
1379
|
)
|
|
@@ -1338,11 +1535,11 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1338
1535
|
});
|
|
1339
1536
|
yield* checkpointInternal({ runId, seq: upTo, state: captured }, unfenced);
|
|
1340
1537
|
yield* compactInternal({ runId, upTo }, unfenced);
|
|
1341
|
-
|
|
1538
|
+
policyRunCounts.set(runId, yield* countEntries(runId));
|
|
1342
1539
|
}).pipe(
|
|
1343
1540
|
Effect.catchCause(
|
|
1344
1541
|
(cause) => Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.sync(() => {
|
|
1345
|
-
|
|
1542
|
+
policyRunCounts.set(runId, 0);
|
|
1346
1543
|
}).pipe(
|
|
1347
1544
|
Effect.andThen(
|
|
1348
1545
|
Effect.logWarning("journal auto-compaction failed; retrying after the next threshold", cause)
|
|
@@ -1361,9 +1558,9 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1361
1558
|
if (compactingRuns.has(runId)) {
|
|
1362
1559
|
return false;
|
|
1363
1560
|
}
|
|
1364
|
-
const known =
|
|
1561
|
+
const known = policyRunCounts.get(runId);
|
|
1365
1562
|
const current = known === void 0 ? yield* countEntries(runId) : known + committed;
|
|
1366
|
-
|
|
1563
|
+
policyRunCounts.set(runId, current);
|
|
1367
1564
|
if (current < policy.entryThreshold) {
|
|
1368
1565
|
return false;
|
|
1369
1566
|
}
|
|
@@ -1410,9 +1607,10 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1410
1607
|
state.flushedLossEpoch = state.lossEpoch;
|
|
1411
1608
|
}
|
|
1412
1609
|
completeFlushWaiters(Effect.fail(cause));
|
|
1413
|
-
for (const
|
|
1414
|
-
for (const
|
|
1415
|
-
|
|
1610
|
+
for (const runId of new Set(batch.map((queued) => queued.runId))) {
|
|
1611
|
+
for (const follower of wakes.get(runId) ?? []) {
|
|
1612
|
+
follower.lost = cause;
|
|
1613
|
+
PubSub.publishUnsafe(follower.wake, void 0);
|
|
1416
1614
|
}
|
|
1417
1615
|
}
|
|
1418
1616
|
};
|
|
@@ -1488,8 +1686,10 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1488
1686
|
yield* Effect.sync(() => {
|
|
1489
1687
|
state.status = "closing";
|
|
1490
1688
|
});
|
|
1491
|
-
yield*
|
|
1492
|
-
|
|
1689
|
+
yield* Effect.forEach(
|
|
1690
|
+
[reportUnflushedLoss, awaitQueued, flushDeferred, reportDeferredFailure],
|
|
1691
|
+
(step) => step.pipe(Effect.catch((cause) => Effect.logWarning("journal final flush failed while closing", cause))),
|
|
1692
|
+
{ discard: true }
|
|
1493
1693
|
);
|
|
1494
1694
|
yield* Effect.sync(() => {
|
|
1495
1695
|
state.status = "closed";
|
|
@@ -1525,16 +1725,44 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1525
1725
|
yield* JournalGeneration.onTruncate((runIds) => {
|
|
1526
1726
|
for (const runId of runIds) {
|
|
1527
1727
|
state.sequences.delete(runId);
|
|
1728
|
+
state.knownSequences.delete(runId);
|
|
1528
1729
|
const prefix = `${runId.length}:${runId}`;
|
|
1529
1730
|
for (const key of state.sourceSequences.keys()) {
|
|
1530
1731
|
if (key.startsWith(prefix)) state.sourceSequences.delete(key);
|
|
1531
1732
|
}
|
|
1733
|
+
for (const key of state.knownSourceSequences) {
|
|
1734
|
+
if (key.startsWith(prefix)) state.knownSourceSequences.delete(key);
|
|
1735
|
+
}
|
|
1532
1736
|
for (const key of state.sourceEvents.keys()) {
|
|
1533
1737
|
if (key.startsWith(prefix)) state.sourceEvents.delete(key);
|
|
1534
1738
|
}
|
|
1535
1739
|
}
|
|
1536
1740
|
});
|
|
1741
|
+
const heads = Effect.fn("SqlJournal.heads")(function* (runIds) {
|
|
1742
|
+
const heads2 = [];
|
|
1743
|
+
for (let offset = 0; offset < runIds.length; offset += 2e3) {
|
|
1744
|
+
const ids = JSON.stringify(runIds.slice(offset, offset + 2e3));
|
|
1745
|
+
const selected = Dialect.isPostgres(sql) ? sql`SELECT value AS run_id FROM jsonb_array_elements_text(${ids}::jsonb)` : sql`SELECT value AS run_id FROM json_each(${ids})`;
|
|
1746
|
+
const rows = yield* sql`
|
|
1747
|
+
SELECT ids.run_id AS "runId", COALESCE(g.generation, 0) AS generation,
|
|
1748
|
+
COALESCE(g.after_seq, -1) AS "afterSeq",
|
|
1749
|
+
(SELECT MAX(e.seq) FROM flows_journal_events e WHERE e.run_id = ids.run_id) AS "maxSeq"
|
|
1750
|
+
FROM (${selected}) AS ids
|
|
1751
|
+
LEFT JOIN flows_journal_generations g ON g.run_id = ids.run_id
|
|
1752
|
+
`.pipe(Effect.mapError((cause) => error("read_failed", "could not read journal heads", cause)));
|
|
1753
|
+
for (const row of rows) {
|
|
1754
|
+
heads2.push({
|
|
1755
|
+
runId: row.runId,
|
|
1756
|
+
generation: Number(row.generation),
|
|
1757
|
+
afterSeq: Number(row.afterSeq),
|
|
1758
|
+
maxSeq: row.maxSeq === null ? null : Number(row.maxSeq)
|
|
1759
|
+
});
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
return heads2;
|
|
1763
|
+
});
|
|
1537
1764
|
return (0, import_Journal.make)({
|
|
1765
|
+
heads,
|
|
1538
1766
|
generation: (runId) => sql`
|
|
1539
1767
|
SELECT generation, after_seq AS "afterSeq" FROM flows_journal_generations WHERE run_id = ${runId}
|
|
1540
1768
|
`.pipe(
|
|
@@ -1544,6 +1772,9 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1544
1772
|
emitLossy,
|
|
1545
1773
|
emitDurable,
|
|
1546
1774
|
emitDurableUnfenced,
|
|
1775
|
+
emitDeferred,
|
|
1776
|
+
recall,
|
|
1777
|
+
recorded,
|
|
1547
1778
|
transact,
|
|
1548
1779
|
whenCommitted: (update) => Effect.flatMap(
|
|
1549
1780
|
Effect.serviceOption(sql.transactionService),
|
|
@@ -1554,8 +1785,16 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1554
1785
|
changes: PubSub.subscribe(changes),
|
|
1555
1786
|
project,
|
|
1556
1787
|
flush: Effect.fn("Journal.flush")(
|
|
1557
|
-
() => Effect.suspend(() => Effect.annotateCurrentSpan({ pending: state.pending })).pipe(
|
|
1558
|
-
|
|
1788
|
+
() => Effect.suspend(() => Effect.annotateCurrentSpan({ pending: state.pending, deferred: deferred.size })).pipe(
|
|
1789
|
+
// A failed lossy flush says nothing about the `emitDeferred`
|
|
1790
|
+
// entries, so they are written either way; the first failure wins.
|
|
1791
|
+
Effect.andThen(Effect.exit(flushInternal)),
|
|
1792
|
+
Effect.flatMap(
|
|
1793
|
+
(lossy) => Effect.flatMap(
|
|
1794
|
+
Effect.exit(flushDeferred),
|
|
1795
|
+
(queued) => Exit.isFailure(lossy) ? lossy : Exit.isFailure(queued) ? queued : reportDeferredFailure
|
|
1796
|
+
)
|
|
1797
|
+
)
|
|
1559
1798
|
)
|
|
1560
1799
|
)(),
|
|
1561
1800
|
checkpoint,
|