@optimystic/db-core 1.19.0 → 1.20.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/dist/src/cluster/structs.d.ts +41 -8
- package/dist/src/cluster/structs.d.ts.map +1 -1
- package/dist/src/cluster/structs.js +18 -7
- package/dist/src/cluster/structs.js.map +1 -1
- package/dist/src/collection/action.d.ts +29 -0
- package/dist/src/collection/action.d.ts.map +1 -1
- package/dist/src/collection/action.js +6 -0
- package/dist/src/collection/action.js.map +1 -1
- package/dist/src/collection/collection.d.ts +533 -120
- package/dist/src/collection/collection.d.ts.map +1 -1
- package/dist/src/collection/collection.js +974 -500
- package/dist/src/collection/collection.js.map +1 -1
- package/dist/src/collection/head-commit.d.ts +110 -0
- package/dist/src/collection/head-commit.d.ts.map +1 -0
- package/dist/src/collection/head-commit.js +65 -0
- package/dist/src/collection/head-commit.js.map +1 -0
- package/dist/src/collection/index.d.ts +2 -0
- package/dist/src/collection/index.d.ts.map +1 -1
- package/dist/src/collection/index.js +2 -0
- package/dist/src/collection/index.js.map +1 -1
- package/dist/src/collection/segments.d.ts +77 -0
- package/dist/src/collection/segments.d.ts.map +1 -0
- package/dist/src/collection/segments.js +110 -0
- package/dist/src/collection/segments.js.map +1 -0
- package/dist/src/collection/struct.d.ts +156 -1
- package/dist/src/collection/struct.d.ts.map +1 -1
- package/dist/src/collection/struct.js +70 -0
- package/dist/src/collection/struct.js.map +1 -1
- package/dist/src/collection/write-attempts.d.ts +245 -0
- package/dist/src/collection/write-attempts.d.ts.map +1 -0
- package/dist/src/collection/write-attempts.js +439 -0
- package/dist/src/collection/write-attempts.js.map +1 -0
- package/dist/src/collections/tree/struct.d.ts +100 -17
- package/dist/src/collections/tree/struct.d.ts.map +1 -1
- package/dist/src/collections/tree/struct.js +38 -7
- package/dist/src/collections/tree/struct.js.map +1 -1
- package/dist/src/collections/tree/tree.d.ts +17 -5
- package/dist/src/collections/tree/tree.d.ts.map +1 -1
- package/dist/src/collections/tree/tree.js +112 -26
- package/dist/src/collections/tree/tree.js.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/network/i-repo.d.ts +3 -0
- package/dist/src/network/i-repo.d.ts.map +1 -1
- package/dist/src/network/index.d.ts +3 -0
- package/dist/src/network/index.d.ts.map +1 -1
- package/dist/src/network/index.js +3 -0
- package/dist/src/network/index.js.map +1 -1
- package/dist/src/network/promise-shortfall-error.d.ts +40 -0
- package/dist/src/network/promise-shortfall-error.d.ts.map +1 -0
- package/dist/src/network/promise-shortfall-error.js +67 -0
- package/dist/src/network/promise-shortfall-error.js.map +1 -0
- package/dist/src/network/refuse-solo.d.ts +8 -0
- package/dist/src/network/refuse-solo.d.ts.map +1 -0
- package/dist/src/network/refuse-solo.js +8 -0
- package/dist/src/network/refuse-solo.js.map +1 -0
- package/dist/src/network/stale-failure.d.ts +20 -3
- package/dist/src/network/stale-failure.d.ts.map +1 -1
- package/dist/src/network/stale-failure.js +24 -3
- package/dist/src/network/stale-failure.js.map +1 -1
- package/dist/src/network/struct.d.ts +148 -2
- package/dist/src/network/struct.d.ts.map +1 -1
- package/dist/src/network/struct.js.map +1 -1
- package/dist/src/network/validity-rejected-error.d.ts +28 -0
- package/dist/src/network/validity-rejected-error.d.ts.map +1 -0
- package/dist/src/network/validity-rejected-error.js +51 -0
- package/dist/src/network/validity-rejected-error.js.map +1 -0
- package/dist/src/offline/entry.d.ts +72 -0
- package/dist/src/offline/entry.d.ts.map +1 -0
- package/dist/src/offline/entry.js +73 -0
- package/dist/src/offline/entry.js.map +1 -0
- package/dist/src/offline/index.d.ts +5 -0
- package/dist/src/offline/index.d.ts.map +1 -0
- package/dist/src/offline/index.js +5 -0
- package/dist/src/offline/index.js.map +1 -0
- package/dist/src/offline/journal.d.ts +61 -0
- package/dist/src/offline/journal.d.ts.map +1 -0
- package/dist/src/offline/journal.js +2 -0
- package/dist/src/offline/journal.js.map +1 -0
- package/dist/src/offline/queue.d.ts +484 -0
- package/dist/src/offline/queue.d.ts.map +1 -0
- package/dist/src/offline/queue.js +1236 -0
- package/dist/src/offline/queue.js.map +1 -0
- package/dist/src/offline/reachability.d.ts +25 -0
- package/dist/src/offline/reachability.d.ts.map +1 -0
- package/dist/src/offline/reachability.js +13 -0
- package/dist/src/offline/reachability.js.map +1 -0
- package/dist/src/offline/status.d.ts +148 -0
- package/dist/src/offline/status.d.ts.map +1 -0
- package/dist/src/offline/status.js +63 -0
- package/dist/src/offline/status.js.map +1 -0
- package/dist/src/offline/wake.d.ts +30 -0
- package/dist/src/offline/wake.d.ts.map +1 -0
- package/dist/src/offline/wake.js +68 -0
- package/dist/src/offline/wake.js.map +1 -0
- package/dist/src/testing/test-transactor.d.ts +29 -0
- package/dist/src/testing/test-transactor.d.ts.map +1 -1
- package/dist/src/testing/test-transactor.js +50 -0
- package/dist/src/testing/test-transactor.js.map +1 -1
- package/dist/src/transaction/commit-failure-class.d.ts +103 -0
- package/dist/src/transaction/commit-failure-class.d.ts.map +1 -0
- package/dist/src/transaction/commit-failure-class.js +96 -0
- package/dist/src/transaction/commit-failure-class.js.map +1 -0
- package/dist/src/transaction/coordinator.d.ts +165 -41
- package/dist/src/transaction/coordinator.d.ts.map +1 -1
- package/dist/src/transaction/coordinator.js +605 -300
- package/dist/src/transaction/coordinator.js.map +1 -1
- package/dist/src/transaction/entry-commit.d.ts +139 -0
- package/dist/src/transaction/entry-commit.d.ts.map +1 -0
- package/dist/src/transaction/entry-commit.js +199 -0
- package/dist/src/transaction/entry-commit.js.map +1 -0
- package/dist/src/transaction/index.d.ts +4 -1
- package/dist/src/transaction/index.d.ts.map +1 -1
- package/dist/src/transaction/index.js +1 -0
- package/dist/src/transaction/index.js.map +1 -1
- package/dist/src/transaction/transaction.d.ts +20 -0
- package/dist/src/transaction/transaction.d.ts.map +1 -1
- package/dist/src/transaction/validator.d.ts +7 -0
- package/dist/src/transaction/validator.d.ts.map +1 -1
- package/dist/src/transaction/validator.js +21 -12
- package/dist/src/transaction/validator.js.map +1 -1
- package/dist/src/transactor/network-transactor.d.ts +1 -0
- package/dist/src/transactor/network-transactor.d.ts.map +1 -1
- package/dist/src/transactor/network-transactor.js +61 -28
- package/dist/src/transactor/network-transactor.js.map +1 -1
- package/dist/src/transactor/transactor-source.d.ts +22 -11
- package/dist/src/transactor/transactor-source.d.ts.map +1 -1
- package/dist/src/transactor/transactor-source.js +9 -13
- package/dist/src/transactor/transactor-source.js.map +1 -1
- package/dist/src/utility/visit-every.d.ts +14 -0
- package/dist/src/utility/visit-every.d.ts.map +1 -0
- package/dist/src/utility/visit-every.js +27 -0
- package/dist/src/utility/visit-every.js.map +1 -0
- package/package.json +1 -1
- package/src/cluster/structs.ts +42 -7
- package/src/collection/action.ts +34 -0
- package/src/collection/collection.ts +1048 -543
- package/src/collection/head-commit.ts +148 -0
- package/src/collection/index.ts +2 -0
- package/src/collection/segments.ts +149 -0
- package/src/collection/struct.ts +183 -1
- package/src/collection/write-attempts.ts +506 -0
- package/src/collections/tree/struct.ts +97 -17
- package/src/collections/tree/tree.ts +125 -29
- package/src/index.ts +2 -0
- package/src/network/i-repo.ts +3 -0
- package/src/network/index.ts +3 -0
- package/src/network/promise-shortfall-error.ts +69 -0
- package/src/network/refuse-solo.ts +7 -0
- package/src/network/stale-failure.ts +27 -4
- package/src/network/struct.ts +154 -2
- package/src/network/validity-rejected-error.ts +55 -0
- package/src/offline/entry.ts +127 -0
- package/src/offline/index.ts +4 -0
- package/src/offline/journal.ts +60 -0
- package/src/offline/queue.ts +1342 -0
- package/src/offline/reachability.ts +32 -0
- package/src/offline/status.ts +183 -0
- package/src/offline/wake.ts +67 -0
- package/src/testing/test-transactor.ts +53 -0
- package/src/transaction/commit-failure-class.ts +133 -0
- package/src/transaction/coordinator.ts +696 -349
- package/src/transaction/entry-commit.ts +285 -0
- package/src/transaction/index.ts +4 -0
- package/src/transaction/transaction.ts +21 -0
- package/src/transaction/validator.ts +22 -12
- package/src/transactor/network-transactor.ts +62 -28
- package/src/transactor/transactor-source.ts +29 -13
- package/src/utility/visit-every.ts +25 -0
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { isConflictFailure, isExpiryFailure } from "../network/stale-failure.js";
|
|
1
|
+
import { isConflictFailure, isExpiryFailure, isRejectedFailure, isShortfallFailure } from "../network/stale-failure.js";
|
|
2
2
|
import { withBlockRevs } from "../network/durability.js";
|
|
3
3
|
import { TransactionExpiredError } from "../network/transaction-expired-error.js";
|
|
4
|
+
import { PromiseShortfallError } from "../network/promise-shortfall-error.js";
|
|
5
|
+
import { ValidityRejectedError } from "../network/validity-rejected-error.js";
|
|
6
|
+
import { refuseSoloField } from "../network/refuse-solo.js";
|
|
4
7
|
import { TransactionTooLargeError } from "../network/transaction-size.js";
|
|
5
8
|
import { BlockTooLargeError, oversizedBlock } from "../network/block-size.js";
|
|
6
9
|
import { TornActionError } from "../collection/struct.js";
|
|
7
10
|
import { isTransactionExpired, clampPriority } from "./transaction.js";
|
|
11
|
+
import { EntryParticipants, writeContextOf } from "./entry-commit.js";
|
|
8
12
|
import { Log } from "../log/log.js";
|
|
9
13
|
import { blockIdsForTransforms, copyTransforms } from "../transform/helpers.js";
|
|
10
14
|
import { describeTouchedBlocks, blockDigestsField, baseRevsField } from "../transform/digest.js";
|
|
@@ -12,6 +16,7 @@ import { collectOperations, hashOperations } from "./operations-hash.js";
|
|
|
12
16
|
import { CoordinatorConcurrentStampError, CoordinatorPartialCommitError, CoordinatorStaleLossError } from "./errors.js";
|
|
13
17
|
import { recordCollectionDurability, transactionDurability } from "./durability.js";
|
|
14
18
|
import { jitteredBackoffMs, abortableDelay, makeAbortError } from "../utility/backoff.js";
|
|
19
|
+
import { visitEveryThenThrow } from "../utility/visit-every.js";
|
|
15
20
|
import { createLogger } from "../logger.js";
|
|
16
21
|
const log = createLogger('trx:coordinator');
|
|
17
22
|
/** Default max consecutive clean-stale-loss retries before {@link TransactionCoordinator.commit}
|
|
@@ -41,36 +46,61 @@ class PendRejectedError extends Error {
|
|
|
41
46
|
this.name = 'PendRejectedError';
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
|
-
/** The error a refused pend of `collectionIds` stands for: its
|
|
45
|
-
*
|
|
49
|
+
/** The error a refused pend of `collectionIds` stands for: its validity rejection — never retried, since
|
|
50
|
+
* a re-drive sends the same content to the same checks — its expiry — never retried, since a re-drive's
|
|
51
|
+
* expiration comes from the same clock — or its promise shortfall — never retried, since a re-drive
|
|
52
|
+
* meets the same silent members — or a rejection carrying whether a re-read can clear it.
|
|
46
53
|
* Retryability comes from the response itself: a producer that classified the failure sets `conflict`,
|
|
47
54
|
* and only where none did is it inferred from `missing`/`pending` (`isConflictFailure`). */
|
|
48
55
|
function pendRefusalError(collectionIds, refusal) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
if (isRejectedFailure(refusal))
|
|
57
|
+
return new ValidityRejectedError(refusal.rejected);
|
|
58
|
+
if (isExpiryFailure(refusal))
|
|
59
|
+
return new TransactionExpiredError(refusal.expired);
|
|
60
|
+
if (isShortfallFailure(refusal))
|
|
61
|
+
return new PromiseShortfallError(refusal.shortfall);
|
|
62
|
+
return new PendRejectedError(collectionIds, isConflictFailure(refusal), refusal.reason, refusal.staleAt);
|
|
63
|
+
}
|
|
64
|
+
/** A head commit's staged attempt as the phases send it: its transforms are the attempt tracker's. */
|
|
65
|
+
function collectionAttemptOf(staged) {
|
|
66
|
+
return {
|
|
67
|
+
transforms: staged.tracker.transforms,
|
|
68
|
+
tailId: staged.tailId,
|
|
69
|
+
rev: staged.rev,
|
|
70
|
+
baseRevs: staged.baseRevs,
|
|
71
|
+
blockDigests: staged.blockDigests,
|
|
72
|
+
};
|
|
52
73
|
}
|
|
53
74
|
/**
|
|
54
75
|
* The pend phase's failure for `reasons` — what every failed pend threw or stood for, at least one.
|
|
55
76
|
* The error text is the first reason's. The attempt is a retryable clean stale loss only if at least one
|
|
56
77
|
* reason was a conflicting pend ({@link PendRejectedError}`.conflict`) AND none was a hard failure: a
|
|
57
78
|
* single hard failure (storage/policy rejection, or a thrown/unavailable transactor) will not clear on a
|
|
58
|
-
* re-read, so re-driving it would just burn the retry budget.
|
|
59
|
-
* to send, are hard failures carried whole: each is the error
|
|
79
|
+
* re-read, so re-driving it would just burn the retry budget. A validity rejection, an expiry, a promise
|
|
80
|
+
* shortfall, and a pend or a block too large to send, are hard failures carried whole: each is the error
|
|
81
|
+
* the commit surfaces.
|
|
60
82
|
*/
|
|
61
83
|
function pendFailure(reasons) {
|
|
62
84
|
const first = reasons[0];
|
|
63
85
|
const conflicts = reasons.filter(reason => reason instanceof PendRejectedError && reason.conflict);
|
|
86
|
+
const rejected = reasons.find((reason) => reason instanceof ValidityRejectedError);
|
|
64
87
|
const expired = reasons.find((reason) => reason instanceof TransactionExpiredError);
|
|
88
|
+
const shortfall = reasons.find((reason) => reason instanceof PromiseShortfallError);
|
|
65
89
|
const tooLarge = reasons.find((reason) => reason instanceof TransactionTooLargeError || reason instanceof BlockTooLargeError);
|
|
66
90
|
return {
|
|
67
91
|
success: false,
|
|
68
92
|
error: first instanceof Error ? first.message : String(first),
|
|
69
93
|
staleLoss: conflicts.length > 0 && conflicts.length === reasons.length,
|
|
94
|
+
...(rejected === undefined ? {} : { rejected }),
|
|
70
95
|
...(expired === undefined ? {} : { expired }),
|
|
96
|
+
...(shortfall === undefined ? {} : { shortfall }),
|
|
71
97
|
...(tooLarge === undefined ? {} : { tooLarge }),
|
|
72
98
|
};
|
|
73
99
|
}
|
|
100
|
+
/** What a commit's `options` ask of every send of its cycle. */
|
|
101
|
+
function inFlightCycleOf(options) {
|
|
102
|
+
return options?.refuseSoloCommit === true ? { refuseSolo: true } : {};
|
|
103
|
+
}
|
|
74
104
|
/**
|
|
75
105
|
* How `collectionId`'s commit ended when `result` answered its `request` — the answer to its own
|
|
76
106
|
* commit, or, in a batched commit, the batch's one answer for every collection of it. A success is
|
|
@@ -112,6 +142,102 @@ function collectionCommitOutcome(collectionId, request, result) {
|
|
|
112
142
|
function writtenDurability(written) {
|
|
113
143
|
return written.size === 0 ? undefined : transactionDurability(written);
|
|
114
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* The error an attempt whose phases failed with NOTHING committed is thrown as. A clean stale loss
|
|
147
|
+
* — every refusal a conflict a re-read can clear — is {@link CoordinatorStaleLossError}, which the
|
|
148
|
+
* retry loop re-drives. The four refusals a re-drive can never clear are thrown by name: a validity
|
|
149
|
+
* rejection (the retry's pend would send the same content to the same checks), an expiry (the
|
|
150
|
+
* retry's expiration comes from the same clock), a promise shortfall (the retry meets the same
|
|
151
|
+
* silent members), and a pend or block too large to send. Anything else is a plain error, the
|
|
152
|
+
* historical fail-fast for a hard failure.
|
|
153
|
+
*/
|
|
154
|
+
function cleanFailureError(result, failed) {
|
|
155
|
+
if (result.staleLoss) {
|
|
156
|
+
return new CoordinatorStaleLossError(failed, result.error);
|
|
157
|
+
}
|
|
158
|
+
return result.rejected ?? result.expired ?? result.shortfall ?? result.tooLarge
|
|
159
|
+
?? new Error(`Transaction commit failed: ${result.error}`);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The retry budget one {@link TransactionCoordinator.commitAttempts} cycle spends: attempts and
|
|
163
|
+
* refused refresh rounds alike are counted against `maxAttempts`, under the wall-clock `deadlineMs`
|
|
164
|
+
* and the abort `signal`, with the same jittered backoff policy `Collection.sync` uses between
|
|
165
|
+
* rounds. There is no forward-progress notion here (a commit either lands or it does not), so this
|
|
166
|
+
* simply bounds how many times a losing write is re-driven. The single-collection accounting
|
|
167
|
+
* (`AttemptCycle`) also checks for a wedged revision view; this one cannot yet, since the confirmed
|
|
168
|
+
* revision does not reach it (backlog `debt-multi-collection-retry-cannot-see-the-taken-revision`).
|
|
169
|
+
*/
|
|
170
|
+
class RetryBudget {
|
|
171
|
+
maxAttempts;
|
|
172
|
+
backoff;
|
|
173
|
+
deadlineMs;
|
|
174
|
+
signal;
|
|
175
|
+
rand;
|
|
176
|
+
startedAt = Date.now();
|
|
177
|
+
/** Consecutive clean stale losses: attempts, and refresh rounds refused for a cause that can clear. */
|
|
178
|
+
losses = 0;
|
|
179
|
+
lastLoss;
|
|
180
|
+
constructor(options) {
|
|
181
|
+
this.maxAttempts = options?.maxAttempts ?? DefaultMaxAttempts;
|
|
182
|
+
this.backoff = {
|
|
183
|
+
baseMs: options?.baseBackoffMs ?? DefaultBaseBackoffMs,
|
|
184
|
+
capMs: options?.maxBackoffMs ?? DefaultMaxBackoffMs,
|
|
185
|
+
};
|
|
186
|
+
this.deadlineMs = options?.deadlineMs;
|
|
187
|
+
this.signal = options?.signal;
|
|
188
|
+
this.rand = options?.rand;
|
|
189
|
+
}
|
|
190
|
+
/** The checks before an attempt: the abort signal, and — a progress-agnostic ceiling, once at
|
|
191
|
+
* least one loss was taken — the wall-clock deadline, reported as that loss. */
|
|
192
|
+
assertOpen() {
|
|
193
|
+
if (this.signal?.aborted) {
|
|
194
|
+
throw makeAbortError(this.signal);
|
|
195
|
+
}
|
|
196
|
+
if (this.lastLoss !== undefined && this.pastDeadline()) {
|
|
197
|
+
throw this.lastLoss;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** The aged retry priority for the next attempt, from the losses taken so far, so a
|
|
201
|
+
* repeatedly-losing write out-ranks fresh (priority-0) rivals in the cluster's `resolveRace`.
|
|
202
|
+
* Fairness-only and capped at `MaxPriority`; 0 on the first attempt, so the initial pend
|
|
203
|
+
* serializes exactly as before. */
|
|
204
|
+
priority() {
|
|
205
|
+
return clampPriority(this.losses);
|
|
206
|
+
}
|
|
207
|
+
/** Count a clean stale loss of an attempt; thrown back when that spends the budget. */
|
|
208
|
+
recordLoss(loss) {
|
|
209
|
+
this.lastLoss = loss;
|
|
210
|
+
this.losses++;
|
|
211
|
+
if (this.losses >= this.maxAttempts) {
|
|
212
|
+
throw loss;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** The backoff before a refresh round: none ahead of a cycle's opening round, there being no
|
|
216
|
+
* race to space out; otherwise jittered and growing with the losses taken, so a herd of writers
|
|
217
|
+
* that lost the same race does not re-collide on the next tick. */
|
|
218
|
+
async pause() {
|
|
219
|
+
if (this.losses === 0) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
await abortableDelay(jitteredBackoffMs(this.losses - 1, this.backoff, this.rand), this.signal);
|
|
223
|
+
}
|
|
224
|
+
/** Whether the next refresh round spends the last of the budget, so a participant whose
|
|
225
|
+
* half-landed write is refused again settles it rather than ask for a round it will not get. */
|
|
226
|
+
lastChance() {
|
|
227
|
+
return this.losses + 1 >= this.maxAttempts;
|
|
228
|
+
}
|
|
229
|
+
/** Count a refresh round refused for a cause that can clear (a `completion-refused`
|
|
230
|
+
* {@link TornActionError}); thrown back when that spends the budget or passes the deadline. */
|
|
231
|
+
recordRefusedRound(refused) {
|
|
232
|
+
this.losses++;
|
|
233
|
+
if (this.losses >= this.maxAttempts || this.pastDeadline()) {
|
|
234
|
+
throw refused;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
pastDeadline() {
|
|
238
|
+
return this.deadlineMs !== undefined && Date.now() - this.startedAt >= this.deadlineMs;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
115
241
|
/**
|
|
116
242
|
* Coordinates multi-collection transactions.
|
|
117
243
|
*
|
|
@@ -303,7 +429,9 @@ export class TransactionCoordinator {
|
|
|
303
429
|
* unavailable transactor, unreachable cluster) also propagates without retry — including a pend
|
|
304
430
|
* or commit the cohort refused because the transaction had expired by its members' clocks, which
|
|
305
431
|
* surfaces as {@link TransactionExpiredError} (inside a partial commit's `reason` once a sibling
|
|
306
|
-
* has landed)
|
|
432
|
+
* has landed), a pend too few of the cohort's members answered, which surfaces as
|
|
433
|
+
* {@link PromiseShortfallError}, and a pend the members that sank it judged invalid, which surfaces
|
|
434
|
+
* as {@link ValidityRejectedError}.
|
|
307
435
|
*
|
|
308
436
|
* A participant can also be SAVED between attempts: an attempt that reported a loss may still
|
|
309
437
|
* have stored that participant's log tail, and the refresh before the next attempt then finishes
|
|
@@ -340,14 +468,45 @@ export class TransactionCoordinator {
|
|
|
340
468
|
inFlightDisposers: [],
|
|
341
469
|
participants: new Set(),
|
|
342
470
|
saved: new Map(),
|
|
471
|
+
inFlight: inFlightCycleOf(options),
|
|
472
|
+
};
|
|
473
|
+
const driver = {
|
|
474
|
+
opensWithRefresh: false,
|
|
475
|
+
attempt: priority => {
|
|
476
|
+
// Age the transaction's advisory priority by the losses taken so far (see
|
|
477
|
+
// RetryBudget.priority). Set on the transaction itself because a validated pend carries
|
|
478
|
+
// the transaction, and a member's race resolution reads the priority off it; excluded
|
|
479
|
+
// from the tx id / client signature, so bumping it does not churn identity. Left
|
|
480
|
+
// untouched on the first attempt so the initial pend serializes exactly as before.
|
|
481
|
+
if (priority > 0) {
|
|
482
|
+
transaction.priority = priority;
|
|
483
|
+
}
|
|
484
|
+
return this.commitOnce(transaction, cycle);
|
|
485
|
+
},
|
|
486
|
+
refresh: lastChance => this.refreshBetweenAttempts(this.registeredRefreshers(), cycle, lastChance),
|
|
487
|
+
// Tested on what is still STAGED, not on "every participant saved": a participant can be
|
|
488
|
+
// saved and still hold actions staged after the attempt began, and those the next attempt
|
|
489
|
+
// must commit.
|
|
490
|
+
nothingLeft: () => this.stagedCollections().length === 0,
|
|
343
491
|
};
|
|
344
492
|
try {
|
|
345
|
-
|
|
493
|
+
const written = await this.commitAttempts(driver, cycle, options);
|
|
494
|
+
// Released on every successful exit. An attempt that commits releases it itself; this is
|
|
495
|
+
// for the exit where the refresh between attempts saved what was left, whose consume and
|
|
496
|
+
// replay already gave each saved participant the success path's local treatment.
|
|
497
|
+
this.stampData.delete(transaction.stamp.id);
|
|
498
|
+
return written;
|
|
346
499
|
}
|
|
347
500
|
catch (err) {
|
|
348
501
|
// The ONE place a failure leaves this commit, so no exit — present or future — can skip
|
|
349
502
|
// reporting a participant that is already saved.
|
|
350
|
-
|
|
503
|
+
const reported = this.reportSaved(transaction.id, cycle, err);
|
|
504
|
+
if (reported instanceof CoordinatorPartialCommitError) {
|
|
505
|
+
// The stamp is released, as on the attempt-level partial path (which has already
|
|
506
|
+
// released it): the transaction can no longer be cleanly rolled back.
|
|
507
|
+
this.stampData.delete(transaction.stamp.id);
|
|
508
|
+
}
|
|
509
|
+
throw reported;
|
|
351
510
|
}
|
|
352
511
|
finally {
|
|
353
512
|
// The in-flight marks must outlive each individual attempt (see CommitCycle), so they are
|
|
@@ -358,41 +517,32 @@ export class TransactionCoordinator {
|
|
|
358
517
|
}
|
|
359
518
|
}
|
|
360
519
|
}
|
|
361
|
-
/**
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
// Progress-agnostic ceiling: once we've taken at least one loss, give up if the
|
|
382
|
-
// wall-clock deadline passed (independent of the attempt cap).
|
|
383
|
-
if (deadlineMs !== undefined && lastLoss && Date.now() - startedAt >= deadlineMs) {
|
|
384
|
-
throw lastLoss;
|
|
385
|
-
}
|
|
386
|
-
// Age the transaction's advisory priority by the number of losses taken so far, so a
|
|
387
|
-
// repeatedly-losing transaction out-ranks fresh (priority-0) rivals in the cluster's
|
|
388
|
-
// resolveRace. Fairness-only and capped at MaxPriority; excluded from the tx id / client
|
|
389
|
-
// signature, so bumping it here does not churn identity. Left untouched on the first
|
|
390
|
-
// attempt (staleLosses == 0) so the initial pend serializes exactly as before.
|
|
391
|
-
if (staleLosses > 0) {
|
|
392
|
-
transaction.priority = clampPriority(staleLosses);
|
|
520
|
+
/**
|
|
521
|
+
* The retry loop behind {@link commit} and {@link commitEntry}: every attempt, and the refresh
|
|
522
|
+
* rounds between them, over the `driver`'s steps and the budget `options` sets
|
|
523
|
+
* ({@link RetryBudget}). Records what it learns in `cycle`; the caller owns turning a failure into
|
|
524
|
+
* the right report. On success, reports every participant the cycle saved together with what the
|
|
525
|
+
* final attempt committed (see {@link commit}'s return).
|
|
526
|
+
*
|
|
527
|
+
* Only a CLEAN stale loss is retried. A partial landing on a hard failure, an expired transaction,
|
|
528
|
+
* an unavailable transactor, etc. all propagate unchanged. Once a refresh round has saved a
|
|
529
|
+
* participant and the driver has nothing left to commit, the write is finished and the loop
|
|
530
|
+
* returns at once rather than going round again, where an abort, the deadline or an expiry check
|
|
531
|
+
* could still report failure for a write that is entirely saved.
|
|
532
|
+
*/
|
|
533
|
+
async commitAttempts(driver, cycle, options) {
|
|
534
|
+
const budget = new RetryBudget(options);
|
|
535
|
+
if (driver.opensWithRefresh) {
|
|
536
|
+
budget.assertOpen();
|
|
537
|
+
await this.refreshRounds(driver, budget);
|
|
538
|
+
if (cycle.saved.size > 0 && driver.nothingLeft()) {
|
|
539
|
+
return transactionDurability(cycle.saved);
|
|
393
540
|
}
|
|
541
|
+
}
|
|
542
|
+
for (;;) {
|
|
543
|
+
budget.assertOpen();
|
|
394
544
|
try {
|
|
395
|
-
const committed = await
|
|
545
|
+
const committed = await driver.attempt(budget.priority());
|
|
396
546
|
// Merged, not overwritten, should one participant be both saved and committed (see the
|
|
397
547
|
// NOTE in reportSaved).
|
|
398
548
|
const written = new Map(cycle.saved);
|
|
@@ -402,95 +552,92 @@ export class TransactionCoordinator {
|
|
|
402
552
|
return writtenDurability(written);
|
|
403
553
|
}
|
|
404
554
|
catch (err) {
|
|
405
|
-
// Only a CLEAN stale loss is retryable. A partial landing, an expired transaction, an
|
|
406
|
-
// unavailable transactor, etc. all propagate unchanged.
|
|
407
555
|
if (!(err instanceof CoordinatorStaleLossError)) {
|
|
408
556
|
throw err;
|
|
409
557
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
if (
|
|
413
|
-
throw err;
|
|
414
|
-
}
|
|
415
|
-
// Re-read fresh state before re-attempting so the next commit pends against current
|
|
416
|
-
// revisions (mirrors how Collection.sync calls updateInternal() before retrying).
|
|
417
|
-
//
|
|
418
|
-
// A participant whose log tail landed despite the reported loss finds its own entry
|
|
419
|
-
// here and FINISHES that action before consuming it (Collection.completeOwnEntry,
|
|
420
|
-
// from the attempt commitOnceLatched retained). When finishing is refused for a cause
|
|
421
|
-
// that can clear, the refresh — not the commit — is what gets retried: the
|
|
422
|
-
// participant's log entry is already durable, and once a refresh adopts a newer
|
|
423
|
-
// revision commitOnce appends a SECOND entry under this transaction's id. (At an
|
|
424
|
-
// unchanged revision the re-append is byte-identical — see
|
|
425
|
-
// Collection.logAppendBlockIds — so what is at stake is the duplicate entry, not two
|
|
426
|
-
// versions of one tail block.)
|
|
427
|
-
// Each such round counts against the same budget as a stale loss; a permanent
|
|
428
|
-
// refusal (a rival holds the revision) escapes as the named TornActionError at once.
|
|
429
|
-
// Re-refreshing collections an earlier round already refreshed is a no-op.
|
|
430
|
-
// The round that would spend the last of the budget is told so, which makes each
|
|
431
|
-
// participant SETTLE a half-landed write rather than ask for another round: the
|
|
432
|
-
// error thrown below then says whether the write can still land (`final`). A
|
|
433
|
-
// deadline cannot be foreseen the same way; a write given up on it stays unsettled.
|
|
434
|
-
for (;;) {
|
|
435
|
-
const delay = jitteredBackoffMs(staleLosses - 1, { baseMs: baseBackoffMs, capMs: maxBackoffMs }, options?.rand);
|
|
436
|
-
await abortableDelay(delay, signal);
|
|
437
|
-
const refused = await this.refreshBetweenAttempts(cycle, staleLosses + 1 >= maxAttempts);
|
|
438
|
-
if (refused === undefined) {
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
staleLosses++;
|
|
442
|
-
if (staleLosses >= maxAttempts
|
|
443
|
-
|| (deadlineMs !== undefined && Date.now() - startedAt >= deadlineMs)) {
|
|
444
|
-
throw refused;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
if (cycle.saved.size > 0 && this.stagedCollections().length === 0) {
|
|
448
|
-
// The refresh saved what was left: nothing is staged any more, so this transaction
|
|
449
|
-
// is committed. Return now rather than go round again, where an abort, the deadline
|
|
450
|
-
// or an expiry check could still report failure for a transaction that is entirely
|
|
451
|
-
// saved. The refresh's consume and replay already gave each saved participant the
|
|
452
|
-
// success path's local treatment, so only the stamp is left to release.
|
|
453
|
-
// Tested on what is still STAGED, not on "every participant saved": a participant
|
|
454
|
-
// can be saved and still hold actions staged after the attempt began, and those
|
|
455
|
-
// the next attempt must commit.
|
|
456
|
-
this.stampData.delete(transaction.stamp.id);
|
|
558
|
+
budget.recordLoss(err);
|
|
559
|
+
await this.refreshRounds(driver, budget);
|
|
560
|
+
if (cycle.saved.size > 0 && driver.nothingLeft()) {
|
|
457
561
|
return transactionDurability(cycle.saved);
|
|
458
562
|
}
|
|
459
563
|
}
|
|
460
564
|
}
|
|
461
565
|
}
|
|
462
566
|
/**
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
567
|
+
* Back off, refresh, and keep refreshing while a round reports that a participant's half-landed
|
|
568
|
+
* write could not be finished YET. Re-reading fresh state before re-attempting is what lets the
|
|
569
|
+
* next attempt pend against current revisions (mirrors how Collection.sync calls updateInternal()
|
|
570
|
+
* before retrying), and a participant whose log tail landed despite the reported loss finds its
|
|
571
|
+
* own entry here and FINISHES that action before consuming it (Collection.completeOwnEntry, from
|
|
572
|
+
* the attempt the previous one retained).
|
|
573
|
+
*
|
|
574
|
+
* When finishing is refused for a cause that can clear, the refresh — not the commit — is what
|
|
575
|
+
* gets retried: the participant's log entry is already durable, and once a refresh adopts a newer
|
|
576
|
+
* revision the next attempt appends a SECOND entry under the write's id. (At an unchanged
|
|
577
|
+
* revision the re-append is byte-identical — see Collection.logAppendBlockIds — so what is at
|
|
578
|
+
* stake is the duplicate entry, not two versions of one tail block.) Each such round counts
|
|
579
|
+
* against the same budget as a stale loss; a permanent refusal (a rival holds the revision)
|
|
580
|
+
* escapes as the named TornActionError at once. Re-refreshing a collection an earlier round
|
|
581
|
+
* already refreshed is a no-op. The round that would spend the last of the budget is told so,
|
|
582
|
+
* which makes each participant SETTLE a half-landed write rather than ask for another round: the
|
|
583
|
+
* error thrown then says whether the write can still land (`final`). A deadline cannot be
|
|
584
|
+
* foreseen the same way; a write given up on it stays unsettled.
|
|
585
|
+
*/
|
|
586
|
+
async refreshRounds(driver, budget) {
|
|
587
|
+
for (;;) {
|
|
588
|
+
await budget.pause();
|
|
589
|
+
const refused = await driver.refresh(budget.lastChance());
|
|
590
|
+
if (refused === undefined) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
budget.recordRefusedRound(refused);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/** Every registered collection as a refresh-round participant of a transaction's commit.
|
|
597
|
+
*
|
|
598
|
+
* NOTE: EVERY registered collection, not only the participants of the transaction. Not free: a
|
|
599
|
+
* non-participant's refresh throws CollectionHeaderVanishedError if its header momentarily reads
|
|
600
|
+
* absent while it holds a committed revision, aborting this retry. And because a throw does not
|
|
601
|
+
* stop the round, an unreachable cluster costs one failed read per registered collection rather
|
|
602
|
+
* than one. The registered set is small today; if that (or retry latency) ever bites, narrow this
|
|
603
|
+
* to the transaction's participants. (The per-entry commit already refreshes its participants
|
|
604
|
+
* only, through their handles — see `EntryParticipants.refreshers`.) */
|
|
605
|
+
registeredRefreshers() {
|
|
606
|
+
return [...this.collections].map(([collectionId, collection]) => ({
|
|
607
|
+
collectionId,
|
|
608
|
+
refresh: (report, lastChance) => collection.refreshInFlight(report, lastChance),
|
|
609
|
+
}));
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* One refresh round between attempts: refresh every participant, recording in `cycle.saved` each
|
|
613
|
+
* one whose refresh finished the write's own log entry (see {@link Collection.refreshInFlight}),
|
|
614
|
+
* with who holds it — `undefined` when the refresh could not say, which still records it as
|
|
615
|
+
* saved — and telling the participant so (`RefreshParticipant.saved`).
|
|
466
616
|
*
|
|
467
|
-
* Visits EVERY
|
|
468
|
-
* participant later in the
|
|
617
|
+
* Visits EVERY participant even after one throws. Stopping at the first throw would leave a
|
|
618
|
+
* participant later in the round unfinished whenever an earlier one is torn — its log tail stored
|
|
469
619
|
* and its other blocks never landed, the silent loss `Collection.completeOwnEntry` exists to
|
|
470
620
|
* prevent — and unreported. Each refresh is independent (its own latch, its own instance state),
|
|
471
621
|
* so one failing says nothing about the next.
|
|
472
622
|
*
|
|
473
|
-
* NOTE:
|
|
474
|
-
*
|
|
475
|
-
*
|
|
476
|
-
* a throw no longer stops the round, an unreachable cluster costs one failed read per registered
|
|
477
|
-
* collection rather than one. The registered set is small today; if that (or retry latency) ever
|
|
478
|
-
* bites, narrow this to the transaction's participants.
|
|
623
|
+
* NOTE: no `tailFloor` is passed to any refresh — the coordinator's loop loses the confirmed
|
|
624
|
+
* revision before the round runs (backlog `debt-multi-collection-retry-cannot-see-the-taken-revision`);
|
|
625
|
+
* the per-entry commit has the same gap, through the same round.
|
|
479
626
|
*
|
|
480
627
|
* @param lastChance - this is the round that spends the last of the retry budget, so a
|
|
481
628
|
* participant whose half-landed write is refused again settles it (`Collection.completeOwnEntry`)
|
|
482
629
|
* rather than asking for a round it will not get; the error returned then carries `final`.
|
|
483
630
|
* @returns the first `completion-refused` {@link TornActionError} when that is the only way any
|
|
484
631
|
* refresh failed — the round is worth retrying; `undefined` when every refresh succeeded.
|
|
485
|
-
* @throws the first other error, once every
|
|
632
|
+
* @throws the first other error, once every participant has been visited.
|
|
486
633
|
*/
|
|
487
|
-
async refreshBetweenAttempts(cycle, lastChance) {
|
|
634
|
+
async refreshBetweenAttempts(participants, cycle, lastChance) {
|
|
488
635
|
let terminal;
|
|
489
636
|
let refused;
|
|
490
|
-
for (const
|
|
637
|
+
for (const participant of participants) {
|
|
491
638
|
const report = {};
|
|
492
639
|
try {
|
|
493
|
-
await
|
|
640
|
+
await participant.refresh(report, lastChance);
|
|
494
641
|
}
|
|
495
642
|
catch (err) {
|
|
496
643
|
if (err instanceof TornActionError && err.reason === 'completion-refused') {
|
|
@@ -503,7 +650,8 @@ export class TransactionCoordinator {
|
|
|
503
650
|
// Read after the try, not only on success: a refresh can throw AFTER it finished the
|
|
504
651
|
// entry, and that participant is saved either way.
|
|
505
652
|
if (report.ownEntryFinished !== undefined) {
|
|
506
|
-
recordCollectionDurability(cycle.saved, collectionId, report.ownEntryFinished.durability);
|
|
653
|
+
recordCollectionDurability(cycle.saved, participant.collectionId, report.ownEntryFinished.durability);
|
|
654
|
+
participant.saved?.();
|
|
507
655
|
}
|
|
508
656
|
}
|
|
509
657
|
if (terminal !== undefined) {
|
|
@@ -546,10 +694,11 @@ export class TransactionCoordinator {
|
|
|
546
694
|
*
|
|
547
695
|
* Local state already matches that report: a saved participant was consumed by its refresh, and
|
|
548
696
|
* an unsaved one kept its staged actions (a failed finish throws before touching anything, and
|
|
549
|
-
* commitOnceLatched restores every participant it did not commit
|
|
550
|
-
*
|
|
697
|
+
* commitOnceLatched restores every participant it did not commit; the per-entry commit leaves a
|
|
698
|
+
* refused participant's segment sealed and first). Releasing a transaction's stamp on the partial
|
|
699
|
+
* report is {@link commit}'s, which reads the class of what this returns.
|
|
551
700
|
*/
|
|
552
|
-
reportSaved(
|
|
701
|
+
reportSaved(actionId, cycle, err) {
|
|
553
702
|
if (cycle.saved.size === 0) {
|
|
554
703
|
return err;
|
|
555
704
|
}
|
|
@@ -566,8 +715,7 @@ export class TransactionCoordinator {
|
|
|
566
715
|
// committed. Unreachable while a session stages and commits on one call path; if staging ever
|
|
567
716
|
// races a commit, report such a participant in both lists.
|
|
568
717
|
const failed = [...cycle.participants].filter(id => !committed.has(id));
|
|
569
|
-
log('commit:partial-after-refresh tx=%s committed=%o failed=%o reason=%s',
|
|
570
|
-
this.stampData.delete(transaction.stamp.id);
|
|
718
|
+
log('commit:partial-after-refresh tx=%s committed=%o failed=%o reason=%s', actionId, [...committed.keys()], failed, reason instanceof Error ? reason.name : String(reason));
|
|
571
719
|
return new CoordinatorPartialCommitError(committed, failed, reason);
|
|
572
720
|
}
|
|
573
721
|
/**
|
|
@@ -647,7 +795,7 @@ export class TransactionCoordinator {
|
|
|
647
795
|
// (Collection.completeOwnEntry). `transaction.id` is stable across retries, so re-marking on a
|
|
648
796
|
// later attempt re-states the same fact. Only participants are marked; a registered
|
|
649
797
|
// non-participant is left unmarked and its refresh behaves exactly as a reader's.
|
|
650
|
-
cycle.inFlightDisposers.push(collection.beginInFlightAction(transaction.id));
|
|
798
|
+
cycle.inFlightDisposers.push(collection.beginInFlightAction(transaction.id, cycle.inFlight));
|
|
651
799
|
}
|
|
652
800
|
return await this.commitOnceLatched(transaction, collectionData, cycle);
|
|
653
801
|
}
|
|
@@ -710,10 +858,12 @@ export class TransactionCoordinator {
|
|
|
710
858
|
for (const { collectionId, collection } of collectionData) {
|
|
711
859
|
preCommitSnapshots.set(collectionId, collection.snapshotPending());
|
|
712
860
|
}
|
|
861
|
+
// Read per attempt: commitAttempts ages the transaction's priority between attempts.
|
|
862
|
+
const context = writeContextOf(transaction, cycle.inFlight);
|
|
713
863
|
let coordResult;
|
|
714
864
|
try {
|
|
715
865
|
for (const { collectionId, collection } of collectionData) {
|
|
716
|
-
const applyResult = await this.applyActionsToCollection({ collectionId, actions: collection.getPendingActions() },
|
|
866
|
+
const applyResult = await this.applyActionsToCollection({ collectionId, actions: collection.getPendingActions() }, context, allCollectionIds);
|
|
717
867
|
if (!applyResult.success) {
|
|
718
868
|
throw new Error(`Transaction commit failed: ${applyResult.error}`);
|
|
719
869
|
}
|
|
@@ -724,7 +874,7 @@ export class TransactionCoordinator {
|
|
|
724
874
|
// What each pend carries for members to re-check (post-log-append) — see pendValidationFor.
|
|
725
875
|
const validation = await this.pendValidationFor(transaction, collectionTransforms);
|
|
726
876
|
// Execute consensus phases (GATHER, PEND, COMMIT)
|
|
727
|
-
coordResult = await this.coordinateTransaction(
|
|
877
|
+
coordResult = await this.coordinateTransaction(context, validation, collectionTransforms, criticalBlocks, pendedRevs);
|
|
728
878
|
}
|
|
729
879
|
catch (err) {
|
|
730
880
|
// A throw here means the failure happened BEFORE any collection could
|
|
@@ -733,7 +883,7 @@ export class TransactionCoordinator {
|
|
|
733
883
|
// to its pre-append snapshot — a genuinely clean rollback that leaves each
|
|
734
884
|
// tracker pristine for retry (see txn-failed-commit-leaves-staged-log-entry).
|
|
735
885
|
for (const { collectionId, collection } of collectionData) {
|
|
736
|
-
collection.restorePending(preCommitSnapshots.get(collectionId));
|
|
886
|
+
await collection.restorePending(preCommitSnapshots.get(collectionId));
|
|
737
887
|
}
|
|
738
888
|
throw err;
|
|
739
889
|
}
|
|
@@ -752,7 +902,8 @@ export class TransactionCoordinator {
|
|
|
752
902
|
for (const { collectionId, collection } of collectionData) {
|
|
753
903
|
if (committed.has(collectionId))
|
|
754
904
|
continue;
|
|
755
|
-
collection.retainInFlightAttempt(
|
|
905
|
+
collection.retainInFlightAttempt({
|
|
906
|
+
actionId: transaction.id,
|
|
756
907
|
rev: pendedRevs.get(collectionId),
|
|
757
908
|
transforms: copyTransforms(collectionTransforms.get(collectionId)),
|
|
758
909
|
tailId: criticalBlocks.get(collectionId),
|
|
@@ -763,27 +914,34 @@ export class TransactionCoordinator {
|
|
|
763
914
|
// while another failed permanently. A uniform pre-append restore would
|
|
764
915
|
// corrupt the committed half — re-staging its already-durable actions as
|
|
765
916
|
// still-pending, so tracker memory would disagree with cluster storage.
|
|
766
|
-
// Split the local handling instead:
|
|
917
|
+
// Split the local handling instead, in two passes: the committed half is folded
|
|
918
|
+
// first, in one await-free pass (so a committed read observes every committed
|
|
919
|
+
// participant pre-fold or post-fold, as the success fold below promises), and the
|
|
920
|
+
// failed half is restored after it — a restore can replay, so it is awaited, which
|
|
921
|
+
// admits no interleaved stage because every participant's latch is held across
|
|
922
|
+
// this span.
|
|
767
923
|
for (const { collectionId, collection } of collectionData) {
|
|
768
|
-
if (committed.has(collectionId))
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
924
|
+
if (!committed.has(collectionId))
|
|
925
|
+
continue;
|
|
926
|
+
// Committed → the success-path local treatment (see below): fold the
|
|
927
|
+
// committed transforms into the read cache BEFORE resetting the tracker,
|
|
928
|
+
// then drop the now-durable pending actions so a retry cannot re-log them.
|
|
929
|
+
// NOTE: no-double-apply on retry depends on clearPendingActions() running for
|
|
930
|
+
// EVERY committed collection here before any re-drive of commit(). If a committed
|
|
931
|
+
// collection kept its pending queue, a subsequent commit() would re-append and
|
|
932
|
+
// re-log its already-durable actions — a duplicate log entry on the winner. The
|
|
933
|
+
// no-double-apply-on-retry test in transaction.spec.ts locks this.
|
|
934
|
+
const rev = collection.recordCommitted(transaction.id, pendedRevs.get(collectionId));
|
|
935
|
+
collection.applyCommittedToCache(collectionTransforms.get(collectionId), rev);
|
|
936
|
+
collection.tracker.reset();
|
|
937
|
+
collection.clearPendingActions();
|
|
938
|
+
}
|
|
939
|
+
for (const { collectionId, collection } of collectionData) {
|
|
940
|
+
if (committed.has(collectionId))
|
|
941
|
+
continue;
|
|
942
|
+
// Failed / never-committed → restore the pre-append snapshot so a retry
|
|
943
|
+
// re-appends cleanly (no duplicate log entry).
|
|
944
|
+
await collection.restorePending(preCommitSnapshots.get(collectionId));
|
|
787
945
|
}
|
|
788
946
|
const failed = [...(coordResult.failedCollections ?? new Set())];
|
|
789
947
|
if (coordResult.staleLoss) {
|
|
@@ -823,25 +981,14 @@ export class TransactionCoordinator {
|
|
|
823
981
|
// EMPTY committed set: PEND failed, or the whole commit failed cleanly with
|
|
824
982
|
// nothing durable. Restore every tracker so each is pristine for retry.
|
|
825
983
|
for (const { collectionId, collection } of collectionData) {
|
|
826
|
-
collection.restorePending(preCommitSnapshots.get(collectionId));
|
|
984
|
+
await collection.restorePending(preCommitSnapshots.get(collectionId));
|
|
827
985
|
}
|
|
828
986
|
// Distinguish a genuine optimistic-concurrency conflict (a stale loss / pending
|
|
829
987
|
// contention — retryable after a re-read) from a hard failure (unavailable transactor,
|
|
830
988
|
// storage rejection, expired). Only the former is worth re-driving; the retry wrapper in
|
|
831
989
|
// commit() catches CoordinatorStaleLossError and re-attempts, while a plain Error escapes
|
|
832
990
|
// immediately (preserving the historical fail-fast behaviour for hard failures).
|
|
833
|
-
|
|
834
|
-
throw new CoordinatorStaleLossError([...(coordResult.failedCollections ?? new Set(allCollectionIds))], coordResult.error);
|
|
835
|
-
}
|
|
836
|
-
// An expiry is surfaced by name and never re-driven: the retry's pend would carry an
|
|
837
|
-
// expiration from the same clock.
|
|
838
|
-
if (coordResult.expired !== undefined) {
|
|
839
|
-
throw coordResult.expired;
|
|
840
|
-
}
|
|
841
|
-
if (coordResult.tooLarge !== undefined) {
|
|
842
|
-
throw coordResult.tooLarge;
|
|
843
|
-
}
|
|
844
|
-
throw new Error(`Transaction commit failed: ${coordResult.error}`);
|
|
991
|
+
throw cleanFailureError(coordResult, [...(coordResult.failedCollections ?? new Set(allCollectionIds))]);
|
|
845
992
|
}
|
|
846
993
|
// Advance actionContext, fold the committed transforms into each
|
|
847
994
|
// collection's read cache, reset the tracker, and drop the now-committed
|
|
@@ -864,6 +1011,150 @@ export class TransactionCoordinator {
|
|
|
864
1011
|
this.stampData.delete(transaction.stamp.id);
|
|
865
1012
|
return coordResult.committedCollections;
|
|
866
1013
|
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Commit one queued entry's slice of every collection it wrote, as ONE all-or-nothing round
|
|
1016
|
+
* (docs/offline-sync.md §One entry, one network transaction): for each collection in
|
|
1017
|
+
* `entry.collectionIds`, exactly its HEAD sealed segment, which must be `entry.entryId`, as one
|
|
1018
|
+
* log entry under `entry.actionId` and `entry.timestamp`. The offline-sync queue is the caller,
|
|
1019
|
+
* for an entry that wrote more than one collection; an entry that wrote one commits through
|
|
1020
|
+
* `Collection.commitHead`, whose steps this drives in lock-step across the participants
|
|
1021
|
+
* (`HeadCommitHandle` in collection/head-commit.ts): open a handle per participant, in sorted
|
|
1022
|
+
* collection-id order; the opening refresh, which finishes and consumes an entry an earlier
|
|
1023
|
+
* `commitEntry` under this action id left in a participant's log; then per attempt, build every
|
|
1024
|
+
* participant's attempt on its head tracker, record them through `entry.recordAttempt`, release
|
|
1025
|
+
* every latch, run the phases ({@link coordinateAttempts}: pend every participant, then commit),
|
|
1026
|
+
* retake every latch, and apply the outcome — fold then replay on a commit, the recorded refusal
|
|
1027
|
+
* re-checked and the attempt retained on a refusal — with the retry loop of {@link commit}
|
|
1028
|
+
* ({@link commitAttempts}) between attempts.
|
|
1029
|
+
*
|
|
1030
|
+
* What the slice deliberately does NOT do, where {@link commit} does: no pre-commit snapshots or
|
|
1031
|
+
* `restorePending` (an attempt never touches the live tracker; a failed one leaves it as it was
|
|
1032
|
+
* by construction); no `getPendingActions`, `clearPendingActions` or `tracker.reset` (the head
|
|
1033
|
+
* segment is dropped and folded through the handle); no stamp (the one-open-stamp rule is about
|
|
1034
|
+
* staged state, which a slice does not stage — a slice may run while a stamp is open on another
|
|
1035
|
+
* coordinator over the same collections, which is the plugin's open SQL transaction; the
|
|
1036
|
+
* collection latch is what serialises them); no expiry check (an entry has no stamp to expire).
|
|
1037
|
+
* Its refresh rounds refresh the entry's participants only, through their handles, never the
|
|
1038
|
+
* coordinator's whole map.
|
|
1039
|
+
*
|
|
1040
|
+
* Requires a coordinator built with `pendValidation: 'none'`, and throws before touching
|
|
1041
|
+
* anything otherwise: a queued entry has no statements for a member to re-execute, and the pend
|
|
1042
|
+
* carries bare transforms exactly as the plugin's `commitBatchLegacy` sends them. The queue builds
|
|
1043
|
+
* one coordinator per submission over exactly the entry's collections; running `commitEntry`
|
|
1044
|
+
* while a {@link commit} of the same coordinator is open is not a supported combination, and
|
|
1045
|
+
* nothing but the collection latches guards it.
|
|
1046
|
+
*
|
|
1047
|
+
* @returns who holds each collection the entry wrote, across every attempt, as {@link commit}
|
|
1048
|
+
* answers it — a participant the refresh saved included, listed with an `undefined` class (and
|
|
1049
|
+
* `overall` then absent) when the refresh established it was saved under `entry.actionId` without
|
|
1050
|
+
* re-sending (the status-read fallback of `Collection.completeOwnEntry`, where
|
|
1051
|
+
* `Collection.commitHead` answers `undefined`). Never `undefined`: a slice always writes at least
|
|
1052
|
+
* one collection, so the "nothing was written" answer `commit` has is unrepresentable here.
|
|
1053
|
+
* @throws what {@link commit} throws — a {@link CoordinatorStaleLossError} once the retry budget
|
|
1054
|
+
* is spent, a {@link CoordinatorPartialCommitError} for a partial landing (every participant it
|
|
1055
|
+
* names as committed has had its head consumed; every failed one's head is still sealed and
|
|
1056
|
+
* first, its attempt retained), the named refusals, `TornActionError` out of a refresh — plus the
|
|
1057
|
+
* recorded refusal of any participant's head (an `ActionRefusedError`), thrown with every other
|
|
1058
|
+
* participant's segment intact since nothing was sent or the send was refused, and
|
|
1059
|
+
* `SealedSegmentsError` from `Collection.openHeadCommit`'s preconditions, before anything is done.
|
|
1060
|
+
*/
|
|
1061
|
+
async commitEntry(entry, options) {
|
|
1062
|
+
if (this.pendValidation !== 'none') {
|
|
1063
|
+
throw new Error(`commitEntry ${entry.entryId}: a queued entry commits through a coordinator built with `
|
|
1064
|
+
+ `pendValidation: 'none' — it has no statements for a member to re-execute`);
|
|
1065
|
+
}
|
|
1066
|
+
const inFlight = inFlightCycleOf(options);
|
|
1067
|
+
const participants = await EntryParticipants.open(this.collections, entry, inFlight);
|
|
1068
|
+
const cycle = {
|
|
1069
|
+
// The handles own the in-flight marks (closed with them), so none to dispose here.
|
|
1070
|
+
inFlightDisposers: [],
|
|
1071
|
+
participants: new Set(participants.ids()),
|
|
1072
|
+
saved: new Map(),
|
|
1073
|
+
inFlight,
|
|
1074
|
+
};
|
|
1075
|
+
const driver = {
|
|
1076
|
+
opensWithRefresh: true,
|
|
1077
|
+
attempt: priority => this.commitEntryOnce(entry, participants, cycle, priority),
|
|
1078
|
+
refresh: lastChance => this.refreshBetweenAttempts(participants.refreshers(), cycle, lastChance),
|
|
1079
|
+
nothingLeft: () => participants.size === 0,
|
|
1080
|
+
};
|
|
1081
|
+
try {
|
|
1082
|
+
const written = await this.commitAttempts(driver, cycle, options);
|
|
1083
|
+
if (written === undefined) {
|
|
1084
|
+
throw new Error(`commitEntry ${entry.entryId}: the cycle ended with nothing committed and nothing saved`);
|
|
1085
|
+
}
|
|
1086
|
+
return written;
|
|
1087
|
+
}
|
|
1088
|
+
catch (err) {
|
|
1089
|
+
throw this.reportSaved(entry.actionId, cycle, err);
|
|
1090
|
+
}
|
|
1091
|
+
finally {
|
|
1092
|
+
participants.close();
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* One attempt of {@link commitEntry}, under the latches `participants` hold: every remaining
|
|
1097
|
+
* participant's attempt is built (`HeadCommitHandle.stage`, which re-checks the head's recorded
|
|
1098
|
+
* refusal and throws it), recorded through the write-ahead hook — a throw there aborts with
|
|
1099
|
+
* nothing sent and nothing changed — then sent as one round with every latch released, and the
|
|
1100
|
+
* outcome applied under the retaken latches:
|
|
1101
|
+
*
|
|
1102
|
+
* - a commit: fold every participant in one await-free pass, then replay each
|
|
1103
|
+
* (`EntryParticipants.landed`); the attempt's durability per collection is the answer;
|
|
1104
|
+
* - a partial landing: the committed participants folded, replayed and recorded in `cycle.saved`
|
|
1105
|
+
* — before their local treatment, so a throw in it is still reported over the right committed
|
|
1106
|
+
* set — and the rest refused (their recorded refusal re-checked, their attempt retained); then
|
|
1107
|
+
* {@link CoordinatorStaleLossError} when every refusal was a returned one, so the retry loop
|
|
1108
|
+
* finishes the refused participants forward over what is left (as `commitOnceLatched`'s
|
|
1109
|
+
* partial-retry branch does), or {@link CoordinatorPartialCommitError} on a hard failure;
|
|
1110
|
+
* - nothing committed: every participant refused, then the failure thrown by its class
|
|
1111
|
+
* ({@link cleanFailureError}).
|
|
1112
|
+
*
|
|
1113
|
+
* The refusal re-check runs after the committed participants are folded, never before: a
|
|
1114
|
+
* sibling's recorded refusal must not keep a durable commit from being applied locally.
|
|
1115
|
+
*/
|
|
1116
|
+
async commitEntryOnce(entry, participants, cycle, priority) {
|
|
1117
|
+
const staged = await participants.stage();
|
|
1118
|
+
if (entry.recordAttempt !== undefined) {
|
|
1119
|
+
await entry.recordAttempt(participants.retainedAttempts(staged));
|
|
1120
|
+
}
|
|
1121
|
+
const context = {
|
|
1122
|
+
actionId: entry.actionId,
|
|
1123
|
+
timestamp: entry.timestamp,
|
|
1124
|
+
reads: [],
|
|
1125
|
+
priority,
|
|
1126
|
+
...refuseSoloField(cycle.inFlight.refuseSolo),
|
|
1127
|
+
};
|
|
1128
|
+
const attempts = new Map(Array.from(staged, ([collectionId, attempt]) => [collectionId, collectionAttemptOf(attempt)]));
|
|
1129
|
+
participants.release();
|
|
1130
|
+
let coordResult;
|
|
1131
|
+
try {
|
|
1132
|
+
coordResult = await this.coordinateAttempts(context, undefined, attempts);
|
|
1133
|
+
}
|
|
1134
|
+
finally {
|
|
1135
|
+
await participants.retake();
|
|
1136
|
+
}
|
|
1137
|
+
if (coordResult.success) {
|
|
1138
|
+
await participants.landed(staged, participants.ids());
|
|
1139
|
+
return coordResult.committedCollections;
|
|
1140
|
+
}
|
|
1141
|
+
const committed = coordResult.committedCollections ?? new Map();
|
|
1142
|
+
const failed = participants.ids().filter(collectionId => !committed.has(collectionId));
|
|
1143
|
+
if (committed.size === 0) {
|
|
1144
|
+
participants.refused(staged, failed);
|
|
1145
|
+
throw cleanFailureError(coordResult, failed);
|
|
1146
|
+
}
|
|
1147
|
+
for (const [collectionId, durability] of committed) {
|
|
1148
|
+
recordCollectionDurability(cycle.saved, collectionId, durability);
|
|
1149
|
+
}
|
|
1150
|
+
await participants.landed(staged, committed.keys());
|
|
1151
|
+
participants.refused(staged, failed);
|
|
1152
|
+
if (coordResult.staleLoss) {
|
|
1153
|
+
log('commitEntry:partial-retry entry=%s action=%s committed=%o failed=%o reason=%s', entry.entryId, entry.actionId, [...committed.keys()], failed, coordResult.error);
|
|
1154
|
+
throw new CoordinatorStaleLossError(failed, coordResult.error);
|
|
1155
|
+
}
|
|
1156
|
+
throw new CoordinatorPartialCommitError(committed, failed, coordResult.expired ?? coordResult.error);
|
|
1157
|
+
}
|
|
867
1158
|
/**
|
|
868
1159
|
* Rollback a transaction (undo only the given stampId's applied actions).
|
|
869
1160
|
*
|
|
@@ -910,9 +1201,14 @@ export class TransactionCoordinator {
|
|
|
910
1201
|
// Accepted: the tracker half already discards their transforms, and symmetric is the safer
|
|
911
1202
|
// state — leaving such an action queued while its transforms are gone is exactly the
|
|
912
1203
|
// phantom that a conflicting sync's replayActions would resurrect as live data.
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1204
|
+
// Awaited: a restore across a change to the collection's sealed segments replays them
|
|
1205
|
+
// (Collection.restorePending), and the single-call-path assumption above is what keeps a
|
|
1206
|
+
// stage from landing on the collection while that replay runs. Every participant is
|
|
1207
|
+
// restored even after one throws (a refusal of the restored open segment, or a failed
|
|
1208
|
+
// re-read inside the replay): each restore is independent, and stopping at the first throw
|
|
1209
|
+
// would leave the later participants holding the rolled-back stamp's actions, which the
|
|
1210
|
+
// next transaction would then log as its own.
|
|
1211
|
+
await visitEveryThenThrow(data.preSnapshot, ([collection, snapshot]) => collection.restorePending(snapshot));
|
|
916
1212
|
}
|
|
917
1213
|
/**
|
|
918
1214
|
* Get current transforms from all collections.
|
|
@@ -1120,10 +1416,11 @@ export class TransactionCoordinator {
|
|
|
1120
1416
|
// Released in the finally: execute has early failure returns.
|
|
1121
1417
|
const latchReleases = [];
|
|
1122
1418
|
try {
|
|
1123
|
-
// NOTE: deadlock-freedom against a concurrent commitOnce
|
|
1124
|
-
// the SAME order, not merely each in a sorted one. `CollectionId` is a
|
|
1125
|
-
// default `.sort()
|
|
1126
|
-
//
|
|
1419
|
+
// NOTE: deadlock-freedom against a concurrent commitOnce or commitEntry needs ALL three
|
|
1420
|
+
// paths to acquire in the SAME order, not merely each in a sorted one. `CollectionId` is a
|
|
1421
|
+
// string, so this default `.sort()`, commitOnce's explicit `<`/`>` comparator and
|
|
1422
|
+
// `compareCollectionIds` in entry-commit.ts agree today. If any spelling changes — a
|
|
1423
|
+
// custom comparator, a non-string id — change all three together.
|
|
1127
1424
|
for (const collectionId of [...allCollectionIds].sort()) {
|
|
1128
1425
|
const collection = this.collections.get(collectionId);
|
|
1129
1426
|
if (collection) {
|
|
@@ -1135,8 +1432,9 @@ export class TransactionCoordinator {
|
|
|
1135
1432
|
for (const collectionId of allCollectionIds) {
|
|
1136
1433
|
await this.collections.get(collectionId)?.restageIfBasesMoved();
|
|
1137
1434
|
}
|
|
1435
|
+
const context = writeContextOf(transaction);
|
|
1138
1436
|
for (const [collectionId, actions] of batches) {
|
|
1139
|
-
const applyResult = await this.applyActionsToCollection({ collectionId, actions },
|
|
1437
|
+
const applyResult = await this.applyActionsToCollection({ collectionId, actions }, context, allCollectionIds);
|
|
1140
1438
|
if (!applyResult.success) {
|
|
1141
1439
|
return { success: false, error: applyResult.error };
|
|
1142
1440
|
}
|
|
@@ -1150,7 +1448,7 @@ export class TransactionCoordinator {
|
|
|
1150
1448
|
const applyMs = Date.now() - tApply;
|
|
1151
1449
|
// 4. Coordinate (GATHER if multi-collection)
|
|
1152
1450
|
const tCoord = Date.now();
|
|
1153
|
-
const coordResult = await this.coordinateTransaction(
|
|
1451
|
+
const coordResult = await this.coordinateTransaction(context, validation, collectionTransforms, criticalBlocks, pendedRevs);
|
|
1154
1452
|
const coordMs = Date.now() - tCoord;
|
|
1155
1453
|
if (!coordResult.success) {
|
|
1156
1454
|
log('execute:done trxId=%s engine=%dms apply=%dms coordinate=%dms success=false total=%dms', trxId, engineMs, applyMs, coordMs, Date.now() - t0);
|
|
@@ -1161,8 +1459,12 @@ export class TransactionCoordinator {
|
|
|
1161
1459
|
// pending queues aren't left mis-tracking already-durable state; the failed subset
|
|
1162
1460
|
// is unwound to its pre-staging snapshot, and the stamp tracking is dropped so no
|
|
1163
1461
|
// unsafe all-or-nothing undo handle survives (rollback() would rewind the winner).
|
|
1164
|
-
//
|
|
1165
|
-
//
|
|
1462
|
+
// Two passes, as in commitOnceLatched: the committed subset's fold is kept
|
|
1463
|
+
// await-free for the same reason the success fold is, and the failed subset's
|
|
1464
|
+
// restore comes after it. That restore is awaited — it can replay the collection's
|
|
1465
|
+
// sealed segments — and the await admits no interleaved stage, because every
|
|
1466
|
+
// participant's latch is held across this span (restorePending stays latch-free by
|
|
1467
|
+
// contract and relies on exactly that).
|
|
1166
1468
|
// NOTE: execute() has no retry loop at all, so a returned refusal after a sibling
|
|
1167
1469
|
// committed is reported here at once, where commit() would re-drive the refused
|
|
1168
1470
|
// collection forward (the partial-retry branch of commitOnceLatched). No production
|
|
@@ -1175,26 +1477,27 @@ export class TransactionCoordinator {
|
|
|
1175
1477
|
if (committed.size > 0) {
|
|
1176
1478
|
for (const collectionId of batches.keys()) {
|
|
1177
1479
|
const collection = this.collections.get(collectionId);
|
|
1178
|
-
if (!collection)
|
|
1480
|
+
if (!collection || !committed.has(collectionId))
|
|
1179
1481
|
continue;
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1482
|
+
const rev = collection.recordCommitted(transaction.id, pendedRevs.get(collectionId));
|
|
1483
|
+
collection.applyCommittedToCache(collectionTransforms.get(collectionId), rev);
|
|
1484
|
+
collection.tracker.reset();
|
|
1485
|
+
collection.clearPendingActions();
|
|
1486
|
+
}
|
|
1487
|
+
for (const collectionId of batches.keys()) {
|
|
1488
|
+
const collection = this.collections.get(collectionId);
|
|
1489
|
+
if (!collection || committed.has(collectionId))
|
|
1490
|
+
continue;
|
|
1491
|
+
// NOTE: blind overwrite of this collection's staged state — anything
|
|
1492
|
+
// staged on it between the pre-stage snapshot above and here is discarded.
|
|
1493
|
+
// No sibling stamp can be that writer (at most one stamp is open), so the
|
|
1494
|
+
// only source is the untracked Tree.stage / Collection.act path, and this
|
|
1495
|
+
// is the same single-call-path assumption rollback() documents. It cannot
|
|
1496
|
+
// be softened by re-applying those actions here: collection.act takes the
|
|
1497
|
+
// same non-reentrant instance latch this span already holds.
|
|
1498
|
+
const snapshot = preStageSnapshots.get(collectionId);
|
|
1499
|
+
if (snapshot)
|
|
1500
|
+
await collection.restorePending(snapshot);
|
|
1198
1501
|
}
|
|
1199
1502
|
// The undo handle is now unsafe: rollback() is all-or-nothing and would rewind
|
|
1200
1503
|
// the collections that DID durably commit. Drop it, matching commitOnceLatched.
|
|
@@ -1256,9 +1559,10 @@ export class TransactionCoordinator {
|
|
|
1256
1559
|
/**
|
|
1257
1560
|
* Apply actions to a collection.
|
|
1258
1561
|
*
|
|
1259
|
-
* This runs the action handlers, writes to the log, and collects transforms.
|
|
1562
|
+
* This runs the action handlers, writes to the log, and collects transforms. The log entry is
|
|
1563
|
+
* written under `context`: its action id, its timestamp and its reads.
|
|
1260
1564
|
*/
|
|
1261
|
-
async applyActionsToCollection(collectionActions,
|
|
1565
|
+
async applyActionsToCollection(collectionActions, context, allCollectionIds) {
|
|
1262
1566
|
const collection = this.collections.get(collectionActions.collectionId);
|
|
1263
1567
|
if (!collection) {
|
|
1264
1568
|
return {
|
|
@@ -1271,8 +1575,7 @@ export class TransactionCoordinator {
|
|
|
1271
1575
|
// already has the transforms, and the actions are in the pending buffer.
|
|
1272
1576
|
// Get transforms from the collection's tracker
|
|
1273
1577
|
const transforms = collection.tracker.transforms;
|
|
1274
|
-
|
|
1275
|
-
const actionId = transaction.id;
|
|
1578
|
+
const actionId = context.actionId;
|
|
1276
1579
|
const newRev = collection.getNextRev();
|
|
1277
1580
|
// Write actions to the collection's log to get the log tail block ID. The append must be a
|
|
1278
1581
|
// fixed function of this write and the revision it requests, because a refused commit is
|
|
@@ -1314,7 +1617,7 @@ export class TransactionCoordinator {
|
|
|
1314
1617
|
// correctness; a cross-collection invalidation cascade that treats it as "the definitive
|
|
1315
1618
|
// participant set of this transaction" must union it across the transaction's entries
|
|
1316
1619
|
// rather than trusting any single one.
|
|
1317
|
-
const addResult = await log.addActions(collectionActions.actions, actionId, newRev, () => blockIdsForTransforms(transforms), { collectionIds: allCollectionIds, reads:
|
|
1620
|
+
const addResult = await log.addActions(collectionActions.actions, actionId, newRev, () => blockIdsForTransforms(transforms), { collectionIds: allCollectionIds, reads: [...context.reads], timestamp: context.timestamp });
|
|
1318
1621
|
// Return the transforms and log tail block ID
|
|
1319
1622
|
return {
|
|
1320
1623
|
success: true,
|
|
@@ -1325,9 +1628,11 @@ export class TransactionCoordinator {
|
|
|
1325
1628
|
};
|
|
1326
1629
|
}
|
|
1327
1630
|
/**
|
|
1328
|
-
* Coordinate a transaction across multiple collections
|
|
1631
|
+
* Coordinate a transaction's attempt across multiple collections: describe what each collection's
|
|
1632
|
+
* staged transforms send ({@link declareTouchedBlocks}), then run the phases
|
|
1633
|
+
* ({@link coordinateAttempts}).
|
|
1329
1634
|
*
|
|
1330
|
-
* @param
|
|
1635
|
+
* @param context - The write being coordinated (its action id and aged priority)
|
|
1331
1636
|
* @param validation - What every pend carries for members to re-check, or undefined for an
|
|
1332
1637
|
* unvalidated pend (see {@link pendValidationFor})
|
|
1333
1638
|
* @param collectionTransforms - Map of collectionId to its transforms
|
|
@@ -1336,24 +1641,33 @@ export class TransactionCoordinator {
|
|
|
1336
1641
|
* applyActionsToCollection) — repeated verbatim on the pend and commit requests so storage
|
|
1337
1642
|
* and the local record name the same number.
|
|
1338
1643
|
*/
|
|
1339
|
-
async coordinateTransaction(
|
|
1340
|
-
const trxId = transaction.id;
|
|
1341
|
-
const t0 = Date.now();
|
|
1644
|
+
async coordinateTransaction(context, validation, collectionTransforms, criticalBlocks, pendedRevs) {
|
|
1342
1645
|
// Before anything is sent: one materialization of every block the transaction touches, measured
|
|
1343
1646
|
// against MAX_BLOCK_BYTES here and declared on the commit below.
|
|
1344
|
-
const described = await this.declareTouchedBlocks(collectionTransforms);
|
|
1647
|
+
const described = await this.declareTouchedBlocks(collectionTransforms, criticalBlocks, pendedRevs);
|
|
1345
1648
|
if (!described.success) {
|
|
1346
|
-
log('trx:phases trxId=%s refused before sending: %s',
|
|
1649
|
+
log('trx:phases trxId=%s refused before sending: %s', context.actionId, described.error);
|
|
1347
1650
|
return described;
|
|
1348
1651
|
}
|
|
1652
|
+
return this.coordinateAttempts(context, validation, described.attempts);
|
|
1653
|
+
}
|
|
1654
|
+
/**
|
|
1655
|
+
* The distributed phases of one attempt — GATHER, PEND, COMMIT, and the CANCEL of what a failed
|
|
1656
|
+
* commit left pending — over every collection's share of it, already described
|
|
1657
|
+
* (`CollectionAttempt`). Run by {@link coordinateTransaction} for a transaction's attempt and by
|
|
1658
|
+
* {@link commitEntryOnce} for a queued entry's, whose attempts the head commit described.
|
|
1659
|
+
*/
|
|
1660
|
+
async coordinateAttempts(context, validation, attempts) {
|
|
1661
|
+
const trxId = context.actionId;
|
|
1662
|
+
const t0 = Date.now();
|
|
1349
1663
|
// 1. GATHER phase: collect critical cluster nominees (skip if single collection)
|
|
1350
|
-
const criticalBlockIds = Array.from(
|
|
1664
|
+
const criticalBlockIds = Array.from(attempts.values(), attempt => attempt.tailId);
|
|
1351
1665
|
const tGather = Date.now();
|
|
1352
1666
|
const superclusterNominees = await this.gatherPhase(criticalBlockIds);
|
|
1353
1667
|
const gatherMs = Date.now() - tGather;
|
|
1354
1668
|
// 2. PEND phase: distribute to all block clusters
|
|
1355
1669
|
const tPend = Date.now();
|
|
1356
|
-
const pendResult = await this.pendPhase(
|
|
1670
|
+
const pendResult = await this.pendPhase(context, validation, attempts, superclusterNominees);
|
|
1357
1671
|
const pendMs = Date.now() - tPend;
|
|
1358
1672
|
if (!pendResult.success) {
|
|
1359
1673
|
log('trx:phases trxId=%s gather=%dms pend=%dms (failed) total=%dms', trxId, gatherMs, pendMs, Date.now() - t0);
|
|
@@ -1361,11 +1675,11 @@ export class TransactionCoordinator {
|
|
|
1361
1675
|
}
|
|
1362
1676
|
// 3. COMMIT phase: commit to all critical blocks (with retry for forward recovery)
|
|
1363
1677
|
const tCommit = Date.now();
|
|
1364
|
-
const commitResult = await this.commitPhase(
|
|
1678
|
+
const commitResult = await this.commitPhase(context, attempts, pendResult.pendedBlockIds);
|
|
1365
1679
|
const commitMs = Date.now() - tCommit;
|
|
1366
1680
|
if (!commitResult.success) {
|
|
1367
1681
|
// Targeted cancel: only cancel collections that are still pending (not already committed)
|
|
1368
|
-
await this.cancelPhase(
|
|
1682
|
+
await this.cancelPhase(context.actionId, pendResult.pendedBlockIds, commitResult.committedCollections);
|
|
1369
1683
|
log('trx:phases trxId=%s gather=%dms pend=%dms commit=%dms (failed) total=%dms', trxId, gatherMs, pendMs, commitMs, Date.now() - t0);
|
|
1370
1684
|
// Surface the committed/failed partition so commit()/execute() can report which
|
|
1371
1685
|
// collections durably landed. A non-empty committedCollections is a PARTIAL commit:
|
|
@@ -1416,31 +1730,42 @@ export class TransactionCoordinator {
|
|
|
1416
1730
|
return new Set(byId.values());
|
|
1417
1731
|
}
|
|
1418
1732
|
/**
|
|
1419
|
-
*
|
|
1733
|
+
* What each collection's staged transforms send, described from its live tracker after the log
|
|
1734
|
+
* append: one local materialization of every block the transforms touch
|
|
1420
1735
|
* ({@link describeTouchedBlocks}), taken before anything is sent. A block the write would leave
|
|
1421
1736
|
* over `MAX_BLOCK_BYTES` refuses the transaction here, as a pend-phase failure carrying the
|
|
1422
1737
|
* {@link BlockTooLargeError} — the same shape as a pend too large to send, so `commit` throws it by
|
|
1423
1738
|
* name and `execute` reports it as a failure. Otherwise each collection's digests are kept as its
|
|
1424
|
-
* commit's declaration ({@link commitRequestFor}), so the commit does not materialize again
|
|
1739
|
+
* commit's declaration ({@link commitRequestFor}), so the commit does not materialize again, and
|
|
1740
|
+
* the base each update-only block's operations were computed against is read from the same
|
|
1741
|
+
* tracker's pins for its pend (`PendRequest.baseRevs`; storage keeps it with the pending record so
|
|
1742
|
+
* the record is never applied to a different version of the block).
|
|
1425
1743
|
*
|
|
1426
1744
|
* Purely local: each collection's tracker holds this transaction's staged transforms until the commit
|
|
1427
|
-
* succeeds, under the instance latch the commit span holds
|
|
1428
|
-
*
|
|
1429
|
-
* to report as it always has.
|
|
1745
|
+
* succeeds, under the instance latch the commit span holds. A collection missing from the registry
|
|
1746
|
+
* is a defect here: {@link applyActionsToCollection} has already refused it.
|
|
1430
1747
|
*/
|
|
1431
|
-
async declareTouchedBlocks(collectionTransforms) {
|
|
1432
|
-
const
|
|
1748
|
+
async declareTouchedBlocks(collectionTransforms, criticalBlocks, pendedRevs) {
|
|
1749
|
+
const attempts = new Map();
|
|
1433
1750
|
for (const [collectionId, transforms] of collectionTransforms) {
|
|
1434
1751
|
const collection = this.collections.get(collectionId);
|
|
1435
|
-
if (!collection)
|
|
1436
|
-
|
|
1437
|
-
|
|
1752
|
+
if (!collection) {
|
|
1753
|
+
throw new Error(`Collection not found: ${collectionId}`);
|
|
1754
|
+
}
|
|
1755
|
+
const blockIds = blockIdsForTransforms(transforms);
|
|
1756
|
+
const touched = await describeTouchedBlocks(collection.tracker, blockIds);
|
|
1438
1757
|
const tooLarge = oversizedBlock(touched.bytes);
|
|
1439
1758
|
if (tooLarge)
|
|
1440
1759
|
return pendFailure([tooLarge]);
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1760
|
+
attempts.set(collectionId, {
|
|
1761
|
+
transforms,
|
|
1762
|
+
tailId: criticalBlocks.get(collectionId),
|
|
1763
|
+
rev: pendedRevs.get(collectionId),
|
|
1764
|
+
baseRevs: collection.tracker.stagedBaseRevs(blockIds),
|
|
1765
|
+
blockDigests: touched.digests,
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
return { success: true, attempts };
|
|
1444
1769
|
}
|
|
1445
1770
|
/**
|
|
1446
1771
|
* PEND phase: Distribute transaction to all affected block clusters.
|
|
@@ -1448,26 +1773,25 @@ export class TransactionCoordinator {
|
|
|
1448
1773
|
* Two or more collections go as ONE request when the transactor can send them so
|
|
1449
1774
|
* ({@link pendTogether}); otherwise, and for one collection, each collection is pended on its own.
|
|
1450
1775
|
*
|
|
1451
|
-
* @param
|
|
1452
|
-
*
|
|
1776
|
+
* @param context - The write being pended (its action id names the action; its aged priority
|
|
1777
|
+
* rides on an unvalidated pend)
|
|
1453
1778
|
* @param validation - What every pend carries for members to re-check, or undefined for an
|
|
1454
1779
|
* unvalidated pend (see {@link pendValidationFor})
|
|
1455
|
-
* @param
|
|
1456
|
-
*
|
|
1457
|
-
*
|
|
1458
|
-
*
|
|
1780
|
+
* @param attempts - Per collection, its share of the attempt: its transforms, and the revision
|
|
1781
|
+
* its log entry was stamped with — the pend request repeats it verbatim rather than recomputing
|
|
1782
|
+
* from the collection (a recompute after the append could name a different number if the
|
|
1783
|
+
* collection refreshed in between).
|
|
1459
1784
|
* @param superclusterNominees - Nominees for multi-collection consensus (null for single-collection)
|
|
1460
1785
|
*/
|
|
1461
|
-
async pendPhase(
|
|
1462
|
-
if (
|
|
1786
|
+
async pendPhase(context, validation, attempts, superclusterNominees) {
|
|
1787
|
+
if (attempts.size === 0) {
|
|
1463
1788
|
return { success: false, error: 'No transforms to pend' };
|
|
1464
1789
|
}
|
|
1465
|
-
const actionId = transaction.id;
|
|
1466
1790
|
const nominees = superclusterNominees ? Array.from(superclusterNominees) : undefined;
|
|
1467
1791
|
// Both methods, never the pend alone: the batched pend's mark promises that the commit goes as one
|
|
1468
1792
|
// request too (`PendRequest.soleReservation`), which only `commitMany` can keep.
|
|
1469
|
-
if (
|
|
1470
|
-
const together = await this.pendTogether(
|
|
1793
|
+
if (attempts.size > 1 && this.transactor.pendMany && this.transactor.commitMany) {
|
|
1794
|
+
const together = await this.pendTogether(context, validation, attempts, nominees);
|
|
1471
1795
|
if (together !== undefined)
|
|
1472
1796
|
return together;
|
|
1473
1797
|
}
|
|
@@ -1475,13 +1799,13 @@ export class TransactionCoordinator {
|
|
|
1475
1799
|
// `Collection.sync` write does, so its pend may say so (`PendRequest.soleReservation`). Two or
|
|
1476
1800
|
// more pended one by one never may: each collection's commit can land on its own, and a late
|
|
1477
1801
|
// refusal of another collection's pend would surface after it.
|
|
1478
|
-
const sole =
|
|
1802
|
+
const sole = attempts.size === 1;
|
|
1479
1803
|
// Fan out the independent per-collection pends concurrently. Each settles to a
|
|
1480
1804
|
// { collectionId, blockIds } on success, or rejects with the per-collection reason.
|
|
1481
1805
|
// NOTE: unbounded fan-out — one concurrent coordinator round-trip per collection.
|
|
1482
1806
|
// Transactions touch few collections today; if one ever spans very many, bound this
|
|
1483
1807
|
// with a concurrency limiter so peak in-flight round-trips stays sane. Same for commitPhase.
|
|
1484
|
-
const outcomes = await Promise.allSettled(Array.from(
|
|
1808
|
+
const outcomes = await Promise.allSettled(Array.from(attempts.entries()).map(([collectionId, attempt]) => this.pendCollection(context, validation, collectionId, attempt, nominees, sole)));
|
|
1485
1809
|
// Partition settled results: every collection that DID pend (keyed with its block ids), and
|
|
1486
1810
|
// every reason one did not, classified together (pendFailure) independent of iteration order.
|
|
1487
1811
|
const pendedBlockIds = new Map();
|
|
@@ -1499,7 +1823,7 @@ export class TransactionCoordinator {
|
|
|
1499
1823
|
// have pended in parallel, so cancel EVERY successfully-pended collection — not
|
|
1500
1824
|
// only those started before the failure. Cancels are best-effort (cancelPhase
|
|
1501
1825
|
// swallows their errors) so they cannot mask the original pend failure.
|
|
1502
|
-
await this.cancelPhase(actionId, pendedBlockIds);
|
|
1826
|
+
await this.cancelPhase(context.actionId, pendedBlockIds);
|
|
1503
1827
|
return pendFailure(reasons);
|
|
1504
1828
|
}
|
|
1505
1829
|
return { success: true, pendedBlockIds };
|
|
@@ -1522,61 +1846,48 @@ export class TransactionCoordinator {
|
|
|
1522
1846
|
* pends each collection on its own with requests rebuilt WITHOUT the mark. The marked requests are
|
|
1523
1847
|
* never re-sent one by one: each carries every block of its collection, so `NetworkTransactor.pend`
|
|
1524
1848
|
* would keep the mark, and one collection's late refusal could then surface after a sibling's commit
|
|
1525
|
-
* landed — the tear the mark exists to prevent.
|
|
1526
|
-
* the registry, so that the per-collection path reports it as it always has.
|
|
1849
|
+
* landed — the tear the mark exists to prevent.
|
|
1527
1850
|
*/
|
|
1528
|
-
async pendTogether(
|
|
1529
|
-
const requests =
|
|
1530
|
-
|
|
1531
|
-
const collection = this.collections.get(collectionId);
|
|
1532
|
-
if (!collection)
|
|
1533
|
-
return undefined;
|
|
1534
|
-
requests.push(this.pendRequestFor(transaction, validation, collection, transforms, pendedRevs.get(collectionId), actionId, nominees, true));
|
|
1535
|
-
}
|
|
1536
|
-
const blockIds = new Map(Array.from(collectionTransforms, ([collectionId, transforms]) => [collectionId, blockIdsForTransforms(transforms)]));
|
|
1851
|
+
async pendTogether(context, validation, attempts, nominees) {
|
|
1852
|
+
const requests = Array.from(attempts.values(), attempt => this.pendRequestFor(context, validation, attempt, nominees, true));
|
|
1853
|
+
const blockIds = new Map(Array.from(attempts, ([collectionId, attempt]) => [collectionId, blockIdsForTransforms(attempt.transforms)]));
|
|
1537
1854
|
let result;
|
|
1538
1855
|
try {
|
|
1539
1856
|
result = await this.transactor.pendMany(requests);
|
|
1540
1857
|
}
|
|
1541
1858
|
catch (e) {
|
|
1542
|
-
await this.cancelPhase(actionId, blockIds);
|
|
1859
|
+
await this.cancelPhase(context.actionId, blockIds);
|
|
1543
1860
|
return pendFailure([e]);
|
|
1544
1861
|
}
|
|
1545
1862
|
if (result === undefined)
|
|
1546
1863
|
return undefined;
|
|
1547
1864
|
return result.success
|
|
1548
1865
|
? { success: true, pendedBlockIds: blockIds }
|
|
1549
|
-
: pendFailure([pendRefusalError(Array.from(
|
|
1866
|
+
: pendFailure([pendRefusalError(Array.from(attempts.keys()), result)]);
|
|
1550
1867
|
}
|
|
1551
1868
|
/**
|
|
1552
1869
|
* Pend a single collection's transforms. Resolves with the collection id and its
|
|
1553
1870
|
* pended block ids on success; throws with a per-collection reason on failure so the
|
|
1554
1871
|
* fan-out in {@link pendPhase} can settle it as a rejection.
|
|
1555
1872
|
*/
|
|
1556
|
-
async pendCollection(
|
|
1557
|
-
/**
|
|
1558
|
-
*
|
|
1559
|
-
*
|
|
1560
|
-
|
|
1873
|
+
async pendCollection(context, validation, collectionId,
|
|
1874
|
+
/** Its revision is the one the log entry was stamped with (threaded from the append), NOT
|
|
1875
|
+
* recomputed here: a `getNextRev()` after the append round trips could name a number a
|
|
1876
|
+
* concurrent refresh already moved past. */
|
|
1877
|
+
attempt, nominees,
|
|
1561
1878
|
/** Whether this is the transaction's only collection (see {@link pendPhase}). */
|
|
1562
1879
|
sole) {
|
|
1563
|
-
const
|
|
1564
|
-
if (!collection) {
|
|
1565
|
-
throw new Error(`Collection not found: ${collectionId}`);
|
|
1566
|
-
}
|
|
1567
|
-
const pendResult = await this.transactor.pend(this.pendRequestFor(transaction, validation, collection, transforms, rev, actionId, nominees, sole));
|
|
1880
|
+
const pendResult = await this.transactor.pend(this.pendRequestFor(context, validation, attempt, nominees, sole));
|
|
1568
1881
|
if (!pendResult.success) {
|
|
1569
1882
|
throw pendRefusalError([collectionId], pendResult);
|
|
1570
1883
|
}
|
|
1571
1884
|
return { collectionId, blockIds: pendResult.blockIds };
|
|
1572
1885
|
}
|
|
1573
1886
|
/**
|
|
1574
|
-
* The pend request for one collection's
|
|
1575
|
-
* goes on its own ({@link pendCollection}) or with its siblings ({@link pendTogether}).
|
|
1887
|
+
* The pend request for one collection's share of an attempt — the one place a pend request is built,
|
|
1888
|
+
* whether it goes on its own ({@link pendCollection}) or with its siblings ({@link pendTogether}).
|
|
1576
1889
|
*/
|
|
1577
|
-
pendRequestFor(
|
|
1578
|
-
/** The revision the log entry was stamped with (see {@link pendCollection}). */
|
|
1579
|
-
rev, actionId, nominees,
|
|
1890
|
+
pendRequestFor(context, validation, attempt, nominees,
|
|
1580
1891
|
/** Whether this request is the action's whole reservation, alone or with the siblings it is sent
|
|
1581
1892
|
* with (`PendRequest.soleReservation`). */
|
|
1582
1893
|
sole) {
|
|
@@ -1588,24 +1899,20 @@ export class TransactionCoordinator {
|
|
|
1588
1899
|
// never age. Omitted at 0, exactly as the single-collection path sends it
|
|
1589
1900
|
// (TransactorSource.transact), so the common first-attempt pend is indistinguishable from a
|
|
1590
1901
|
// Collection.sync pend.
|
|
1591
|
-
|
|
1592
|
-
//
|
|
1593
|
-
// tracker the commit's declaration was taken from (declareTouchedBlocks); it still holds this
|
|
1594
|
-
// transaction's staged transforms and pins. Storage keeps it with the pending record so the
|
|
1595
|
-
// record is never applied to a different version of the block (see PendRequest.baseRevs).
|
|
1596
|
-
// Omitted when empty.
|
|
1597
|
-
const baseRevs = collection.tracker.stagedBaseRevs(blockIdsForTransforms(transforms));
|
|
1902
|
+
// The bases (`PendRequest.baseRevs`) were read from the same tracker the commit's declaration was
|
|
1903
|
+
// taken from, when the attempt was described; omitted when empty.
|
|
1598
1904
|
return {
|
|
1599
|
-
actionId,
|
|
1600
|
-
rev,
|
|
1601
|
-
transforms,
|
|
1905
|
+
actionId: context.actionId,
|
|
1906
|
+
rev: attempt.rev,
|
|
1907
|
+
transforms: attempt.transforms,
|
|
1602
1908
|
policy: 'r', // Return policy: fail but return pending actions
|
|
1603
1909
|
...(validation === undefined
|
|
1604
|
-
? (priority > 0 ? { priority } : {})
|
|
1910
|
+
? (context.priority > 0 ? { priority: context.priority } : {})
|
|
1605
1911
|
: { validation }),
|
|
1606
|
-
...baseRevsField(baseRevs),
|
|
1912
|
+
...baseRevsField(attempt.baseRevs),
|
|
1607
1913
|
superclusterNominees: nominees,
|
|
1608
|
-
...(sole ? { soleReservation: true } : {})
|
|
1914
|
+
...(sole ? { soleReservation: true } : {}),
|
|
1915
|
+
...refuseSoloField(context.refuseSolo)
|
|
1609
1916
|
};
|
|
1610
1917
|
}
|
|
1611
1918
|
/**
|
|
@@ -1617,9 +1924,9 @@ export class TransactionCoordinator {
|
|
|
1617
1924
|
* retried (forward recovery) before giving up. Returns which collections committed vs failed so
|
|
1618
1925
|
* the caller can do targeted cancel.
|
|
1619
1926
|
*/
|
|
1620
|
-
async commitPhase(
|
|
1927
|
+
async commitPhase(context, attempts, pendedBlockIds) {
|
|
1621
1928
|
const together = pendedBlockIds.size > 1 && this.transactor.commitMany
|
|
1622
|
-
? await this.commitTogether(
|
|
1929
|
+
? await this.commitTogether(context, attempts, pendedBlockIds)
|
|
1623
1930
|
: undefined;
|
|
1624
1931
|
// NOTE: a fallback after a batched pend leaves the multi-collection residual in place: the
|
|
1625
1932
|
// pend's decision may have been held back for a commit that now goes per collection, so a member
|
|
@@ -1631,7 +1938,7 @@ export class TransactionCoordinator {
|
|
|
1631
1938
|
// cache's TTL is at least a minute, its size cap 1000 transactions), so a live lookup can split
|
|
1632
1939
|
// the blocks. If that ever shows up, pin the commit to the coordinator the batched pend answered
|
|
1633
1940
|
// from. See docs/correctness.md §2 Definitions.
|
|
1634
|
-
const outcomes = together ?? await this.commitEach(
|
|
1941
|
+
const outcomes = together ?? await this.commitEach(context, attempts, pendedBlockIds);
|
|
1635
1942
|
const committedCollections = new Map();
|
|
1636
1943
|
const failedCollections = new Set();
|
|
1637
1944
|
const errors = new Set();
|
|
@@ -1678,15 +1985,16 @@ export class TransactionCoordinator {
|
|
|
1678
1985
|
* land (`Collection.completeOwnEntry`) — the path a refused one-collection commit already takes.
|
|
1679
1986
|
*
|
|
1680
1987
|
* `undefined` when they could not go as one, when the request threw, or when a collection's request
|
|
1681
|
-
* could not be built (
|
|
1682
|
-
*
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1988
|
+
* could not be built (no log tail among its pended blocks): {@link commitPhase} then commits each
|
|
1989
|
+
* collection on its own, which re-sends everything and reports a request it cannot build as it
|
|
1990
|
+
* always has. The batch is never retried here: a returned refusal cannot be won by the identical
|
|
1991
|
+
* request, and a throw is the per-collection path's to retry.
|
|
1685
1992
|
*/
|
|
1686
|
-
async commitTogether(
|
|
1993
|
+
async commitTogether(context, attempts, pendedBlockIds) {
|
|
1994
|
+
const actionId = context.actionId;
|
|
1687
1995
|
const requests = [];
|
|
1688
1996
|
for (const [collectionId, blockIds] of pendedBlockIds) {
|
|
1689
|
-
const request = this.commitRequestFor(
|
|
1997
|
+
const request = this.commitRequestFor(context, collectionId, attempts.get(collectionId), blockIds);
|
|
1690
1998
|
if (typeof request === 'string')
|
|
1691
1999
|
return undefined;
|
|
1692
2000
|
requests.push({ collectionId, request });
|
|
@@ -1704,9 +2012,9 @@ export class TransactionCoordinator {
|
|
|
1704
2012
|
: requests.map(({ collectionId, request }) => collectionCommitOutcome(collectionId, request, result));
|
|
1705
2013
|
}
|
|
1706
2014
|
/** Commits each collection on its own, concurrently ({@link commitCollection}), one outcome per collection. */
|
|
1707
|
-
async commitEach(
|
|
2015
|
+
async commitEach(context, attempts, pendedBlockIds) {
|
|
1708
2016
|
const entries = Array.from(pendedBlockIds.entries());
|
|
1709
|
-
const settled = await Promise.allSettled(entries.map(([collectionId, blockIds]) => this.commitCollection(
|
|
2017
|
+
const settled = await Promise.allSettled(entries.map(([collectionId, blockIds]) => this.commitCollection(context, collectionId, attempts.get(collectionId), blockIds)));
|
|
1710
2018
|
// commitCollection resolves rather than rejects, but treat any unexpected rejection as a (hard)
|
|
1711
2019
|
// failure of its collection so the partitioned sets stay honest.
|
|
1712
2020
|
return settled.map((outcome, i) => outcome.status === 'fulfilled'
|
|
@@ -1719,10 +2027,10 @@ export class TransactionCoordinator {
|
|
|
1719
2027
|
* `committed` flag, with the commit's `durability` beside it — so the fan-out in
|
|
1720
2028
|
* {@link commitEach} can aggregate every result.
|
|
1721
2029
|
*/
|
|
1722
|
-
async commitCollection(
|
|
1723
|
-
/**
|
|
1724
|
-
|
|
1725
|
-
const commitRequest = this.commitRequestFor(
|
|
2030
|
+
async commitCollection(context, collectionId,
|
|
2031
|
+
/** Its revision is the one this collection PENDED at (see {@link commitRequestFor}). */
|
|
2032
|
+
attempt, blockIds) {
|
|
2033
|
+
const commitRequest = this.commitRequestFor(context, collectionId, attempt, blockIds);
|
|
1726
2034
|
if (typeof commitRequest === 'string') {
|
|
1727
2035
|
return { collectionId, committed: false, error: commitRequest };
|
|
1728
2036
|
}
|
|
@@ -1751,30 +2059,27 @@ export class TransactionCoordinator {
|
|
|
1751
2059
|
* whether it goes on its own ({@link commitCollection}) or with its siblings ({@link commitTogether})
|
|
1752
2060
|
* — or, as a string, why there is none.
|
|
1753
2061
|
*/
|
|
1754
|
-
commitRequestFor(
|
|
1755
|
-
/**
|
|
1756
|
-
* family as pendCollection's: recomputing `getNextRev()` here, after the pend round
|
|
1757
|
-
*
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
if (!
|
|
1761
|
-
return `Collection not found: ${collectionId}`;
|
|
1762
|
-
}
|
|
1763
|
-
// Find the critical block (log tail) for this collection
|
|
1764
|
-
const logTailBlockId = criticalBlockIds.find(blockId => blockIds.includes(blockId));
|
|
1765
|
-
if (!logTailBlockId) {
|
|
2062
|
+
commitRequestFor(context, collectionId,
|
|
2063
|
+
/** Its revision is the one this collection PENDED at, threaded from the log append — the same
|
|
2064
|
+
* bug family as pendCollection's: recomputing `getNextRev()` here, after the pend round trips,
|
|
2065
|
+
* could stamp the CommitRequest with a different number than the pend named. */
|
|
2066
|
+
attempt, blockIds) {
|
|
2067
|
+
// The critical block (log tail) for this collection must be among what the pend reserved.
|
|
2068
|
+
if (!blockIds.includes(attempt.tailId)) {
|
|
1766
2069
|
return `Log tail block not found for collection ${collectionId}`;
|
|
1767
2070
|
}
|
|
1768
2071
|
// Declare what each pended block will contain once committed: the description taken before the
|
|
1769
|
-
// pend (declareTouchedBlocks), over the blocks the pend
|
|
1770
|
-
// is omitted and falls back to corroboration on the
|
|
1771
|
-
// this — CoordinatorRepo.commit forwards without
|
|
2072
|
+
// pend (declareTouchedBlocks, or stageAttempt for a queued entry's), over the blocks the pend
|
|
2073
|
+
// carried. An id whose base was not cached is omitted and falls back to corroboration on the
|
|
2074
|
+
// member side. Only the client can declare this — CoordinatorRepo.commit forwards without
|
|
2075
|
+
// materializing.
|
|
1772
2076
|
return {
|
|
1773
|
-
actionId,
|
|
2077
|
+
actionId: context.actionId,
|
|
1774
2078
|
blockIds,
|
|
1775
|
-
tailId:
|
|
1776
|
-
rev,
|
|
1777
|
-
...blockDigestsField(
|
|
2079
|
+
tailId: attempt.tailId,
|
|
2080
|
+
rev: attempt.rev,
|
|
2081
|
+
...blockDigestsField(attempt.blockDigests),
|
|
2082
|
+
...refuseSoloField(context.refuseSolo)
|
|
1778
2083
|
};
|
|
1779
2084
|
}
|
|
1780
2085
|
/**
|