@serfab/cadre-core 1.22.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cadre-node.d.ts +14 -9
- package/dist/cadre-node.js +37 -10
- package/dist/cadre-node.js.map +1 -1
- package/dist/membership-view.js +4 -1
- package/dist/membership-view.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +67 -9
- package/dist/strand-instance-manager.js +168 -22
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/types.d.ts +11 -2
- package/package.json +8 -8
- package/src/cadre-node.ts +38 -10
- package/src/membership-view.ts +4 -1
- package/src/strand-instance-manager.ts +194 -23
- package/src/types.ts +11 -2
package/dist/membership-view.js
CHANGED
|
@@ -29,7 +29,10 @@ export const MEMBERSHIP_VIEW_MAX_AGE_MS = 15000;
|
|
|
29
29
|
* Optimystic's lazy read-repair window (`clusterPolicy.readRepairWindowMs`, which cadre does
|
|
30
30
|
* not set, so this is Optimystic's default): a read of a held block checked less than this
|
|
31
31
|
* long ago is served from the local copy without asking the cohort. A read after it is
|
|
32
|
-
* served locally too, but starts a cohort consult whose landing raises a table change
|
|
32
|
+
* served locally too, but starts a cohort consult whose landing raises a table change, unless
|
|
33
|
+
* a cohort of two or three has stayed connected to this node since that block's last consult
|
|
34
|
+
* (Optimystic's quiet span). A sibling's new connection ends the span, so the reload below
|
|
35
|
+
* still consults.
|
|
33
36
|
*
|
|
34
37
|
* NOTE: a copy, since db-p2p exports no default for it. If cadre ever sets
|
|
35
38
|
* `readRepairWindowMs` in its cluster policy, derive this from that value.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"membership-view.js","sourceRoot":"","sources":["../src/membership-view.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAM,CAAC;AAEjD
|
|
1
|
+
{"version":3,"file":"membership-view.js","sourceRoot":"","sources":["../src/membership-view.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,KAAM,CAAC;AAEjD;;;;;;;;;;;GAWG;AACH,MAAM,gCAAgC,GAAG,KAAM,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,gCAAgC,GAAG,IAAK,CAAC;AA0B5F,MAAM,OAAO,cAAc;IAc1B,YAA6B,MAAiC,EAAE,UAAiC,EAAE;QAAtE,WAAM,GAAN,MAAM,CAA2B;QAZ9D,yFAAyF;QACjF,aAAQ,GAAG,CAAC,CAAC;QACrB,wGAAwG;QAChG,eAAU,GAAG,CAAC,CAAC;QACvB;;;WAGG;QACc,aAAQ,GAAG,IAAI,GAAG,EAAoC,CAAC;QAKvE,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,0BAA0B,CAAC;QAC/D,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC;IACrD,CAAC;IAED;;;;;OAKG;IACH,IAAI,CAAC,KAAc;QAClB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC3E,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;IAED,mEAAmE;IACnE,KAAK,CAAC,WAAW,CAAC,KAAc;QAC/B,OAAO,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,UAAU;QACT,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAEO,SAAS,CAAC,KAAc;QAC/B,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QAC3D,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAC/B,MAAM,MAAM,GAAG,GAAS,EAAE;YACzB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;gBACvC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7B,CAAC;QACF,CAAC,CAAC;QACF,gFAAgF;QAChF,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC/B,OAAO,IAAI,CAAC;IACb,CAAC;IAEO,KAAK,CAAC,IAAI,CAAC,KAAc,EAAE,UAAkB,EAAE,SAAiB;QACvE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvD,IAAI,WAAW,IAAI,UAAU,KAAK,IAAI,CAAC,UAAU,EAAE,CAAC;YACnD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACjB,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC3B,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;CACD"}
|
|
@@ -478,6 +478,14 @@ export declare class StrandInstanceManager {
|
|
|
478
478
|
* {@link releaseRuntime}, so a stop or a quiesce cancels it.
|
|
479
479
|
*/
|
|
480
480
|
private unreachableJoinRetries;
|
|
481
|
+
/**
|
|
482
|
+
* Per strand id, the sApp claim ({@link attachSApp}) whose schema the retained launch config
|
|
483
|
+
* carries but no apply has landed yet: the claim found no live database, or its live apply failed
|
|
484
|
+
* for a reason that may pass. Lets a build that refuses that schema fall back to the replica
|
|
485
|
+
* ({@link revertRefusedAttach}). Deleted when a database applies the schema, by that fallback,
|
|
486
|
+
* and when {@link stopStrand} begins.
|
|
487
|
+
*/
|
|
488
|
+
private unappliedAttaches;
|
|
481
489
|
/**
|
|
482
490
|
* The offline mode set per strand id ({@link setOfflineMode}); a strand absent here runs in its
|
|
483
491
|
* launch's `initialMode`. Never cleared: the mode belongs to the strand, not to one database, so
|
|
@@ -618,9 +626,17 @@ export declare class StrandInstanceManager {
|
|
|
618
626
|
* runtime still attached. Skipped when the strand was stopped or released since — a quiesce
|
|
619
627
|
* releases it and the resume after rebuilds the whole runtime — or already has a database.
|
|
620
628
|
* A failure other than an unanswered seed releases the runtime and marks the strand
|
|
621
|
-
* `'error'`, as a failed resume does
|
|
629
|
+
* `'error'`, as a failed resume does, unless it is a refusal of a claim's schema the replica
|
|
630
|
+
* can fall back from ({@link buildJoinDatabaseOrReplica}).
|
|
622
631
|
*/
|
|
623
632
|
private retryUnreachableJoin;
|
|
633
|
+
/**
|
|
634
|
+
* A deferred join's database phase ({@link buildStrandDatabase}), run once more as the replica
|
|
635
|
+
* when it fails on the schema of a claim that has not applied yet ({@link revertRefusedAttach}).
|
|
636
|
+
* The failed attempt's database is closed first: a failure leaves it registered with its gate.
|
|
637
|
+
* `'released'` when a stop began while it closed.
|
|
638
|
+
*/
|
|
639
|
+
private buildJoinDatabaseOrReplica;
|
|
624
640
|
/** Whether `instance` is still tracked, with `node` still its runtime's libp2p node. */
|
|
625
641
|
private isCurrentRuntime;
|
|
626
642
|
/**
|
|
@@ -823,6 +839,28 @@ export declare class StrandInstanceManager {
|
|
|
823
839
|
private settleTransition;
|
|
824
840
|
/** Body of {@link resumeStrand}; at most one runs per strand at a time. */
|
|
825
841
|
private runResume;
|
|
842
|
+
/**
|
|
843
|
+
* {@link buildStrandRuntime}, run once more as the replica when it fails on the schema of a
|
|
844
|
+
* claim that has not applied yet ({@link revertRefusedAttach}). Safe to run twice: a failed
|
|
845
|
+
* build releases both handles before it rejects.
|
|
846
|
+
*/
|
|
847
|
+
private buildRuntimeOrReplica;
|
|
848
|
+
/**
|
|
849
|
+
* Undo a claim of a storage replica ({@link attachSApp}) after an apply of its schema was refused
|
|
850
|
+
* (`AppSchemaRefusedError`): the retained launch config drops the sApp, so later rebuilds run the
|
|
851
|
+
* replica, and the instance gets back the replica's unset `sAppInfo` and its latency hint. Only
|
|
852
|
+
* when `error` is such a refusal, `refused` is the claim the strand has not applied yet, and the
|
|
853
|
+
* retained config still carries it, so a config rewritten since is left alone.
|
|
854
|
+
*
|
|
855
|
+
* @returns the replica's launch config, or `undefined` when nothing was undone.
|
|
856
|
+
*/
|
|
857
|
+
private revertRefusedAttach;
|
|
858
|
+
/**
|
|
859
|
+
* Forget the unapplied claim of `strandId` once `database`, given the claim's sApp config as
|
|
860
|
+
* `applied`, has applied its schema. An apply still deferred keeps it, since its retry can still be
|
|
861
|
+
* refused by the strand's rows.
|
|
862
|
+
*/
|
|
863
|
+
private forgetAppliedAttach;
|
|
826
864
|
/**
|
|
827
865
|
* Honor a founder request against an ALREADY-TRACKED strand — the seam that
|
|
828
866
|
* closes the "whoever launches first decides whether the bootstrap runs" gap:
|
|
@@ -866,19 +904,24 @@ export declare class StrandInstanceManager {
|
|
|
866
904
|
*
|
|
867
905
|
* A runtime build in flight (the replica's launch, or a hibernation wake) is waited out
|
|
868
906
|
* first, so the attach acts on the database that build produced. Then, in order: the
|
|
869
|
-
* schema signature
|
|
870
|
-
*
|
|
871
|
-
*
|
|
872
|
-
*
|
|
873
|
-
*
|
|
874
|
-
*
|
|
907
|
+
* schema signature, and the refusals the schema's text alone decides (`checkAppSchema`: a
|
|
908
|
+
* `seed` item, an item the parser skipped, a syntax error), are checked before anything
|
|
909
|
+
* changes; the retained launch config takes the sApp, so every later rebuild applies it
|
|
910
|
+
* through `composeStrand`; the live database — published, or still held by the first-sync
|
|
911
|
+
* gate — gets the schema; and only then does the instance record `sAppInfo` and the sApp's
|
|
912
|
+
* latency hint. An instance with no live database gets the config alone: a quiesced one
|
|
913
|
+
* applies the schema at its next resume, a launch waiting for a seed peer at its next
|
|
914
|
+
* database retry, and that build falls back to the replica if the strand's tables and rows
|
|
915
|
+
* refuse the schema ({@link revertRefusedAttach}).
|
|
875
916
|
*
|
|
876
917
|
* @returns `'already-attached'` when the instance already runs an sApp — a DIFFERENT one
|
|
877
918
|
* is logged and left as it is, as a second claim of a claimed strand always has been —
|
|
878
919
|
* else `'attached'`.
|
|
879
920
|
* @throws when the strand is not tracked (a caller bug, or a launch that failed while
|
|
880
|
-
* this waited on it); when the schema signature is refused, having
|
|
881
|
-
*
|
|
921
|
+
* this waited on it); when the schema signature or the schema's text is refused, having
|
|
922
|
+
* changed nothing; `AppSchemaRefusedError` when the live apply refuses the schema against
|
|
923
|
+
* the replica's tables and rows, having put the retained config back to the replica's; and
|
|
924
|
+
* whatever else the live apply throws (e.g. a quiesce closed the database mid-apply) — the
|
|
882
925
|
* retained config then still carries the sApp and `sAppInfo` stays unset, so the next
|
|
883
926
|
* claim retries the apply.
|
|
884
927
|
*
|
|
@@ -890,6 +933,21 @@ export declare class StrandInstanceManager {
|
|
|
890
933
|
attachSApp(strandId: string, sAppConfig: SAppConfig, options?: {
|
|
891
934
|
requireSignedSchemas?: boolean;
|
|
892
935
|
}): Promise<'attached' | 'already-attached'>;
|
|
936
|
+
/**
|
|
937
|
+
* The tracked instance and retained config an {@link attachSApp} claim acts on, or
|
|
938
|
+
* `'already-attached'` when the instance already runs an sApp (a claim for a different one is
|
|
939
|
+
* logged and ignored).
|
|
940
|
+
*
|
|
941
|
+
* @throws when the strand is not tracked.
|
|
942
|
+
*/
|
|
943
|
+
private claimTarget;
|
|
944
|
+
/**
|
|
945
|
+
* Apply a claim's schema to the replica's live database. A refusal by the replica's tables and
|
|
946
|
+
* rows puts the retained config back to the replica's ({@link revertRefusedAttach}): the replica
|
|
947
|
+
* holds no `App` schema, so the refused apply left nothing to drop. Any other failure leaves the
|
|
948
|
+
* claim retained and unapplied, for the next claim or build to retry.
|
|
949
|
+
*/
|
|
950
|
+
private applyClaimedSchema;
|
|
893
951
|
/**
|
|
894
952
|
* Whether `strandId` is launched but still waiting for its first sync — a joiner whose
|
|
895
953
|
* runtime is up while its database is withheld (status `'syncing'`, or `'idle'` after
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import debug from 'debug';
|
|
2
2
|
import { createLibp2pNode } from '@optimystic/db-p2p';
|
|
3
|
-
import { wrapStorageWithCache, disposeStorageCache } from '@serfab/quereus-plugin-sereus';
|
|
3
|
+
import { wrapStorageWithCache, disposeStorageCache, checkAppSchema, appSchemaRefusalOf } from '@serfab/quereus-plugin-sereus';
|
|
4
4
|
import { StrandDatabase } from './strand-database.js';
|
|
5
5
|
import { PeerJoinBackfill } from './peer-join-backfill.js';
|
|
6
6
|
import { strandNetworkStatePersistence } from './strand-network-state.js';
|
|
@@ -271,6 +271,14 @@ export class StrandInstanceManager {
|
|
|
271
271
|
* {@link releaseRuntime}, so a stop or a quiesce cancels it.
|
|
272
272
|
*/
|
|
273
273
|
this.unreachableJoinRetries = new Map();
|
|
274
|
+
/**
|
|
275
|
+
* Per strand id, the sApp claim ({@link attachSApp}) whose schema the retained launch config
|
|
276
|
+
* carries but no apply has landed yet: the claim found no live database, or its live apply failed
|
|
277
|
+
* for a reason that may pass. Lets a build that refuses that schema fall back to the replica
|
|
278
|
+
* ({@link revertRefusedAttach}). Deleted when a database applies the schema, by that fallback,
|
|
279
|
+
* and when {@link stopStrand} begins.
|
|
280
|
+
*/
|
|
281
|
+
this.unappliedAttaches = new Map();
|
|
274
282
|
/**
|
|
275
283
|
* The offline mode set per strand id ({@link setOfflineMode}); a strand absent here runs in its
|
|
276
284
|
* launch's `initialMode`. Never cleared: the mode belongs to the strand, not to one database, so
|
|
@@ -882,6 +890,7 @@ export class StrandInstanceManager {
|
|
|
882
890
|
await strandDb.close();
|
|
883
891
|
return 'released';
|
|
884
892
|
}
|
|
893
|
+
this.forgetAppliedAttach(strandId, config.sAppConfig, strandDb);
|
|
885
894
|
// The first-sync write gate. A founder just wrote the Header in its bootstrap; any
|
|
886
895
|
// other machine may commit only once it holds the Header — which it can only have
|
|
887
896
|
// received from a peer (now, or on an earlier run over the same store) — and has read
|
|
@@ -1044,7 +1053,8 @@ export class StrandInstanceManager {
|
|
|
1044
1053
|
* runtime still attached. Skipped when the strand was stopped or released since — a quiesce
|
|
1045
1054
|
* releases it and the resume after rebuilds the whole runtime — or already has a database.
|
|
1046
1055
|
* A failure other than an unanswered seed releases the runtime and marks the strand
|
|
1047
|
-
* `'error'`, as a failed resume does
|
|
1056
|
+
* `'error'`, as a failed resume does, unless it is a refusal of a claim's schema the replica
|
|
1057
|
+
* can fall back from ({@link buildJoinDatabaseOrReplica}).
|
|
1048
1058
|
*/
|
|
1049
1059
|
async retryUnreachableJoin(instance) {
|
|
1050
1060
|
const strandId = instance.strandId;
|
|
@@ -1059,7 +1069,7 @@ export class StrandInstanceManager {
|
|
|
1059
1069
|
try {
|
|
1060
1070
|
// Tracked like any runtime build, so a wake, a quiesce or an sApp attach waits for it
|
|
1061
1071
|
// rather than acting on a database that is still initializing.
|
|
1062
|
-
build = await this.trackRuntimeBuild(strandId, this.
|
|
1072
|
+
build = await this.trackRuntimeBuild(strandId, this.buildJoinDatabaseOrReplica(instance, config, node));
|
|
1063
1073
|
}
|
|
1064
1074
|
catch (error) {
|
|
1065
1075
|
if (!this.isCurrentRuntime(instance, node)) {
|
|
@@ -1078,6 +1088,35 @@ export class StrandInstanceManager {
|
|
|
1078
1088
|
this.deferUnreachableJoin(instance);
|
|
1079
1089
|
}
|
|
1080
1090
|
}
|
|
1091
|
+
/**
|
|
1092
|
+
* A deferred join's database phase ({@link buildStrandDatabase}), run once more as the replica
|
|
1093
|
+
* when it fails on the schema of a claim that has not applied yet ({@link revertRefusedAttach}).
|
|
1094
|
+
* The failed attempt's database is closed first: a failure leaves it registered with its gate.
|
|
1095
|
+
* `'released'` when a stop began while it closed.
|
|
1096
|
+
*/
|
|
1097
|
+
async buildJoinDatabaseOrReplica(instance, config, node) {
|
|
1098
|
+
try {
|
|
1099
|
+
return await this.buildStrandDatabase(instance, config, node);
|
|
1100
|
+
}
|
|
1101
|
+
catch (error) {
|
|
1102
|
+
const replicaConfig = this.isCurrentRuntime(instance, node)
|
|
1103
|
+
? this.revertRefusedAttach(instance, config.sAppConfig, error)
|
|
1104
|
+
: undefined;
|
|
1105
|
+
if (!replicaConfig) {
|
|
1106
|
+
throw error;
|
|
1107
|
+
}
|
|
1108
|
+
const failedAttempt = this.firstSyncGates.get(instance.strandId);
|
|
1109
|
+
if (failedAttempt) {
|
|
1110
|
+
await this.discardDatabaseAttempt(instance.strandId, failedAttempt);
|
|
1111
|
+
}
|
|
1112
|
+
if (!this.isCurrentRuntime(instance, node) || instance.status === 'stopping') {
|
|
1113
|
+
// A stop that began while the failed attempt closed found no gate to release, so a
|
|
1114
|
+
// database built now would outlive the stop, open and unreleased.
|
|
1115
|
+
return 'released';
|
|
1116
|
+
}
|
|
1117
|
+
return await this.buildStrandDatabase(instance, replicaConfig, node);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1081
1120
|
/** Whether `instance` is still tracked, with `node` still its runtime's libp2p node. */
|
|
1082
1121
|
isCurrentRuntime(instance, node) {
|
|
1083
1122
|
return !this.stopping && this.instances.get(instance.strandId) === instance && instance.libp2pNode === node;
|
|
@@ -1596,7 +1635,7 @@ export class StrandInstanceManager {
|
|
|
1596
1635
|
this.launchConfigs.set(strandId, resumeConfig);
|
|
1597
1636
|
instance.status = 'starting';
|
|
1598
1637
|
try {
|
|
1599
|
-
await this.trackRuntimeBuild(strandId, this.
|
|
1638
|
+
await this.trackRuntimeBuild(strandId, this.buildRuntimeOrReplica(instance, resumeConfig));
|
|
1600
1639
|
timing('[resumeStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
|
|
1601
1640
|
log('Strand %s resumed successfully', strandId);
|
|
1602
1641
|
return instance;
|
|
@@ -1608,6 +1647,60 @@ export class StrandInstanceManager {
|
|
|
1608
1647
|
throw error;
|
|
1609
1648
|
}
|
|
1610
1649
|
}
|
|
1650
|
+
/**
|
|
1651
|
+
* {@link buildStrandRuntime}, run once more as the replica when it fails on the schema of a
|
|
1652
|
+
* claim that has not applied yet ({@link revertRefusedAttach}). Safe to run twice: a failed
|
|
1653
|
+
* build releases both handles before it rejects.
|
|
1654
|
+
*/
|
|
1655
|
+
async buildRuntimeOrReplica(instance, config) {
|
|
1656
|
+
try {
|
|
1657
|
+
await this.buildStrandRuntime(instance, config);
|
|
1658
|
+
}
|
|
1659
|
+
catch (error) {
|
|
1660
|
+
const replicaConfig = this.revertRefusedAttach(instance, config.sAppConfig, error);
|
|
1661
|
+
if (!replicaConfig) {
|
|
1662
|
+
throw error;
|
|
1663
|
+
}
|
|
1664
|
+
await this.buildStrandRuntime(instance, replicaConfig);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
/**
|
|
1668
|
+
* Undo a claim of a storage replica ({@link attachSApp}) after an apply of its schema was refused
|
|
1669
|
+
* (`AppSchemaRefusedError`): the retained launch config drops the sApp, so later rebuilds run the
|
|
1670
|
+
* replica, and the instance gets back the replica's unset `sAppInfo` and its latency hint. Only
|
|
1671
|
+
* when `error` is such a refusal, `refused` is the claim the strand has not applied yet, and the
|
|
1672
|
+
* retained config still carries it, so a config rewritten since is left alone.
|
|
1673
|
+
*
|
|
1674
|
+
* @returns the replica's launch config, or `undefined` when nothing was undone.
|
|
1675
|
+
*/
|
|
1676
|
+
revertRefusedAttach(instance, refused, error) {
|
|
1677
|
+
const strandId = instance.strandId;
|
|
1678
|
+
const unapplied = this.unappliedAttaches.get(strandId);
|
|
1679
|
+
const config = this.launchConfigs.get(strandId);
|
|
1680
|
+
const refusal = appSchemaRefusalOf(error);
|
|
1681
|
+
if (!refusal || !unapplied || !config || this.stopping || this.instances.get(strandId) !== instance
|
|
1682
|
+
|| refused !== unapplied.sAppConfig || config.sAppConfig !== refused) {
|
|
1683
|
+
return undefined;
|
|
1684
|
+
}
|
|
1685
|
+
this.unappliedAttaches.delete(strandId);
|
|
1686
|
+
const { sAppConfig: _refused, ...replicaConfig } = config;
|
|
1687
|
+
this.launchConfigs.set(strandId, replicaConfig);
|
|
1688
|
+
instance.sAppInfo = undefined;
|
|
1689
|
+
instance.latencyHint = unapplied.latencyHint;
|
|
1690
|
+
log('Strand %s: the schema of sApp %s, which claimed this storage replica, was refused here; ' +
|
|
1691
|
+
'the strand runs as a replica again: %s', strandId, refused.id, refusal.message);
|
|
1692
|
+
return replicaConfig;
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Forget the unapplied claim of `strandId` once `database`, given the claim's sApp config as
|
|
1696
|
+
* `applied`, has applied its schema. An apply still deferred keeps it, since its retry can still be
|
|
1697
|
+
* refused by the strand's rows.
|
|
1698
|
+
*/
|
|
1699
|
+
forgetAppliedAttach(strandId, applied, database) {
|
|
1700
|
+
if (applied && this.unappliedAttaches.get(strandId)?.sAppConfig === applied && database.appSchemaState() === 'applied') {
|
|
1701
|
+
this.unappliedAttaches.delete(strandId);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1611
1704
|
/**
|
|
1612
1705
|
* Honor a founder request against an ALREADY-TRACKED strand — the seam that
|
|
1613
1706
|
* closes the "whoever launches first decides whether the bootstrap runs" gap:
|
|
@@ -1676,19 +1769,24 @@ export class StrandInstanceManager {
|
|
|
1676
1769
|
*
|
|
1677
1770
|
* A runtime build in flight (the replica's launch, or a hibernation wake) is waited out
|
|
1678
1771
|
* first, so the attach acts on the database that build produced. Then, in order: the
|
|
1679
|
-
* schema signature
|
|
1680
|
-
*
|
|
1681
|
-
*
|
|
1682
|
-
*
|
|
1683
|
-
*
|
|
1684
|
-
*
|
|
1772
|
+
* schema signature, and the refusals the schema's text alone decides (`checkAppSchema`: a
|
|
1773
|
+
* `seed` item, an item the parser skipped, a syntax error), are checked before anything
|
|
1774
|
+
* changes; the retained launch config takes the sApp, so every later rebuild applies it
|
|
1775
|
+
* through `composeStrand`; the live database — published, or still held by the first-sync
|
|
1776
|
+
* gate — gets the schema; and only then does the instance record `sAppInfo` and the sApp's
|
|
1777
|
+
* latency hint. An instance with no live database gets the config alone: a quiesced one
|
|
1778
|
+
* applies the schema at its next resume, a launch waiting for a seed peer at its next
|
|
1779
|
+
* database retry, and that build falls back to the replica if the strand's tables and rows
|
|
1780
|
+
* refuse the schema ({@link revertRefusedAttach}).
|
|
1685
1781
|
*
|
|
1686
1782
|
* @returns `'already-attached'` when the instance already runs an sApp — a DIFFERENT one
|
|
1687
1783
|
* is logged and left as it is, as a second claim of a claimed strand always has been —
|
|
1688
1784
|
* else `'attached'`.
|
|
1689
1785
|
* @throws when the strand is not tracked (a caller bug, or a launch that failed while
|
|
1690
|
-
* this waited on it); when the schema signature is refused, having
|
|
1691
|
-
*
|
|
1786
|
+
* this waited on it); when the schema signature or the schema's text is refused, having
|
|
1787
|
+
* changed nothing; `AppSchemaRefusedError` when the live apply refuses the schema against
|
|
1788
|
+
* the replica's tables and rows, having put the retained config back to the replica's; and
|
|
1789
|
+
* whatever else the live apply throws (e.g. a quiesce closed the database mid-apply) — the
|
|
1692
1790
|
* retained config then still carries the sApp and `sAppInfo` stays unset, so the next
|
|
1693
1791
|
* claim retries the apply.
|
|
1694
1792
|
*
|
|
@@ -1699,26 +1797,35 @@ export class StrandInstanceManager {
|
|
|
1699
1797
|
*/
|
|
1700
1798
|
async attachSApp(strandId, sAppConfig, options = {}) {
|
|
1701
1799
|
await this.settleRuntimeBuilds(strandId);
|
|
1702
|
-
|
|
1703
|
-
const config = this.launchConfigs.get(strandId);
|
|
1704
|
-
if (!instance || !config) {
|
|
1705
|
-
throw new Error(`Cannot attach an sApp to strand ${strandId}: not tracked`);
|
|
1706
|
-
}
|
|
1707
|
-
if (instance.sAppInfo) {
|
|
1708
|
-
if (instance.sAppInfo.id !== sAppConfig.id) {
|
|
1709
|
-
log('attachSApp: strand %s already runs sApp %s — the claim for sApp %s is ignored', strandId, instance.sAppInfo.id, sAppConfig.id);
|
|
1710
|
-
}
|
|
1800
|
+
if (this.claimTarget(strandId, sAppConfig) === 'already-attached') {
|
|
1711
1801
|
return 'already-attached';
|
|
1712
1802
|
}
|
|
1713
1803
|
const sAppInfo = verifiedSAppInfo(strandId, sAppConfig, options.requireSignedSchemas);
|
|
1804
|
+
await checkAppSchema(sAppConfig.schema);
|
|
1805
|
+
// The check yielded, so a wake or another claim may have started meanwhile: wait out its build
|
|
1806
|
+
// and read the instance and config again, or the config written below would drop what a
|
|
1807
|
+
// resume just retained (its fresh `bootstrapNodes`).
|
|
1808
|
+
await this.settleRuntimeBuilds(strandId);
|
|
1809
|
+
const target = this.claimTarget(strandId, sAppConfig);
|
|
1810
|
+
if (target === 'already-attached') {
|
|
1811
|
+
return target;
|
|
1812
|
+
}
|
|
1813
|
+
const { instance, config } = target;
|
|
1714
1814
|
// A fresh object rather than mutating in place: startStrand retains the CALLER'S
|
|
1715
1815
|
// config object, which is not ours to rewrite.
|
|
1716
1816
|
this.launchConfigs.set(strandId, { ...config, sAppConfig });
|
|
1817
|
+
this.unappliedAttaches.set(strandId, { sAppConfig, latencyHint: instance.latencyHint });
|
|
1717
1818
|
const database = instance.database ?? this.firstSyncGates.get(strandId)?.database;
|
|
1718
1819
|
if (database) {
|
|
1719
|
-
await
|
|
1820
|
+
await this.applyClaimedSchema(instance, database, sAppConfig);
|
|
1720
1821
|
}
|
|
1721
1822
|
else {
|
|
1823
|
+
// NOTE: accepted gap — this claim resolves `'attached'` before any apply, so the app learns of
|
|
1824
|
+
// a refusal by the strand's rows only from the log and from `sAppInfo` going back to unset when
|
|
1825
|
+
// the build falls back to the replica; `CadreNode.sAppConfigs` keeps the claim, so a restart's
|
|
1826
|
+
// launch with it is refused and suppressed (`suppressIfAppSchemaRefused`). Failing the claim
|
|
1827
|
+
// instead would mean waking a quiesced replica to apply now, and would still not cover a
|
|
1828
|
+
// launch waiting for a seed peer.
|
|
1722
1829
|
log('attachSApp: strand %s has no live database — its next build applies the sApp schema', strandId);
|
|
1723
1830
|
}
|
|
1724
1831
|
instance.sAppInfo = sAppInfo;
|
|
@@ -1731,6 +1838,43 @@ export class StrandInstanceManager {
|
|
|
1731
1838
|
log('Strand %s upgraded from storage replica to %s', strandId, describeSApp(sAppConfig));
|
|
1732
1839
|
return 'attached';
|
|
1733
1840
|
}
|
|
1841
|
+
/**
|
|
1842
|
+
* The tracked instance and retained config an {@link attachSApp} claim acts on, or
|
|
1843
|
+
* `'already-attached'` when the instance already runs an sApp (a claim for a different one is
|
|
1844
|
+
* logged and ignored).
|
|
1845
|
+
*
|
|
1846
|
+
* @throws when the strand is not tracked.
|
|
1847
|
+
*/
|
|
1848
|
+
claimTarget(strandId, sAppConfig) {
|
|
1849
|
+
const instance = this.instances.get(strandId);
|
|
1850
|
+
const config = this.launchConfigs.get(strandId);
|
|
1851
|
+
if (!instance || !config) {
|
|
1852
|
+
throw new Error(`Cannot attach an sApp to strand ${strandId}: not tracked`);
|
|
1853
|
+
}
|
|
1854
|
+
if (!instance.sAppInfo) {
|
|
1855
|
+
return { instance, config };
|
|
1856
|
+
}
|
|
1857
|
+
if (instance.sAppInfo.id !== sAppConfig.id) {
|
|
1858
|
+
log('attachSApp: strand %s already runs sApp %s — the claim for sApp %s is ignored', strandId, instance.sAppInfo.id, sAppConfig.id);
|
|
1859
|
+
}
|
|
1860
|
+
return 'already-attached';
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Apply a claim's schema to the replica's live database. A refusal by the replica's tables and
|
|
1864
|
+
* rows puts the retained config back to the replica's ({@link revertRefusedAttach}): the replica
|
|
1865
|
+
* holds no `App` schema, so the refused apply left nothing to drop. Any other failure leaves the
|
|
1866
|
+
* claim retained and unapplied, for the next claim or build to retry.
|
|
1867
|
+
*/
|
|
1868
|
+
async applyClaimedSchema(instance, database, sAppConfig) {
|
|
1869
|
+
try {
|
|
1870
|
+
await database.attachAppSchema(sAppConfig);
|
|
1871
|
+
}
|
|
1872
|
+
catch (error) {
|
|
1873
|
+
this.revertRefusedAttach(instance, sAppConfig, error);
|
|
1874
|
+
throw error;
|
|
1875
|
+
}
|
|
1876
|
+
this.forgetAppliedAttach(instance.strandId, sAppConfig, database);
|
|
1877
|
+
}
|
|
1734
1878
|
/**
|
|
1735
1879
|
* Whether `strandId` is launched but still waiting for its first sync — a joiner whose
|
|
1736
1880
|
* runtime is up while its database is withheld (status `'syncing'`, or `'idle'` after
|
|
@@ -1986,6 +2130,8 @@ export class StrandInstanceManager {
|
|
|
1986
2130
|
}
|
|
1987
2131
|
log('Stopping strand instance: %s', strandId);
|
|
1988
2132
|
instance.status = 'stopping';
|
|
2133
|
+
// Before the release awaits, so a build failing meanwhile does not rebuild as the replica.
|
|
2134
|
+
this.unappliedAttaches.delete(strandId);
|
|
1989
2135
|
this.rejectWritableWaiters(strandId, 'was stopped');
|
|
1990
2136
|
try {
|
|
1991
2137
|
await this.releaseRuntime(instance);
|