@serfab/quereus-plugin-sereus 1.19.1 → 1.20.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 +1 -1
- package/dist/app-schema.d.ts +30 -12
- package/dist/app-schema.js +95 -26
- package/dist/app-schema.js.map +1 -1
- package/dist/cached-storage.d.ts +3 -1
- package/dist/cached-storage.js +12 -2
- package/dist/cached-storage.js.map +1 -1
- package/dist/compose-strand.js +9 -5
- package/dist/compose-strand.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/plugin-browser.js +28 -28
- package/dist/plugin-browser.js.map +4 -4
- package/dist/types.d.ts +16 -8
- package/package.json +8 -8
- package/src/app-schema.ts +121 -30
- package/src/cached-storage.ts +13 -2
- package/src/compose-strand.ts +9 -5
- package/src/index.ts +1 -1
- package/src/types.ts +17 -8
package/dist/types.d.ts
CHANGED
|
@@ -93,12 +93,14 @@ export type StrandOfflineController = Pick<OfflineMode, 'enter' | 'exit' | 'isEn
|
|
|
93
93
|
/**
|
|
94
94
|
* How a connection's sApp schema apply ended (see {@link SereusPluginResult.appSchema}):
|
|
95
95
|
*
|
|
96
|
-
* - `applied` — the schema is in place. `deferred` when
|
|
97
|
-
*
|
|
98
|
-
* - `failed` — a deferred retry failed for
|
|
99
|
-
*
|
|
96
|
+
* - `applied` — the schema is in place. `deferred` when the first apply was refused (by queued
|
|
97
|
+
* offline transactions, or because the strand's cohort could not be reached) and a later one landed.
|
|
98
|
+
* - `failed` — a deferred retry failed for another reason; the connection keeps the schema it had
|
|
99
|
+
* before.
|
|
100
100
|
* - `cancelled` — the connection shut down before a deferred retry landed.
|
|
101
101
|
*/
|
|
102
|
+
/** What a deferred sApp schema apply waits for (see {@link SereusPluginResult.appSchemaWaitingFor}). */
|
|
103
|
+
export type AppSchemaWait = 'queue' | 'cohort';
|
|
102
104
|
export type AppSchemaOutcome = {
|
|
103
105
|
state: 'applied';
|
|
104
106
|
deferred?: true;
|
|
@@ -163,12 +165,18 @@ export interface SereusPluginResult {
|
|
|
163
165
|
/**
|
|
164
166
|
* Present when {@link StrandConnectionOptions.schema} was given; never rejects. Resolves
|
|
165
167
|
* `{ state: 'applied' }` at once when the apply succeeded at connect. When queued offline
|
|
166
|
-
* transactions refused it, the
|
|
167
|
-
*
|
|
168
|
-
* {@link shutdown} runs first.
|
|
168
|
+
* transactions refused it, or the strand's cohort could not be reached, the connection comes up
|
|
169
|
+
* on the `App` schema it already had and retries ({@link appSchemaWaitingFor}); this settles when
|
|
170
|
+
* a retry lands, fails otherwise, or {@link shutdown} runs first.
|
|
169
171
|
*/
|
|
170
172
|
appSchema?: Promise<AppSchemaOutcome>;
|
|
171
|
-
/**
|
|
173
|
+
/**
|
|
174
|
+
* What the connect-time apply waits for: `'queue'`, queued offline transactions (retried each time
|
|
175
|
+
* the queue drains), or `'cohort'`, the strand's members (retried when the strand node connects to
|
|
176
|
+
* a peer, and on a backoff). Present only when it was deferred.
|
|
177
|
+
*/
|
|
178
|
+
appSchemaWaitingFor?: AppSchemaWait;
|
|
179
|
+
/** The queued transaction ids that refused the connect-time apply. Present only when the queue refused it. */
|
|
172
180
|
appSchemaDeferredBy?: readonly string[];
|
|
173
181
|
/** Shuts down the libp2p node and collection factory. Call when done. */
|
|
174
182
|
shutdown: () => Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serfab/quereus-plugin-sereus",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Quereus plugin that connects to a Sereus strand for SQL access",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -68,13 +68,13 @@
|
|
|
68
68
|
"@libp2p/circuit-relay-v2": "^4.2.13",
|
|
69
69
|
"@libp2p/interface": "^3.3.0",
|
|
70
70
|
"@libp2p/websockets": "^10.1.21",
|
|
71
|
-
"@optimystic/db-core": "^1.
|
|
72
|
-
"@optimystic/db-p2p": "^1.
|
|
73
|
-
"@optimystic/db-p2p-storage-fs": "^1.
|
|
74
|
-
"@optimystic/db-p2p-storage-web": "^1.
|
|
75
|
-
"@optimystic/quereus-plugin-crypto": "^1.
|
|
76
|
-
"@optimystic/quereus-plugin-optimystic": "^1.
|
|
77
|
-
"@quereus/quereus": "^4.
|
|
71
|
+
"@optimystic/db-core": "^1.23.0",
|
|
72
|
+
"@optimystic/db-p2p": "^1.23.0",
|
|
73
|
+
"@optimystic/db-p2p-storage-fs": "^1.23.0",
|
|
74
|
+
"@optimystic/db-p2p-storage-web": "^1.23.0",
|
|
75
|
+
"@optimystic/quereus-plugin-crypto": "^1.23.0",
|
|
76
|
+
"@optimystic/quereus-plugin-optimystic": "^1.23.0",
|
|
77
|
+
"@quereus/quereus": "^4.23.0",
|
|
78
78
|
"debug": "^4.4.3"
|
|
79
79
|
},
|
|
80
80
|
"quereus": {
|
package/src/app-schema.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import debug from 'debug';
|
|
2
2
|
import { TransactionActiveError, type Database } from '@quereus/quereus';
|
|
3
|
+
import type { Libp2p } from '@libp2p/interface';
|
|
4
|
+
import { classifyCommitFailure } from '@optimystic/db-core';
|
|
3
5
|
import { QueuedEntriesBlockSchemaChangeError, QueueDisposedError } from '@optimystic/quereus-plugin-optimystic/plugin';
|
|
4
|
-
import type { StrandOfflineController, AppSchemaOutcome } from './types.js';
|
|
6
|
+
import type { StrandOfflineController, AppSchemaOutcome, AppSchemaWait } from './types.js';
|
|
5
7
|
|
|
6
8
|
const log = debug('sereus:plugin:strand');
|
|
7
9
|
|
|
@@ -9,7 +11,7 @@ const log = debug('sereus:plugin:strand');
|
|
|
9
11
|
* Step 7 of `composeStrand`: declare the sApp's schema as `App` and apply it — a
|
|
10
12
|
* declarative diff against the catalog, so re-applying a schema that is already in place
|
|
11
13
|
* emits nothing. The one site that applies an `App` schema: `composeStrand` calls it at
|
|
12
|
-
* bring-up (through {@link
|
|
14
|
+
* bring-up (through {@link applyAppSchemaOrDefer}, whose deferred retry shares its
|
|
13
15
|
* declaration), and cadre-core's `StrandDatabase.attachAppSchema` calls it on a live storage
|
|
14
16
|
* replica when an app claims the strand. The database must already be composed (optimystic
|
|
15
17
|
* set as the default vtab), or the tables land in memory instead of the strand.
|
|
@@ -38,22 +40,44 @@ async function declareAppSchema(db: Database, schema: string): Promise<void> {
|
|
|
38
40
|
assertNoSeedItems(db);
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
/** What {@link
|
|
43
|
+
/** What {@link applyAppSchemaOrDefer} reports, in the shape `SereusPluginResult` carries it. */
|
|
42
44
|
export interface AppSchemaApply {
|
|
43
45
|
appSchema: Promise<AppSchemaOutcome>;
|
|
46
|
+
/** What the connect-time apply waits for. Present only when it was deferred. */
|
|
47
|
+
appSchemaWaitingFor?: AppSchemaWait;
|
|
48
|
+
/** The queued transaction ids that refused the connect-time apply. Present only when the queue refused it. */
|
|
44
49
|
appSchemaDeferredBy?: readonly string[];
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
/** The strand node's peer events, which a retry waiting for the cohort wakes on. */
|
|
53
|
+
export type PeerConnectEvents = Pick<Libp2p, 'addEventListener' | 'removeEventListener'>;
|
|
54
|
+
|
|
55
|
+
/** Where a deferred apply waits: the offline queue, when there is one, and the strand node, when there is one. */
|
|
56
|
+
export interface AppSchemaWaits {
|
|
57
|
+
queue?: Pick<StrandOfflineController, 'whenDrained'>;
|
|
58
|
+
peers?: PeerConnectEvents;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Why an apply was refused, when it is a refusal a later retry can get past. */
|
|
62
|
+
type Deferral = { wait: 'queue'; transactionIds: readonly string[] } | { wait: 'cohort' };
|
|
63
|
+
|
|
47
64
|
/**
|
|
48
|
-
* Step 7 of `composeStrand`: {@link applyAppSchema}, except that an apply refused
|
|
49
|
-
*
|
|
50
|
-
* throw. Quereus unwinds the refused apply, so `db` keeps the `App` schema it had; the apply is
|
|
51
|
-
* retried in the background each time `queue` drains, for as long as the queue is what refuses it,
|
|
52
|
-
* and `appSchema` settles with how that ended. Any other error from the first apply is thrown.
|
|
65
|
+
* Step 7 of `composeStrand`: {@link applyAppSchema}, except that an apply refused for a reason
|
|
66
|
+
* that passes does not throw. Two such reasons:
|
|
53
67
|
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* the
|
|
68
|
+
* - queued offline transactions wrote a table the apply changes (`QueuedEntriesBlockSchemaChangeError`);
|
|
69
|
+
* retried each time the queue drains;
|
|
70
|
+
* - the strand's cohort could not be reached (Optimystic's `classifyCommitFailure` says
|
|
71
|
+
* `cohort-unreachable`): an apply writes the strand's catalog, which needs the cohort, so a member
|
|
72
|
+
* alone after an app update would otherwise not open the strand at all (gotchoices/sereus#55).
|
|
73
|
+
* Retried when the strand node connects to a peer, and on a backoff ({@link COHORT_RETRY_MS}).
|
|
74
|
+
*
|
|
75
|
+
* Quereus unwinds a refused apply, so `db` keeps the `App` schema it had, and `appSchema` settles
|
|
76
|
+
* with how the retries ended. Any other error from the first apply is thrown.
|
|
77
|
+
*
|
|
78
|
+
* Waiting before returning was rejected: that withholds the strand from the app right after an app
|
|
79
|
+
* update, which is when a phone is most likely to be offline. So was refusing the connect: the queue
|
|
80
|
+
* can drain only while the composition is alive.
|
|
57
81
|
*
|
|
58
82
|
* `released` is aborted when the composition shuts down; a retry not yet landed then settles
|
|
59
83
|
* `cancelled`.
|
|
@@ -63,10 +87,10 @@ export interface AppSchemaApply {
|
|
|
63
87
|
* apply would be committed or rolled back with the app's rows, and a rollback leaves the index it
|
|
64
88
|
* built holding entries for rows that are gone.
|
|
65
89
|
*/
|
|
66
|
-
export async function
|
|
90
|
+
export async function applyAppSchemaOrDefer(
|
|
67
91
|
db: Database,
|
|
68
92
|
schema: string,
|
|
69
|
-
|
|
93
|
+
waits: AppSchemaWaits,
|
|
70
94
|
released: AbortSignal,
|
|
71
95
|
): Promise<AppSchemaApply> {
|
|
72
96
|
try {
|
|
@@ -74,30 +98,34 @@ export async function applyAppSchemaAfterQueue(
|
|
|
74
98
|
log('sApp schema applied');
|
|
75
99
|
return { appSchema: Promise.resolve({ state: 'applied' }) };
|
|
76
100
|
} catch (err) {
|
|
77
|
-
const
|
|
78
|
-
if (!
|
|
101
|
+
const deferral = deferralOf(err);
|
|
102
|
+
if (!deferral) {
|
|
79
103
|
throw err;
|
|
80
104
|
}
|
|
81
|
-
|
|
105
|
+
logDeferral('sApp schema apply', deferral);
|
|
82
106
|
return {
|
|
83
|
-
appSchema:
|
|
84
|
-
|
|
107
|
+
appSchema: retryDeferredAppSchema(db, schema, deferral, waits, released),
|
|
108
|
+
appSchemaWaitingFor: deferral.wait,
|
|
109
|
+
...(deferral.wait === 'queue' && { appSchemaDeferredBy: deferral.transactionIds }),
|
|
85
110
|
};
|
|
86
111
|
}
|
|
87
112
|
}
|
|
88
113
|
|
|
89
|
-
async function
|
|
114
|
+
async function retryDeferredAppSchema(
|
|
90
115
|
db: Database,
|
|
91
116
|
schema: string,
|
|
92
|
-
|
|
117
|
+
first: Deferral,
|
|
118
|
+
waits: AppSchemaWaits,
|
|
93
119
|
released: AbortSignal,
|
|
94
120
|
): Promise<AppSchemaOutcome> {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// as undrained, so no such state is known; if one shows up as a busy loop, back off between tries.
|
|
121
|
+
let deferral = first;
|
|
122
|
+
let cohortRetries = 0;
|
|
98
123
|
for (;;) {
|
|
99
124
|
try {
|
|
100
|
-
await
|
|
125
|
+
await nextChance(deferral, waits, cohortRetries, released);
|
|
126
|
+
if (deferral.wait === 'cohort') {
|
|
127
|
+
cohortRetries++;
|
|
128
|
+
}
|
|
101
129
|
if (released.aborted) {
|
|
102
130
|
return { state: 'cancelled' };
|
|
103
131
|
}
|
|
@@ -115,26 +143,89 @@ async function retryAppSchemaAfterDrain(
|
|
|
115
143
|
await abortableDelay(OPEN_TRANSACTION_RETRY_MS, released);
|
|
116
144
|
continue;
|
|
117
145
|
}
|
|
118
|
-
const
|
|
119
|
-
if (!
|
|
146
|
+
const again = deferralOf(err);
|
|
147
|
+
if (!again) {
|
|
120
148
|
log('Deferred sApp schema apply failed: %O', err);
|
|
121
149
|
return { state: 'failed', error: err };
|
|
122
150
|
}
|
|
123
|
-
|
|
151
|
+
logDeferral('Deferred sApp schema apply', again);
|
|
152
|
+
deferral = again;
|
|
124
153
|
}
|
|
125
154
|
}
|
|
126
155
|
}
|
|
127
156
|
|
|
128
|
-
/**
|
|
129
|
-
function
|
|
157
|
+
/** Waits for whatever may get the retry past `deferral`. */
|
|
158
|
+
async function nextChance(deferral: Deferral, waits: AppSchemaWaits, cohortRetries: number, released: AbortSignal): Promise<void> {
|
|
159
|
+
if (deferral.wait === 'queue' && waits.queue) {
|
|
160
|
+
// NOTE: this loops without a pause when `whenDrained` resolves at once while the refusal stands
|
|
161
|
+
// (a segment left sealed with nothing queued, say). Upstream counts a removal still in progress
|
|
162
|
+
// as undrained, so no such state is known; if one shows up as a busy loop, back off between tries.
|
|
163
|
+
await waits.queue.whenDrained();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
await nextCohortChance(waits.peers, cohortRetryDelay(cohortRetries), released);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The refusal `err` is or wraps, when a later retry can get past it: Quereus rewraps an error out of
|
|
171
|
+
* `apply schema`, keeping it as `cause`.
|
|
172
|
+
*/
|
|
173
|
+
function deferralOf(err: unknown): Deferral | undefined {
|
|
130
174
|
for (let at = err; at instanceof Error; at = at.cause) {
|
|
131
175
|
if (at instanceof QueuedEntriesBlockSchemaChangeError) {
|
|
132
|
-
return at;
|
|
176
|
+
return { wait: 'queue', transactionIds: at.transactionIds };
|
|
177
|
+
}
|
|
178
|
+
if (classifyCommitFailure(at).kind === 'cohort-unreachable') {
|
|
179
|
+
return { wait: 'cohort' };
|
|
133
180
|
}
|
|
134
181
|
}
|
|
135
182
|
return undefined;
|
|
136
183
|
}
|
|
137
184
|
|
|
185
|
+
function logDeferral(what: string, deferral: Deferral): void {
|
|
186
|
+
if (deferral.wait === 'queue') {
|
|
187
|
+
log('%s refused by queued transactions %o; retrying once they settle', what, deferral.transactionIds);
|
|
188
|
+
} else {
|
|
189
|
+
log('%s could not reach the strand cohort; retrying when a peer connects', what);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Backoff for a retry waiting for the cohort, by how many came before it. A connect wakes the retry
|
|
195
|
+
* sooner, so the backoff only bounds how long a missed or too-early connect leaves the schema behind.
|
|
196
|
+
*/
|
|
197
|
+
const COHORT_RETRY_MS = [15_000, 30_000, 60_000, 120_000, 300_000] as const;
|
|
198
|
+
|
|
199
|
+
function cohortRetryDelay(retriesBefore: number): number {
|
|
200
|
+
return COHORT_RETRY_MS[Math.min(retriesBefore, COHORT_RETRY_MS.length - 1)]!;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* How long a retry woken by a connect waits first, so the peer is identified and in the ring before
|
|
205
|
+
* the apply asks for its cohort; a retry that still finds it missing waits for the next chance.
|
|
206
|
+
*/
|
|
207
|
+
const COHORT_CONNECT_SETTLE_MS = 2_000;
|
|
208
|
+
|
|
209
|
+
/** Resolves {@link COHORT_CONNECT_SETTLE_MS} after the strand node's next peer connect, after `delayMs`, or when `signal` aborts. */
|
|
210
|
+
async function nextCohortChance(peers: PeerConnectEvents | undefined, delayMs: number, signal: AbortSignal): Promise<void> {
|
|
211
|
+
const byConnect = await new Promise<boolean>(resolve => {
|
|
212
|
+
const done = (connected: boolean): void => {
|
|
213
|
+
clearTimeout(timer);
|
|
214
|
+
peers?.removeEventListener('peer:connect', onConnect);
|
|
215
|
+
signal.removeEventListener('abort', onAbort);
|
|
216
|
+
resolve(connected);
|
|
217
|
+
};
|
|
218
|
+
const onConnect = (): void => done(true);
|
|
219
|
+
const onAbort = (): void => done(false);
|
|
220
|
+
const timer = setTimeout(() => done(false), delayMs);
|
|
221
|
+
peers?.addEventListener('peer:connect', onConnect);
|
|
222
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
223
|
+
});
|
|
224
|
+
if (byConnect) {
|
|
225
|
+
await abortableDelay(COHORT_CONNECT_SETTLE_MS, signal);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
138
229
|
/** How long a deferred sApp schema apply waits before trying again while another caller's transaction is open. */
|
|
139
230
|
const OPEN_TRANSACTION_RETRY_MS = 250;
|
|
140
231
|
|
package/src/cached-storage.ts
CHANGED
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
* backend, which is the invariant's other half.
|
|
30
30
|
*
|
|
31
31
|
* `MemoryRawStorage` is returned unwrapped on upstream's own guidance: the backend
|
|
32
|
-
* is already in-memory, so the cache adds bookkeeping with nothing to save.
|
|
32
|
+
* is already in-memory, so the cache adds bookkeeping with nothing to save. So is a
|
|
33
|
+
* storage that reports `readCached` with a cache of its own (React Native's
|
|
34
|
+
* `LevelDBRawStorage`), which upstream refuses to have wrapped again.
|
|
33
35
|
*
|
|
34
36
|
* **{@link wrapStorageWithCache} and {@link disposeStorageCache} are a matched pair
|
|
35
37
|
* with a holder count between them.** Every wrap that hands back a cache this module
|
|
@@ -108,6 +110,15 @@ function addHolder(wrapped: CachedRawStorage): void {
|
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
112
|
|
|
113
|
+
/**
|
|
114
|
+
* A storage that carries its own read cache and is not one of this module's wrappers, such as
|
|
115
|
+
* the React Native `LevelDBRawStorage`. Upstream refuses a second cache over it
|
|
116
|
+
* (`redundant read cache`), and it stays its creator's to close.
|
|
117
|
+
*/
|
|
118
|
+
function isOwnReadCache(storage: IRawStorage): boolean {
|
|
119
|
+
return storage.readCached === true && !(storage instanceof CachedRawStorage);
|
|
120
|
+
}
|
|
121
|
+
|
|
111
122
|
/**
|
|
112
123
|
* The cached view of `storage`, memoized per instance, with one holder claim taken for
|
|
113
124
|
* this caller — release it with {@link disposeStorageCache} when this caller's scope
|
|
@@ -116,7 +127,7 @@ function addHolder(wrapped: CachedRawStorage): void {
|
|
|
116
127
|
* one backend is the failure that matters and a stale pool label is not.
|
|
117
128
|
*/
|
|
118
129
|
export function wrapStorageWithCache(storage: IRawStorage, label: string): IRawStorage {
|
|
119
|
-
if (storage instanceof MemoryRawStorage) {
|
|
130
|
+
if (storage instanceof MemoryRawStorage || isOwnReadCache(storage)) {
|
|
120
131
|
return storage;
|
|
121
132
|
}
|
|
122
133
|
if (storage instanceof CachedRawStorage) {
|
package/src/compose-strand.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
StrandConnectionOptions, SereusPluginResult, StrandTransactor, Libp2pNodeWithRepo,
|
|
10
10
|
StrandOfflineOptions, StrandOfflineController,
|
|
11
11
|
} from './types.js';
|
|
12
|
-
import {
|
|
12
|
+
import { applyAppSchemaOrDefer, type AppSchemaApply, type PeerConnectEvents } from './app-schema.js';
|
|
13
13
|
import { STRAND_SCHEMA } from './strand-schema.js';
|
|
14
14
|
import { resolveStrandClusterSize, STRAND_CLUSTER_POLICY } from './cluster-size.js';
|
|
15
15
|
import { wrapStorageWithCache, disposeStorageCache } from './cached-storage.js';
|
|
@@ -311,6 +311,8 @@ export async function composeStrand(
|
|
|
311
311
|
let appSchemaApply: AppSchemaApply | undefined;
|
|
312
312
|
|
|
313
313
|
let createdNode: Libp2p | null = null;
|
|
314
|
+
// The strand node, when there is one: a deferred sApp schema apply waiting for the cohort wakes on its connects.
|
|
315
|
+
let peers: PeerConnectEvents | undefined;
|
|
314
316
|
let hydrated: { tables: number; indexes: number } | undefined;
|
|
315
317
|
|
|
316
318
|
try {
|
|
@@ -347,6 +349,7 @@ export async function composeStrand(
|
|
|
347
349
|
coordinatedRepo = repo;
|
|
348
350
|
log('Created libp2p node (port: %d, fretProfile: %s, storage=%s)', port, fretProfile, !!storage);
|
|
349
351
|
}
|
|
352
|
+
peers = node;
|
|
350
353
|
|
|
351
354
|
collectionFactory.registerLibp2pNode(networkName, node, coordinatedRepo);
|
|
352
355
|
log('Registered libp2p node with collection factory');
|
|
@@ -417,12 +420,13 @@ export async function composeStrand(
|
|
|
417
420
|
// migrated instead of restored. No schema in this repo evolves that way, so this
|
|
418
421
|
// is a note for whoever first ships a migration that does.
|
|
419
422
|
//
|
|
420
|
-
// The
|
|
421
|
-
// connect. The strand comes up on the `App`
|
|
422
|
-
// queue drains (see
|
|
423
|
+
// The exception: an apply refused by queued offline transactions, or because the strand's
|
|
424
|
+
// cohort could not be reached, does not fail the connect. The strand comes up on the `App`
|
|
425
|
+
// schema it had, and the apply is retried as the queue drains or when a peer connects (see
|
|
426
|
+
// `applyAppSchemaOrDefer`).
|
|
423
427
|
if (schema) {
|
|
424
428
|
log('Applying sApp schema for strand %s', strandId);
|
|
425
|
-
appSchemaApply = await
|
|
429
|
+
appSchemaApply = await applyAppSchemaOrDefer(db, schema, { queue: pluginResult.offline, peers }, released.signal);
|
|
426
430
|
}
|
|
427
431
|
} catch (err) {
|
|
428
432
|
// Clean up resources if setup fails after partial initialization.
|
package/src/index.ts
CHANGED
|
@@ -16,7 +16,7 @@ export {
|
|
|
16
16
|
} from './cluster-size.js';
|
|
17
17
|
export { wrapStorageWithCache, disposeStorageCache } from './cached-storage.js';
|
|
18
18
|
export type {
|
|
19
|
-
StrandConnectionOptions, SereusPluginResult, StrandTransactor, StrandOfflineOptions, StrandOfflineController, AppSchemaOutcome,
|
|
19
|
+
StrandConnectionOptions, SereusPluginResult, StrandTransactor, StrandOfflineOptions, StrandOfflineController, AppSchemaOutcome, AppSchemaWait,
|
|
20
20
|
} from './types.js';
|
|
21
21
|
export type { SqlCommitDurability, CommitFullyHeld } from '@optimystic/quereus-plugin-optimystic/plugin';
|
|
22
22
|
export {
|
package/src/types.ts
CHANGED
|
@@ -100,12 +100,15 @@ export type StrandOfflineController = Pick<OfflineMode,
|
|
|
100
100
|
/**
|
|
101
101
|
* How a connection's sApp schema apply ended (see {@link SereusPluginResult.appSchema}):
|
|
102
102
|
*
|
|
103
|
-
* - `applied` — the schema is in place. `deferred` when
|
|
104
|
-
*
|
|
105
|
-
* - `failed` — a deferred retry failed for
|
|
106
|
-
*
|
|
103
|
+
* - `applied` — the schema is in place. `deferred` when the first apply was refused (by queued
|
|
104
|
+
* offline transactions, or because the strand's cohort could not be reached) and a later one landed.
|
|
105
|
+
* - `failed` — a deferred retry failed for another reason; the connection keeps the schema it had
|
|
106
|
+
* before.
|
|
107
107
|
* - `cancelled` — the connection shut down before a deferred retry landed.
|
|
108
108
|
*/
|
|
109
|
+
/** What a deferred sApp schema apply waits for (see {@link SereusPluginResult.appSchemaWaitingFor}). */
|
|
110
|
+
export type AppSchemaWait = 'queue' | 'cohort';
|
|
111
|
+
|
|
109
112
|
export type AppSchemaOutcome =
|
|
110
113
|
| { state: 'applied'; deferred?: true }
|
|
111
114
|
| { state: 'failed'; error: unknown }
|
|
@@ -163,12 +166,18 @@ export interface SereusPluginResult {
|
|
|
163
166
|
/**
|
|
164
167
|
* Present when {@link StrandConnectionOptions.schema} was given; never rejects. Resolves
|
|
165
168
|
* `{ state: 'applied' }` at once when the apply succeeded at connect. When queued offline
|
|
166
|
-
* transactions refused it, the
|
|
167
|
-
*
|
|
168
|
-
* {@link shutdown} runs first.
|
|
169
|
+
* transactions refused it, or the strand's cohort could not be reached, the connection comes up
|
|
170
|
+
* on the `App` schema it already had and retries ({@link appSchemaWaitingFor}); this settles when
|
|
171
|
+
* a retry lands, fails otherwise, or {@link shutdown} runs first.
|
|
169
172
|
*/
|
|
170
173
|
appSchema?: Promise<AppSchemaOutcome>;
|
|
171
|
-
/**
|
|
174
|
+
/**
|
|
175
|
+
* What the connect-time apply waits for: `'queue'`, queued offline transactions (retried each time
|
|
176
|
+
* the queue drains), or `'cohort'`, the strand's members (retried when the strand node connects to
|
|
177
|
+
* a peer, and on a backoff). Present only when it was deferred.
|
|
178
|
+
*/
|
|
179
|
+
appSchemaWaitingFor?: AppSchemaWait;
|
|
180
|
+
/** The queued transaction ids that refused the connect-time apply. Present only when the queue refused it. */
|
|
172
181
|
appSchemaDeferredBy?: readonly string[];
|
|
173
182
|
/** Shuts down the libp2p node and collection factory. Call when done. */
|
|
174
183
|
shutdown: () => Promise<void>;
|