@peerbit/shared-log 4.0.5 → 4.0.7
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/exchange-heads.d.ts +2 -2
- package/lib/esm/exchange-heads.js +12 -10
- package/lib/esm/exchange-heads.js.map +1 -1
- package/lib/esm/index.d.ts +8 -3
- package/lib/esm/index.js +207 -139
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/pid.d.ts +1 -0
- package/lib/esm/pid.js +43 -12
- package/lib/esm/pid.js.map +1 -1
- package/lib/esm/replication.d.ts +3 -1
- package/lib/esm/replication.js +2 -2
- package/lib/esm/replication.js.map +1 -1
- package/lib/esm/role.d.ts +4 -0
- package/lib/esm/role.js +18 -6
- package/lib/esm/role.js.map +1 -1
- package/package.json +7 -7
- package/src/exchange-heads.ts +4 -2
- package/src/index.ts +295 -187
- package/src/pid.ts +51 -14
- package/src/replication.ts +2 -3
- package/src/role.ts +21 -3
package/src/pid.ts
CHANGED
|
@@ -5,10 +5,10 @@ export type ReplicationErrorFunction = (objectives: {
|
|
|
5
5
|
}) => number;
|
|
6
6
|
|
|
7
7
|
export class PIDReplicationController {
|
|
8
|
-
integral
|
|
9
|
-
prevError
|
|
10
|
-
prevMemoryUsage
|
|
11
|
-
lastTs
|
|
8
|
+
integral: number;
|
|
9
|
+
prevError: number;
|
|
10
|
+
prevMemoryUsage: number;
|
|
11
|
+
lastTs: number;
|
|
12
12
|
kp: number;
|
|
13
13
|
ki: number;
|
|
14
14
|
kd: number;
|
|
@@ -25,12 +25,13 @@ export class PIDReplicationController {
|
|
|
25
25
|
) {
|
|
26
26
|
const {
|
|
27
27
|
targetMemoryLimit,
|
|
28
|
-
kp = 0.
|
|
29
|
-
ki = 0
|
|
30
|
-
kd = 0.
|
|
28
|
+
kp = 0.5,
|
|
29
|
+
ki = 0.1,
|
|
30
|
+
kd = 0.25,
|
|
31
31
|
errorFunction = ({ balance, coverage, memory }) =>
|
|
32
32
|
memory * 0.8 + balance * 0.1 + coverage * 0.1
|
|
33
33
|
} = options;
|
|
34
|
+
this.reset();
|
|
34
35
|
this.kp = kp;
|
|
35
36
|
this.ki = ki;
|
|
36
37
|
this.kd = kd;
|
|
@@ -45,12 +46,8 @@ export class PIDReplicationController {
|
|
|
45
46
|
peerCount: number
|
|
46
47
|
) {
|
|
47
48
|
this.prevMemoryUsage = memoryUsage;
|
|
48
|
-
if (memoryUsage <= 0) {
|
|
49
|
-
this.integral = 0;
|
|
50
|
-
}
|
|
51
49
|
|
|
52
50
|
const estimatedTotalSize = memoryUsage / currentFactor;
|
|
53
|
-
const errorCoverage = Math.min(1 - totalFactor, 1);
|
|
54
51
|
|
|
55
52
|
let errorMemory = 0;
|
|
56
53
|
const errorTarget = 1 / peerCount - currentFactor;
|
|
@@ -65,12 +62,25 @@ export class PIDReplicationController {
|
|
|
65
62
|
: 0.0001;
|
|
66
63
|
}
|
|
67
64
|
|
|
68
|
-
const
|
|
65
|
+
const errorCoverageUnmodified = Math.min(1 - totalFactor, 1);
|
|
66
|
+
const includeCoverageError =
|
|
67
|
+
Math.max(Math.abs(errorTarget), Math.abs(errorMemory)) < 0.01;
|
|
68
|
+
const errorCoverage = includeCoverageError ? errorCoverageUnmodified : 0; /// 1 / (Math.max(Math.abs(errorTarget), Math.abs(errorMemory))) * errorCoverage / 100;
|
|
69
|
+
|
|
70
|
+
let totalError = this.errorFunction({
|
|
69
71
|
balance: errorTarget,
|
|
70
72
|
coverage: errorCoverage,
|
|
71
73
|
memory: errorMemory
|
|
72
74
|
});
|
|
73
75
|
|
|
76
|
+
if (totalError === 0 && !includeCoverageError) {
|
|
77
|
+
totalError = this.errorFunction({
|
|
78
|
+
balance: errorTarget,
|
|
79
|
+
coverage: errorCoverageUnmodified,
|
|
80
|
+
memory: errorMemory
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
if (this.lastTs === 0) {
|
|
75
85
|
this.lastTs = +new Date();
|
|
76
86
|
}
|
|
@@ -84,7 +94,7 @@ export class PIDReplicationController {
|
|
|
84
94
|
|
|
85
95
|
// Integral term
|
|
86
96
|
this.integral += totalError;
|
|
87
|
-
const beta = 0.
|
|
97
|
+
const beta = 0.5;
|
|
88
98
|
this.integral = beta * totalError + (1 - beta) * this.integral;
|
|
89
99
|
|
|
90
100
|
const iTerm = this.ki * this.integral;
|
|
@@ -94,11 +104,38 @@ export class PIDReplicationController {
|
|
|
94
104
|
const dTerm = this.kd * derivative;
|
|
95
105
|
|
|
96
106
|
// Calculate the new replication factor
|
|
97
|
-
const
|
|
107
|
+
const change = pTerm + iTerm + dTerm;
|
|
108
|
+
const newFactor = currentFactor + change;
|
|
98
109
|
|
|
99
110
|
// Update state for the next iteration
|
|
100
111
|
this.prevError = totalError;
|
|
101
112
|
|
|
113
|
+
if (newFactor < 0 || newFactor > 1) {
|
|
114
|
+
this.integral = 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* console.log({
|
|
118
|
+
newFactor,
|
|
119
|
+
currentFactor,
|
|
120
|
+
pTerm,
|
|
121
|
+
dTerm,
|
|
122
|
+
iTerm,
|
|
123
|
+
kpAdjusted,
|
|
124
|
+
totalError,
|
|
125
|
+
errorTarget,
|
|
126
|
+
errorCoverage,
|
|
127
|
+
errorMemory,
|
|
128
|
+
peerCount,
|
|
129
|
+
totalFactor
|
|
130
|
+
}); */
|
|
131
|
+
|
|
102
132
|
return Math.max(Math.min(newFactor, 1), 0);
|
|
103
133
|
}
|
|
134
|
+
|
|
135
|
+
reset() {
|
|
136
|
+
this.prevError = 0;
|
|
137
|
+
this.integral = 0;
|
|
138
|
+
this.prevMemoryUsage = 0;
|
|
139
|
+
this.lastTs = 0;
|
|
140
|
+
}
|
|
104
141
|
}
|
package/src/replication.ts
CHANGED
|
@@ -56,10 +56,9 @@ export class ResponseRoleMessage extends TransportMessage {
|
|
|
56
56
|
@field({ type: option(Role) })
|
|
57
57
|
role: Observer | Replicator;
|
|
58
58
|
|
|
59
|
-
constructor(role: Observer | Replicator) {
|
|
59
|
+
constructor(properties: { role: Observer | Replicator }) {
|
|
60
60
|
super();
|
|
61
|
-
|
|
62
|
-
this.role = role;
|
|
61
|
+
this.role = properties.role;
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
|
package/src/role.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { field, option, variant, vec } from "@dao-xyz/borsh";
|
|
2
2
|
|
|
3
|
-
export abstract class Role {
|
|
3
|
+
export abstract class Role {
|
|
4
|
+
abstract equals(other: Role);
|
|
5
|
+
}
|
|
4
6
|
|
|
5
7
|
export const NO_TYPE_VARIANT = new Uint8Array([0]);
|
|
6
8
|
|
|
7
9
|
@variant(0)
|
|
8
|
-
export class NoType extends Role {
|
|
10
|
+
export class NoType extends Role {
|
|
11
|
+
equals(other: Role) {
|
|
12
|
+
return other instanceof NoType;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
9
15
|
|
|
10
16
|
export const OBSERVER_TYPE_VARIANT = new Uint8Array([1]);
|
|
11
17
|
|
|
12
18
|
@variant(1)
|
|
13
|
-
export class Observer extends Role {
|
|
19
|
+
export class Observer extends Role {
|
|
20
|
+
equals(other: Role) {
|
|
21
|
+
return other instanceof Observer;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
14
24
|
|
|
15
25
|
export const REPLICATOR_TYPE_VARIANT = new Uint8Array([2]);
|
|
16
26
|
|
|
@@ -84,4 +94,12 @@ export class Replicator extends Role {
|
|
|
84
94
|
get timestamp(): bigint {
|
|
85
95
|
return this.segments[0]!.timestamp;
|
|
86
96
|
}
|
|
97
|
+
|
|
98
|
+
equals(other: Role) {
|
|
99
|
+
return (
|
|
100
|
+
other instanceof Replicator &&
|
|
101
|
+
other.factor === this.factor &&
|
|
102
|
+
other.offset === this.offset
|
|
103
|
+
);
|
|
104
|
+
}
|
|
87
105
|
}
|