@serfab/quereus-plugin-sereus 0.10.0 → 0.11.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 +367 -361
- package/dist/cached-storage.d.ts +66 -0
- package/dist/cached-storage.js +103 -0
- package/dist/cached-storage.js.map +1 -0
- package/dist/compose-strand.d.ts +2 -2
- package/dist/compose-strand.js +18 -22
- package/dist/compose-strand.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/parse-config.js +23 -11
- package/dist/parse-config.js.map +1 -1
- package/dist/plugin-browser.js +15040 -11647
- package/dist/plugin-browser.js.map +4 -4
- package/dist/strand-schema.js +503 -503
- package/dist/types.d.ts +37 -10
- package/package.json +8 -8
- package/src/cached-storage.ts +107 -0
- package/src/cluster-size.ts +248 -248
- package/src/compose-strand.ts +21 -23
- package/src/connect-browser.ts +57 -57
- package/src/connect.ts +38 -38
- package/src/index.ts +2 -1
- package/src/parse-config.ts +70 -55
- package/src/plugin-browser.ts +29 -29
- package/src/plugin.ts +39 -39
- package/src/strand-schema.ts +533 -533
- package/src/types.ts +106 -78
package/src/cluster-size.ts
CHANGED
|
@@ -1,248 +1,248 @@
|
|
|
1
|
-
import type { NodeOptions } from '@optimystic/db-p2p';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Smallest cluster size Optimystic will honour — its own `minAbsoluteClusterSize`,
|
|
5
|
-
* and the smallest value that reaches the cluster path at all (a lone node writes to
|
|
6
|
-
* local storage without forming a cluster). A validation floor, not a default.
|
|
7
|
-
*/
|
|
8
|
-
export const MIN_CLUSTER_SIZE = 2;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* How many nodes each block of the **control** (cadre membership) database is replicated
|
|
12
|
-
* to. Chosen to exceed any party's node count, so in practice every control block lands
|
|
13
|
-
* on every member of the party.
|
|
14
|
-
*
|
|
15
|
-
* **Why full replication here.** Every control node reads the whole control database —
|
|
16
|
-
* membership, peer addresses, the strand list — so a member left out of a block's cohort is
|
|
17
|
-
* a member that may never learn the fact, and at a cohort of two the *read repair* that is
|
|
18
|
-
* supposed to make that safe cannot converge (it can ask exactly one peer, and that peer may
|
|
19
|
-
* be the member that also missed the write). Measured: 4 failures in 10 runs of the
|
|
20
|
-
* control-DB replication scenario at size 2, 0 in 20 at size 3 and 0 in 10 at size 8.
|
|
21
|
-
*
|
|
22
|
-
* **Why this number, and why a constant.** Optimystic caps a cohort at the peers that
|
|
23
|
-
* actually serve the network and downsizes a cohort it cannot fill (`allowDownsize: true`,
|
|
24
|
-
* which Cadre passes), so any value at or above the party's node count yields the same
|
|
25
|
-
* behaviour: cohort = whole party. 16 is roughly twice the largest deployment the product
|
|
26
|
-
* documents (`docs/architecture.md` → "Enterprise (Multi-Node Mixed)", 7 nodes) — headroom
|
|
27
|
-
* without pretending to support arbitrarily large parties; a party genuinely running more
|
|
28
|
-
* than 16 nodes is back to partial replication for its control database and should raise
|
|
29
|
-
* this. It is a constant rather than the live member count because Optimystic freezes the
|
|
30
|
-
* value at libp2p-node construction, before the `ControlDatabase` holding the `CadrePeer`
|
|
31
|
-
* rows exists, and because a per-node derivation lets two members disagree.
|
|
32
|
-
*
|
|
33
|
-
* A cadre is one party by construction, so the owner-blind cohort-selection question that
|
|
34
|
-
* applies to strand networks (see {@link DEFAULT_STRAND_CLUSTER_SIZE} below) is not a question
|
|
35
|
-
* here: "replicate to the whole party" already is the whole intent, and there is no party
|
|
36
|
-
* diversity for this breadth to buy or fail to buy.
|
|
37
|
-
*
|
|
38
|
-
* Canonical explanation of all of the above — the read-repair mechanics, what whole-party
|
|
39
|
-
* breadth does and does not remove, and what the wider cohort costs in write availability —
|
|
40
|
-
* is `docs/architecture.md` → "Replication cluster size". Keep it there, not here.
|
|
41
|
-
*
|
|
42
|
-
* **Deliberately not `clusterPolicy.assumedClusterSize`.** That option means "the smallest
|
|
43
|
-
* cohort this deployment can genuinely field", and it feeds both the membership admission
|
|
44
|
-
* gate's low-confidence path *and* the read-repair/reconcile corroboration floor. A Cadre
|
|
45
|
-
* party legitimately runs one or two nodes, so Cadre leaves it at Optimystic's default of 2;
|
|
46
|
-
* asserting 16 there would make the admission gate demand `ceil(0.75 x 16) = 12` declared
|
|
47
|
-
* peers and refuse every real party's writes.
|
|
48
|
-
*
|
|
49
|
-
* NOTE: cohort selection overfetches proportionally to this number —
|
|
50
|
-
* `Libp2pKeyPeerNetwork.membershipOverfetch` asks FRET for
|
|
51
|
-
* `max(clusterSize * 4, clusterSize + 16)` candidates and does a peerStore protocol lookup
|
|
52
|
-
* per candidate, so 16 requests a 64-peer proximity band. Free today, because the result is
|
|
53
|
-
* bounded by the peers FRET actually knows (2 in a 3-node party). If control-network cohort
|
|
54
|
-
* selection ever shows up as slow on a large mesh, look here first.
|
|
55
|
-
*/
|
|
56
|
-
export const CONTROL_REPLICATION_BREADTH = 16;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Cluster consensus policy every CONTROL-network libp2p node runs — production
|
|
60
|
-
* (`CadreNode.buildControlNodeOptions`) and the integration harness alike. One definition so
|
|
61
|
-
* the two cannot drift: the object literal used to be hand-copied at both sites, and the copy
|
|
62
|
-
* at the harness had picked up a `superMajorityThreshold` override production never had.
|
|
63
|
-
*
|
|
64
|
-
* `superMajorityThreshold` is deliberately ABSENT. Omitting it makes both the cluster member
|
|
65
|
-
* and the coordinator fall back to Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75) —
|
|
66
|
-
* the bar a real party commits control writes against. Setting it here, or at any one
|
|
67
|
-
* consumer, reintroduces exactly the divergence this constant exists to prevent. The absence
|
|
68
|
-
* is a researched decision, not an inherited default: no threshold both relaxes the
|
|
69
|
-
* three-node unanimity bar and satisfies Optimystic's partition-safety condition at the
|
|
70
|
-
* shipped admission fraction — see `docs/architecture.md` → "Replication cluster size".
|
|
71
|
-
*
|
|
72
|
-
* `sizeTolerance` only makes sense alongside `allowDownsize`: a fixed
|
|
73
|
-
* {@link CONTROL_REPLICATION_BREADTH}-wide target is unsatisfiable by any real (2-7 node)
|
|
74
|
-
* party, so the cohort must be allowed to shrink to the party that actually exists.
|
|
75
|
-
*
|
|
76
|
-
* NOT for strand networks: {@link STRAND_CLUSTER_POLICY} is a structurally identical object
|
|
77
|
-
* for a different network with different reasoning. The shape match is a coincidence; keep
|
|
78
|
-
* them separate.
|
|
79
|
-
*
|
|
80
|
-
* `assumedClusterSize` is REQUIRED here, and 2 is not a guess — it is the smallest party we
|
|
81
|
-
* support. The field feeds two Optimystic consumers with opposite failure modes, and only one
|
|
82
|
-
* of them defaults to something workable for us:
|
|
83
|
-
*
|
|
84
|
-
* - The membership admission gate already defaults to 2 (`minAbsoluteClusterSize`), so naming
|
|
85
|
-
* it changes nothing there.
|
|
86
|
-
* - The read-repair / reconcile corroboration floor does NOT. Absent this field it falls back
|
|
87
|
-
* to `clusterSize` — which for the control network is {@link CONTROL_REPLICATION_BREADTH},
|
|
88
|
-
* 16. `corroboratorCapacity(peers, 16)` is then `max(peers, 15)`, so the floor of two
|
|
89
|
-
* distinct non-self corroborators binds. A two-node party fields exactly one, can never
|
|
90
|
-
* reach two, and so can never repair a block: `cluster-fetch:no-quorum` forever.
|
|
91
|
-
*
|
|
92
|
-
* Declaring 2 does NOT lower the replication factor — that stays at
|
|
93
|
-
* {@link CONTROL_REPLICATION_BREADTH}. It states the cohort size we can genuinely field, which
|
|
94
|
-
* is what the corroboration floor is asking. See Optimystic's `cluster/cluster-policy.ts`
|
|
95
|
-
* ("Why two size yardsticks, not one") and `corroboratorCapacity` in `cluster/quorum-restore.ts`.
|
|
96
|
-
*
|
|
97
|
-
* The `satisfies` is load-bearing: without it a mistyped or obsolete key would compile at both
|
|
98
|
-
* this definition and every consumer, because TypeScript only excess-property-checks fresh
|
|
99
|
-
* object literals, not a shared constant handed to `clusterPolicy`.
|
|
100
|
-
*/
|
|
101
|
-
export const CONTROL_CLUSTER_POLICY = Object.freeze({
|
|
102
|
-
allowDownsize: true,
|
|
103
|
-
sizeTolerance: 0.5,
|
|
104
|
-
assumedClusterSize: 2,
|
|
105
|
-
} satisfies NonNullable<NodeOptions['clusterPolicy']>);
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Default number of nodes a **strand** network is told its replication cluster should have.
|
|
109
|
-
*
|
|
110
|
-
* Strand data is application data, where partial replication is a legitimate choice: only
|
|
111
|
-
* the control database's every-member-reads-all-of-it character forces full replication
|
|
112
|
-
* ({@link CONTROL_REPLICATION_BREADTH}). So the question here is not "everyone" but "how
|
|
113
|
-
* few is too few", and the answer is four.
|
|
114
|
-
*
|
|
115
|
-
* **Why four.** A write commits when a super-majority of the cohort approves, and that bar is
|
|
116
|
-
* Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` of 0.75 — which Cadre selects by naming no
|
|
117
|
-
* threshold at all (see {@link CONTROL_CLUSTER_POLICY}). Approvals needed is
|
|
118
|
-
* `ceil(cohort x 0.75)`, so:
|
|
119
|
-
*
|
|
120
|
-
* | copies | approvals needed | holders that may be offline |
|
|
121
|
-
* |--------|------------------|-----------------------------|
|
|
122
|
-
* | 2 | 2 | 0 |
|
|
123
|
-
* | 3 | 3 | 0 |
|
|
124
|
-
* | 4 | 3 | 1 |
|
|
125
|
-
* | 5 | 4 | 1 |
|
|
126
|
-
* | 6 | 5 | 1 |
|
|
127
|
-
*
|
|
128
|
-
* Four is the smallest breadth that lets a write commit while one holder is away. At two or
|
|
129
|
-
* three every holder must be awake for every write, which for a workspace shared between
|
|
130
|
-
* phones and laptops is the ordinary case, not the rare one. Six buys no more fault tolerance
|
|
131
|
-
* than four and costs more overfetch (see the NOTE below).
|
|
132
|
-
*
|
|
133
|
-
* **It is also a correctness floor, not only a durability one — but the floor is measured in
|
|
134
|
-
* machines, not in this number.** A node that has fallen behind and can ask exactly one peer
|
|
135
|
-
* whether it is current has Optimystic accept that single answer as the cluster's truth
|
|
136
|
-
* (`corroboratorCapacity` in `db-p2p/src/cluster/quorum-restore.ts` lowers the corroboration
|
|
137
|
-
* floor to one when the cohort cannot hold a second voter). If that peer is also behind it
|
|
138
|
-
* honestly reports the stale revision, the reader concludes it is current, and re-arms its
|
|
139
|
-
* repair window forever. Measured on the control-DB replication scenario: 4 failures in 10 runs
|
|
140
|
-
* at breadth 2, 0 in 20 at breadth 3, 0 in 10 at breadth 8. What lifts the floor off a single
|
|
141
|
-
* voter is a *third machine*, not a wider target: `corroboratorCapacity` takes the cohort peers
|
|
142
|
-
* actually present against {@link STRAND_CLUSTER_POLICY}'s declared `assumedClusterSize`, so a
|
|
143
|
-
* three-machine strand needs two corroborators whatever this number says, and a two-machine one
|
|
144
|
-
* cannot supply them however wide the target is. The underlying Optimystic behaviour is unfixed
|
|
145
|
-
* — `backlog/debt-read-repair-single-voter-corroboration` — so a two-machine strand still takes
|
|
146
|
-
* the exposure. Raising this number does not buy the way out; adding a machine does.
|
|
147
|
-
*
|
|
148
|
-
* **Why not derived from the party or member count.** The strand's `Member` rows live *in* the
|
|
149
|
-
* strand database, which runs on the strand libp2p node, whose cluster size is frozen at
|
|
150
|
-
* construction — reading the list to configure the node needs the node to exist. Open strands
|
|
151
|
-
* have no member list at all. The count also changes in the unsafe direction: a node that
|
|
152
|
-
* restarted after someone joined derives a wider expected cohort than one that did not, and the
|
|
153
|
-
* membership admission gate's confident path is what rejects the *wider* view. And a `Member` is
|
|
154
|
-
* a party, not a machine; cohort width consumes machines. Full reasoning in
|
|
155
|
-
* `docs/architecture.md` → "Replication cluster size".
|
|
156
|
-
*
|
|
157
|
-
* **Four machines, not four parties.** This number counts machines, and today that is not the
|
|
158
|
-
* same as counting independent holders. Cohort selection upstream is owner-blind — it
|
|
159
|
-
* picks nodes by hash proximity with no concept of which peer belongs to which party
|
|
160
|
-
* (`optimystic/tickets/backlog/feat-cohort-selection-owner-aware-placement`) — and on the
|
|
161
|
-
* cadre-driven path, the one that resolves a seed for itself, a strand's libp2p mesh is seeded
|
|
162
|
-
* from exactly one party's own control-database peer rows (`CadreNode.resolveCohortSeed`), so
|
|
163
|
-
* the cohort actually formed is that one party's machines. Four copies are, on that path, four
|
|
164
|
-
* machines belonging to one party — one failure domain, one witness. A cross-party mesh is not
|
|
165
|
-
* forbidden, only undiscoverable: it takes hand-supplied addresses
|
|
166
|
-
* (`StrandConnectionOptions.bootstrapNodes`) or a hand-written dial (see the doc). Tracked:
|
|
167
|
-
* `backlog/feat-strand-party-identity`. Full reasoning: `docs/architecture.md` →
|
|
168
|
-
* "Replication cluster size".
|
|
169
|
-
*
|
|
170
|
-
* Raising or lowering it per strand is still the embedder's call
|
|
171
|
-
* ({@link resolveStrandClusterSize}, `CadreNodeConfig.strandClusterSize`,
|
|
172
|
-
* `StrandConnectionOptions.clusterSize`) — but every node on one strand must agree, so a change
|
|
173
|
-
* means restarting all of them. There is no smaller-than-{@link MIN_CLUSTER_SIZE} option.
|
|
174
|
-
*
|
|
175
|
-
* NOTE: cohort selection overfetches proportionally — `Libp2pKeyPeerNetwork.membershipOverfetch`
|
|
176
|
-
* asks FRET for `max(clusterSize * 4, clusterSize + 16)` candidates and does one peerStore
|
|
177
|
-
* protocol lookup per candidate, so 4 requests a 20-peer band (was 18 at breadth 2). Negligible,
|
|
178
|
-
* and bounded by the peers FRET actually knows. If strand cohort selection ever shows up as slow,
|
|
179
|
-
* look here first.
|
|
180
|
-
*/
|
|
181
|
-
export const DEFAULT_STRAND_CLUSTER_SIZE = 4;
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Cluster consensus policy every STRAND-network libp2p node runs — production
|
|
185
|
-
* (`StrandInstanceManager.buildStrandRuntime`) and the plugin's own networked e2e mesh alike.
|
|
186
|
-
* One definition so the two cannot drift; the literal used to be hand-copied at both sites.
|
|
187
|
-
*
|
|
188
|
-
* Structurally identical to {@link CONTROL_CLUSTER_POLICY} and deliberately separate from it:
|
|
189
|
-
* that one is the control database's, whose breadth is fixed at
|
|
190
|
-
* {@link CONTROL_REPLICATION_BREADTH} for reasons that do not apply to application data.
|
|
191
|
-
*
|
|
192
|
-
* `superMajorityThreshold` is ABSENT for the same reason it is absent there: omitting it is how
|
|
193
|
-
* both the coordinator and the cluster member select Optimystic's
|
|
194
|
-
* `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75), the bar {@link DEFAULT_STRAND_CLUSTER_SIZE}'s
|
|
195
|
-
* copies/approvals table is computed against.
|
|
196
|
-
*
|
|
197
|
-
* `sizeTolerance` only makes sense alongside `allowDownsize`: a {@link
|
|
198
|
-
* DEFAULT_STRAND_CLUSTER_SIZE}-wide target is unsatisfiable by a strand of one, two or three
|
|
199
|
-
* machines, so the cohort must be allowed to shrink to the mesh that actually exists.
|
|
200
|
-
*
|
|
201
|
-
* `assumedClusterSize` is REQUIRED, and {@link MIN_CLUSTER_SIZE} is not a guess — it is the
|
|
202
|
-
* smallest strand that reaches the cluster path at all. Absent this field the read-repair /
|
|
203
|
-
* reconcile corroboration floor falls back to `clusterSize`, so
|
|
204
|
-
* `corroboratorCapacity(peers, 4)` is `max(peers, 3)` and two distinct non-self corroborators
|
|
205
|
-
* become mandatory. A two-machine strand fields exactly one peer, can never reach two, and so
|
|
206
|
-
* can never repair a block — `cluster-fetch:no-quorum` forever, which is a read that never
|
|
207
|
-
* catches up and a `create table` that dies with `Missing block` on the joining node. Declaring
|
|
208
|
-
* the honest floor does NOT lower the replication factor (that stays at
|
|
209
|
-
* {@link DEFAULT_STRAND_CLUSTER_SIZE}) and does not relax anything a strand with three or more
|
|
210
|
-
* machines does: `corroboratorCapacity` takes the MAX of the cohort actually present and this
|
|
211
|
-
* number, so the relaxed branch is reachable only by a cohort that is genuinely that small. See
|
|
212
|
-
* Optimystic's `cluster/cluster-policy.ts` ("Why two size yardsticks, not one").
|
|
213
|
-
*
|
|
214
|
-
* The `satisfies` is load-bearing for the same reason as {@link CONTROL_CLUSTER_POLICY}'s.
|
|
215
|
-
*/
|
|
216
|
-
export const STRAND_CLUSTER_POLICY = Object.freeze({
|
|
217
|
-
allowDownsize: true,
|
|
218
|
-
sizeTolerance: 0.5,
|
|
219
|
-
assumedClusterSize: MIN_CLUSTER_SIZE,
|
|
220
|
-
} satisfies NonNullable<NodeOptions['clusterPolicy']>);
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Resolve the cluster size to hand `createLibp2pNode` for a **strand** network, applying
|
|
224
|
-
* {@link DEFAULT_STRAND_CLUSTER_SIZE} and rejecting values Optimystic cannot honour. The
|
|
225
|
-
* control network does not route through here — it uses the fixed
|
|
226
|
-
* {@link CONTROL_REPLICATION_BREADTH}.
|
|
227
|
-
*
|
|
228
|
-
* Leaving the value unset is NOT the same as passing the default: Optimystic's own fallback
|
|
229
|
-
* is 10, so every strand node-creating path must route through here.
|
|
230
|
-
*
|
|
231
|
-
* Every node on the same strand should resolve to the same value: the membership admission
|
|
232
|
-
* gate's *confident* path compares a coordinator's declared set against the member's own
|
|
233
|
-
* cohort view, and that view is bounded by this number, so a member configured much higher
|
|
234
|
-
* can reject a smaller declared set as a downsize. Divergence is a live hazard whenever FRET
|
|
235
|
-
* has a confident network-size estimate — not the unconditional refusal earlier revisions of
|
|
236
|
-
* this comment described. See `docs/architecture.md` → "Replication cluster size".
|
|
237
|
-
*/
|
|
238
|
-
export function resolveStrandClusterSize(configured?: number): number {
|
|
239
|
-
if (configured === undefined) {
|
|
240
|
-
return DEFAULT_STRAND_CLUSTER_SIZE;
|
|
241
|
-
}
|
|
242
|
-
if (!Number.isInteger(configured) || configured < MIN_CLUSTER_SIZE) {
|
|
243
|
-
throw new Error(
|
|
244
|
-
`clusterSize must be an integer >= ${MIN_CLUSTER_SIZE} (Optimystic's minimum cluster size); got ${configured}`
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
return configured;
|
|
248
|
-
}
|
|
1
|
+
import type { NodeOptions } from '@optimystic/db-p2p';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Smallest cluster size Optimystic will honour — its own `minAbsoluteClusterSize`,
|
|
5
|
+
* and the smallest value that reaches the cluster path at all (a lone node writes to
|
|
6
|
+
* local storage without forming a cluster). A validation floor, not a default.
|
|
7
|
+
*/
|
|
8
|
+
export const MIN_CLUSTER_SIZE = 2;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How many nodes each block of the **control** (cadre membership) database is replicated
|
|
12
|
+
* to. Chosen to exceed any party's node count, so in practice every control block lands
|
|
13
|
+
* on every member of the party.
|
|
14
|
+
*
|
|
15
|
+
* **Why full replication here.** Every control node reads the whole control database —
|
|
16
|
+
* membership, peer addresses, the strand list — so a member left out of a block's cohort is
|
|
17
|
+
* a member that may never learn the fact, and at a cohort of two the *read repair* that is
|
|
18
|
+
* supposed to make that safe cannot converge (it can ask exactly one peer, and that peer may
|
|
19
|
+
* be the member that also missed the write). Measured: 4 failures in 10 runs of the
|
|
20
|
+
* control-DB replication scenario at size 2, 0 in 20 at size 3 and 0 in 10 at size 8.
|
|
21
|
+
*
|
|
22
|
+
* **Why this number, and why a constant.** Optimystic caps a cohort at the peers that
|
|
23
|
+
* actually serve the network and downsizes a cohort it cannot fill (`allowDownsize: true`,
|
|
24
|
+
* which Cadre passes), so any value at or above the party's node count yields the same
|
|
25
|
+
* behaviour: cohort = whole party. 16 is roughly twice the largest deployment the product
|
|
26
|
+
* documents (`docs/architecture.md` → "Enterprise (Multi-Node Mixed)", 7 nodes) — headroom
|
|
27
|
+
* without pretending to support arbitrarily large parties; a party genuinely running more
|
|
28
|
+
* than 16 nodes is back to partial replication for its control database and should raise
|
|
29
|
+
* this. It is a constant rather than the live member count because Optimystic freezes the
|
|
30
|
+
* value at libp2p-node construction, before the `ControlDatabase` holding the `CadrePeer`
|
|
31
|
+
* rows exists, and because a per-node derivation lets two members disagree.
|
|
32
|
+
*
|
|
33
|
+
* A cadre is one party by construction, so the owner-blind cohort-selection question that
|
|
34
|
+
* applies to strand networks (see {@link DEFAULT_STRAND_CLUSTER_SIZE} below) is not a question
|
|
35
|
+
* here: "replicate to the whole party" already is the whole intent, and there is no party
|
|
36
|
+
* diversity for this breadth to buy or fail to buy.
|
|
37
|
+
*
|
|
38
|
+
* Canonical explanation of all of the above — the read-repair mechanics, what whole-party
|
|
39
|
+
* breadth does and does not remove, and what the wider cohort costs in write availability —
|
|
40
|
+
* is `docs/architecture.md` → "Replication cluster size". Keep it there, not here.
|
|
41
|
+
*
|
|
42
|
+
* **Deliberately not `clusterPolicy.assumedClusterSize`.** That option means "the smallest
|
|
43
|
+
* cohort this deployment can genuinely field", and it feeds both the membership admission
|
|
44
|
+
* gate's low-confidence path *and* the read-repair/reconcile corroboration floor. A Cadre
|
|
45
|
+
* party legitimately runs one or two nodes, so Cadre leaves it at Optimystic's default of 2;
|
|
46
|
+
* asserting 16 there would make the admission gate demand `ceil(0.75 x 16) = 12` declared
|
|
47
|
+
* peers and refuse every real party's writes.
|
|
48
|
+
*
|
|
49
|
+
* NOTE: cohort selection overfetches proportionally to this number —
|
|
50
|
+
* `Libp2pKeyPeerNetwork.membershipOverfetch` asks FRET for
|
|
51
|
+
* `max(clusterSize * 4, clusterSize + 16)` candidates and does a peerStore protocol lookup
|
|
52
|
+
* per candidate, so 16 requests a 64-peer proximity band. Free today, because the result is
|
|
53
|
+
* bounded by the peers FRET actually knows (2 in a 3-node party). If control-network cohort
|
|
54
|
+
* selection ever shows up as slow on a large mesh, look here first.
|
|
55
|
+
*/
|
|
56
|
+
export const CONTROL_REPLICATION_BREADTH = 16;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Cluster consensus policy every CONTROL-network libp2p node runs — production
|
|
60
|
+
* (`CadreNode.buildControlNodeOptions`) and the integration harness alike. One definition so
|
|
61
|
+
* the two cannot drift: the object literal used to be hand-copied at both sites, and the copy
|
|
62
|
+
* at the harness had picked up a `superMajorityThreshold` override production never had.
|
|
63
|
+
*
|
|
64
|
+
* `superMajorityThreshold` is deliberately ABSENT. Omitting it makes both the cluster member
|
|
65
|
+
* and the coordinator fall back to Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75) —
|
|
66
|
+
* the bar a real party commits control writes against. Setting it here, or at any one
|
|
67
|
+
* consumer, reintroduces exactly the divergence this constant exists to prevent. The absence
|
|
68
|
+
* is a researched decision, not an inherited default: no threshold both relaxes the
|
|
69
|
+
* three-node unanimity bar and satisfies Optimystic's partition-safety condition at the
|
|
70
|
+
* shipped admission fraction — see `docs/architecture.md` → "Replication cluster size".
|
|
71
|
+
*
|
|
72
|
+
* `sizeTolerance` only makes sense alongside `allowDownsize`: a fixed
|
|
73
|
+
* {@link CONTROL_REPLICATION_BREADTH}-wide target is unsatisfiable by any real (2-7 node)
|
|
74
|
+
* party, so the cohort must be allowed to shrink to the party that actually exists.
|
|
75
|
+
*
|
|
76
|
+
* NOT for strand networks: {@link STRAND_CLUSTER_POLICY} is a structurally identical object
|
|
77
|
+
* for a different network with different reasoning. The shape match is a coincidence; keep
|
|
78
|
+
* them separate.
|
|
79
|
+
*
|
|
80
|
+
* `assumedClusterSize` is REQUIRED here, and 2 is not a guess — it is the smallest party we
|
|
81
|
+
* support. The field feeds two Optimystic consumers with opposite failure modes, and only one
|
|
82
|
+
* of them defaults to something workable for us:
|
|
83
|
+
*
|
|
84
|
+
* - The membership admission gate already defaults to 2 (`minAbsoluteClusterSize`), so naming
|
|
85
|
+
* it changes nothing there.
|
|
86
|
+
* - The read-repair / reconcile corroboration floor does NOT. Absent this field it falls back
|
|
87
|
+
* to `clusterSize` — which for the control network is {@link CONTROL_REPLICATION_BREADTH},
|
|
88
|
+
* 16. `corroboratorCapacity(peers, 16)` is then `max(peers, 15)`, so the floor of two
|
|
89
|
+
* distinct non-self corroborators binds. A two-node party fields exactly one, can never
|
|
90
|
+
* reach two, and so can never repair a block: `cluster-fetch:no-quorum` forever.
|
|
91
|
+
*
|
|
92
|
+
* Declaring 2 does NOT lower the replication factor — that stays at
|
|
93
|
+
* {@link CONTROL_REPLICATION_BREADTH}. It states the cohort size we can genuinely field, which
|
|
94
|
+
* is what the corroboration floor is asking. See Optimystic's `cluster/cluster-policy.ts`
|
|
95
|
+
* ("Why two size yardsticks, not one") and `corroboratorCapacity` in `cluster/quorum-restore.ts`.
|
|
96
|
+
*
|
|
97
|
+
* The `satisfies` is load-bearing: without it a mistyped or obsolete key would compile at both
|
|
98
|
+
* this definition and every consumer, because TypeScript only excess-property-checks fresh
|
|
99
|
+
* object literals, not a shared constant handed to `clusterPolicy`.
|
|
100
|
+
*/
|
|
101
|
+
export const CONTROL_CLUSTER_POLICY = Object.freeze({
|
|
102
|
+
allowDownsize: true,
|
|
103
|
+
sizeTolerance: 0.5,
|
|
104
|
+
assumedClusterSize: 2,
|
|
105
|
+
} satisfies NonNullable<NodeOptions['clusterPolicy']>);
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Default number of nodes a **strand** network is told its replication cluster should have.
|
|
109
|
+
*
|
|
110
|
+
* Strand data is application data, where partial replication is a legitimate choice: only
|
|
111
|
+
* the control database's every-member-reads-all-of-it character forces full replication
|
|
112
|
+
* ({@link CONTROL_REPLICATION_BREADTH}). So the question here is not "everyone" but "how
|
|
113
|
+
* few is too few", and the answer is four.
|
|
114
|
+
*
|
|
115
|
+
* **Why four.** A write commits when a super-majority of the cohort approves, and that bar is
|
|
116
|
+
* Optimystic's `DEFAULT_SUPER_MAJORITY_THRESHOLD` of 0.75 — which Cadre selects by naming no
|
|
117
|
+
* threshold at all (see {@link CONTROL_CLUSTER_POLICY}). Approvals needed is
|
|
118
|
+
* `ceil(cohort x 0.75)`, so:
|
|
119
|
+
*
|
|
120
|
+
* | copies | approvals needed | holders that may be offline |
|
|
121
|
+
* |--------|------------------|-----------------------------|
|
|
122
|
+
* | 2 | 2 | 0 |
|
|
123
|
+
* | 3 | 3 | 0 |
|
|
124
|
+
* | 4 | 3 | 1 |
|
|
125
|
+
* | 5 | 4 | 1 |
|
|
126
|
+
* | 6 | 5 | 1 |
|
|
127
|
+
*
|
|
128
|
+
* Four is the smallest breadth that lets a write commit while one holder is away. At two or
|
|
129
|
+
* three every holder must be awake for every write, which for a workspace shared between
|
|
130
|
+
* phones and laptops is the ordinary case, not the rare one. Six buys no more fault tolerance
|
|
131
|
+
* than four and costs more overfetch (see the NOTE below).
|
|
132
|
+
*
|
|
133
|
+
* **It is also a correctness floor, not only a durability one — but the floor is measured in
|
|
134
|
+
* machines, not in this number.** A node that has fallen behind and can ask exactly one peer
|
|
135
|
+
* whether it is current has Optimystic accept that single answer as the cluster's truth
|
|
136
|
+
* (`corroboratorCapacity` in `db-p2p/src/cluster/quorum-restore.ts` lowers the corroboration
|
|
137
|
+
* floor to one when the cohort cannot hold a second voter). If that peer is also behind it
|
|
138
|
+
* honestly reports the stale revision, the reader concludes it is current, and re-arms its
|
|
139
|
+
* repair window forever. Measured on the control-DB replication scenario: 4 failures in 10 runs
|
|
140
|
+
* at breadth 2, 0 in 20 at breadth 3, 0 in 10 at breadth 8. What lifts the floor off a single
|
|
141
|
+
* voter is a *third machine*, not a wider target: `corroboratorCapacity` takes the cohort peers
|
|
142
|
+
* actually present against {@link STRAND_CLUSTER_POLICY}'s declared `assumedClusterSize`, so a
|
|
143
|
+
* three-machine strand needs two corroborators whatever this number says, and a two-machine one
|
|
144
|
+
* cannot supply them however wide the target is. The underlying Optimystic behaviour is unfixed
|
|
145
|
+
* — `backlog/debt-read-repair-single-voter-corroboration` — so a two-machine strand still takes
|
|
146
|
+
* the exposure. Raising this number does not buy the way out; adding a machine does.
|
|
147
|
+
*
|
|
148
|
+
* **Why not derived from the party or member count.** The strand's `Member` rows live *in* the
|
|
149
|
+
* strand database, which runs on the strand libp2p node, whose cluster size is frozen at
|
|
150
|
+
* construction — reading the list to configure the node needs the node to exist. Open strands
|
|
151
|
+
* have no member list at all. The count also changes in the unsafe direction: a node that
|
|
152
|
+
* restarted after someone joined derives a wider expected cohort than one that did not, and the
|
|
153
|
+
* membership admission gate's confident path is what rejects the *wider* view. And a `Member` is
|
|
154
|
+
* a party, not a machine; cohort width consumes machines. Full reasoning in
|
|
155
|
+
* `docs/architecture.md` → "Replication cluster size".
|
|
156
|
+
*
|
|
157
|
+
* **Four machines, not four parties.** This number counts machines, and today that is not the
|
|
158
|
+
* same as counting independent holders. Cohort selection upstream is owner-blind — it
|
|
159
|
+
* picks nodes by hash proximity with no concept of which peer belongs to which party
|
|
160
|
+
* (`optimystic/tickets/backlog/feat-cohort-selection-owner-aware-placement`) — and on the
|
|
161
|
+
* cadre-driven path, the one that resolves a seed for itself, a strand's libp2p mesh is seeded
|
|
162
|
+
* from exactly one party's own control-database peer rows (`CadreNode.resolveCohortSeed`), so
|
|
163
|
+
* the cohort actually formed is that one party's machines. Four copies are, on that path, four
|
|
164
|
+
* machines belonging to one party — one failure domain, one witness. A cross-party mesh is not
|
|
165
|
+
* forbidden, only undiscoverable: it takes hand-supplied addresses
|
|
166
|
+
* (`StrandConnectionOptions.bootstrapNodes`) or a hand-written dial (see the doc). Tracked:
|
|
167
|
+
* `backlog/feat-strand-party-identity`. Full reasoning: `docs/architecture.md` →
|
|
168
|
+
* "Replication cluster size".
|
|
169
|
+
*
|
|
170
|
+
* Raising or lowering it per strand is still the embedder's call
|
|
171
|
+
* ({@link resolveStrandClusterSize}, `CadreNodeConfig.strandClusterSize`,
|
|
172
|
+
* `StrandConnectionOptions.clusterSize`) — but every node on one strand must agree, so a change
|
|
173
|
+
* means restarting all of them. There is no smaller-than-{@link MIN_CLUSTER_SIZE} option.
|
|
174
|
+
*
|
|
175
|
+
* NOTE: cohort selection overfetches proportionally — `Libp2pKeyPeerNetwork.membershipOverfetch`
|
|
176
|
+
* asks FRET for `max(clusterSize * 4, clusterSize + 16)` candidates and does one peerStore
|
|
177
|
+
* protocol lookup per candidate, so 4 requests a 20-peer band (was 18 at breadth 2). Negligible,
|
|
178
|
+
* and bounded by the peers FRET actually knows. If strand cohort selection ever shows up as slow,
|
|
179
|
+
* look here first.
|
|
180
|
+
*/
|
|
181
|
+
export const DEFAULT_STRAND_CLUSTER_SIZE = 4;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Cluster consensus policy every STRAND-network libp2p node runs — production
|
|
185
|
+
* (`StrandInstanceManager.buildStrandRuntime`) and the plugin's own networked e2e mesh alike.
|
|
186
|
+
* One definition so the two cannot drift; the literal used to be hand-copied at both sites.
|
|
187
|
+
*
|
|
188
|
+
* Structurally identical to {@link CONTROL_CLUSTER_POLICY} and deliberately separate from it:
|
|
189
|
+
* that one is the control database's, whose breadth is fixed at
|
|
190
|
+
* {@link CONTROL_REPLICATION_BREADTH} for reasons that do not apply to application data.
|
|
191
|
+
*
|
|
192
|
+
* `superMajorityThreshold` is ABSENT for the same reason it is absent there: omitting it is how
|
|
193
|
+
* both the coordinator and the cluster member select Optimystic's
|
|
194
|
+
* `DEFAULT_SUPER_MAJORITY_THRESHOLD` (0.75), the bar {@link DEFAULT_STRAND_CLUSTER_SIZE}'s
|
|
195
|
+
* copies/approvals table is computed against.
|
|
196
|
+
*
|
|
197
|
+
* `sizeTolerance` only makes sense alongside `allowDownsize`: a {@link
|
|
198
|
+
* DEFAULT_STRAND_CLUSTER_SIZE}-wide target is unsatisfiable by a strand of one, two or three
|
|
199
|
+
* machines, so the cohort must be allowed to shrink to the mesh that actually exists.
|
|
200
|
+
*
|
|
201
|
+
* `assumedClusterSize` is REQUIRED, and {@link MIN_CLUSTER_SIZE} is not a guess — it is the
|
|
202
|
+
* smallest strand that reaches the cluster path at all. Absent this field the read-repair /
|
|
203
|
+
* reconcile corroboration floor falls back to `clusterSize`, so
|
|
204
|
+
* `corroboratorCapacity(peers, 4)` is `max(peers, 3)` and two distinct non-self corroborators
|
|
205
|
+
* become mandatory. A two-machine strand fields exactly one peer, can never reach two, and so
|
|
206
|
+
* can never repair a block — `cluster-fetch:no-quorum` forever, which is a read that never
|
|
207
|
+
* catches up and a `create table` that dies with `Missing block` on the joining node. Declaring
|
|
208
|
+
* the honest floor does NOT lower the replication factor (that stays at
|
|
209
|
+
* {@link DEFAULT_STRAND_CLUSTER_SIZE}) and does not relax anything a strand with three or more
|
|
210
|
+
* machines does: `corroboratorCapacity` takes the MAX of the cohort actually present and this
|
|
211
|
+
* number, so the relaxed branch is reachable only by a cohort that is genuinely that small. See
|
|
212
|
+
* Optimystic's `cluster/cluster-policy.ts` ("Why two size yardsticks, not one").
|
|
213
|
+
*
|
|
214
|
+
* The `satisfies` is load-bearing for the same reason as {@link CONTROL_CLUSTER_POLICY}'s.
|
|
215
|
+
*/
|
|
216
|
+
export const STRAND_CLUSTER_POLICY = Object.freeze({
|
|
217
|
+
allowDownsize: true,
|
|
218
|
+
sizeTolerance: 0.5,
|
|
219
|
+
assumedClusterSize: MIN_CLUSTER_SIZE,
|
|
220
|
+
} satisfies NonNullable<NodeOptions['clusterPolicy']>);
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Resolve the cluster size to hand `createLibp2pNode` for a **strand** network, applying
|
|
224
|
+
* {@link DEFAULT_STRAND_CLUSTER_SIZE} and rejecting values Optimystic cannot honour. The
|
|
225
|
+
* control network does not route through here — it uses the fixed
|
|
226
|
+
* {@link CONTROL_REPLICATION_BREADTH}.
|
|
227
|
+
*
|
|
228
|
+
* Leaving the value unset is NOT the same as passing the default: Optimystic's own fallback
|
|
229
|
+
* is 10, so every strand node-creating path must route through here.
|
|
230
|
+
*
|
|
231
|
+
* Every node on the same strand should resolve to the same value: the membership admission
|
|
232
|
+
* gate's *confident* path compares a coordinator's declared set against the member's own
|
|
233
|
+
* cohort view, and that view is bounded by this number, so a member configured much higher
|
|
234
|
+
* can reject a smaller declared set as a downsize. Divergence is a live hazard whenever FRET
|
|
235
|
+
* has a confident network-size estimate — not the unconditional refusal earlier revisions of
|
|
236
|
+
* this comment described. See `docs/architecture.md` → "Replication cluster size".
|
|
237
|
+
*/
|
|
238
|
+
export function resolveStrandClusterSize(configured?: number): number {
|
|
239
|
+
if (configured === undefined) {
|
|
240
|
+
return DEFAULT_STRAND_CLUSTER_SIZE;
|
|
241
|
+
}
|
|
242
|
+
if (!Number.isInteger(configured) || configured < MIN_CLUSTER_SIZE) {
|
|
243
|
+
throw new Error(
|
|
244
|
+
`clusterSize must be an integer >= ${MIN_CLUSTER_SIZE} (Optimystic's minimum cluster size); got ${configured}`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
return configured;
|
|
248
|
+
}
|
package/src/compose-strand.ts
CHANGED
|
@@ -4,9 +4,10 @@ import optimysticPlugin from '@optimystic/quereus-plugin-optimystic/plugin';
|
|
|
4
4
|
import type { Libp2p } from '@libp2p/interface';
|
|
5
5
|
import type { IRepo } from '@optimystic/db-core';
|
|
6
6
|
import type { IRawStorage } from '@optimystic/db-p2p';
|
|
7
|
-
import type { StrandConnectionOptions, SereusPluginResult, Libp2pNodeWithRepo } from './types.js';
|
|
7
|
+
import type { StrandConnectionOptions, SereusPluginResult, StrandTransactor, Libp2pNodeWithRepo } from './types.js';
|
|
8
8
|
import { STRAND_SCHEMA } from './strand-schema.js';
|
|
9
9
|
import { resolveStrandClusterSize } from './cluster-size.js';
|
|
10
|
+
import { wrapStorageWithCache } from './cached-storage.js';
|
|
10
11
|
|
|
11
12
|
const log = debug('sereus:plugin:strand');
|
|
12
13
|
const timing = debug('sereus:plugin:strand:timing');
|
|
@@ -88,7 +89,7 @@ export interface CreateNodeContext {
|
|
|
88
89
|
/** Context passed to the platform's storage-resolution strategy. */
|
|
89
90
|
export interface ResolveStorageContext {
|
|
90
91
|
strandId: string;
|
|
91
|
-
resolvedTransactor:
|
|
92
|
+
resolvedTransactor: StrandTransactor;
|
|
92
93
|
/** The storage the caller passed in `options.storage`, if any. */
|
|
93
94
|
requestedStorage?: IRawStorage;
|
|
94
95
|
}
|
|
@@ -143,44 +144,40 @@ export async function composeStrand(
|
|
|
143
144
|
port = 0,
|
|
144
145
|
enableCache = true,
|
|
145
146
|
fretProfile = 'edge',
|
|
146
|
-
|
|
147
|
+
transactor = 'network',
|
|
147
148
|
} = options;
|
|
148
149
|
|
|
149
150
|
// Resolve (and validate) up front so a nonsense value fails before any plugin
|
|
150
151
|
// registration or node creation has happened.
|
|
151
152
|
const clusterSize = resolveStrandClusterSize(options.clusterSize);
|
|
152
153
|
|
|
153
|
-
//
|
|
154
|
-
//
|
|
155
|
-
//
|
|
156
|
-
|
|
157
|
-
if (mode !== undefined) {
|
|
158
|
-
resolvedTransactor = mode === 'bootstrap' ? 'local' : 'network';
|
|
159
|
-
} else if (options.transactor !== undefined) {
|
|
160
|
-
resolvedTransactor = options.transactor;
|
|
161
|
-
} else {
|
|
162
|
-
resolvedTransactor = 'network';
|
|
163
|
-
}
|
|
154
|
+
// The storage engine every write and read below goes through — see
|
|
155
|
+
// `StrandConnectionOptions.transactor`. Named separately from the option so
|
|
156
|
+
// the default is applied exactly once and reported on the result.
|
|
157
|
+
const resolvedTransactor: StrandTransactor = transactor;
|
|
164
158
|
|
|
165
159
|
const networkName = `strand-${strandId}`;
|
|
166
|
-
log('Connecting to strand %s (network: %s,
|
|
167
|
-
strandId, networkName,
|
|
160
|
+
log('Connecting to strand %s (network: %s, transactor=%s)',
|
|
161
|
+
strandId, networkName, resolvedTransactor);
|
|
168
162
|
|
|
169
163
|
// Resolve storage. Node passes `options.storage` through; the browser
|
|
170
|
-
// defaults to IndexedDB. The resolved instance feeds BOTH the
|
|
171
|
-
//
|
|
172
|
-
// front (before pluginConfig is built).
|
|
173
|
-
|
|
164
|
+
// defaults to IndexedDB. The resolved instance feeds BOTH the local
|
|
165
|
+
// transactor's `rawStorageFactory` and node creation, so it must be decided
|
|
166
|
+
// up front (before pluginConfig is built). Wrapped in the write-through
|
|
167
|
+
// raw-storage cache (cached-storage.ts) — idempotent, so a caller that
|
|
168
|
+
// already wrapped (cadre-core's seams) passes through unchanged.
|
|
169
|
+
const resolvedStorage = platform.resolveStorage
|
|
174
170
|
? await platform.resolveStorage({ strandId, resolvedTransactor, requestedStorage: options.storage })
|
|
175
171
|
: options.storage;
|
|
172
|
+
const storage = resolvedStorage ? wrapStorageWithCache(resolvedStorage, strandId) : resolvedStorage;
|
|
176
173
|
|
|
177
174
|
// 1. Register crypto plugin (digest, sign, verify, etc.)
|
|
178
175
|
await platform.registerCrypto(db);
|
|
179
176
|
log('Registered crypto plugin');
|
|
180
177
|
|
|
181
|
-
// 2. Register optimystic plugin with transactor defaults.
|
|
182
|
-
// persistent storage, hand the same instance to the plugin so
|
|
183
|
-
//
|
|
178
|
+
// 2. Register optimystic plugin with transactor defaults. On the local
|
|
179
|
+
// transactor with persistent storage, hand the same instance to the plugin so
|
|
180
|
+
// DML persists on the host backend (not in-memory).
|
|
184
181
|
const pluginConfig: Record<string, unknown> = {
|
|
185
182
|
default_transactor: resolvedTransactor,
|
|
186
183
|
default_key_network: 'libp2p',
|
|
@@ -313,6 +310,7 @@ export async function composeStrand(
|
|
|
313
310
|
functions: [],
|
|
314
311
|
collations: [],
|
|
315
312
|
hydrated,
|
|
313
|
+
transactor: resolvedTransactor,
|
|
316
314
|
async shutdown() {
|
|
317
315
|
log('Shutting down strand connection %s', strandId);
|
|
318
316
|
await collectionFactory.shutdown();
|