@peerbit/shared-log 8.0.5 → 8.0.7-0691c73
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/benchmark/index.d.ts +2 -0
- package/dist/benchmark/index.d.ts.map +1 -0
- package/{lib/esm/__benchmark__ → dist/benchmark}/index.js +2 -1
- package/dist/benchmark/index.js.map +1 -0
- package/{lib/esm → dist/src}/blocks.d.ts +1 -0
- package/dist/src/blocks.d.ts.map +1 -0
- package/{lib/esm → dist/src}/blocks.js +1 -1
- package/dist/src/blocks.js.map +1 -0
- package/{lib/esm → dist/src}/cpu.d.ts +2 -1
- package/dist/src/cpu.d.ts.map +1 -0
- package/{lib/esm → dist/src}/cpu.js +2 -2
- package/dist/src/cpu.js.map +1 -0
- package/{lib/esm → dist/src}/exchange-heads.d.ts +1 -0
- package/dist/src/exchange-heads.d.ts.map +1 -0
- package/{lib/esm → dist/src}/exchange-heads.js +2 -0
- package/dist/src/exchange-heads.js.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +17 -13
- package/dist/src/index.d.ts.map +1 -0
- package/{lib/esm → dist/src}/index.js +76 -46
- package/dist/src/index.js.map +1 -0
- package/{lib/esm → dist/src}/message.d.ts +1 -0
- package/dist/src/message.d.ts.map +1 -0
- package/{lib/esm → dist/src}/pid.d.ts +1 -0
- package/dist/src/pid.d.ts.map +1 -0
- package/{lib/esm → dist/src}/pid.js.map +1 -1
- package/{lib/esm → dist/src}/ranges.d.ts +3 -2
- package/dist/src/ranges.d.ts.map +1 -0
- package/{lib/esm → dist/src}/ranges.js +5 -2
- package/dist/src/ranges.js.map +1 -0
- package/{lib/esm → dist/src}/replication.d.ts +1 -0
- package/dist/src/replication.d.ts.map +1 -0
- package/{lib/esm → dist/src}/replication.js +3 -1
- package/dist/src/replication.js.map +1 -0
- package/{lib/esm → dist/src}/role.d.ts +2 -1
- package/dist/src/role.d.ts.map +1 -0
- package/package.json +69 -43
- package/src/cpu.ts +5 -4
- package/src/exchange-heads.ts +2 -2
- package/src/index.ts +142 -108
- package/src/pid.ts +1 -1
- package/src/ranges.ts +2 -4
- package/src/role.ts +1 -1
- package/LICENSE +0 -202
- package/lib/esm/__benchmark__/index.d.ts +0 -1
- package/lib/esm/__benchmark__/index.js.map +0 -1
- package/lib/esm/blocks.js.map +0 -1
- package/lib/esm/cpu.js.map +0 -1
- package/lib/esm/exchange-heads.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/ranges.js.map +0 -1
- package/lib/esm/replication.js.map +0 -1
- package/src/__benchmark__/index.ts +0 -115
- /package/{lib/esm → dist/src}/message.js +0 -0
- /package/{lib/esm → dist/src}/message.js.map +0 -0
- /package/{lib/esm → dist/src}/pid.js +0 -0
- /package/{lib/esm → dist/src}/role.js +0 -0
- /package/{lib/esm → dist/src}/role.js.map +0 -0
package/src/index.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { RequestContext, RPC } from "@peerbit/rpc";
|
|
1
|
+
import { type RequestContext, RPC } from "@peerbit/rpc";
|
|
2
2
|
import { TransportMessage } from "./message.js";
|
|
3
3
|
import {
|
|
4
|
-
AppendOptions,
|
|
4
|
+
type AppendOptions,
|
|
5
|
+
type Change,
|
|
5
6
|
Entry,
|
|
6
7
|
Log,
|
|
7
|
-
LogEvents,
|
|
8
|
-
LogProperties,
|
|
9
|
-
ShallowEntry
|
|
8
|
+
type LogEvents,
|
|
9
|
+
type LogProperties,
|
|
10
10
|
} from "@peerbit/log";
|
|
11
|
-
import { Program, ProgramEvents } from "@peerbit/program";
|
|
11
|
+
import { Program, type ProgramEvents } from "@peerbit/program";
|
|
12
12
|
import { BinaryWriter, BorshError, field, variant } from "@dao-xyz/borsh";
|
|
13
13
|
import {
|
|
14
14
|
AccessError,
|
|
@@ -35,8 +35,8 @@ import { Observer, Replicator, Role } from "./role.js";
|
|
|
35
35
|
import {
|
|
36
36
|
AbsoluteReplicas,
|
|
37
37
|
ReplicationError,
|
|
38
|
-
ReplicationLimits,
|
|
39
|
-
ReplicatorRect,
|
|
38
|
+
type ReplicationLimits,
|
|
39
|
+
type ReplicatorRect,
|
|
40
40
|
RequestRoleMessage,
|
|
41
41
|
ResponseRoleMessage,
|
|
42
42
|
decodeReplicas,
|
|
@@ -44,7 +44,7 @@ import {
|
|
|
44
44
|
hashToUniformNumber,
|
|
45
45
|
maxReplicas
|
|
46
46
|
} from "./replication.js";
|
|
47
|
-
import pDefer, { DeferredPromise } from "p-defer";
|
|
47
|
+
import pDefer, { type DeferredPromise } from "p-defer";
|
|
48
48
|
import { Cache } from "@peerbit/cache";
|
|
49
49
|
import { CustomEvent } from "@libp2p/interface";
|
|
50
50
|
import yallist from "yallist";
|
|
@@ -60,7 +60,7 @@ import debounce from "p-debounce";
|
|
|
60
60
|
import { PIDReplicationController } from "./pid.js";
|
|
61
61
|
export * from "./replication.js";
|
|
62
62
|
import PQueue from "p-queue";
|
|
63
|
-
import { CPUUsage, CPUUsageIntervalLag } from "./cpu.js";
|
|
63
|
+
import { type CPUUsage, CPUUsageIntervalLag } from "./cpu.js";
|
|
64
64
|
import { getCoverSet, getSamples, isMatured } from "./ranges.js";
|
|
65
65
|
export { type CPUUsage, CPUUsageIntervalLag };
|
|
66
66
|
export { Observer, Replicator, Role };
|
|
@@ -101,6 +101,7 @@ export type AdaptiveReplicatorOptions = {
|
|
|
101
101
|
|
|
102
102
|
export type FixedReplicatorOptions = {
|
|
103
103
|
type: "replicator";
|
|
104
|
+
offset?: number;
|
|
104
105
|
factor: number;
|
|
105
106
|
};
|
|
106
107
|
|
|
@@ -167,14 +168,14 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
167
168
|
rpc: RPC<TransportMessage, TransportMessage>;
|
|
168
169
|
|
|
169
170
|
// options
|
|
170
|
-
private _role
|
|
171
|
-
private _roleConfig
|
|
172
|
-
private _sortedPeersCache
|
|
173
|
-
private _totalParticipation
|
|
174
|
-
private _gidPeersHistory
|
|
171
|
+
private _role!: Observer | Replicator;
|
|
172
|
+
private _roleConfig!: AdaptiveReplicatorOptions | Observer | Replicator;
|
|
173
|
+
private _sortedPeersCache!: yallist<ReplicatorRect> | undefined;
|
|
174
|
+
private _totalParticipation!: number;
|
|
175
|
+
private _gidPeersHistory!: Map<string, Set<string>>;
|
|
175
176
|
|
|
176
|
-
private _onSubscriptionFn
|
|
177
|
-
private _onUnsubscriptionFn
|
|
177
|
+
private _onSubscriptionFn!: (arg: any) => any;
|
|
178
|
+
private _onUnsubscriptionFn!: (arg: any) => any;
|
|
178
179
|
|
|
179
180
|
private _canReplicate?: (
|
|
180
181
|
publicKey: PublicSignKey,
|
|
@@ -182,10 +183,10 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
182
183
|
) => Promise<boolean> | boolean;
|
|
183
184
|
|
|
184
185
|
private _logProperties?: LogProperties<T> & LogEvents<T>;
|
|
185
|
-
private _closeController
|
|
186
|
-
private _gidParentCache
|
|
187
|
-
private _respondToIHaveTimeout;
|
|
188
|
-
private _pendingDeletes
|
|
186
|
+
private _closeController!: AbortController;
|
|
187
|
+
private _gidParentCache!: Cache<Entry<any>[]>;
|
|
188
|
+
private _respondToIHaveTimeout!: any;
|
|
189
|
+
private _pendingDeletes!: Map<
|
|
189
190
|
string,
|
|
190
191
|
{
|
|
191
192
|
promise: DeferredPromise<void>;
|
|
@@ -195,17 +196,17 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
195
196
|
}
|
|
196
197
|
>;
|
|
197
198
|
|
|
198
|
-
private _pendingIHave
|
|
199
|
+
private _pendingIHave!: Map<
|
|
199
200
|
string,
|
|
200
201
|
{ clear: () => void; callback: (entry: Entry<T>) => void }
|
|
201
202
|
>;
|
|
202
203
|
|
|
203
|
-
private latestRoleMessages
|
|
204
|
+
private latestRoleMessages!: Map<string, bigint>;
|
|
204
205
|
|
|
205
|
-
private remoteBlocks
|
|
206
|
+
private remoteBlocks!: RemoteBlocks;
|
|
206
207
|
|
|
207
|
-
private openTime
|
|
208
|
-
private oldestOpenTime
|
|
208
|
+
private openTime!: number;
|
|
209
|
+
private oldestOpenTime!: number;
|
|
209
210
|
|
|
210
211
|
private sync?: (entry: Entry<T>) => boolean;
|
|
211
212
|
|
|
@@ -215,22 +216,30 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
215
216
|
| undefined;
|
|
216
217
|
|
|
217
218
|
// regular distribution checks
|
|
218
|
-
private distributeInterval
|
|
219
|
+
private distributeInterval!: ReturnType<typeof setInterval>;
|
|
219
220
|
private distributeQueue?: PQueue;
|
|
220
221
|
|
|
221
222
|
// Syncing and dedeplucation work
|
|
222
223
|
private syncMoreInterval?: ReturnType<typeof setTimeout>;
|
|
223
|
-
private syncInFlightQueue: Map<string, PublicSignKey[]>;
|
|
224
|
-
private syncInFlightQueueInverted: Map<string, Set<string>>;
|
|
225
|
-
private syncInFlight: Map<string, Map<string, { timestamp: number }>>;
|
|
226
224
|
|
|
227
|
-
|
|
225
|
+
// map of hash to public keys that we can ask for entries
|
|
226
|
+
private syncInFlightQueue!: Map<string, PublicSignKey[]>;
|
|
227
|
+
private syncInFlightQueueInverted!: Map<string, Set<string>>;
|
|
228
|
+
|
|
229
|
+
// map of hash to public keys that we have asked for entries
|
|
230
|
+
private syncInFlight!: Map<string, Map<string, { timestamp: number }>>;
|
|
231
|
+
|
|
232
|
+
replicas!: ReplicationLimits;
|
|
228
233
|
|
|
229
234
|
private cpuUsage?: CPUUsage;
|
|
230
235
|
|
|
231
|
-
timeUntilRoleMaturity
|
|
232
|
-
waitForReplicatorTimeout
|
|
233
|
-
distributionDebounceTime
|
|
236
|
+
timeUntilRoleMaturity!: number;
|
|
237
|
+
waitForReplicatorTimeout!: number;
|
|
238
|
+
distributionDebounceTime!: number;
|
|
239
|
+
|
|
240
|
+
replicationController!: PIDReplicationController;
|
|
241
|
+
history!: { usedMemory: number; factor: number }[];
|
|
242
|
+
|
|
234
243
|
|
|
235
244
|
constructor(properties?: { id?: Uint8Array }) {
|
|
236
245
|
super();
|
|
@@ -263,7 +272,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
263
272
|
REBALANCE_DEBOUNCE_INTERVAL,
|
|
264
273
|
Math.log(
|
|
265
274
|
(this.getReplicatorsSorted()?.length || 0) *
|
|
266
|
-
|
|
275
|
+
REBALANCE_DEBOUNCE_INTERVAL
|
|
267
276
|
)
|
|
268
277
|
)
|
|
269
278
|
);
|
|
@@ -283,11 +292,11 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
283
292
|
cpu:
|
|
284
293
|
options?.limits?.cpu != null
|
|
285
294
|
? {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
295
|
+
max:
|
|
296
|
+
typeof options?.limits?.cpu === "object"
|
|
297
|
+
? options.limits.cpu.max
|
|
298
|
+
: options?.limits?.cpu
|
|
299
|
+
}
|
|
291
300
|
: undefined
|
|
292
301
|
}
|
|
293
302
|
);
|
|
@@ -316,7 +325,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
316
325
|
} else {
|
|
317
326
|
this._roleConfig = new Replicator({
|
|
318
327
|
factor: options.factor,
|
|
319
|
-
offset: this.getReplicationOffset()
|
|
328
|
+
offset: options?.offset ?? this.getReplicationOffset()
|
|
320
329
|
});
|
|
321
330
|
}
|
|
322
331
|
} else {
|
|
@@ -341,12 +350,12 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
341
350
|
if (this._roleConfig?.limits) {
|
|
342
351
|
this._role = new Replicator({
|
|
343
352
|
factor: this._role instanceof Replicator ? this._role.factor : 0,
|
|
344
|
-
offset: this.getReplicationOffset()
|
|
353
|
+
offset: this._role instanceof Replicator ? this._role.offset : this.getReplicationOffset()
|
|
345
354
|
});
|
|
346
355
|
} else {
|
|
347
356
|
this._role = new Replicator({
|
|
348
357
|
factor: this._role instanceof Replicator ? this._role.factor : 1,
|
|
349
|
-
offset: this.getReplicationOffset()
|
|
358
|
+
offset: this._role instanceof Replicator ? this._role.offset : this.getReplicationOffset()
|
|
350
359
|
});
|
|
351
360
|
}
|
|
352
361
|
}
|
|
@@ -403,6 +412,19 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
403
412
|
} else {
|
|
404
413
|
appendOptions.meta.data = minReplicasData;
|
|
405
414
|
}
|
|
415
|
+
if (options?.canAppend) {
|
|
416
|
+
appendOptions.canAppend = async (entry) => {
|
|
417
|
+
await this.canAppend(entry);
|
|
418
|
+
return options.canAppend!(entry);
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (options?.onChange) {
|
|
423
|
+
appendOptions.onChange = async (change) => {
|
|
424
|
+
await this.onChange(change);
|
|
425
|
+
return options.onChange!(change);
|
|
426
|
+
};
|
|
427
|
+
}
|
|
406
428
|
|
|
407
429
|
const result = await this.log.append(data, appendOptions);
|
|
408
430
|
let mode: DeliveryMode | undefined = undefined;
|
|
@@ -478,9 +500,9 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
478
500
|
this.openTime = +new Date();
|
|
479
501
|
this.oldestOpenTime = this.openTime;
|
|
480
502
|
this.distributionDebounceTime =
|
|
481
|
-
options?.distributionDebounceTime || DEFAULT_DISTRIBUTION_DEBOUNCE_TIME;
|
|
503
|
+
options?.distributionDebounceTime || DEFAULT_DISTRIBUTION_DEBOUNCE_TIME; // expect > 0
|
|
482
504
|
this.timeUntilRoleMaturity =
|
|
483
|
-
options?.timeUntilRoleMaturity
|
|
505
|
+
options?.timeUntilRoleMaturity ?? WAIT_FOR_ROLE_MATURITY;
|
|
484
506
|
this.waitForReplicatorTimeout =
|
|
485
507
|
options?.waitForReplicatorTimeout || WAIT_FOR_REPLICATOR_TIMEOUT;
|
|
486
508
|
this._gidParentCache = new Cache({ max: 1000 });
|
|
@@ -521,40 +543,13 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
521
543
|
keychain: this.node.services.keychain,
|
|
522
544
|
...this._logProperties,
|
|
523
545
|
onChange: (change) => {
|
|
524
|
-
|
|
525
|
-
this.onEntryAdded(added);
|
|
526
|
-
}
|
|
527
|
-
for (const removed of change.removed) {
|
|
528
|
-
this.onEntryRemoved(removed.hash);
|
|
529
|
-
}
|
|
546
|
+
this.onChange(change);
|
|
530
547
|
return this._logProperties?.onChange?.(change);
|
|
531
548
|
},
|
|
532
549
|
canAppend: async (entry) => {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
logger.warn("Received entry without meta data, skipping");
|
|
536
|
-
return false;
|
|
537
|
-
}
|
|
538
|
-
const replicas = decodeReplicas(entry).getValue(this);
|
|
539
|
-
if (Number.isFinite(replicas) === false) {
|
|
540
|
-
return false;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
// Don't verify entries that we have created (TODO should we? perf impact?)
|
|
544
|
-
if (!entry.createdLocally && !(await entry.verifySignatures())) {
|
|
545
|
-
return false;
|
|
546
|
-
}
|
|
547
|
-
} catch (error) {
|
|
548
|
-
if (
|
|
549
|
-
error instanceof BorshError ||
|
|
550
|
-
error instanceof ReplicationError
|
|
551
|
-
) {
|
|
552
|
-
logger.warn("Received payload that could not be decoded, skipping");
|
|
553
|
-
return false;
|
|
554
|
-
}
|
|
555
|
-
throw error;
|
|
550
|
+
if (!(await this.canAppend(entry))) {
|
|
551
|
+
return false;
|
|
556
552
|
}
|
|
557
|
-
|
|
558
553
|
return this._logProperties?.canAppend?.(entry) ?? true;
|
|
559
554
|
},
|
|
560
555
|
trim: this._logProperties?.trim && {
|
|
@@ -666,6 +661,40 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
666
661
|
return this.log.idString;
|
|
667
662
|
}
|
|
668
663
|
|
|
664
|
+
async onChange(change: Change<T>) {
|
|
665
|
+
for (const added of change.added) {
|
|
666
|
+
this.onEntryAdded(added);
|
|
667
|
+
}
|
|
668
|
+
for (const removed of change.removed) {
|
|
669
|
+
this.onEntryRemoved(removed.hash);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
async canAppend(entry: Entry<T>) {
|
|
674
|
+
try {
|
|
675
|
+
if (!entry.meta.data) {
|
|
676
|
+
logger.warn("Received entry without meta data, skipping");
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
const replicas = decodeReplicas(entry).getValue(this);
|
|
680
|
+
if (Number.isFinite(replicas) === false) {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Don't verify entries that we have created (TODO should we? perf impact?)
|
|
685
|
+
if (!entry.createdLocally && !(await entry.verifySignatures())) {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
return true;
|
|
689
|
+
} catch (error) {
|
|
690
|
+
if (error instanceof BorshError || error instanceof ReplicationError) {
|
|
691
|
+
logger.warn("Received payload that could not be decoded, skipping");
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
throw error;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
669
698
|
private async _close() {
|
|
670
699
|
clearTimeout(this.syncMoreInterval);
|
|
671
700
|
clearInterval(this.distributeInterval);
|
|
@@ -684,11 +713,11 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
684
713
|
this._onUnsubscriptionFn
|
|
685
714
|
);
|
|
686
715
|
|
|
687
|
-
for (const [
|
|
716
|
+
for (const [_k, v] of this._pendingDeletes) {
|
|
688
717
|
v.clear();
|
|
689
718
|
v.promise.resolve(); // TODO or reject?
|
|
690
719
|
}
|
|
691
|
-
for (const [
|
|
720
|
+
for (const [_k, v] of this._pendingIHave) {
|
|
692
721
|
v.clear();
|
|
693
722
|
}
|
|
694
723
|
|
|
@@ -748,8 +777,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
748
777
|
const { heads } = msg;
|
|
749
778
|
|
|
750
779
|
logger.debug(
|
|
751
|
-
`${this.node.identity.publicKey.hashcode()}: Recieved heads: ${
|
|
752
|
-
heads.length === 1 ? heads[0].entry.hash : "#" + heads.length
|
|
780
|
+
`${this.node.identity.publicKey.hashcode()}: Recieved heads: ${heads.length === 1 ? heads[0].entry.hash : "#" + heads.length
|
|
753
781
|
}, logId: ${this.log.idString}`
|
|
754
782
|
);
|
|
755
783
|
if (heads) {
|
|
@@ -792,13 +820,13 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
792
820
|
|
|
793
821
|
const leaders = await (this.role instanceof Observer
|
|
794
822
|
? this.findLeaders(
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
823
|
+
gid,
|
|
824
|
+
Math.max(maxReplicasFromHead, maxReplicasFromNewEntries)
|
|
825
|
+
)
|
|
798
826
|
: this.waitForIsLeader(
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
827
|
+
gid,
|
|
828
|
+
Math.max(maxReplicasFromHead, maxReplicasFromNewEntries)
|
|
829
|
+
));
|
|
802
830
|
|
|
803
831
|
const isLeader = !!leaders;
|
|
804
832
|
|
|
@@ -832,8 +860,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
832
860
|
}
|
|
833
861
|
|
|
834
862
|
logger.debug(
|
|
835
|
-
`${this.node.identity.publicKey.hashcode()}: Dropping heads with gid: ${
|
|
836
|
-
entry.entry.gid
|
|
863
|
+
`${this.node.identity.publicKey.hashcode()}: Dropping heads with gid: ${entry.entry.gid
|
|
837
864
|
}. Because not leader`
|
|
838
865
|
);
|
|
839
866
|
}
|
|
@@ -913,7 +940,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
913
940
|
clearTimeout(timeout);
|
|
914
941
|
prevPendingIHave?.clear();
|
|
915
942
|
},
|
|
916
|
-
callback: async (entry) => {
|
|
943
|
+
callback: async (entry: any) => {
|
|
917
944
|
if (
|
|
918
945
|
await this.isLeader(
|
|
919
946
|
entry.meta.gid,
|
|
@@ -979,6 +1006,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
979
1006
|
await this.requestSync(requestHashes, [context.from.hashcode()]);
|
|
980
1007
|
} else if (msg instanceof ResponseMaybeSync) {
|
|
981
1008
|
// TODO better choice of step size
|
|
1009
|
+
|
|
982
1010
|
const entries = (
|
|
983
1011
|
await Promise.all(msg.hashes.map((x) => this.log.get(x)))
|
|
984
1012
|
).filter((x): x is Entry<any> => !!x);
|
|
@@ -987,6 +1015,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
987
1015
|
entries,
|
|
988
1016
|
this._gidParentCache
|
|
989
1017
|
);
|
|
1018
|
+
|
|
990
1019
|
// TODO perhaps send less messages to more receivers for performance reasons?
|
|
991
1020
|
// TODO wait for previous send to target before trying to send more?
|
|
992
1021
|
let p = Promise.resolve();
|
|
@@ -1089,7 +1118,15 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1089
1118
|
}
|
|
1090
1119
|
return true;
|
|
1091
1120
|
};
|
|
1092
|
-
return waitFor(() => check(), {
|
|
1121
|
+
return waitFor(() => check(), {
|
|
1122
|
+
signal: this._closeController.signal
|
|
1123
|
+
}).catch((e) => {
|
|
1124
|
+
if (e instanceof AbortError) {
|
|
1125
|
+
// ignore error
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
throw e;
|
|
1129
|
+
});
|
|
1093
1130
|
}
|
|
1094
1131
|
|
|
1095
1132
|
async isLeader(
|
|
@@ -1198,17 +1235,14 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1198
1235
|
) {
|
|
1199
1236
|
const roleAge = options?.roleAge ?? this.getDefaultMinRoleAge(); // TODO -500 as is added so that i f someone else is just as new as us, then we treat them as mature as us. without -500 we might be slower syncing if two nodes starts almost at the same time
|
|
1200
1237
|
|
|
1201
|
-
const
|
|
1238
|
+
const samples = getSamples(
|
|
1202
1239
|
cursor,
|
|
1203
1240
|
this.getReplicatorsSorted()!,
|
|
1204
1241
|
numberOfLeaders,
|
|
1205
|
-
roleAge
|
|
1206
|
-
this.role instanceof Replicator && this.role.factor === 0
|
|
1207
|
-
? this.node.identity.publicKey.hashcode()
|
|
1208
|
-
: undefined
|
|
1242
|
+
roleAge
|
|
1209
1243
|
);
|
|
1210
1244
|
|
|
1211
|
-
return
|
|
1245
|
+
return samples;
|
|
1212
1246
|
}
|
|
1213
1247
|
|
|
1214
1248
|
/**
|
|
@@ -1387,6 +1421,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1387
1421
|
}
|
|
1388
1422
|
|
|
1389
1423
|
if (role.offset > currentNode.value.role.offset) {
|
|
1424
|
+
// @ts-ignore
|
|
1390
1425
|
const next = currentNode?.next;
|
|
1391
1426
|
if (next) {
|
|
1392
1427
|
currentNode = next;
|
|
@@ -1620,7 +1655,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1620
1655
|
signal: this._closeController.signal
|
|
1621
1656
|
}).then(() => this._distribute())
|
|
1622
1657
|
)
|
|
1623
|
-
.catch(() => {}); // catch ignore delay abort errror
|
|
1658
|
+
.catch(() => { }); // catch ignore delay abort errror
|
|
1624
1659
|
}
|
|
1625
1660
|
|
|
1626
1661
|
async _distribute() {
|
|
@@ -1779,9 +1814,8 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1779
1814
|
true
|
|
1780
1815
|
);
|
|
1781
1816
|
}
|
|
1782
|
-
replicationController: PIDReplicationController;
|
|
1783
1817
|
|
|
1784
|
-
|
|
1818
|
+
|
|
1785
1819
|
async addToHistory(usedMemory: number, factor: number) {
|
|
1786
1820
|
(this.history || (this.history = [])).push({ usedMemory, factor });
|
|
1787
1821
|
|
|
@@ -1908,17 +1942,17 @@ function _insertAfter(
|
|
|
1908
1942
|
) {
|
|
1909
1943
|
const inserted = !node
|
|
1910
1944
|
? new yallist.Node(
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1945
|
+
value,
|
|
1946
|
+
null as any,
|
|
1947
|
+
self.head as yallist.Node<ReplicatorRect> | undefined,
|
|
1948
|
+
self
|
|
1949
|
+
)
|
|
1916
1950
|
: new yallist.Node(
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1951
|
+
value,
|
|
1952
|
+
node,
|
|
1953
|
+
node.next as yallist.Node<ReplicatorRect> | undefined,
|
|
1954
|
+
self
|
|
1955
|
+
);
|
|
1922
1956
|
|
|
1923
1957
|
// is tail
|
|
1924
1958
|
if (inserted.next === null) {
|
package/src/pid.ts
CHANGED
package/src/ranges.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import yallist from "yallist";
|
|
2
|
-
import { ReplicatorRect } from "./replication.js";
|
|
2
|
+
import { type ReplicatorRect } from "./replication.js";
|
|
3
3
|
import { Replicator } from "./role.js";
|
|
4
4
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
5
5
|
|
|
@@ -96,8 +96,7 @@ export const getSamples = (
|
|
|
96
96
|
cursor: number,
|
|
97
97
|
peers: yallist<ReplicatorRect>,
|
|
98
98
|
amount: number,
|
|
99
|
-
roleAge: number
|
|
100
|
-
dbg?: string
|
|
99
|
+
roleAge: number
|
|
101
100
|
) => {
|
|
102
101
|
const leaders: Set<string> = new Set();
|
|
103
102
|
const width = 1;
|
|
@@ -108,7 +107,6 @@ export const getSamples = (
|
|
|
108
107
|
|
|
109
108
|
const t = +new Date();
|
|
110
109
|
|
|
111
|
-
const matured = 0;
|
|
112
110
|
|
|
113
111
|
const maturedLeaders = new Set();
|
|
114
112
|
for (let i = 0; i < amount; i++) {
|
package/src/role.ts
CHANGED