@optimystic/db-core 1.22.0 → 1.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/dist/src/collection/action.d.ts +7 -4
- package/dist/src/collection/action.d.ts.map +1 -1
- package/dist/src/collection/action.js +13 -5
- package/dist/src/collection/action.js.map +1 -1
- package/dist/src/collection/collection.d.ts +134 -46
- package/dist/src/collection/collection.d.ts.map +1 -1
- package/dist/src/collection/collection.js +207 -70
- package/dist/src/collection/collection.js.map +1 -1
- package/dist/src/collection/struct.d.ts +38 -8
- package/dist/src/collection/struct.d.ts.map +1 -1
- package/dist/src/collection/struct.js.map +1 -1
- package/dist/src/collection/write-attempts.d.ts +138 -34
- package/dist/src/collection/write-attempts.d.ts.map +1 -1
- package/dist/src/collection/write-attempts.js +168 -101
- package/dist/src/collection/write-attempts.js.map +1 -1
- package/dist/src/collections/diary/diary.d.ts.map +1 -1
- package/dist/src/collections/diary/diary.js +5 -1
- package/dist/src/collections/diary/diary.js.map +1 -1
- package/dist/src/collections/tree/tree.d.ts +15 -9
- package/dist/src/collections/tree/tree.d.ts.map +1 -1
- package/dist/src/collections/tree/tree.js +6 -4
- package/dist/src/collections/tree/tree.js.map +1 -1
- package/dist/src/hlc/clock-store.d.ts +63 -0
- package/dist/src/hlc/clock-store.d.ts.map +1 -0
- package/dist/src/hlc/clock-store.js +49 -0
- package/dist/src/hlc/clock-store.js.map +1 -0
- package/dist/src/hlc/clock.d.ts +64 -0
- package/dist/src/hlc/clock.d.ts.map +1 -0
- package/dist/src/hlc/clock.js +171 -0
- package/dist/src/hlc/clock.js.map +1 -0
- package/dist/src/hlc/hlc.d.ts +38 -0
- package/dist/src/hlc/hlc.d.ts.map +1 -0
- package/dist/src/hlc/hlc.js +41 -0
- package/dist/src/hlc/hlc.js.map +1 -0
- package/dist/src/hlc/index.d.ts +5 -0
- package/dist/src/hlc/index.d.ts.map +1 -0
- package/dist/src/hlc/index.js +5 -0
- package/dist/src/hlc/index.js.map +1 -0
- package/dist/src/hlc/skew.d.ts +35 -0
- package/dist/src/hlc/skew.d.ts.map +1 -0
- package/dist/src/hlc/skew.js +35 -0
- package/dist/src/hlc/skew.js.map +1 -0
- 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/lane/index.d.ts +2 -0
- package/dist/src/lane/index.d.ts.map +1 -0
- package/dist/src/lane/index.js +2 -0
- package/dist/src/lane/index.js.map +1 -0
- package/dist/src/lane/lane.d.ts +46 -0
- package/dist/src/lane/lane.d.ts.map +1 -0
- package/dist/src/lane/lane.js +26 -0
- package/dist/src/lane/lane.js.map +1 -0
- package/dist/src/log/log.d.ts +50 -22
- package/dist/src/log/log.d.ts.map +1 -1
- package/dist/src/log/log.js +21 -13
- package/dist/src/log/log.js.map +1 -1
- package/dist/src/log/struct.d.ts +61 -1
- package/dist/src/log/struct.d.ts.map +1 -1
- package/dist/src/log/struct.js +24 -0
- package/dist/src/log/struct.js.map +1 -1
- package/dist/src/network/i-repo.d.ts +10 -0
- package/dist/src/network/i-repo.d.ts.map +1 -1
- package/dist/src/network/stale-failure.d.ts +20 -7
- package/dist/src/network/stale-failure.d.ts.map +1 -1
- package/dist/src/network/stale-failure.js +29 -7
- package/dist/src/network/stale-failure.js.map +1 -1
- package/dist/src/network/struct.d.ts +35 -0
- package/dist/src/network/struct.d.ts.map +1 -1
- package/dist/src/network/struct.js.map +1 -1
- package/dist/src/offline/entry.d.ts +14 -0
- package/dist/src/offline/entry.d.ts.map +1 -1
- package/dist/src/offline/entry.js +2 -0
- package/dist/src/offline/entry.js.map +1 -1
- package/dist/src/offline/journal.d.ts +10 -0
- package/dist/src/offline/journal.d.ts.map +1 -1
- package/dist/src/offline/queue.d.ts +10 -0
- package/dist/src/offline/queue.d.ts.map +1 -1
- package/dist/src/offline/queue.js +29 -3
- package/dist/src/offline/queue.js.map +1 -1
- package/dist/src/offline/status.d.ts +7 -0
- package/dist/src/offline/status.d.ts.map +1 -1
- package/dist/src/offline/status.js.map +1 -1
- package/dist/src/testing/test-transactor.d.ts +9 -1
- package/dist/src/testing/test-transactor.d.ts.map +1 -1
- package/dist/src/testing/test-transactor.js +6 -1
- package/dist/src/testing/test-transactor.js.map +1 -1
- package/dist/src/transaction/actions-engine.d.ts +3 -0
- package/dist/src/transaction/actions-engine.d.ts.map +1 -1
- package/dist/src/transaction/actions-engine.js +3 -0
- package/dist/src/transaction/actions-engine.js.map +1 -1
- package/dist/src/transaction/coordinator.d.ts +10 -5
- package/dist/src/transaction/coordinator.d.ts.map +1 -1
- package/dist/src/transaction/coordinator.js +197 -48
- package/dist/src/transaction/coordinator.js.map +1 -1
- package/dist/src/transaction/entry-commit.d.ts +45 -10
- package/dist/src/transaction/entry-commit.d.ts.map +1 -1
- package/dist/src/transaction/entry-commit.js +20 -7
- package/dist/src/transaction/entry-commit.js.map +1 -1
- package/dist/src/transaction/errors.d.ts +69 -1
- package/dist/src/transaction/errors.d.ts.map +1 -1
- package/dist/src/transaction/errors.js +70 -1
- package/dist/src/transaction/errors.js.map +1 -1
- package/dist/src/transaction/index.d.ts +1 -1
- package/dist/src/transaction/index.d.ts.map +1 -1
- package/dist/src/transaction/index.js +1 -1
- package/dist/src/transaction/index.js.map +1 -1
- package/dist/src/transaction/session.d.ts.map +1 -1
- package/dist/src/transaction/session.js +18 -5
- package/dist/src/transaction/session.js.map +1 -1
- package/dist/src/transaction/transaction.d.ts +74 -15
- package/dist/src/transaction/transaction.d.ts.map +1 -1
- package/dist/src/transaction/transaction.js +35 -15
- package/dist/src/transaction/transaction.js.map +1 -1
- package/dist/src/transaction/validator.d.ts +25 -4
- package/dist/src/transaction/validator.d.ts.map +1 -1
- package/dist/src/transaction/validator.js +45 -5
- package/dist/src/transaction/validator.js.map +1 -1
- package/dist/src/transactor/block-floors.d.ts +12 -1
- package/dist/src/transactor/block-floors.d.ts.map +1 -1
- package/dist/src/transactor/block-floors.js +14 -1
- package/dist/src/transactor/block-floors.js.map +1 -1
- package/dist/src/transactor/network-transactor.d.ts +8 -0
- package/dist/src/transactor/network-transactor.d.ts.map +1 -1
- package/dist/src/transactor/network-transactor.js +8 -4
- package/dist/src/transactor/network-transactor.js.map +1 -1
- package/dist/src/transactor/transactor-source.d.ts +11 -0
- package/dist/src/transactor/transactor-source.d.ts.map +1 -1
- package/dist/src/transactor/transactor-source.js +5 -1
- package/dist/src/transactor/transactor-source.js.map +1 -1
- package/dist/src/transactor/transactor.d.ts +8 -0
- package/dist/src/transactor/transactor.d.ts.map +1 -1
- package/dist/src/transform/cache-source.d.ts +7 -0
- package/dist/src/transform/cache-source.d.ts.map +1 -1
- package/dist/src/transform/cache-source.js +14 -18
- package/dist/src/transform/cache-source.js.map +1 -1
- package/package.json +1 -1
- package/src/collection/action.ts +13 -5
- package/src/collection/collection.ts +250 -76
- package/src/collection/struct.ts +38 -8
- package/src/collection/write-attempts.ts +213 -109
- package/src/collections/diary/diary.ts +5 -1
- package/src/collections/tree/tree.ts +18 -11
- package/src/hlc/clock-store.ts +68 -0
- package/src/hlc/clock.ts +196 -0
- package/src/hlc/hlc.ts +56 -0
- package/src/hlc/index.ts +4 -0
- package/src/hlc/skew.ts +55 -0
- package/src/index.ts +2 -0
- package/src/lane/index.ts +1 -0
- package/src/lane/lane.ts +54 -0
- package/src/log/log.ts +341 -310
- package/src/log/struct.ts +62 -1
- package/src/network/i-repo.ts +11 -1
- package/src/network/stale-failure.ts +31 -7
- package/src/network/struct.ts +805 -770
- package/src/offline/entry.ts +16 -0
- package/src/offline/journal.ts +10 -0
- package/src/offline/queue.ts +38 -4
- package/src/offline/status.ts +7 -0
- package/src/testing/test-transactor.ts +1065 -1053
- package/src/transaction/actions-engine.ts +3 -0
- package/src/transaction/coordinator.ts +229 -54
- package/src/transaction/entry-commit.ts +56 -14
- package/src/transaction/errors.ts +60 -0
- package/src/transaction/index.ts +1 -1
- package/src/transaction/session.ts +18 -5
- package/src/transaction/transaction.ts +90 -17
- package/src/transaction/validator.ts +46 -5
- package/src/transactor/block-floors.ts +15 -1
- package/src/transactor/network-transactor.ts +15 -4
- package/src/transactor/transactor-source.ts +15 -1
- package/src/transactor/transactor.ts +9 -0
- package/src/transform/cache-source.ts +15 -17
|
@@ -1,1053 +1,1065 @@
|
|
|
1
|
-
import type { ITransactor, GetBlockResults, ActionBlocks, ActionLineage, BlockLineage, BlockActionStatus, PendResult, CommitResult, PendRequest, BlockId, CommitRequest, BlockGets, IBlock, ActionId, ActionRev, ActionTransforms, StaleFailure, Transform, Transforms, ClusterNomineesResult, CollectionId } from "../index.js";
|
|
2
|
-
import { highestStaleAt, isOwnRevision } from "../network/stale-failure.js";
|
|
3
|
-
import { localDurability } from "../network/durability.js";
|
|
4
|
-
import { ensuredMap } from "../utility/ensured.js";
|
|
5
|
-
import { Latches } from "../utility/latches.js";
|
|
6
|
-
import { applyTransform, blockIdsForTransforms, transformForBlockId, emptyTransforms, concatTransform, transformsFromTransform } from "../transform/index.js";
|
|
7
|
-
import { Tree } from "../collections/tree/tree.js";
|
|
8
|
-
import type { TreeReplaceAction } from "../collections/tree/struct.js";
|
|
9
|
-
import type { Collection } from "../collection/collection.js";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
//
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
async
|
|
314
|
-
this.checkAvailable();
|
|
315
|
-
const
|
|
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
|
-
const
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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
|
-
|
|
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
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
return inner.
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
)
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
*
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
this.
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
*
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
*
|
|
723
|
-
* the
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
*
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
*
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
*
|
|
981
|
-
*
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
* `
|
|
1046
|
-
*
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1
|
+
import type { ITransactor, GetBlockResults, ActionBlocks, ActionLineage, BlockLineage, BlockActionStatus, PendResult, CommitResult, PendRequest, BlockId, CommitRequest, BlockGets, IBlock, ActionId, ActionRev, ActionTransforms, StaleFailure, Transform, Transforms, ClusterNomineesResult, CollectionId } from "../index.js";
|
|
2
|
+
import { highestStaleAt, isOwnRevision } from "../network/stale-failure.js";
|
|
3
|
+
import { localDurability } from "../network/durability.js";
|
|
4
|
+
import { ensuredMap } from "../utility/ensured.js";
|
|
5
|
+
import { Latches } from "../utility/latches.js";
|
|
6
|
+
import { applyTransform, blockIdsForTransforms, transformForBlockId, emptyTransforms, concatTransform, transformsFromTransform } from "../transform/index.js";
|
|
7
|
+
import { Tree } from "../collections/tree/tree.js";
|
|
8
|
+
import type { TreeReplaceAction } from "../collections/tree/struct.js";
|
|
9
|
+
import type { Collection } from "../collection/collection.js";
|
|
10
|
+
import { HlcClock } from "../hlc/clock.js";
|
|
11
|
+
|
|
12
|
+
type RevisionNumber = number;
|
|
13
|
+
|
|
14
|
+
type BlockState = {
|
|
15
|
+
/** The current materialized block at each revision */
|
|
16
|
+
materializedBlocks: Map<RevisionNumber, IBlock>;
|
|
17
|
+
/** The latest revision number */
|
|
18
|
+
latestRev: RevisionNumber;
|
|
19
|
+
/** The action that created each revision */
|
|
20
|
+
revisionActions: Map<RevisionNumber, ActionId>;
|
|
21
|
+
/** Currently pending actions */
|
|
22
|
+
pendingActions: Map<ActionId, Transform>;
|
|
23
|
+
/** Committed actions */
|
|
24
|
+
committedActions: Map<ActionId, Transform>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type TestTransactorOptions = {
|
|
28
|
+
/** The clock writes through this transactor mint from. Default: a fresh in-memory clock in the node id `'test'`;
|
|
29
|
+
* inject one to drive a fake local clock, or to have two transactors mint from one sequence. */
|
|
30
|
+
clock?: HlcClock;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Simple in-memory transactor for testing that maintains materialized blocks for every revision
|
|
34
|
+
export class TestTransactor implements ITransactor {
|
|
35
|
+
private blocks = new Map<BlockId, BlockState>();
|
|
36
|
+
available = true;
|
|
37
|
+
private getLocks = new Map<BlockId, Promise<() => void>>(); // Track lock releases
|
|
38
|
+
readonly clock: HlcClock;
|
|
39
|
+
|
|
40
|
+
constructor(options?: TestTransactorOptions) {
|
|
41
|
+
this.clock = options?.clock ?? new HlcClock({ nodeId: 'test' });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async get(blockGets: BlockGets): Promise<GetBlockResults> {
|
|
45
|
+
this.checkAvailable();
|
|
46
|
+
const results: GetBlockResults = {};
|
|
47
|
+
const uniqueBlockIds = [...new Set(blockGets.blockIds)].sort(); // Ensure consistent lock order if needed, though get is read-only
|
|
48
|
+
const releases: (() => void)[] = [];
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
// Acquire locks for all requested blocks to ensure consistent read
|
|
52
|
+
for (const blockId of uniqueBlockIds) {
|
|
53
|
+
const lockId = `TestTransactor.commit:${blockId}`; // Use the same lock as commit
|
|
54
|
+
// Wait for any existing lock promise to resolve before acquiring the next
|
|
55
|
+
let release = await this.getLocks.get(lockId);
|
|
56
|
+
if (release) await Promise.resolve(release); // Ensure previous lock released if overlapping calls happen
|
|
57
|
+
|
|
58
|
+
const releasePromise = Latches.acquire(lockId);
|
|
59
|
+
this.getLocks.set(blockId, releasePromise.then(r => () => {
|
|
60
|
+
r();
|
|
61
|
+
this.getLocks.delete(blockId); // Clean up map entry after release
|
|
62
|
+
}));
|
|
63
|
+
release = await releasePromise;
|
|
64
|
+
releases.push(release);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// --- Start of Critical Section (Read) ---
|
|
68
|
+
for (const blockId of blockGets.blockIds) {
|
|
69
|
+
const blockState = this.blocks.get(blockId);
|
|
70
|
+
if (!blockState) {
|
|
71
|
+
// Block doesn't exist yet
|
|
72
|
+
results[blockId] = {
|
|
73
|
+
block: undefined,
|
|
74
|
+
state: { latest: undefined, pendings: [] }
|
|
75
|
+
};
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Get the appropriate materialized block based on context
|
|
80
|
+
let block: IBlock | undefined;
|
|
81
|
+
// The revision `block` was actually materialized at, reported (with its action id) as
|
|
82
|
+
// GetBlockResult.materialized. Equals `latestRev` on every unpinned path; only a
|
|
83
|
+
// revision-pinned read of a block committed further since the pin makes them differ,
|
|
84
|
+
// and there the pinned value is what the reader observed (see the field's doc).
|
|
85
|
+
let materializedRev: number | undefined;
|
|
86
|
+
// A named pending is overlaid only where this block holds it; every other block gets the
|
|
87
|
+
// plain answer for the same context (mirrors StorageRepo.get).
|
|
88
|
+
const pin = blockGets.context?.rev;
|
|
89
|
+
const namedPending = blockGets.context?.actionId !== undefined
|
|
90
|
+
? blockState.pendingActions.get(blockGets.context.actionId) : undefined;
|
|
91
|
+
if (namedPending) {
|
|
92
|
+
// Overlay over the committed base at the context's pin, as StorageRepo does.
|
|
93
|
+
const base = committedAt(blockState, pin);
|
|
94
|
+
block = applyTransformSafe(base?.block, namedPending);
|
|
95
|
+
// A pending carries no revision of its own — report the committed base it was
|
|
96
|
+
// applied over. Absent when there was no base (a pending-only insert).
|
|
97
|
+
materializedRev = base?.rev;
|
|
98
|
+
} else {
|
|
99
|
+
// context.committed proves its actions succeeded, so a block still holding one of them
|
|
100
|
+
// pending is served with it applied — mirrors the coordinator's read-driven promotion.
|
|
101
|
+
for (const { actionId: cId } of blockGets.context?.committed ?? []) {
|
|
102
|
+
const pendingTransform = blockState.pendingActions.get(cId);
|
|
103
|
+
if (pendingTransform) {
|
|
104
|
+
const baseBlock = blockState.materializedBlocks.get(blockState.latestRev);
|
|
105
|
+
block = applyTransformSafe(baseBlock, pendingTransform);
|
|
106
|
+
if (baseBlock) materializedRev = blockState.latestRev;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Otherwise the committed content at the highest revision ≤ the pin, or the latest.
|
|
111
|
+
if (block === undefined) {
|
|
112
|
+
const found = committedAt(blockState, pin);
|
|
113
|
+
block = structuredClone(found?.block);
|
|
114
|
+
materializedRev = found?.rev;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const actionId = blockState.revisionActions.get(blockState.latestRev);
|
|
119
|
+
// The materialized revision travels WITH its action id (one field, so the pair can
|
|
120
|
+
// never disagree) — the same `(rev, actionId)` the commit recorded for it.
|
|
121
|
+
const materializedActionId = materializedRev !== undefined
|
|
122
|
+
? blockState.revisionActions.get(materializedRev) : undefined;
|
|
123
|
+
results[blockId] = {
|
|
124
|
+
block,
|
|
125
|
+
...(materializedRev !== undefined && materializedActionId !== undefined
|
|
126
|
+
? { materialized: { rev: materializedRev, actionId: materializedActionId } } : {}),
|
|
127
|
+
state: {
|
|
128
|
+
latest: actionId !== undefined ? {
|
|
129
|
+
rev: blockState.latestRev,
|
|
130
|
+
actionId
|
|
131
|
+
} : undefined,
|
|
132
|
+
pendings: Array.from(blockState.pendingActions.keys())
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
// --- End of Critical Section (Read) ---
|
|
137
|
+
|
|
138
|
+
} finally {
|
|
139
|
+
// Release locks in reverse order
|
|
140
|
+
releases.reverse().forEach(release => release());
|
|
141
|
+
}
|
|
142
|
+
return results;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async getStatus(actionRefs: ActionBlocks[]): Promise<BlockActionStatus[]> {
|
|
146
|
+
return actionRefs.map(ref => ({
|
|
147
|
+
...ref,
|
|
148
|
+
statuses: ref.blockIds.map(blockId => {
|
|
149
|
+
const blockState = this.blocks.get(blockId);
|
|
150
|
+
if (!blockState) return 'aborted';
|
|
151
|
+
return blockState.pendingActions.has(ref.actionId) ? 'pending'
|
|
152
|
+
: Array.from(blockState.revisionActions.values()).some(actionId => actionId === ref.actionId) ? 'committed'
|
|
153
|
+
: 'aborted';
|
|
154
|
+
})
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** One store, no replicas: every revision was applied here onto the one before it, so the
|
|
159
|
+
* revision index answers for lineage outright — mirroring `BlockStorage.lineageOf`, including
|
|
160
|
+
* the one way a later revision does NOT build on its predecessor (an insert replaces the block
|
|
161
|
+
* wholesale). The "cohort" is this store alone, which is what the durability says. */
|
|
162
|
+
async getLineage(ref: ActionBlocks & { rev: number }): Promise<ActionLineage> {
|
|
163
|
+
this.checkAvailable();
|
|
164
|
+
const blocks = ref.blockIds.map((blockId): BlockLineage => {
|
|
165
|
+
const blockState = this.blocks.get(blockId);
|
|
166
|
+
if (!blockState || blockState.latestRev < ref.rev) return 'behind';
|
|
167
|
+
if (blockState.revisionActions.get(ref.rev) !== ref.actionId) return 'excludes';
|
|
168
|
+
const replacedSince = Array.from(blockState.revisionActions.entries()).some(([rev, actionId]) =>
|
|
169
|
+
rev > ref.rev && blockState.committedActions.get(actionId)?.insert !== undefined);
|
|
170
|
+
return replacedSince ? 'unknown' : 'contains';
|
|
171
|
+
});
|
|
172
|
+
return {
|
|
173
|
+
blocks,
|
|
174
|
+
...(blocks.length > 0 && blocks.every(lineage => lineage === 'contains') ? { durability: localDurability() } : {})
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async pend(request: PendRequest): Promise<PendResult> {
|
|
179
|
+
this.checkAvailable();
|
|
180
|
+
const { actionId, transforms, policy, rev } = request;
|
|
181
|
+
const blockIds = blockIdsForTransforms(transforms);
|
|
182
|
+
const conflictingPendings: { blockId: BlockId, actionId: ActionId }[] = [];
|
|
183
|
+
const missing: ActionTransforms[] = [];
|
|
184
|
+
// Confirmed revisions this pend is up against, mirroring StorageRepo.pend: a block whose own
|
|
185
|
+
// storage is already at or past the requested revision, held by someone other than this same
|
|
186
|
+
// action. Reported as `staleAt` so a caller reading the shared harness sees the real shape.
|
|
187
|
+
const staleCandidates: StaleFailure['staleAt'][] = [];
|
|
188
|
+
// Blocks this SAME action already committed at exactly the requested revision — the durable
|
|
189
|
+
// half of a torn action, met again by its own retry. Mirrors StorageRepo.pend's `satisfied`
|
|
190
|
+
// set: such a block is neither a committed conflict nor a pending one, and no pending record
|
|
191
|
+
// is written for it (commit's own-revision arm below never promotes one, so it would never
|
|
192
|
+
// clear). It still rides in the returned `blockIds`, as in the real repo, so `cancel` covers it.
|
|
193
|
+
const satisfied = new Set<BlockId>();
|
|
194
|
+
|
|
195
|
+
// Check for conflicts (pending or committed based on rev/insert)
|
|
196
|
+
for (const blockId of blockIds) {
|
|
197
|
+
const blockState = this.blocks.get(blockId);
|
|
198
|
+
const blockTransform = transformForBlockId(transforms, blockId);
|
|
199
|
+
if (!blockTransform) continue; // Should not happen
|
|
200
|
+
|
|
201
|
+
if (blockState && isOwnRevision(latestActionRev(blockState), rev, actionId)) {
|
|
202
|
+
satisfied.add(blockId);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (blockState) {
|
|
207
|
+
// Check for existing pending actions
|
|
208
|
+
if (blockState.pendingActions.size > 0) {
|
|
209
|
+
blockState.pendingActions.forEach((_, pendingActionId) => {
|
|
210
|
+
conflictingPendings.push({ blockId, actionId: pendingActionId });
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Check for conflicting committed revisions (if rev specified or it's an insert)
|
|
215
|
+
if (rev !== undefined || blockTransform.insert) {
|
|
216
|
+
const checkRev = rev ?? 0; // Check from revision 0 if it's an insert
|
|
217
|
+
if (blockState.latestRev >= checkRev) {
|
|
218
|
+
// Mirrors StorageRepo.pend exactly: only a real revision race yields a
|
|
219
|
+
// meaningful `staleAt`. A rev-less pend reaches here as an insert collision
|
|
220
|
+
// (`checkRev` degraded to 0), where the block's revision answers a question
|
|
221
|
+
// nobody asked. (Our own durable half of a torn action never reaches here —
|
|
222
|
+
// it was set aside as `satisfied` above.)
|
|
223
|
+
if (rev !== undefined) {
|
|
224
|
+
staleCandidates.push({ blockId, rev: blockState.latestRev });
|
|
225
|
+
}
|
|
226
|
+
// Collect conflicting committed actions
|
|
227
|
+
const missingForBlock = new Map<ActionId, { rev: number, transform: Transform }>();
|
|
228
|
+
for (let r = checkRev as number; r <= blockState.latestRev; r++) {
|
|
229
|
+
const committedActionId = blockState.revisionActions.get(r);
|
|
230
|
+
if (committedActionId !== undefined) {
|
|
231
|
+
const committedTransform = blockState.committedActions.get(committedActionId);
|
|
232
|
+
if (committedTransform) {
|
|
233
|
+
missingForBlock.set(committedActionId, { rev: r, transform: committedTransform });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Add collected missing transforms for this block to the main missing list
|
|
239
|
+
for (const [mActionId, data] of missingForBlock.entries()) {
|
|
240
|
+
let existing = missing.find(m => m.actionId === mActionId);
|
|
241
|
+
if (!existing) {
|
|
242
|
+
existing = { actionId: mActionId, rev: data.rev, transforms: emptyTransforms() };
|
|
243
|
+
missing.push(existing);
|
|
244
|
+
}
|
|
245
|
+
existing.rev = Math.max(existing.rev ?? 0, data.rev);
|
|
246
|
+
existing.transforms = concatTransform(existing.transforms, blockId, data.transform);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Handle failure due to committed conflicts first.
|
|
254
|
+
// `conflict: true` on the three optimistic-concurrency returns below mirrors what
|
|
255
|
+
// StorageRepo.pend now emits, so consumers of this test transactor see the real shape.
|
|
256
|
+
if (missing.length > 0) {
|
|
257
|
+
const staleAt = highestStaleAt(staleCandidates);
|
|
258
|
+
return {
|
|
259
|
+
success: false,
|
|
260
|
+
conflict: true,
|
|
261
|
+
missing,
|
|
262
|
+
...(staleAt ? { staleAt } : {})
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Handle failure/retry due to pending conflicts
|
|
267
|
+
if (conflictingPendings.length > 0) {
|
|
268
|
+
if (policy === 'f') {
|
|
269
|
+
return { success: false, conflict: true, pending: conflictingPendings };
|
|
270
|
+
} else if (policy === 'r') {
|
|
271
|
+
// Simulate fetching pending transforms for 'r' policy
|
|
272
|
+
const pendingWithTransforms = conflictingPendings
|
|
273
|
+
.map(({ blockId: pBlockId, actionId: pActionId }) => {
|
|
274
|
+
const pBlockState = this.blocks.get(pBlockId);
|
|
275
|
+
const pTransform = pBlockState?.pendingActions.get(pActionId)
|
|
276
|
+
?? pBlockState?.committedActions.get(pActionId); // Might have been committed since check
|
|
277
|
+
if (pTransform) {
|
|
278
|
+
return { blockId: pBlockId, actionId: pActionId, transform: pTransform };
|
|
279
|
+
}
|
|
280
|
+
return null; // Handle case where it disappeared (cancelled?)
|
|
281
|
+
})
|
|
282
|
+
.filter(p => p !== null) as { blockId: BlockId, actionId: ActionId, transform: Transform }[];
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
success: false,
|
|
286
|
+
conflict: true,
|
|
287
|
+
pending: pendingWithTransforms
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
// Policy 'w' allows proceeding despite pending transactions
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// No fatal conflicts found, proceed to pend
|
|
294
|
+
for (const blockId of blockIds) {
|
|
295
|
+
if (satisfied.has(blockId)) continue;
|
|
296
|
+
const blockTransform = transformForBlockId(transforms, blockId);
|
|
297
|
+
if (blockTransform) {
|
|
298
|
+
const blockState = ensuredMap(this.blocks, blockId, () => newBlockState());
|
|
299
|
+
blockState.pendingActions.set(actionId, blockTransform);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Return success, include pending list as per StorageRepo behavior. The in-memory double is one
|
|
304
|
+
// node with no cohort, so its honest class is `local` — exactly what a bare StorageRepo answers.
|
|
305
|
+
return {
|
|
306
|
+
success: true,
|
|
307
|
+
pending: conflictingPendings,
|
|
308
|
+
blockIds,
|
|
309
|
+
durability: localDurability()
|
|
310
|
+
} as PendResult;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async cancel(actionRef: ActionBlocks): Promise<void> {
|
|
314
|
+
this.checkAvailable();
|
|
315
|
+
for (const blockId of actionRef.blockIds) {
|
|
316
|
+
const blockState = this.blocks.get(blockId);
|
|
317
|
+
if (blockState) {
|
|
318
|
+
blockState.pendingActions.delete(actionRef.actionId);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async commit(request: CommitRequest): Promise<CommitResult> {
|
|
324
|
+
this.checkAvailable();
|
|
325
|
+
const { actionId, rev, blockIds } = request;
|
|
326
|
+
const uniqueBlockIds = [...new Set(blockIds)].sort();
|
|
327
|
+
const releases: (() => void)[] = [];
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
// Simulate acquiring locks sequentially like StorageRepo
|
|
331
|
+
for (const id of uniqueBlockIds) {
|
|
332
|
+
const lockId = `TestTransactor.commit:${id}`;
|
|
333
|
+
const release = await Latches.acquire(lockId);
|
|
334
|
+
releases.push(release);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// --- Start of Critical Section (Simulated) ---
|
|
338
|
+
|
|
339
|
+
// Blocks this same action already committed at exactly this revision: an idempotent no-op,
|
|
340
|
+
// mirroring StorageRepo.commit's `alreadyDone` partition. They are neither stale nor in need
|
|
341
|
+
// of a pending record (pend wrote none for them — see `satisfied` there), which is what lets
|
|
342
|
+
// a torn action's retry roll its remaining blocks forward.
|
|
343
|
+
const alreadyDone = new Set(blockIds.filter(blockId => {
|
|
344
|
+
const blockState = this.blocks.get(blockId);
|
|
345
|
+
return blockState !== undefined && isOwnRevision(latestActionRev(blockState), rev, actionId);
|
|
346
|
+
}));
|
|
347
|
+
|
|
348
|
+
// Check for stale revisions
|
|
349
|
+
const staleBlocks = blockIds.filter(blockId => {
|
|
350
|
+
const blockState = this.blocks.get(blockId);
|
|
351
|
+
return blockState && blockState.latestRev >= rev && !alreadyDone.has(blockId);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
if (staleBlocks.length > 0) {
|
|
355
|
+
// Collect missing actions for stale blocks
|
|
356
|
+
const missingByAction = new Map<ActionId, Transforms>();
|
|
357
|
+
for (const blockId of staleBlocks) {
|
|
358
|
+
const blockState = this.blocks.get(blockId)!;
|
|
359
|
+
for (let r = rev; r <= blockState.latestRev; r++) {
|
|
360
|
+
const committedActionId = blockState.revisionActions.get(r);
|
|
361
|
+
if (committedActionId) {
|
|
362
|
+
const transform = blockState.committedActions.get(committedActionId);
|
|
363
|
+
if (transform) {
|
|
364
|
+
const existing = missingByAction.get(committedActionId) ?? emptyTransforms();
|
|
365
|
+
missingByAction.set(committedActionId, concatTransform(existing, blockId, transform));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const missing: ActionTransforms[] = Array.from(missingByAction.entries()).map(([actionId, transforms]) => ({
|
|
372
|
+
actionId,
|
|
373
|
+
rev: Array.from(this.blocks.values())
|
|
374
|
+
.flatMap(bs => Array.from(bs.revisionActions.entries()))
|
|
375
|
+
.find(([, aId]) => aId === actionId)?.[0] ?? rev,
|
|
376
|
+
transforms
|
|
377
|
+
}));
|
|
378
|
+
// Same rule as StorageRepo.commit's missedCommits branch: report the highest confirmed
|
|
379
|
+
// revision a stale block is already at. A block held by this very action never reaches
|
|
380
|
+
// here — it was partitioned out as `alreadyDone` above.
|
|
381
|
+
const staleAt = highestStaleAt(staleBlocks.map(blockId =>
|
|
382
|
+
({ blockId, rev: this.blocks.get(blockId)!.latestRev })));
|
|
383
|
+
return { success: false, missing, ...(staleAt ? { staleAt } : {}) };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const toCommit = blockIds.filter(blockId => !alreadyDone.has(blockId));
|
|
387
|
+
|
|
388
|
+
// Verify all blocks that still need committing have the pending action
|
|
389
|
+
for (const blockId of toCommit) {
|
|
390
|
+
const blockState = this.blocks.get(blockId);
|
|
391
|
+
if (!blockState || !blockState.pendingActions.has(actionId)) {
|
|
392
|
+
return {
|
|
393
|
+
success: false,
|
|
394
|
+
reason: `Action ${actionId} not found or not pending for block ${blockId}`
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Commit the action for each block
|
|
400
|
+
for (const blockId of toCommit) {
|
|
401
|
+
const blockState = this.blocks.get(blockId)!;
|
|
402
|
+
const transform = blockState.pendingActions.get(actionId)!;
|
|
403
|
+
|
|
404
|
+
// Get base block to apply transform to
|
|
405
|
+
const baseBlock = blockState.materializedBlocks.get(blockState.latestRev);
|
|
406
|
+
|
|
407
|
+
let newBlock: IBlock | undefined;
|
|
408
|
+
if (!baseBlock) {
|
|
409
|
+
if (!transform.insert) {
|
|
410
|
+
throw new Error(`Commit Error: Action ${actionId} has no insert for new block ${blockId}`);
|
|
411
|
+
}
|
|
412
|
+
newBlock = structuredClone(transform.insert);
|
|
413
|
+
} else {
|
|
414
|
+
newBlock = applyTransformSafe(baseBlock, transform);
|
|
415
|
+
if (!newBlock && !transform.delete) {
|
|
416
|
+
throw new Error(`Commit Error: Action ${actionId} resulted in undefined block but had no delete flag for block ${blockId}`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (newBlock) {
|
|
421
|
+
blockState.materializedBlocks.set(rev, newBlock);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Update block state
|
|
425
|
+
blockState.latestRev = rev;
|
|
426
|
+
blockState.revisionActions.set(rev, actionId);
|
|
427
|
+
blockState.committedActions.set(actionId, transform);
|
|
428
|
+
blockState.pendingActions.delete(actionId);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// --- End of Critical Section (Simulated) ---
|
|
432
|
+
|
|
433
|
+
return { success: true, durability: localDurability() };
|
|
434
|
+
|
|
435
|
+
} finally {
|
|
436
|
+
// Release locks in reverse order
|
|
437
|
+
releases.reverse().forEach(release => release());
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Helper methods for testing
|
|
442
|
+
reset() {
|
|
443
|
+
this.blocks.clear();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
getPendingActions(): Map<ActionId, ActionTransforms> {
|
|
447
|
+
const allPending = new Map<ActionId, ActionTransforms>();
|
|
448
|
+
for (const [blockId, blockState] of this.blocks.entries()) {
|
|
449
|
+
for (const [actionId, transform] of blockState.pendingActions) {
|
|
450
|
+
const existing = allPending.get(actionId);
|
|
451
|
+
if (!existing) {
|
|
452
|
+
allPending.set(actionId, { actionId, transforms: transformsFromTransform(transform, blockId) });
|
|
453
|
+
} else {
|
|
454
|
+
existing.transforms = concatTransform(existing.transforms, blockId, transform);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return allPending;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
getCommittedActions(): Map<ActionId, ActionTransforms> {
|
|
462
|
+
const allCommitted = new Map<ActionId, ActionTransforms>();
|
|
463
|
+
for (const [blockId, blockState] of this.blocks.entries()) {
|
|
464
|
+
for (const [rev, actionId] of blockState.revisionActions) {
|
|
465
|
+
const transform = blockState.committedActions.get(actionId);
|
|
466
|
+
if (transform) {
|
|
467
|
+
const existing = allCommitted.get(actionId);
|
|
468
|
+
if (!existing) {
|
|
469
|
+
allCommitted.set(actionId, {
|
|
470
|
+
actionId,
|
|
471
|
+
rev,
|
|
472
|
+
transforms: transformsFromTransform(transform, blockId)
|
|
473
|
+
});
|
|
474
|
+
} else {
|
|
475
|
+
existing.transforms = concatTransform(existing.transforms, blockId, transform);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
return allCommitted;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
setAvailable(available: boolean) {
|
|
484
|
+
this.available = available;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
checkAvailable() {
|
|
488
|
+
if (!this.available) {
|
|
489
|
+
throw new Error('Transactor is not available');
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Optional method for querying cluster nominees (used in GATHER phase for multi-collection transactions) */
|
|
494
|
+
queryClusterNominees?: (blockId: BlockId) => Promise<ClusterNomineesResult>;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Base for the intercepting wrappers below: forwards every {@link ITransactor} member to `inner`,
|
|
499
|
+
* so a wrapper overrides only the one call it intercepts and cannot silently drop the rest.
|
|
500
|
+
*
|
|
501
|
+
* `queryClusterNominees` is forwarded through a GETTER rather than a method, because the coordinator
|
|
502
|
+
* treats its ABSENCE as "no supercluster" (see `gatherPhase`). A wrapper that always defined it
|
|
503
|
+
* would push every wrapped multi-collection test onto the GATHER path even when the inner
|
|
504
|
+
* transactor never opted in; a wrapper that omits it (as these did before) does the opposite —
|
|
505
|
+
* a test that sets `inner.queryClusterNominees` and then wraps would skip GATHER and pass
|
|
506
|
+
* vacuously. The getter reproduces the inner transactor's own answer either way.
|
|
507
|
+
*/
|
|
508
|
+
export abstract class DelegatingTransactor implements ITransactor {
|
|
509
|
+
protected constructor(protected readonly inner: TestTransactor) {}
|
|
510
|
+
|
|
511
|
+
get clock(): HlcClock { return this.inner.clock; }
|
|
512
|
+
|
|
513
|
+
get(b: BlockGets): Promise<GetBlockResults> { return this.inner.get(b); }
|
|
514
|
+
getStatus(a: ActionBlocks[]): Promise<BlockActionStatus[]> { return this.inner.getStatus(a); }
|
|
515
|
+
pend(r: PendRequest): Promise<PendResult> { return this.inner.pend(r); }
|
|
516
|
+
cancel(a: ActionBlocks): Promise<void> { return this.inner.cancel(a); }
|
|
517
|
+
commit(r: CommitRequest): Promise<CommitResult> { return this.inner.commit(r); }
|
|
518
|
+
getLineage(ref: ActionBlocks & { rev: number }): Promise<ActionLineage> { return this.inner.getLineage(ref); }
|
|
519
|
+
|
|
520
|
+
get queryClusterNominees(): ((blockId: BlockId) => Promise<ClusterNomineesResult>) | undefined {
|
|
521
|
+
return this.inner.queryClusterNominees?.bind(this.inner);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// Getters for the same reason: the coordinator treats their absence as "pend and commit each
|
|
525
|
+
// collection on its own". `TestTransactor` offers neither, so a wrapped one keeps that path.
|
|
526
|
+
get pendMany(): ITransactor['pendMany'] {
|
|
527
|
+
const inner: ITransactor = this.inner;
|
|
528
|
+
return inner.pendMany?.bind(inner);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
get commitMany(): ITransactor['commitMany'] {
|
|
532
|
+
const inner: ITransactor = this.inner;
|
|
533
|
+
return inner.commitMany?.bind(inner);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Wraps a {@link TestTransactor} and forces its commit phase to fail a bounded (or unbounded)
|
|
539
|
+
* number of times before delegating, so tests can exercise the sync retry / backoff / give-up
|
|
540
|
+
* path deterministically. Everything else delegates unchanged, so the pend→commit→cancel
|
|
541
|
+
* round-trip runs exactly as in production.
|
|
542
|
+
*/
|
|
543
|
+
export class FlakyCommitTransactor extends DelegatingTransactor {
|
|
544
|
+
/** Number of commit() calls observed so far (across success and forced failure). */
|
|
545
|
+
commitAttempts = 0;
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* @param inner delegate transactor
|
|
549
|
+
* @param failFirstN number of initial commit() calls to fail; Infinity to always fail
|
|
550
|
+
* @param reason the StaleFailure.reason returned on a forced failure
|
|
551
|
+
*/
|
|
552
|
+
constructor(
|
|
553
|
+
inner: TestTransactor,
|
|
554
|
+
private readonly failFirstN: number,
|
|
555
|
+
private readonly reason = 'forced stale',
|
|
556
|
+
) {
|
|
557
|
+
super(inner);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
561
|
+
this.commitAttempts++;
|
|
562
|
+
if (this.commitAttempts <= this.failFirstN) {
|
|
563
|
+
return { success: false, reason: this.reason };
|
|
564
|
+
}
|
|
565
|
+
return this.inner.commit(request);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Commits the WHOLE action durably on the inner {@link TestTransactor}, then reports a stale
|
|
571
|
+
* failure anyway — a write that fully landed but whose writer was told it failed.
|
|
572
|
+
*
|
|
573
|
+
* This is NOT the shape `NetworkTransactor.commit` produces when it refuses a write whose log tail
|
|
574
|
+
* landed: there the tail is committed BEFORE the sweep of the remaining blocks, so the blocks after
|
|
575
|
+
* the tail did NOT land. That shape is {@link TailLandsButReportsStale}, and it is the one that
|
|
576
|
+
* matters in production. This double makes "my own log entry is visible" and "my action is fully
|
|
577
|
+
* durable" the same thing, so on its own it cannot tell a writer that finishes its half-landed
|
|
578
|
+
* action from one that merely assumes it is finished. Keep it for the case it does model — every
|
|
579
|
+
* block already holds the action's revision, so the writer's completion pass has nothing left to
|
|
580
|
+
* land and must not duplicate or move the entry.
|
|
581
|
+
*
|
|
582
|
+
* Safe against the inner transactor's bookkeeping because `TransactorSource.transact` cancels
|
|
583
|
+
* the pend on the reported failure and {@link TestTransactor.cancel} only deletes PENDING records
|
|
584
|
+
* — the real commit already promoted them, so the cancel is a no-op.
|
|
585
|
+
*
|
|
586
|
+
* `afterLanding`, when given, runs a competing writer ONCE, after the first masked commit has
|
|
587
|
+
* landed and before its failure is reported: the rival reads the landed write and commits the next
|
|
588
|
+
* revision ON TOP of it, so by the time the writer looks again every block it wrote has moved past
|
|
589
|
+
* its revision while still containing it. Safe to await from inside `commit` here (unlike
|
|
590
|
+
* {@link TailLandsButReportsStale}'s rival, which has to wait for the cancel): the whole action
|
|
591
|
+
* landed, so it left no pending record for the rival's pend to collide with.
|
|
592
|
+
*
|
|
593
|
+
* Used by both write paths' own-entry regression suites (collection-own-action-replay.spec.ts and
|
|
594
|
+
* coordinator-own-action-replay.spec.ts); it lives here so the two cannot drift apart.
|
|
595
|
+
*/
|
|
596
|
+
export class CommitLandsButReportsStale extends DelegatingTransactor {
|
|
597
|
+
/** Remaining number of successful commits to mask as stale failures. */
|
|
598
|
+
private injections: number;
|
|
599
|
+
/** Commits that actually landed on the inner transactor (masked or not). */
|
|
600
|
+
landedCommits = 0;
|
|
601
|
+
private rivalDue: boolean;
|
|
602
|
+
|
|
603
|
+
constructor(inner: TestTransactor, injections = 1, private readonly afterLanding?: RivalWrite) {
|
|
604
|
+
super(inner);
|
|
605
|
+
this.injections = injections;
|
|
606
|
+
this.rivalDue = afterLanding !== undefined;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
610
|
+
const result = await this.inner.commit(request);
|
|
611
|
+
if (result.success) {
|
|
612
|
+
this.landedCommits++;
|
|
613
|
+
if (this.injections-- > 0) {
|
|
614
|
+
if (this.rivalDue) {
|
|
615
|
+
this.rivalDue = false;
|
|
616
|
+
await this.afterLanding!(this.inner);
|
|
617
|
+
}
|
|
618
|
+
return { success: false, conflict: true, reason: 'stale commit: injected torn-action conflict' };
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return result;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Lands ONLY the action's log tail on the inner {@link TestTransactor}, then reports a retryable
|
|
627
|
+
* failure — the shape `NetworkTransactor.commit` actually produces for a half-landed write. It
|
|
628
|
+
* commits the tail first and sweeps the remaining blocks only if the tail answered success, and a
|
|
629
|
+
* failed tail answer does not mean the tail is absent: the coordinator's durability gate answers
|
|
630
|
+
* `commit-not-durable` whenever fewer than a majority of the cohort hold the revision, even though
|
|
631
|
+
* some members stored it. So the writer is told "failed" over a log entry that is durable, while
|
|
632
|
+
* none of the entry's other blocks were ever committed — and the writer's own cancel then drops
|
|
633
|
+
* their pending records.
|
|
634
|
+
*
|
|
635
|
+
* A commit carrying nothing but the tail is delegated untouched (there is nothing to abandon), and
|
|
636
|
+
* does not consume an injection.
|
|
637
|
+
*
|
|
638
|
+
* `afterCancel`, when given, runs a competing writer ONCE, right after the writer's cancel of the
|
|
639
|
+
* torn attempt has been delegated. It is on `cancel` and not inside `commit` for the reason
|
|
640
|
+
* {@link CompetingWriterTransactor} documents: until that cancel lands, the torn action's data
|
|
641
|
+
* blocks still carry its pending records, a rival pending with policy `'r'` collides with them, and
|
|
642
|
+
* awaiting that rival from inside the writer's own commit is a livelock.
|
|
643
|
+
*/
|
|
644
|
+
export class TailLandsButReportsStale extends DelegatingTransactor {
|
|
645
|
+
/** Remaining number of commits to tear. */
|
|
646
|
+
private injections: number;
|
|
647
|
+
/** Commits torn so far: the tail landed, every other block of the action was abandoned. */
|
|
648
|
+
tears = 0;
|
|
649
|
+
/** Commits delegated whole that succeeded on the inner transactor. */
|
|
650
|
+
landedCommits = 0;
|
|
651
|
+
private rivalDue = false;
|
|
652
|
+
|
|
653
|
+
constructor(inner: TestTransactor, injections = 1, private readonly afterCancel?: RivalWrite) {
|
|
654
|
+
super(inner);
|
|
655
|
+
this.injections = injections;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
659
|
+
if (this.injections > 0 && request.blockIds.some(blockId => blockId !== request.tailId)) {
|
|
660
|
+
const tail = await this.inner.commit({ ...request, blockIds: [request.tailId] });
|
|
661
|
+
// A tail that itself lost is an ordinary loss — nothing is torn, so report it verbatim
|
|
662
|
+
// and keep the injection for a commit that can actually tear.
|
|
663
|
+
if (!tail.success) return tail;
|
|
664
|
+
this.injections--;
|
|
665
|
+
this.tears++;
|
|
666
|
+
this.rivalDue = this.afterCancel !== undefined;
|
|
667
|
+
return { success: false, conflict: true, reason: 'commit-not-durable: injected tail-only landing' };
|
|
668
|
+
}
|
|
669
|
+
const result = await this.inner.commit(request);
|
|
670
|
+
if (result.success) this.landedCommits++;
|
|
671
|
+
return result;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
override async cancel(actionRef: ActionBlocks): Promise<void> {
|
|
675
|
+
await this.inner.cancel(actionRef);
|
|
676
|
+
if (this.rivalDue) {
|
|
677
|
+
this.rivalDue = false;
|
|
678
|
+
await this.afterCancel!(this.inner);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Answers every pend with a promise shortfall — the cohort's silence, in the shape
|
|
685
|
+
* `CoordinatorRepo.pend` returns it — and delegates nothing, so nothing is reserved and nothing
|
|
686
|
+
* lands. For the write paths' "stopped by name" cases: a shortfall is not a conflict a retry can
|
|
687
|
+
* win, so one attempt is made and `PromiseShortfallError` escapes with every staged action still
|
|
688
|
+
* staged.
|
|
689
|
+
*/
|
|
690
|
+
export class RefusesPendsAsShortfall extends DelegatingTransactor {
|
|
691
|
+
/** Pends refused so far. */
|
|
692
|
+
pends = 0;
|
|
693
|
+
/** While `true` every pend is refused; set `false` to let pends through again — the cohort back
|
|
694
|
+
* in reach, for a test of what waits on it. */
|
|
695
|
+
refusing = true;
|
|
696
|
+
|
|
697
|
+
constructor(inner: TestTransactor) {
|
|
698
|
+
super(inner);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
override async pend(request: PendRequest): Promise<PendResult> {
|
|
702
|
+
if (!this.refusing) return this.inner.pend(request);
|
|
703
|
+
this.pends++;
|
|
704
|
+
return {
|
|
705
|
+
success: false,
|
|
706
|
+
conflict: false,
|
|
707
|
+
reason: 'Failed to get super-majority: injected shortfall',
|
|
708
|
+
shortfall: { approvals: 1, rejections: 0, peerCount: 3, needed: 3, silentPeers: ['peer-b', 'peer-c'] },
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/** What one attempt of a write put on the wire: the pend's transforms, with the action id and
|
|
714
|
+
* revision it was pended under. Recorded by {@link RecordsAttemptsRefusesFirstCommit}. */
|
|
715
|
+
export type RecordedAttempt = {
|
|
716
|
+
actionId: ActionId;
|
|
717
|
+
rev?: number;
|
|
718
|
+
transforms: Transforms;
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* Records what every attempt of a write pends, and refuses the FIRST commit outright — nothing is
|
|
723
|
+
* delegated, so nothing lands and the retry's refresh cannot find the write's own log entry.
|
|
724
|
+
*
|
|
725
|
+
* That is the case `Collection.completeOwnEntry` does NOT reach: the refused commit left the log
|
|
726
|
+
* block (and, for a brand-new collection, the header) uncommitted, so the refresh has no entry to
|
|
727
|
+
* finish from and the retry REBUILDS its log append from scratch. Storage identifies a saved block
|
|
728
|
+
* revision by `(action id, revision)` and deliberately accepts a retry of the same action at the
|
|
729
|
+
* same revision, so any per-attempt value in that rebuild becomes two contents under one
|
|
730
|
+
* `(action, revision)` across the machines that landed different attempts.
|
|
731
|
+
*
|
|
732
|
+
* `attempts` is therefore asserted on WHOLE — not field by field — so the rule it pins ("a retry at
|
|
733
|
+
* the same revision sends byte-identical transforms") fails for any future per-attempt value, not
|
|
734
|
+
* only the ones known today. Transforms are cloned on the way in, since the caller keeps mutating
|
|
735
|
+
* the live tracker afterwards.
|
|
736
|
+
*
|
|
737
|
+
* Lives here beside {@link CommitLandsButReportsStale} for the reason that one gives: the
|
|
738
|
+
* collection-path and coordinator-path specs must not drift apart on the double they share.
|
|
739
|
+
*/
|
|
740
|
+
export class RecordsAttemptsRefusesFirstCommit extends DelegatingTransactor {
|
|
741
|
+
readonly attempts: RecordedAttempt[] = [];
|
|
742
|
+
private refusals: number;
|
|
743
|
+
|
|
744
|
+
constructor(inner: TestTransactor, refusals = 1) {
|
|
745
|
+
super(inner);
|
|
746
|
+
this.refusals = refusals;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/** Arm (or re-arm) the refusal counter, so a test can let its setup writes land cleanly and
|
|
750
|
+
* then refuse only the write under test. Replaces whatever is left of the current count. */
|
|
751
|
+
refuseNextCommits(count: number): void {
|
|
752
|
+
this.refusals = count;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
override async pend(request: PendRequest): Promise<PendResult> {
|
|
756
|
+
this.attempts.push({
|
|
757
|
+
actionId: request.actionId,
|
|
758
|
+
rev: request.rev,
|
|
759
|
+
transforms: structuredClone(request.transforms),
|
|
760
|
+
});
|
|
761
|
+
return this.inner.pend(request);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
765
|
+
if (this.refusals > 0) {
|
|
766
|
+
this.refusals--;
|
|
767
|
+
// Never delegated: nothing of this attempt is durable, so `TransactorSource.transact`
|
|
768
|
+
// cancels the pend and the writer re-drives at the same action id and revision.
|
|
769
|
+
return { success: false, conflict: true, reason: 'stale commit: injected refusal, nothing landed' };
|
|
770
|
+
}
|
|
771
|
+
return this.inner.commit(request);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/** A competing writer: a real write driven against the UNWRAPPED transactor, so its own
|
|
776
|
+
* pend/commit calls are invisible to {@link CompetingWriterTransactor}'s counters and cannot
|
|
777
|
+
* re-trigger the interception. See {@link commitRivalTreeWrite} for the usual implementation. */
|
|
778
|
+
export type RivalWrite = (inner: ITransactor) => Promise<void>;
|
|
779
|
+
|
|
780
|
+
export type CompetingWriterOptions = {
|
|
781
|
+
/** Fires on the first pend whose request satisfies this predicate. `callIndex` is 1-based over
|
|
782
|
+
* ALL pend calls seen (not only matching ones). Default: fire on the first pend call. */
|
|
783
|
+
when?: (request: PendRequest, callIndex: number) => boolean;
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Wraps a {@link TestTransactor} and, exactly once, runs a real competing writer to completion
|
|
788
|
+
* BEFORE delegating the intercepted pend. The rival durably commits (real log entry, real
|
|
789
|
+
* revision bump), so the delegated pend then fails as a GENUINE optimistic-concurrency loss —
|
|
790
|
+
* nothing is forced or faked, unlike {@link FlakyCommitTransactor}, which returns a stale failure
|
|
791
|
+
* without ever advancing a block's revision.
|
|
792
|
+
*
|
|
793
|
+
* That distinction is the whole point: only a rival that actually landed can prove the loser
|
|
794
|
+
* OBSERVED a newer revision, re-applied its work on top of it, and did not lose an update.
|
|
795
|
+
*
|
|
796
|
+
* Two deliberate design constraints:
|
|
797
|
+
*
|
|
798
|
+
* 1. **The trigger is on PEND, and there is no commit trigger.** The rival is a real
|
|
799
|
+
* Collection/Tree, so its write pends with policy `'r'` (see `TransactorSource.transact`), and
|
|
800
|
+
* {@link TestTransactor.pend} rejects a pend whose blocks already carry a *pending* action.
|
|
801
|
+
* Firing before the loser's pend delegates means the loser has pended nothing yet, so the
|
|
802
|
+
* rival pends and commits cleanly and the loser's delegated pend then fails with `missing` — a
|
|
803
|
+
* real conflict. Firing at COMMIT time instead would leave the loser already pending on the
|
|
804
|
+
* shared log-tail block, so the rival's own pend would collide with it and spin through its
|
|
805
|
+
* sync retry budget (~10 attempts, ~21s) while the loser sits awaiting the rival inside its
|
|
806
|
+
* own commit: a livelock dressed up as a slow test. Do not add a commit trigger.
|
|
807
|
+
*
|
|
808
|
+
* 2. **The rival runs before delegation, never inside {@link TestTransactor}'s critical section.**
|
|
809
|
+
* `TestTransactor.get`/`commit` hold per-block latches (`TestTransactor.commit:${blockId}`), and
|
|
810
|
+
* those latch keys are process-global — a rival invoked from inside one of them would
|
|
811
|
+
* self-deadlock on its own reads. Intercepting here in the wrapper, before
|
|
812
|
+
* `await this.inner.pend(...)`, is outside every such section.
|
|
813
|
+
*/
|
|
814
|
+
export class CompetingWriterTransactor extends DelegatingTransactor {
|
|
815
|
+
/** Pend calls observed (including the intercepted one). */
|
|
816
|
+
pendCalls = 0;
|
|
817
|
+
/** Commit calls observed. */
|
|
818
|
+
commitCalls = 0;
|
|
819
|
+
/** The 1-based pend call index the rival fired on; undefined if it never fired. */
|
|
820
|
+
firedAtCall?: number;
|
|
821
|
+
/** How many times the rival ran. Latched to at most 1 — a second firing would collide with the
|
|
822
|
+
* loser's now-pending blocks and the transaction under test could never win. */
|
|
823
|
+
rivalRuns = 0;
|
|
824
|
+
|
|
825
|
+
constructor(
|
|
826
|
+
inner: TestTransactor,
|
|
827
|
+
private readonly rival: RivalWrite,
|
|
828
|
+
private readonly options?: CompetingWriterOptions,
|
|
829
|
+
) {
|
|
830
|
+
super(inner);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
override async pend(request: PendRequest): Promise<PendResult> {
|
|
834
|
+
// Increment synchronously, before any await, so call indexes reflect the caller's fan-out
|
|
835
|
+
// order rather than scheduling order.
|
|
836
|
+
const callIndex = ++this.pendCalls;
|
|
837
|
+
const matches = this.options?.when
|
|
838
|
+
? this.options.when(request, callIndex)
|
|
839
|
+
: callIndex === 1;
|
|
840
|
+
if (this.rivalRuns === 0 && matches) {
|
|
841
|
+
this.rivalRuns++;
|
|
842
|
+
this.firedAtCall = callIndex;
|
|
843
|
+
// A throwing rival escapes as a rejected pend, which pendPhase flattens to a bare message
|
|
844
|
+
// string ("hard failure") with the stack discarded — so name the source in the message, or
|
|
845
|
+
// a broken rival reads as an unexplained coordinator pend failure.
|
|
846
|
+
try {
|
|
847
|
+
await this.rival(this.inner);
|
|
848
|
+
} catch (e) {
|
|
849
|
+
throw new Error(`competing writer failed: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return this.inner.pend(request);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
856
|
+
this.commitCalls++;
|
|
857
|
+
return this.inner.commit(request);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
type Deferred = { promise: Promise<void>; resolve: () => void };
|
|
862
|
+
|
|
863
|
+
function deferred(): Deferred {
|
|
864
|
+
let resolve!: () => void;
|
|
865
|
+
const promise = new Promise<void>(r => { resolve = r; });
|
|
866
|
+
return { promise, resolve };
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Parks the commit AFTER the inner transactor has made it durable and BEFORE the result gets back
|
|
871
|
+
* to the coordinator — precisely the window in which storage already holds the new revision but
|
|
872
|
+
* the caller's local fold (`Collection.recordCommitted`) has not run.
|
|
873
|
+
*
|
|
874
|
+
* Everything else delegates, so the pend/commit round trip is otherwise the real one.
|
|
875
|
+
*
|
|
876
|
+
* Parks only the FIRST commit call and delegates every later one: a multi-collection commit issues
|
|
877
|
+
* one `commit` per participant, and parking all of them would mean the span never completes.
|
|
878
|
+
*/
|
|
879
|
+
export class GatedCommitTransactor extends DelegatingTransactor {
|
|
880
|
+
/** The `rev` carried on each pend request seen, in call order. */
|
|
881
|
+
readonly pendRevs: (number | undefined)[] = [];
|
|
882
|
+
/** The `rev` carried on each commit request seen, in call order. */
|
|
883
|
+
readonly commitRevs: number[] = [];
|
|
884
|
+
/** Resolves once a commit has landed durably in the inner transactor and is parked. */
|
|
885
|
+
readonly commitParked: Promise<void>;
|
|
886
|
+
private readonly parked = deferred();
|
|
887
|
+
private readonly gate = deferred();
|
|
888
|
+
private parkedOnce = false;
|
|
889
|
+
|
|
890
|
+
constructor(inner: TestTransactor) {
|
|
891
|
+
super(inner);
|
|
892
|
+
this.commitParked = this.parked.promise;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
override async pend(request: PendRequest): Promise<PendResult> {
|
|
896
|
+
this.pendRevs.push(request.rev);
|
|
897
|
+
return this.inner.pend(request);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
901
|
+
this.commitRevs.push(request.rev);
|
|
902
|
+
const result = await this.inner.commit(request); // durable FIRST
|
|
903
|
+
if (!this.parkedOnce) {
|
|
904
|
+
this.parkedOnce = true;
|
|
905
|
+
this.parked.resolve();
|
|
906
|
+
await this.gate.promise;
|
|
907
|
+
}
|
|
908
|
+
return result;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
openGate(): void { this.gate.resolve(); }
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* Throws, nothing delegated, on every commit of the collection whose log tail is `tailId` while
|
|
916
|
+
* `armed` — the hard failure of one participant's commit after its siblings' landed, which a
|
|
917
|
+
* multi-collection commit reports as `CoordinatorPartialCommitError`. Disarmed, everything delegates.
|
|
918
|
+
*/
|
|
919
|
+
export class ThrowsOnCommitOf extends DelegatingTransactor {
|
|
920
|
+
/** Commits thrown so far. */
|
|
921
|
+
thrown = 0;
|
|
922
|
+
armed = true;
|
|
923
|
+
|
|
924
|
+
constructor(inner: TestTransactor, private readonly tailId: BlockId) {
|
|
925
|
+
super(inner);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
override async commit(request: CommitRequest): Promise<CommitResult> {
|
|
929
|
+
if (this.armed && request.tailId === this.tailId) {
|
|
930
|
+
this.thrown++;
|
|
931
|
+
throw new Error('connection dropped before the commit was sent');
|
|
932
|
+
}
|
|
933
|
+
return this.inner.commit(request);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Parks the FIRST pend BEFORE it reaches the inner transactor, so a rival can durably take the
|
|
939
|
+
* revision while the coordinator is already committed to a set of transforms it captured at the
|
|
940
|
+
* log append.
|
|
941
|
+
*
|
|
942
|
+
* Snapshots the pair `(what was handed to the network, what the collection's tracker held)` at
|
|
943
|
+
* DELEGATION time — after the gate — for every pend, so a refresh that swapped the tracker's
|
|
944
|
+
* transforms object out from under the coordinator shows up as a divergent pair.
|
|
945
|
+
*/
|
|
946
|
+
export class GatedPendTransactor<TAction> extends DelegatingTransactor {
|
|
947
|
+
/** One entry per delegated pend, in call order. */
|
|
948
|
+
readonly snapshots: { pended: Transforms; staged: Transforms }[] = [];
|
|
949
|
+
/** Resolves once a pend is parked and has NOT yet reached the inner transactor. */
|
|
950
|
+
readonly pendParked: Promise<void>;
|
|
951
|
+
/** The collection under test. Assigned after construction — the collection is built over this
|
|
952
|
+
* wrapper, so it cannot be a constructor argument. */
|
|
953
|
+
collection?: Collection<TAction>;
|
|
954
|
+
private readonly parked = deferred();
|
|
955
|
+
private readonly gate = deferred();
|
|
956
|
+
private parkedOnce = false;
|
|
957
|
+
|
|
958
|
+
constructor(inner: TestTransactor) {
|
|
959
|
+
super(inner);
|
|
960
|
+
this.pendParked = this.parked.promise;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
override async pend(request: PendRequest): Promise<PendResult> {
|
|
964
|
+
if (!this.parkedOnce) {
|
|
965
|
+
this.parkedOnce = true;
|
|
966
|
+
this.parked.resolve();
|
|
967
|
+
await this.gate.promise;
|
|
968
|
+
}
|
|
969
|
+
this.snapshots.push({
|
|
970
|
+
pended: structuredClone(request.transforms),
|
|
971
|
+
staged: structuredClone(this.collection!.tracker.transforms),
|
|
972
|
+
});
|
|
973
|
+
return this.inner.pend(request);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
openGate(): void { this.gate.resolve(); }
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
/**
|
|
980
|
+
* Durably commit a conflicting change to a tree collection: opens a SECOND {@link Tree} over the
|
|
981
|
+
* same transactor + collection id and replaces `entries` through it, producing a real log entry
|
|
982
|
+
* and a real revision bump — the durable competitor a {@link CompetingWriterTransactor} needs.
|
|
983
|
+
*
|
|
984
|
+
* `Tree.replace` is act + `updateAndSync`, i.e. a full commit through the single-collection sync
|
|
985
|
+
* path, so the rival's write is indistinguishable from any other client's.
|
|
986
|
+
*
|
|
987
|
+
* Pass the UNWRAPPED transactor (that is what {@link RivalWrite} receives).
|
|
988
|
+
*
|
|
989
|
+
* NOTE: opens the rival tree at the DEFAULT node capacity (64), because fan-out is not persisted
|
|
990
|
+
* in the collection header (see Tree.createOrOpen's `nodeCapacity` note). Fine while every rival
|
|
991
|
+
* race is between default-fan-out trees; if a test ever needs to race a small-capacity tree, add a
|
|
992
|
+
* capacity parameter here and pass the same value both sides, or the two writers will split nodes
|
|
993
|
+
* at different fan-outs.
|
|
994
|
+
*/
|
|
995
|
+
export async function commitRivalTreeWrite<TKey, TEntry>(
|
|
996
|
+
inner: ITransactor,
|
|
997
|
+
collectionId: CollectionId,
|
|
998
|
+
keyFromEntry: (entry: TEntry) => TKey,
|
|
999
|
+
entries: TreeReplaceAction<TKey, TEntry>,
|
|
1000
|
+
): Promise<void> {
|
|
1001
|
+
const tree = await Tree.createOrOpen<TKey, TEntry>(inner, collectionId, keyFromEntry);
|
|
1002
|
+
await tree.replace(entries);
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
function newBlockState(): BlockState {
|
|
1006
|
+
return {
|
|
1007
|
+
materializedBlocks: new Map(),
|
|
1008
|
+
latestRev: 0,
|
|
1009
|
+
revisionActions: new Map(),
|
|
1010
|
+
pendingActions: new Map(),
|
|
1011
|
+
committedActions: new Map()
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/** This block's latest committed revision in the shape {@link isOwnRevision} compares — the
|
|
1016
|
+
* harness equivalent of `IBlockStorage.getLatest()`. `undefined` when nothing is recorded at that
|
|
1017
|
+
* revision, which is how a never-written block reads (`latestRev` starts at 0 with no entry). */
|
|
1018
|
+
function latestActionRev(blockState: BlockState): ActionRev | undefined {
|
|
1019
|
+
const actionId = blockState.revisionActions.get(blockState.latestRev);
|
|
1020
|
+
return actionId === undefined ? undefined : { actionId, rev: blockState.latestRev };
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/** The committed content a read at `pin` sees — the highest materialized revision at or below
|
|
1024
|
+
* it — or the latest revision's content when the read is unpinned. */
|
|
1025
|
+
function committedAt(blockState: BlockState, pin: number | undefined): { block: IBlock, rev: number } | undefined {
|
|
1026
|
+
if (pin !== undefined) return latestMaterializedAt(blockState, pin);
|
|
1027
|
+
const block = blockState.materializedBlocks.get(blockState.latestRev);
|
|
1028
|
+
return block ? { block, rev: blockState.latestRev } : undefined;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** Returns the block as it stood at the highest revision ≤ the given revision that committed it,
|
|
1032
|
+
* together with that revision — the caller reports it as {@link GetBlockResult.materialized} — or
|
|
1033
|
+
* `undefined` when that revision deleted it, as storage answers a tombstone: a committed revision
|
|
1034
|
+
* with no materialized block is a delete, never a reason to serve the content before it. */
|
|
1035
|
+
function latestMaterializedAt(blockState: BlockState, maxRev: number): { block: IBlock, rev: number } | undefined {
|
|
1036
|
+
for (let rev = maxRev; rev >= 0; rev--) {
|
|
1037
|
+
const block = blockState.materializedBlocks.get(rev);
|
|
1038
|
+
if (block) return { block, rev };
|
|
1039
|
+
if (blockState.revisionActions.has(rev)) return undefined;
|
|
1040
|
+
}
|
|
1041
|
+
return undefined;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* `applyTransform` over cloned inputs, so a returned block never aliases stored state.
|
|
1046
|
+
*
|
|
1047
|
+
* The `insert` clone here is not redundant with `transformForBlockId`'s: callers pass `Transform`
|
|
1048
|
+
* values pulled straight out of `blockState.pendingActions` (a `Map<ActionId, Transform>` written
|
|
1049
|
+
* once by `pend()`), which can be `get()`'d more than once and read again at commit time before
|
|
1050
|
+
* being superseded. `applyTransform` mutates `transform.insert` in place when `updates` ride
|
|
1051
|
+
* along, so cloning immediately before each call is what keeps that stored entry pristine across
|
|
1052
|
+
* repeated reads.
|
|
1053
|
+
*
|
|
1054
|
+
* An absent base is NOT a short circuit: an insert needs no base — `applyTransform` adopts it as
|
|
1055
|
+
* the block. Bailing out on `!block` made this double silently drop a pending-only insert read
|
|
1056
|
+
* through the pending overlay, the one shape `StorageRepo.get` serves with content but no
|
|
1057
|
+
* `materialized` (see docs/internals.md § the `unavailable`/`materialized` bullets). A
|
|
1058
|
+
* pending UPDATE over an absent base still resolves to undefined, matching the real repo.
|
|
1059
|
+
*/
|
|
1060
|
+
function applyTransformSafe(block: IBlock | undefined, transform: Transform): IBlock | undefined {
|
|
1061
|
+
return applyTransform(
|
|
1062
|
+
block ? structuredClone(block) : undefined,
|
|
1063
|
+
transform.insert ? { ...transform, insert: structuredClone(transform.insert) } : transform
|
|
1064
|
+
);
|
|
1065
|
+
}
|