@smthrs/journal 1.0.0-rc.4 → 1.0.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/cjs/JournalGeneration.d.ts +2 -2
- package/dist/cjs/JournalGeneration.d.ts.map +1 -1
- package/dist/cjs/JournalGeneration.js +3 -2
- package/dist/cjs/JournalGeneration.js.map +2 -2
- package/dist/cjs/Migrations.d.ts +2 -2
- package/dist/cjs/Migrations.d.ts.map +1 -1
- package/dist/cjs/SqlConsensus.d.ts +1 -1
- package/dist/cjs/SqlConsensus.d.ts.map +1 -1
- package/dist/cjs/SqlConsensus.js +1 -1
- package/dist/cjs/SqlConsensus.js.map +1 -1
- package/dist/cjs/SqlJournal.d.ts +1 -1
- package/dist/cjs/SqlJournal.d.ts.map +1 -1
- package/dist/cjs/SqlJournal.js +2 -2
- package/dist/cjs/SqlJournal.js.map +2 -2
- package/dist/cjs/migrations/0001_initial.d.ts +1 -1
- package/dist/cjs/migrations/0001_initial.d.ts.map +1 -1
- package/dist/cjs/migrations/0001_initial.js +1 -1
- package/dist/cjs/migrations/0001_initial.js.map +1 -1
- package/dist/cjs/migrations/0002_checkpoints.d.ts +1 -1
- package/dist/cjs/migrations/0002_checkpoints.d.ts.map +1 -1
- package/dist/cjs/migrations/0002_checkpoints.js +1 -1
- package/dist/cjs/migrations/0002_checkpoints.js.map +1 -1
- package/dist/cjs/migrations/0003_startup_index.d.ts +2 -2
- package/dist/cjs/migrations/0003_startup_index.d.ts.map +1 -1
- package/dist/cjs/migrations/0003_startup_index.js +1 -1
- package/dist/cjs/migrations/0003_startup_index.js.map +1 -1
- package/dist/cjs/migrations/0004_dedup.d.ts +1 -1
- package/dist/cjs/migrations/0004_dedup.d.ts.map +1 -1
- package/dist/cjs/migrations/0004_dedup.js +1 -1
- package/dist/cjs/migrations/0004_dedup.js.map +1 -1
- package/dist/cjs/migrations/0005_run_event_type.d.ts +1 -1
- package/dist/cjs/migrations/0005_run_event_type.d.ts.map +1 -1
- package/dist/cjs/migrations/0005_run_event_type.js +1 -1
- package/dist/cjs/migrations/0005_run_event_type.js.map +1 -1
- package/dist/cjs/migrations/0006_consensus.d.ts +1 -1
- package/dist/cjs/migrations/0006_consensus.d.ts.map +1 -1
- package/dist/cjs/migrations/0006_consensus.js +1 -1
- package/dist/cjs/migrations/0006_consensus.js.map +1 -1
- package/dist/cjs/test/TestJournal.d.ts +1 -1
- package/dist/cjs/test/TestJournal.d.ts.map +1 -1
- package/dist/esm/JournalGeneration.d.ts +2 -2
- package/dist/esm/JournalGeneration.d.ts.map +1 -1
- package/dist/esm/JournalGeneration.js +8 -2
- package/dist/esm/JournalGeneration.js.map +1 -1
- package/dist/esm/Migrations.d.ts +2 -2
- package/dist/esm/Migrations.d.ts.map +1 -1
- package/dist/esm/SqlConsensus.d.ts +1 -1
- package/dist/esm/SqlConsensus.d.ts.map +1 -1
- package/dist/esm/SqlConsensus.js +1 -1
- package/dist/esm/SqlConsensus.js.map +1 -1
- package/dist/esm/SqlJournal.d.ts +1 -1
- package/dist/esm/SqlJournal.d.ts.map +1 -1
- package/dist/esm/SqlJournal.js +2 -2
- package/dist/esm/SqlJournal.js.map +1 -1
- package/dist/esm/migrations/0001_initial.d.ts +1 -1
- package/dist/esm/migrations/0001_initial.d.ts.map +1 -1
- package/dist/esm/migrations/0001_initial.js +1 -1
- package/dist/esm/migrations/0001_initial.js.map +1 -1
- package/dist/esm/migrations/0002_checkpoints.d.ts +1 -1
- package/dist/esm/migrations/0002_checkpoints.d.ts.map +1 -1
- package/dist/esm/migrations/0002_checkpoints.js +1 -1
- package/dist/esm/migrations/0002_checkpoints.js.map +1 -1
- package/dist/esm/migrations/0003_startup_index.d.ts +2 -2
- package/dist/esm/migrations/0003_startup_index.d.ts.map +1 -1
- package/dist/esm/migrations/0003_startup_index.js +1 -1
- package/dist/esm/migrations/0003_startup_index.js.map +1 -1
- package/dist/esm/migrations/0004_dedup.d.ts +1 -1
- package/dist/esm/migrations/0004_dedup.d.ts.map +1 -1
- package/dist/esm/migrations/0004_dedup.js +1 -1
- package/dist/esm/migrations/0004_dedup.js.map +1 -1
- package/dist/esm/migrations/0005_run_event_type.d.ts +1 -1
- package/dist/esm/migrations/0005_run_event_type.d.ts.map +1 -1
- package/dist/esm/migrations/0005_run_event_type.js +1 -1
- package/dist/esm/migrations/0005_run_event_type.js.map +1 -1
- package/dist/esm/migrations/0006_consensus.d.ts +1 -1
- package/dist/esm/migrations/0006_consensus.d.ts.map +1 -1
- package/dist/esm/migrations/0006_consensus.js +1 -1
- package/dist/esm/migrations/0006_consensus.js.map +1 -1
- package/dist/esm/test/TestJournal.d.ts +1 -1
- package/dist/esm/test/TestJournal.d.ts.map +1 -1
- package/docs/README.md +1 -1
- package/docs/installation.md +3 -3
- package/docs/quickstart.md +1 -1
- package/package.json +7 -7
- package/src/JournalGeneration.ts +8 -2
- package/src/SqlConsensus.ts +1 -1
- package/src/SqlJournal.ts +3 -3
- package/src/migrations/0001_initial.ts +1 -1
- package/src/migrations/0002_checkpoints.ts +1 -1
- package/src/migrations/0003_startup_index.ts +1 -1
- package/src/migrations/0004_dedup.ts +1 -1
- package/src/migrations/0005_run_event_type.ts +1 -1
- package/src/migrations/0006_consensus.ts +1 -1
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
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
|
|
6
|
-
`
|
|
7
|
-
|
|
5
|
+
This package declares `effect` as a `^4.0.4` peer dependency. Install one
|
|
6
|
+
`effect` release in that range so all Smithers packages share one Effect
|
|
7
|
+
runtime.
|
|
8
8
|
|
|
9
9
|
**Documentation:** https://journal.smithers.sh
|
|
10
10
|
|
|
@@ -23,7 +23,7 @@ package is required.
|
|
|
23
23
|
|
|
24
24
|
Not on npm yet; see [Installation](https://github.com/smithersai/smithers/blob/main/packages/smithers/flows/flow/docs/installation.md#use-the-libraries).
|
|
25
25
|
|
|
26
|
-
`effect` is a peer dependency
|
|
26
|
+
`effect` is a peer dependency in the range `^4.0.4`. Two copies of `effect`
|
|
27
27
|
in one program are two sets of service tags, so a journal layer built against
|
|
28
28
|
one copy cannot be provided to a program holding the other.
|
|
29
29
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @since 1.0.0-rc.0
|
|
5
5
|
*/
|
|
6
6
|
import * as Effect from "effect/Effect";
|
|
7
|
-
import * as SqlClient from "effect/
|
|
7
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
8
8
|
/**
|
|
9
9
|
* Installs the generation table idempotently. Both the SQL journal and time
|
|
10
10
|
* travel initialize it, including databases whose migration ladder already
|
|
@@ -13,7 +13,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
|
|
|
13
13
|
* @category migrations
|
|
14
14
|
* @since 1.0.0-rc.0
|
|
15
15
|
*/
|
|
16
|
-
export declare const initialize: Effect.Effect<void, import("effect/
|
|
16
|
+
export declare const initialize: Effect.Effect<void, import("effect/sql/SqlError").SqlError, SqlClient.SqlClient>;
|
|
17
17
|
/**
|
|
18
18
|
* Registers a live journal's cache invalidation until its scope closes.
|
|
19
19
|
* Multiple journals over the same SQL client must observe the same rewind.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JournalGeneration.d.ts","sourceRoot":"","sources":["../../src/JournalGeneration.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,SAAS,MAAM
|
|
1
|
+
{"version":3,"file":"JournalGeneration.d.ts","sourceRoot":"","sources":["../../src/JournalGeneration.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA;AAEjD;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,kFAqBrB,CAAA;AAIF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,KAAK,IAAI,mFAgBtE,CAAA;AAEJ;;;;;;;GAOG;AACH,eAAO,MAAM,MAAM,GAAI,QAAQ,aAAa,CAAC,MAAM,CAAC,oDAMhD,CAAA"}
|
|
@@ -35,17 +35,18 @@ __export(JournalGeneration_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(JournalGeneration_exports);
|
|
36
36
|
var Dialect = __toESM(require("@smthrs/database/Dialect"), 1);
|
|
37
37
|
var Effect = __toESM(require("effect/Effect"), 1);
|
|
38
|
-
var SqlClient = __toESM(require("effect/
|
|
38
|
+
var SqlClient = __toESM(require("effect/sql/SqlClient"), 1);
|
|
39
39
|
const initialize = Effect.gen(function* () {
|
|
40
40
|
const sql = yield* SqlClient.SqlClient;
|
|
41
41
|
if (Dialect.isReadOnly(sql)) return;
|
|
42
|
-
|
|
42
|
+
const create = sql`
|
|
43
43
|
CREATE TABLE IF NOT EXISTS flows_journal_generations (
|
|
44
44
|
run_id TEXT PRIMARY KEY NOT NULL CHECK (length(run_id) > 0),
|
|
45
45
|
generation ${Dialect.integer(sql)} NOT NULL CHECK (${Dialect.isInteger(sql, sql`generation`)} AND generation > 0 AND generation <= 9007199254740991),
|
|
46
46
|
after_seq ${Dialect.integer(sql)} NOT NULL CHECK (${Dialect.isInteger(sql, sql`after_seq`)} AND after_seq >= -1 AND after_seq <= 9007199254740991)
|
|
47
47
|
)
|
|
48
48
|
`;
|
|
49
|
+
yield* sql.onDialectOrElse({ pg: () => sql.withTransaction(create), orElse: () => create });
|
|
49
50
|
});
|
|
50
51
|
const journals = /* @__PURE__ */ new WeakMap();
|
|
51
52
|
const onTruncate = (forget2) => Effect.gen(function* () {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/JournalGeneration.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Durable generations distinguish histories that reuse journal sequences.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport * as Effect from \"effect/Effect\"\nimport * as SqlClient from \"effect/
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,cAAyB;AACzB,aAAwB;AACxB,gBAA2B;AAUpB,MAAM,aAAa,OAAO,IAAI,aAAY;AAC/C,QAAM,MAAM,OAAO,UAAU;AAE7B,MAAI,QAAQ,WAAW,GAAG,EAAG;AAC7B,
|
|
4
|
+
"sourcesContent": ["/**\n * Durable generations distinguish histories that reuse journal sequences.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport * as Effect from \"effect/Effect\"\nimport * as SqlClient from \"effect/sql/SqlClient\"\n\n/**\n * Installs the generation table idempotently. Both the SQL journal and time\n * travel initialize it, including databases whose migration ladder already\n * passed the journal's reserved block. A read-only client installs nothing.\n *\n * @category migrations\n * @since 1.0.0-rc.0\n */\nexport const initialize = Effect.gen(function*() {\n const sql = yield* SqlClient.SqlClient\n // A read-only observer reads the schema it finds and installs nothing.\n if (Dialect.isReadOnly(sql)) return\n const create = sql`\n CREATE TABLE IF NOT EXISTS flows_journal_generations (\n run_id TEXT PRIMARY KEY NOT NULL CHECK (length(run_id) > 0),\n generation ${Dialect.integer(sql)} NOT NULL CHECK (${\n Dialect.isInteger(sql, sql`generation`)\n } AND generation > 0 AND generation <= 9007199254740991),\n after_seq ${Dialect.integer(sql)} NOT NULL CHECK (${\n Dialect.isInteger(sql, sql`after_seq`)\n } AND after_seq >= -1 AND after_seq <= 9007199254740991)\n )\n `\n // The journal layer and time travel's migration both run this, side by side\n // on a host that builds them together. PostgreSQL serializes only what opens\n // a transaction, which takes the schema's writer lock first: two bare\n // CREATE TABLE IF NOT EXISTS statements race in the catalog, and the loser\n // fails on `pg_type_typname_nsp_index`.\n yield* sql.onDialectOrElse({ pg: () => sql.withTransaction(create), orElse: () => create })\n})\n\nconst journals = new WeakMap<SqlClient.SqlClient, Set<(runIds: ReadonlyArray<string>) => void>>()\n\n/**\n * Registers a live journal's cache invalidation until its scope closes.\n * Multiple journals over the same SQL client must observe the same rewind.\n *\n * @category lifecycle\n * @since 1.0.0-rc.0\n */\nexport const onTruncate = (forget: (runIds: ReadonlyArray<string>) => void) =>\n Effect.gen(function*() {\n const sql = yield* SqlClient.SqlClient\n let listeners = journals.get(sql)\n if (listeners === undefined) {\n listeners = new Set()\n journals.set(sql, listeners)\n }\n const registered = listeners\n yield* Effect.acquireRelease(\n Effect.sync(() => registered.add(forget)),\n () =>\n Effect.sync(() => {\n registered.delete(forget)\n })\n )\n })\n\n/**\n * Forgets cached identities and allocation floors after a committed truncation.\n * The rewinder must quiesce producers and flush pending admissions first.\n * Database rows remain authoritative; this operation changes no durable data.\n *\n * @category lifecycle\n * @since 1.0.0-rc.0\n */\nexport const forget = (runIds: ReadonlyArray<string>) =>\n Effect.gen(function*() {\n const sql = yield* SqlClient.SqlClient\n yield* Effect.sync(() => {\n for (const invalidate of journals.get(sql) ?? []) invalidate(runIds)\n })\n })\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAMA,cAAyB;AACzB,aAAwB;AACxB,gBAA2B;AAUpB,MAAM,aAAa,OAAO,IAAI,aAAY;AAC/C,QAAM,MAAM,OAAO,UAAU;AAE7B,MAAI,QAAQ,WAAW,GAAG,EAAG;AAC7B,QAAM,SAAS;AAAA;AAAA;AAAA,mBAGE,QAAQ,QAAQ,GAAG,CAAC,oBACnC,QAAQ,UAAU,KAAK,eAAe,CACxC;AAAA,kBACgB,QAAQ,QAAQ,GAAG,CAAC,oBAClC,QAAQ,UAAU,KAAK,cAAc,CACvC;AAAA;AAAA;AAQA,SAAO,IAAI,gBAAgB,EAAE,IAAI,MAAM,IAAI,gBAAgB,MAAM,GAAG,QAAQ,MAAM,OAAO,CAAC;AAC5F,CAAC;AAED,MAAM,WAAW,oBAAI,QAA2E;AASzF,MAAM,aAAa,CAACA,YACzB,OAAO,IAAI,aAAY;AACrB,QAAM,MAAM,OAAO,UAAU;AAC7B,MAAI,YAAY,SAAS,IAAI,GAAG;AAChC,MAAI,cAAc,QAAW;AAC3B,gBAAY,oBAAI,IAAI;AACpB,aAAS,IAAI,KAAK,SAAS;AAAA,EAC7B;AACA,QAAM,aAAa;AACnB,SAAO,OAAO;AAAA,IACZ,OAAO,KAAK,MAAM,WAAW,IAAIA,OAAM,CAAC;AAAA,IACxC,MACE,OAAO,KAAK,MAAM;AAChB,iBAAW,OAAOA,OAAM;AAAA,IAC1B,CAAC;AAAA,EACL;AACF,CAAC;AAUI,MAAM,SAAS,CAAC,WACrB,OAAO,IAAI,aAAY;AACrB,QAAM,MAAM,OAAO,UAAU;AAC7B,SAAO,OAAO,KAAK,MAAM;AACvB,eAAW,cAAc,SAAS,IAAI,GAAG,KAAK,CAAC,EAAG,YAAW,MAAM;AAAA,EACrE,CAAC;AACH,CAAC;",
|
|
6
6
|
"names": ["forget"]
|
|
7
7
|
}
|
package/dist/cjs/Migrations.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export declare const set: DatabaseMigrations.MigrationSet;
|
|
|
30
30
|
* @category migrations
|
|
31
31
|
* @since 0.1.0
|
|
32
32
|
*/
|
|
33
|
-
export declare const run: import("effect/Effect").Effect<readonly (readonly [id: number, name: string])[], import("effect/
|
|
33
|
+
export declare const run: import("effect/Effect").Effect<readonly (readonly [id: number, name: string])[], import("effect/sql/SqlError").SqlError | import("effect/sql/Migrator").MigrationError, import("effect/sql/SqlClient").SqlClient>;
|
|
34
34
|
/**
|
|
35
35
|
* Layer that runs journal migrations before exposing the database to journal
|
|
36
36
|
* services.
|
|
@@ -38,5 +38,5 @@ export declare const run: import("effect/Effect").Effect<readonly (readonly [id:
|
|
|
38
38
|
* @category layers
|
|
39
39
|
* @since 0.1.0
|
|
40
40
|
*/
|
|
41
|
-
export declare const layer: Layer.Layer<never, import("effect/
|
|
41
|
+
export declare const layer: Layer.Layer<never, import("effect/sql/SqlError").SqlError | import("effect/sql/Migrator").MigrationError, import("effect/sql/SqlClient").SqlClient>;
|
|
42
42
|
//# sourceMappingURL=Migrations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Migrations.d.ts","sourceRoot":"","sources":["../../src/Migrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,kBAAkB,MAAM,6BAA6B,CAAA;AACjE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAQrC;;;;;;GAMG;AACH,eAAO,MAAM,GAAG,EAAE,kBAAkB,CAAC,YAWpC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,GAAG,
|
|
1
|
+
{"version":3,"file":"Migrations.d.ts","sourceRoot":"","sources":["../../src/Migrations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,kBAAkB,MAAM,6BAA6B,CAAA;AACjE,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAQrC;;;;;;GAMG;AACH,eAAO,MAAM,GAAG,EAAE,kBAAkB,CAAC,YAWpC,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,GAAG,mNAAgC,CAAA;AAEhD;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,qJAA2B,CAAA"}
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
import { DurableWriter } from "@smthrs/database/DurableWriter";
|
|
25
25
|
import * as Effect from "effect/Effect";
|
|
26
26
|
import * as Layer from "effect/Layer";
|
|
27
|
-
import * as SqlClient from "effect/
|
|
27
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
28
28
|
import { Consensus, type Service } from "./Consensus.ts";
|
|
29
29
|
/**
|
|
30
30
|
* Constructs the database-backed strategy over the context's SQL client and
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqlConsensus.d.ts","sourceRoot":"","sources":["../../src/SqlConsensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAA;AAE9D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,SAAS,MAAM
|
|
1
|
+
{"version":3,"file":"SqlConsensus.d.ts","sourceRoot":"","sources":["../../src/SqlConsensus.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAA;AAE9D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAIL,SAAS,EAUT,KAAK,OAAO,EACb,MAAM,gBAAgB,CAAA;AAoBvB;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,aAAa,GAAG,SAAS,CAAC,SAAS,CAuQlF,CAAA;AAEF;;;;;GAKG;AACH,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,KAAK,EAAE,aAAa,GAAG,SAAS,CAAC,SAAS,CAAiC,CAAA"}
|
package/dist/cjs/SqlConsensus.js
CHANGED
|
@@ -37,7 +37,7 @@ var import_DurableWriter = require("@smthrs/database/DurableWriter");
|
|
|
37
37
|
var Duration = __toESM(require("effect/Duration"), 1);
|
|
38
38
|
var Effect = __toESM(require("effect/Effect"), 1);
|
|
39
39
|
var Layer = __toESM(require("effect/Layer"), 1);
|
|
40
|
-
var SqlClient = __toESM(require("effect/
|
|
40
|
+
var SqlClient = __toESM(require("effect/sql/SqlClient"), 1);
|
|
41
41
|
var import_Consensus = require("./Consensus.js");
|
|
42
42
|
const staleAfterMs = Duration.toMillis(import_Consensus.heartbeatStaleAfter);
|
|
43
43
|
const rejected = (reason) => ({ _tag: "Rejected", reason });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/SqlConsensus.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * The default database-backed consensus strategy.\n *\n * This is the run store's former `flows_runs` ownership compare-and-swap\n * relocated: the owner tuple, the two-phase claim columns, claim recovery, and\n * the grant timestamp are the private schema of one strategy \u2014 the\n * `flows_consensus_leases` table \u2014 instead of a public contract on the run\n * row. The run row still mirrors the outcome, but the lease is what\n * arbitrates.\n *\n * Commit-time admission (R3) is exact: `guard` is a plain SELECT that joins\n * the caller's open write transaction \u2014 `DurableWriter` serializes write\n * transactions, so no reclaim can commit between the guard read and the\n * statements beside it \u2014 and every mutating operation runs through\n * `DurableWriter.write`, joining an enclosing transaction as a savepoint.\n * Prior art: Temporal's shard `rangeID` check\n * (`reference/temporal/service/history/shard/context_impl.go`,\n * `renewRangeLocked`).\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n *\n * @since 1.0.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport { DurableWriter } from \"@smthrs/database/DurableWriter\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as SqlClient from \"effect/unstable/sql/SqlClient\"\nimport {\n type ActivateOutcome,\n type Claimed,\n type ClaimOutcome,\n Consensus,\n ConsensusError,\n fenceLost,\n type HeartbeatOutcome,\n heartbeatStaleAfter,\n type LivenessEvidence,\n matchesEvidence,\n type RecoverOutcome,\n type Rejected,\n type RejectionReason,\n type Service\n} from \"./Consensus.ts\"\nimport type { OwnerId } from \"./OwnerId.ts\"\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost = { _tag: \"Lost\" } as const\nconst activated = { _tag: \"Activated\" } as const\nconst recovered = { _tag: \"Recovered\" } as const\n\ninterface LeaseRow {\n readonly owner_host_id: string | null\n readonly owner_pid: number | null\n readonly owner_nonce: string | null\n readonly claim_host_id: string | null\n readonly claim_pid: number | null\n readonly claim_nonce: string | null\n}\n\n/**\n * Constructs the database-backed strategy over the context's SQL client and\n * durable writer.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make: Effect.Effect<Service, never, DurableWriter | SqlClient.SqlClient> = Effect.gen(function*() {\n const sql = yield* Effect.service(SqlClient.SqlClient)\n const writer = yield* DurableWriter\n\n const strategyError = (cause: unknown): ConsensusError =>\n new ConsensusError({\n code: \"persistence_failed\",\n message: \"consensus lease operation failed\",\n cause\n })\n\n // The original cause is preserved through the wrapper so an enclosing\n // `DurableWriter.write`'s retry classification still sees a transient\n // conflict raised by a savepoint-nested lease statement.\n const write = <A>(\n body: Effect.Effect<A, unknown>\n ): Effect.Effect<A, ConsensusError> =>\n writer.write(body).pipe(Effect.mapError((cause) => cause instanceof ConsensusError ? cause : strategyError(cause)))\n\n const leaseOf = (runId: string) =>\n sql<LeaseRow>`\n SELECT owner_host_id, owner_pid, owner_nonce, claim_host_id, claim_pid, claim_nonce\n FROM flows_consensus_leases\n WHERE run_id = ${runId}\n `\n\n const prune = (runId: string) =>\n sql`\n DELETE FROM flows_consensus_leases\n WHERE run_id = ${runId} AND owner_host_id IS NULL AND claim_host_id IS NULL\n `\n\n const claim: Service[\"claim\"] = Effect.fn(\"Consensus.claim\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n INSERT INTO flows_consensus_leases (run_id, claim_host_id, claim_pid, claim_nonce, claimed_at_ms)\n VALUES (${runId}, ${claimant.hostId}, ${claimant.pid}, ${claimant.nonce}, ${nowMs})\n ON CONFLICT (run_id) DO UPDATE SET\n claim_host_id = excluded.claim_host_id,\n claim_pid = excluded.claim_pid,\n claim_nonce = excluded.claim_nonce,\n claimed_at_ms = excluded.claimed_at_ms\n WHERE flows_consensus_leases.claim_host_id IS NULL\n AND flows_consensus_leases.owner_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n // The upsert only misses on a conflicting row, so the lease exists and\n // holds a claim, an owner, or both.\n const lease = (yield* leaseOf(runId))[0]!\n return lease.claim_host_id !== null ? rejected(\"already_claimed\") : rejected(\"owner_held\")\n }))\n )\n\n const activate: Service[\"activate\"] = Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n owner_host_id = ${owner.hostId},\n owner_pid = ${owner.pid},\n owner_nonce = ${owner.nonce},\n granted_at_ms = ${grantedAtMs},\n heartbeat_at_ms = ${nowMs},\n claim_host_id = NULL,\n claim_pid = NULL,\n claim_nonce = NULL,\n claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n RETURNING run_id\n `\n return (rows.length > 0 ? activated : lost) satisfies ActivateOutcome\n }))\n )\n\n const heartbeat: Service[\"heartbeat\"] = Effect.fn(\"Consensus.heartbeat\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // Monotonic: a pulse delayed past a newer one from the same owner never\n // moves the lease backwards and never makes a live run look stale.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const reconfirm: Service[\"reconfirm\"] = Effect.fn(\"Consensus.reconfirm\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // A pending steal has already reserved the lease, even before activation.\n // Compare both owner and claim in the same update as the renewal.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n AND claim_host_id IS NULL\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const release: Service[\"release\"] = Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n yield* sql`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n `\n yield* sql`\n UPDATE flows_consensus_leases\n SET owner_host_id = NULL, owner_pid = NULL, owner_nonce = NULL,\n granted_at_ms = NULL, heartbeat_at_ms = NULL\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n yield* prune(runId)\n }))\n )\n\n const steal: Service[\"steal\"] = Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<ClaimOutcome, ConsensusError> => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n claim_host_id = ${claimant.hostId},\n claim_pid = ${claimant.pid},\n claim_nonce = ${claimant.nonce},\n claimed_at_ms = ${nowMs}\n WHERE run_id = ${runId}\n AND owner_host_id = ${evidence.expectedOwner.hostId}\n AND owner_pid = ${evidence.expectedOwner.pid}\n AND owner_nonce = ${evidence.expectedOwner.nonce}\n AND heartbeat_at_ms < ${nowMs - staleAfterMs}\n AND claim_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n const lease = (yield* leaseOf(runId))[0]\n if (lease !== undefined && lease.claim_host_id !== null) return rejected(\"already_claimed\")\n if (\n lease === undefined ||\n lease.owner_host_id !== evidence.expectedOwner.hostId ||\n Number(lease.owner_pid) !== evidence.expectedOwner.pid ||\n lease.owner_nonce !== evidence.expectedOwner.nonce\n ) {\n // A run nobody owns, or one owned by somebody else, has no owner\n // this evidence could disprove.\n return rejected(\"evidence_invalid\")\n }\n return rejected(\"owner_live\")\n }))\n })\n )\n\n const recover: Service[\"recover\"] = Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<RecoverOutcome, ConsensusError> => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n AND claimed_at_ms < ${nowMs - staleAfterMs}\n RETURNING run_id\n `\n if (rows.length > 0) {\n yield* prune(runId)\n return recovered\n }\n const matches = yield* sql<{ readonly run_id: string }>`\n SELECT run_id FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n `\n // The exact claim survived the compare-and-swap only because it is\n // not yet stale; anything else means the claim moved on.\n return matches.length > 0 ? rejected(\"claim_fresh\") : rejected(\"claim_changed\")\n }))\n })\n )\n\n const guard: Service[\"guard\"] = Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n const held = yield* sql<{ readonly ok: number }>`\n SELECT 1 AS ok FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n if (held.length === 0) {\n return yield* Effect.fail(fenceLost(runId, owner))\n }\n }))\n )\n\n return Consensus.of({ claim, activate, heartbeat, reconfirm, release, steal, recover, guard })\n})\n\n/**\n * Provides the database-backed strategy.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layer: Layer.Layer<Consensus, never, DurableWriter | SqlClient.SqlClient> = Layer.effect(Consensus, make)\n"],
|
|
4
|
+
"sourcesContent": ["/**\n * The default database-backed consensus strategy.\n *\n * This is the run store's former `flows_runs` ownership compare-and-swap\n * relocated: the owner tuple, the two-phase claim columns, claim recovery, and\n * the grant timestamp are the private schema of one strategy \u2014 the\n * `flows_consensus_leases` table \u2014 instead of a public contract on the run\n * row. The run row still mirrors the outcome, but the lease is what\n * arbitrates.\n *\n * Commit-time admission (R3) is exact: `guard` is a plain SELECT that joins\n * the caller's open write transaction \u2014 `DurableWriter` serializes write\n * transactions, so no reclaim can commit between the guard read and the\n * statements beside it \u2014 and every mutating operation runs through\n * `DurableWriter.write`, joining an enclosing transaction as a savepoint.\n * Prior art: Temporal's shard `rangeID` check\n * (`reference/temporal/service/history/shard/context_impl.go`,\n * `renewRangeLocked`).\n *\n * Governing design: `packages/smithers/flows/journal/docs/concepts/owner-fence.md`.\n *\n * @since 1.0.0\n */\n\nimport * as Dialect from \"@smthrs/database/Dialect\"\nimport { DurableWriter } from \"@smthrs/database/DurableWriter\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as SqlClient from \"effect/sql/SqlClient\"\nimport {\n type ActivateOutcome,\n type Claimed,\n type ClaimOutcome,\n Consensus,\n ConsensusError,\n fenceLost,\n type HeartbeatOutcome,\n heartbeatStaleAfter,\n type LivenessEvidence,\n matchesEvidence,\n type RecoverOutcome,\n type Rejected,\n type RejectionReason,\n type Service\n} from \"./Consensus.ts\"\nimport type { OwnerId } from \"./OwnerId.ts\"\n\nconst staleAfterMs = Duration.toMillis(heartbeatStaleAfter)\n\nconst rejected = (reason: RejectionReason): Rejected => ({ _tag: \"Rejected\", reason })\nconst claimed = (grantedAtMs: number): Claimed => ({ _tag: \"Claimed\", grantedAtMs })\nconst lost = { _tag: \"Lost\" } as const\nconst activated = { _tag: \"Activated\" } as const\nconst recovered = { _tag: \"Recovered\" } as const\n\ninterface LeaseRow {\n readonly owner_host_id: string | null\n readonly owner_pid: number | null\n readonly owner_nonce: string | null\n readonly claim_host_id: string | null\n readonly claim_pid: number | null\n readonly claim_nonce: string | null\n}\n\n/**\n * Constructs the database-backed strategy over the context's SQL client and\n * durable writer.\n *\n * @since 1.0.0\n * @category constructors\n */\nexport const make: Effect.Effect<Service, never, DurableWriter | SqlClient.SqlClient> = Effect.gen(function*() {\n const sql = yield* Effect.service(SqlClient.SqlClient)\n const writer = yield* DurableWriter\n\n const strategyError = (cause: unknown): ConsensusError =>\n new ConsensusError({\n code: \"persistence_failed\",\n message: \"consensus lease operation failed\",\n cause\n })\n\n // The original cause is preserved through the wrapper so an enclosing\n // `DurableWriter.write`'s retry classification still sees a transient\n // conflict raised by a savepoint-nested lease statement.\n const write = <A>(\n body: Effect.Effect<A, unknown>\n ): Effect.Effect<A, ConsensusError> =>\n writer.write(body).pipe(Effect.mapError((cause) => cause instanceof ConsensusError ? cause : strategyError(cause)))\n\n const leaseOf = (runId: string) =>\n sql<LeaseRow>`\n SELECT owner_host_id, owner_pid, owner_nonce, claim_host_id, claim_pid, claim_nonce\n FROM flows_consensus_leases\n WHERE run_id = ${runId}\n `\n\n const prune = (runId: string) =>\n sql`\n DELETE FROM flows_consensus_leases\n WHERE run_id = ${runId} AND owner_host_id IS NULL AND claim_host_id IS NULL\n `\n\n const claim: Service[\"claim\"] = Effect.fn(\"Consensus.claim\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n INSERT INTO flows_consensus_leases (run_id, claim_host_id, claim_pid, claim_nonce, claimed_at_ms)\n VALUES (${runId}, ${claimant.hostId}, ${claimant.pid}, ${claimant.nonce}, ${nowMs})\n ON CONFLICT (run_id) DO UPDATE SET\n claim_host_id = excluded.claim_host_id,\n claim_pid = excluded.claim_pid,\n claim_nonce = excluded.claim_nonce,\n claimed_at_ms = excluded.claimed_at_ms\n WHERE flows_consensus_leases.claim_host_id IS NULL\n AND flows_consensus_leases.owner_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n // The upsert only misses on a conflicting row, so the lease exists and\n // holds a claim, an owner, or both.\n const lease = (yield* leaseOf(runId))[0]!\n return lease.claim_host_id !== null ? rejected(\"already_claimed\") : rejected(\"owner_held\")\n }))\n )\n\n const activate: Service[\"activate\"] = Effect.fn(\"Consensus.activate\")((\n runId: string,\n owner: OwnerId,\n grantedAtMs: number,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n owner_host_id = ${owner.hostId},\n owner_pid = ${owner.pid},\n owner_nonce = ${owner.nonce},\n granted_at_ms = ${grantedAtMs},\n heartbeat_at_ms = ${nowMs},\n claim_host_id = NULL,\n claim_pid = NULL,\n claim_nonce = NULL,\n claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n RETURNING run_id\n `\n return (rows.length > 0 ? activated : lost) satisfies ActivateOutcome\n }))\n )\n\n const heartbeat: Service[\"heartbeat\"] = Effect.fn(\"Consensus.heartbeat\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // Monotonic: a pulse delayed past a newer one from the same owner never\n // moves the lease backwards and never makes a live run look stale.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const reconfirm: Service[\"reconfirm\"] = Effect.fn(\"Consensus.reconfirm\")((\n runId: string,\n owner: OwnerId,\n nowMs: number\n ) =>\n write(Effect.gen(function*() {\n // A pending steal has already reserved the lease, even before activation.\n // Compare both owner and claim in the same update as the renewal.\n const rows = yield* sql<{ readonly heartbeat_at_ms: number }>`\n UPDATE flows_consensus_leases\n SET heartbeat_at_ms = ${Dialect.greatest(sql)}(heartbeat_at_ms, ${nowMs})\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n AND claim_host_id IS NULL\n RETURNING heartbeat_at_ms\n `\n const row = rows[0]\n return (row === undefined\n ? lost\n : { _tag: \"Renewed\", heartbeatAtMs: Number(row.heartbeat_at_ms) }) satisfies HeartbeatOutcome\n }))\n )\n\n const release: Service[\"release\"] = Effect.fn(\"Consensus.release\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n yield* sql`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${owner.hostId}\n AND claim_pid = ${owner.pid}\n AND claim_nonce = ${owner.nonce}\n `\n yield* sql`\n UPDATE flows_consensus_leases\n SET owner_host_id = NULL, owner_pid = NULL, owner_nonce = NULL,\n granted_at_ms = NULL, heartbeat_at_ms = NULL\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n yield* prune(runId)\n }))\n )\n\n const steal: Service[\"steal\"] = Effect.fn(\"Consensus.steal\")((\n runId: string,\n claimant: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<ClaimOutcome, ConsensusError> => {\n if (!matchesEvidence(evidence.expectedOwner, claimant, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET\n claim_host_id = ${claimant.hostId},\n claim_pid = ${claimant.pid},\n claim_nonce = ${claimant.nonce},\n claimed_at_ms = ${nowMs}\n WHERE run_id = ${runId}\n AND owner_host_id = ${evidence.expectedOwner.hostId}\n AND owner_pid = ${evidence.expectedOwner.pid}\n AND owner_nonce = ${evidence.expectedOwner.nonce}\n AND heartbeat_at_ms < ${nowMs - staleAfterMs}\n AND claim_host_id IS NULL\n RETURNING run_id\n `\n if (rows.length > 0) return claimed(nowMs)\n const lease = (yield* leaseOf(runId))[0]\n if (lease !== undefined && lease.claim_host_id !== null) return rejected(\"already_claimed\")\n if (\n lease === undefined ||\n lease.owner_host_id !== evidence.expectedOwner.hostId ||\n Number(lease.owner_pid) !== evidence.expectedOwner.pid ||\n lease.owner_nonce !== evidence.expectedOwner.nonce\n ) {\n // A run nobody owns, or one owned by somebody else, has no owner\n // this evidence could disprove.\n return rejected(\"evidence_invalid\")\n }\n return rejected(\"owner_live\")\n }))\n })\n )\n\n const recover: Service[\"recover\"] = Effect.fn(\"Consensus.recover\")((\n runId: string,\n staleClaimant: OwnerId,\n grantedAtMs: number,\n observer: OwnerId,\n nowMs: number,\n evidence: LivenessEvidence\n ) =>\n Effect.suspend((): Effect.Effect<RecoverOutcome, ConsensusError> => {\n if (!matchesEvidence(staleClaimant, observer, nowMs, evidence)) {\n return Effect.succeed(rejected(\"evidence_invalid\"))\n }\n return write(Effect.gen(function*() {\n const rows = yield* sql<{ readonly run_id: string }>`\n UPDATE flows_consensus_leases\n SET claim_host_id = NULL, claim_pid = NULL, claim_nonce = NULL, claimed_at_ms = NULL\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n AND claimed_at_ms < ${nowMs - staleAfterMs}\n RETURNING run_id\n `\n if (rows.length > 0) {\n yield* prune(runId)\n return recovered\n }\n const matches = yield* sql<{ readonly run_id: string }>`\n SELECT run_id FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND claim_host_id = ${staleClaimant.hostId}\n AND claim_pid = ${staleClaimant.pid}\n AND claim_nonce = ${staleClaimant.nonce}\n AND claimed_at_ms = ${grantedAtMs}\n `\n // The exact claim survived the compare-and-swap only because it is\n // not yet stale; anything else means the claim moved on.\n return matches.length > 0 ? rejected(\"claim_fresh\") : rejected(\"claim_changed\")\n }))\n })\n )\n\n const guard: Service[\"guard\"] = Effect.fn(\"Consensus.guard\")((runId: string, owner: OwnerId) =>\n write(Effect.gen(function*() {\n const held = yield* sql<{ readonly ok: number }>`\n SELECT 1 AS ok FROM flows_consensus_leases\n WHERE run_id = ${runId}\n AND owner_host_id = ${owner.hostId}\n AND owner_pid = ${owner.pid}\n AND owner_nonce = ${owner.nonce}\n `\n if (held.length === 0) {\n return yield* Effect.fail(fenceLost(runId, owner))\n }\n }))\n )\n\n return Consensus.of({ claim, activate, heartbeat, reconfirm, release, steal, recover, guard })\n})\n\n/**\n * Provides the database-backed strategy.\n *\n * @since 1.0.0\n * @category layers\n */\nexport const layer: Layer.Layer<Consensus, never, DurableWriter | SqlClient.SqlClient> = Layer.effect(Consensus, make)\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwBA,cAAyB;AACzB,2BAA8B;AAC9B,eAA0B;AAC1B,aAAwB;AACxB,YAAuB;AACvB,gBAA2B;AAC3B,uBAeO;AAGP,MAAM,eAAe,SAAS,SAAS,oCAAmB;AAE1D,MAAM,WAAW,CAAC,YAAuC,EAAE,MAAM,YAAY,OAAO;AACpF,MAAM,UAAU,CAAC,iBAAkC,EAAE,MAAM,WAAW,YAAY;AAClF,MAAM,OAAO,EAAE,MAAM,OAAO;AAC5B,MAAM,YAAY,EAAE,MAAM,YAAY;AACtC,MAAM,YAAY,EAAE,MAAM,YAAY;AAkB/B,MAAM,OAA2E,OAAO,IAAI,aAAY;AAC7G,QAAM,M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|
7
7
|
}
|
package/dist/cjs/SqlJournal.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ import { DurableWriter } from "@smthrs/database/DurableWriter";
|
|
|
21
21
|
import * as Effect from "effect/Effect";
|
|
22
22
|
import * as Layer from "effect/Layer";
|
|
23
23
|
import * as Scope from "effect/Scope";
|
|
24
|
-
import * as SqlClient from "effect/
|
|
24
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
25
25
|
import { Consensus } from "./Consensus.ts";
|
|
26
26
|
import { Journal, JournalError, type OverflowPolicy, type Service } from "./Journal.ts";
|
|
27
27
|
import { RunId, type Seq } from "./JournalEvent.ts";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SqlJournal.d.ts","sourceRoot":"","sources":["../../src/SqlJournal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAA8B,aAAa,EAAE,MAAM,gCAAgC,CAAA;AAI1F,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAOrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"SqlJournal.d.ts","sourceRoot":"","sources":["../../src/SqlJournal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAA8B,aAAa,EAAE,MAAM,gCAAgC,CAAA;AAI1F,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAOrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAA;AAGjD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE1C,OAAO,EASL,OAAO,EACP,YAAY,EAGZ,KAAK,cAAc,EACnB,KAAK,OAAO,EAEb,MAAM,cAAc,CAAA;AACrB,OAAO,EAKL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AAiB,KACzB,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,SAAS,GAAG,aAAa,GAAG,SAAS,CAAC,SAAS,CAC/C,CAAA;AAEtC;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,GAChB,SAAS,iBAAiB,KACzB,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,aAAa,GAAG,SAAS,CAAC,SAAS,CACb,CAAA"}
|
package/dist/cjs/SqlJournal.js
CHANGED
|
@@ -49,8 +49,8 @@ var Result = __toESM(require("effect/Result"), 1);
|
|
|
49
49
|
var Schema = __toESM(require("effect/Schema"), 1);
|
|
50
50
|
var Scope = __toESM(require("effect/Scope"), 1);
|
|
51
51
|
var Semaphore = __toESM(require("effect/Semaphore"), 1);
|
|
52
|
+
var SqlClient = __toESM(require("effect/sql/SqlClient"), 1);
|
|
52
53
|
var Stream = __toESM(require("effect/Stream"), 1);
|
|
53
|
-
var SqlClient = __toESM(require("effect/unstable/sql/SqlClient"), 1);
|
|
54
54
|
var import_Consensus = require("./Consensus.js");
|
|
55
55
|
var import_JournalInternalFault = require("./internal/JournalInternalFault.js");
|
|
56
56
|
var import_Journal = require("./Journal.js");
|
|
@@ -1507,7 +1507,7 @@ const make = (options) => Effect.gen(function* () {
|
|
|
1507
1507
|
...activeOptions.afterSequence === void 0 ? {} : { afterSequence: activeOptions.afterSequence }
|
|
1508
1508
|
}).pipe(Effect.andThen(Effect.succeed(
|
|
1509
1509
|
stream(activeOptions).pipe(
|
|
1510
|
-
Stream.scanEffect(activeProjection.initial, (state2, entry) => Effect.suspend(() => activeProjection.reduce(state2, entry)).pipe(
|
|
1510
|
+
Stream.scanEffect(() => activeProjection.initial, (state2, entry) => Effect.suspend(() => activeProjection.reduce(state2, entry)).pipe(
|
|
1511
1511
|
Effect.catchCause(
|
|
1512
1512
|
(cause) => Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.fail(
|
|
1513
1513
|
error(
|