nowity 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -6
- package/dist/core/types.d.ts +9 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/server/api/runtime.d.ts +16 -0
- package/dist/server/api/runtime.d.ts.map +1 -1
- package/dist/server/api/runtime.js +2 -1
- package/dist/server/change-source/change-source.d.ts +4 -2
- package/dist/server/change-source/change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.d.ts +35 -0
- package/dist/server/change-source/outbox-change-source.d.ts.map +1 -1
- package/dist/server/change-source/outbox-change-source.js +23 -1
- package/dist/server/sync-engine.d.ts +6 -1
- package/dist/server/sync-engine.d.ts.map +1 -1
- package/dist/server/sync-engine.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1007,6 +1007,26 @@ invisible in the snapshot is above the cursor and gets streamed. Delivery is
|
|
|
1007
1007
|
at-least-once; because patches come from re-running the query and diffing, a replay
|
|
1008
1008
|
produces an empty diff, so the client observes exactly once.
|
|
1009
1009
|
|
|
1010
|
+
### One snapshot per read
|
|
1011
|
+
|
|
1012
|
+
Every transaction one read of the outbox returns has committed before the engine
|
|
1013
|
+
opens its snapshot, so they are fanned out together: each subscription they touch
|
|
1014
|
+
is recomputed once, and the mutation watermarks of every client that wrote are
|
|
1015
|
+
read in one statement. Under a burst that is a handful of read transactions a
|
|
1016
|
+
second instead of one per write. A client never sees half of a transaction —
|
|
1017
|
+
the snapshot is taken after all of them — but it may skip the states between two
|
|
1018
|
+
of them, which the next snapshot would have overwritten anyway. A
|
|
1019
|
+
hand-built `OutboxChangeSource` can turn it off with `coalesce: false`; the WAL
|
|
1020
|
+
source still delivers one transaction at a time.
|
|
1021
|
+
|
|
1022
|
+
Snapshots are interactive transactions. On the application's own Prisma client
|
|
1023
|
+
they wait in the same pool as its writes, and under load each starves the other.
|
|
1024
|
+
`createNowityServer({ snapshotPrisma })` takes a second client of the same schema
|
|
1025
|
+
— typically the same URL with a small `connection_limit` — and runs every
|
|
1026
|
+
snapshot there: the fan-out, the first answer of a subscription, server-side
|
|
1027
|
+
rendering in that process. The engine fans out one batch at a time, so a pool of
|
|
1028
|
+
a few connections is plenty.
|
|
1029
|
+
|
|
1010
1030
|
## Known limitations
|
|
1011
1031
|
|
|
1012
1032
|
- **A long-running write transaction stalls the outbox stream** until it finishes
|
|
@@ -1018,12 +1038,6 @@ produces an empty diff, so the client observes exactly once.
|
|
|
1018
1038
|
table (see above), so what is left is one database round trip per subscription
|
|
1019
1039
|
the change could actually have altered — about 1.3ms each, and the dominant
|
|
1020
1040
|
term in every remaining measurement.
|
|
1021
|
-
- **Change batches are not coalesced.** Each committed transaction gets its own
|
|
1022
|
-
fan-out pass, including its own snapshot, even when fifty of them are already
|
|
1023
|
-
queued. Merging what is waiting would save about a quarter of the wall time
|
|
1024
|
-
under a burst (measured: 250 moves/s in `npm run perf -- throughput`), at the
|
|
1025
|
-
cost of the change source's "one batch per transaction" contract. Not done:
|
|
1026
|
-
the contract is relied on elsewhere, and the ratio does not justify it yet.
|
|
1027
1041
|
- **Read keys narrow by equality only.** A range, a `contains`, a `NOT` or a
|
|
1028
1042
|
case-insensitive comparison contributes no narrowing, and a query with nothing
|
|
1029
1043
|
else to go on watches its whole table. The cost is the pre-existing one; the
|
package/dist/core/types.d.ts
CHANGED
|
@@ -76,8 +76,16 @@ export interface TransactionBatch {
|
|
|
76
76
|
* `xid` for the outbox source, an LSN for the WAL source.
|
|
77
77
|
*/
|
|
78
78
|
position: string;
|
|
79
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Database transaction id (or LSN), for logging and ordering assertions.
|
|
81
|
+
* The last one, when the batch carries several transactions.
|
|
82
|
+
*/
|
|
80
83
|
xid: string;
|
|
84
|
+
/**
|
|
85
|
+
* How many whole transactions the batch carries, in commit order. Absent
|
|
86
|
+
* means one.
|
|
87
|
+
*/
|
|
88
|
+
transactions?: number;
|
|
81
89
|
events: ChangeEvent[];
|
|
82
90
|
}
|
|
83
91
|
/** Field-level upsert for a single entity instance. */
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,6EAA6E;AAC7E,MAAM,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE1C,4EAA4E;AAC5E,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE9B,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEtD,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAExD;;;;;GAKG;AACH,MAAM,WAAW,OAAO;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,QAAQ,CAAC;CACnB;AAED,kDAAkD;AAClD,eAAO,MAAM,QAAQ,EAAG,KAAc,CAAC;AAEvC,iFAAiF;AACjF,MAAM,MAAM,SAAS,GAAG,OAAO,EAAE,GAAG,OAAO,QAAQ,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAEhD;;;;;;GAMG;AACH,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,QAAQ,CAAC;IACb,+DAA+D;IAC/D,EAAE,EAAE,GAAG,CAAC;IACR,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC;IACnB,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC;IAClB,qEAAqE;IACrE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,0EAA0E;IAC1E,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;;;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C;IAC1B;;;;;;;;;OASG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC7B;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,WAAW,EAAE,CAAC;CACzB;AAED,uDAAuD;AACvD,MAAM,WAAW,QAAQ;IACrB,EAAE,EAAE,QAAQ,CAAC;IACb,sEAAsE;IACtE,MAAM,EAAE,GAAG,CAAC;IACZ,oCAAoC;IACpC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,yEAAyE;AACzE,MAAM,WAAW,OAAO;IACpB,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnB,MAAM,EAAE,QAAQ,EAAE,CAAC;CACtB;AAED,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAElD"}
|
|
@@ -41,6 +41,22 @@ export interface NowityRuntimeConfig {
|
|
|
41
41
|
* hand-written — for comparing the two.
|
|
42
42
|
*/
|
|
43
43
|
derivedMutators?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* A second client of the same schema, used for nothing but read snapshots:
|
|
46
|
+
* the change stream's fan-out, the first answer of a subscription, and
|
|
47
|
+
* server-side rendering.
|
|
48
|
+
*
|
|
49
|
+
* Every snapshot is an interactive transaction, and on a shared client it
|
|
50
|
+
* waits in the same pool as the application's writes. Under load the two
|
|
51
|
+
* starve each other — a write cannot get a connection because the stream
|
|
52
|
+
* holds them, the stream falls behind because writes do. Give it a client
|
|
53
|
+
* with a pool of its own (`datasourceUrl` with `connection_limit`) and the
|
|
54
|
+
* two stop competing. The client is extended for read tracking here, like
|
|
55
|
+
* {@link prisma}; pass the plain one.
|
|
56
|
+
*
|
|
57
|
+
* Absent, snapshots run on {@link prisma}.
|
|
58
|
+
*/
|
|
59
|
+
snapshotPrisma?: any;
|
|
44
60
|
onWarning?: (message: string) => void;
|
|
45
61
|
}
|
|
46
62
|
export interface NowityRuntime {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/server/api/runtime.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,oBAAoB,EAAkB,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAInD,OAAO,EAAE,cAAc,EAAe,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,mBAAmB;IAChC,uCAAuC;IACvC,MAAM,EAAE,GAAG,CAAC;IACZ,gDAAgD;IAChD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB;;;;;OAKG;IACH,EAAE,CAAC,EAAE,QAAQ,CAAC;IACd;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IACtB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,aAAa;IAC1B,GAAG,EAAE,QAAQ,CAAC;IACd,SAAS,EAAE,oBAAoB,CAAC;IAChC,2EAA2E;IAC3E,MAAM,EAAE,GAAG,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/server/api/runtime.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,oBAAoB,EAAkB,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAInD,OAAO,EAAE,cAAc,EAAe,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,mBAAmB;IAChC,uCAAuC;IACvC,MAAM,EAAE,GAAG,CAAC;IACZ,gDAAgD;IAChD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB;;;;;OAKG;IACH,EAAE,CAAC,EAAE,QAAQ,CAAC;IACd;;;;;;;;OAQG;IACH,QAAQ,CAAC,EAAE,QAAQ,EAAE,CAAC;IACtB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;;;;;;;;;;OAcG;IACH,cAAc,CAAC,EAAE,GAAG,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,aAAa;IAC1B,GAAG,EAAE,QAAQ,CAAC;IACd,SAAS,EAAE,oBAAoB,CAAC;IAChC,2EAA2E;IAC3E,MAAM,EAAE,GAAG,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;CAC3B;AAED,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA4C9E"}
|
|
@@ -60,5 +60,6 @@ function createNowityRuntime(config) {
|
|
|
60
60
|
derivedMutators: config.derivedMutators,
|
|
61
61
|
onWarning: config.onWarning,
|
|
62
62
|
});
|
|
63
|
-
|
|
63
|
+
const snapshotClient = config.snapshotPrisma ? (0, tracked_client_1.trackReads)(config.snapshotPrisma, tracker) : prisma;
|
|
64
|
+
return { api, snapshots: new db_1.PrismaSnapshotRunner(snapshotClient), prisma, tracker };
|
|
64
65
|
}
|
|
@@ -9,8 +9,10 @@ export type BatchHandler = (batch: TransactionBatch) => Promise<void>;
|
|
|
9
9
|
* WAL can be swapped in later without touching the sync engine.
|
|
10
10
|
*
|
|
11
11
|
* Contract every implementation must honour:
|
|
12
|
-
* 1. `handler` is called
|
|
13
|
-
*
|
|
12
|
+
* 1. `handler` is called with whole database transactions in commit order —
|
|
13
|
+
* one per call, or several committed ones together (the outbox does that
|
|
14
|
+
* per read, so the engine takes one snapshot for all of them). A
|
|
15
|
+
* transaction is never split across calls.
|
|
14
16
|
* 2. `handler` is awaited; no further batch is delivered until it resolves.
|
|
15
17
|
* 3. Delivery is at-least-once. Duplicates are allowed — the engine recomputes
|
|
16
18
|
* queries from the database, so replaying a batch produces an empty diff.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/change-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAE7C,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEtE
|
|
1
|
+
{"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/change-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAE7C,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEtE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,YAAY;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAEpC,aAAa,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEtD,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1D,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtB,2EAA2E;IAC3E,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Pool } from "pg";
|
|
2
|
+
import type { ChangeEvent, Row, TransactionBatch } from "../../core/types";
|
|
2
3
|
import type { SnapshotContext } from "../db";
|
|
3
4
|
import type { BatchHandler, ChangeSource } from "./change-source";
|
|
4
5
|
export interface OutboxChangeSourceOptions {
|
|
@@ -9,6 +10,18 @@ export interface OutboxChangeSourceOptions {
|
|
|
9
10
|
pollIntervalMs?: number;
|
|
10
11
|
/** Max rows fetched per round-trip (transactions are never split). */
|
|
11
12
|
batchSize?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Hand every whole transaction of one read to the engine as a single
|
|
15
|
+
* batch, so the engine opens one snapshot per read instead of one per
|
|
16
|
+
* application transaction.
|
|
17
|
+
*
|
|
18
|
+
* On unless `false`. Each transaction in the read has committed before the
|
|
19
|
+
* snapshot opens, so a client still never sees half of one; what it no
|
|
20
|
+
* longer sees is the states *between* them, which a later snapshot would
|
|
21
|
+
* have overwritten anyway. Under load that is the difference between a
|
|
22
|
+
* read transaction per write and a few per second.
|
|
23
|
+
*/
|
|
24
|
+
coalesce?: boolean;
|
|
12
25
|
/**
|
|
13
26
|
* Identity of this reader in `nowity_consumer`, for retention.
|
|
14
27
|
*
|
|
@@ -28,6 +41,17 @@ export interface OutboxChangeSourceOptions {
|
|
|
28
41
|
pruneStaleAfter?: string;
|
|
29
42
|
onError?: (err: Error) => void;
|
|
30
43
|
}
|
|
44
|
+
interface OutboxRow {
|
|
45
|
+
seq: string;
|
|
46
|
+
xid: string;
|
|
47
|
+
tbl: string;
|
|
48
|
+
op: ChangeEvent["op"];
|
|
49
|
+
pk: Row;
|
|
50
|
+
before: Row | null;
|
|
51
|
+
after: Row | null;
|
|
52
|
+
client_id: string | null;
|
|
53
|
+
mutation_id: string | null;
|
|
54
|
+
}
|
|
31
55
|
/**
|
|
32
56
|
* Change source built on an outbox table filled by triggers, woken by
|
|
33
57
|
* LISTEN/NOTIFY.
|
|
@@ -58,6 +82,7 @@ export declare class OutboxChangeSource implements ChangeSource {
|
|
|
58
82
|
private readonly channel;
|
|
59
83
|
private readonly pollIntervalMs;
|
|
60
84
|
private readonly batchSize;
|
|
85
|
+
private readonly coalesce;
|
|
61
86
|
private readonly onError;
|
|
62
87
|
readonly consumerId: string;
|
|
63
88
|
private readonly pruneIntervalMs;
|
|
@@ -103,4 +128,14 @@ export declare class OutboxChangeSource implements ChangeSource {
|
|
|
103
128
|
prune(): Promise<number>;
|
|
104
129
|
private fetch;
|
|
105
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Whole transactions of one read as one batch, in commit order.
|
|
133
|
+
*
|
|
134
|
+
* The position is the last transaction's, so a failed handler replays the
|
|
135
|
+
* whole read — which delivery is allowed to do, see the {@link ChangeSource}
|
|
136
|
+
* contract.
|
|
137
|
+
*/
|
|
138
|
+
export declare function mergeTransactions(batches: TransactionBatch[]): TransactionBatch;
|
|
139
|
+
export declare function groupByTransaction(rows: OutboxRow[]): TransactionBatch[];
|
|
140
|
+
export {};
|
|
106
141
|
//# sourceMappingURL=outbox-change-source.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"outbox-change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/outbox-change-source.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"outbox-change-source.d.ts","sourceRoot":"","sources":["../../../src/server/change-source/outbox-change-source.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;AAE3C,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,gBAAgB,E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,EAAE,SAAS,EAAE,GAAG,gBAAgB,EAAE,CAyBxE"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OutboxChangeSource = void 0;
|
|
4
|
+
exports.mergeTransactions = mergeTransactions;
|
|
5
|
+
exports.groupByTransaction = groupByTransaction;
|
|
4
6
|
const node_crypto_1 = require("node:crypto");
|
|
5
7
|
const normalize_1 = require("../../core/normalize");
|
|
6
8
|
const SELECT_COLUMNS = `seq::text AS seq, xid::text AS xid, tbl, op, pk, before, after, client_id, mutation_id::text AS mutation_id`;
|
|
@@ -47,6 +49,7 @@ class OutboxChangeSource {
|
|
|
47
49
|
this.channel = opts.channel ?? "nowity_change";
|
|
48
50
|
this.pollIntervalMs = opts.pollIntervalMs ?? 250;
|
|
49
51
|
this.batchSize = opts.batchSize ?? 500;
|
|
52
|
+
this.coalesce = opts.coalesce ?? true;
|
|
50
53
|
this.consumerId = opts.consumerId ?? `outbox-${(0, node_crypto_1.randomUUID)()}`;
|
|
51
54
|
this.pruneIntervalMs = opts.pruneIntervalMs ?? 0;
|
|
52
55
|
this.pruneStaleAfter = opts.pruneStaleAfter ?? "1 hour";
|
|
@@ -146,7 +149,8 @@ class OutboxChangeSource {
|
|
|
146
149
|
const rows = await this.fetch();
|
|
147
150
|
if (rows.length === 0)
|
|
148
151
|
break;
|
|
149
|
-
|
|
152
|
+
const transactions = groupByTransaction(rows);
|
|
153
|
+
for (const batch of this.coalesce ? [mergeTransactions(transactions)] : transactions) {
|
|
150
154
|
if (!this.handler)
|
|
151
155
|
return;
|
|
152
156
|
await this.handler(batch);
|
|
@@ -211,6 +215,24 @@ class OutboxChangeSource {
|
|
|
211
215
|
}
|
|
212
216
|
}
|
|
213
217
|
exports.OutboxChangeSource = OutboxChangeSource;
|
|
218
|
+
/**
|
|
219
|
+
* Whole transactions of one read as one batch, in commit order.
|
|
220
|
+
*
|
|
221
|
+
* The position is the last transaction's, so a failed handler replays the
|
|
222
|
+
* whole read — which delivery is allowed to do, see the {@link ChangeSource}
|
|
223
|
+
* contract.
|
|
224
|
+
*/
|
|
225
|
+
function mergeTransactions(batches) {
|
|
226
|
+
if (batches.length === 1)
|
|
227
|
+
return batches[0];
|
|
228
|
+
const last = batches[batches.length - 1];
|
|
229
|
+
return {
|
|
230
|
+
position: last.position,
|
|
231
|
+
xid: last.xid,
|
|
232
|
+
transactions: batches.reduce((sum, batch) => sum + (batch.transactions ?? 1), 0),
|
|
233
|
+
events: batches.flatMap((batch) => batch.events),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
214
236
|
function groupByTransaction(rows) {
|
|
215
237
|
const batches = [];
|
|
216
238
|
let current = null;
|
|
@@ -172,11 +172,16 @@ export declare class SyncEngine {
|
|
|
172
172
|
* What the fan-out actually cost, for anyone measuring it.
|
|
173
173
|
*
|
|
174
174
|
* `evaluations` is the number the whole design turns on: how many times a
|
|
175
|
-
* live query had to be run against the database.
|
|
175
|
+
* live query had to be run against the database.
|
|
176
|
+
* `snapshots` is how many read transactions the change stream opened —
|
|
177
|
+
* the connections it took from the pool — and `transactions` how many
|
|
178
|
+
* application transactions they covered. Without it a regression
|
|
176
179
|
* here looks like nothing at all until the process is under load.
|
|
177
180
|
*/
|
|
178
181
|
readonly stats: {
|
|
179
182
|
batches: number;
|
|
183
|
+
transactions: number;
|
|
184
|
+
snapshots: number;
|
|
180
185
|
candidates: number;
|
|
181
186
|
evaluations: number;
|
|
182
187
|
fanOutMs: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-engine.d.ts","sourceRoot":"","sources":["../../src/server/sync-engine.ts"],"names":[],"mappings":"AAIA,OAAO,EAEH,KAAK,aAAa,EAElB,KAAK,aAAa,EAIrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAGH,KAAK,WAAW,EAEhB,KAAK,UAAU,EAElB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,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OAQG;IACH,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,oBAAoB;IAO5B;;;OAGG;YACW,cAAc;YAwCd,wBAAwB;YA6BxB,QAAQ;IAItB,gFAAgF;IAChF,OAAO,CAAC,QAAQ;IAmChB,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,UAAU;IAwBlB,6CAA6C;IAC7C,OAAO,CAAC,UAAU;IAelB,gFAAgF;IAChF,OAAO,CAAC,eAAe;IAQvB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAcnB,sEAAsE;IACtE,MAAM,CACF,OAAO,EAAE,aAAa,EACtB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,OAAO,GACd,OAAO,CAAC,eAAe,CAAC;YAQb,eAAe;IAgI7B;;;;;;;OAOG;YACW,SAAS;YAMT,SAAS;YAgBT,kBAAkB;YAQlB,mBAAmB;CAiBpC"}
|
|
@@ -85,10 +85,13 @@ class SyncEngine {
|
|
|
85
85
|
* What the fan-out actually cost, for anyone measuring it.
|
|
86
86
|
*
|
|
87
87
|
* `evaluations` is the number the whole design turns on: how many times a
|
|
88
|
-
* live query had to be run against the database.
|
|
88
|
+
* live query had to be run against the database.
|
|
89
|
+
* `snapshots` is how many read transactions the change stream opened —
|
|
90
|
+
* the connections it took from the pool — and `transactions` how many
|
|
91
|
+
* application transactions they covered. Without it a regression
|
|
89
92
|
* here looks like nothing at all until the process is under load.
|
|
90
93
|
*/
|
|
91
|
-
this.stats = { batches: 0, candidates: 0, evaluations: 0, fanOutMs: 0 };
|
|
94
|
+
this.stats = { batches: 0, transactions: 0, snapshots: 0, candidates: 0, evaluations: 0, fanOutMs: 0 };
|
|
92
95
|
this.opts = opts;
|
|
93
96
|
this.permissionTables = new Set(opts.permissionTables ?? []);
|
|
94
97
|
this.narrow = opts.narrowInvalidation ?? true;
|
|
@@ -368,6 +371,7 @@ class SyncEngine {
|
|
|
368
371
|
return;
|
|
369
372
|
const candidates = this.candidatesFor(batch, invalidatedScopes, revalidateEverything);
|
|
370
373
|
this.stats.batches += 1;
|
|
374
|
+
this.stats.transactions += batch.transactions ?? 1;
|
|
371
375
|
this.stats.candidates += candidates.size;
|
|
372
376
|
const bySession = new Map();
|
|
373
377
|
for (const sub of candidates) {
|
|
@@ -392,6 +396,7 @@ class SyncEngine {
|
|
|
392
396
|
if (interested.size === 0)
|
|
393
397
|
return;
|
|
394
398
|
const outgoing = [];
|
|
399
|
+
this.stats.snapshots += 1;
|
|
395
400
|
await this.opts.snapshots.withSnapshot(async (snap) => {
|
|
396
401
|
const epochCache = new Map();
|
|
397
402
|
const mutationIds = await this.readLastMutationIds(snap, [...interested].map((s) => s.info.clientId));
|