@peerbit/shared-log 13.2.32 → 13.2.33
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/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +574 -92
- package/dist/src/index.js.map +1 -1
- package/dist/src/replication-info-v2-binding.d.ts +16 -0
- package/dist/src/replication-info-v2-binding.d.ts.map +1 -0
- package/dist/src/replication-info-v2-binding.js +30 -0
- package/dist/src/replication-info-v2-binding.js.map +1 -0
- package/dist/src/replication-info-v2-receive.d.ts +181 -0
- package/dist/src/replication-info-v2-receive.d.ts.map +1 -0
- package/dist/src/replication-info-v2-receive.js +725 -0
- package/dist/src/replication-info-v2-receive.js.map +1 -0
- package/dist/src/replication-info-v2-send.d.ts +5 -17
- package/dist/src/replication-info-v2-send.d.ts.map +1 -1
- package/dist/src/replication-info-v2-send.js +15 -32
- package/dist/src/replication-info-v2-send.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +838 -208
- package/src/replication-info-v2-binding.ts +44 -0
- package/src/replication-info-v2-receive.ts +1045 -0
- package/src/replication-info-v2-send.ts +17 -45
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
import { serialize } from "@dao-xyz/borsh";
|
|
2
|
+
import { randomBytes, sha256Sync } from "@peerbit/crypto";
|
|
3
|
+
import { SYNC_CAPABILITY_REPLICATION_INFO_V2_DECODE, SYNC_CAPABILITY_REPLICATION_INFO_V2_SEND, } from "./exchange-heads.js";
|
|
4
|
+
import { deriveReplicationInfoV2ReceiverBinding } from "./replication-info-v2-binding.js";
|
|
5
|
+
import { AddedReplicationInfoV2Message, AddedReplicationSegmentMessage, AllReplicatingSegmentsMessage, FullReplicationInfoV2Message, RequestReplicationInfoV2Message, StoppedReplicating, StoppedReplicationInfoV2Message, } from "./replication.js";
|
|
6
|
+
const REQUIRED_SENDER_CAPABILITIES = SYNC_CAPABILITY_REPLICATION_INFO_V2_DECODE |
|
|
7
|
+
SYNC_CAPABILITY_REPLICATION_INFO_V2_SEND;
|
|
8
|
+
const DEFAULT_REQUEST_RETRY_MS = 1_000;
|
|
9
|
+
const DEFAULT_MAX_REQUEST_RETRY_MS = 30_000;
|
|
10
|
+
const DEFAULT_REQUEST_MAX_ATTEMPTS = 7;
|
|
11
|
+
const DEFAULT_LEGACY_FALLBACK_DELAY_MS = 5_000;
|
|
12
|
+
const MAX_U64 = (1n << 64n) - 1n;
|
|
13
|
+
const bytesEqual = (left, right) => {
|
|
14
|
+
if (left.byteLength !== right.byteLength) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
for (let index = 0; index < left.byteLength; index++) {
|
|
18
|
+
if (left[index] !== right[index]) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
const replicationInfoPayloadFingerprint = (message) => {
|
|
25
|
+
const canonical = message instanceof FullReplicationInfoV2Message
|
|
26
|
+
? new AllReplicatingSegmentsMessage({ segments: message.segments })
|
|
27
|
+
: message instanceof AddedReplicationInfoV2Message
|
|
28
|
+
? new AddedReplicationSegmentMessage({ segments: message.segments })
|
|
29
|
+
: message instanceof StoppedReplicationInfoV2Message
|
|
30
|
+
? new StoppedReplicating({ segmentIds: message.segmentIds })
|
|
31
|
+
: message;
|
|
32
|
+
return sha256Sync(serialize(canonical));
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Authenticated receive grants and sender-authoritative ordering for
|
|
36
|
+
* replication-info V2. State is bounded to one entry per subscribed peer and
|
|
37
|
+
* is always scoped to the exact PeerSession object.
|
|
38
|
+
*/
|
|
39
|
+
export class ReplicationInfoV2ReceiveCoordinator {
|
|
40
|
+
deps;
|
|
41
|
+
_receiveStates;
|
|
42
|
+
_cutoverPeerSessions;
|
|
43
|
+
_localCapabilityReadyBySession;
|
|
44
|
+
_reservedAdmissionsByPeer;
|
|
45
|
+
now;
|
|
46
|
+
requestRetryMs;
|
|
47
|
+
maxRequestRetryMs;
|
|
48
|
+
requestMaxAttempts;
|
|
49
|
+
legacyFallbackDelayMs;
|
|
50
|
+
constructor(deps) {
|
|
51
|
+
this.deps = deps;
|
|
52
|
+
this.now = deps.now ?? Date.now;
|
|
53
|
+
this.requestRetryMs = Math.max(1, deps.requestRetryMs ?? DEFAULT_REQUEST_RETRY_MS);
|
|
54
|
+
this.maxRequestRetryMs = Math.max(this.requestRetryMs, deps.maxRequestRetryMs ?? DEFAULT_MAX_REQUEST_RETRY_MS);
|
|
55
|
+
this.requestMaxAttempts = Math.max(1, Math.floor(deps.requestMaxAttempts ?? DEFAULT_REQUEST_MAX_ATTEMPTS));
|
|
56
|
+
this.legacyFallbackDelayMs = Math.max(this.requestRetryMs, deps.legacyFallbackDelayMs ?? DEFAULT_LEGACY_FALLBACK_DELAY_MS);
|
|
57
|
+
this._receiveStates = new Map();
|
|
58
|
+
this._cutoverPeerSessions = new WeakSet();
|
|
59
|
+
this._localCapabilityReadyBySession = new WeakMap();
|
|
60
|
+
this._reservedAdmissionsByPeer = new Map();
|
|
61
|
+
}
|
|
62
|
+
resetForOpen() {
|
|
63
|
+
this.clearForClose();
|
|
64
|
+
this._receiveStates = new Map();
|
|
65
|
+
this._cutoverPeerSessions = new WeakSet();
|
|
66
|
+
this._localCapabilityReadyBySession = new WeakMap();
|
|
67
|
+
this._reservedAdmissionsByPeer = new Map();
|
|
68
|
+
}
|
|
69
|
+
clearForClose() {
|
|
70
|
+
for (const state of this._receiveStates?.values() ?? []) {
|
|
71
|
+
this.clearState(state);
|
|
72
|
+
}
|
|
73
|
+
this._receiveStates?.clear();
|
|
74
|
+
this._cutoverPeerSessions = new WeakSet();
|
|
75
|
+
this._localCapabilityReadyBySession = new WeakMap();
|
|
76
|
+
}
|
|
77
|
+
clearPeer(peerHash, expectedSession) {
|
|
78
|
+
const state = this._receiveStates.get(peerHash);
|
|
79
|
+
if (state && (!expectedSession || state.peerSession === expectedSession)) {
|
|
80
|
+
this.clearState(state);
|
|
81
|
+
this._localCapabilityReadyBySession.delete(state.peerSession);
|
|
82
|
+
this._cutoverPeerSessions.delete(state.peerSession);
|
|
83
|
+
}
|
|
84
|
+
if (expectedSession) {
|
|
85
|
+
this._localCapabilityReadyBySession.delete(expectedSession);
|
|
86
|
+
this._cutoverPeerSessions.delete(expectedSession);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Revoke an unauthenticated or downgraded capability generation. */
|
|
90
|
+
revokePeerCapability(peerHash, reopenLegacy = true) {
|
|
91
|
+
const state = this._receiveStates.get(peerHash);
|
|
92
|
+
if (!state) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.clearState(state);
|
|
96
|
+
if (reopenLegacy) {
|
|
97
|
+
this._cutoverPeerSessions.delete(state.peerSession);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
clearState(state) {
|
|
101
|
+
if (state.requestTimer) {
|
|
102
|
+
clearTimeout(state.requestTimer);
|
|
103
|
+
state.requestTimer = undefined;
|
|
104
|
+
}
|
|
105
|
+
if (state.legacyFallbackTimer) {
|
|
106
|
+
clearTimeout(state.legacyFallbackTimer);
|
|
107
|
+
state.legacyFallbackTimer = undefined;
|
|
108
|
+
}
|
|
109
|
+
state.controller.abort();
|
|
110
|
+
state.version++;
|
|
111
|
+
if (this._receiveStates.get(state.peerHash) === state) {
|
|
112
|
+
this._receiveStates.delete(state.peerHash);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** Record success of this session's ACKed local APPLY advertisement. */
|
|
116
|
+
markLocalCapabilityReady(properties) {
|
|
117
|
+
const { peerHash, peerSession, receiverTransportSession } = properties;
|
|
118
|
+
const state = this._receiveStates.get(peerHash);
|
|
119
|
+
if (this.deps.isClosed() ||
|
|
120
|
+
!this.deps.isPeerStateCurrent(peerHash, peerSession, properties.receiveEpoch) ||
|
|
121
|
+
this.deps.getReceiverTransportSession() !== receiverTransportSession) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const ready = {
|
|
125
|
+
peerHash,
|
|
126
|
+
receiverTransportSession,
|
|
127
|
+
requestNotBeforeMs: properties.requestNotBeforeMs,
|
|
128
|
+
};
|
|
129
|
+
this._localCapabilityReadyBySession.set(peerSession, ready);
|
|
130
|
+
if (state?.peerSession === peerSession &&
|
|
131
|
+
state.receiveEpoch === properties.receiveEpoch) {
|
|
132
|
+
if (state.receiverTransportSession !== undefined &&
|
|
133
|
+
state.receiverTransportSession !== receiverTransportSession) {
|
|
134
|
+
this.clearState(state);
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
this.bindLocalCapability(state, ready);
|
|
138
|
+
state.requestAttempts = 0;
|
|
139
|
+
state.requestsSinceCapabilityRefresh = 0;
|
|
140
|
+
state.requestParked = false;
|
|
141
|
+
this.armRequest(state, 0);
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Promote one signed capability generation after the opening barrier has
|
|
147
|
+
* committed. Repeated same-session advertisements refresh freshness only;
|
|
148
|
+
* they never reset sequence state.
|
|
149
|
+
*/
|
|
150
|
+
observeCapability(properties) {
|
|
151
|
+
const { peerHash, target, peerSession, receiveEpoch, capabilities, senderTransportSession, capabilityTimestamp, } = properties;
|
|
152
|
+
if (target.equals(this.deps.getSelfKey()) ||
|
|
153
|
+
this.deps.isClosed() ||
|
|
154
|
+
!this.deps.isPeerStateCurrent(peerHash, peerSession, receiveEpoch)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const senderReady = (capabilities & REQUIRED_SENDER_CAPABILITIES) ===
|
|
158
|
+
REQUIRED_SENDER_CAPABILITIES;
|
|
159
|
+
let state = this._receiveStates.get(peerHash);
|
|
160
|
+
if (state &&
|
|
161
|
+
(state.peerSession !== peerSession ||
|
|
162
|
+
state.senderTransportSession !== senderTransportSession ||
|
|
163
|
+
!state.target.equals(target))) {
|
|
164
|
+
const preserveCutover = state.peerSession === peerSession && senderReady;
|
|
165
|
+
this.clearState(state);
|
|
166
|
+
if (!preserveCutover) {
|
|
167
|
+
this._cutoverPeerSessions.delete(state.peerSession);
|
|
168
|
+
}
|
|
169
|
+
state = undefined;
|
|
170
|
+
}
|
|
171
|
+
if (!senderReady) {
|
|
172
|
+
if (state) {
|
|
173
|
+
this.clearState(state);
|
|
174
|
+
this._cutoverPeerSessions.delete(peerSession);
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (state) {
|
|
179
|
+
if (capabilityTimestamp < state.capabilityTimestamp) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
const previousCapabilities = state.capabilities;
|
|
183
|
+
const previousTimestamp = state.capabilityTimestamp;
|
|
184
|
+
const receiveEpochChanged = state.receiveEpoch !== receiveEpoch;
|
|
185
|
+
const addsCapabilities = (capabilities & ~previousCapabilities) !== 0;
|
|
186
|
+
if (capabilityTimestamp === previousTimestamp &&
|
|
187
|
+
!addsCapabilities &&
|
|
188
|
+
!receiveEpochChanged) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
state.capabilities |= capabilities;
|
|
192
|
+
state.capabilityTimestamp = capabilityTimestamp;
|
|
193
|
+
if (receiveEpochChanged) {
|
|
194
|
+
state.receiveEpoch = receiveEpoch;
|
|
195
|
+
this.transitionToResync(state, {
|
|
196
|
+
force: true,
|
|
197
|
+
refreshCapability: true,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const ready = this._localCapabilityReadyBySession.get(peerSession);
|
|
201
|
+
if (ready?.peerHash === peerHash && state.receiverBinding === undefined) {
|
|
202
|
+
this.bindLocalCapability(state, ready);
|
|
203
|
+
}
|
|
204
|
+
if (state.phase !== "active") {
|
|
205
|
+
state.requestAttempts = 0;
|
|
206
|
+
state.requestParked = false;
|
|
207
|
+
this.armRequest(state, 0);
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
const retainedCutover = this._cutoverPeerSessions.has(peerSession);
|
|
212
|
+
state = {
|
|
213
|
+
peerHash,
|
|
214
|
+
target,
|
|
215
|
+
peerSession,
|
|
216
|
+
receiveEpoch,
|
|
217
|
+
capabilities,
|
|
218
|
+
capabilityTimestamp,
|
|
219
|
+
senderTransportSession,
|
|
220
|
+
receiverRequestChallenge: randomBytes(32),
|
|
221
|
+
phase: retainedCutover ? "resync" : "awaiting-full",
|
|
222
|
+
version: 0,
|
|
223
|
+
controller: new AbortController(),
|
|
224
|
+
requestAttempts: 0,
|
|
225
|
+
requestsSinceCapabilityRefresh: 0,
|
|
226
|
+
requestParked: false,
|
|
227
|
+
capabilityRefreshRequired: retainedCutover,
|
|
228
|
+
legacyFallbackAmbiguous: false,
|
|
229
|
+
recentCommittedPayloads: [],
|
|
230
|
+
lastLegacyObservationAmbiguous: false,
|
|
231
|
+
};
|
|
232
|
+
this._receiveStates.set(peerHash, state);
|
|
233
|
+
const ready = this._localCapabilityReadyBySession.get(peerSession);
|
|
234
|
+
if (ready?.peerHash === peerHash) {
|
|
235
|
+
this.bindLocalCapability(state, ready);
|
|
236
|
+
this.armRequest(state, 0);
|
|
237
|
+
}
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
bindLocalCapability(state, ready) {
|
|
241
|
+
state.receiverTransportSession = ready.receiverTransportSession;
|
|
242
|
+
state.receiverBinding = deriveReplicationInfoV2ReceiverBinding({
|
|
243
|
+
receiverChallenge: state.receiverRequestChallenge,
|
|
244
|
+
receiver: this.deps.getSelfKey(),
|
|
245
|
+
receiverTransportSession: ready.receiverTransportSession,
|
|
246
|
+
sender: state.target,
|
|
247
|
+
senderTransportSession: state.senderTransportSession,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/** Require a fresh capability-bound grant and authoritative Full. */
|
|
251
|
+
advanceRecovery(properties) {
|
|
252
|
+
const state = this._receiveStates.get(properties.peerHash);
|
|
253
|
+
if (!state || state.peerSession !== properties.peerSession) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
state.receiveEpoch = properties.receiveEpoch;
|
|
257
|
+
this.transitionToResync(state, {
|
|
258
|
+
force: true,
|
|
259
|
+
refreshCapability: true,
|
|
260
|
+
});
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
transitionToResync(state, options) {
|
|
264
|
+
const shouldRestart = state.phase !== "resync" ||
|
|
265
|
+
options?.force === true ||
|
|
266
|
+
state.requestParked;
|
|
267
|
+
if (state.phase !== "resync" || options?.force === true) {
|
|
268
|
+
state.phase = "resync";
|
|
269
|
+
state.version++;
|
|
270
|
+
}
|
|
271
|
+
if (options?.refreshCapability) {
|
|
272
|
+
state.capabilityRefreshRequired = true;
|
|
273
|
+
}
|
|
274
|
+
if (shouldRestart) {
|
|
275
|
+
state.requestAttempts = 0;
|
|
276
|
+
state.requestParked = false;
|
|
277
|
+
this.armRequest(state, 0);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
isLegacyCutover(peerSession) {
|
|
281
|
+
return peerSession !== null && this._cutoverPeerSessions.has(peerSession);
|
|
282
|
+
}
|
|
283
|
+
prepare(message, properties) {
|
|
284
|
+
const peerHash = properties.from.hashcode();
|
|
285
|
+
const state = this._receiveStates.get(peerHash);
|
|
286
|
+
if (!state ||
|
|
287
|
+
state.peerSession !== properties.peerSession ||
|
|
288
|
+
state.receiveEpoch !== properties.receiveEpoch ||
|
|
289
|
+
state.senderTransportSession !== properties.senderTransportSession ||
|
|
290
|
+
!state.target.equals(properties.from) ||
|
|
291
|
+
!state.receiverBinding ||
|
|
292
|
+
!bytesEqual(message.receiverChallenge, state.receiverBinding) ||
|
|
293
|
+
message.sequence <= 0n ||
|
|
294
|
+
!this.isStateCurrent(state)) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
const kind = message instanceof FullReplicationInfoV2Message
|
|
298
|
+
? "full"
|
|
299
|
+
: message instanceof AddedReplicationInfoV2Message
|
|
300
|
+
? "added"
|
|
301
|
+
: message instanceof StoppedReplicationInfoV2Message
|
|
302
|
+
? "stopped"
|
|
303
|
+
: undefined;
|
|
304
|
+
if (!kind) {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
if (state.senderEpoch === undefined) {
|
|
308
|
+
if (kind !== "full") {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
else if (!bytesEqual(message.senderEpoch, state.senderEpoch)) {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
const lastSequence = state.lastSequence;
|
|
316
|
+
if (kind === "full") {
|
|
317
|
+
if (lastSequence !== undefined && message.sequence <= lastSequence) {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
if (state.phase !== "active" ||
|
|
323
|
+
lastSequence === undefined ||
|
|
324
|
+
message.sequence !== lastSequence + 1n) {
|
|
325
|
+
if (state.phase === "active" &&
|
|
326
|
+
lastSequence !== undefined &&
|
|
327
|
+
message.sequence > lastSequence + 1n) {
|
|
328
|
+
this.transitionToResync(state);
|
|
329
|
+
}
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
state,
|
|
335
|
+
version: state.version,
|
|
336
|
+
receiveEpoch: state.receiveEpoch,
|
|
337
|
+
message,
|
|
338
|
+
kind,
|
|
339
|
+
payloadFingerprint: replicationInfoPayloadFingerprint(message),
|
|
340
|
+
transportTimestamp: properties.transportTimestamp,
|
|
341
|
+
committed: false,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/** Reserve at most one decoded V2 frame per peer ahead of the apply lane. */
|
|
345
|
+
reserve(message, properties) {
|
|
346
|
+
const peerHash = properties.from.hashcode();
|
|
347
|
+
const reserved = this._reservedAdmissionsByPeer.get(peerHash);
|
|
348
|
+
if (reserved) {
|
|
349
|
+
const state = reserved.state;
|
|
350
|
+
const currentState = this._receiveStates.get(peerHash);
|
|
351
|
+
const knownMessage = message instanceof FullReplicationInfoV2Message ||
|
|
352
|
+
message instanceof AddedReplicationInfoV2Message ||
|
|
353
|
+
message instanceof StoppedReplicationInfoV2Message;
|
|
354
|
+
if (knownMessage &&
|
|
355
|
+
this._receiveStates.get(peerHash) === state &&
|
|
356
|
+
state.peerSession === properties.peerSession &&
|
|
357
|
+
state.receiveEpoch === properties.receiveEpoch &&
|
|
358
|
+
state.senderTransportSession === properties.senderTransportSession &&
|
|
359
|
+
state.target.equals(properties.from) &&
|
|
360
|
+
state.receiverBinding !== undefined &&
|
|
361
|
+
bytesEqual(message.receiverChallenge, state.receiverBinding) &&
|
|
362
|
+
bytesEqual(message.senderEpoch, reserved.message.senderEpoch) &&
|
|
363
|
+
message.sequence > reserved.message.sequence &&
|
|
364
|
+
this.isStateCurrent(state)) {
|
|
365
|
+
// Transport ACKs precede application. Do not invalidate the frame
|
|
366
|
+
// already applying, and do not retain an unbounded successor queue.
|
|
367
|
+
// Commit the reservation, then request one authoritative Full.
|
|
368
|
+
reserved.resyncAfterRelease = true;
|
|
369
|
+
}
|
|
370
|
+
else if (knownMessage &&
|
|
371
|
+
currentState !== undefined &&
|
|
372
|
+
currentState !== state &&
|
|
373
|
+
this.prepare(message, properties)?.state === currentState) {
|
|
374
|
+
// A previous generation can still be parked in the host apply lane.
|
|
375
|
+
// Transport already ACKed this current-generation frame, so wake the
|
|
376
|
+
// current state once the peer-global reservation is finally released.
|
|
377
|
+
reserved.resyncAfterRelease = true;
|
|
378
|
+
}
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const admission = this.prepare(message, properties);
|
|
382
|
+
if (!admission) {
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
const { state } = admission;
|
|
386
|
+
this._reservedAdmissionsByPeer.set(peerHash, admission);
|
|
387
|
+
state.reservedAdmission = admission;
|
|
388
|
+
return admission;
|
|
389
|
+
}
|
|
390
|
+
release(admission) {
|
|
391
|
+
const { state } = admission;
|
|
392
|
+
if (this._reservedAdmissionsByPeer.get(state.peerHash) !== admission) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
this._reservedAdmissionsByPeer.delete(state.peerHash);
|
|
396
|
+
if (state.reservedAdmission === admission) {
|
|
397
|
+
state.reservedAdmission = undefined;
|
|
398
|
+
}
|
|
399
|
+
const currentState = this._receiveStates.get(state.peerHash);
|
|
400
|
+
if (currentState &&
|
|
401
|
+
this.isStateCurrent(currentState) &&
|
|
402
|
+
(admission.resyncAfterRelease ||
|
|
403
|
+
(currentState !== state && currentState.phase !== "active"))) {
|
|
404
|
+
this.transitionToResync(currentState, { force: true });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* B9 can lose its sender grant while keeping the topic session open. Its
|
|
409
|
+
* unmatched legacy sidecar is the compatibility signal to re-handshake; a
|
|
410
|
+
* matching V2 payload before or after the legacy copy suppresses the timer.
|
|
411
|
+
*/
|
|
412
|
+
noteLegacyAnnouncement(properties) {
|
|
413
|
+
const state = this._receiveStates.get(properties.peerHash);
|
|
414
|
+
if (!state ||
|
|
415
|
+
state.peerSession !== properties.peerSession ||
|
|
416
|
+
state.receiveEpoch !== properties.receiveEpoch ||
|
|
417
|
+
state.senderTransportSession !== properties.senderTransportSession ||
|
|
418
|
+
!this._cutoverPeerSessions.has(properties.peerSession) ||
|
|
419
|
+
!this.isStateCurrent(state)) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
const fingerprint = replicationInfoPayloadFingerprint(properties.message);
|
|
423
|
+
if (state.lastCommittedTransportTimestamp !== undefined &&
|
|
424
|
+
properties.transportTimestamp < state.lastCommittedTransportTimestamp) {
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
if (state.recentCommittedPayloads.some((committed) => properties.transportTimestamp <= committed.transportTimestamp &&
|
|
428
|
+
bytesEqual(committed.fingerprint, fingerprint))) {
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
if (state.lastLegacyObservationTimestamp !== undefined) {
|
|
432
|
+
if (properties.transportTimestamp < state.lastLegacyObservationTimestamp) {
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
if (properties.transportTimestamp === state.lastLegacyObservationTimestamp) {
|
|
436
|
+
if (state.lastLegacyObservationAmbiguous ||
|
|
437
|
+
(state.lastLegacyObservationFingerprint !== undefined &&
|
|
438
|
+
bytesEqual(state.lastLegacyObservationFingerprint, fingerprint))) {
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
state.lastLegacyObservationAmbiguous = true;
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
state.lastLegacyObservationTimestamp = properties.transportTimestamp;
|
|
445
|
+
state.lastLegacyObservationFingerprint = fingerprint.slice();
|
|
446
|
+
state.lastLegacyObservationAmbiguous = false;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
state.lastLegacyObservationTimestamp = properties.transportTimestamp;
|
|
451
|
+
state.lastLegacyObservationFingerprint = fingerprint.slice();
|
|
452
|
+
state.lastLegacyObservationAmbiguous = false;
|
|
453
|
+
}
|
|
454
|
+
const reserved = this._reservedAdmissionsByPeer.get(properties.peerHash);
|
|
455
|
+
if (reserved?.state === state &&
|
|
456
|
+
!bytesEqual(reserved.payloadFingerprint, fingerprint)) {
|
|
457
|
+
// A legacy sidecar observed while a different V2 payload is applying
|
|
458
|
+
// cannot be erased by that commit. The transport has already ACKed the
|
|
459
|
+
// sidecar, so require one authoritative successor after release.
|
|
460
|
+
reserved.resyncAfterRelease = true;
|
|
461
|
+
}
|
|
462
|
+
if (!state.legacyFallbackFingerprint) {
|
|
463
|
+
state.legacyFallbackFingerprint = fingerprint;
|
|
464
|
+
state.legacyFallbackTimestamp = properties.transportTimestamp;
|
|
465
|
+
state.legacyFallbackAmbiguous = false;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
if (!bytesEqual(state.legacyFallbackFingerprint, fingerprint)) {
|
|
469
|
+
state.legacyFallbackAmbiguous = true;
|
|
470
|
+
}
|
|
471
|
+
if (state.legacyFallbackTimestamp === undefined ||
|
|
472
|
+
properties.transportTimestamp > state.legacyFallbackTimestamp) {
|
|
473
|
+
state.legacyFallbackTimestamp = properties.transportTimestamp;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (state.phase !== "active") {
|
|
477
|
+
if (state.requestParked) {
|
|
478
|
+
this.transitionToResync(state, {
|
|
479
|
+
force: true,
|
|
480
|
+
refreshCapability: true,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
if (state.legacyFallbackTimer) {
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
state.legacyFallbackTimer = setTimeout(() => {
|
|
489
|
+
state.legacyFallbackTimer = undefined;
|
|
490
|
+
if (this.isStateCurrent(state) && state.phase === "active") {
|
|
491
|
+
this.transitionToResync(state, {
|
|
492
|
+
force: true,
|
|
493
|
+
refreshCapability: true,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}, this.legacyFallbackDelayMs);
|
|
497
|
+
state.legacyFallbackTimer.unref?.();
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
clearLegacyFallback(state) {
|
|
501
|
+
if (state.legacyFallbackTimer) {
|
|
502
|
+
clearTimeout(state.legacyFallbackTimer);
|
|
503
|
+
state.legacyFallbackTimer = undefined;
|
|
504
|
+
}
|
|
505
|
+
state.legacyFallbackFingerprint = undefined;
|
|
506
|
+
state.legacyFallbackTimestamp = undefined;
|
|
507
|
+
state.legacyFallbackAmbiguous = false;
|
|
508
|
+
}
|
|
509
|
+
isAdmissionCurrent(admission) {
|
|
510
|
+
const { state } = admission;
|
|
511
|
+
return (state.version === admission.version &&
|
|
512
|
+
state.receiveEpoch === admission.receiveEpoch &&
|
|
513
|
+
this.isStateCurrent(state));
|
|
514
|
+
}
|
|
515
|
+
commit(admission) {
|
|
516
|
+
if (admission.committed) {
|
|
517
|
+
return this.isAdmissionCurrent(admission);
|
|
518
|
+
}
|
|
519
|
+
if (!this.isAdmissionCurrent(admission)) {
|
|
520
|
+
this.release(admission);
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
const { state, message } = admission;
|
|
524
|
+
if (admission.kind === "full") {
|
|
525
|
+
if (state.senderEpoch === undefined) {
|
|
526
|
+
state.senderEpoch = message.senderEpoch.slice();
|
|
527
|
+
this._cutoverPeerSessions.add(state.peerSession);
|
|
528
|
+
}
|
|
529
|
+
else if (!bytesEqual(state.senderEpoch, message.senderEpoch)) {
|
|
530
|
+
this.release(admission);
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
else if (state.senderEpoch === undefined ||
|
|
535
|
+
!bytesEqual(state.senderEpoch, message.senderEpoch)) {
|
|
536
|
+
this.release(admission);
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
state.lastSequence = message.sequence;
|
|
540
|
+
state.phase = "active";
|
|
541
|
+
state.requestAttempts = 0;
|
|
542
|
+
state.requestsSinceCapabilityRefresh = 0;
|
|
543
|
+
state.requestParked = false;
|
|
544
|
+
state.capabilityRefreshRequired = false;
|
|
545
|
+
state.lastCommittedTransportTimestamp = admission.transportTimestamp;
|
|
546
|
+
state.recentCommittedPayloads.push({
|
|
547
|
+
fingerprint: admission.payloadFingerprint.slice(),
|
|
548
|
+
transportTimestamp: admission.transportTimestamp,
|
|
549
|
+
});
|
|
550
|
+
if (state.recentCommittedPayloads.length > 8) {
|
|
551
|
+
state.recentCommittedPayloads.shift();
|
|
552
|
+
}
|
|
553
|
+
if ((state.legacyFallbackTimestamp !== undefined &&
|
|
554
|
+
admission.transportTimestamp > state.legacyFallbackTimestamp) ||
|
|
555
|
+
(!state.legacyFallbackAmbiguous &&
|
|
556
|
+
state.legacyFallbackFingerprint !== undefined &&
|
|
557
|
+
bytesEqual(state.legacyFallbackFingerprint, admission.payloadFingerprint))) {
|
|
558
|
+
this.clearLegacyFallback(state);
|
|
559
|
+
}
|
|
560
|
+
if (state.requestTimer) {
|
|
561
|
+
clearTimeout(state.requestTimer);
|
|
562
|
+
state.requestTimer = undefined;
|
|
563
|
+
}
|
|
564
|
+
state.version++;
|
|
565
|
+
admission.version = state.version;
|
|
566
|
+
admission.receiveEpoch = state.receiveEpoch;
|
|
567
|
+
admission.committed = true;
|
|
568
|
+
this.release(admission);
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
requireFullAfterFailure(admission) {
|
|
572
|
+
if (!this.isAdmissionCurrent(admission)) {
|
|
573
|
+
this.release(admission);
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
this.release(admission);
|
|
577
|
+
this.transitionToResync(admission.state, { force: true });
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
isStateCurrent(state) {
|
|
581
|
+
return (this._receiveStates.get(state.peerHash) === state &&
|
|
582
|
+
!state.controller.signal.aborted &&
|
|
583
|
+
!this.deps.isClosed() &&
|
|
584
|
+
state.receiverTransportSession !== undefined &&
|
|
585
|
+
this.deps.getReceiverTransportSession() ===
|
|
586
|
+
state.receiverTransportSession &&
|
|
587
|
+
this.deps.isSenderTransportSessionCurrent(state.peerHash, state.senderTransportSession) &&
|
|
588
|
+
this.deps.isPeerStateCurrent(state.peerHash, state.peerSession, state.receiveEpoch));
|
|
589
|
+
}
|
|
590
|
+
armRequest(state, delayMs) {
|
|
591
|
+
if (state.requestTimer) {
|
|
592
|
+
clearTimeout(state.requestTimer);
|
|
593
|
+
}
|
|
594
|
+
if (this._receiveStates.get(state.peerHash) !== state ||
|
|
595
|
+
state.controller.signal.aborted ||
|
|
596
|
+
this.deps.isClosed() ||
|
|
597
|
+
(this._reservedAdmissionsByPeer.has(state.peerHash) &&
|
|
598
|
+
this._reservedAdmissionsByPeer.get(state.peerHash)?.state !== state) ||
|
|
599
|
+
state.receiverBinding === undefined ||
|
|
600
|
+
state.phase === "active" ||
|
|
601
|
+
state.requestParked ||
|
|
602
|
+
state.lastSequence === MAX_U64) {
|
|
603
|
+
state.requestTimer = undefined;
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
state.requestTimer = setTimeout(() => {
|
|
607
|
+
state.requestTimer = undefined;
|
|
608
|
+
void this.runRequest(state);
|
|
609
|
+
}, Math.max(0, delayMs));
|
|
610
|
+
state.requestTimer.unref?.();
|
|
611
|
+
}
|
|
612
|
+
requestRetryDelay(state) {
|
|
613
|
+
const exponent = Math.max(0, state.requestAttempts - 1);
|
|
614
|
+
return Math.min(this.maxRequestRetryMs, this.requestRetryMs * 2 ** Math.min(exponent, 20));
|
|
615
|
+
}
|
|
616
|
+
async refreshGrant(state) {
|
|
617
|
+
const refreshed = await this.deps.refreshLocalCapability({
|
|
618
|
+
peerHash: state.peerHash,
|
|
619
|
+
target: state.target,
|
|
620
|
+
peerSession: state.peerSession,
|
|
621
|
+
receiveEpoch: state.receiveEpoch,
|
|
622
|
+
signal: state.controller.signal,
|
|
623
|
+
});
|
|
624
|
+
if (!refreshed ||
|
|
625
|
+
!this.isStateCurrent(state) ||
|
|
626
|
+
this.deps.getReceiverTransportSession() !==
|
|
627
|
+
refreshed.receiverTransportSession) {
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
const ready = {
|
|
631
|
+
peerHash: state.peerHash,
|
|
632
|
+
receiverTransportSession: refreshed.receiverTransportSession,
|
|
633
|
+
requestNotBeforeMs: refreshed.requestNotBeforeMs,
|
|
634
|
+
};
|
|
635
|
+
this._localCapabilityReadyBySession.set(state.peerSession, ready);
|
|
636
|
+
state.receiverRequestChallenge = randomBytes(32);
|
|
637
|
+
state.senderEpoch = undefined;
|
|
638
|
+
state.lastSequence = undefined;
|
|
639
|
+
state.phase = "resync";
|
|
640
|
+
state.capabilityRefreshRequired = false;
|
|
641
|
+
state.requestsSinceCapabilityRefresh = 0;
|
|
642
|
+
state.version++;
|
|
643
|
+
this.bindLocalCapability(state, ready);
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
async runRequest(state) {
|
|
647
|
+
if (state.requestInFlight) {
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
if (!this.isStateCurrent(state)) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (state.phase === "active" ||
|
|
654
|
+
state.requestParked ||
|
|
655
|
+
state.lastSequence === MAX_U64) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (state.requestAttempts >= this.requestMaxAttempts) {
|
|
659
|
+
state.requestParked = true;
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
let operation;
|
|
663
|
+
operation = (async () => {
|
|
664
|
+
if (state.capabilityRefreshRequired) {
|
|
665
|
+
state.requestAttempts++;
|
|
666
|
+
if (!(await this.refreshGrant(state))) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
if (!this.isStateCurrent(state)) {
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
const ready = this._localCapabilityReadyBySession.get(state.peerSession);
|
|
674
|
+
if (!ready ||
|
|
675
|
+
ready.peerHash !== state.peerHash ||
|
|
676
|
+
ready.receiverTransportSession !== state.receiverTransportSession) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
const now = this.now();
|
|
680
|
+
if (now <= ready.requestNotBeforeMs) {
|
|
681
|
+
this.armRequest(state, ready.requestNotBeforeMs - now + 1);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
if (state.requestAttempts >= this.requestMaxAttempts) {
|
|
685
|
+
state.requestParked = true;
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
state.requestAttempts++;
|
|
689
|
+
state.requestsSinceCapabilityRefresh++;
|
|
690
|
+
const request = new RequestReplicationInfoV2Message({
|
|
691
|
+
receiverChallenge: state.receiverRequestChallenge.slice(),
|
|
692
|
+
intendedSender: state.target,
|
|
693
|
+
senderSession: state.senderTransportSession,
|
|
694
|
+
});
|
|
695
|
+
await this.deps.sendRequest(request, state.target, state.controller.signal);
|
|
696
|
+
})()
|
|
697
|
+
.catch((error) => {
|
|
698
|
+
if (!state.controller.signal.aborted && !this.deps.isClosed()) {
|
|
699
|
+
this.deps.onRequestError?.(error);
|
|
700
|
+
}
|
|
701
|
+
})
|
|
702
|
+
.finally(() => {
|
|
703
|
+
if (state.requestInFlight === operation) {
|
|
704
|
+
state.requestInFlight = undefined;
|
|
705
|
+
}
|
|
706
|
+
if (this._receiveStates.get(state.peerHash) === state &&
|
|
707
|
+
!state.controller.signal.aborted &&
|
|
708
|
+
!state.requestTimer &&
|
|
709
|
+
state.phase !== "active") {
|
|
710
|
+
if (state.requestsSinceCapabilityRefresh >= 3) {
|
|
711
|
+
state.capabilityRefreshRequired = true;
|
|
712
|
+
}
|
|
713
|
+
if (state.requestAttempts >= this.requestMaxAttempts) {
|
|
714
|
+
state.requestParked = true;
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
this.armRequest(state, this.requestRetryDelay(state));
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
state.requestInFlight = operation;
|
|
722
|
+
await operation;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
//# sourceMappingURL=replication-info-v2-receive.js.map
|