@kumiai/rpc 0.10.4 → 0.10.6

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.
@@ -34,12 +34,24 @@ export type LogDeliveryParams = {
34
34
  remove: (seq: number) => Promise<void>;
35
35
  clear: () => Promise<void>;
36
36
  cleared: (notice: AppOutboxCleared) => void;
37
+ /** How long a delivered entry stays eligible to be sent again to a rejoined member. */
38
+ deliveredWindowMs: number;
39
+ /** How many delivered entries are kept, oldest dropped first. */
40
+ deliveredLimit: number;
37
41
  };
38
42
  /** Publication evidence stays in memory. A prepared durable attempt proves nothing. */
39
43
  export declare function createLogDelivery(params: LogDeliveryParams): {
40
44
  trigger: () => void;
41
45
  beginWalk: () => number;
42
46
  certify: (epoch: number, started: number) => void;
47
+ /**
48
+ * Another member rejoined by external commit. A member stranded behind a retention gap cannot
49
+ * read the epochs it missed, so entries delivered there never reach it. Send the delivered
50
+ * entries again at the current epoch, but only those from epochs at or after `anchorEpoch`,
51
+ * the anchor before the rejoin: the roster has not changed since, so every member that can
52
+ * read the copy was a member when the entry was first sent. Receivers deduplicate by content.
53
+ */
54
+ rejoined: (anchorEpoch: number) => void;
43
55
  removed: () => void;
44
56
  close: () => void;
45
57
  };
