@serfab/cadre-core 1.23.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 +216 -294
- package/dist/arachnode-stub.d.ts +12 -4
- package/dist/arachnode-stub.js +15 -8
- package/dist/arachnode-stub.js.map +1 -1
- package/dist/cadre-invite-protocol.js +5 -4
- package/dist/cadre-invite-protocol.js.map +1 -1
- package/dist/cadre-node.d.ts +145 -44
- package/dist/cadre-node.js +239 -79
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +14 -2
- package/dist/control-authorization.js +8 -3
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +27 -13
- package/dist/control-cohort.js +21 -14
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +18 -16
- package/dist/control-database.js +57 -22
- package/dist/control-database.js.map +1 -1
- package/dist/control-protocol-guard.d.ts +1 -1
- package/dist/control-protocol-guard.js +7 -3
- package/dist/control-protocol-guard.js.map +1 -1
- package/dist/control-read-retry.d.ts +49 -11
- package/dist/control-read-retry.js +56 -15
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +32 -3
- package/dist/control-retry.js +35 -4
- package/dist/control-retry.js.map +1 -1
- package/dist/control-schema-version.d.ts +5 -5
- package/dist/control-schema-version.js +5 -5
- package/dist/control-schema-version.js.map +1 -1
- package/dist/control-schema.d.ts +5 -5
- package/dist/control-schema.js +1032 -1011
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +54 -65
- package/dist/control-write-retry.js +134 -158
- package/dist/control-write-retry.js.map +1 -1
- package/dist/delegate-admission.d.ts +1 -1
- package/dist/delegate-admission.js +1 -1
- package/dist/edge-node/config.d.ts +4 -3
- package/dist/edge-node/config.js +11 -9
- package/dist/edge-node/config.js.map +1 -1
- package/dist/edge-node/dissolve.d.ts +4 -4
- package/dist/edge-node/dissolve.js +4 -4
- package/dist/edge-node/edge-join.d.ts +1 -1
- package/dist/edge-node/edge-join.js +1 -1
- package/dist/edge-node/edge-node.js +2 -2
- package/dist/edge-node/edge-node.js.map +1 -1
- package/dist/edge-node/index.d.ts +1 -0
- package/dist/edge-node/index.js +1 -0
- package/dist/edge-node/index.js.map +1 -1
- package/dist/edge-node/join-credential.d.ts +1 -1
- package/dist/edge-node/join-credential.js +1 -1
- package/dist/edge-node/lazy-raw-storage.js +12 -0
- package/dist/edge-node/lazy-raw-storage.js.map +1 -1
- package/dist/edge-node/listen-addrs.d.ts +21 -0
- package/dist/edge-node/listen-addrs.js +25 -0
- package/dist/edge-node/listen-addrs.js.map +1 -0
- package/dist/edge-node/owner-grant.d.ts +1 -1
- package/dist/edge-node/owner-grant.js +1 -1
- package/dist/edge-node/platform.d.ts +5 -4
- package/dist/edge-node/platform.js.map +1 -1
- package/dist/edge-node/strand-move.d.ts +1 -1
- package/dist/edge-node/strand-move.js +1 -1
- package/dist/edge-node/strands.js +27 -11
- package/dist/edge-node/strands.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/dist/invitation-bootstrap.d.ts +28 -5
- package/dist/invitation-bootstrap.js +41 -8
- package/dist/invitation-bootstrap.js.map +1 -1
- package/dist/link-budget.d.ts +53 -4
- package/dist/link-budget.js +58 -5
- package/dist/link-budget.js.map +1 -1
- package/dist/machine-removal.d.ts +2 -2
- package/dist/machine-removal.js +4 -3
- package/dist/machine-removal.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +2 -2
- package/dist/membership-connection-gater.js +2 -2
- package/dist/membership-view.d.ts +2 -2
- package/dist/membership-view.js +2 -2
- package/dist/node-claim-payload.d.ts +15 -7
- package/dist/node-claim-payload.js +25 -9
- package/dist/node-claim-payload.js.map +1 -1
- package/dist/node-class.d.ts +36 -0
- package/dist/node-class.js +26 -0
- package/dist/node-class.js.map +1 -0
- package/dist/node-default-transports.d.ts +50 -3
- package/dist/node-default-transports.js +48 -3
- package/dist/node-default-transports.js.map +1 -1
- package/dist/node-roles.d.ts +118 -0
- package/dist/node-roles.js +142 -0
- package/dist/node-roles.js.map +1 -0
- package/dist/node-transports.d.ts +6 -2
- package/dist/node-transports.js +39 -7
- package/dist/node-transports.js.map +1 -1
- package/dist/owner-anchor-sync.js +1 -1
- package/dist/owner-key-export.js +1 -1
- package/dist/owner-key-flows.d.ts +1 -1
- package/dist/owner-key-store.js +1 -1
- package/dist/owner-signer.js +1 -1
- package/dist/peer-authorization.d.ts +3 -1
- package/dist/peer-authorization.js +3 -1
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-dial.d.ts +74 -10
- package/dist/peer-dial.js +89 -17
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.js +4 -4
- package/dist/peer-join-backfill.js.map +1 -1
- package/dist/peer-record.d.ts +34 -9
- package/dist/peer-record.js +66 -13
- package/dist/peer-record.js.map +1 -1
- package/dist/pending-join-runner.d.ts +1 -1
- package/dist/pending-join-runner.js +1 -1
- package/dist/provider/client.d.ts +6 -5
- package/dist/provider/client.js +3 -3
- package/dist/provider/client.js.map +1 -1
- package/dist/provider/descriptor.d.ts +1 -1
- package/dist/provider/descriptor.js +1 -1
- package/dist/provider/oauth.d.ts +1 -1
- package/dist/provider/oauth.js +1 -1
- package/dist/provider/rent-node.d.ts +4 -3
- package/dist/provider/rent-node.js +3 -3
- package/dist/provider/rent-node.js.map +1 -1
- package/dist/relay-addrs.d.ts +62 -7
- package/dist/relay-addrs.js +74 -11
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +1 -1
- package/dist/relay-reservation.js +1 -1
- package/dist/relay-server.d.ts +6 -5
- package/dist/relay-server.js +5 -5
- package/dist/relay-server.js.map +1 -1
- package/dist/schema-verification.d.ts +13 -10
- package/dist/schema-verification.js +32 -24
- package/dist/schema-verification.js.map +1 -1
- package/dist/sealed-party-key.js +1 -1
- package/dist/seed-bootstrap.d.ts +4 -3
- package/dist/seed-bootstrap.js +6 -7
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +8 -6
- package/dist/strand-addr-protocol.js +12 -10
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-app-schema-report.d.ts +16 -2
- package/dist/strand-app-schema-report.js +15 -0
- package/dist/strand-app-schema-report.js.map +1 -1
- package/dist/strand-cohort.d.ts +1 -4
- package/dist/strand-cohort.js.map +1 -1
- package/dist/strand-commit-durability.d.ts +1 -1
- package/dist/strand-database.d.ts +13 -6
- package/dist/strand-database.js +17 -9
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +3 -2
- package/dist/strand-first-sync-gate.js +3 -2
- package/dist/strand-first-sync-gate.js.map +1 -1
- package/dist/strand-formation-deadlines.d.ts +2 -2
- package/dist/strand-formation-deadlines.js +2 -2
- package/dist/strand-formation-protocol.d.ts +1 -1
- package/dist/strand-formation-protocol.js +51 -16
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +67 -33
- package/dist/strand-instance-manager.js +164 -55
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +8 -3
- package/dist/strand-membership-reconciler.js +8 -3
- package/dist/strand-membership-reconciler.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +2 -2
- package/dist/strand-membership-writer.js +41 -39
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-move.js +1 -1
- package/dist/strand-network-config.d.ts +15 -5
- package/dist/strand-network-config.js +7 -6
- package/dist/strand-network-config.js.map +1 -1
- package/dist/strand-party-key-rotation.d.ts +7 -3
- package/dist/strand-party-key-rotation.js +7 -3
- package/dist/strand-party-key-rotation.js.map +1 -1
- package/dist/strand-party-machines.d.ts +19 -8
- package/dist/strand-party-machines.js +26 -11
- package/dist/strand-party-machines.js.map +1 -1
- package/dist/strand-party-peers.d.ts +36 -14
- package/dist/strand-party-peers.js +24 -15
- package/dist/strand-party-peers.js.map +1 -1
- package/dist/strand-peer-redial.d.ts +1 -1
- package/dist/strand-peer-redial.js +1 -1
- package/dist/strand-reactivity.d.ts +20 -17
- package/dist/strand-reactivity.js +31 -18
- package/dist/strand-reactivity.js.map +1 -1
- package/dist/strand-revocation-enforcer.d.ts +3 -3
- package/dist/strand-revocation-enforcer.js +3 -3
- package/dist/strand-wake-protocol.d.ts +19 -7
- package/dist/strand-wake-protocol.js +22 -10
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.d.ts +1 -1
- package/dist/strand-watcher.js +1 -1
- package/dist/table-change-watch.js +1 -1
- package/dist/types.d.ts +116 -55
- package/dist/types.js.map +1 -1
- package/dist/webrtc-upgrade.d.ts +254 -0
- package/dist/webrtc-upgrade.js +355 -0
- package/dist/webrtc-upgrade.js.map +1 -0
- package/package.json +9 -9
- package/src/announce-addrs.ts +93 -93
- package/src/arachnode-stub.ts +18 -10
- package/src/bootstrap-peer-store-file.ts +44 -44
- package/src/bootstrap-peer-store.ts +214 -214
- package/src/cadre-invite-protocol.ts +741 -740
- package/src/cadre-node.ts +12462 -12280
- package/src/claim-proof.ts +147 -147
- package/src/control-authorization.ts +290 -276
- package/src/control-cohort.ts +223 -204
- package/src/control-database.ts +72 -28
- package/src/control-formation-recorder.ts +359 -359
- package/src/control-protocol-guard.ts +370 -366
- package/src/control-read-retry.ts +222 -180
- package/src/control-retry.ts +36 -4
- package/src/control-schema-version.ts +301 -301
- package/src/control-schema.ts +1108 -1087
- package/src/control-write-retry.ts +639 -669
- package/src/delegate-admission.ts +351 -351
- package/src/describe-thrown.ts +60 -60
- package/src/ed25519-key.ts +185 -185
- package/src/edge-node/config.ts +13 -10
- package/src/edge-node/dissolve.ts +252 -252
- package/src/edge-node/edge-join.ts +320 -320
- package/src/edge-node/edge-node.ts +1095 -1095
- package/src/edge-node/index.ts +60 -59
- package/src/edge-node/join-credential.ts +16 -16
- package/src/edge-node/lazy-raw-storage.ts +194 -178
- package/src/edge-node/listen-addrs.ts +26 -0
- package/src/edge-node/node-local.ts +84 -84
- package/src/edge-node/options.ts +195 -195
- package/src/edge-node/owner-grant.ts +63 -63
- package/src/edge-node/platform.ts +5 -4
- package/src/edge-node/strand-move.ts +266 -266
- package/src/edge-node/strands.ts +27 -12
- package/src/enrolled-machine-store-file.ts +43 -43
- package/src/enrolled-machine-store.ts +280 -280
- package/src/fetch-impl.ts +23 -23
- package/src/file-durable-slot.ts +64 -64
- package/src/file-safe-name.ts +32 -32
- package/src/formation-approval.ts +604 -604
- package/src/formation-token.ts +63 -63
- package/src/fs-atomic.ts +92 -92
- package/src/index.ts +987 -957
- package/src/invitation-bootstrap.ts +152 -118
- package/src/joined-strand-store.ts +553 -553
- package/src/key-store-file.ts +142 -142
- package/src/link-budget.ts +61 -5
- package/src/machine-removal.ts +211 -210
- package/src/member-redemption.ts +643 -643
- package/src/membership-connection-gater.ts +650 -650
- package/src/membership-view.ts +147 -147
- package/src/node-claim-payload.ts +34 -12
- package/src/node-class.ts +47 -0
- package/src/node-default-transports.ts +65 -3
- package/src/node-roles.ts +220 -0
- package/src/node-transports.ts +86 -49
- package/src/owner-anchor-sync.ts +172 -172
- package/src/owner-grant-protocol.ts +547 -547
- package/src/owner-key-export.ts +348 -348
- package/src/owner-key-flows.ts +109 -109
- package/src/owner-key-store.ts +1 -1
- package/src/owner-keys.ts +179 -179
- package/src/owner-signer.ts +92 -92
- package/src/peer-addr-book.ts +252 -252
- package/src/peer-authorization.ts +584 -582
- package/src/peer-dial.ts +698 -605
- package/src/peer-join-backfill.ts +961 -961
- package/src/peer-record.ts +71 -16
- package/src/pending-join-runner.ts +798 -798
- package/src/primary-lan-address.ts +47 -47
- package/src/provider/client.ts +327 -326
- package/src/provider/descriptor.ts +129 -129
- package/src/provider/index.ts +59 -59
- package/src/provider/oauth.ts +285 -285
- package/src/provider/rent-node.ts +276 -275
- package/src/relay-addrs.ts +90 -12
- package/src/relay-reservation.ts +1 -1
- package/src/relay-server.ts +99 -98
- package/src/schema-verification.ts +38 -23
- package/src/sealed-party-key.ts +190 -190
- package/src/seed-bootstrap.ts +8 -11
- package/src/seed-trust-policy.ts +353 -353
- package/src/self-address-update.ts +19 -19
- package/src/strand-addr-protocol.ts +17 -14
- package/src/strand-app-schema-report.ts +147 -122
- package/src/strand-cohort.ts +1 -5
- package/src/strand-commit-durability.ts +165 -165
- package/src/strand-database.ts +522 -510
- package/src/strand-first-sync-gate.ts +3 -2
- package/src/strand-formation-deadlines.ts +227 -227
- package/src/strand-formation-manager.ts +731 -731
- package/src/strand-formation-protocol.ts +1174 -1138
- package/src/strand-fret-addrs.ts +179 -179
- package/src/strand-instance-manager.ts +188 -63
- package/src/strand-membership-reconciler.ts +1315 -1310
- package/src/strand-membership-writer.ts +2152 -2150
- package/src/strand-move.ts +1 -1
- package/src/strand-network-config.ts +16 -6
- package/src/strand-offline.ts +178 -178
- package/src/strand-party-key-rotation.ts +440 -436
- package/src/strand-party-machines.ts +308 -293
- package/src/strand-party-peers.ts +151 -129
- package/src/strand-peer-redial.ts +1 -1
- package/src/strand-reactivity.ts +36 -19
- package/src/strand-revocation-enforcer.ts +758 -758
- package/src/strand-wake-protocol.ts +406 -394
- package/src/strand-watcher.ts +1 -1
- package/src/table-change-watch.ts +285 -285
- package/src/timed-step.ts +28 -28
- package/src/timeout-scheduler.ts +29 -29
- package/src/trusted-owner-store.ts +242 -242
- package/src/types.ts +3182 -3121
- package/src/webrtc-upgrade.ts +439 -0
|
@@ -1,669 +1,639 @@
|
|
|
1
|
-
import { CoordinatorPartialCommitError, SyncRetryExhaustedError, TornActionError } from '@optimystic/db-core';
|
|
2
|
-
import { ConcurrentModificationError, PartialCommitError } from '@optimystic/quereus-plugin-optimystic';
|
|
3
|
-
import { causeChain, chainMessages, reportsUnreachableCohort, retryControlOperation } from './control-retry.js';
|
|
4
|
-
import type { ControlRetryOptions } from './control-retry.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Bounded retry for TRANSIENT control-write failures — the classifier and loop behind
|
|
8
|
-
* `ControlDatabase.lockedWithRetry`, the single funnel every local control write passes
|
|
9
|
-
* through.
|
|
10
|
-
*
|
|
11
|
-
* Why this exists: the control database replicates every block to the whole party, so a
|
|
12
|
-
* three-machine party commits control writes unanimously (`ceil(3 × 0.75) = 3` approvals) —
|
|
13
|
-
* and no `superMajorityThreshold` can lower that bar without violating Optimystic's
|
|
14
|
-
* partition-safety condition at the shipped admission fraction (the arithmetic lives in
|
|
15
|
-
* `docs/
|
|
16
|
-
* and absorbs the transient half of its cost here: a write that failed because the cohort
|
|
17
|
-
* did not ANSWER (a stream reset mid-connection-formation, zero approvals) is re-presented
|
|
18
|
-
* a moment later, which the degraded-cohort-member scenario measured as safe (a failed
|
|
19
|
-
* write rolls back, nothing half-commits) and effective (the next write commits in ~1 s).
|
|
20
|
-
*
|
|
21
|
-
* A write somebody actually REJECTED is never retried on the strength of the rejection itself —
|
|
22
|
-
*
|
|
23
|
-
* re-present a spent signature against a
|
|
24
|
-
* anyway when they arrive inside the
|
|
25
|
-
* {@link isUncommittedTransactorAggregate}
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* accepted-tradeoff `NOTE:` there for which, and why no
|
|
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
|
-
*
|
|
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
|
-
* the
|
|
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
|
-
|
|
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
|
-
* emits
|
|
282
|
-
* `
|
|
283
|
-
*
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* -
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* so
|
|
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
|
-
*
|
|
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
|
-
* super-majority shortfall
|
|
499
|
-
* (
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
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
|
-
* The
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*/
|
|
598
|
-
export const
|
|
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
|
-
* `ControlDatabase.withWriteLock`). The backoff sleeps happen with NO lock held; the caller
|
|
641
|
-
* takes and releases its lock inside `attempt`.
|
|
642
|
-
*
|
|
643
|
-
* On exhaustion (attempts or budget) the LAST error is rethrown unchanged — never wrapped,
|
|
644
|
-
* so the exact messages downstream code and the degraded-cohort-member scenario assert on
|
|
645
|
-
* survive. A non-retriable failure propagates from the attempt that raised it.
|
|
646
|
-
*
|
|
647
|
-
* Either way — declined or exhausted — an `options.onAbandon` observer is notified exactly
|
|
648
|
-
* once before the rethrow (a `ControlRetryAbandonment`). Only WRITES pass one: an abandoned
|
|
649
|
-
* read throws to a caller that is awaiting it, so nothing is lost silently there
|
|
650
|
-
* (`control-read-retry.ts` says so at its own seam), while an abandoned write may have no
|
|
651
|
-
* caller at all — a background self-record republish is `void`-ed with a `debug` catch, so
|
|
652
|
-
* its failure reaches nobody unless something is listening here.
|
|
653
|
-
* `ControlDatabase.setControlWriteAbandonedListener` is the only thing that passes one in
|
|
654
|
-
* production; `retryControlWrite` itself neither supplies nor requires an observer.
|
|
655
|
-
*/
|
|
656
|
-
export function retryControlWrite<T>(
|
|
657
|
-
attempt: () => Promise<T>,
|
|
658
|
-
options: ControlWriteRetryOptions = {}
|
|
659
|
-
): Promise<T> {
|
|
660
|
-
return retryControlOperation(attempt, {
|
|
661
|
-
attempts: CONTROL_WRITE_ATTEMPTS,
|
|
662
|
-
delaysMs: CONTROL_WRITE_RETRY_DELAYS_MS,
|
|
663
|
-
budgetMs: CONTROL_WRITE_RETRY_BUDGET_MS,
|
|
664
|
-
isRetriable: isRetriableControlWriteFailure,
|
|
665
|
-
// The default prefix, named anyway: the degraded-cohort scenario asserts on
|
|
666
|
-
// `Control write [<label>] …` lines byte-for-byte.
|
|
667
|
-
logPrefix: 'Control write',
|
|
668
|
-
}, options);
|
|
669
|
-
}
|
|
1
|
+
import { CoordinatorPartialCommitError, PeersIncompleteError, SyncRetryExhaustedError, TornActionError } from '@optimystic/db-core';
|
|
2
|
+
import { ConcurrentModificationError, PartialCommitError } from '@optimystic/quereus-plugin-optimystic';
|
|
3
|
+
import { causeChain, chainMessages, reportsUnansweredShortfall, reportsUnreachableCohort, retryControlOperation } from './control-retry.js';
|
|
4
|
+
import type { ControlRetryOptions } from './control-retry.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Bounded retry for TRANSIENT control-write failures — the classifier and loop behind
|
|
8
|
+
* `ControlDatabase.lockedWithRetry`, the single funnel every local control write passes
|
|
9
|
+
* through.
|
|
10
|
+
*
|
|
11
|
+
* Why this exists: the control database replicates every block to the whole party, so a
|
|
12
|
+
* three-machine party commits control writes unanimously (`ceil(3 × 0.75) = 3` approvals) —
|
|
13
|
+
* and no `superMajorityThreshold` can lower that bar without violating Optimystic's
|
|
14
|
+
* partition-safety condition at the shipped admission fraction (the arithmetic lives in
|
|
15
|
+
* `docs/cadre/control-replication.md` → "Replication cluster size"). Cadre therefore accepts unanimity
|
|
16
|
+
* and absorbs the transient half of its cost here: a write that failed because the cohort
|
|
17
|
+
* did not ANSWER (a stream reset mid-connection-formation, zero approvals) is re-presented
|
|
18
|
+
* a moment later, which the degraded-cohort-member scenario measured as safe (a failed
|
|
19
|
+
* write rolls back, nothing half-commits) and effective (the next write commits in ~1 s).
|
|
20
|
+
*
|
|
21
|
+
* A write somebody actually REJECTED is never retried on the strength of the rejection itself —
|
|
22
|
+
* nothing here claims a rejection, at any phase (the shortfall claim reads the vote count for
|
|
23
|
+
* exactly this reason), because re-presenting one would re-present a spent signature against a
|
|
24
|
+
* cohort that already said no. Rejections are re-presented anyway when they arrive inside the
|
|
25
|
+
* transactor's promise-phase aggregate, which {@link isUncommittedTransactorAggregate}
|
|
26
|
+
* claims on the PHASE alone, whatever the cause inside says. Safe because nothing has committed
|
|
27
|
+
* at that phase, and deliberately kept — one of the remaining promise-phase rejections is exactly
|
|
28
|
+
* the kind a re-presentation fixes. See the accepted-tradeoff `NOTE:` there for which, and why no
|
|
29
|
+
* blanket rejection veto is wanted.
|
|
30
|
+
*
|
|
31
|
+
* A failure that says some of the write may be STORED is a different matter, and never
|
|
32
|
+
* re-presented whatever text it carries: {@link reportsPossiblyStoredWrite} vetoes it by type
|
|
33
|
+
* before any matcher runs. The one half-landed write that IS re-presented is a torn write the
|
|
34
|
+
* library marks final — not saved and unable to land ({@link isFinalTornWrite}).
|
|
35
|
+
*
|
|
36
|
+
* THREE policies ship from here, and the split is deliberate: the default
|
|
37
|
+
* ({@link isRetriableControlWriteFailure}, {@link CONTROL_WRITE_ATTEMPTS}) sits under every
|
|
38
|
+
* control write; {@link SCHEMA_INIT_RETRY_POLICY} is opted into by
|
|
39
|
+
* `ControlDatabase.applyControlSchema` alone; {@link LOST_UPDATE_RETRY_POLICY} is opted into by
|
|
40
|
+
* the `CadrePeer` writers whose bodies re-read the row and decide again. Each widened one absorbs
|
|
41
|
+
* one extra failure class whose re-run safety holds for its opted-in bodies and NOT for writes in
|
|
42
|
+
* general; anything added to the default list must be safe for all ~19 callers.
|
|
43
|
+
*
|
|
44
|
+
* Lives outside `control-database.ts` so a spec can drive the classifier against real engine
|
|
45
|
+
* errors without importing the whole database class. The loop itself lives in
|
|
46
|
+
* `control-retry.ts`, shared with the READ policy (`control-read-retry.ts`) — this module
|
|
47
|
+
* owns the write policies: their classifiers, attempt counts, backoff and budget.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Attempts allowed per control write: the first, plus two retries of a transiently-failed
|
|
52
|
+
* cluster commit. Bounded so a genuinely unreachable cohort terminates in the transactor's
|
|
53
|
+
* own error rather than spinning; two retries covers the observed failure (a stream still
|
|
54
|
+
* forming when the write fired) with margin.
|
|
55
|
+
*/
|
|
56
|
+
export const CONTROL_WRITE_ATTEMPTS = 3;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Attempts allowed for {@link SCHEMA_INIT_RETRY_POLICY}, the one call site that also absorbs a
|
|
60
|
+
* self-coordination refusal.
|
|
61
|
+
*
|
|
62
|
+
* Two more than {@link CONTROL_WRITE_ATTEMPTS} because what this retry buys is wall-clock for a
|
|
63
|
+
* cold-starting node's bootstrap dials to land, and because the refusal is cheap to re-provoke
|
|
64
|
+
* relative to a cluster round-trip: `findCoordinator` already spends its own bounded wait before
|
|
65
|
+
* raising (3 attempts, 500 ms apart, `db-p2p/src/libp2p-key-network.ts` ~422) and then decides
|
|
66
|
+
* LOCALLY that it may not elect itself, so no peer is contacted and no cohort deadline is
|
|
67
|
+
* consumed.
|
|
68
|
+
*
|
|
69
|
+
* Note that the ~1 s `findCoordinator` spends internally is real, on top of this policy's own
|
|
70
|
+
* backoff — which is exactly why {@link CONTROL_WRITE_RETRY_BUDGET_MS} and not this count is what
|
|
71
|
+
* actually terminates the loop in the worst case.
|
|
72
|
+
*/
|
|
73
|
+
export const SCHEMA_INIT_ATTEMPTS = 5;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Elapsed-time ceiling on the whole retry loop, measured from the START of the first
|
|
77
|
+
* attempt and checked after a failed attempt BEFORE sleeping.
|
|
78
|
+
*
|
|
79
|
+
* This budget — not the attempt count — is what makes the policy safe to sit under every
|
|
80
|
+
* control write. A transient failure (stream reset while a connection is still forming)
|
|
81
|
+
* surfaces in well under a second, so it is retried and the loop adds at most ~2.2 s. A
|
|
82
|
+
* genuinely silent cohort member costs ~21 s per consensus round (two `ClusterClient`
|
|
83
|
+
* response-deadline attempts, 10.5 s each at the default declared link): one pend round plus
|
|
84
|
+
* the cancel discharge's three rounds, about 84 s for one write in
|
|
85
|
+
* `control-write-degraded-cohort-member.integration.ts`, which already exceeds this budget
|
|
86
|
+
* when attempt 1 returns — so that case is surfaced immediately and retry adds ZERO latency
|
|
87
|
+
* to the case where it cannot help.
|
|
88
|
+
*
|
|
89
|
+
* It cuts the loop off by design, so it does not grow with the link: its job is to end the
|
|
90
|
+
* retries of slow attempts, and the caller gets the last attempt's error unchanged. An attempt
|
|
91
|
+
* whose read phase consulted a silent peer ends at about one per-peer read deadline
|
|
92
|
+
* (`cohortReadDeadlineMs` in `link-budget.ts`: 7 s at the default declared link, 5 s before it
|
|
93
|
+
* was derived, 1 s at Optimystic's own default), so it still gets one retry inside this budget
|
|
94
|
+
* where it once got two; an attempt that runs into the `ClusterClient` response deadline
|
|
95
|
+
* (10.5 s at the default declared link) still gets none. See `docs/network/deadlines.md` →
|
|
96
|
+
* "Deadlines over Optimystic's reads and commits".
|
|
97
|
+
*
|
|
98
|
+
* NOTE: a failed commit attempt also pays a cancel discharge before its error returns
|
|
99
|
+
* (optimystic `TransactorSource.transact`'s catch) — bounded by six rounds and
|
|
100
|
+
* `abortOrCancelTimeoutMs`, which every collection this repo opens sets to the larger of 5 s
|
|
101
|
+
* and the RPC dial deadline: 42.5 s at the default declared link (`optimysticDialLimits` in
|
|
102
|
+
* `link-budget.ts`;
|
|
103
|
+
* `../optimystic/packages/quereus-plugin-optimystic/src/optimystic-adapter/collection-factory.ts`).
|
|
104
|
+
* One cancel that runs its full budget therefore ends the retries on its own, cutting the
|
|
105
|
+
* three-attempt policy to however many attempts came before it. A cancel only runs that long
|
|
106
|
+
* when a peer stays silent, and that attempt has already spent its pend round past this
|
|
107
|
+
* budget, so the cut loses nothing there. After a transient fault the cancel is either
|
|
108
|
+
* answered or gives up after its six rounds against a dead transport, in about a second
|
|
109
|
+
* either way (optimystic `NetworkTransactor.MAX_CANCEL_ROUNDS`). Not observed — every
|
|
110
|
+
* measured round of the transient-reset case committed on attempt 3 of 3 — so this is a
|
|
111
|
+
* condition to watch, not work to do: if that case ever starts failing with
|
|
112
|
+
* `failed after 2/3 attempt(s)`, the cancel discharge is where the time went.
|
|
113
|
+
*/
|
|
114
|
+
// eslint-disable-next-line no-restricted-syntax -- cuts off by design: it ends the retries of slow attempts rather than waiting them out; see docs/network/deadlines.md → "Deadlines over Optimystic's reads and commits"
|
|
115
|
+
export const CONTROL_WRITE_RETRY_BUDGET_MS = 10_000;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Backoff before retry N, jittered ±50% (so the first sleep is 125–375 ms — the jitter can
|
|
119
|
+
* never floor a delay to zero). Each single sleep is additionally capped at the LARGEST
|
|
120
|
+
* delay in the list, so a `stop()` racing a backoff is never held up by more than ~1 s.
|
|
121
|
+
* There is no `AbortSignal` at this seam to cancel a sleep outright; the cap is the
|
|
122
|
+
* substitute, deliberately not plumbing one through every write path.
|
|
123
|
+
*/
|
|
124
|
+
const CONTROL_WRITE_RETRY_DELAYS_MS: readonly number[] = [250, 1_000];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Backoff for {@link SCHEMA_INIT_RETRY_POLICY}, jittered and capped the same way. Worst case
|
|
128
|
+
* ~4.6 s of sleep across four retries (375 + 750 + 1500 + 2000, each capped at the largest base),
|
|
129
|
+
* which stays inside {@link CONTROL_WRITE_RETRY_BUDGET_MS} — the budget still terminates the loop
|
|
130
|
+
* first if any single attempt is itself slow.
|
|
131
|
+
*
|
|
132
|
+
* Sized against measured facts, not guessed:
|
|
133
|
+
*
|
|
134
|
+
* - the self-coordination guard clears the moment the node has ONE connection again (both
|
|
135
|
+
* branches that raise `grace-period-not-elapsed` require `getConnections().length === 0`), not
|
|
136
|
+
* when the 30 s grace period expires — so this does NOT need to cover the grace period, only
|
|
137
|
+
* the gap until a bootstrap dial completes;
|
|
138
|
+
* - the guard's 30 s `gracePeriodMs` cannot be tuned from this repo anyway: no caller in either
|
|
139
|
+
* repo passes a `SelfCoordinationConfig`, so the default is always in force;
|
|
140
|
+
* - a node that has NEVER connected is waved through as a bootstrap node (network high-water
|
|
141
|
+
* mark of 1), so this retry only ever delays a node that HAS seen peers. One whose peers are
|
|
142
|
+
* gone for good still fails, ~4.6 s later — which is why the budget stays at the shared
|
|
143
|
+
* ceiling rather than stretching to cover the full 30 s window.
|
|
144
|
+
*/
|
|
145
|
+
const SCHEMA_INIT_RETRY_DELAYS_MS: readonly number[] = [250, 500, 1_000, 2_000];
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Does any link of the `cause` chain report a transactor aggregate from a phase where nothing can
|
|
149
|
+
* have committed yet?
|
|
150
|
+
*
|
|
151
|
+
* `NetworkTransactor` composes one `PeersIncompleteError` for all four of its operations and says
|
|
152
|
+
* which in `phase` (`db-core/src/transactor/peers-incomplete-error.ts`). Only two of the four are
|
|
153
|
+
* safe to re-present:
|
|
154
|
+
*
|
|
155
|
+
* - `get` (a block read, which a write body also performs) and `pend` (phase 1) fail before
|
|
156
|
+
* anything commits, so the write is known not to have landed. `get` raises it only when a
|
|
157
|
+
* coordinator answered and left a block out; a block nobody answered for fails typed
|
|
158
|
+
* instead, which {@link matchesRetriableFailure} claims ({@link reportsUnreachableCohort});
|
|
159
|
+
* - `commit` (phase 2) is REACHABLE here and NOT safe. `commitBlock` throws the
|
|
160
|
+
* aggregate when a header/tail commit got NO response at all — precisely the transient
|
|
161
|
+
* class this retry targets — and that escapes `commit()`, survives `TransactorSource`'s
|
|
162
|
+
* cancel-and-rethrow (cancel reaches PENDING actions only; a peer that already committed
|
|
163
|
+
* stays committed) and `Collection.syncInternal` (which retries StaleFailure RETURN values,
|
|
164
|
+
* not throws), reaching this funnel QuereusError-wrapped. The tail commit is one batch to
|
|
165
|
+
* one coordinator running consensus internally, so a no-response there is INDETERMINATE:
|
|
166
|
+
* the commit may have completed with only the response lost. Re-running the write body over
|
|
167
|
+
* a write that landed turns a success into a constraint failure (e.g.
|
|
168
|
+
* `UNIQUE constraint failed: CadrePeer.PeerId`). Surfacing the transient error instead lets
|
|
169
|
+
* the caller re-read committed state and decide. The commit phase is vetoed by
|
|
170
|
+
* {@link reportsIndeterminateCommit}, not here;
|
|
171
|
+
* - `cancel` — a failed attempt's own cancel that could not discharge the pend it left behind —
|
|
172
|
+
* is neither claimed here nor vetoed there, so it falls through unmatched and is not retried.
|
|
173
|
+
* Per LINK, not per chain: a cancel aggregate carrying a `get`/`pend` one below it is still
|
|
174
|
+
* claimed on that lower link, which is right for the same reason the lower link alone is
|
|
175
|
+
* (nothing committed at either phase) and is what the text matcher this replaced also did.
|
|
176
|
+
*
|
|
177
|
+
* What still fails CLOSED is the error not arriving: a future wrapper that drops `cause` takes
|
|
178
|
+
* every link below it with it, so no typed claim matches and the write is simply not retried.
|
|
179
|
+
* `control-write-degraded-cohort-member.integration.ts` turns that into a red assertion on a live
|
|
180
|
+
* aggregate; NOTE: on Optimystic 1.20.0 none of its cases produced an aggregate at all (2026-10-28:
|
|
181
|
+
* the injected reset failed the write's read typed, and a pend's shortfall arrives on its own), so
|
|
182
|
+
* that guard can pass without ever seeing one.
|
|
183
|
+
*
|
|
184
|
+
* NOTE: accepted tradeoff — a REJECTION raised while promises are still being collected reaches
|
|
185
|
+
* this repo wrapped in this same aggregate, and this matcher claims it on the phase alone,
|
|
186
|
+
* whatever the cause inside says. That re-presents such a rejection up to two more times inside
|
|
187
|
+
* the 10 s budget. Kept deliberately, and re-decided on 2026-09-17 against the current upstream
|
|
188
|
+
* behaviour rather than inherited:
|
|
189
|
+
*
|
|
190
|
+
* - Contention is no longer a rejection at all. "Another write holds this block right now"
|
|
191
|
+
* (`pending conflict`) is now a `held` verdict that counts toward neither approvals nor
|
|
192
|
+
* rejections (`validatePendOperations`,
|
|
193
|
+
* `../optimystic/packages/db-p2p/src/cluster/cluster-repo.ts`), so the rejection this tradeoff
|
|
194
|
+
* was originally written about does not arrive any more. It reaches this funnel as
|
|
195
|
+
* `SyncRetryExhaustedError` instead, which {@link reportsPossiblyStoredWrite} declines by type
|
|
196
|
+
* — upstream documents resubmitting after it as unsafe (the attempt that spent the budget may
|
|
197
|
+
* have left its log entry standing), and its collection sync has already spent ten retries on
|
|
198
|
+
* the race. The torn-write rule beside it: a `TornActionError` is re-presented only when
|
|
199
|
+
* `final` is true; any other torn write is declined.
|
|
200
|
+
* - What remains rejectable at the promise phase is stale revision, membership-not-admitted, an
|
|
201
|
+
* expired transaction, and a configured validator's refusal (`ClusterMember.evaluatePromise`).
|
|
202
|
+
* A block whose revision a member cannot establish is approved, not refused: the member logs
|
|
203
|
+
* `validation-block-unavailable` and votes on (`judgeStaleRevisions`). Stale revision is worth
|
|
204
|
+
* a retry: this loop re-runs the WHOLE write body, reads included
|
|
205
|
+
* (`ControlDatabase.withWriteLock`'s contract), so attempt 2 presents a pend against the
|
|
206
|
+
* revision that made attempt 1 stale. A blanket "decline any chain reporting a validator
|
|
207
|
+
* rejection" rule would remove that retry to save two wasted attempts on the others.
|
|
208
|
+
*
|
|
209
|
+
* Revisit if a re-presentation ever becomes expensive relative to what it buys — a rejection class
|
|
210
|
+
* that costs a full cohort round-trip to re-present, or a write body whose reads are no longer
|
|
211
|
+
* cheap — or when upstream offers a typed refusal surface to classify the rejection itself on.
|
|
212
|
+
* Contention is no longer the reason to revisit.
|
|
213
|
+
*
|
|
214
|
+
* An aggregate whose per-peer details came out EMPTY (possible when `formatBatchStatuses` has no
|
|
215
|
+
* batches to format) is claimed like any other: `phase` is on the error whether or not a batch
|
|
216
|
+
* formatted, and `get`/`pend` is itself the proof that nothing committed.
|
|
217
|
+
*
|
|
218
|
+
* Exported for the READ classifier (`control-read-retry.ts`), which reuses it verbatim, `pend`
|
|
219
|
+
* included: a control read presents no transaction, so it cannot produce a `pend` aggregate at
|
|
220
|
+
* all — claiming that phase there is unreachable rather than unsafe, and one shared predicate is
|
|
221
|
+
* one thing to keep correct instead of two.
|
|
222
|
+
*/
|
|
223
|
+
export function isUncommittedTransactorAggregate(links: readonly Error[]): boolean {
|
|
224
|
+
return links.some(link => link instanceof PeersIncompleteError
|
|
225
|
+
&& (link.phase === 'get' || link.phase === 'pend'));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Does any link of the failure's `cause` chain report a COMMIT-phase transactor aggregate — i.e.
|
|
230
|
+
* an outcome nobody can call committed or not?
|
|
231
|
+
*
|
|
232
|
+
* Vetoes the whole chain rather than the one link carrying the phase, and vetoes it regardless of
|
|
233
|
+
* which claim would otherwise have taken it: an indeterminate commit anywhere in the chain costs a
|
|
234
|
+
* retry we could have had instead of risking a re-run over a write that landed.
|
|
235
|
+
*
|
|
236
|
+
* Fails CLOSED in the only direction that matters: this veto and every TYPED claim the write
|
|
237
|
+
* classifier makes rest on the same `instanceof`, so a chain this cannot see (a wrapper that
|
|
238
|
+
* dropped `cause`, a second loaded copy of `@optimystic/db-core`) is a chain no typed claim can
|
|
239
|
+
* see either, and nothing is retried for the veto to have to stop.
|
|
240
|
+
*
|
|
241
|
+
* NOTE: that symmetry holds for every claim matched by type, which is all of them in the default
|
|
242
|
+
* and lost-update policies. Schema init adds one matched by TEXT
|
|
243
|
+
* ({@link isSelfCoordinationGraceRefusal}), and a commit aggregate raised OVER that refusal embeds
|
|
244
|
+
* it as the aggregate's own `; root: …` tail — so a chain that kept the text and lost the typed
|
|
245
|
+
* link would be claimed with this veto blind, where the text veto this replaced declined it. Fine
|
|
246
|
+
* today: every wrapper between the transactor and `applyControlSchema` passes the inner error as
|
|
247
|
+
* `cause` (Quereus' DDL, module-create and failed-apply wrappers all do). Revisit if one stops, or
|
|
248
|
+
* if the default policy ever grows a text matcher of its own — either way the veto then needs a
|
|
249
|
+
* text arm beside the typed one.
|
|
250
|
+
*/
|
|
251
|
+
function reportsIndeterminateCommit(links: readonly Error[]): boolean {
|
|
252
|
+
return links.some(link => link instanceof PeersIncompleteError && link.phase === 'commit');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Does any link of the `cause` chain say some of this write may be stored, or may still be?
|
|
257
|
+
* Such a failure is never re-presented — a re-run could store the write twice.
|
|
258
|
+
*
|
|
259
|
+
* - `SyncRetryExhaustedError` (and its `SyncRevisionStalledError` subclass): upstream documents
|
|
260
|
+
* resubmitting as unsafe, because the attempt that spent the budget may have left its log
|
|
261
|
+
* entry standing.
|
|
262
|
+
* - `TornActionError` without `final: true`: the write's log entry is stored and nobody could
|
|
263
|
+
* establish that the rest can never land — it may be saved already, or land later.
|
|
264
|
+
* - `CoordinatorPartialCommitError` / `PartialCommitError`: another collection or tree of the
|
|
265
|
+
* same transaction is already durable, and re-running the whole body would apply it twice.
|
|
266
|
+
*
|
|
267
|
+
* Every one of these embeds text from elsewhere in its message (a responder's refusal, the
|
|
268
|
+
* underlying failure), and upstream may chain that failure on `cause` besides — so this veto runs
|
|
269
|
+
* before any claim.
|
|
270
|
+
*
|
|
271
|
+
* Matched by TYPE: each survives the bridge's and Quereus' rewraps on `cause`. An error built by a
|
|
272
|
+
* second loaded copy of `@optimystic/db-core` (or of the plugin, for `PartialCommitError`) fails
|
|
273
|
+
* `instanceof`, and then this veto does not fire — but neither does any claim against that same
|
|
274
|
+
* copy, so nothing is retried for it to have to stop. The asymmetry with
|
|
275
|
+
* {@link isFinalTornWrite} is deliberate: there, a missed `instanceof` means no retry, which is the
|
|
276
|
+
* safe side. No text fallback parses `TornActionError`'s closing sentence, since upstream says its
|
|
277
|
+
* wording is for log lines only.
|
|
278
|
+
*
|
|
279
|
+
* NOTE: the plugin is registered through its `/plugin` entry (`control-database.ts`) but
|
|
280
|
+
* `PartialCommitError` is imported from its root entry; the two are one class only because the
|
|
281
|
+
* plugin's build emits both entries over one shared chunk. If upstream ever bundles the entries
|
|
282
|
+
* separately, this `instanceof` stops matching with every spec still green — import the class from
|
|
283
|
+
* the entry the plugin is registered through.
|
|
284
|
+
*/
|
|
285
|
+
function reportsPossiblyStoredWrite(links: readonly Error[]): boolean {
|
|
286
|
+
return links.some(link =>
|
|
287
|
+
link instanceof SyncRetryExhaustedError
|
|
288
|
+
|| (link instanceof TornActionError && link.final !== true)
|
|
289
|
+
|| link instanceof CoordinatorPartialCommitError
|
|
290
|
+
|| link instanceof PartialCommitError);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* A torn write the library marks FINAL: not saved, unable to land, its pending records confirmed
|
|
295
|
+
* cancelled — so submitting it again stores it once.
|
|
296
|
+
*
|
|
297
|
+
* Worth re-presenting, not merely safe to: upstream raises it mostly when a rival holds the
|
|
298
|
+
* revision this write's log entry claimed, which is contention, and the loop re-runs the write
|
|
299
|
+
* body's reads, so the next attempt builds on the rival's revision — the same argument that keeps
|
|
300
|
+
* stale-revision rejections retried (the accepted-tradeoff `NOTE:` on
|
|
301
|
+
* {@link isUncommittedTransactorAggregate}).
|
|
302
|
+
*
|
|
303
|
+
* NOTE: `CoordinatorStaleLossError` (db-core; "nothing durably committed, safe to re-drive") is not
|
|
304
|
+
* claimed, so it falls to the text matchers; it escapes only after the coordinator's own retry budget
|
|
305
|
+
* ran out. Seen abandoning a control write on 2026-09-18 (`control-delete-while-alone-convergence`,
|
|
306
|
+
* under load): a node's own storage refused its own pend as a `stale conflict` for the ~14 s the
|
|
307
|
+
* coordinator spent re-driving, then gave up. Still not claimed here — the failing attempt already
|
|
308
|
+
* ran past {@link CONTROL_WRITE_RETRY_BUDGET_MS}, so a retry from this loop would never get to run,
|
|
309
|
+
* and the refusal came from the node's OWN storage disagreeing with its own revision view, which does
|
|
310
|
+
* not change while the node is alone; re-driving the same write body again would hit the same
|
|
311
|
+
* refusal. The cause (a commit torn by a sibling that stopped mid-commit, leaving the node with a
|
|
312
|
+
* revision view its own storage disputes) is `tickets/blocked/forked-control-collection-sync-livelocks.md`
|
|
313
|
+
* → "Second trigger".
|
|
314
|
+
*/
|
|
315
|
+
function isFinalTornWrite(link: Error): boolean {
|
|
316
|
+
return link instanceof TornActionError && link.final === true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* A LOST UPDATE: the optimystic bridge refusing a commit because another writer changed or
|
|
321
|
+
* removed a row this statement read (`TransactionBridge.mapCommitRefusal` over a
|
|
322
|
+
* `TreeEntryChangedError`, `quereus-plugin-optimystic/src/optimystic-adapter/txn-bridge.ts`).
|
|
323
|
+
* Claimed by TYPE for {@link LOST_UPDATE_RETRY_POLICY} only.
|
|
324
|
+
*
|
|
325
|
+
* Why it is a proven non-commit: the refusal is raised inside the action's atomic handler, so
|
|
326
|
+
* none of that action's entries land. A refusal after a sibling tree or collection of the same
|
|
327
|
+
* transaction already synced is not delivered bare — the per-tree sweep wraps it in
|
|
328
|
+
* `PartialCommitError`, the coordinator path in `CoordinatorPartialCommitError` — and
|
|
329
|
+
* {@link reportsPossiblyStoredWrite} vetoes both by type before this predicate runs. A bare chain
|
|
330
|
+
* carrying this class therefore means nothing of the write is durable.
|
|
331
|
+
*
|
|
332
|
+
* Why it is not in the default list: the rejection is of the write's READ SET, not of its
|
|
333
|
+
* signature, so re-presenting the same statement is only right when the body re-reads the row and
|
|
334
|
+
* decides again over what it finds. That is a property of each opted-in body (see the policy's
|
|
335
|
+
* `NOTE:`), not of control writes in general.
|
|
336
|
+
*
|
|
337
|
+
* Not matched by text: `ConcurrentModificationError` is exported from both the plugin's root and
|
|
338
|
+
* `/plugin` entries over one shared chunk, as `PartialCommitError` is, so the one `instanceof`
|
|
339
|
+
* covers both (the `NOTE:` on {@link reportsPossiblyStoredWrite} applies to it too). A second
|
|
340
|
+
* loaded copy of the plugin would fail the `instanceof` and the lost update would then simply not
|
|
341
|
+
* be retried — the safe side.
|
|
342
|
+
*/
|
|
343
|
+
function isLostUpdate(link: Error): boolean {
|
|
344
|
+
return link instanceof ConcurrentModificationError;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Optimystic refusing to let a node elect ITSELF coordinator because its last connection dropped
|
|
349
|
+
* less than the guard's grace period ago (`Libp2pKeyPeerNetwork.shouldAllowSelfCoordination` →
|
|
350
|
+
* `findCoordinator`, `db-p2p/src/libp2p-key-network.ts`).
|
|
351
|
+
*
|
|
352
|
+
* Matched ONLY with the `grace-period-not-elapsed` reason. The same sentence is raised with three
|
|
353
|
+
* other reasons and none of them is worth a retry: `disabled` is static configuration, and
|
|
354
|
+
* `partition-detected` / `suspicious-shrinkage` describe a network condition that will not resolve
|
|
355
|
+
* inside a few seconds of backoff. The grace-period one WILL — it clears as soon as the node holds
|
|
356
|
+
* one connection again, which a cold-starting node dialling its bootstrap peers is actively
|
|
357
|
+
* working on.
|
|
358
|
+
*
|
|
359
|
+
* NOTE: matched by TEXT, not by type. `FindCoordinatorError` (`code: 'SELF_COORDINATION_BLOCKED'`)
|
|
360
|
+
* is exported from `@optimystic/db-p2p`, and when this matcher was written the error OBJECT did not
|
|
361
|
+
* survive the trip: `OptimysticVirtualTable.initialize` rethrew as `new Error(message)` with no
|
|
362
|
+
* `cause`. It now rethrows through `rewrapAsQueryError`, which keeps `cause`
|
|
363
|
+
* (`quereus-plugin-optimystic/src/optimystic-module.ts`); whether every Quereus wrapper above it on
|
|
364
|
+
* the schema-init path keeps it too has not been checked, and the text still arrives either way.
|
|
365
|
+
* This matcher — the module's only one — fails CLOSED: a rewording upstream stops the retry
|
|
366
|
+
* engaging, it never makes it unsafe.
|
|
367
|
+
*/
|
|
368
|
+
const SELF_COORDINATION_GRACE_REFUSAL =
|
|
369
|
+
/Self-coordination blocked: grace-period-not-elapsed\. No coordinator available for key\./;
|
|
370
|
+
|
|
371
|
+
/** Self-coordination refused because the node's last connection dropped moments ago. */
|
|
372
|
+
function isSelfCoordinationGraceRefusal(message: string): boolean {
|
|
373
|
+
return SELF_COORDINATION_GRACE_REFUSAL.test(message);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The error messages the DEFAULT write policy claims: none. Every failure it re-presents is now
|
|
378
|
+
* claimed by TYPE in {@link matchesRetriableFailure} — the transactor's get/pend aggregate
|
|
379
|
+
* ({@link isUncommittedTransactorAggregate}), a block read the cohort could not answer
|
|
380
|
+
* ({@link reportsUnreachableCohort}), an unanswered super-majority shortfall
|
|
381
|
+
* ({@link reportsUnansweredShortfall}) and a final torn write ({@link isFinalTornWrite}) — and so
|
|
382
|
+
* are both vetoes.
|
|
383
|
+
*
|
|
384
|
+
* Kept empty rather than deleted for two reasons: it is what {@link RETRIABLE_SCHEMA_INIT_MATCHERS}
|
|
385
|
+
* spreads, so the "widened, not replaced" relation between the two policies stays expressed in
|
|
386
|
+
* code; and a future text matcher has one obvious home. The only text matcher left in this module
|
|
387
|
+
* is the schema-init self-coordination refusal, which comes from `@optimystic/db-p2p`, not from the
|
|
388
|
+
* transactor.
|
|
389
|
+
*
|
|
390
|
+
* Deliberately NOT matched, here or anywhere: every constraint / authorization failure
|
|
391
|
+
* (`CHECK constraint failed:`, `UNIQUE constraint failed:`) — those reach the same funnel
|
|
392
|
+
* and must be retried zero times: the cohort REFUSED the write, so re-presenting it
|
|
393
|
+
* re-presents a spent signature and can never commit.
|
|
394
|
+
*/
|
|
395
|
+
const RETRIABLE_CONTROL_WRITE_MATCHERS: readonly ((message: string) => boolean)[] = [];
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* The (currently empty) default list PLUS the self-coordination grace refusal — the classifier for
|
|
399
|
+
* schema init ONLY ({@link SCHEMA_INIT_RETRY_POLICY}), deliberately not folded into the list above.
|
|
400
|
+
*
|
|
401
|
+
* Why this class is safe HERE and not everywhere: a write refused at coordinator SELECTION never
|
|
402
|
+
* reached a peer, so re-presenting it is a proven non-commit. But `findCoordinator` is also
|
|
403
|
+
* reached from `NetworkTransactor.commitBlock` during PHASE 2 (`resolveCoordinator`,
|
|
404
|
+
* `db-core/src/transactor/network-transactor.ts`), and `commit()` commits the header block before
|
|
405
|
+
* the rest — so a general control write refused there can be refused AFTER something already
|
|
406
|
+
* committed. Worse, it is thrown from the batch assembly OUTSIDE the `try` that composes the
|
|
407
|
+
* commit-phase aggregate (`batchesForPayload`, `db-core/src/transactor/network-transactor.ts`), so
|
|
408
|
+
* it normally arrives with no aggregate around it at all and
|
|
409
|
+
* {@link reportsIndeterminateCommit} has nothing to veto on — and re-running an insert body over a
|
|
410
|
+
* write that landed is exactly the `UNIQUE constraint failed: CadrePeer.PeerId` failure that veto
|
|
411
|
+
* exists to prevent. (The one path where the veto does fire is the per-block retry's
|
|
412
|
+
* re-resolution inside `processBatches`, where such a throw becomes the commit aggregate's root
|
|
413
|
+
* cause.)
|
|
414
|
+
*
|
|
415
|
+
* Schema init does not have that problem: `apply schema` is a diff rather than a replay, and a
|
|
416
|
+
* failed apply is taken back whole — Quereus unwinds every step it ran and verifies the catalog
|
|
417
|
+
* against a pre-apply fingerprint — so a re-run emits exactly the DDL the live catalog is missing.
|
|
418
|
+
* The full argument, including the plugin-side dependency that unwind carries, is at the
|
|
419
|
+
* `applyControlSchema` call site in `control-database.ts`.
|
|
420
|
+
*
|
|
421
|
+
* RETRIED DELIBERATELY, not by oversight: the failures Quereus cannot take back. When the unwind
|
|
422
|
+
* does not complete — an undo statement threw, the post-unwind catalog did not match the pre-apply
|
|
423
|
+
* fingerprint, that catalog could not be re-collected to check, or a step the differ marks
|
|
424
|
+
* irreversible because it discards data (dropping a table or a column, narrowing a column's type —
|
|
425
|
+
* of which only dropping a column reaches a `CadreControl` apply, as the catalog-only migration
|
|
426
|
+
* step `refuseIncompatibleControlStore` in `control-schema-version.ts` lets through; it discards
|
|
427
|
+
* nothing there, since rows keep the field) poisoned the journal before it
|
|
428
|
+
* ran, leaving nothing unwound — Quereus keeps the failing step's own message, APPENDS the reason the
|
|
429
|
+
* schema could not be restored, and carries the original as `cause`. So a transient cause
|
|
430
|
+
* underneath still matches here, and neither veto ({@link reportsPossiblyStoredWrite},
|
|
431
|
+
* {@link reportsIndeterminateCommit}) looks for that reason text. Retrying over a partially migrated
|
|
432
|
+
* schema is safe for the same reason the ordinary re-run is: the plugin commits whatever its write
|
|
433
|
+
* batch holds, so storage and the catalog are left in step either way, and the next apply diffs
|
|
434
|
+
* against what is really there. Vetoing it instead would turn a healable transient outage into a
|
|
435
|
+
* dead start.
|
|
436
|
+
*/
|
|
437
|
+
const RETRIABLE_SCHEMA_INIT_MATCHERS: readonly ((message: string) => boolean)[] = [
|
|
438
|
+
...RETRIABLE_CONTROL_WRITE_MATCHERS,
|
|
439
|
+
isSelfCoordinationGraceRefusal,
|
|
440
|
+
];
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Did this control write fail because the cluster cohort did not ANSWER — i.e. is
|
|
444
|
+
* re-presenting the SAME signed write a moment later the right response?
|
|
445
|
+
*
|
|
446
|
+
* Classifies entirely by TYPE, over the `cause` chain — by the time a failure reaches
|
|
447
|
+
* `ControlDatabase` it is wrapped in a `QuereusError` (the real chain is `QuereusError` → `Error`
|
|
448
|
+
* → `Error`), so every claim and veto must work at any depth. Anything that is not an `Error` is
|
|
449
|
+
* never retried.
|
|
450
|
+
*
|
|
451
|
+
* Two vetoes run over the whole chain before any claim: {@link reportsPossiblyStoredWrite}
|
|
452
|
+
* (a spent sync budget, a non-final torn write, a partial commit) and
|
|
453
|
+
* {@link reportsIndeterminateCommit} (a commit-phase transactor aggregate). A chain either veto
|
|
454
|
+
* flags, at any level, is never retried on the strength of some other level looking transient.
|
|
455
|
+
* Four failures are claimed as retriable: the transactor's get/pend aggregate
|
|
456
|
+
* ({@link isUncommittedTransactorAggregate}, shared with the read classifier), a block read the
|
|
457
|
+
* cohort could not answer ({@link reportsUnreachableCohort}, also shared with it), an unanswered
|
|
458
|
+
* super-majority shortfall ({@link reportsUnansweredShortfall}, shared with the edge node's
|
|
459
|
+
* `retryAfterRestart`) and a final torn write ({@link isFinalTornWrite}).
|
|
460
|
+
*
|
|
461
|
+
* The RETRIABLE failures need a real multi-node cluster to produce, so the unit cases build the
|
|
462
|
+
* upstream classes directly; the degraded-cohort scenario
|
|
463
|
+
* (`control-write-degraded-cohort-member.integration.ts`) asserts this classifier against the
|
|
464
|
+
* super-majority shortfall from a real silent member, and absorbs a real stream reset end to end
|
|
465
|
+
* (on Optimystic 1.20.0 the reset fails the write's read as an unanswered block, claimed by type).
|
|
466
|
+
*/
|
|
467
|
+
export function isRetriableControlWriteFailure(error: unknown): boolean {
|
|
468
|
+
return matchesRetriableFailure(error, RETRIABLE_CONTROL_WRITE_MATCHERS);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* {@link isRetriableControlWriteFailure} widened by exactly one class — the self-coordination
|
|
473
|
+
* grace refusal ({@link RETRIABLE_SCHEMA_INIT_MATCHERS}) — for `ControlDatabase.applyControlSchema`.
|
|
474
|
+
*
|
|
475
|
+
* Opt-in per call site, via {@link SCHEMA_INIT_RETRY_POLICY}. Every other control write keeps the
|
|
476
|
+
* default classifier untouched.
|
|
477
|
+
*/
|
|
478
|
+
export function isRetriableSchemaInitFailure(error: unknown): boolean {
|
|
479
|
+
return matchesRetriableFailure(error, RETRIABLE_SCHEMA_INIT_MATCHERS);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* {@link isRetriableControlWriteFailure} widened by exactly one class — a lost update
|
|
484
|
+
* ({@link isLostUpdate}), claimed by type — for the `CadrePeer` writers that re-read the row inside
|
|
485
|
+
* their locked body. Opt-in per call site, via {@link LOST_UPDATE_RETRY_POLICY}; both vetoes still
|
|
486
|
+
* run first, so a lost update carried inside a partial commit, a spent sync budget or a non-final
|
|
487
|
+
* torn write stays declined.
|
|
488
|
+
*/
|
|
489
|
+
export function isRetriableLostUpdateFailure(error: unknown): boolean {
|
|
490
|
+
return matchesRetriableFailure(error, RETRIABLE_CONTROL_WRITE_MATCHERS, isLostUpdate);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Shared body of the three classifiers: walk the `cause` chain, let
|
|
495
|
+
* {@link reportsPossiblyStoredWrite} and then {@link reportsIndeterminateCommit} veto it, then claim
|
|
496
|
+
* a get/pend transactor aggregate ({@link isUncommittedTransactorAggregate}), a block read the
|
|
497
|
+
* cohort could not answer ({@link reportsUnreachableCohort}), an unanswered
|
|
498
|
+
* super-majority shortfall ({@link reportsUnansweredShortfall}), a final torn write
|
|
499
|
+
* ({@link isFinalTornWrite}) or any link `claimsLink` names, then ask `matchers`. The vetoes run
|
|
500
|
+
* BEFORE any claim for every policy — a chain reporting a possibly-stored write or a commit-phase
|
|
501
|
+
* batch is never retried, however transient some other level looks, and a final torn write or a
|
|
502
|
+
* lost update carried by a partial commit is still declined because a sibling collection landed.
|
|
503
|
+
* `claimsLink` is the one typed claim a policy may add; it sits here beside the shared typed claims
|
|
504
|
+
* so the vetoes stay in one place above all of them.
|
|
505
|
+
*
|
|
506
|
+
* The three shared typed claims are predicates over the WHOLE chain and the policy's own is per
|
|
507
|
+
* link, which is why the one `some` below carries only the latter. The shortfall claim being shared
|
|
508
|
+
* with the edge node is what keeps the two write policies from drifting on it; where a shortfall
|
|
509
|
+
* leaves the write is still decided here, by the commit-phase veto above.
|
|
510
|
+
*/
|
|
511
|
+
function matchesRetriableFailure(
|
|
512
|
+
error: unknown,
|
|
513
|
+
matchers: readonly ((message: string) => boolean)[],
|
|
514
|
+
claimsLink?: (link: Error) => boolean
|
|
515
|
+
): boolean {
|
|
516
|
+
if (!(error instanceof Error)) {
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
const links = causeChain(error);
|
|
520
|
+
if (reportsPossiblyStoredWrite(links) || reportsIndeterminateCommit(links)) {
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
// NOTE: an unanswered read can fail AFTER this write's log entry is stored: the refresh between
|
|
524
|
+
// commit attempts reads the collection's log ends, and finishing the write's own entry
|
|
525
|
+
// (`Collection.completeOwnEntry`) asks `getStatus` when no attempt is retained at the entry's
|
|
526
|
+
// revision (a forked lineage — both commit paths retain the failed attempt before refreshing).
|
|
527
|
+
// Either throw escapes the refresh loop unwrapped. Claimed anyway, because the get/pend
|
|
528
|
+
// aggregate those same reads raise is already claimed beside it, so this adds no new case: the
|
|
529
|
+
// re-run is a new transaction whose body re-reads the blocks, so it diffs to nothing or fails on
|
|
530
|
+
// `UNIQUE` where the earlier write landed, and writes it once where it did not. A refresh that
|
|
531
|
+
// already saved a sibling collection arrives as `CoordinatorPartialCommitError`, which the veto
|
|
532
|
+
// above declines.
|
|
533
|
+
if (isUncommittedTransactorAggregate(links) || reportsUnreachableCohort(links)
|
|
534
|
+
|| reportsUnansweredShortfall(links)
|
|
535
|
+
|| links.some(link => isFinalTornWrite(link) || claimsLink?.(link) === true)) {
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
return chainMessages(error).some(message => matchers.some(matches => matches(message)));
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Policy and pacing overrides for {@link retryControlWrite}. Production callers pass nothing
|
|
543
|
+
* (or a named policy such as {@link SCHEMA_INIT_RETRY_POLICY}); specs inject a recorded
|
|
544
|
+
* `sleep`, a shorter delay list, or a fake clock so no test ever waits out a real backoff.
|
|
545
|
+
*
|
|
546
|
+
* Every field defaults to the shipped control-write policy ({@link CONTROL_WRITE_ATTEMPTS},
|
|
547
|
+
* {@link CONTROL_WRITE_RETRY_DELAYS_MS}, {@link isRetriableControlWriteFailure}), so an
|
|
548
|
+
* omitted field is byte-identical to the behaviour before per-call-site policies existed.
|
|
549
|
+
* The field shapes and semantics live on the shared {@link ControlRetryOptions}.
|
|
550
|
+
*/
|
|
551
|
+
export type ControlWriteRetryOptions = ControlRetryOptions;
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* The schema-init policy: `ControlDatabase.applyControlSchema`'s distributed DDL, which
|
|
555
|
+
* additionally absorbs optimystic's self-coordination grace refusal and gets more attempts over a
|
|
556
|
+
* longer backoff to do it (see {@link SCHEMA_INIT_ATTEMPTS} and
|
|
557
|
+
* {@link SCHEMA_INIT_RETRY_DELAYS_MS}). Still bounded by the shared
|
|
558
|
+
* {@link CONTROL_WRITE_RETRY_BUDGET_MS}.
|
|
559
|
+
*
|
|
560
|
+
* NOTE: exactly one call site opts in today, and the re-run safety this policy assumes is that
|
|
561
|
+
* site's, not a general property — `apply schema` is a diff, and a failed apply is unwound whole
|
|
562
|
+
* and verified against the pre-apply catalog (full argument at the `applyControlSchema` call site
|
|
563
|
+
* in `control-database.ts`). A second opt-in must re-derive that argument for its own write body
|
|
564
|
+
* first; if this policy ever grows a third consumer, rename it for what the callers share rather
|
|
565
|
+
* than widening it by default. {@link LOST_UPDATE_RETRY_POLICY} is a separate policy for a
|
|
566
|
+
* separate class, not a second consumer of this one.
|
|
567
|
+
*/
|
|
568
|
+
export const SCHEMA_INIT_RETRY_POLICY: Readonly<ControlWriteRetryOptions> = {
|
|
569
|
+
attempts: SCHEMA_INIT_ATTEMPTS,
|
|
570
|
+
delaysMs: SCHEMA_INIT_RETRY_DELAYS_MS,
|
|
571
|
+
isRetriable: isRetriableSchemaInitFailure,
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* The lost-update policy: the default classifier plus a lost update claimed by type
|
|
576
|
+
* ({@link isRetriableLostUpdateFailure}), over the default attempts, backoff and budget. A re-run
|
|
577
|
+
* here is a RE-DECISION, not a re-presentation: the body reads the row again under the lock and
|
|
578
|
+
* signs over what it finds, so the write that lost to a rival is replaced by one made over the
|
|
579
|
+
* rival's result. The budget bounds it as it bounds every write — a losing attempt whose read
|
|
580
|
+
* phase waited out one per-peer read deadline (~7.5 s measured in the degraded-cohort scenario's
|
|
581
|
+
* trio boot) gets exactly one retry, which is the ceiling to expect; one that waited out two
|
|
582
|
+
* (14.7 s, measured once in 18 of that scenario's boots, on the member that cannot reach one
|
|
583
|
+
* sibling) gets none and is abandoned on the budget, and the lost publish is then the reconcile
|
|
584
|
+
* pass's or the heartbeat's to redo, as every abandoned self-publish is.
|
|
585
|
+
*
|
|
586
|
+
* NOTE: the re-run safety this policy assumes is each opted-in body's, not a general property. A
|
|
587
|
+
* body may opt in only when its read of the row, its decision and its signature ALL run inside
|
|
588
|
+
* the locked body, so that the re-run presents a write made over the row as it now stands rather
|
|
589
|
+
* than the first attempt's signed record (which the rival may have stamped past, or which may
|
|
590
|
+
* overwrite what the rival wrote). Three `CadrePeer` writers opt in, through
|
|
591
|
+
* `ControlDatabase.mutateCadrePeer`'s policy parameter or `lockedWithRetry` directly:
|
|
592
|
+
* `updateSelfPeerRecord` (re-reads and re-signs the node's own record), `reauthorizeCadrePeer`
|
|
593
|
+
* (re-reads the stamp and decides again whether the row is still the owner's to re-touch) and
|
|
594
|
+
* the two removals `deleteCadrePeer` / `removeMachine` (stamp read and signature already inside
|
|
595
|
+
* the body, and a self-publish never changes the stamp the `'remove'` digest binds). A new opt-in
|
|
596
|
+
* must re-derive that argument for its own body first.
|
|
597
|
+
*/
|
|
598
|
+
export const LOST_UPDATE_RETRY_POLICY: Readonly<ControlWriteRetryOptions> = {
|
|
599
|
+
isRetriable: isRetriableLostUpdateFailure,
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Run `attempt` up to {@link CONTROL_WRITE_ATTEMPTS} times (or `options.attempts`), retrying only
|
|
604
|
+
* failures the policy's classifier — {@link isRetriableControlWriteFailure} by default —
|
|
605
|
+
* calls transient, and only while {@link CONTROL_WRITE_RETRY_BUDGET_MS} has not elapsed.
|
|
606
|
+
*
|
|
607
|
+
* `attempt` must be atomic and re-runnable: a failed cluster write rolls back (nothing is
|
|
608
|
+
* half-applied), and re-running the body re-runs its reads too — `ControlDatabase`'s
|
|
609
|
+
* locked write bodies all satisfy this (see the contract on
|
|
610
|
+
* `ControlDatabase.withWriteLock`). The backoff sleeps happen with NO lock held; the caller
|
|
611
|
+
* takes and releases its lock inside `attempt`.
|
|
612
|
+
*
|
|
613
|
+
* On exhaustion (attempts or budget) the LAST error is rethrown unchanged — never wrapped,
|
|
614
|
+
* so the exact messages downstream code and the degraded-cohort-member scenario assert on
|
|
615
|
+
* survive. A non-retriable failure propagates from the attempt that raised it.
|
|
616
|
+
*
|
|
617
|
+
* Either way — declined or exhausted — an `options.onAbandon` observer is notified exactly
|
|
618
|
+
* once before the rethrow (a `ControlRetryAbandonment`). Only WRITES pass one: an abandoned
|
|
619
|
+
* read throws to a caller that is awaiting it, so nothing is lost silently there
|
|
620
|
+
* (`control-read-retry.ts` says so at its own seam), while an abandoned write may have no
|
|
621
|
+
* caller at all — a background self-record republish is `void`-ed with a `debug` catch, so
|
|
622
|
+
* its failure reaches nobody unless something is listening here.
|
|
623
|
+
* `ControlDatabase.setControlWriteAbandonedListener` is the only thing that passes one in
|
|
624
|
+
* production; `retryControlWrite` itself neither supplies nor requires an observer.
|
|
625
|
+
*/
|
|
626
|
+
export function retryControlWrite<T>(
|
|
627
|
+
attempt: () => Promise<T>,
|
|
628
|
+
options: ControlWriteRetryOptions = {}
|
|
629
|
+
): Promise<T> {
|
|
630
|
+
return retryControlOperation(attempt, {
|
|
631
|
+
attempts: CONTROL_WRITE_ATTEMPTS,
|
|
632
|
+
delaysMs: CONTROL_WRITE_RETRY_DELAYS_MS,
|
|
633
|
+
budgetMs: CONTROL_WRITE_RETRY_BUDGET_MS,
|
|
634
|
+
isRetriable: isRetriableControlWriteFailure,
|
|
635
|
+
// The default prefix, named anyway: the degraded-cohort scenario asserts on
|
|
636
|
+
// `Control write [<label>] …` lines byte-for-byte.
|
|
637
|
+
logPrefix: 'Control write',
|
|
638
|
+
}, options);
|
|
639
|
+
}
|