@peerbit/shared-log 4.1.0 → 5.0.1
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/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +39 -28
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/pid.d.ts +11 -2
- package/lib/esm/pid.js +50 -28
- package/lib/esm/pid.js.map +1 -1
- package/lib/esm/replication.d.ts +0 -1
- package/lib/esm/replication.js.map +1 -1
- package/lib/esm/role.d.ts +5 -5
- package/lib/esm/role.js +6 -10
- package/lib/esm/role.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +58 -44
- package/src/pid.ts +57 -37
- package/src/replication.ts +0 -1
- package/src/role.ts +10 -16
package/lib/esm/pid.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pid.js","sourceRoot":"","sources":["../../src/pid.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,wBAAwB;
|
|
1
|
+
{"version":3,"file":"pid.js","sourceRoot":"","sources":["../../src/pid.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,wBAAwB;IAW1B;IAVV,QAAQ,CAAS;IACjB,SAAS,CAAS;IAClB,eAAe,CAAS;IACxB,eAAe,CAAS;IACxB,EAAE,CAAS;IACX,EAAE,CAAS;IACX,EAAE,CAAS;IACX,aAAa,CAA2B;IACxC,iBAAiB,CAAU;IAC3B,YACU,EAAU,EACnB,UAMI,EAAE;QAPG,OAAE,GAAF,EAAE,CAAQ;QASnB,MAAM,EACL,iBAAiB,EACjB,EAAE,GAAG,GAAG,EACR,EAAE,GAAG,KAAK,EACV,EAAE,GAAG,IAAI,EACT,aAAa,GAAG,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE;YACjD,OAAO,MAAM,GAAG,CAAC;gBAChB,CAAC,CAAC,MAAM,GAAG,GAAG,GAAG,OAAO,GAAG,IAAI,GAAG,QAAQ,GA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AM,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;QACrC,MAAM,SAAS,GAAG,aAAa,GAAG,MAAM,CAAC;QAEzC,sCAAsC;QACtC,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC;QAE5B,IAAI,SAAS,GAAG,CAAC,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;YACpC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QACnB,CAAC;QAED,8CAA8C;QAC9C;;;;cAIG;QAEH;;;;;;;;;;;;;;;;;cAiBM;QAEN,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK;QACJ,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC;IAC1B,CAAC;CACD"}
|
package/lib/esm/replication.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replication.js","sourceRoot":"","sources":["../../src/replication.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACN,OAAO,EACP,WAAW,EACX,SAAS,EACT,KAAK,EACL,MAAM,EACN,YAAY,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAwB,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"replication.js","sourceRoot":"","sources":["../../src/replication.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACN,OAAO,EACP,WAAW,EACX,SAAS,EACT,KAAK,EACL,MAAM,EACN,YAAY,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAwB,IAAI,EAAE,MAAM,WAAW,C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,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAC3C,CAAC,GAAG,EAAE,CAAC;IACR,OAAO,UAAU,GAAG,UAAU,CAAC,CAAC,0BAA0B;AAC3D,CAAC,CAAC"}
|
package/lib/esm/role.d.ts
CHANGED
|
@@ -13,24 +13,24 @@ export declare const REPLICATOR_TYPE_VARIANT: Uint8Array;
|
|
|
13
13
|
declare class ReplicationSegment {
|
|
14
14
|
timestamp: bigint;
|
|
15
15
|
private factorNominator;
|
|
16
|
-
private offsetNominator
|
|
16
|
+
private offsetNominator;
|
|
17
17
|
constructor(properties: {
|
|
18
18
|
factor: number;
|
|
19
|
+
offset: number;
|
|
19
20
|
timestamp?: bigint;
|
|
20
|
-
offset?: number;
|
|
21
21
|
});
|
|
22
22
|
get factor(): number;
|
|
23
|
-
get offset(): number
|
|
23
|
+
get offset(): number;
|
|
24
24
|
}
|
|
25
25
|
export declare class Replicator extends Role {
|
|
26
26
|
segments: ReplicationSegment[];
|
|
27
27
|
constructor(properties: {
|
|
28
28
|
factor: number;
|
|
29
29
|
timestamp?: bigint;
|
|
30
|
-
offset
|
|
30
|
+
offset: number;
|
|
31
31
|
});
|
|
32
32
|
get factor(): number;
|
|
33
|
-
get offset(): number
|
|
33
|
+
get offset(): number;
|
|
34
34
|
get timestamp(): bigint;
|
|
35
35
|
equals(other: Role): boolean;
|
|
36
36
|
}
|
package/lib/esm/role.js
CHANGED
|
@@ -8,7 +8,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
var NoType_1, Observer_1, Replicator_1;
|
|
11
|
-
import { field,
|
|
11
|
+
import { field, variant, vec } from "@dao-xyz/borsh";
|
|
12
12
|
export class Role {
|
|
13
13
|
}
|
|
14
14
|
export const NO_TYPE_VARIANT = new Uint8Array([0]);
|
|
@@ -43,20 +43,16 @@ class ReplicationSegment {
|
|
|
43
43
|
}
|
|
44
44
|
this.timestamp = timestamp ?? BigInt(+new Date());
|
|
45
45
|
this.factorNominator = Math.round(4294967295 * factor);
|
|
46
|
-
if (offset
|
|
47
|
-
|
|
48
|
-
throw new Error("Expecting offset to be between 0 and 1, got: " + offset);
|
|
49
|
-
}
|
|
50
|
-
this.offsetNominator = Math.round(4294967295 * offset);
|
|
46
|
+
if (offset > 1 || offset < 0) {
|
|
47
|
+
throw new Error("Expecting offset to be between 0 and 1, got: " + offset);
|
|
51
48
|
}
|
|
49
|
+
this.offsetNominator = Math.round(4294967295 * offset);
|
|
52
50
|
}
|
|
53
51
|
get factor() {
|
|
54
52
|
return this.factorNominator / 4294967295;
|
|
55
53
|
}
|
|
56
54
|
get offset() {
|
|
57
|
-
return this.offsetNominator
|
|
58
|
-
? this.offsetNominator / 4294967295
|
|
59
|
-
: undefined;
|
|
55
|
+
return this.offsetNominator / 4294967295;
|
|
60
56
|
}
|
|
61
57
|
}
|
|
62
58
|
__decorate([
|
|
@@ -68,7 +64,7 @@ __decorate([
|
|
|
68
64
|
__metadata("design:type", Number)
|
|
69
65
|
], ReplicationSegment.prototype, "factorNominator", void 0);
|
|
70
66
|
__decorate([
|
|
71
|
-
field({ type:
|
|
67
|
+
field({ type: "u32" }),
|
|
72
68
|
__metadata("design:type", Number)
|
|
73
69
|
], ReplicationSegment.prototype, "offsetNominator", void 0);
|
|
74
70
|
let Replicator = Replicator_1 = class Replicator extends Role {
|
package/lib/esm/role.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"role.js","sourceRoot":"","sources":["../../src/role.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"role.js","sourceRoot":"","sources":["../../src/role.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAU,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAE7D,MAAM,OAAgB,IAAI;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CAAA;AA/BA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,kBAAkB,CAAC,EAAE,CAAC;;4CACV;AAFnB,UAAU;IADtB,OAAO,CAAC,CAAC,CAAC;;GACE,UAAU,CAiCtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/shared-log",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "Shared log",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -31,15 +31,15 @@
|
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@dao-xyz/borsh": "^5.1.8",
|
|
34
|
-
"@peerbit/log": "3.0.
|
|
34
|
+
"@peerbit/log": "3.0.12",
|
|
35
35
|
"@peerbit/logger": "1.0.2",
|
|
36
|
-
"@peerbit/program": "3.0.
|
|
37
|
-
"@peerbit/rpc": "3.0.
|
|
38
|
-
"@peerbit/time": "2.0.
|
|
36
|
+
"@peerbit/program": "3.0.10",
|
|
37
|
+
"@peerbit/rpc": "3.0.12",
|
|
38
|
+
"@peerbit/time": "2.0.4",
|
|
39
39
|
"p-debounce": "^4.0.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@peerbit/test-utils": "^2.0.
|
|
42
|
+
"@peerbit/test-utils": "^2.0.12"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "2539f936bbe572f34a297bc8275fcd059105fc2a"
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -127,12 +127,13 @@ export type SharedLogOptions<T> = {
|
|
|
127
127
|
respondToIHaveTimeout?: number;
|
|
128
128
|
canReplicate?: (publicKey: PublicSignKey) => Promise<boolean> | boolean;
|
|
129
129
|
sync?: (entry: Entry<T>) => boolean;
|
|
130
|
+
timeUntilRoleMaturity?: number;
|
|
130
131
|
};
|
|
131
132
|
|
|
132
133
|
export const DEFAULT_MIN_REPLICAS = 2;
|
|
133
134
|
export const WAIT_FOR_REPLICATOR_TIMEOUT = 9000;
|
|
134
135
|
export const WAIT_FOR_ROLE_MATURITY = 5000;
|
|
135
|
-
const
|
|
136
|
+
const REBALANCE_DEBOUNCE_INTERVAL = 100;
|
|
136
137
|
|
|
137
138
|
export type Args<T> = LogProperties<T> & LogEvents<T> & SharedLogOptions<T>;
|
|
138
139
|
|
|
@@ -205,6 +206,8 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
205
206
|
|
|
206
207
|
replicas: ReplicationLimits;
|
|
207
208
|
|
|
209
|
+
timeUntilRoleMaturity: number;
|
|
210
|
+
|
|
208
211
|
constructor(properties?: { id?: Uint8Array }) {
|
|
209
212
|
super();
|
|
210
213
|
this.log = new Log(properties);
|
|
@@ -223,9 +226,8 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
223
226
|
this.rebalanceParticipationDebounced = debounce(
|
|
224
227
|
() => this.rebalanceParticipation(),
|
|
225
228
|
Math.max(
|
|
226
|
-
|
|
227
|
-
(this.getReplicatorsSorted()?.length || 0) *
|
|
228
|
-
REBALANCE_DEBOUNCE_INTERAVAL
|
|
229
|
+
REBALANCE_DEBOUNCE_INTERVAL,
|
|
230
|
+
(this.getReplicatorsSorted()?.length || 0) * REBALANCE_DEBOUNCE_INTERVAL
|
|
229
231
|
)
|
|
230
232
|
);
|
|
231
233
|
}
|
|
@@ -233,10 +235,13 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
233
235
|
this.rebalanceParticipationDebounced = undefined;
|
|
234
236
|
|
|
235
237
|
const setupDebouncedRebalancing = (options?: AdaptiveReplicatorOptions) => {
|
|
236
|
-
this.replicationController = new PIDReplicationController(
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
238
|
+
this.replicationController = new PIDReplicationController(
|
|
239
|
+
this.node.identity.publicKey.hashcode(),
|
|
240
|
+
{
|
|
241
|
+
targetMemoryLimit: options?.limits?.memory,
|
|
242
|
+
errorFunction: options?.error
|
|
243
|
+
}
|
|
244
|
+
);
|
|
240
245
|
|
|
241
246
|
this.setupRebalanceDebounceFunction();
|
|
242
247
|
};
|
|
@@ -254,7 +259,10 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
254
259
|
setupDebouncedRebalancing(options);
|
|
255
260
|
this._roleOptions = options;
|
|
256
261
|
} else {
|
|
257
|
-
this._roleOptions = new Replicator({
|
|
262
|
+
this._roleOptions = new Replicator({
|
|
263
|
+
factor: options.factor,
|
|
264
|
+
offset: hashToUniformNumber(this.node.identity.publicKey.bytes)
|
|
265
|
+
});
|
|
258
266
|
}
|
|
259
267
|
} else {
|
|
260
268
|
this._roleOptions = new Observer();
|
|
@@ -276,11 +284,13 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
276
284
|
if (this._roleOptions.limits) {
|
|
277
285
|
this._role = new Replicator({
|
|
278
286
|
// initial role in a dynamic setup
|
|
279
|
-
factor: 1
|
|
287
|
+
factor: 1,
|
|
288
|
+
offset: hashToUniformNumber(this.node.identity.publicKey.bytes)
|
|
280
289
|
});
|
|
281
290
|
} else {
|
|
282
291
|
this._role = new Replicator({
|
|
283
|
-
factor: 1
|
|
292
|
+
factor: 1,
|
|
293
|
+
offset: hashToUniformNumber(this.node.identity.publicKey.bytes)
|
|
284
294
|
});
|
|
285
295
|
}
|
|
286
296
|
}
|
|
@@ -392,7 +402,8 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
392
402
|
this._pendingIHave = new Map();
|
|
393
403
|
this.latestRoleMessages = new Map();
|
|
394
404
|
this.openTime = +new Date();
|
|
395
|
-
|
|
405
|
+
this.timeUntilRoleMaturity =
|
|
406
|
+
options?.timeUntilRoleMaturity || WAIT_FOR_ROLE_MATURITY;
|
|
396
407
|
this._gidParentCache = new Cache({ max: 1000 });
|
|
397
408
|
this._closeController = new AbortController();
|
|
398
409
|
|
|
@@ -426,17 +437,6 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
426
437
|
this._sortedPeersCache = yallist.create();
|
|
427
438
|
this._gidPeersHistory = new Map();
|
|
428
439
|
|
|
429
|
-
await this.node.services.pubsub.addEventListener(
|
|
430
|
-
"subscribe",
|
|
431
|
-
this._onSubscriptionFn
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
this._onUnsubscriptionFn = this._onUnsubscription.bind(this);
|
|
435
|
-
await this.node.services.pubsub.addEventListener(
|
|
436
|
-
"unsubscribe",
|
|
437
|
-
this._onUnsubscriptionFn
|
|
438
|
-
);
|
|
439
|
-
|
|
440
440
|
const cache = await storage.sublevel("cache");
|
|
441
441
|
|
|
442
442
|
await this.log.open(this.remoteBlocks, this.node.identity, {
|
|
@@ -496,6 +496,17 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
496
496
|
topic: this.topic
|
|
497
497
|
});
|
|
498
498
|
|
|
499
|
+
await this.node.services.pubsub.addEventListener(
|
|
500
|
+
"subscribe",
|
|
501
|
+
this._onSubscriptionFn
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
this._onUnsubscriptionFn = this._onUnsubscription.bind(this);
|
|
505
|
+
await this.node.services.pubsub.addEventListener(
|
|
506
|
+
"unsubscribe",
|
|
507
|
+
this._onUnsubscriptionFn
|
|
508
|
+
);
|
|
509
|
+
|
|
499
510
|
await this.log.load();
|
|
500
511
|
}
|
|
501
512
|
|
|
@@ -769,7 +780,6 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
769
780
|
}
|
|
770
781
|
|
|
771
782
|
prevPendingIHave && prevPendingIHave.callback(entry);
|
|
772
|
-
|
|
773
783
|
this._pendingIHave.delete(entry.hash);
|
|
774
784
|
}
|
|
775
785
|
};
|
|
@@ -819,8 +829,6 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
819
829
|
timeout: WAIT_FOR_REPLICATOR_TIMEOUT
|
|
820
830
|
})
|
|
821
831
|
.then(async () => {
|
|
822
|
-
/* await delay(1000 * Math.random()) */
|
|
823
|
-
|
|
824
832
|
const prev = this.latestRoleMessages.get(context.from!.hashcode());
|
|
825
833
|
if (prev && prev > context.timestamp) {
|
|
826
834
|
return;
|
|
@@ -997,7 +1005,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
997
1005
|
const startNode = currentNode;
|
|
998
1006
|
const diffs: { diff: number; rect: ReplicatorRect }[] = [];
|
|
999
1007
|
while (currentNode && !done()) {
|
|
1000
|
-
const start = currentNode.value.offset % width;
|
|
1008
|
+
const start = currentNode.value.role.offset % width;
|
|
1001
1009
|
const absDelta = Math.abs(start - point());
|
|
1002
1010
|
const diff = Math.min(absDelta, width - absDelta);
|
|
1003
1011
|
|
|
@@ -1056,7 +1064,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1056
1064
|
const t = +new Date();
|
|
1057
1065
|
const roleAge =
|
|
1058
1066
|
options?.roleAge ??
|
|
1059
|
-
Math.min(
|
|
1067
|
+
Math.min(this.timeUntilRoleMaturity, +new Date() - this.openTime);
|
|
1060
1068
|
|
|
1061
1069
|
for (let i = 0; i < numberOfLeaders; i++) {
|
|
1062
1070
|
const point = ((cursor + i / numberOfLeaders) % 1) * width;
|
|
@@ -1079,7 +1087,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1079
1087
|
*
|
|
1080
1088
|
* @returns groups where at least one in any group will have the entry you are looking for
|
|
1081
1089
|
*/
|
|
1082
|
-
getReplicatorUnion(roleAge: number =
|
|
1090
|
+
getReplicatorUnion(roleAge: number = this.timeUntilRoleMaturity) {
|
|
1083
1091
|
if (this.closed === true) {
|
|
1084
1092
|
throw new Error("Closed");
|
|
1085
1093
|
}
|
|
@@ -1127,7 +1135,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1127
1135
|
return [];
|
|
1128
1136
|
}
|
|
1129
1137
|
|
|
1130
|
-
let nextPoint = startNode.value.offset;
|
|
1138
|
+
let nextPoint = startNode.value.role.offset;
|
|
1131
1139
|
const t = +new Date();
|
|
1132
1140
|
this.collectNodesAroundPoint(
|
|
1133
1141
|
t,
|
|
@@ -1173,7 +1181,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1173
1181
|
this._closeController.signal.removeEventListener("abort", listener);
|
|
1174
1182
|
await this.rebalanceParticipationDebounced?.();
|
|
1175
1183
|
this.distribute();
|
|
1176
|
-
},
|
|
1184
|
+
}, this.timeUntilRoleMaturity + 2000);
|
|
1177
1185
|
|
|
1178
1186
|
const listener = () => {
|
|
1179
1187
|
clearTimeout(timer);
|
|
@@ -1194,14 +1202,13 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1194
1202
|
publicKey: PublicSignKey
|
|
1195
1203
|
) {
|
|
1196
1204
|
const update = await this._modifyReplicators(role, publicKey);
|
|
1205
|
+
|
|
1197
1206
|
if (update.changed !== "none") {
|
|
1198
1207
|
if (update.changed === "added" || update.changed === "removed") {
|
|
1199
1208
|
this.setupRebalanceDebounceFunction();
|
|
1200
1209
|
}
|
|
1201
1210
|
|
|
1202
|
-
|
|
1203
|
-
await this.rebalanceParticipationDebounced?.(); /* await this.rebalanceParticipation(false); */
|
|
1204
|
-
}
|
|
1211
|
+
await this.rebalanceParticipationDebounced?.(); /* await this.rebalanceParticipation(false); */
|
|
1205
1212
|
if (update.changed === "added") {
|
|
1206
1213
|
await this.rpc.send(new ResponseRoleMessage({ role: this._role }), {
|
|
1207
1214
|
mode: new SeekDelivery({
|
|
@@ -1251,10 +1258,8 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1251
1258
|
|
|
1252
1259
|
if (isOnline) {
|
|
1253
1260
|
// insert or if already there do nothing
|
|
1254
|
-
const code = hashToUniformNumber(publicKey.bytes);
|
|
1255
1261
|
const rect: ReplicatorRect = {
|
|
1256
1262
|
publicKey,
|
|
1257
|
-
offset: code,
|
|
1258
1263
|
role
|
|
1259
1264
|
};
|
|
1260
1265
|
|
|
@@ -1276,7 +1281,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1276
1281
|
return { prev: prev.role, changed: "updated" };
|
|
1277
1282
|
}
|
|
1278
1283
|
|
|
1279
|
-
if (
|
|
1284
|
+
if (role.offset > currentNode.value.role.offset) {
|
|
1280
1285
|
const next = currentNode?.next;
|
|
1281
1286
|
if (next) {
|
|
1282
1287
|
currentNode = next;
|
|
@@ -1483,7 +1488,7 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1483
1488
|
_queue: PQueue;
|
|
1484
1489
|
async distribute() {
|
|
1485
1490
|
if (this._queue?.size > 0) {
|
|
1486
|
-
return;
|
|
1491
|
+
return this._queue.onEmpty();
|
|
1487
1492
|
}
|
|
1488
1493
|
(this._queue || (this._queue = new PQueue({ concurrency: 1 }))).add(() =>
|
|
1489
1494
|
this._distribute()
|
|
@@ -1659,10 +1664,14 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1659
1664
|
);
|
|
1660
1665
|
}
|
|
1661
1666
|
|
|
1667
|
+
xxx: number;
|
|
1662
1668
|
async rebalanceParticipation(onRoleChange = true) {
|
|
1663
1669
|
// update more participation rate to converge to the average expected rate or bounded by
|
|
1664
1670
|
// resources such as memory and or cpu
|
|
1665
1671
|
|
|
1672
|
+
const t = +new Date();
|
|
1673
|
+
// console.log(t - this.xxx)
|
|
1674
|
+
this.xxx = t;
|
|
1666
1675
|
if (this.closed) {
|
|
1667
1676
|
return false;
|
|
1668
1677
|
}
|
|
@@ -1688,15 +1697,16 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1688
1697
|
peers?.length || 1
|
|
1689
1698
|
);
|
|
1690
1699
|
|
|
1691
|
-
const newRole = new Replicator({
|
|
1692
|
-
factor: newFactor,
|
|
1693
|
-
timestamp: this._role.timestamp
|
|
1694
|
-
});
|
|
1695
|
-
|
|
1696
1700
|
const relativeDifference =
|
|
1697
|
-
Math.abs(this._role.factor -
|
|
1701
|
+
Math.abs(this._role.factor - newFactor) / this._role.factor;
|
|
1698
1702
|
|
|
1699
1703
|
if (relativeDifference > 0.0001) {
|
|
1704
|
+
const newRole = new Replicator({
|
|
1705
|
+
factor: newFactor,
|
|
1706
|
+
timestamp: this._role.timestamp,
|
|
1707
|
+
offset: hashToUniformNumber(this.node.identity.publicKey.bytes)
|
|
1708
|
+
});
|
|
1709
|
+
|
|
1700
1710
|
const canReplicate =
|
|
1701
1711
|
!this._canReplicate ||
|
|
1702
1712
|
(await this._canReplicate(this.node.identity.publicKey, newRole));
|
|
@@ -1705,7 +1715,11 @@ export class SharedLog<T = Uint8Array> extends Program<
|
|
|
1705
1715
|
}
|
|
1706
1716
|
|
|
1707
1717
|
await this._updateRole(newRole, onRoleChange);
|
|
1718
|
+
this.rebalanceParticipationDebounced?.();
|
|
1719
|
+
|
|
1708
1720
|
return true;
|
|
1721
|
+
} else {
|
|
1722
|
+
this.rebalanceParticipationDebounced?.();
|
|
1709
1723
|
}
|
|
1710
1724
|
return false;
|
|
1711
1725
|
}
|
package/src/pid.ts
CHANGED
|
@@ -8,13 +8,14 @@ export class PIDReplicationController {
|
|
|
8
8
|
integral: number;
|
|
9
9
|
prevError: number;
|
|
10
10
|
prevMemoryUsage: number;
|
|
11
|
-
|
|
11
|
+
prevTotalFactor: number;
|
|
12
12
|
kp: number;
|
|
13
13
|
ki: number;
|
|
14
14
|
kd: number;
|
|
15
15
|
errorFunction: ReplicationErrorFunction;
|
|
16
16
|
targetMemoryLimit?: number;
|
|
17
17
|
constructor(
|
|
18
|
+
readonly id: string,
|
|
18
19
|
options: {
|
|
19
20
|
errorFunction?: ReplicationErrorFunction;
|
|
20
21
|
targetMemoryLimit?: number;
|
|
@@ -25,11 +26,14 @@ export class PIDReplicationController {
|
|
|
25
26
|
) {
|
|
26
27
|
const {
|
|
27
28
|
targetMemoryLimit,
|
|
28
|
-
kp = 0.
|
|
29
|
-
ki = 0.
|
|
30
|
-
kd = 0.
|
|
31
|
-
errorFunction = ({ balance, coverage, memory }) =>
|
|
32
|
-
memory
|
|
29
|
+
kp = 0.7,
|
|
30
|
+
ki = 0.025,
|
|
31
|
+
kd = 0.05,
|
|
32
|
+
errorFunction = ({ balance, coverage, memory }) => {
|
|
33
|
+
return memory < 0
|
|
34
|
+
? memory * 0.9 + balance * 0.06 + coverage * 0.04
|
|
35
|
+
: balance * 0.6 + coverage * 0.4;
|
|
36
|
+
}
|
|
33
37
|
} = options;
|
|
34
38
|
this.reset();
|
|
35
39
|
this.kp = kp;
|
|
@@ -39,18 +43,27 @@ export class PIDReplicationController {
|
|
|
39
43
|
this.errorFunction = errorFunction;
|
|
40
44
|
}
|
|
41
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Call this function on a period interval since it does not track time passed
|
|
48
|
+
* @param memoryUsage
|
|
49
|
+
* @param currentFactor
|
|
50
|
+
* @param totalFactor
|
|
51
|
+
* @param peerCount
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
42
54
|
async adjustReplicationFactor(
|
|
43
55
|
memoryUsage: number,
|
|
44
56
|
currentFactor: number,
|
|
45
57
|
totalFactor: number,
|
|
46
58
|
peerCount: number
|
|
47
59
|
) {
|
|
60
|
+
const totalFactorDiff = totalFactor - this.prevTotalFactor;
|
|
61
|
+
this.prevTotalFactor = totalFactor;
|
|
48
62
|
this.prevMemoryUsage = memoryUsage;
|
|
49
63
|
|
|
50
64
|
const estimatedTotalSize = memoryUsage / currentFactor;
|
|
51
65
|
|
|
52
66
|
let errorMemory = 0;
|
|
53
|
-
const errorTarget = 1 / peerCount - currentFactor;
|
|
54
67
|
|
|
55
68
|
if (this.targetMemoryLimit != null) {
|
|
56
69
|
errorMemory =
|
|
@@ -59,42 +72,39 @@ export class PIDReplicationController {
|
|
|
59
72
|
Math.min(1, this.targetMemoryLimit / estimatedTotalSize),
|
|
60
73
|
0
|
|
61
74
|
) - currentFactor
|
|
62
|
-
: 0
|
|
75
|
+
: 0;
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
const errorCoverageUnmodified = Math.min(1 - totalFactor, 1);
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
79
|
+
const errorCoverage =
|
|
80
|
+
(this.targetMemoryLimit ? 1 - Math.sqrt(Math.abs(errorMemory)) : 1) *
|
|
81
|
+
errorCoverageUnmodified;
|
|
69
82
|
|
|
70
|
-
|
|
71
|
-
|
|
83
|
+
const errorFromEven = 1 / peerCount - currentFactor;
|
|
84
|
+
|
|
85
|
+
const balanceErrorScaler = this.targetMemoryLimit
|
|
86
|
+
? Math.abs(errorMemory)
|
|
87
|
+
: 1 - Math.abs(errorCoverage);
|
|
88
|
+
|
|
89
|
+
const errorBalance = (this.targetMemoryLimit ? errorMemory > -0.01 : true)
|
|
90
|
+
? errorFromEven > 0
|
|
91
|
+
? balanceErrorScaler * errorFromEven
|
|
92
|
+
: 0
|
|
93
|
+
: 0;
|
|
94
|
+
|
|
95
|
+
const totalError = this.errorFunction({
|
|
96
|
+
balance: errorBalance,
|
|
72
97
|
coverage: errorCoverage,
|
|
73
98
|
memory: errorMemory
|
|
74
99
|
});
|
|
75
100
|
|
|
76
|
-
|
|
77
|
-
totalError = this.errorFunction({
|
|
78
|
-
balance: errorTarget,
|
|
79
|
-
coverage: errorCoverageUnmodified,
|
|
80
|
-
memory: errorMemory
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (this.lastTs === 0) {
|
|
85
|
-
this.lastTs = +new Date();
|
|
86
|
-
}
|
|
87
|
-
const kpAdjusted = Math.min(
|
|
88
|
-
Math.max(this.kp, (+new Date() - this.lastTs) / 100),
|
|
89
|
-
0.8
|
|
90
|
-
);
|
|
91
|
-
const pTerm = kpAdjusted * totalError;
|
|
92
|
-
|
|
93
|
-
this.lastTs = +new Date();
|
|
101
|
+
const pTerm = this.kp * totalError;
|
|
94
102
|
|
|
95
103
|
// Integral term
|
|
96
104
|
this.integral += totalError;
|
|
97
|
-
|
|
105
|
+
|
|
106
|
+
// Beta controls how much of the accumulated error we should forget
|
|
107
|
+
const beta = 0.8;
|
|
98
108
|
this.integral = beta * totalError + (1 - beta) * this.integral;
|
|
99
109
|
|
|
100
110
|
const iTerm = this.ki * this.integral;
|
|
@@ -114,19 +124,30 @@ export class PIDReplicationController {
|
|
|
114
124
|
this.integral = 0;
|
|
115
125
|
}
|
|
116
126
|
|
|
127
|
+
// prevent drift when everone wants to do less
|
|
128
|
+
/* if (newFactor < currentFactor && totalFactorDiff < 0 && totalFactor < 0.5) {
|
|
129
|
+
newFactor = currentFactor;
|
|
130
|
+
this.integral = 0;
|
|
131
|
+
}
|
|
132
|
+
*/
|
|
133
|
+
|
|
117
134
|
/* console.log({
|
|
118
|
-
|
|
135
|
+
id: this.id,
|
|
119
136
|
currentFactor,
|
|
137
|
+
newFactor,
|
|
138
|
+
factorDiff: newFactor - currentFactor,
|
|
120
139
|
pTerm,
|
|
121
140
|
dTerm,
|
|
122
141
|
iTerm,
|
|
123
|
-
kpAdjusted,
|
|
124
142
|
totalError,
|
|
125
|
-
errorTarget,
|
|
143
|
+
errorTarget: errorBalance,
|
|
126
144
|
errorCoverage,
|
|
127
145
|
errorMemory,
|
|
128
146
|
peerCount,
|
|
129
|
-
totalFactor
|
|
147
|
+
totalFactor,
|
|
148
|
+
totalFactorDiff,
|
|
149
|
+
targetScaler: balanceErrorScaler,
|
|
150
|
+
estimatedTotalSize
|
|
130
151
|
}); */
|
|
131
152
|
|
|
132
153
|
return Math.max(Math.min(newFactor, 1), 0);
|
|
@@ -136,6 +157,5 @@ export class PIDReplicationController {
|
|
|
136
157
|
this.prevError = 0;
|
|
137
158
|
this.integral = 0;
|
|
138
159
|
this.prevMemoryUsage = 0;
|
|
139
|
-
this.lastTs = 0;
|
|
140
160
|
}
|
|
141
161
|
}
|