@soulcraft/brainy 2.15.0 → 3.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/CHANGELOG.md +18 -0
- package/README.md +249 -152
- package/dist/api/ConfigAPI.d.ts +67 -0
- package/dist/api/ConfigAPI.js +166 -0
- package/dist/api/DataAPI.d.ts +123 -0
- package/dist/api/DataAPI.js +391 -0
- package/dist/api/SecurityAPI.d.ts +50 -0
- package/dist/api/SecurityAPI.js +139 -0
- package/dist/api/UniversalImportAPI.d.ts +134 -0
- package/dist/api/UniversalImportAPI.js +615 -0
- package/dist/augmentationManager.js +12 -7
- package/dist/augmentationPipeline.d.ts +0 -61
- package/dist/augmentationPipeline.js +0 -87
- package/dist/augmentationRegistry.d.ts +1 -1
- package/dist/augmentationRegistry.js +1 -1
- package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
- package/dist/augmentations/apiServerAugmentation.js +288 -7
- package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
- package/dist/augmentations/auditLogAugmentation.js +358 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
- package/dist/augmentations/batchProcessingAugmentation.js +123 -22
- package/dist/augmentations/brainyAugmentation.d.ts +87 -8
- package/dist/augmentations/brainyAugmentation.js +159 -2
- package/dist/augmentations/cacheAugmentation.d.ts +6 -5
- package/dist/augmentations/cacheAugmentation.js +113 -17
- package/dist/augmentations/conduitAugmentations.d.ts +2 -2
- package/dist/augmentations/conduitAugmentations.js +2 -2
- package/dist/augmentations/configResolver.d.ts +122 -0
- package/dist/augmentations/configResolver.js +440 -0
- package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
- package/dist/augmentations/connectionPoolAugmentation.js +37 -12
- package/dist/augmentations/defaultAugmentations.d.ts +9 -11
- package/dist/augmentations/defaultAugmentations.js +4 -11
- package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
- package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
- package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
- package/dist/augmentations/discovery/localDiscovery.js +246 -0
- package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
- package/dist/augmentations/discovery/runtimeLoader.js +337 -0
- package/dist/augmentations/discovery.d.ts +152 -0
- package/dist/augmentations/discovery.js +441 -0
- package/dist/augmentations/display/intelligentComputation.d.ts +1 -1
- package/dist/augmentations/display/intelligentComputation.js +4 -4
- package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
- package/dist/augmentations/entityRegistryAugmentation.js +5 -1
- package/dist/augmentations/indexAugmentation.d.ts +3 -3
- package/dist/augmentations/indexAugmentation.js +2 -2
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +22 -6
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +106 -23
- package/dist/augmentations/manifest.d.ts +176 -0
- package/dist/augmentations/manifest.js +8 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
- package/dist/augmentations/marketplace/cli.d.ts +47 -0
- package/dist/augmentations/marketplace/cli.js +265 -0
- package/dist/augmentations/metricsAugmentation.d.ts +3 -3
- package/dist/augmentations/metricsAugmentation.js +2 -2
- package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
- package/dist/augmentations/monitoringAugmentation.js +2 -2
- package/dist/augmentations/neuralImport.d.ts +1 -1
- package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
- package/dist/augmentations/rateLimitAugmentation.js +321 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
- package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
- package/dist/augmentations/storageAugmentation.d.ts +1 -1
- package/dist/augmentations/storageAugmentation.js +2 -2
- package/dist/augmentations/storageAugmentations.d.ts +37 -8
- package/dist/augmentations/storageAugmentations.js +204 -15
- package/dist/augmentations/synapseAugmentation.d.ts +1 -1
- package/dist/augmentations/synapseAugmentation.js +35 -16
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
- package/dist/augmentations/universalDisplayAugmentation.d.ts +2 -2
- package/dist/augmentations/universalDisplayAugmentation.js +2 -2
- package/dist/brainy-unified.d.ts +106 -0
- package/dist/brainy-unified.js +327 -0
- package/dist/brainy.d.ts +273 -0
- package/dist/brainy.js +1181 -0
- package/dist/brainyData.d.ts +29 -72
- package/dist/brainyData.js +350 -304
- package/dist/brainyDataV3.d.ts +186 -0
- package/dist/brainyDataV3.js +337 -0
- package/dist/browserFramework.d.ts +6 -6
- package/dist/browserFramework.js +11 -8
- package/dist/browserFramework.minimal.d.ts +5 -5
- package/dist/browserFramework.minimal.js +11 -8
- package/dist/config/index.d.ts +2 -2
- package/dist/config/index.js +3 -3
- package/dist/config/modelAutoConfig.d.ts +6 -7
- package/dist/config/modelAutoConfig.js +17 -76
- package/dist/cortex/backupRestore.d.ts +2 -2
- package/dist/cortex/backupRestore.js +85 -27
- package/dist/cortex/healthCheck.d.ts +2 -2
- package/dist/cortex/neuralImport.d.ts +2 -2
- package/dist/cortex/neuralImport.js +18 -13
- package/dist/cortex/performanceMonitor.d.ts +2 -2
- package/dist/critical/model-guardian.d.ts +4 -0
- package/dist/critical/model-guardian.js +31 -11
- package/dist/demo.d.ts +4 -4
- package/dist/demo.js +7 -7
- package/dist/distributed/cacheSync.d.ts +112 -0
- package/dist/distributed/cacheSync.js +265 -0
- package/dist/distributed/coordinator.d.ts +193 -0
- package/dist/distributed/coordinator.js +548 -0
- package/dist/distributed/httpTransport.d.ts +120 -0
- package/dist/distributed/httpTransport.js +446 -0
- package/dist/distributed/index.d.ts +8 -0
- package/dist/distributed/index.js +5 -0
- package/dist/distributed/networkTransport.d.ts +132 -0
- package/dist/distributed/networkTransport.js +633 -0
- package/dist/distributed/queryPlanner.d.ts +104 -0
- package/dist/distributed/queryPlanner.js +327 -0
- package/dist/distributed/readWriteSeparation.d.ts +134 -0
- package/dist/distributed/readWriteSeparation.js +350 -0
- package/dist/distributed/shardManager.d.ts +114 -0
- package/dist/distributed/shardManager.js +357 -0
- package/dist/distributed/shardMigration.d.ts +110 -0
- package/dist/distributed/shardMigration.js +289 -0
- package/dist/distributed/storageDiscovery.d.ts +160 -0
- package/dist/distributed/storageDiscovery.js +551 -0
- package/dist/embeddings/EmbeddingManager.d.ts +0 -4
- package/dist/embeddings/EmbeddingManager.js +21 -26
- package/dist/errors/brainyError.d.ts +5 -1
- package/dist/errors/brainyError.js +12 -0
- package/dist/examples/basicUsage.js +3 -3
- package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
- package/dist/graph/graphAdjacencyIndex.js +288 -0
- package/dist/graph/pathfinding.js +4 -2
- package/dist/hnsw/scaledHNSWSystem.js +11 -2
- package/dist/importManager.js +6 -3
- package/dist/index.d.ts +12 -21
- package/dist/index.js +14 -22
- package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
- package/dist/mcp/brainyMCPAdapter.js +5 -5
- package/dist/mcp/brainyMCPService.d.ts +3 -3
- package/dist/mcp/brainyMCPService.js +3 -11
- package/dist/mcp/mcpAugmentationToolset.js +20 -30
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +2 -2
- package/dist/neural/entityExtractor.d.ts +65 -0
- package/dist/neural/entityExtractor.js +316 -0
- package/dist/neural/improvedNeuralAPI.js +90 -79
- package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
- package/dist/neural/naturalLanguageProcessor.js +941 -66
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
- package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
- package/dist/neural/neuralAPI.js +8 -2
- package/dist/neural/patternLibrary.d.ts +57 -3
- package/dist/neural/patternLibrary.js +348 -13
- package/dist/neural/staticPatternMatcher.d.ts +2 -2
- package/dist/neural/staticPatternMatcher.js +2 -2
- package/dist/shared/default-augmentations.d.ts +3 -3
- package/dist/shared/default-augmentations.js +5 -5
- package/dist/storage/adapters/fileSystemStorage.d.ts +4 -0
- package/dist/storage/adapters/fileSystemStorage.js +54 -1
- package/dist/storage/adapters/memoryStorage.js +13 -8
- package/dist/storage/backwardCompatibility.d.ts +10 -78
- package/dist/storage/backwardCompatibility.js +17 -132
- package/dist/storage/baseStorage.d.ts +6 -0
- package/dist/storage/baseStorage.js +17 -0
- package/dist/storage/cacheManager.js +2 -2
- package/dist/storage/readOnlyOptimizations.js +8 -3
- package/dist/streaming/pipeline.d.ts +154 -0
- package/dist/streaming/pipeline.js +551 -0
- package/dist/triple/TripleIntelligence.d.ts +25 -110
- package/dist/triple/TripleIntelligence.js +4 -574
- package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
- package/dist/triple/TripleIntelligenceSystem.js +519 -0
- package/dist/types/apiTypes.d.ts +278 -0
- package/dist/types/apiTypes.js +33 -0
- package/dist/types/brainy.types.d.ts +308 -0
- package/dist/types/brainy.types.js +8 -0
- package/dist/types/brainyDataInterface.d.ts +3 -3
- package/dist/types/brainyDataInterface.js +2 -2
- package/dist/types/graphTypes.js +2 -2
- package/dist/utils/cacheAutoConfig.d.ts +3 -3
- package/dist/utils/embedding.js +8 -14
- package/dist/utils/enhancedLogger.d.ts +104 -0
- package/dist/utils/enhancedLogger.js +232 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/intelligentTypeMapper.d.ts +60 -0
- package/dist/utils/intelligentTypeMapper.js +349 -0
- package/dist/utils/metadataIndex.d.ts +118 -1
- package/dist/utils/metadataIndex.js +539 -16
- package/dist/utils/paramValidation.d.ts +39 -0
- package/dist/utils/paramValidation.js +192 -0
- package/dist/utils/rateLimiter.d.ts +160 -0
- package/dist/utils/rateLimiter.js +271 -0
- package/dist/utils/statistics.d.ts +4 -4
- package/dist/utils/statistics.js +3 -3
- package/dist/utils/structuredLogger.d.ts +146 -0
- package/dist/utils/structuredLogger.js +394 -0
- package/dist/utils/textEncoding.js +2 -1
- package/dist/utils/typeValidation.d.ts +34 -0
- package/dist/utils/typeValidation.js +247 -0
- package/package.json +14 -6
- package/scripts/download-models.cjs +6 -15
- package/dist/augmentations/walAugmentation.d.ts +0 -111
- package/dist/augmentations/walAugmentation.js +0 -519
- package/dist/chat/BrainyChat.d.ts +0 -121
- package/dist/chat/BrainyChat.js +0 -396
- package/dist/chat/ChatCLI.d.ts +0 -61
- package/dist/chat/ChatCLI.js +0 -351
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read/Write Separation for Distributed Scaling
|
|
3
|
+
* Implements primary-replica architecture for scalable reads
|
|
4
|
+
*/
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
/**
|
|
7
|
+
* Read/Write Separation Manager
|
|
8
|
+
*/
|
|
9
|
+
export class ReadWriteSeparation extends EventEmitter {
|
|
10
|
+
constructor(config, coordinator, _shardManager, cacheSync) {
|
|
11
|
+
super();
|
|
12
|
+
this.replicas = new Map();
|
|
13
|
+
this.isRunning = false;
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.nodeId = config.nodeId;
|
|
16
|
+
this.role = config.role === 'auto' ? this.determineRole() : (config.role || 'replica');
|
|
17
|
+
this.coordinator = coordinator;
|
|
18
|
+
this.cacheSync = cacheSync;
|
|
19
|
+
this.replicationLog = {
|
|
20
|
+
operations: [],
|
|
21
|
+
lastSequence: 0,
|
|
22
|
+
primaryVersion: 0
|
|
23
|
+
};
|
|
24
|
+
// Setup connections based on role
|
|
25
|
+
this.setupConnections();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Start read/write separation
|
|
29
|
+
*/
|
|
30
|
+
async start() {
|
|
31
|
+
if (this.isRunning)
|
|
32
|
+
return;
|
|
33
|
+
this.isRunning = true;
|
|
34
|
+
// Start components
|
|
35
|
+
await this.coordinator.start();
|
|
36
|
+
// Setup role-specific behavior
|
|
37
|
+
if (this.role === 'primary') {
|
|
38
|
+
this.startAsPrimary();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.startAsReplica();
|
|
42
|
+
}
|
|
43
|
+
this.emit('started', { nodeId: this.nodeId, role: this.role });
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Stop read/write separation
|
|
47
|
+
*/
|
|
48
|
+
async stop() {
|
|
49
|
+
if (!this.isRunning)
|
|
50
|
+
return;
|
|
51
|
+
this.isRunning = false;
|
|
52
|
+
if (this.syncTimer) {
|
|
53
|
+
clearInterval(this.syncTimer);
|
|
54
|
+
this.syncTimer = undefined;
|
|
55
|
+
}
|
|
56
|
+
await this.coordinator.stop();
|
|
57
|
+
this.emit('stopped', { nodeId: this.nodeId });
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Execute a write operation (primary only)
|
|
61
|
+
*/
|
|
62
|
+
async write(operation) {
|
|
63
|
+
if (this.role !== 'primary') {
|
|
64
|
+
// Forward to primary
|
|
65
|
+
if (this.primaryConnection) {
|
|
66
|
+
return this.primaryConnection.forwardWrite(operation);
|
|
67
|
+
}
|
|
68
|
+
throw new Error('Cannot write: not connected to primary');
|
|
69
|
+
}
|
|
70
|
+
// Generate operation metadata
|
|
71
|
+
const writeOp = {
|
|
72
|
+
id: this.generateOperationId(),
|
|
73
|
+
...operation,
|
|
74
|
+
timestamp: Date.now(),
|
|
75
|
+
version: ++this.replicationLog.primaryVersion
|
|
76
|
+
};
|
|
77
|
+
// Add to replication log
|
|
78
|
+
this.replicationLog.operations.push(writeOp);
|
|
79
|
+
this.replicationLog.lastSequence++;
|
|
80
|
+
// Propagate to replicas
|
|
81
|
+
this.propagateToReplicas(writeOp);
|
|
82
|
+
// Update cache
|
|
83
|
+
if (operation.type !== 'delete') {
|
|
84
|
+
this.cacheSync.set(writeOp.id, operation.data);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.cacheSync.delete(writeOp.id);
|
|
88
|
+
}
|
|
89
|
+
this.emit('write', writeOp);
|
|
90
|
+
return writeOp.id;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Execute a read operation
|
|
94
|
+
*/
|
|
95
|
+
async read(key, options) {
|
|
96
|
+
const consistency = options?.consistency || this.config.consistencyLevel || 'eventual';
|
|
97
|
+
if (consistency === 'strong' && this.role === 'replica') {
|
|
98
|
+
// For strong consistency, read from primary
|
|
99
|
+
if (this.primaryConnection) {
|
|
100
|
+
return this.primaryConnection.read(key);
|
|
101
|
+
}
|
|
102
|
+
throw new Error('Cannot guarantee strong consistency: not connected to primary');
|
|
103
|
+
}
|
|
104
|
+
// Check cache first
|
|
105
|
+
const cached = this.cacheSync.get(key);
|
|
106
|
+
if (cached !== undefined) {
|
|
107
|
+
return cached;
|
|
108
|
+
}
|
|
109
|
+
// Read from appropriate source based on preference
|
|
110
|
+
if (this.config.readPreference === 'primary' && this.primaryConnection) {
|
|
111
|
+
return this.primaryConnection.read(key);
|
|
112
|
+
}
|
|
113
|
+
// Read locally (replica or primary)
|
|
114
|
+
return this.readLocal(key);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get replication lag (replica only)
|
|
118
|
+
*/
|
|
119
|
+
getReplicationLag() {
|
|
120
|
+
if (this.role === 'primary')
|
|
121
|
+
return 0;
|
|
122
|
+
if (this.primaryConnection) {
|
|
123
|
+
return Date.now() - this.primaryConnection.lastSync;
|
|
124
|
+
}
|
|
125
|
+
return -1; // Unknown
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Setup connections based on role
|
|
129
|
+
*/
|
|
130
|
+
setupConnections() {
|
|
131
|
+
if (this.role === 'primary') {
|
|
132
|
+
// Setup replica connections
|
|
133
|
+
if (this.config.replicaUrls) {
|
|
134
|
+
for (const url of this.config.replicaUrls) {
|
|
135
|
+
const replica = new ReplicaConnection(url);
|
|
136
|
+
this.replicas.set(url, replica);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
// Setup primary connection
|
|
142
|
+
if (this.config.primaryUrl) {
|
|
143
|
+
this.primaryConnection = new PrimaryConnection(this.config.primaryUrl);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Start as primary node
|
|
149
|
+
*/
|
|
150
|
+
startAsPrimary() {
|
|
151
|
+
// Start accepting writes
|
|
152
|
+
this.emit('roleEstablished', { role: 'primary' });
|
|
153
|
+
// Start replication timer
|
|
154
|
+
this.syncTimer = setInterval(() => {
|
|
155
|
+
this.syncReplicas();
|
|
156
|
+
}, this.config.syncInterval || 1000);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Start as replica node
|
|
160
|
+
*/
|
|
161
|
+
startAsReplica() {
|
|
162
|
+
// Start syncing from primary
|
|
163
|
+
this.emit('roleEstablished', { role: 'replica' });
|
|
164
|
+
// Start sync timer
|
|
165
|
+
this.syncTimer = setInterval(() => {
|
|
166
|
+
this.syncFromPrimary();
|
|
167
|
+
}, this.config.syncInterval || 1000);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Sync replicas (primary only)
|
|
171
|
+
*/
|
|
172
|
+
async syncReplicas() {
|
|
173
|
+
const batch = this.replicationLog.operations.slice(-100); // Last 100 ops
|
|
174
|
+
for (const [url, replica] of this.replicas) {
|
|
175
|
+
try {
|
|
176
|
+
await replica.sync(batch, this.replicationLog.primaryVersion);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
this.emit('replicaSyncError', { url, error });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Sync from primary (replica only)
|
|
185
|
+
*/
|
|
186
|
+
async syncFromPrimary() {
|
|
187
|
+
if (!this.primaryConnection)
|
|
188
|
+
return;
|
|
189
|
+
try {
|
|
190
|
+
const updates = await this.primaryConnection.getUpdates(this.replicationLog.lastSequence);
|
|
191
|
+
// Apply updates
|
|
192
|
+
for (const op of updates) {
|
|
193
|
+
await this.applyOperation(op);
|
|
194
|
+
}
|
|
195
|
+
this.emit('synced', { operations: updates.length });
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
this.emit('primarySyncError', { error });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Apply a replicated operation
|
|
203
|
+
*/
|
|
204
|
+
async applyOperation(op) {
|
|
205
|
+
// Update local state
|
|
206
|
+
this.replicationLog.operations.push(op);
|
|
207
|
+
this.replicationLog.lastSequence = Math.max(this.replicationLog.lastSequence, op.version);
|
|
208
|
+
// Update cache
|
|
209
|
+
switch (op.type) {
|
|
210
|
+
case 'add':
|
|
211
|
+
case 'update':
|
|
212
|
+
this.cacheSync.set(op.id, op.data);
|
|
213
|
+
break;
|
|
214
|
+
case 'delete':
|
|
215
|
+
this.cacheSync.delete(op.id);
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
this.emit('operationApplied', op);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Propagate operation to replicas
|
|
222
|
+
*/
|
|
223
|
+
propagateToReplicas(op) {
|
|
224
|
+
for (const replica of this.replicas.values()) {
|
|
225
|
+
replica.sendOperation(op).catch(error => {
|
|
226
|
+
this.emit('replicationError', { replica, error });
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Determine role automatically
|
|
232
|
+
*/
|
|
233
|
+
determineRole() {
|
|
234
|
+
// Use coordinator's leader election
|
|
235
|
+
return this.coordinator.isLeader() ? 'primary' : 'replica';
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Read from local storage
|
|
239
|
+
*/
|
|
240
|
+
async readLocal(key) {
|
|
241
|
+
// This would connect to actual storage
|
|
242
|
+
// For now, return from cache or undefined
|
|
243
|
+
return this.cacheSync.get(key);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Generate unique operation ID
|
|
247
|
+
*/
|
|
248
|
+
generateOperationId() {
|
|
249
|
+
return `${this.nodeId}-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Get replication statistics
|
|
253
|
+
*/
|
|
254
|
+
getStats() {
|
|
255
|
+
return {
|
|
256
|
+
role: this.role,
|
|
257
|
+
replicas: this.replicas.size,
|
|
258
|
+
replicationLag: this.getReplicationLag(),
|
|
259
|
+
operationsInLog: this.replicationLog.operations.length,
|
|
260
|
+
primaryVersion: this.replicationLog.primaryVersion
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Check if node can accept writes
|
|
265
|
+
*/
|
|
266
|
+
canWrite() {
|
|
267
|
+
return this.role === 'primary';
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Check if node can serve reads
|
|
271
|
+
*/
|
|
272
|
+
canRead() {
|
|
273
|
+
if (this.config.consistencyLevel === 'strong') {
|
|
274
|
+
return this.role === 'primary' || this.primaryConnection !== undefined;
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Set whether this node is primary (for leader election integration)
|
|
280
|
+
*/
|
|
281
|
+
setPrimary(isPrimary) {
|
|
282
|
+
const newRole = isPrimary ? 'primary' : 'replica';
|
|
283
|
+
if (this.role !== newRole) {
|
|
284
|
+
this.role = newRole;
|
|
285
|
+
this.emit('roleChange', { oldRole: this.role, newRole });
|
|
286
|
+
if (isPrimary) {
|
|
287
|
+
// Became primary - stop syncing from old primary
|
|
288
|
+
this.primaryConnection = undefined;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
// Became replica - connect to new primary if URL is known
|
|
292
|
+
if (this.config.primaryUrl) {
|
|
293
|
+
this.primaryConnection = new PrimaryConnection(this.config.primaryUrl);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Connection to a replica (used by primary)
|
|
301
|
+
*/
|
|
302
|
+
class ReplicaConnection {
|
|
303
|
+
constructor(url) {
|
|
304
|
+
this.url = url;
|
|
305
|
+
// Store URL for connection
|
|
306
|
+
void this.url;
|
|
307
|
+
}
|
|
308
|
+
async sync(_operations, _version) {
|
|
309
|
+
// In real implementation, this would use HTTP/gRPC to this.url
|
|
310
|
+
// For now, simulate network call
|
|
311
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
312
|
+
}
|
|
313
|
+
async sendOperation(_op) {
|
|
314
|
+
// Send single operation to replica at this.url
|
|
315
|
+
await new Promise(resolve => setTimeout(resolve, 5));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Connection to primary (used by replicas)
|
|
320
|
+
*/
|
|
321
|
+
class PrimaryConnection {
|
|
322
|
+
constructor(url) {
|
|
323
|
+
this.url = url;
|
|
324
|
+
this.lastSync = Date.now();
|
|
325
|
+
// Store URL for connection
|
|
326
|
+
void this.url;
|
|
327
|
+
}
|
|
328
|
+
async getUpdates(_fromSequence) {
|
|
329
|
+
// In real implementation, fetch from primary at this.url
|
|
330
|
+
this.lastSync = Date.now();
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
async forwardWrite(_operation) {
|
|
334
|
+
// Forward write to primary at this.url
|
|
335
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
336
|
+
return `forwarded-${Date.now()}`;
|
|
337
|
+
}
|
|
338
|
+
async read(_key) {
|
|
339
|
+
// Read from primary at this.url for strong consistency
|
|
340
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Create read/write separation manager
|
|
346
|
+
*/
|
|
347
|
+
export function createReadWriteSeparation(config, coordinator, shardManager, cacheSync) {
|
|
348
|
+
return new ReadWriteSeparation(config, coordinator, shardManager, cacheSync);
|
|
349
|
+
}
|
|
350
|
+
//# sourceMappingURL=readWriteSeparation.js.map
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shard Manager for Horizontal Scaling
|
|
3
|
+
* Implements consistent hashing for data distribution across shards
|
|
4
|
+
*/
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
export interface ShardConfig {
|
|
7
|
+
shardCount?: number;
|
|
8
|
+
replicationFactor?: number;
|
|
9
|
+
virtualNodes?: number;
|
|
10
|
+
autoRebalance?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface Shard {
|
|
13
|
+
id: string;
|
|
14
|
+
nodeId: string;
|
|
15
|
+
virtualNodes: string[];
|
|
16
|
+
itemCount: number;
|
|
17
|
+
sizeBytes: number;
|
|
18
|
+
status: 'active' | 'rebalancing' | 'offline';
|
|
19
|
+
}
|
|
20
|
+
export interface ShardAssignment {
|
|
21
|
+
shardId: string;
|
|
22
|
+
nodeId: string;
|
|
23
|
+
replicas: string[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Shard Manager for distributing data across multiple nodes
|
|
27
|
+
*/
|
|
28
|
+
export declare class ShardManager extends EventEmitter {
|
|
29
|
+
private hashRing;
|
|
30
|
+
private shards;
|
|
31
|
+
private nodeToShards;
|
|
32
|
+
private shardCount;
|
|
33
|
+
private replicationFactor;
|
|
34
|
+
private autoRebalance;
|
|
35
|
+
constructor(config?: ShardConfig);
|
|
36
|
+
/**
|
|
37
|
+
* Initialize shard configuration
|
|
38
|
+
*/
|
|
39
|
+
private initializeShards;
|
|
40
|
+
/**
|
|
41
|
+
* Add a node to the cluster
|
|
42
|
+
*/
|
|
43
|
+
addNode(nodeId: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* Remove a node from the cluster
|
|
46
|
+
*/
|
|
47
|
+
removeNode(nodeId: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get shard assignment for a key
|
|
50
|
+
*/
|
|
51
|
+
getShardForKey(key: string): ShardAssignment | null;
|
|
52
|
+
/**
|
|
53
|
+
* Get nodes responsible for a shard
|
|
54
|
+
*/
|
|
55
|
+
getNodesForShard(shardId: string): string[];
|
|
56
|
+
/**
|
|
57
|
+
* Get total number of shards
|
|
58
|
+
*/
|
|
59
|
+
getTotalShards(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Update shard assignment to a new node
|
|
62
|
+
*/
|
|
63
|
+
updateShardAssignment(shardId: string, newNodeId: string): void;
|
|
64
|
+
/**
|
|
65
|
+
* Get shard ID for a key
|
|
66
|
+
*/
|
|
67
|
+
private getShardId;
|
|
68
|
+
/**
|
|
69
|
+
* Rebalance shards across nodes
|
|
70
|
+
*/
|
|
71
|
+
private rebalanceShards;
|
|
72
|
+
/**
|
|
73
|
+
* Get shard assignment for all shards
|
|
74
|
+
*/
|
|
75
|
+
getShardAssignments(): ShardAssignment[];
|
|
76
|
+
/**
|
|
77
|
+
* Get shard statistics
|
|
78
|
+
*/
|
|
79
|
+
getShardStats(): {
|
|
80
|
+
totalShards: number;
|
|
81
|
+
activeShards: number;
|
|
82
|
+
rebalancingShards: number;
|
|
83
|
+
offlineShards: number;
|
|
84
|
+
averageItemsPerShard: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Update shard metrics
|
|
88
|
+
*/
|
|
89
|
+
updateShardMetrics(shardId: string, itemCount: number, sizeBytes: number): void;
|
|
90
|
+
/**
|
|
91
|
+
* Get replication nodes for a shard
|
|
92
|
+
*/
|
|
93
|
+
getReplicationNodes(shardId: string): string[];
|
|
94
|
+
/**
|
|
95
|
+
* Check if rebalancing is needed
|
|
96
|
+
*/
|
|
97
|
+
needsRebalancing(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Get cluster health
|
|
100
|
+
*/
|
|
101
|
+
getHealth(): {
|
|
102
|
+
healthy: boolean;
|
|
103
|
+
nodes: number;
|
|
104
|
+
shards: {
|
|
105
|
+
total: number;
|
|
106
|
+
active: number;
|
|
107
|
+
inactive: number;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Create a shard manager instance
|
|
113
|
+
*/
|
|
114
|
+
export declare function createShardManager(config?: ShardConfig): ShardManager;
|