@@ -7,6 +7,8 @@ export function checkedFetchResult(result) {
7
7
  /** Publication evidence stays in memory. A prepared durable attempt proves nothing. */ export function createLogDelivery(params) {
8
8
  const publications = new Map();
9
9
  const proven = new Set();
10
+ const delivered = [];
11
+ let resend = [];
10
12
  let acknowledgements = 0;
11
13
  let removed = false;
12
14
  let closed = false;
@@ -17,9 +19,28 @@ export function checkedFetchResult(result) {
17
19
  let timer;
18
20
  let backoff = 1000;
19
21
  const headPastFloor = (head, floor)=>head != null && (floor.position == null || head > floor.position);
22
+ const pruneDelivered = ()=>{
23
+ const oldest = Date.now() - params.deliveredWindowMs;
24
+ while(delivered.length > 0 && ((delivered[0]?.at ?? 0) < oldest || delivered.length > params.deliveredLimit))delivered.shift();
25
+ };
26
+ const keepDelivered = (seq)=>{
27
+ const entry = params.queue.entries().find((queued)=>queued.seq === seq);
28
+ const epoch = publications.get(seq)?.epoch;
29
+ if (entry == null || epoch == null) return;
30
+ const { protocol, prc, data } = entry;
31
+ delivered.push({
32
+ protocol,
33
+ prc,
34
+ data,
35
+ epoch,
36
+ at: Date.now()
37
+ });
38
+ pruneDelivered();
39
+ };
20
40
  const removeProven = async ()=>{
21
41
  for (const seq of proven){
22
42
  await params.remove(seq);
43
+ keepDelivered(seq);
23
44
  params.queue.remove(seq);
24
45
  publications.delete(seq);
25
46
  proven.delete(seq);
@@ -52,6 +73,24 @@ export function checkedFetchResult(result) {
52
73
  }
53
74
  // A failed durable removal retries alone, even after another epoch arrives.
54
75
  if (!await removeProven()) return;
76
+ // Sent again ahead of the queue, so a rejoined member reads them in their original order.
77
+ while(resend.length > 0){
78
+ const next = resend[0];
79
+ if (next == null || closed || params.held()) return;
80
+ const { protocol, prc, data } = next;
81
+ const frame = await params.seal({
82
+ protocol,
83
+ prc,
84
+ data,
85
+ seq: -1,
86
+ lastAttempt: null
87
+ });
88
+ if (closed || params.held()) return;
89
+ const submission = params.publish(frame);
90
+ if (submission == null) return;
91
+ await submission;
92
+ resend.shift();
93
+ }
55
94
  if (params.queue.entries().length === 0) return;
56
95
  let count = 0;
57
96
  for (const entry of params.queue.entries()){
@@ -143,7 +182,7 @@ export function checkedFetchResult(result) {
143
182
  active = false;
144
183
  if (!closed) {
145
184
  retrying = failed;
146
- if (failed || params.queue.entries().length > 0 || removed && needed) {
185
+ if (failed || params.queue.entries().length > 0 || resend.length > 0 || removed && needed) {
147
186
  schedule(!failed && immediate ? 0 : backoff);
148
187
  if (failed) backoff = Math.min(backoff * 2, 60_000);
149
188
  } else if (needed) schedule(0);
@@ -163,8 +202,27 @@ export function checkedFetchResult(result) {
163
202
  },
164
203
  beginWalk: ()=>acknowledgements,
165
204
  certify,
205
+ /**
206
+ * Another member rejoined by external commit. A member stranded behind a retention gap cannot
207
+ * read the epochs it missed, so entries delivered there never reach it. Send the delivered
208
+ * entries again at the current epoch, but only those from epochs at or after `anchorEpoch`,
209
+ * the anchor before the rejoin: the roster has not changed since, so every member that can
210
+ * read the copy was a member when the entry was first sent. Receivers deduplicate by content.
211
+ */ rejoined: (anchorEpoch)=>{
212
+ if (closed || removed) return;
213
+ pruneDelivered();
214
+ const queued = new Set(resend);
215
+ for (const entry of delivered){
216
+ if (entry.epoch >= anchorEpoch && !queued.has(entry)) resend.push(entry);
217
+ }
218
+ if (resend.length === 0) return;
219
+ needed = true;
220
+ schedule(0);
221
+ },
166
222
  removed: ()=>{
167
223
  removed = true;
224
+ delivered.length = 0;
225
+ resend = [];
168
226
  params.queue.stop();
169
227
  needed = true;
170
228
  schedule(0);
package/lib/peer.js CHANGED
@@ -480,7 +480,9 @@ export function createGroupPeer(input) {
480
480
  },
481
481
  remove: (seq)=>params.appOutbox.remove(seq),
482
482
  clear: ()=>params.appOutbox.clear(),
483
- cleared: (notice)=>notifyHost(params.onAppOutboxCleared, notice)
483
+ cleared: (notice)=>notifyHost(params.onAppOutboxCleared, notice),
484
+ deliveredWindowMs: appLogRetentionSeconds * 1000,
485
+ deliveredLimit: params.appOutboxLimit
484
486
  });
485
487
  /**
486
488
  * Set as `dispose()`'s FIRST statement. The peer's post-dispose rule has two forms and this is
@@ -1030,13 +1032,16 @@ export function createGroupPeer(input) {
1030
1032
  }));
1031
1033
  if (disposed) return false;
1032
1034
  const gap = result.gap || result.messages.length === 0 && result.head != null && pageAfter != null && result.head > pageAfter;
1033
- if (pageAfter == null) {
1035
+ // With no cursor the hub flags ANY log removal as a gap, so an unflagged read from the
1036
+ // oldest frame holds the whole log: a fresh member's floor stays covered, and its first
1037
+ // publication must not request a heal.
1038
+ if (pageAfter == null && result.gap) {
1034
1039
  covered = false;
1035
1040
  floor = {
1036
1041
  ...floor,
1037
1042
  covered: false
1038
1043
  };
1039
- } else if (gap) {
1044
+ } else if (pageAfter != null && gap) {
1040
1045
  floor = {
1041
1046
  ...floor,
1042
1047
  covered: false
@@ -1261,6 +1266,7 @@ export function createGroupPeer(input) {
1261
1266
  // the port's answer to it.
1262
1267
  if (disposed) return false;
1263
1268
  const framedEpoch = localEpoch;
1269
+ const anchorEpochBefore = anchor.epoch;
1264
1270
  let applied;
1265
1271
  try {
1266
1272
  // Through the seam, like every other site that ratchets the handle: it reads this
@@ -1346,6 +1352,7 @@ export function createGroupPeer(input) {
1346
1352
  sequenceID: position,
1347
1353
  digest: commitDigest
1348
1354
  });
1355
+ if (header?.external) logDelivery?.rejoined(anchorEpochBefore);
1349
1356
  }
1350
1357
  if (header?.external && !applied.advanced && !stranded) recordCommitOutcome(position, {
1351
1358
  kind: 'refused',
@@ -1735,6 +1742,8 @@ export function createGroupPeer(input) {
1735
1742
  }
1736
1743
  recoveryGeneration += 1;
1737
1744
  closeEpisode();
1745
+ // As on a rejoin that bootstraps at once: frames sent before the subscription woke nothing.
1746
+ requestAppPull();
1738
1747
  if (commitTopicID != null) {
1739
1748
  emitRecovery({
1740
1749
  phase: 'bootstrapped',
@@ -2371,6 +2380,9 @@ export function createGroupPeer(input) {
2371
2380
  clearRecoveryRetry();
2372
2381
  recoveryBackoff = 1000;
2373
2382
  closeEpisode();
2383
+ // A push only wakes a pull, and frames published to the rejoined segment before its
2384
+ // subscription existed woke nothing.
2385
+ requestAppPull();
2374
2386
  emitRecovery({
2375
2387
  ...base,
2376
2388
  phase: 'succeeded'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/rpc",
3
- "version": "0.10.4",
3
+ "version": "0.10.6",
4
4
  "description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
5
5
  "keywords": [
6
6
  "rpc",
@@ -28,12 +28,12 @@
28
28
  "dependencies": {
29
29
  "@enkaku/client": "^0.21.3",
30
30
  "@enkaku/protocol": "^0.21.4",
31
- "@enkaku/server": "^0.21.3",
31
+ "@enkaku/server": "^0.21.4",
32
32
  "@enkaku/transport": "^0.21.3",
33
33
  "@kokuin/token": "^0.5.0",
34
34
  "@kumiai/broadcast": "^0.10.0",
35
35
  "@kumiai/hub-protocol": "^0.10.1",
36
- "@kumiai/hub-tunnel": "^0.10.2",
36
+ "@kumiai/hub-tunnel": "^0.10.3",
37
37
  "@noble/hashes": "^2.4.0",
38
38
  "@sozai/codec": "^0.4.2",
39
39
  "@sozai/event": "^0.1.5",