@optimystic/db-core 1.20.1 → 1.22.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/btree/btree.d.ts +36 -4
- package/dist/src/btree/btree.d.ts.map +1 -1
- package/dist/src/btree/btree.js +86 -18
- package/dist/src/btree/btree.js.map +1 -1
- package/dist/src/cohort-topic/gossip/bus.d.ts +15 -14
- package/dist/src/cohort-topic/gossip/bus.d.ts.map +1 -1
- package/dist/src/cohort-topic/gossip/bus.js +15 -16
- package/dist/src/cohort-topic/gossip/bus.js.map +1 -1
- package/dist/src/cohort-topic/ports.d.ts +30 -4
- package/dist/src/cohort-topic/ports.d.ts.map +1 -1
- package/dist/src/cohort-topic/ports.js +14 -0
- package/dist/src/cohort-topic/ports.js.map +1 -1
- package/dist/src/cohort-topic/registration/renewal.d.ts +27 -1
- package/dist/src/cohort-topic/registration/renewal.d.ts.map +1 -1
- package/dist/src/cohort-topic/registration/renewal.js +15 -4
- package/dist/src/cohort-topic/registration/renewal.js.map +1 -1
- package/dist/src/cohort-topic/service.d.ts +21 -4
- package/dist/src/cohort-topic/service.d.ts.map +1 -1
- package/dist/src/cohort-topic/service.js +208 -18
- package/dist/src/cohort-topic/service.js.map +1 -1
- package/dist/src/cohort-topic/willingness.d.ts +1 -1
- package/dist/src/cohort-topic/willingness.js +1 -1
- package/dist/src/cohort-topic/wire/codec.d.ts +3 -1
- package/dist/src/cohort-topic/wire/codec.d.ts.map +1 -1
- package/dist/src/cohort-topic/wire/codec.js +7 -1
- package/dist/src/cohort-topic/wire/codec.js.map +1 -1
- package/dist/src/cohort-topic/wire/index.d.ts +1 -1
- package/dist/src/cohort-topic/wire/index.d.ts.map +1 -1
- package/dist/src/cohort-topic/wire/index.js +1 -1
- package/dist/src/cohort-topic/wire/index.js.map +1 -1
- package/dist/src/cohort-topic/wire/types.d.ts +17 -1
- package/dist/src/cohort-topic/wire/types.d.ts.map +1 -1
- package/dist/src/cohort-topic/wire/validate.d.ts +15 -1
- package/dist/src/cohort-topic/wire/validate.d.ts.map +1 -1
- package/dist/src/cohort-topic/wire/validate.js +39 -0
- package/dist/src/cohort-topic/wire/validate.js.map +1 -1
- package/dist/src/collection/collection.d.ts +43 -5
- package/dist/src/collection/collection.d.ts.map +1 -1
- package/dist/src/collection/collection.js +60 -13
- package/dist/src/collection/collection.js.map +1 -1
- package/dist/src/collection/struct.d.ts +6 -0
- package/dist/src/collection/struct.d.ts.map +1 -1
- package/dist/src/collection/struct.js.map +1 -1
- package/dist/src/collections/tree/tree.d.ts +3 -1
- package/dist/src/collections/tree/tree.d.ts.map +1 -1
- package/dist/src/collections/tree/tree.js +11 -5
- package/dist/src/collections/tree/tree.js.map +1 -1
- package/dist/src/network/struct.d.ts +15 -0
- package/dist/src/network/struct.d.ts.map +1 -1
- package/dist/src/network/struct.js.map +1 -1
- package/dist/src/testing/test-transactor.js +6 -2
- package/dist/src/testing/test-transactor.js.map +1 -1
- package/dist/src/transaction/coordinator.d.ts +6 -0
- package/dist/src/transaction/coordinator.d.ts.map +1 -1
- package/dist/src/transaction/coordinator.js +6 -0
- package/dist/src/transaction/coordinator.js.map +1 -1
- package/dist/src/transactor/network-transactor.d.ts.map +1 -1
- package/dist/src/transactor/network-transactor.js +1 -0
- package/dist/src/transactor/network-transactor.js.map +1 -1
- package/package.json +1 -1
- package/src/btree/btree.ts +92 -18
- package/src/cohort-topic/gossip/bus.ts +25 -26
- package/src/cohort-topic/ports.ts +31 -4
- package/src/cohort-topic/registration/renewal.ts +40 -5
- package/src/cohort-topic/service.ts +251 -21
- package/src/cohort-topic/willingness.ts +1 -1
- package/src/cohort-topic/wire/codec.ts +12 -0
- package/src/cohort-topic/wire/index.ts +3 -0
- package/src/cohort-topic/wire/types.ts +20 -0
- package/src/cohort-topic/wire/validate.ts +45 -0
- package/src/collection/collection.ts +3098 -3039
- package/src/collection/struct.ts +463 -456
- package/src/collections/tree/tree.ts +15 -7
- package/src/network/struct.ts +770 -755
- package/src/testing/test-transactor.ts +5 -2
- package/src/transaction/coordinator.ts +6 -0
- package/src/transactor/network-transactor.ts +1897 -1896
package/src/collection/struct.ts
CHANGED
|
@@ -1,456 +1,463 @@
|
|
|
1
|
-
import type { IBlock, BlockId, Action, ActionId, Transforms, WriteDurability } from "../index.js";
|
|
2
|
-
import type { IChainHeader } from "../chain/chain-nodes.js";
|
|
3
|
-
import type { RandFn } from "../utility/backoff.js";
|
|
4
|
-
import type { EntryId } from "./segments.js";
|
|
5
|
-
import type { AlreadyAppliedTally } from "./action.js";
|
|
6
|
-
import type { BlockBaseRevs, BlockContentDigests } from "../network/struct.js";
|
|
7
|
-
|
|
8
|
-
export type CollectionId = BlockId;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A handler's statement that an action cannot be applied over the committed state it is being
|
|
12
|
-
* replayed against — a uniqueness or lost-update check failed. Not a transport failure and never
|
|
13
|
-
* retryable: the action's staged writes are discarded (the handler runs inside an Atomic), and
|
|
14
|
-
* nothing downgrades it to a stale failure. `TreeGuardRefusedError` in
|
|
15
|
-
* `packages/db-core/src/collections/tree/struct.ts` is the tree's subclass, and carries the
|
|
16
|
-
* contract every refusal obeys.
|
|
17
|
-
*
|
|
18
|
-
* Lives here rather than beside the tree so the collection can recognise a refusal without
|
|
19
|
-
* importing the tree module (which imports the collection). Segment-aware replay
|
|
20
|
-
* (`Collection.replayActions`) treats a throw of this class from a SEALED segment as that
|
|
21
|
-
* segment's refusal and keeps going; from the open segment it propagates as it always has.
|
|
22
|
-
*/
|
|
23
|
-
export class ActionRefusedError extends Error {
|
|
24
|
-
constructor(
|
|
25
|
-
/** The collection whose handler refused the action. */
|
|
26
|
-
readonly collectionId: CollectionId,
|
|
27
|
-
message: string,
|
|
28
|
-
) {
|
|
29
|
-
super(message);
|
|
30
|
-
this.name = 'ActionRefusedError';
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Why a sealed segment stood in the way of a commit (see {@link SealedSegmentsError}):
|
|
35
|
-
* - `whole-collection` — a whole-collection commit was asked for while a segment is sealed.
|
|
36
|
-
* - `not-head` — `commitHead` named a segment that is not the oldest sealed one.
|
|
37
|
-
* - `refused` — `commitHead` named a segment a replay has refused; the queue drops it instead.
|
|
38
|
-
* - `in-flight` — a head commit is on the network for the named segment, so neither a second
|
|
39
|
-
* `commitHead` nor a `dropSealed` of it can proceed. */
|
|
40
|
-
export type SealedSegmentsReason = 'whole-collection' | 'not-head' | 'refused' | 'in-flight';
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Thrown by the whole-collection commit paths — {@link ICollection.sync},
|
|
44
|
-
* {@link ICollection.updateAndSync}, `Collection.actAndSync` and `Collection.getPendingActions`
|
|
45
|
-
* (the coordinator's append) — while the collection holds sealed segments. Those paths commit
|
|
46
|
-
* everything staged as ONE log entry under one action id; over sealed segments that would merge
|
|
47
|
-
* several queue entries into one, which the queue's identity rules forbid
|
|
48
|
-
* (docs/offline-sync.md §Identity across attempts). A sealed segment is committed on its own,
|
|
49
|
-
* through `Collection.commitHead`, whose own preconditions throw this class too, with a
|
|
50
|
-
* {@link reason} other than `whole-collection` (see {@link SealedSegmentsReason}).
|
|
51
|
-
*
|
|
52
|
-
* Nothing is unstaged or changed when this is thrown: every segment, sealed and open, is exactly
|
|
53
|
-
* as it was.
|
|
54
|
-
*/
|
|
55
|
-
export class SealedSegmentsError extends Error {
|
|
56
|
-
constructor(
|
|
57
|
-
readonly collectionId: CollectionId,
|
|
58
|
-
/** The sealed segments' entry ids, in order. */
|
|
59
|
-
readonly sealedEntries: EntryId[],
|
|
60
|
-
readonly reason: SealedSegmentsReason = 'whole-collection',
|
|
61
|
-
/** The entry a `commitHead` or `dropSealed` named, for every reason but `whole-collection`. */
|
|
62
|
-
readonly entryId?: EntryId,
|
|
63
|
-
) {
|
|
64
|
-
super(sealedSegmentsMessage(collectionId, sealedEntries, reason, entryId));
|
|
65
|
-
this.name = 'SealedSegmentsError';
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function sealedSegmentsMessage(collectionId: CollectionId, sealedEntries: EntryId[], reason: SealedSegmentsReason, entryId: EntryId | undefined): string {
|
|
70
|
-
const listed = sealedEntries.length === 0 ? 'nothing' : sealedEntries.join(', ');
|
|
71
|
-
switch (reason) {
|
|
72
|
-
case 'whole-collection':
|
|
73
|
-
return `collection ${collectionId} holds sealed segment(s) ${listed}; `
|
|
74
|
-
+ `a whole-collection commit would merge them into one log entry — commit the head segment instead`;
|
|
75
|
-
case 'not-head':
|
|
76
|
-
return `collection ${collectionId}: entry ${entryId} is not the head sealed segment (sealed: ${listed}); `
|
|
77
|
-
+ `only the oldest sealed segment can be committed`;
|
|
78
|
-
case 'refused':
|
|
79
|
-
return `collection ${collectionId}: entry ${entryId} carries a recorded refusal; `
|
|
80
|
-
+ `a refused segment is dropped, never committed`;
|
|
81
|
-
case 'in-flight':
|
|
82
|
-
return `collection ${collectionId}: a head commit of entry ${entryId} is in flight on this instance`;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Exactly what one write attempt sent to the transactor — enough to send the same thing again.
|
|
87
|
-
* Retained by `Collection.retainInFlightAttempt` so that a refresh which then finds this attempt's
|
|
88
|
-
* own log entry can land the blocks the attempt left behind, at the same action id and revision
|
|
89
|
-
* (see `Collection.completeOwnEntry`).
|
|
90
|
-
*
|
|
91
|
-
* Also the shape a caller RECORDS before an attempt is sent and hands back on a later cycle
|
|
92
|
-
* ({@link HeadCommitOptions.recordAttempt} and {@link HeadCommitOptions.resume};
|
|
93
|
-
* docs/offline-sync.md §Identity across attempts), so it must stay plain data — ids, numbers and the
|
|
94
|
-
* transforms as the wire carries them, nothing with object identity and no functions: the
|
|
95
|
-
* offline-sync journal serialises it as JSON. */
|
|
96
|
-
export type InFlightAttempt = {
|
|
97
|
-
/** The action id the attempt was sent under — the one every attempt of the write reuses, and
|
|
98
|
-
* the one a cycle that resumes from this record must run under. */
|
|
99
|
-
actionId: ActionId;
|
|
100
|
-
/** The revision the attempt's log entry was stamped with, and pended and committed at. */
|
|
101
|
-
rev: number;
|
|
102
|
-
/** Every transform the attempt pended — the log blocks included. The caller hands over a copy
|
|
103
|
-
* it will not mutate; the collection keeps it as given. */
|
|
104
|
-
transforms: Transforms;
|
|
105
|
-
/** The attempt's log tail block, committed first. */
|
|
106
|
-
tailId: BlockId;
|
|
107
|
-
/** The per-block content declarations the attempt's commit carried, if any. */
|
|
108
|
-
blockDigests?: BlockContentDigests;
|
|
109
|
-
/** The per-block bases the attempt's pend carried, if any (see `PendRequest.baseRevs`). Kept
|
|
110
|
-
* with the transforms because they describe them: a re-send of these operations must name the
|
|
111
|
-
* same bases they were computed against. */
|
|
112
|
-
baseRevs?: BlockBaseRevs;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/** What `Collection.commitHead` commits the head segment under, beside the retry bounds of
|
|
116
|
-
* {@link SyncOptions}. Both fields are the queue's (docs/offline-sync.md §Identity across
|
|
117
|
-
* attempts): the collection mints neither. */
|
|
118
|
-
export type HeadCommitOptions = SyncOptions & {
|
|
119
|
-
/** The action id the attempt cycle runs under. The queue mints it, and re-supplies the SAME id
|
|
120
|
-
* when it re-drives an entry whose earlier attempt may have landed, so the refresh that opens the
|
|
121
|
-
* cycle recognises that attempt's log entry as the write's own and finishes or consumes it
|
|
122
|
-
* instead of committing a second copy. */
|
|
123
|
-
actionId: ActionId;
|
|
124
|
-
/** The entry's local commit time, stamped on the log entry in place of the time
|
|
125
|
-
* `Collection.syncInternal` mints for a sync. Fixed per write, so every attempt appends the same
|
|
126
|
-
* entry (see "A write's log append is a fixed function of the write and the revision it is
|
|
127
|
-
* requesting" in docs/internals.md). */
|
|
128
|
-
timestamp: number;
|
|
129
|
-
/** The write-ahead hook: called with each attempt exactly as it is about to be sent — after it is
|
|
130
|
-
* built and BEFORE the collection latch is released for the network — and awaited under that
|
|
131
|
-
* latch. It is where the queue records the attempt's identity before sending it
|
|
132
|
-
* (docs/offline-sync.md §Identity across attempts) and, later, where the journal writes it. A
|
|
133
|
-
* throw aborts the cycle before anything is sent: it escapes `commitHead` unchanged, the handle
|
|
134
|
-
* closes, and every segment is exactly as it was. */
|
|
135
|
-
recordAttempt?: (attempt: InFlightAttempt) => Promise<void>;
|
|
136
|
-
/** The attempt an earlier cycle under the SAME `actionId` handed to `recordAttempt` and did not
|
|
137
|
-
* resolve — it must carry this `actionId`, and `openHeadCommit` throws before touching anything
|
|
138
|
-
* when it does not. Seeded as the retained attempt right after the action is marked in flight,
|
|
139
|
-
* so the cycle's opening refresh, finding that attempt's log entry with blocks missing, finishes
|
|
140
|
-
* it from these transforms (`Collection.completeOwnEntry`) instead of refusing it as torn with
|
|
141
|
-
* `transforms-not-held`. Without it a re-drive can recognise only an attempt that landed whole.
|
|
142
|
-
* The revision check still applies: an attempt recorded at a revision other than the entry's is
|
|
143
|
-
* refused as before. And when that refresh finds nothing new, this attempt is what the cycle
|
|
144
|
-
* re-sends, verbatim, while the next attempt would request its revision (`resumedAttempt`), since
|
|
145
|
-
* it may have landed on machines the refresh did not hear from. */
|
|
146
|
-
resume?: InFlightAttempt;
|
|
147
|
-
/** Called once, with what the head segment's actions accepted as already applied
|
|
148
|
-
* ({@link AlreadyAppliedTally}, as recorded on the segment by its last staging over the committed
|
|
149
|
-
* state), at the moment the segment is made durable and dropped: when the committed attempt is
|
|
150
|
-
* folded (`HeadCommitHandle.fold`), or when the cycle's refresh finds the entry's own log entry and
|
|
151
|
-
* consumes the head (`Collection.consumeHead`) — so a re-drive that finds its earlier attempt landed
|
|
152
|
-
* reports it too. Runs synchronously under the collection latch, like `onSealedRefused`, so it must
|
|
153
|
-
* not call a latched method of the collection; the queue's closure only records into a map. A throw
|
|
154
|
-
* out of it is logged, never raised: the commit is already durable. A hook rather than a widened
|
|
155
|
-
* return type because the partial-commit path reports the committed half through
|
|
156
|
-
* `CoordinatorPartialCommitError` and the refresh-consumed path through `RefreshReport`; one hook
|
|
157
|
-
* reaches all three. */
|
|
158
|
-
onCommittedHead?: (applied: AlreadyAppliedTally) => void;
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
export type CollectionHeaderBlock = IBlock & Partial<IChainHeader>;
|
|
162
|
-
|
|
163
|
-
/** A collection's committed tail: the log tail block its header names, and that block's latest
|
|
164
|
-
* committed revision, which is the collection's (every commit touches the log tail). */
|
|
165
|
-
export type CommittedTail = { tailId: BlockId; rev: number };
|
|
166
|
-
|
|
167
|
-
/** One collection a batched committed-tail read names (`Collection.readCommittedTails`). */
|
|
168
|
-
export type CommittedTailQuery = {
|
|
169
|
-
readonly id: CollectionId;
|
|
170
|
-
/** The tail block the caller's previous read found. While the header still names it, the read
|
|
171
|
-
* needs no second request for this collection. */
|
|
172
|
-
readonly knownTailId?: BlockId;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
*
|
|
184
|
-
export
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
*
|
|
204
|
-
* the
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
*
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* {@link
|
|
221
|
-
*
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
*
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
* (
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
readonly
|
|
391
|
-
/** The
|
|
392
|
-
readonly
|
|
393
|
-
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* has
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
1
|
+
import type { IBlock, BlockId, Action, ActionId, Transforms, WriteDurability } from "../index.js";
|
|
2
|
+
import type { IChainHeader } from "../chain/chain-nodes.js";
|
|
3
|
+
import type { RandFn } from "../utility/backoff.js";
|
|
4
|
+
import type { EntryId } from "./segments.js";
|
|
5
|
+
import type { AlreadyAppliedTally } from "./action.js";
|
|
6
|
+
import type { BlockBaseRevs, BlockContentDigests } from "../network/struct.js";
|
|
7
|
+
|
|
8
|
+
export type CollectionId = BlockId;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A handler's statement that an action cannot be applied over the committed state it is being
|
|
12
|
+
* replayed against — a uniqueness or lost-update check failed. Not a transport failure and never
|
|
13
|
+
* retryable: the action's staged writes are discarded (the handler runs inside an Atomic), and
|
|
14
|
+
* nothing downgrades it to a stale failure. `TreeGuardRefusedError` in
|
|
15
|
+
* `packages/db-core/src/collections/tree/struct.ts` is the tree's subclass, and carries the
|
|
16
|
+
* contract every refusal obeys.
|
|
17
|
+
*
|
|
18
|
+
* Lives here rather than beside the tree so the collection can recognise a refusal without
|
|
19
|
+
* importing the tree module (which imports the collection). Segment-aware replay
|
|
20
|
+
* (`Collection.replayActions`) treats a throw of this class from a SEALED segment as that
|
|
21
|
+
* segment's refusal and keeps going; from the open segment it propagates as it always has.
|
|
22
|
+
*/
|
|
23
|
+
export class ActionRefusedError extends Error {
|
|
24
|
+
constructor(
|
|
25
|
+
/** The collection whose handler refused the action. */
|
|
26
|
+
readonly collectionId: CollectionId,
|
|
27
|
+
message: string,
|
|
28
|
+
) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'ActionRefusedError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Why a sealed segment stood in the way of a commit (see {@link SealedSegmentsError}):
|
|
35
|
+
* - `whole-collection` — a whole-collection commit was asked for while a segment is sealed.
|
|
36
|
+
* - `not-head` — `commitHead` named a segment that is not the oldest sealed one.
|
|
37
|
+
* - `refused` — `commitHead` named a segment a replay has refused; the queue drops it instead.
|
|
38
|
+
* - `in-flight` — a head commit is on the network for the named segment, so neither a second
|
|
39
|
+
* `commitHead` nor a `dropSealed` of it can proceed. */
|
|
40
|
+
export type SealedSegmentsReason = 'whole-collection' | 'not-head' | 'refused' | 'in-flight';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Thrown by the whole-collection commit paths — {@link ICollection.sync},
|
|
44
|
+
* {@link ICollection.updateAndSync}, `Collection.actAndSync` and `Collection.getPendingActions`
|
|
45
|
+
* (the coordinator's append) — while the collection holds sealed segments. Those paths commit
|
|
46
|
+
* everything staged as ONE log entry under one action id; over sealed segments that would merge
|
|
47
|
+
* several queue entries into one, which the queue's identity rules forbid
|
|
48
|
+
* (docs/offline-sync.md §Identity across attempts). A sealed segment is committed on its own,
|
|
49
|
+
* through `Collection.commitHead`, whose own preconditions throw this class too, with a
|
|
50
|
+
* {@link reason} other than `whole-collection` (see {@link SealedSegmentsReason}).
|
|
51
|
+
*
|
|
52
|
+
* Nothing is unstaged or changed when this is thrown: every segment, sealed and open, is exactly
|
|
53
|
+
* as it was.
|
|
54
|
+
*/
|
|
55
|
+
export class SealedSegmentsError extends Error {
|
|
56
|
+
constructor(
|
|
57
|
+
readonly collectionId: CollectionId,
|
|
58
|
+
/** The sealed segments' entry ids, in order. */
|
|
59
|
+
readonly sealedEntries: EntryId[],
|
|
60
|
+
readonly reason: SealedSegmentsReason = 'whole-collection',
|
|
61
|
+
/** The entry a `commitHead` or `dropSealed` named, for every reason but `whole-collection`. */
|
|
62
|
+
readonly entryId?: EntryId,
|
|
63
|
+
) {
|
|
64
|
+
super(sealedSegmentsMessage(collectionId, sealedEntries, reason, entryId));
|
|
65
|
+
this.name = 'SealedSegmentsError';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function sealedSegmentsMessage(collectionId: CollectionId, sealedEntries: EntryId[], reason: SealedSegmentsReason, entryId: EntryId | undefined): string {
|
|
70
|
+
const listed = sealedEntries.length === 0 ? 'nothing' : sealedEntries.join(', ');
|
|
71
|
+
switch (reason) {
|
|
72
|
+
case 'whole-collection':
|
|
73
|
+
return `collection ${collectionId} holds sealed segment(s) ${listed}; `
|
|
74
|
+
+ `a whole-collection commit would merge them into one log entry — commit the head segment instead`;
|
|
75
|
+
case 'not-head':
|
|
76
|
+
return `collection ${collectionId}: entry ${entryId} is not the head sealed segment (sealed: ${listed}); `
|
|
77
|
+
+ `only the oldest sealed segment can be committed`;
|
|
78
|
+
case 'refused':
|
|
79
|
+
return `collection ${collectionId}: entry ${entryId} carries a recorded refusal; `
|
|
80
|
+
+ `a refused segment is dropped, never committed`;
|
|
81
|
+
case 'in-flight':
|
|
82
|
+
return `collection ${collectionId}: a head commit of entry ${entryId} is in flight on this instance`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Exactly what one write attempt sent to the transactor — enough to send the same thing again.
|
|
87
|
+
* Retained by `Collection.retainInFlightAttempt` so that a refresh which then finds this attempt's
|
|
88
|
+
* own log entry can land the blocks the attempt left behind, at the same action id and revision
|
|
89
|
+
* (see `Collection.completeOwnEntry`).
|
|
90
|
+
*
|
|
91
|
+
* Also the shape a caller RECORDS before an attempt is sent and hands back on a later cycle
|
|
92
|
+
* ({@link HeadCommitOptions.recordAttempt} and {@link HeadCommitOptions.resume};
|
|
93
|
+
* docs/offline-sync.md §Identity across attempts), so it must stay plain data — ids, numbers and the
|
|
94
|
+
* transforms as the wire carries them, nothing with object identity and no functions: the
|
|
95
|
+
* offline-sync journal serialises it as JSON. */
|
|
96
|
+
export type InFlightAttempt = {
|
|
97
|
+
/** The action id the attempt was sent under — the one every attempt of the write reuses, and
|
|
98
|
+
* the one a cycle that resumes from this record must run under. */
|
|
99
|
+
actionId: ActionId;
|
|
100
|
+
/** The revision the attempt's log entry was stamped with, and pended and committed at. */
|
|
101
|
+
rev: number;
|
|
102
|
+
/** Every transform the attempt pended — the log blocks included. The caller hands over a copy
|
|
103
|
+
* it will not mutate; the collection keeps it as given. */
|
|
104
|
+
transforms: Transforms;
|
|
105
|
+
/** The attempt's log tail block, committed first. */
|
|
106
|
+
tailId: BlockId;
|
|
107
|
+
/** The per-block content declarations the attempt's commit carried, if any. */
|
|
108
|
+
blockDigests?: BlockContentDigests;
|
|
109
|
+
/** The per-block bases the attempt's pend carried, if any (see `PendRequest.baseRevs`). Kept
|
|
110
|
+
* with the transforms because they describe them: a re-send of these operations must name the
|
|
111
|
+
* same bases they were computed against. */
|
|
112
|
+
baseRevs?: BlockBaseRevs;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** What `Collection.commitHead` commits the head segment under, beside the retry bounds of
|
|
116
|
+
* {@link SyncOptions}. Both fields are the queue's (docs/offline-sync.md §Identity across
|
|
117
|
+
* attempts): the collection mints neither. */
|
|
118
|
+
export type HeadCommitOptions = SyncOptions & {
|
|
119
|
+
/** The action id the attempt cycle runs under. The queue mints it, and re-supplies the SAME id
|
|
120
|
+
* when it re-drives an entry whose earlier attempt may have landed, so the refresh that opens the
|
|
121
|
+
* cycle recognises that attempt's log entry as the write's own and finishes or consumes it
|
|
122
|
+
* instead of committing a second copy. */
|
|
123
|
+
actionId: ActionId;
|
|
124
|
+
/** The entry's local commit time, stamped on the log entry in place of the time
|
|
125
|
+
* `Collection.syncInternal` mints for a sync. Fixed per write, so every attempt appends the same
|
|
126
|
+
* entry (see "A write's log append is a fixed function of the write and the revision it is
|
|
127
|
+
* requesting" in docs/internals.md). */
|
|
128
|
+
timestamp: number;
|
|
129
|
+
/** The write-ahead hook: called with each attempt exactly as it is about to be sent — after it is
|
|
130
|
+
* built and BEFORE the collection latch is released for the network — and awaited under that
|
|
131
|
+
* latch. It is where the queue records the attempt's identity before sending it
|
|
132
|
+
* (docs/offline-sync.md §Identity across attempts) and, later, where the journal writes it. A
|
|
133
|
+
* throw aborts the cycle before anything is sent: it escapes `commitHead` unchanged, the handle
|
|
134
|
+
* closes, and every segment is exactly as it was. */
|
|
135
|
+
recordAttempt?: (attempt: InFlightAttempt) => Promise<void>;
|
|
136
|
+
/** The attempt an earlier cycle under the SAME `actionId` handed to `recordAttempt` and did not
|
|
137
|
+
* resolve — it must carry this `actionId`, and `openHeadCommit` throws before touching anything
|
|
138
|
+
* when it does not. Seeded as the retained attempt right after the action is marked in flight,
|
|
139
|
+
* so the cycle's opening refresh, finding that attempt's log entry with blocks missing, finishes
|
|
140
|
+
* it from these transforms (`Collection.completeOwnEntry`) instead of refusing it as torn with
|
|
141
|
+
* `transforms-not-held`. Without it a re-drive can recognise only an attempt that landed whole.
|
|
142
|
+
* The revision check still applies: an attempt recorded at a revision other than the entry's is
|
|
143
|
+
* refused as before. And when that refresh finds nothing new, this attempt is what the cycle
|
|
144
|
+
* re-sends, verbatim, while the next attempt would request its revision (`resumedAttempt`), since
|
|
145
|
+
* it may have landed on machines the refresh did not hear from. */
|
|
146
|
+
resume?: InFlightAttempt;
|
|
147
|
+
/** Called once, with what the head segment's actions accepted as already applied
|
|
148
|
+
* ({@link AlreadyAppliedTally}, as recorded on the segment by its last staging over the committed
|
|
149
|
+
* state), at the moment the segment is made durable and dropped: when the committed attempt is
|
|
150
|
+
* folded (`HeadCommitHandle.fold`), or when the cycle's refresh finds the entry's own log entry and
|
|
151
|
+
* consumes the head (`Collection.consumeHead`) — so a re-drive that finds its earlier attempt landed
|
|
152
|
+
* reports it too. Runs synchronously under the collection latch, like `onSealedRefused`, so it must
|
|
153
|
+
* not call a latched method of the collection; the queue's closure only records into a map. A throw
|
|
154
|
+
* out of it is logged, never raised: the commit is already durable. A hook rather than a widened
|
|
155
|
+
* return type because the partial-commit path reports the committed half through
|
|
156
|
+
* `CoordinatorPartialCommitError` and the refresh-consumed path through `RefreshReport`; one hook
|
|
157
|
+
* reaches all three. */
|
|
158
|
+
onCommittedHead?: (applied: AlreadyAppliedTally) => void;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export type CollectionHeaderBlock = IBlock & Partial<IChainHeader>;
|
|
162
|
+
|
|
163
|
+
/** A collection's committed tail: the log tail block its header names, and that block's latest
|
|
164
|
+
* committed revision, which is the collection's (every commit touches the log tail). */
|
|
165
|
+
export type CommittedTail = { tailId: BlockId; rev: number };
|
|
166
|
+
|
|
167
|
+
/** One collection a batched committed-tail read names (`Collection.readCommittedTails`). */
|
|
168
|
+
export type CommittedTailQuery = {
|
|
169
|
+
readonly id: CollectionId;
|
|
170
|
+
/** The tail block the caller's previous read found. While the header still names it, the read
|
|
171
|
+
* needs no second request for this collection. */
|
|
172
|
+
readonly knownTailId?: BlockId;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/** How a batched committed-tail read (`Collection.readCommittedTails`) asks. */
|
|
176
|
+
export type CommittedTailReadOptions = {
|
|
177
|
+
/** Ask every answering repo not to start the background read-repair consult its lazy window would
|
|
178
|
+
* run behind the read (`BlockGets.skipWindowConsult`) — for a poll that only wants what is held. */
|
|
179
|
+
readonly skipWindowConsult?: boolean;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** One collection's answer to a batched committed-tail read: its tail (`undefined` when it has
|
|
183
|
+
* never committed), or why reading it failed — what the single-collection read would have thrown. */
|
|
184
|
+
export type CommittedTailAnswer =
|
|
185
|
+
| { readonly tail: CommittedTail | undefined }
|
|
186
|
+
| { readonly error: unknown };
|
|
187
|
+
|
|
188
|
+
/** Bounds the retry loop inside {@link ICollection.sync} / {@link ICollection.updateAndSync}
|
|
189
|
+
* so a transactor that keeps rejecting the sync can no longer spin the collection latch forever.
|
|
190
|
+
* All fields are optional; unset fields fall back to conservative defaults. */
|
|
191
|
+
export interface SyncOptions {
|
|
192
|
+
/** Max consecutive stale-failure retries that make no progress before giving up.
|
|
193
|
+
* The counter resets to 0 on every successful commit, so a legitimate large multi-batch
|
|
194
|
+
* sync (which iterates many times making forward progress) is never falsely tripped.
|
|
195
|
+
* Default 10. */
|
|
196
|
+
maxAttempts?: number;
|
|
197
|
+
/** Optional wall-clock deadline in ms measured from the start of the sync call. Independent
|
|
198
|
+
* of the attempt count — a progress-agnostic ceiling. Unset means no deadline. */
|
|
199
|
+
deadlineMs?: number;
|
|
200
|
+
/** Base backoff delay in ms applied before the first retry; subsequent retries grow the delay
|
|
201
|
+
* exponentially up to {@link maxBackoffMs}. Default 100. Not applied once `SlotHoldAfterLosses` of the
|
|
202
|
+
* write's refused attempts carried a confirmed committed revision (`StaleFailure.staleAt`) and the
|
|
203
|
+
* latest refusal is one of them: a member then holds the next slot for this write,
|
|
204
|
+
* and the retry follows at once rather than spending the hold asleep. */
|
|
205
|
+
baseBackoffMs?: number;
|
|
206
|
+
/** Upper bound on any single backoff sleep, in ms. Default 5000. Like {@link baseBackoffMs}, not
|
|
207
|
+
* applied to a retry that follows at once because a member holds the slot for this write. */
|
|
208
|
+
maxBackoffMs?: number;
|
|
209
|
+
/** Optional abort signal. Checked at the top of each loop iteration and raced against the
|
|
210
|
+
* backoff sleep, so an aborted sync rejects promptly (with an AbortError) rather than finishing
|
|
211
|
+
* the current sleep. */
|
|
212
|
+
signal?: AbortSignal;
|
|
213
|
+
/** Advanced/testing hook: source of uniform [0,1) randomness for the backoff jitter. Defaults to
|
|
214
|
+
* the package CSPRNG; inject a deterministic sequence to assert exact retry delays. */
|
|
215
|
+
rand?: RandFn;
|
|
216
|
+
/** Consecutive refreshes that move this collection's revision NOWHERE while a responder has
|
|
217
|
+
* CONFIRMED a revision at or above the one the next attempt would request, before sync gives up
|
|
218
|
+
* with {@link SyncRevisionStalledError}. Such a retry provably re-sends the identical, already
|
|
219
|
+
* lost request, so the wait buys nothing. Two absorbs one transiently-lagging read; set it to
|
|
220
|
+
* {@link maxAttempts} or higher to restore the pre-existing behaviour of burning the whole
|
|
221
|
+
* budget. Default 2.
|
|
222
|
+
*
|
|
223
|
+
* Neither shape of progress trips this. Ordinary contention: the rival's commit is what the
|
|
224
|
+
* refresh adopts, so the next request lands above the confirmed revision. A collection still
|
|
225
|
+
* catching up: the refresh moves the revision forward without yet clearing the confirmed
|
|
226
|
+
* number, so the next request differs from the one that just failed. Either resets the counter.
|
|
227
|
+
* {@link deadlineMs} is still checked first, so a sync that is both past its deadline and
|
|
228
|
+
* stalled reports the deadline (as {@link SyncRetryExhaustedError}), not the stall. */
|
|
229
|
+
maxStalledAttempts?: number;
|
|
230
|
+
/** Refuse to let this write be committed by a lone node — a coordinator whose cohort for the
|
|
231
|
+
* write is only itself — while that node knows of members of the cohort it could not reach, is
|
|
232
|
+
* still waiting to hear from a peer it was configured to join through, or could not route the
|
|
233
|
+
* write at all. A node that is genuinely alone (no absent member known, nothing awaited, routed to
|
|
234
|
+
* itself) still commits. The refusal is a promise shortfall (`PromiseShortfallError`, with
|
|
235
|
+
* `soloRefused` naming the reason), so the write stops and is classified `cohort-unreachable`.
|
|
236
|
+
* Rides on every send of the write's attempt cycle as `PendRequest.refuseSolo` and
|
|
237
|
+
* `CommitRequest.refuseSolo`. Default off: only the offline queue sets it (docs/offline-sync.md
|
|
238
|
+
* §When to submit: the reachability gate). A coordinator on a build that predates the field
|
|
239
|
+
* ignores it and commits alone as before. */
|
|
240
|
+
refuseSoloCommit?: boolean;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** What a write's attempt cycle carries besides its identity, recorded beside the in-flight mark
|
|
244
|
+
* (`Collection.beginInFlightAction`) so that every send of the cycle carries it: each attempt, and
|
|
245
|
+
* the re-send a refresh makes to finish a torn one (`Collection.completeOwnEntry`). */
|
|
246
|
+
export type InFlightCycle = {
|
|
247
|
+
/** {@link SyncOptions.refuseSoloCommit}, for this cycle. */
|
|
248
|
+
readonly refuseSolo?: boolean;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/** What `Collection.openHeadCommit` opens a head commit with besides its identity: the cycle's
|
|
252
|
+
* {@link InFlightCycle}, the attempt an earlier cycle under the same action id recorded and did not
|
|
253
|
+
* resolve ({@link HeadCommitOptions.resume}), and the committed-head hook
|
|
254
|
+
* ({@link HeadCommitOptions.onCommittedHead}). */
|
|
255
|
+
export type OpenHeadCommitOptions = InFlightCycle & {
|
|
256
|
+
readonly resume?: InFlightAttempt;
|
|
257
|
+
readonly onCommittedHead?: (applied: AlreadyAppliedTally) => void;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/** Thrown by {@link ICollection.sync} / {@link ICollection.updateAndSync} when the retry budget
|
|
261
|
+
* (attempt count or deadline) is exhausted while the transactor keeps returning stale failures.
|
|
262
|
+
* Catchable so callers can surface a clear "gave up syncing" condition instead of hanging.
|
|
263
|
+
*
|
|
264
|
+
* It says the write was NOT SAVED; it does not prove nothing was stored. A write's log tail is
|
|
265
|
+
* committed before its other blocks, and the attempt that spent the budget is not followed by a
|
|
266
|
+
* refresh, so that attempt's log entry may be standing in the log with none of its data — the state
|
|
267
|
+
* {@link TornActionError} names when a refresh does get to see it. */
|
|
268
|
+
export class SyncRetryExhaustedError extends Error {
|
|
269
|
+
constructor(
|
|
270
|
+
readonly collectionId: CollectionId,
|
|
271
|
+
readonly attempts: number,
|
|
272
|
+
readonly lastReason?: string,
|
|
273
|
+
/** The last confirmed revision a responder reported holding, if any responder reported one
|
|
274
|
+
* (see `StaleFailure.staleAt`). Absent whenever no rejection carried a confirmed number —
|
|
275
|
+
* which is normal, not a signal that the failure was something other than a lost race. */
|
|
276
|
+
readonly staleAt?: { blockId: BlockId; rev: number },
|
|
277
|
+
) {
|
|
278
|
+
super(`sync for collection ${collectionId} exhausted ${attempts} retries` +
|
|
279
|
+
(lastReason ? `: ${lastReason}` : '') +
|
|
280
|
+
(staleAt ? `, last seen block ${staleAt.blockId} at rev ${staleAt.rev}` : ''));
|
|
281
|
+
this.name = 'SyncRetryExhaustedError';
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Thrown by {@link ICollection.sync} / {@link ICollection.updateAndSync} when refreshing
|
|
286
|
+
* repeatedly moved this collection nowhere at all while a responder confirmed a revision at or
|
|
287
|
+
* above the one being requested — the client's view of the current revision disagrees with the
|
|
288
|
+
* cluster's, and retrying would re-send the identical taken number.
|
|
289
|
+
*
|
|
290
|
+
* The distinction matters because the two failures need different responses. Plain exhaustion
|
|
291
|
+
* means "I lost a race too many times", and waiting longer or retrying later can succeed. This
|
|
292
|
+
* one means the next attempt is provably identical to the one that just failed, so the remaining
|
|
293
|
+
* budget buys nothing — the caller's view of the collection has to be repaired first.
|
|
294
|
+
*
|
|
295
|
+
* Sync deliberately does NOT adopt the responder's revision to get past this. `staleAt` is a bare
|
|
296
|
+
* number, not content: submitting this client's staged transforms at a revision built on a
|
|
297
|
+
* history it never read would overwrite that history silently. Reconciling a genuine fork is
|
|
298
|
+
* partition healing's job (docs/transactions.md), not the retry loop's.
|
|
299
|
+
*
|
|
300
|
+
* Extends {@link SyncRetryExhaustedError} so existing callers that catch the base class keep
|
|
301
|
+
* working; catch this subclass to distinguish "my revision view is wrong" from "I lost a race
|
|
302
|
+
* too many times". */
|
|
303
|
+
export class SyncRevisionStalledError extends SyncRetryExhaustedError {
|
|
304
|
+
/** Required here, unlike on the base class — it is the evidence the stall is based on. */
|
|
305
|
+
declare readonly staleAt: { blockId: BlockId; rev: number };
|
|
306
|
+
|
|
307
|
+
constructor(
|
|
308
|
+
collectionId: CollectionId,
|
|
309
|
+
attempts: number,
|
|
310
|
+
staleAt: { blockId: BlockId; rev: number },
|
|
311
|
+
/** The revision the next attempt would have requested. */
|
|
312
|
+
readonly requestedRev: number,
|
|
313
|
+
/** The revision this client believes is current. `undefined` for a collection that has
|
|
314
|
+
* committed nothing. */
|
|
315
|
+
readonly heldRev: number | undefined,
|
|
316
|
+
lastReason?: string,
|
|
317
|
+
) {
|
|
318
|
+
super(collectionId, attempts, lastReason, staleAt);
|
|
319
|
+
// The base class's "exhausted N retries" wording is deliberately NOT reused: it reads as
|
|
320
|
+
// ordinary contention, which is exactly the misdiagnosis this class exists to prevent.
|
|
321
|
+
this.message = `sync for collection ${collectionId} stopped after ${attempts} attempts: `
|
|
322
|
+
+ `this client holds rev ${heldRev ?? 'none'} and would request rev ${requestedRev}, `
|
|
323
|
+
+ `but block ${staleAt.blockId} is confirmed committed at rev ${staleAt.rev} and `
|
|
324
|
+
+ `refreshing did not close the gap`
|
|
325
|
+
+ (lastReason ? `: ${lastReason}` : '');
|
|
326
|
+
this.name = 'SyncRevisionStalledError';
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Why a half-landed write could not be finished (see {@link TornActionError}). This is the CAUSE
|
|
331
|
+
* of giving up; whether the write is then definitely not saved is a separate fact, carried by
|
|
332
|
+
* {@link TornActionError.final} — every reason can arrive with either value.
|
|
333
|
+
*
|
|
334
|
+
* - `rival-holds-revision` — finishing was refused because a DIFFERENT action holds a revision past
|
|
335
|
+
* the one the write needed on some block. That alone does NOT mean the write is lost: a later
|
|
336
|
+
* revision is often built ON the write (a rival that read it and added to it), and a write every
|
|
337
|
+
* block of which was built upon is reported SAVED, never by this error. It is raised only once
|
|
338
|
+
* the blocks' own history was asked (`ITransactor.getLineage`) and did not vouch for every block.
|
|
339
|
+
* - `completion-refused` — finishing was attempted and refused for a cause that can clear on its
|
|
340
|
+
* own (another write in flight on the block, the revision not yet held by a majority). The write
|
|
341
|
+
* paths retry this inside their own budget, so seeing it means that budget ran out.
|
|
342
|
+
* - `transforms-not-held` — the write's own log entry was found with blocks still missing, but this
|
|
343
|
+
* collection no longer holds the transforms that would finish them. */
|
|
344
|
+
export type TornActionReason = 'rival-holds-revision' | 'completion-refused' | 'transforms-not-held';
|
|
345
|
+
|
|
346
|
+
/** Thrown when a write HALF-LANDED and was not finished: its log entry is stored, but at least one
|
|
347
|
+
* of the other blocks that entry names is not known to hold the write.
|
|
348
|
+
*
|
|
349
|
+
* A write's log tail is committed before the rest of its blocks, so a write can be refused AFTER the
|
|
350
|
+
* tail was stored (see `NetworkTransactor.commit`). The writer's retry then finds its own log entry.
|
|
351
|
+
* That entry proves only that the tail landed; the write is saved only once EVERY block the entry
|
|
352
|
+
* names holds the write — at the write's own revision, or at a later revision that was built from
|
|
353
|
+
* it — so the retry finishes the remaining blocks at the same action id and revision (see
|
|
354
|
+
* `Collection.completeOwnEntry`). This error is what it raises when it could not — {@link reason}
|
|
355
|
+
* says why, and {@link final} says what that leaves the caller able to do.
|
|
356
|
+
*
|
|
357
|
+
* **{@link final} is the field to act on.** An application's only safe reaction to a failed write
|
|
358
|
+
* is to submit it again, and that is only safe when the first one can never show up:
|
|
359
|
+
*
|
|
360
|
+
* - `final: true` — the write is NOT saved and never will be. Every block it still needed answered
|
|
361
|
+
* for its whole cohort that it does not hold the write (its history passed the write's revision
|
|
362
|
+
* without it, or it never reached that revision), and the write's pending records were confirmed
|
|
363
|
+
* cancelled first, so nothing is left that could land it. Submitting the same change again stores
|
|
364
|
+
* it once.
|
|
365
|
+
* - `final: false` — that could NOT be established. The write may already be saved, or may still
|
|
366
|
+
* land (a block's cohort did not all answer, members contradicted each other, fewer than a
|
|
367
|
+
* majority hold it, or the cancel could not be confirmed). Submitting again can store the change
|
|
368
|
+
* twice; read the data back first.
|
|
369
|
+
*
|
|
370
|
+
* It is deliberately not a {@link SyncRetryExhaustedError}: that error says the write never landed,
|
|
371
|
+
* whereas here the log already holds an entry for it. Finishing needs the original action id and
|
|
372
|
+
* the transforms it sent, and both are gone once this escapes. Re-driving under a NEW revision
|
|
373
|
+
* records the same actions in the log twice, so nothing here does that on the caller's behalf. The
|
|
374
|
+
* staged actions are left in place on the collection, so at this level "submit again" is calling
|
|
375
|
+
* `sync()` again, and abandoning the write is the caller's to do (`Collection.restorePending`).
|
|
376
|
+
* `Tree.replace` and `Diary.append` do abandon it — they own both the staging and the flush, and a
|
|
377
|
+
* failed call that left its action staged would ride along, unasked, with the caller's next write.
|
|
378
|
+
*
|
|
379
|
+
* Raised out of {@link ICollection.sync} / {@link ICollection.updateAndSync}, and out of the
|
|
380
|
+
* refresh `TransactionCoordinator.commit` runs between attempts (`Collection.refreshInFlight`).
|
|
381
|
+
* The coordinator passes it on bare only when no other participant of the commit is saved; when
|
|
382
|
+
* one is, it arrives as the `reason` of a `CoordinatorPartialCommitError` that names the saved
|
|
383
|
+
* participants. */
|
|
384
|
+
export class TornActionError extends Error {
|
|
385
|
+
constructor(
|
|
386
|
+
readonly collectionId: CollectionId,
|
|
387
|
+
/** The half-landed write's action id. */
|
|
388
|
+
readonly actionId: string,
|
|
389
|
+
/** The revision its log entry landed at — the revision every block it names had to take. */
|
|
390
|
+
readonly rev: number,
|
|
391
|
+
/** The blocks the entry names that are not known to hold the write. */
|
|
392
|
+
readonly blockIds: BlockId[],
|
|
393
|
+
/** Why finishing failed — see {@link TornActionReason}. Says nothing about {@link final}. */
|
|
394
|
+
readonly reason: TornActionReason,
|
|
395
|
+
/** Whether the write is definitely not saved and can never land — see the class comment.
|
|
396
|
+
* `false` means "not established", never "it landed". */
|
|
397
|
+
readonly final: boolean,
|
|
398
|
+
/** The refusal in the responder's own words, for a log line. Never branch on it. */
|
|
399
|
+
readonly detail: string,
|
|
400
|
+
/** The confirmed revision a responder reported holding under ANOTHER action, when the refusal
|
|
401
|
+
* carried one (see `StaleFailure.staleAt`). */
|
|
402
|
+
readonly staleAt?: { blockId: BlockId; rev: number },
|
|
403
|
+
) {
|
|
404
|
+
super(`collection ${collectionId}: action ${actionId} is torn at rev ${rev} — its log entry is stored `
|
|
405
|
+
+ `but block(s) ${blockIds.join(', ') || '(unknown)'} are not known to hold it, and the write cannot be `
|
|
406
|
+
+ `finished: ${detail}`
|
|
407
|
+
+ (staleAt ? ` (block ${staleAt.blockId} is at rev ${staleAt.rev})` : '')
|
|
408
|
+
+ (final
|
|
409
|
+
? ' — the write is not saved and cannot land; it is safe to submit again'
|
|
410
|
+
: ' — whether the write is saved could not be established; it may be saved already or land later'));
|
|
411
|
+
this.name = 'TornActionError';
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Thrown when a collection that already holds a committed revision reads its own header
|
|
416
|
+
* block as authoritatively absent.
|
|
417
|
+
*
|
|
418
|
+
* The two facts contradict each other: this client has proof that something was committed
|
|
419
|
+
* under this id (it holds the revision it committed at, or the one it read off the log tail),
|
|
420
|
+
* and storage has just answered that nothing ever was. Exactly one of them is wrong, so this
|
|
421
|
+
* is a fault rather than an absence — the same reasoning `Collection.attachToLog` applies to a
|
|
422
|
+
* header that probes fine but whose log will not open.
|
|
423
|
+
*
|
|
424
|
+
* Deliberately NOT a `StaleFailure`: {@link ICollection.sync}'s retry loop only absorbs
|
|
425
|
+
* returned stale failures, so throwing this aborts the sync immediately with a named
|
|
426
|
+
* diagnosis instead of letting it spin the full retry budget re-requesting a revision it
|
|
427
|
+
* has silently forgotten.
|
|
428
|
+
*
|
|
429
|
+
* NOTE: durable invalidation restores reverted content to its as-if-absent state, so once the
|
|
430
|
+
* cascade runs end-to-end (docs/right-is-right.md § Durable Invalidation), reverting the commit
|
|
431
|
+
* that CREATED a collection would make its header legitimately absent for a client still holding
|
|
432
|
+
* that revision — a reversal, not a contradiction, which this message would misdiagnose.
|
|
433
|
+
* Aborting is still the right action there; if it ever fires for that reason, distinguish the
|
|
434
|
+
* two by checking the log for an invalidation of the held revision before wording the error. */
|
|
435
|
+
export class CollectionHeaderVanishedError extends Error {
|
|
436
|
+
constructor(
|
|
437
|
+
readonly collectionId: CollectionId,
|
|
438
|
+
/** The committed revision this collection held when the header read came back absent. */
|
|
439
|
+
readonly heldRev: number,
|
|
440
|
+
) {
|
|
441
|
+
super(`collection ${collectionId} holds committed revision ${heldRev}, but its header block `
|
|
442
|
+
+ `read as absent — storage reported that nothing was ever committed under this id`);
|
|
443
|
+
this.name = 'CollectionHeaderVanishedError';
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export interface ICollection<TAction> {
|
|
448
|
+
readonly id: CollectionId;
|
|
449
|
+
act(...actions: Action<TAction>[]): Promise<void>;
|
|
450
|
+
update(): Promise<void>;
|
|
451
|
+
/** Push staged changes. Resolves to who holds what was committed, or `undefined` when NOTHING WAS
|
|
452
|
+
* WRITTEN — nothing was staged, so there was no pend and no commit and there is no durability to
|
|
453
|
+
* report. A write that never landed throws ({@link SyncRetryExhaustedError}) rather than returning
|
|
454
|
+
* a class. Decide "is this saved" with `isFullyDurable`, never by comparing `quorum`. */
|
|
455
|
+
sync(options?: SyncOptions): Promise<WriteDurability | undefined>;
|
|
456
|
+
/** Refresh, then {@link sync}. Same return contract. */
|
|
457
|
+
updateAndSync(options?: SyncOptions): Promise<WriteDurability | undefined>;
|
|
458
|
+
selectLog(forward?: boolean): AsyncIterableIterator<Action<TAction>>;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export type CreateCollectionAction = Action<void> & {
|
|
462
|
+
type: "create";
|
|
463
|
+
};
|