@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/CHANGELOG.md
CHANGED
|
@@ -2,8 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `Journal.Service.heads?` reads committed sequence tails and rewind generations together for a set of runs on SQLite and PostgreSQL, in batches of 2,000 IDs.
|
|
8
|
+
|
|
5
9
|
### Fixed
|
|
6
10
|
|
|
11
|
+
- Logged errors nested in objects, arrays, annotations, or aggregate errors
|
|
12
|
+
keep their redacted messages and stacks instead of appearing as `{}`.
|
|
13
|
+
Self-returning `toJSON` methods retain their fields, and replacement
|
|
14
|
+
serializers no longer recurse until the stack overflows.
|
|
15
|
+
|
|
16
|
+
- `Consensus.layerLocal` undoes a lease change when the write that made it
|
|
17
|
+
rolls back. A claim whose `RunStore` write failed, was interrupted, or was
|
|
18
|
+
replayed after a busy conflict left the in-memory lease claimed over a free
|
|
19
|
+
run row, and every later claim of that run was refused until the process
|
|
20
|
+
restarted. Each change now registers its undo with
|
|
21
|
+
`DurableWriter.afterRollback`.
|
|
22
|
+
- `Service.withPermit` no longer interrupts work when one lease renewal takes
|
|
23
|
+
longer than a second or the strategy returns a transient storage error. Misses
|
|
24
|
+
are retried until the lease has gone unconfirmed for `heartbeatWriteTolerance`,
|
|
25
|
+
and the work then fails with `persistence_failed` instead of `fence_lost`,
|
|
26
|
+
which is kept for a renewal that reports the lease lost.
|
|
7
27
|
- A lossy batch the writer fails to commit is logged and counted on the new
|
|
8
28
|
`flows_journal_lost` counter (`JournalMetrics.lostEntries`, `lost(code)`),
|
|
9
29
|
so telemetry lost with no `flush` caller is no longer silent. A final flush
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Release candidate scope, host requirements and compatibility review are defined in the [library support policy](https://github.com/smithersai/smithers/blob/main/RELEASE_SUPPORT.md).
|
|
4
4
|
|
|
5
|
-
This package declares `effect` as an exact `4.
|
|
5
|
+
This package declares `effect` as an exact `4.1.1` peer dependency. Install one
|
|
6
6
|
`effect` release at that version so all Smithers packages share one Effect
|
|
7
7
|
runtime.
|
|
8
8
|
|
|
@@ -10,9 +10,10 @@ runtime.
|
|
|
10
10
|
|
|
11
11
|
An append-only event history for [Effect](https://effect.website) applications.
|
|
12
12
|
`@smthrs/journal` records what a long-running piece of work did, in order, in
|
|
13
|
-
SQLite
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
SQLite or PostgreSQL. Appends leave existing event rows unchanged. A checkpoint
|
|
14
|
+
replaces saved state at the same run and sequence, and `compact` deletes older
|
|
15
|
+
event rows. A process that dies halfway through leaves behind the committed
|
|
16
|
+
history that has not been compacted.
|
|
16
17
|
|
|
17
18
|
It is a set of Effect services: every write returns a typed receipt or a typed
|
|
18
19
|
failure, and the database arrives as a layer you compose. It belongs to the
|
|
@@ -25,11 +26,11 @@ package is required.
|
|
|
25
26
|
npm install @smthrs/journal@next effect
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
`effect` is a peer dependency at exactly `4.
|
|
29
|
+
`effect` is a peer dependency at exactly `4.1.1`. Two copies of `effect`
|
|
29
30
|
in one program are two sets of service tags, so a journal layer built against
|
|
30
31
|
one copy cannot be provided to a program holding the other.
|
|
31
32
|
|
|
32
|
-
Node.js
|
|
33
|
+
Node.js 22.19.0 or later is required for the Node SQLite driver. The package
|
|
33
34
|
ships as both ESM and CommonJS with TypeScript declarations.
|
|
34
35
|
|
|
35
36
|
## Write one run and read it back
|
|
@@ -131,20 +132,33 @@ journaling cannot abandon the cleanup that follows the commit.
|
|
|
131
132
|
|
|
132
133
|
## Public API
|
|
133
134
|
|
|
134
|
-
The root exports
|
|
135
|
-
|
|
135
|
+
The root exports the namespaces below. Most also have a matching
|
|
136
|
+
`@smthrs/journal/<Module>` subpath. `CallFact`, `ExecutionFact`, and `StepFact`
|
|
137
|
+
are available only from the root:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { CallFact, ExecutionFact, StepFact } from "@smthrs/journal"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The
|
|
136
144
|
[API reference](https://github.com/smithersai/smithers/blob/main/packages/smithers/flows/journal/docs/api.md) lists every export
|
|
137
145
|
with its one-line summary.
|
|
138
146
|
|
|
139
147
|
| Namespace | What it holds |
|
|
140
148
|
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
141
149
|
| `Journal` | The service and its operations, the `Checkpoint` and `Compacted` models, typed errors, receipts, read options, constructors, and the no-op layer. |
|
|
150
|
+
| `EngineEvent` | Typed engine lifecycle event schemas and constructors. |
|
|
151
|
+
| `ExecutionFact` | Native execution observations and versioned facts. |
|
|
152
|
+
| `CallFact` | Authorized native call identities and delivered result facts. |
|
|
153
|
+
| `StepFact` | Durable agent checkpoint observations and invocation coordinates. |
|
|
154
|
+
| `RedactedLogger` | Redacting Effect logger and console bridge. |
|
|
142
155
|
| `JournalEvent` | Branded `RunId`, `Seq`, `SourceId`, and `SourceSeq`; the `Input` and committed `Entry` schemas; deterministic `makeEventId`. |
|
|
143
156
|
| `SqlJournal` | `SqlJournalOptions`, `CompactionPolicy`, the database-backed `layer(options)` fenced by `SqlConsensus`, and `layerWith(options)` over an injected strategy. |
|
|
144
157
|
| `OwnerId` | The fencing token `emitDurable` accepts, carrying `hostId`, `pid`, and `nonce`. |
|
|
145
158
|
| `Consensus` | The injectable ownership strategy: `claim`, `activate`, `heartbeat`, `reconfirm`, `release`, `steal`, `recover`, and the commit-time `guard`; typed outcomes; `LivenessEvidence`; the lease constants; `layerLocal`, `layerNoop`, `make`, `makeLocal`, `makeNoop`. |
|
|
146
159
|
| `SqlConsensus` | The default database-backed strategy over `flows_consensus_leases`: `make` and `layer`. |
|
|
147
160
|
| `Redaction` | The payload redaction applied to entries before they are written, its rule set, and `makeNoop`. |
|
|
161
|
+
| `TelemetryLog` | The `telemetry.log` record schema and the bounded, redacted `snapshot` of an Effect log event that the observability forwarder and the engine's step logs both write. |
|
|
148
162
|
| `Projection` | The reproducible `Projection` model and its identity constructor. |
|
|
149
163
|
| `JournalMetrics` | The `flows_journal_writes` counter and the per-channel views `SqlJournal` updates on every emission receipt. |
|
|
150
164
|
| `Migrations` | `set`, `run`, and `layer` for this package's tables: `flows_journal_events` with its indexes, `flows_journal_checkpoints`, `flows_journal_dedup`, and the `flows_consensus_leases` lease table. |
|
|
@@ -193,6 +207,10 @@ adapters may omit the operation; truncating adapters must implement it. The
|
|
|
193
207
|
`JournalGeneration.initialize` subpath shares the table installation with time
|
|
194
208
|
travel without adding a migration below an already applied migration block.
|
|
195
209
|
|
|
210
|
+
`Journal.Service.heads?(runIds)` reads committed tails and rewind generations
|
|
211
|
+
together in batches of 2,000 IDs. Readers can page only changed runs while
|
|
212
|
+
still observing late records on completed runs. See the [API](./docs/api.md#bulk-journal-heads).
|
|
213
|
+
|
|
196
214
|
## Documentation
|
|
197
215
|
|
|
198
216
|
- [State and event authority](./docs/concepts/state-event-authority.md)
|
package/dist/cjs/CallFact.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare const Call: Schema.Struct<{
|
|
|
33
33
|
readonly layers: Schema.$Array<Schema.String>;
|
|
34
34
|
}>;
|
|
35
35
|
readonly flowName: Schema.String;
|
|
36
|
-
readonly input:
|
|
36
|
+
readonly input: (typeof Schema.Json);
|
|
37
37
|
/**
|
|
38
38
|
* The resolved declaration's display projection, as its owner encoded it.
|
|
39
39
|
*
|
|
@@ -44,7 +44,7 @@ export declare const Call: Schema.Struct<{
|
|
|
44
44
|
* before it is displayed, so the journal carries it and claims nothing about
|
|
45
45
|
* it. Absent means the declaration claimed nothing.
|
|
46
46
|
*/
|
|
47
|
-
readonly descriptor: Schema.optional<
|
|
47
|
+
readonly descriptor: Schema.optional<(typeof Schema.Json)>;
|
|
48
48
|
}>;
|
|
49
49
|
/** Decoded authorized call coordinates.
|
|
50
50
|
* @category models
|
|
@@ -67,12 +67,12 @@ export declare class Annotation extends Annotation_base {
|
|
|
67
67
|
*/
|
|
68
68
|
export declare const Result: Schema.Union<readonly [Schema.Struct<{
|
|
69
69
|
readonly outcome: Schema.Literal<"success">;
|
|
70
|
-
readonly value:
|
|
70
|
+
readonly value: (typeof Schema.Json);
|
|
71
71
|
readonly message: Schema.optional<Schema.String>;
|
|
72
72
|
readonly code: Schema.optionalKey<Schema.Never>;
|
|
73
73
|
}>, Schema.Struct<{
|
|
74
74
|
readonly outcome: Schema.Literal<"failure">;
|
|
75
|
-
readonly value:
|
|
75
|
+
readonly value: (typeof Schema.Json);
|
|
76
76
|
readonly message: Schema.optional<Schema.String>;
|
|
77
77
|
readonly code: Schema.optional<Schema.String>;
|
|
78
78
|
}>]>;
|
|
@@ -82,8 +82,8 @@ export declare const Result: Schema.Union<readonly [Schema.Struct<{
|
|
|
82
82
|
*/
|
|
83
83
|
export declare const Fact: Schema.Union<readonly [Schema.Struct<{
|
|
84
84
|
readonly phase: Schema.Literal<"invoked">;
|
|
85
|
-
readonly input:
|
|
86
|
-
readonly descriptor: Schema.optional<
|
|
85
|
+
readonly input: (typeof Schema.Json);
|
|
86
|
+
readonly descriptor: Schema.optional<(typeof Schema.Json)>;
|
|
87
87
|
readonly version: Schema.Literal<1>;
|
|
88
88
|
readonly callId: Schema.String;
|
|
89
89
|
readonly identity: Schema.Struct<{
|
|
@@ -99,8 +99,8 @@ export declare const Fact: Schema.Union<readonly [Schema.Struct<{
|
|
|
99
99
|
}>, Schema.Struct<{
|
|
100
100
|
readonly phase: Schema.Literal<"settled">;
|
|
101
101
|
readonly outcome: Schema.Literals<readonly ["success", "failure"]>;
|
|
102
|
-
readonly value:
|
|
103
|
-
readonly message: Schema.optional<
|
|
102
|
+
readonly value: (typeof Schema.Json);
|
|
103
|
+
readonly message: Schema.optional<(typeof Schema.Json)>;
|
|
104
104
|
readonly code: Schema.optional<Schema.String>;
|
|
105
105
|
readonly version: Schema.Literal<1>;
|
|
106
106
|
readonly callId: Schema.String;
|
package/dist/cjs/Consensus.d.ts
CHANGED
|
@@ -376,6 +376,9 @@ export declare class ConsensusError extends ConsensusError_base {
|
|
|
376
376
|
* `claim` (or `steal`, its evidence-gated form) reserves the run and returns
|
|
377
377
|
* the grant timestamp, `activate` presents that grant to take ownership, and
|
|
378
378
|
* `release` clears whichever of the caller's claim or ownership still stands.
|
|
379
|
+
* Given the claim's grant timestamp, `release` clears only that claim: an
|
|
380
|
+
* owner that claimed its own stale run keeps its ownership when it drops the
|
|
381
|
+
* claim.
|
|
379
382
|
* `recover` clears a *third party's* stale claim after its claimant was proven
|
|
380
383
|
* dead — the relocated `recoverClaim` mechanic. `guard` is the commit-time
|
|
381
384
|
* admission check (R3) `Journal.emitDurable`, `checkpoint`, and `compact`
|
|
@@ -400,7 +403,11 @@ export interface Service {
|
|
|
400
403
|
readonly heartbeat: (runId: string, owner: OwnerId, nowMs: number) => Effect.Effect<HeartbeatOutcome, ConsensusError>;
|
|
401
404
|
/** Renews only while this owner holds the lease and no takeover claim is pending. */
|
|
402
405
|
readonly reconfirm: (runId: string, owner: OwnerId, nowMs: number) => Effect.Effect<HeartbeatOutcome, ConsensusError>;
|
|
403
|
-
|
|
406
|
+
/**
|
|
407
|
+
* Clears the caller's claim and ownership, or only the claim granted at
|
|
408
|
+
* `claimedAtMs` when that grant is supplied.
|
|
409
|
+
*/
|
|
410
|
+
readonly release: (runId: string, owner: OwnerId, claimedAtMs?: number) => Effect.Effect<void, ConsensusError>;
|
|
404
411
|
readonly steal: (runId: string, claimant: OwnerId, nowMs: number, evidence: LivenessEvidence) => Effect.Effect<ClaimOutcome, ConsensusError>;
|
|
405
412
|
readonly recover: (runId: string, staleClaimant: OwnerId, grantedAtMs: number, observer: OwnerId, nowMs: number, evidence: LivenessEvidence) => Effect.Effect<RecoverOutcome, ConsensusError>;
|
|
406
413
|
readonly guard: (runId: string, owner: OwnerId) => Effect.Effect<void, ConsensusError>;
|
|
@@ -471,11 +478,14 @@ export declare const makeLocal: Effect.Effect<Service>;
|
|
|
471
478
|
* one instance to every service that must agree on it, which is what
|
|
472
479
|
* `SqlJournal.layerWith` and `RunStore.layerWith` exist for.
|
|
473
480
|
*
|
|
474
|
-
* The lease is not
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
481
|
+
* The lease is not in the database, so each transition made inside a managed
|
|
482
|
+
* `DurableWriter` write (every `RunStore` and `Journal.transact` write)
|
|
483
|
+
* registers its undo with `DurableWriter.afterRollback`: when the write rolls
|
|
484
|
+
* back, is interrupted, or is replayed after a busy conflict, the lease
|
|
485
|
+
* returns to what it was before the write. A transition made outside a
|
|
486
|
+
* managed write, including inside a raw SQL transaction, stands. `SqlConsensus`
|
|
487
|
+
* keeps the lease in the database itself; prefer it wherever a database is
|
|
488
|
+
* present.
|
|
479
489
|
*
|
|
480
490
|
* @since 1.0.0
|
|
481
491
|
* @category layers
|
|
@@ -484,6 +494,16 @@ export declare const layerLocal: Layer.Layer<Consensus>;
|
|
|
484
494
|
/**
|
|
485
495
|
* Shares the existing consensus lease lifecycle with named engine resources.
|
|
486
496
|
* The reader is private to the strategy; resource leases never create runs.
|
|
497
|
+
*
|
|
498
|
+
* Heartbeats reconfirm the lease every {@link heartbeatInterval}, each bounded
|
|
499
|
+
* by one interval. A `Lost` outcome is ownership news and interrupts the work
|
|
500
|
+
* with `fence_lost`, as does a renewal failing with `fence_lost`. A slow
|
|
501
|
+
* renewal or a storage failure is not: the persisted lease
|
|
502
|
+
* stays held until {@link heartbeatStaleAfter}, so misses are retried until
|
|
503
|
+
* the lease has gone unconfirmed for {@link heartbeatWriteTolerance}, measured
|
|
504
|
+
* from the timestamp of the last renewal the strategy confirmed. Past that
|
|
505
|
+
* budget the work is interrupted with `persistence_failed`, carrying the last
|
|
506
|
+
* failure as its cause.
|
|
487
507
|
* @since 1.0.0
|
|
488
508
|
* @category constructors
|
|
489
509
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Consensus.d.ts","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;
|
|
1
|
+
{"version":3,"file":"Consensus.d.ts","sourceRoot":"","sources":["../../src/Consensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAIH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;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58,6 +58,7 @@ __export(Consensus_exports, {
|
|
|
58
58
|
sameOwner: () => sameOwner
|
|
59
59
|
});
|
|
60
60
|
module.exports = __toCommonJS(Consensus_exports);
|
|
61
|
+
var import_DurableWriter = require("@smthrs/database/DurableWriter");
|
|
61
62
|
var Clock = __toESM(require("effect/Clock"), 1);
|
|
62
63
|
var Context = __toESM(require("effect/Context"), 1);
|
|
63
64
|
var Duration = __toESM(require("effect/Duration"), 1);
|
|
@@ -151,6 +152,11 @@ const makeNoop = (overrides = {}) => Consensus.of({
|
|
|
151
152
|
...overrides
|
|
152
153
|
});
|
|
153
154
|
const layerNoop = (overrides = {}) => Layer.succeed(Consensus)(makeNoop(overrides));
|
|
155
|
+
const snapshot = (lease) => lease && {
|
|
156
|
+
owner: lease.owner && { ...lease.owner },
|
|
157
|
+
claim: lease.claim && { ...lease.claim }
|
|
158
|
+
};
|
|
159
|
+
const sameLease = (left, right) => left === void 0 || right === void 0 ? left === right : (left.owner === void 0 || right.owner === void 0 ? left.owner === right.owner : sameOwner(left.owner.id, right.owner.id) && left.owner.grantedAtMs === right.owner.grantedAtMs && left.owner.heartbeatAtMs === right.owner.heartbeatAtMs) && (left.claim === void 0 || right.claim === void 0 ? left.claim === right.claim : sameOwner(left.claim.id, right.claim.id) && left.claim.claimedAtMs === right.claim.claimedAtMs);
|
|
154
160
|
const makeLocal = Effect.sync(() => {
|
|
155
161
|
const leases = /* @__PURE__ */ new Map();
|
|
156
162
|
const leaseOf = (runId) => {
|
|
@@ -165,10 +171,21 @@ const makeLocal = Effect.sync(() => {
|
|
|
165
171
|
leases.delete(runId);
|
|
166
172
|
}
|
|
167
173
|
};
|
|
174
|
+
const transactional = (runId, apply) => Effect.suspend(() => {
|
|
175
|
+
const before = snapshot(leases.get(runId));
|
|
176
|
+
const result = apply();
|
|
177
|
+
const after = snapshot(leases.get(runId));
|
|
178
|
+
if (sameLease(before, after)) return Effect.succeed(result);
|
|
179
|
+
return (0, import_DurableWriter.afterRollback)(Effect.sync(() => {
|
|
180
|
+
if (!sameLease(snapshot(leases.get(runId)), after)) return;
|
|
181
|
+
if (before === void 0) leases.delete(runId);
|
|
182
|
+
else leases.set(runId, before);
|
|
183
|
+
})).pipe(Effect.as(result));
|
|
184
|
+
});
|
|
168
185
|
const service = Consensus.of({
|
|
169
186
|
withPermit: (key, owner, isAlive, effect) => resourcePermit(service, () => Effect.sync(() => leases.get(key)), key, owner, isAlive, effect),
|
|
170
187
|
claim: Effect.fn("Consensus.claim")(
|
|
171
|
-
(runId, claimant, nowMs) =>
|
|
188
|
+
(runId, claimant, nowMs) => transactional(runId, () => {
|
|
172
189
|
const lease = leaseOf(runId);
|
|
173
190
|
if (lease.claim !== void 0) return rejected("already_claimed");
|
|
174
191
|
if (lease.owner !== void 0) return rejected("owner_held");
|
|
@@ -177,7 +194,7 @@ const makeLocal = Effect.sync(() => {
|
|
|
177
194
|
})
|
|
178
195
|
),
|
|
179
196
|
activate: Effect.fn("Consensus.activate")(
|
|
180
|
-
(runId, owner, grantedAtMs, nowMs) =>
|
|
197
|
+
(runId, owner, grantedAtMs, nowMs) => transactional(runId, () => {
|
|
181
198
|
const lease = leaseOf(runId);
|
|
182
199
|
if (lease.claim === void 0 || !sameOwner(lease.claim.id, owner) || lease.claim.claimedAtMs !== grantedAtMs) {
|
|
183
200
|
prune(runId, lease);
|
|
@@ -189,7 +206,7 @@ const makeLocal = Effect.sync(() => {
|
|
|
189
206
|
})
|
|
190
207
|
),
|
|
191
208
|
heartbeat: Effect.fn("Consensus.heartbeat")(
|
|
192
|
-
(runId, owner, nowMs) =>
|
|
209
|
+
(runId, owner, nowMs) => transactional(runId, () => {
|
|
193
210
|
const lease = leases.get(runId);
|
|
194
211
|
if (lease?.owner === void 0 || !sameOwner(lease.owner.id, owner)) return lost;
|
|
195
212
|
lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs);
|
|
@@ -197,7 +214,7 @@ const makeLocal = Effect.sync(() => {
|
|
|
197
214
|
})
|
|
198
215
|
),
|
|
199
216
|
reconfirm: Effect.fn("Consensus.reconfirm")(
|
|
200
|
-
(runId, owner, nowMs) =>
|
|
217
|
+
(runId, owner, nowMs) => transactional(runId, () => {
|
|
201
218
|
const lease = leases.get(runId);
|
|
202
219
|
if (lease?.owner === void 0 || lease.claim !== void 0 || !sameOwner(lease.owner.id, owner)) return lost;
|
|
203
220
|
lease.owner.heartbeatAtMs = Math.max(lease.owner.heartbeatAtMs, nowMs);
|
|
@@ -205,20 +222,20 @@ const makeLocal = Effect.sync(() => {
|
|
|
205
222
|
})
|
|
206
223
|
),
|
|
207
224
|
release: Effect.fn("Consensus.release")(
|
|
208
|
-
(runId, owner) =>
|
|
225
|
+
(runId, owner, claimedAtMs) => transactional(runId, () => {
|
|
209
226
|
const lease = leases.get(runId);
|
|
210
227
|
if (lease === void 0) return;
|
|
211
|
-
if (lease.claim !== void 0 && sameOwner(lease.claim.id, owner)) {
|
|
228
|
+
if (lease.claim !== void 0 && sameOwner(lease.claim.id, owner) && (claimedAtMs === void 0 || lease.claim.claimedAtMs === claimedAtMs)) {
|
|
212
229
|
lease.claim = void 0;
|
|
213
230
|
}
|
|
214
|
-
if (lease.owner !== void 0 && sameOwner(lease.owner.id, owner)) {
|
|
231
|
+
if (claimedAtMs === void 0 && lease.owner !== void 0 && sameOwner(lease.owner.id, owner)) {
|
|
215
232
|
lease.owner = void 0;
|
|
216
233
|
}
|
|
217
234
|
prune(runId, lease);
|
|
218
235
|
})
|
|
219
236
|
),
|
|
220
237
|
steal: Effect.fn("Consensus.steal")(
|
|
221
|
-
(runId, claimant, nowMs, evidence) =>
|
|
238
|
+
(runId, claimant, nowMs, evidence) => transactional(runId, () => {
|
|
222
239
|
if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {
|
|
223
240
|
return rejected("evidence_invalid");
|
|
224
241
|
}
|
|
@@ -235,7 +252,7 @@ const makeLocal = Effect.sync(() => {
|
|
|
235
252
|
})
|
|
236
253
|
),
|
|
237
254
|
recover: Effect.fn("Consensus.recover")(
|
|
238
|
-
(runId, staleClaimant, grantedAtMs, observer, nowMs, evidence) =>
|
|
255
|
+
(runId, staleClaimant, grantedAtMs, observer, nowMs, evidence) => transactional(runId, () => {
|
|
239
256
|
if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) return rejected("evidence_invalid");
|
|
240
257
|
const lease = leases.get(runId);
|
|
241
258
|
if (lease?.claim === void 0 || !sameOwner(lease.claim.id, staleClaimant) || lease.claim.claimedAtMs !== grantedAtMs) {
|
|
@@ -258,6 +275,7 @@ const makeLocal = Effect.sync(() => {
|
|
|
258
275
|
});
|
|
259
276
|
const layerLocal = Layer.effect(Consensus, makeLocal);
|
|
260
277
|
const resourcePermit = (strategy, read, key, owner, isAlive, effect) => Effect.uninterruptibleMask((restore) => {
|
|
278
|
+
let confirmedAtMs = 0;
|
|
261
279
|
const acquire = Effect.gen(function* () {
|
|
262
280
|
while (true) {
|
|
263
281
|
const admitted = yield* Effect.uninterruptible(Effect.gen(function* () {
|
|
@@ -283,7 +301,10 @@ const resourcePermit = (strategy, read, key, owner, isAlive, effect) => Effect.u
|
|
|
283
301
|
const activated2 = yield* strategy.activate(key, owner, grant.grantedAtMs, nowMs).pipe(
|
|
284
302
|
Effect.onError(() => strategy.release(key, owner).pipe(Effect.orDie))
|
|
285
303
|
);
|
|
286
|
-
if (activated2._tag === "Activated")
|
|
304
|
+
if (activated2._tag === "Activated") {
|
|
305
|
+
confirmedAtMs = nowMs;
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
287
308
|
yield* strategy.release(key, owner);
|
|
288
309
|
return false;
|
|
289
310
|
}));
|
|
@@ -291,18 +312,38 @@ const resourcePermit = (strategy, read, key, owner, isAlive, effect) => Effect.u
|
|
|
291
312
|
yield* restore(Effect.sleep("50 millis"));
|
|
292
313
|
}
|
|
293
314
|
});
|
|
294
|
-
const pulse = Effect.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
Effect.
|
|
302
|
-
(
|
|
303
|
-
)
|
|
304
|
-
|
|
305
|
-
|
|
315
|
+
const pulse = Effect.gen(function* () {
|
|
316
|
+
const intervalMs = Duration.toMillis(heartbeatInterval);
|
|
317
|
+
const toleranceMs = Duration.toMillis(heartbeatWriteTolerance);
|
|
318
|
+
let waitMs = intervalMs;
|
|
319
|
+
while (true) {
|
|
320
|
+
yield* Effect.sleep(waitMs);
|
|
321
|
+
const nowMs = yield* Clock.currentTimeMillis;
|
|
322
|
+
const renewed = yield* Effect.result(
|
|
323
|
+
strategy.reconfirm(key, owner, nowMs).pipe(Effect.timeout(heartbeatInterval))
|
|
324
|
+
);
|
|
325
|
+
if (renewed._tag === "Success") {
|
|
326
|
+
if (renewed.success._tag !== "Renewed") return yield* Effect.fail(fenceLost(key, owner));
|
|
327
|
+
confirmedAtMs = Math.max(confirmedAtMs, nowMs);
|
|
328
|
+
waitMs = intervalMs;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (renewed.failure instanceof ConsensusError && renewed.failure.code === "fence_lost") {
|
|
332
|
+
return yield* Effect.fail(renewed.failure);
|
|
333
|
+
}
|
|
334
|
+
const unconfirmedMs = (yield* Clock.currentTimeMillis) - confirmedAtMs;
|
|
335
|
+
if (unconfirmedMs >= toleranceMs) {
|
|
336
|
+
return yield* Effect.fail(
|
|
337
|
+
new ConsensusError({
|
|
338
|
+
code: "persistence_failed",
|
|
339
|
+
message: `resource ${key} lease went unconfirmed for ${unconfirmedMs} ms`,
|
|
340
|
+
cause: renewed.failure
|
|
341
|
+
})
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
waitMs = Math.min(intervalMs, toleranceMs - unconfirmedMs);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
306
347
|
return acquire.pipe(
|
|
307
348
|
Effect.andThen(
|
|
308
349
|
() => restore(Effect.raceFirst(effect, pulse)).pipe(
|