@syncular/server 0.77.4 → 0.78.1
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/dist/d1-storage.js +36 -27
- package/dist/handler.js +39 -20
- package/dist/postgres-storage.js +89 -77
- package/dist/push.d.ts +29 -31
- package/dist/push.js +353 -343
- package/dist/sqlite-dialect.d.ts +7 -7
- package/dist/sqlite-dialect.js +41 -25
- package/dist/sqlite-storage.js +17 -20
- package/dist/storage.d.ts +53 -31
- package/package.json +2 -2
- package/src/d1-storage.ts +48 -34
- package/src/handler.ts +53 -31
- package/src/postgres-storage.ts +106 -103
- package/src/push.ts +452 -475
- package/src/sqlite-dialect.ts +57 -34
- package/src/sqlite-storage.ts +29 -30
- package/src/storage.ts +54 -32
package/dist/sqlite-dialect.d.ts
CHANGED
|
@@ -204,14 +204,14 @@ export declare abstract class SqliteFamilyTransaction {
|
|
|
204
204
|
appendCommit(commit: NewCommit): Promise<number>;
|
|
205
205
|
putPushResult(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
|
|
206
206
|
/**
|
|
207
|
-
* The §
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
207
|
+
* The §2.3 records of a request's commits and, with `outbox`, the §9
|
|
208
|
+
* settlement and §6.3.2 steps 1 and 2. The read runs before the writes, so
|
|
209
|
+
* D1, which buffers the writes, answers exactly as SQLite: a result the
|
|
210
|
+
* writes delete counts as absent.
|
|
211
211
|
*/
|
|
212
|
-
protected
|
|
213
|
-
readonly watermark
|
|
214
|
-
readonly persisted: StoredPushResult | undefined;
|
|
212
|
+
protected admitPushRequest(clientId: string, clientCommitIds: readonly string[], outbox: OutboxSettlement | undefined): Promise<{
|
|
213
|
+
readonly watermark?: number;
|
|
214
|
+
readonly persisted: (clientCommitId: string) => StoredPushResult | undefined;
|
|
215
215
|
}>;
|
|
216
216
|
enqueueReactions(reactions: readonly NewReaction[]): Promise<void>;
|
|
217
217
|
}
|
package/dist/sqlite-dialect.js
CHANGED
|
@@ -1328,27 +1328,40 @@ export class SqliteFamilyTransaction {
|
|
|
1328
1328
|
]);
|
|
1329
1329
|
}
|
|
1330
1330
|
/**
|
|
1331
|
-
* The §
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
*
|
|
1331
|
+
* The §2.3 records of a request's commits and, with `outbox`, the §9
|
|
1332
|
+
* settlement and §6.3.2 steps 1 and 2. The read runs before the writes, so
|
|
1333
|
+
* D1, which buffers the writes, answers exactly as SQLite: a result the
|
|
1334
|
+
* writes delete counts as absent.
|
|
1335
1335
|
*/
|
|
1336
|
-
async
|
|
1336
|
+
async admitPushRequest(clientId, clientCommitIds, outbox) {
|
|
1337
|
+
const p = this.partition;
|
|
1338
|
+
const ids = JSON.stringify(clientCommitIds);
|
|
1339
|
+
const persistedOf = (records) => (clientCommitId) => {
|
|
1340
|
+
const record = records.get(clientCommitId);
|
|
1341
|
+
return record === undefined ? undefined : deserializePushResult(record);
|
|
1342
|
+
};
|
|
1343
|
+
if (outbox === undefined) {
|
|
1344
|
+
const rows = await this.all(`SELECT client_commit_id, result FROM sync_push_results
|
|
1345
|
+
WHERE partition=? AND client_id=? AND client_commit_id IN (SELECT value FROM json_each(?))`, [p, clientId, ids]);
|
|
1346
|
+
return {
|
|
1347
|
+
persisted: persistedOf(new Map(rows.map((row) => [row.client_commit_id, row.result]))),
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1337
1350
|
const first = outbox.commits[0];
|
|
1338
1351
|
const last = outbox.commits.at(-1);
|
|
1339
|
-
if (first === undefined ||
|
|
1340
|
-
|
|
1341
|
-
first.clientCommitId !== clientCommitId) {
|
|
1342
|
-
throw new Error("outbox settlement rides the request's first commit");
|
|
1352
|
+
if (first === undefined || last === undefined) {
|
|
1353
|
+
throw new Error('outbox settlement needs the request commits');
|
|
1343
1354
|
}
|
|
1344
|
-
const
|
|
1345
|
-
const [state] = await this.all(`SELECT (SELECT through_seq FROM sync_push_watermarks
|
|
1355
|
+
const rows = await this.all(`SELECT (SELECT through_seq FROM sync_push_watermarks
|
|
1346
1356
|
WHERE partition=?1 AND client_id=?2 AND outbox_id=?3) AS stored,
|
|
1347
1357
|
EXISTS (SELECT 1 FROM sync_push_results WHERE partition=?1
|
|
1348
1358
|
AND client_id=?2 AND client_commit_id=?5 AND outbox_id IS NULL) AS last_sequenceless,
|
|
1349
|
-
r.
|
|
1359
|
+
r.client_commit_id AS client_commit_id, r.result AS result,
|
|
1360
|
+
r.outbox_id AS outbox_id, r.client_seq AS client_seq
|
|
1350
1361
|
FROM (SELECT 1) LEFT JOIN sync_push_results AS r
|
|
1351
|
-
ON r.partition=?1 AND r.client_id=?2
|
|
1362
|
+
ON r.partition=?1 AND r.client_id=?2
|
|
1363
|
+
AND r.client_commit_id IN (SELECT value FROM json_each(?4))`, [p, clientId, outbox.outboxId, ids, last.clientCommitId]);
|
|
1364
|
+
const state = rows[0];
|
|
1352
1365
|
const watermark = Math.max(state?.stored ?? 0, first.clientSeq);
|
|
1353
1366
|
const commits = JSON.stringify(outbox.commits.map((commit) => [commit.clientCommitId, commit.clientSeq]));
|
|
1354
1367
|
// §9 (b), then (a): with no pre-upgrade result on the request's last
|
|
@@ -1367,18 +1380,21 @@ export class SqliteFamilyTransaction {
|
|
|
1367
1380
|
this.run(`INSERT INTO sync_push_watermarks(partition, client_id, outbox_id, through_seq) VALUES (?,?,?,?)
|
|
1368
1381
|
ON CONFLICT(partition, client_id, outbox_id) DO UPDATE SET through_seq=max(through_seq, excluded.through_seq)`, [p, clientId, outbox.outboxId, watermark]);
|
|
1369
1382
|
this.run('DELETE FROM sync_push_results WHERE partition=? AND client_id=? AND outbox_id=? AND client_seq<?', [p, clientId, outbox.outboxId, watermark]);
|
|
1370
|
-
// The sequence step 2 sees: (a) gives a pre-upgrade result
|
|
1371
|
-
//
|
|
1372
|
-
const
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1383
|
+
// The sequence step 2 sees: (a) gives a pre-upgrade result its commit's
|
|
1384
|
+
// sequence in the request; a result of this outbox keeps its stored one.
|
|
1385
|
+
const sequences = new Map(outbox.commits.map((commit) => [commit.clientCommitId, commit.clientSeq]));
|
|
1386
|
+
const records = new Map();
|
|
1387
|
+
for (const row of rows) {
|
|
1388
|
+
if (row.client_commit_id === null || row.result === null)
|
|
1389
|
+
continue;
|
|
1390
|
+
const deleted = row.outbox_id === null
|
|
1391
|
+
? (sequences.get(row.client_commit_id) ?? 0) < watermark
|
|
1392
|
+
: row.outbox_id === outbox.outboxId &&
|
|
1393
|
+
(row.client_seq ?? 0) < watermark;
|
|
1394
|
+
if (!deleted)
|
|
1395
|
+
records.set(row.client_commit_id, row.result);
|
|
1396
|
+
}
|
|
1397
|
+
return { watermark, persisted: persistedOf(records) };
|
|
1382
1398
|
}
|
|
1383
1399
|
async enqueueReactions(reactions) {
|
|
1384
1400
|
this.assertOpen();
|
package/dist/sqlite-storage.js
CHANGED
|
@@ -68,25 +68,17 @@ class SqliteTransaction extends SqliteFamilyTransaction {
|
|
|
68
68
|
// BEGIN IMMEDIATE and observes every row staged so far.
|
|
69
69
|
return this.#storage.runAuthoritativeQuery(this.partition, query);
|
|
70
70
|
}
|
|
71
|
-
async preparePushApply(clientId,
|
|
71
|
+
async preparePushApply(clientId, commits, runningSchemaVersion, outbox) {
|
|
72
72
|
this.assertOpen();
|
|
73
73
|
// BEGIN IMMEDIATE in the constructor already owns SQLite's writer lock.
|
|
74
|
-
// The settlement precedes the savepoint, so
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
: await this.settleOutbox(clientId, clientCommitId, outbox);
|
|
74
|
+
// The settlement precedes the first commit's savepoint, so no rejection
|
|
75
|
+
// discards it.
|
|
76
|
+
const admitted = await this.admitPushRequest(clientId, commits.map((commit) => commit.clientCommitId), outbox);
|
|
78
77
|
this.#storage.db.exec('SAVEPOINT syncular_push_candidate');
|
|
79
78
|
this.#pushApplySavepoint = true;
|
|
80
79
|
const gate = await this.#storage.readServeGate(this.partition, runningSchemaVersion);
|
|
81
80
|
const [floor] = await this.all('SELECT delete_floor FROM sync_partitions WHERE partition=?', [this.partition]);
|
|
82
|
-
|
|
83
|
-
return settled === undefined
|
|
84
|
-
? {
|
|
85
|
-
gate,
|
|
86
|
-
deleteFloor,
|
|
87
|
-
persisted: await this.#storage.getPushResult(this.partition, clientId, clientCommitId),
|
|
88
|
-
}
|
|
89
|
-
: { gate, deleteFloor, ...settled };
|
|
81
|
+
return { gate, deleteFloor: floor?.delete_floor ?? 0, ...admitted };
|
|
90
82
|
}
|
|
91
83
|
async lockPartition() {
|
|
92
84
|
this.assertOpen();
|
|
@@ -107,19 +99,24 @@ class SqliteTransaction extends SqliteFamilyTransaction {
|
|
|
107
99
|
// BEGIN IMMEDIATE, so the gate and the write share one transaction.
|
|
108
100
|
return this.#storage.readServeGate(this.partition, runningSchemaVersion);
|
|
109
101
|
}
|
|
110
|
-
async
|
|
102
|
+
async rejectPushCommit(clientId, clientCommitId, result, sequence) {
|
|
111
103
|
this.assertOpen();
|
|
112
104
|
if (!this.#pushApplySavepoint) {
|
|
113
105
|
throw new Error('push rejection requires its apply savepoint');
|
|
114
106
|
}
|
|
115
107
|
this.#storage.db.exec('ROLLBACK TO SAVEPOINT syncular_push_candidate');
|
|
116
|
-
this.#storage.db.exec('RELEASE SAVEPOINT syncular_push_candidate');
|
|
117
|
-
this.#pushApplySavepoint = false;
|
|
118
108
|
await this.putPushResult(clientId, clientCommitId, result, sequence);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Closes the request's current commit: its writes and result join the
|
|
112
|
+
* transaction, and the next commit writes behind a new savepoint (§6.4).
|
|
113
|
+
*/
|
|
114
|
+
async putPushResult(clientId, clientCommitId, result, sequence) {
|
|
115
|
+
await super.putPushResult(clientId, clientCommitId, result, sequence);
|
|
116
|
+
if (this.#pushApplySavepoint) {
|
|
117
|
+
this.#storage.db.exec('RELEASE SAVEPOINT syncular_push_candidate');
|
|
118
|
+
this.#storage.db.exec('SAVEPOINT syncular_push_candidate');
|
|
119
|
+
}
|
|
123
120
|
}
|
|
124
121
|
async upsertRow(table, row, context) {
|
|
125
122
|
this.assertOpen();
|
package/dist/storage.d.ts
CHANGED
|
@@ -69,15 +69,19 @@ export interface ServeGate {
|
|
|
69
69
|
}[];
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
73
|
-
* the idempotency
|
|
74
|
-
* network cost
|
|
72
|
+
* One push request's admission token (§6.4): the partition lock, the serve
|
|
73
|
+
* gate, and the idempotency records of every commit of the request. A storage
|
|
74
|
+
* with per-statement network cost reads them in one statement (see
|
|
75
75
|
* `PostgresServerStorage`); the lock is held from this call through commit.
|
|
76
76
|
*/
|
|
77
77
|
export interface PreparedPushApply {
|
|
78
78
|
readonly gate: ServeGate;
|
|
79
|
-
/**
|
|
80
|
-
|
|
79
|
+
/**
|
|
80
|
+
* The already-persisted outcome for one of the request's keys, or
|
|
81
|
+
* `undefined` on a miss. Throws `SyncError sync.idempotency_cache_miss`
|
|
82
|
+
* when the stored record is unreadable (§6.3).
|
|
83
|
+
*/
|
|
84
|
+
readonly persisted: (clientCommitId: string) => StoredPushResult | undefined;
|
|
81
85
|
/**
|
|
82
86
|
* The partition's delete floor (§2.2): the highest version any applied
|
|
83
87
|
* delete recorded, `0` before the first. An insert starts one above it.
|
|
@@ -91,6 +95,15 @@ export interface PreparedPushApply {
|
|
|
91
95
|
*/
|
|
92
96
|
readonly watermark?: number;
|
|
93
97
|
}
|
|
98
|
+
/** One commit of a push request, as `preparePushApply` admits it. */
|
|
99
|
+
export interface PushApplyCommit {
|
|
100
|
+
readonly clientCommitId: string;
|
|
101
|
+
/** The rows the commit is known to touch before its operations are built. */
|
|
102
|
+
readonly targets?: readonly {
|
|
103
|
+
readonly table: string;
|
|
104
|
+
readonly rowIds: readonly string[];
|
|
105
|
+
}[];
|
|
106
|
+
}
|
|
94
107
|
/** The outbox position of one pushed commit (§6.3.2). */
|
|
95
108
|
export interface OutboxSequence {
|
|
96
109
|
readonly outboxId: string;
|
|
@@ -541,6 +554,13 @@ export interface AuthoritativeQueryResult {
|
|
|
541
554
|
* land together or not at all.
|
|
542
555
|
*/
|
|
543
556
|
export interface StorageTransaction {
|
|
557
|
+
/**
|
|
558
|
+
* True when `putPushResult` and `rejectPushCommit` persist the closed
|
|
559
|
+
* commit at once (D1, one atomic batch per commit), so a later failure of
|
|
560
|
+
* the request leaves it applied and the push layer still notifies it
|
|
561
|
+
* (§6.4). Absent means the request's commits persist only at `commit()`.
|
|
562
|
+
*/
|
|
563
|
+
readonly persistsCommitsOnClose?: boolean;
|
|
544
564
|
getRow(table: string, rowId: string): Promise<StoredRow | undefined>;
|
|
545
565
|
/**
|
|
546
566
|
* Optional candidate-state batch read by primary key. A storage with
|
|
@@ -611,35 +631,31 @@ export interface StorageTransaction {
|
|
|
611
631
|
lockPartition?(): Promise<void>;
|
|
612
632
|
/**
|
|
613
633
|
* Serialize every push apply for this partition before any operation read,
|
|
614
|
-
* validation, merge, or write, and read the admission token
|
|
615
|
-
*
|
|
616
|
-
* `
|
|
617
|
-
* so the idempotency read is exact and a racing
|
|
618
|
-
* `targets` are the rows
|
|
619
|
-
* are built; a storage with per-statement
|
|
620
|
-
* into the admission read and answers
|
|
621
|
-
* Missing support fails closed
|
|
634
|
+
* validation, merge, or write, and read the admission token once for the
|
|
635
|
+
* whole request (§6.4): the RFC 0007 serve gate and the §2.3 idempotency
|
|
636
|
+
* record of every commit in `commits`. The lock is retained until the
|
|
637
|
+
* transaction finishes, so the idempotency read is exact and a racing
|
|
638
|
+
* duplicate cannot rerun. A commit's `targets` are the rows it is known to
|
|
639
|
+
* touch before its operations are built; a storage with per-statement
|
|
640
|
+
* network cost folds that read-ahead into the admission read and answers
|
|
641
|
+
* the following `getRow` calls from it. Missing support fails closed
|
|
642
|
+
* before an app-row mutation.
|
|
622
643
|
*
|
|
623
|
-
* `outbox`, passed
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
*
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
*/
|
|
630
|
-
preparePushApply(clientId: string, clientCommitId: string, runningSchemaVersion: number, targets?: readonly {
|
|
631
|
-
readonly table: string;
|
|
632
|
-
readonly rowIds: readonly string[];
|
|
633
|
-
}[], outbox?: OutboxSettlement): Promise<PreparedPushApply>;
|
|
644
|
+
* `outbox`, passed when the request carries an `outboxId`, settles the
|
|
645
|
+
* client's results stored before wire version 4 (§9) and runs §6.3.2 steps
|
|
646
|
+
* 1 and 2 under the same lock, before the read of `persisted`. Those
|
|
647
|
+
* writes precede every commit's savepoint, so no rejection discards them.
|
|
648
|
+
*/
|
|
649
|
+
preparePushApply(clientId: string, commits: readonly PushApplyCommit[], runningSchemaVersion: number, outbox?: OutboxSettlement): Promise<PreparedPushApply>;
|
|
634
650
|
/**
|
|
635
|
-
*
|
|
636
|
-
* held: discard every candidate write
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
651
|
+
* Reject the request's current commit while the push-apply serialization
|
|
652
|
+
* lock is still held: discard every candidate write the commit staged since
|
|
653
|
+
* the previous commit closed (its savepoint), keep the request's earlier
|
|
654
|
+
* commits, and record the rejected idempotency result. Like
|
|
655
|
+
* `putPushResult`, it closes the commit; the transaction stays open for the
|
|
656
|
+
* request's next commit.
|
|
641
657
|
*/
|
|
642
|
-
|
|
658
|
+
rejectPushCommit(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
|
|
643
659
|
upsertRow(table: string, row: StoredRow, context?: {
|
|
644
660
|
readonly opIndex: number;
|
|
645
661
|
}): Promise<void>;
|
|
@@ -657,6 +673,12 @@ export interface StorageTransaction {
|
|
|
657
673
|
* first writer wins; callers read the canonical value after commit. A
|
|
658
674
|
* result without `sequence` (no `outboxId`, §6.10, §12.3) is never deleted
|
|
659
675
|
* by a watermark.
|
|
676
|
+
*
|
|
677
|
+
* The call closes the request's current push commit: the next write opens
|
|
678
|
+
* the next commit's savepoint. PostgreSQL writes the request's results as
|
|
679
|
+
* one statement at `commit()`. D1, whose transaction cannot read its own
|
|
680
|
+
* staged writes across commits, flushes the commit's writes and this
|
|
681
|
+
* result as one atomic batch here (§6.4).
|
|
660
682
|
*/
|
|
661
683
|
putPushResult(clientId: string, clientCommitId: string, result: StoredPushResult, sequence?: OutboxSequence): Promise<void>;
|
|
662
684
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.78.1",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.78.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/d1-storage.ts
CHANGED
|
@@ -21,9 +21,11 @@
|
|
|
21
21
|
*
|
|
22
22
|
* - reads (`getRow`) execute immediately against D1 (autocommit);
|
|
23
23
|
* - writes are **buffered** as prepared statements;
|
|
24
|
-
* - `
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* - `putPushResult` closes a push commit and flushes the buffer as one
|
|
25
|
+
* `db.batch(...)` (§6.4: one atomic batch per commit, because a later
|
|
26
|
+
* commit of the request must read the earlier one's writes from D1);
|
|
27
|
+
* `commit()` flushes whatever remains. A rejected commit rolls back by
|
|
28
|
+
* never flushing its part of the buffer;
|
|
27
29
|
* - `rollback()` drops the buffer unsent.
|
|
28
30
|
*
|
|
29
31
|
* `appendCommit` allocates the dense per-partition `commitSeq` (§2.1) by
|
|
@@ -103,6 +105,7 @@ import type {
|
|
|
103
105
|
OutboxSequence,
|
|
104
106
|
OutboxSettlement,
|
|
105
107
|
PreparedPushApply,
|
|
108
|
+
PushApplyCommit,
|
|
106
109
|
RowScanQuery,
|
|
107
110
|
ScopeValueHistoryQuery,
|
|
108
111
|
ServerStorage,
|
|
@@ -208,6 +211,8 @@ class D1Transaction
|
|
|
208
211
|
) => BoundAuthoritativeQuery;
|
|
209
212
|
readonly #pushApplySerialized: boolean;
|
|
210
213
|
readonly #scanVariables: Map<string, string>;
|
|
214
|
+
/** Each push commit reaches D1 as its own batch when it closes (§6.4). */
|
|
215
|
+
readonly persistsCommitsOnClose = true;
|
|
211
216
|
readonly #buffer: SqliteStatementSpec[] = [];
|
|
212
217
|
/** Live snapshot of `max_commit_seq`, advanced within this transaction. */
|
|
213
218
|
#maxCommitSeq: number | undefined;
|
|
@@ -545,12 +550,8 @@ class D1Transaction
|
|
|
545
550
|
|
|
546
551
|
async preparePushApply(
|
|
547
552
|
clientId: string,
|
|
548
|
-
|
|
553
|
+
commits: readonly PushApplyCommit[],
|
|
549
554
|
runningSchemaVersion: number,
|
|
550
|
-
_targets: readonly {
|
|
551
|
-
readonly table: string;
|
|
552
|
-
readonly rowIds: readonly string[];
|
|
553
|
-
}[] = [],
|
|
554
555
|
outbox?: OutboxSettlement,
|
|
555
556
|
): Promise<PreparedPushApply> {
|
|
556
557
|
this.assertOpen();
|
|
@@ -561,12 +562,13 @@ class D1Transaction
|
|
|
561
562
|
}
|
|
562
563
|
// D1 has no interactive lock. The caller explicitly asserted that every
|
|
563
564
|
// write for this partition is already serialized (normally by its DO).
|
|
564
|
-
// The settlement's writes sit below the checkpoint, so a
|
|
565
|
-
// them.
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
565
|
+
// The settlement's writes sit below the first commit's checkpoint, so a
|
|
566
|
+
// rejection keeps them.
|
|
567
|
+
const admitted = await this.admitPushRequest(
|
|
568
|
+
clientId,
|
|
569
|
+
commits.map((commit) => commit.clientCommitId),
|
|
570
|
+
outbox,
|
|
571
|
+
);
|
|
570
572
|
this.#pushApplyCheckpoint = this.#buffer.length;
|
|
571
573
|
// The serve gate and the delete floor (§2.2) in one round trip.
|
|
572
574
|
const read = await this.#db
|
|
@@ -580,14 +582,7 @@ class D1Transaction
|
|
|
580
582
|
logEpoch: undefined,
|
|
581
583
|
pending: [],
|
|
582
584
|
};
|
|
583
|
-
|
|
584
|
-
return settled === undefined
|
|
585
|
-
? {
|
|
586
|
-
gate,
|
|
587
|
-
deleteFloor,
|
|
588
|
-
persisted: await this.getPushResult(clientId, clientCommitId),
|
|
589
|
-
}
|
|
590
|
-
: { gate, deleteFloor, ...settled };
|
|
585
|
+
return { gate, deleteFloor: read?.delete_floor ?? 0, ...admitted };
|
|
591
586
|
}
|
|
592
587
|
|
|
593
588
|
advanceCheckpoint(
|
|
@@ -615,12 +610,12 @@ class D1Transaction
|
|
|
615
610
|
};
|
|
616
611
|
}
|
|
617
612
|
|
|
618
|
-
async
|
|
613
|
+
async rejectPushCommit(
|
|
619
614
|
clientId: string,
|
|
620
615
|
clientCommitId: string,
|
|
621
616
|
result: StoredPushResult,
|
|
622
617
|
sequence?: OutboxSequence,
|
|
623
|
-
): Promise<
|
|
618
|
+
): Promise<void> {
|
|
624
619
|
this.assertOpen();
|
|
625
620
|
const checkpoint = this.#pushApplyCheckpoint;
|
|
626
621
|
if (checkpoint === undefined) {
|
|
@@ -632,10 +627,27 @@ class D1Transaction
|
|
|
632
627
|
this.#maxCommitSeq = undefined;
|
|
633
628
|
this.#applicationOpIndexes.clear();
|
|
634
629
|
await this.putPushResult(clientId, clientCommitId, result, sequence);
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Closes the request's current commit (§6.4): its buffered writes and this
|
|
634
|
+
* result reach D1 as one atomic batch. A later commit of the request then
|
|
635
|
+
* reads its rows, tombstones and registered queries from D1 itself, which
|
|
636
|
+
* a buffer spanning commits could not answer.
|
|
637
|
+
*/
|
|
638
|
+
override async putPushResult(
|
|
639
|
+
clientId: string,
|
|
640
|
+
clientCommitId: string,
|
|
641
|
+
result: StoredPushResult,
|
|
642
|
+
sequence?: OutboxSequence,
|
|
643
|
+
): Promise<void> {
|
|
644
|
+
await super.putPushResult(clientId, clientCommitId, result, sequence);
|
|
645
|
+
await this.#flush();
|
|
646
|
+
this.#pending.clear();
|
|
647
|
+
this.#clearedTombstones.clear();
|
|
648
|
+
this.#prefetched.clear();
|
|
649
|
+
this.#applicationOpIndexes.clear();
|
|
650
|
+
if (this.#pushApplyCheckpoint !== undefined) this.#pushApplyCheckpoint = 0;
|
|
639
651
|
}
|
|
640
652
|
|
|
641
653
|
async #assertNoUniqueCollision(
|
|
@@ -776,17 +788,19 @@ class D1Transaction
|
|
|
776
788
|
|
|
777
789
|
async commit(): Promise<void> {
|
|
778
790
|
this.assertOpen();
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
791
|
+
await this.#flush();
|
|
792
|
+
this.open = false;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/** Send the buffer as one atomic D1 batch: the §6.4 all-or-nothing write. */
|
|
796
|
+
async #flush(): Promise<void> {
|
|
797
|
+
if (this.#buffer.length === 0) return;
|
|
783
798
|
const statements = this.#buffer.map((entry) =>
|
|
784
799
|
this.#db.prepare(entry.sql).bind(...entry.params),
|
|
785
800
|
);
|
|
786
|
-
// One atomic D1 batch — the §6.4 all-or-nothing commit.
|
|
787
801
|
try {
|
|
788
802
|
await this.#db.batch(statements);
|
|
789
|
-
this.
|
|
803
|
+
this.#buffer.length = 0;
|
|
790
804
|
} catch (error) {
|
|
791
805
|
if (isD1ConstraintError(error)) {
|
|
792
806
|
// D1 batches are atomic. Keep this logical transaction open so the
|
package/src/handler.ts
CHANGED
|
@@ -61,7 +61,7 @@ import {
|
|
|
61
61
|
planUnit,
|
|
62
62
|
type UnitPlan,
|
|
63
63
|
} from './pull';
|
|
64
|
-
import { type ProcessedPushCommit,
|
|
64
|
+
import { type ProcessedPushCommit, processPushCommitsWithTrace } from './push';
|
|
65
65
|
import { serveNotReadyError } from './readiness';
|
|
66
66
|
import type { CompiledSchema, CompiledTable } from './schema';
|
|
67
67
|
import { compileSchema } from './schema';
|
|
@@ -621,39 +621,61 @@ async function* streamResponse(
|
|
|
621
621
|
}
|
|
622
622
|
const { clientId, outboxId } = plan.header;
|
|
623
623
|
try {
|
|
624
|
-
// Push half (§6): one PUSH_RESULT per PUSH_COMMIT, in request order
|
|
624
|
+
// Push half (§6): one PUSH_RESULT per PUSH_COMMIT, in request order,
|
|
625
|
+
// sent after the request's one transaction commits (§6.4).
|
|
625
626
|
const currentSchema = compileSchema(ctx.schema);
|
|
626
627
|
let acceptedThrough = 0;
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
628
|
+
const pushed =
|
|
629
|
+
plan.pushes.length === 0
|
|
630
|
+
? []
|
|
631
|
+
: await processPushCommitsWithTrace(
|
|
632
|
+
ctx,
|
|
633
|
+
currentSchema,
|
|
634
|
+
plan.resolved,
|
|
635
|
+
clientId,
|
|
636
|
+
plan.pushes.map((push) => {
|
|
637
|
+
// The frame names its rows before the transaction opens, so the
|
|
638
|
+
// admission read carries the commit's read-ahead. A rowId
|
|
639
|
+
// holding U+0000 is rejected per operation (§6.1) before any
|
|
640
|
+
// store reads it.
|
|
641
|
+
const targets = new Map<string, string[]>();
|
|
642
|
+
for (const op of push.operations) {
|
|
643
|
+
if (
|
|
644
|
+
!currentSchema.tables.has(op.table) ||
|
|
645
|
+
op.rowId.includes('\u0000')
|
|
646
|
+
) {
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
const ids = targets.get(op.table) ?? [];
|
|
650
|
+
ids.push(op.rowId);
|
|
651
|
+
targets.set(op.table, ids);
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
clientCommitId: push.clientCommitId,
|
|
655
|
+
buildOperations: async () => push.operations,
|
|
656
|
+
...(targets.size === 0
|
|
657
|
+
? {}
|
|
658
|
+
: {
|
|
659
|
+
targets: [...targets].map(([table, rowIds]) => ({
|
|
660
|
+
table,
|
|
661
|
+
rowIds,
|
|
650
662
|
})),
|
|
651
|
-
},
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
663
|
+
}),
|
|
664
|
+
};
|
|
665
|
+
}),
|
|
666
|
+
schema,
|
|
667
|
+
outboxId === undefined
|
|
668
|
+
? undefined
|
|
669
|
+
: {
|
|
670
|
+
outboxId,
|
|
671
|
+
commits: plan.pushes.map((commit) => ({
|
|
672
|
+
clientCommitId: commit.clientCommitId,
|
|
673
|
+
clientSeq: commit.clientSeq,
|
|
674
|
+
})),
|
|
675
|
+
},
|
|
676
|
+
);
|
|
677
|
+
for (const [index, push] of plan.pushes.entries()) {
|
|
678
|
+
const processed = pushed[index]!;
|
|
657
679
|
acceptedThrough = Math.max(
|
|
658
680
|
acceptedThrough,
|
|
659
681
|
processed.frame.commitSeq ?? 0,
|