@optimystic/db-p2p 0.1.1 → 0.1.3
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/{readme.md → README.md} +7 -0
- package/dist/index.min.js +31 -30
- package/dist/index.min.js.map +4 -4
- package/dist/src/cluster/cluster-repo.d.ts +27 -0
- package/dist/src/cluster/cluster-repo.d.ts.map +1 -1
- package/dist/src/cluster/cluster-repo.js +139 -18
- package/dist/src/cluster/cluster-repo.js.map +1 -1
- package/dist/src/cluster/service.d.ts +13 -2
- package/dist/src/cluster/service.d.ts.map +1 -1
- package/dist/src/cluster/service.js +17 -7
- package/dist/src/cluster/service.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p-node.d.ts +13 -2
- package/dist/src/libp2p-node.d.ts.map +1 -1
- package/dist/src/libp2p-node.js +35 -16
- package/dist/src/libp2p-node.js.map +1 -1
- package/dist/src/protocol-client.d.ts.map +1 -1
- package/dist/src/protocol-client.js +8 -7
- package/dist/src/protocol-client.js.map +1 -1
- package/dist/src/repo/cluster-coordinator.d.ts +7 -2
- package/dist/src/repo/cluster-coordinator.d.ts.map +1 -1
- package/dist/src/repo/cluster-coordinator.js +18 -3
- package/dist/src/repo/cluster-coordinator.js.map +1 -1
- package/dist/src/repo/coordinator-repo.d.ts +26 -3
- package/dist/src/repo/coordinator-repo.d.ts.map +1 -1
- package/dist/src/repo/coordinator-repo.js +117 -22
- package/dist/src/repo/coordinator-repo.js.map +1 -1
- package/dist/src/repo/service.d.ts +13 -2
- package/dist/src/repo/service.d.ts.map +1 -1
- package/dist/src/repo/service.js +25 -12
- package/dist/src/repo/service.js.map +1 -1
- package/dist/src/storage/memory-storage.d.ts +15 -0
- package/dist/src/storage/memory-storage.d.ts.map +1 -1
- package/dist/src/storage/memory-storage.js +23 -4
- package/dist/src/storage/memory-storage.js.map +1 -1
- package/dist/src/storage/storage-repo.d.ts.map +1 -1
- package/dist/src/storage/storage-repo.js.map +1 -1
- package/dist/src/sync/service.d.ts.map +1 -1
- package/dist/src/sync/service.js +7 -2
- package/dist/src/sync/service.js.map +1 -1
- package/package.json +27 -21
- package/src/cluster/cluster-repo.ts +836 -711
- package/src/cluster/service.ts +44 -31
- package/src/index.ts +1 -1
- package/src/libp2p-key-network.ts +334 -334
- package/src/libp2p-node.ts +371 -339
- package/src/network/network-manager-service.ts +334 -334
- package/src/protocol-client.ts +53 -54
- package/src/repo/client.ts +112 -112
- package/src/repo/cluster-coordinator.ts +613 -592
- package/src/repo/coordinator-repo.ts +269 -137
- package/src/repo/service.ts +237 -219
- package/src/storage/block-storage.ts +182 -182
- package/src/storage/memory-storage.ts +24 -5
- package/src/storage/storage-repo.ts +321 -320
- package/src/sync/service.ts +7 -6
- package/dist/src/storage/file-storage.d.ts +0 -30
- package/dist/src/storage/file-storage.d.ts.map +0 -1
- package/dist/src/storage/file-storage.js +0 -127
- package/dist/src/storage/file-storage.js.map +0 -1
- package/src/storage/file-storage.ts +0 -163
package/src/cluster/service.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import { pipe } from 'it-pipe';
|
|
2
2
|
import { decode as lpDecode, encode as lpEncode } from 'it-length-prefixed';
|
|
3
|
-
import type { Startable, Logger,
|
|
3
|
+
import type { Startable, Logger, Stream, Connection, StreamHandler } from '@libp2p/interface';
|
|
4
4
|
import type { ICluster, ClusterRecord } from '@optimystic/db-core';
|
|
5
|
-
import { computeResponsibility } from '../routing/responsibility.js'
|
|
6
|
-
import { peersEqual } from '../peer-utils.js'
|
|
7
|
-
import { buildKnownPeers } from '../routing/libp2p-known-peers.js'
|
|
8
|
-
import { encodePeers } from '../repo/redirect.js'
|
|
9
5
|
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
10
6
|
|
|
11
7
|
interface BaseComponents {
|
|
12
8
|
logger: { forComponent: (name: string) => Logger },
|
|
13
9
|
registrar: {
|
|
14
|
-
handle: (protocol: string, handler:
|
|
10
|
+
handle: (protocol: string, handler: StreamHandler, options: any) => Promise<void>,
|
|
15
11
|
unhandle: (protocol: string) => Promise<void>
|
|
16
12
|
}
|
|
17
13
|
}
|
|
@@ -26,10 +22,19 @@ export interface ClusterServiceInit {
|
|
|
26
22
|
maxInboundStreams?: number,
|
|
27
23
|
maxOutboundStreams?: number,
|
|
28
24
|
logPrefix?: string,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
kBucketSize?: number,
|
|
26
|
+
configuredClusterSize?: number,
|
|
27
|
+
allowClusterDownsize?: boolean,
|
|
28
|
+
clusterSizeTolerance?: number,
|
|
29
|
+
/**
|
|
30
|
+
* Responsibility K - the replica set size for determining cluster membership.
|
|
31
|
+
* This is distinct from kBucketSize (DHT routing) and configuredClusterSize (consensus quorum).
|
|
32
|
+
* When set, this determines how many peers (by XOR distance) are considered
|
|
33
|
+
* responsible for a key. If this node is not in the top responsibilityK peers,
|
|
34
|
+
* it will redirect requests to closer peers.
|
|
35
|
+
* Default: 1 (only the closest peer handles requests)
|
|
36
|
+
*/
|
|
37
|
+
responsibilityK?: number,
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
export function clusterService(init: ClusterServiceInit = {}): (components: ClusterServiceComponents) => ClusterService {
|
|
@@ -47,10 +52,12 @@ export class ClusterService implements Startable {
|
|
|
47
52
|
private readonly cluster: ICluster;
|
|
48
53
|
private readonly components: ClusterServiceComponents;
|
|
49
54
|
private running: boolean;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
private readonly k: number;
|
|
56
|
+
private readonly configuredClusterSize: number;
|
|
57
|
+
private readonly allowDownsize: boolean;
|
|
58
|
+
private readonly sizeTolerance: number;
|
|
59
|
+
/** Responsibility K - how many peers are responsible for a key (for redirect decisions) */
|
|
60
|
+
private readonly responsibilityK: number;
|
|
54
61
|
|
|
55
62
|
constructor(components: ClusterServiceComponents, init: ClusterServiceInit = {}) {
|
|
56
63
|
this.components = components;
|
|
@@ -60,10 +67,11 @@ export class ClusterService implements Startable {
|
|
|
60
67
|
this.log = components.logger.forComponent(init.logPrefix ?? 'db-p2p:cluster');
|
|
61
68
|
this.cluster = components.cluster;
|
|
62
69
|
this.running = false;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
this.k = init.kBucketSize ?? 10;
|
|
71
|
+
this.configuredClusterSize = init.configuredClusterSize ?? 10;
|
|
72
|
+
this.allowDownsize = init.allowClusterDownsize ?? true;
|
|
73
|
+
this.sizeTolerance = init.clusterSizeTolerance ?? 0.5;
|
|
74
|
+
this.responsibilityK = init.responsibilityK ?? 1;
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
readonly [Symbol.toStringTag] = '@libp2p/cluster';
|
|
@@ -90,8 +98,7 @@ export class ClusterService implements Startable {
|
|
|
90
98
|
this.running = false;
|
|
91
99
|
}
|
|
92
100
|
|
|
93
|
-
private handleIncomingStream(
|
|
94
|
-
const { stream, connection } = data;
|
|
101
|
+
private handleIncomingStream(stream: Stream, connection: Connection): void {
|
|
95
102
|
const peerId = connection.remotePeer;
|
|
96
103
|
|
|
97
104
|
const processStream = async function* (this: ClusterService, source: AsyncIterable<Uint8ArrayList>) {
|
|
@@ -141,16 +148,22 @@ export class ClusterService implements Startable {
|
|
|
141
148
|
}
|
|
142
149
|
};
|
|
143
150
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
151
|
+
void (async () => {
|
|
152
|
+
try {
|
|
153
|
+
const responses = pipe(
|
|
154
|
+
stream,
|
|
155
|
+
(source) => lpDecode(source),
|
|
156
|
+
processStream.bind(this),
|
|
157
|
+
(source) => lpEncode(source)
|
|
158
|
+
);
|
|
159
|
+
for await (const chunk of responses) {
|
|
160
|
+
stream.send(chunk);
|
|
161
|
+
}
|
|
162
|
+
await stream.close();
|
|
163
|
+
} catch (err) {
|
|
164
|
+
this.log.error('error handling cluster protocol message from %p - %e', peerId, err);
|
|
165
|
+
stream.abort(err instanceof Error ? err : new Error(String(err)));
|
|
166
|
+
}
|
|
167
|
+
})();
|
|
155
168
|
}
|
|
156
169
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,10 +7,10 @@ export * from "./repo/cluster-coordinator.js";
|
|
|
7
7
|
export * from "./repo/coordinator-repo.js";
|
|
8
8
|
export * from "./repo/service.js";
|
|
9
9
|
export * from "./storage/block-storage.js";
|
|
10
|
-
export * from "./storage/file-storage.js";
|
|
11
10
|
export * from "./storage/memory-storage.js";
|
|
12
11
|
export * from "./storage/i-block-storage.js";
|
|
13
12
|
export * from "./storage/i-raw-storage.js";
|
|
13
|
+
export * from "./storage/struct.js";
|
|
14
14
|
export * from "./storage/storage-repo.js";
|
|
15
15
|
export * from "./storage/restoration-coordinator-v2.js";
|
|
16
16
|
export * from "./storage/ring-selector.js";
|