@soulcraft/brainy 3.0.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 +53 -3
- package/README.md +353 -110
- package/bin/brainy.js +340 -62
- 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 +290 -9
- 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 +142 -8
- package/dist/augmentations/brainyAugmentation.js +179 -2
- package/dist/augmentations/cacheAugmentation.d.ts +8 -5
- package/dist/augmentations/cacheAugmentation.js +116 -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 +14 -10
- package/dist/augmentations/defaultAugmentations.js +16 -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/cache.d.ts +130 -0
- package/dist/augmentations/display/cache.js +319 -0
- package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
- package/dist/augmentations/display/fieldPatterns.js +393 -0
- package/dist/augmentations/display/iconMappings.d.ts +57 -0
- package/dist/augmentations/display/iconMappings.js +68 -0
- package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
- package/dist/augmentations/display/intelligentComputation.js +462 -0
- package/dist/augmentations/display/types.d.ts +203 -0
- package/dist/augmentations/display/types.js +7 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
- package/dist/augmentations/entityRegistryAugmentation.js +5 -1
- package/dist/augmentations/indexAugmentation.d.ts +5 -3
- package/dist/augmentations/indexAugmentation.js +5 -2
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
- 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/neuralImport.js +4 -4
- 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/brainyTypes.d.ts +83 -0
- package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
- package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
- package/dist/augmentations/universalDisplayAugmentation.js +371 -0
- 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 +56 -111
- package/dist/brainyData.js +912 -756
- 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/distributedPresets-new.d.ts +118 -0
- package/dist/config/distributedPresets-new.js +318 -0
- package/dist/config/distributedPresets.d.ts +118 -0
- package/dist/config/distributedPresets.js +318 -0
- package/dist/config/extensibleConfig.d.ts +99 -0
- package/dist/config/extensibleConfig.js +268 -0
- package/dist/config/index.d.ts +17 -0
- package/dist/config/index.js +35 -0
- package/dist/config/modelAutoConfig.d.ts +32 -0
- package/dist/config/modelAutoConfig.js +139 -0
- package/dist/config/modelPrecisionManager.d.ts +42 -0
- package/dist/config/modelPrecisionManager.js +98 -0
- package/dist/config/sharedConfigManager.d.ts +67 -0
- package/dist/config/sharedConfigManager.js +215 -0
- package/dist/config/storageAutoConfig.d.ts +41 -0
- package/dist/config/storageAutoConfig.js +328 -0
- package/dist/config/zeroConfig.d.ts +68 -0
- package/dist/config/zeroConfig.js +301 -0
- 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/CachedEmbeddings.d.ts +40 -0
- package/dist/embeddings/CachedEmbeddings.js +146 -0
- package/dist/embeddings/EmbeddingManager.d.ts +102 -0
- package/dist/embeddings/EmbeddingManager.js +291 -0
- package/dist/embeddings/SingletonModelManager.d.ts +95 -0
- package/dist/embeddings/SingletonModelManager.js +220 -0
- package/dist/embeddings/index.d.ts +12 -0
- package/dist/embeddings/index.js +16 -0
- package/dist/embeddings/lightweight-embedder.d.ts +0 -1
- package/dist/embeddings/lightweight-embedder.js +4 -12
- package/dist/embeddings/model-manager.d.ts +11 -0
- package/dist/embeddings/model-manager.js +43 -7
- package/dist/embeddings/universal-memory-manager.d.ts +1 -1
- package/dist/embeddings/universal-memory-manager.js +27 -67
- package/dist/embeddings/worker-embedding.js +4 -8
- package/dist/errors/brainyError.d.ts +5 -1
- package/dist/errors/brainyError.js +12 -0
- package/dist/examples/basicUsage.js +7 -4
- 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 +8 -5
- package/dist/index.d.ts +17 -22
- package/dist/index.js +37 -23
- 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.d.ts +357 -0
- package/dist/neural/improvedNeuralAPI.js +2628 -0
- 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/neural/types.d.ts +287 -0
- package/dist/neural/types.js +24 -0
- package/dist/shared/default-augmentations.d.ts +3 -3
- package/dist/shared/default-augmentations.js +5 -5
- package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
- package/dist/storage/adapters/fileSystemStorage.js +218 -15
- package/dist/storage/adapters/memoryStorage.d.ts +4 -4
- package/dist/storage/adapters/memoryStorage.js +17 -12
- package/dist/storage/adapters/opfsStorage.d.ts +2 -2
- package/dist/storage/adapters/opfsStorage.js +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
- package/dist/storage/backwardCompatibility.d.ts +10 -78
- package/dist/storage/backwardCompatibility.js +17 -132
- package/dist/storage/baseStorage.d.ts +18 -2
- package/dist/storage/baseStorage.js +74 -3
- 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 +5 -8
- package/dist/types/brainyDataInterface.js +2 -2
- package/dist/types/graphTypes.js +2 -2
- package/dist/utils/brainyTypes.d.ts +217 -0
- package/dist/utils/brainyTypes.js +261 -0
- package/dist/utils/cacheAutoConfig.d.ts +3 -3
- package/dist/utils/embedding.d.ts +9 -4
- package/dist/utils/embedding.js +89 -26
- package/dist/utils/enhancedLogger.d.ts +104 -0
- package/dist/utils/enhancedLogger.js +232 -0
- package/dist/utils/hybridModelManager.d.ts +19 -28
- package/dist/utils/hybridModelManager.js +36 -200
- 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/nodeVersionCheck.d.ts +24 -0
- package/dist/utils/nodeVersionCheck.js +65 -0
- 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 +59 -0
- package/dist/utils/typeValidation.js +374 -0
- package/dist/utils/version.js +19 -3
- package/package.json +15 -4
- package/scripts/download-models.cjs +94 -20
- package/dist/augmentations/walAugmentation.d.ts +0 -109
- package/dist/augmentations/walAugmentation.js +0 -516
- 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,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed Cache Synchronization
|
|
3
|
+
* Provides cache coherence across multiple Brainy instances
|
|
4
|
+
*/
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
/**
|
|
7
|
+
* Distributed Cache Synchronizer
|
|
8
|
+
*/
|
|
9
|
+
export class CacheSync extends EventEmitter {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
super();
|
|
12
|
+
this.localCache = new Map();
|
|
13
|
+
this.versionVector = new Map();
|
|
14
|
+
this.syncQueue = [];
|
|
15
|
+
this.isRunning = false;
|
|
16
|
+
this.nodeId = config.nodeId;
|
|
17
|
+
this.syncInterval = config.syncInterval || 1000;
|
|
18
|
+
this.maxSyncBatchSize = config.maxSyncBatchSize || 100;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Start cache synchronization
|
|
22
|
+
*/
|
|
23
|
+
start() {
|
|
24
|
+
if (this.isRunning)
|
|
25
|
+
return;
|
|
26
|
+
this.isRunning = true;
|
|
27
|
+
this.startSyncTimer();
|
|
28
|
+
this.emit('started', { nodeId: this.nodeId });
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Stop cache synchronization
|
|
32
|
+
*/
|
|
33
|
+
stop() {
|
|
34
|
+
if (!this.isRunning)
|
|
35
|
+
return;
|
|
36
|
+
this.isRunning = false;
|
|
37
|
+
if (this.syncTimer) {
|
|
38
|
+
clearInterval(this.syncTimer);
|
|
39
|
+
this.syncTimer = undefined;
|
|
40
|
+
}
|
|
41
|
+
this.emit('stopped', { nodeId: this.nodeId });
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get a value from cache
|
|
45
|
+
*/
|
|
46
|
+
get(key) {
|
|
47
|
+
const entry = this.localCache.get(key);
|
|
48
|
+
if (!entry)
|
|
49
|
+
return undefined;
|
|
50
|
+
// Check TTL
|
|
51
|
+
if (entry.ttl && Date.now() - entry.timestamp > entry.ttl) {
|
|
52
|
+
this.localCache.delete(key);
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
return entry.value;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Set a value in cache and propagate
|
|
59
|
+
*/
|
|
60
|
+
set(key, value, ttl) {
|
|
61
|
+
const version = this.incrementVersion(key);
|
|
62
|
+
const entry = {
|
|
63
|
+
key,
|
|
64
|
+
value,
|
|
65
|
+
version,
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
ttl,
|
|
68
|
+
nodeId: this.nodeId
|
|
69
|
+
};
|
|
70
|
+
this.localCache.set(key, entry);
|
|
71
|
+
// Queue for sync
|
|
72
|
+
this.queueSync('update', [entry]);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Delete a value from cache and propagate
|
|
76
|
+
*/
|
|
77
|
+
delete(key) {
|
|
78
|
+
const existed = this.localCache.has(key);
|
|
79
|
+
if (existed) {
|
|
80
|
+
const version = this.incrementVersion(key);
|
|
81
|
+
this.localCache.delete(key);
|
|
82
|
+
// Queue deletion for sync
|
|
83
|
+
this.queueSync('delete', [{
|
|
84
|
+
key,
|
|
85
|
+
value: null,
|
|
86
|
+
version,
|
|
87
|
+
timestamp: Date.now(),
|
|
88
|
+
nodeId: this.nodeId
|
|
89
|
+
}]);
|
|
90
|
+
}
|
|
91
|
+
return existed;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Invalidate a cache entry across all nodes
|
|
95
|
+
*/
|
|
96
|
+
invalidate(key) {
|
|
97
|
+
const version = this.incrementVersion(key);
|
|
98
|
+
this.localCache.delete(key);
|
|
99
|
+
// Queue invalidation
|
|
100
|
+
this.queueSync('invalidate', [{
|
|
101
|
+
key,
|
|
102
|
+
value: null,
|
|
103
|
+
version,
|
|
104
|
+
timestamp: Date.now(),
|
|
105
|
+
nodeId: this.nodeId
|
|
106
|
+
}]);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Clear all cache entries
|
|
110
|
+
*/
|
|
111
|
+
clear() {
|
|
112
|
+
const entries = [];
|
|
113
|
+
for (const key of this.localCache.keys()) {
|
|
114
|
+
const version = this.incrementVersion(key);
|
|
115
|
+
entries.push({
|
|
116
|
+
key,
|
|
117
|
+
value: null,
|
|
118
|
+
version,
|
|
119
|
+
timestamp: Date.now(),
|
|
120
|
+
nodeId: this.nodeId
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
this.localCache.clear();
|
|
124
|
+
if (entries.length > 0) {
|
|
125
|
+
this.queueSync('delete', entries);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Handle incoming sync message from another node
|
|
130
|
+
*/
|
|
131
|
+
handleSyncMessage(message) {
|
|
132
|
+
if (message.source === this.nodeId)
|
|
133
|
+
return; // Ignore own messages
|
|
134
|
+
for (const entry of message.entries) {
|
|
135
|
+
this.handleRemoteEntry(message.type, entry);
|
|
136
|
+
}
|
|
137
|
+
this.emit('synced', {
|
|
138
|
+
type: message.type,
|
|
139
|
+
entries: message.entries.length,
|
|
140
|
+
source: message.source
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Handle a remote cache entry
|
|
145
|
+
*/
|
|
146
|
+
handleRemoteEntry(type, entry) {
|
|
147
|
+
const localEntry = this.localCache.get(entry.key);
|
|
148
|
+
const localVersion = this.versionVector.get(entry.key) || 0;
|
|
149
|
+
// Version vector check - only accept if remote version is newer
|
|
150
|
+
if (entry.version <= localVersion) {
|
|
151
|
+
return; // Our version is newer or same, ignore
|
|
152
|
+
}
|
|
153
|
+
// Update version vector
|
|
154
|
+
this.versionVector.set(entry.key, entry.version);
|
|
155
|
+
switch (type) {
|
|
156
|
+
case 'update':
|
|
157
|
+
case 'batch':
|
|
158
|
+
// Update local cache with remote value
|
|
159
|
+
this.localCache.set(entry.key, entry);
|
|
160
|
+
break;
|
|
161
|
+
case 'delete':
|
|
162
|
+
case 'invalidate':
|
|
163
|
+
// Remove from local cache
|
|
164
|
+
this.localCache.delete(entry.key);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Queue a sync message
|
|
170
|
+
*/
|
|
171
|
+
queueSync(type, entries) {
|
|
172
|
+
const message = {
|
|
173
|
+
type,
|
|
174
|
+
entries,
|
|
175
|
+
source: this.nodeId,
|
|
176
|
+
timestamp: Date.now()
|
|
177
|
+
};
|
|
178
|
+
this.syncQueue.push(message);
|
|
179
|
+
// If queue is getting large, sync immediately
|
|
180
|
+
if (this.syncQueue.length >= this.maxSyncBatchSize) {
|
|
181
|
+
this.performSync();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Start sync timer
|
|
186
|
+
*/
|
|
187
|
+
startSyncTimer() {
|
|
188
|
+
this.syncTimer = setInterval(() => {
|
|
189
|
+
this.performSync();
|
|
190
|
+
}, this.syncInterval);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Perform sync operation
|
|
194
|
+
*/
|
|
195
|
+
performSync() {
|
|
196
|
+
if (this.syncQueue.length === 0)
|
|
197
|
+
return;
|
|
198
|
+
// Batch multiple messages if possible
|
|
199
|
+
const messages = this.syncQueue.splice(0, this.maxSyncBatchSize);
|
|
200
|
+
if (messages.length === 1) {
|
|
201
|
+
// Single message
|
|
202
|
+
this.emit('sync', messages[0]);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
// Batch multiple messages
|
|
206
|
+
const batchedEntries = [];
|
|
207
|
+
for (const msg of messages) {
|
|
208
|
+
batchedEntries.push(...msg.entries);
|
|
209
|
+
}
|
|
210
|
+
const batchMessage = {
|
|
211
|
+
type: 'batch',
|
|
212
|
+
entries: batchedEntries,
|
|
213
|
+
source: this.nodeId,
|
|
214
|
+
timestamp: Date.now()
|
|
215
|
+
};
|
|
216
|
+
this.emit('sync', batchMessage);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Increment version for a key
|
|
221
|
+
*/
|
|
222
|
+
incrementVersion(key) {
|
|
223
|
+
const current = this.versionVector.get(key) || 0;
|
|
224
|
+
const next = current + 1;
|
|
225
|
+
this.versionVector.set(key, next);
|
|
226
|
+
return next;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Get cache statistics
|
|
230
|
+
*/
|
|
231
|
+
getStats() {
|
|
232
|
+
// Estimate memory usage (rough approximation)
|
|
233
|
+
let memoryUsage = 0;
|
|
234
|
+
for (const entry of this.localCache.values()) {
|
|
235
|
+
memoryUsage += JSON.stringify(entry).length;
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
entries: this.localCache.size,
|
|
239
|
+
pendingSync: this.syncQueue.length,
|
|
240
|
+
versionedKeys: this.versionVector.size,
|
|
241
|
+
memoryUsage
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Get cache entries for debugging
|
|
246
|
+
*/
|
|
247
|
+
getEntries() {
|
|
248
|
+
return Array.from(this.localCache.values());
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Merge cache state from another node (for recovery)
|
|
252
|
+
*/
|
|
253
|
+
mergeState(entries) {
|
|
254
|
+
for (const entry of entries) {
|
|
255
|
+
this.handleRemoteEntry('update', entry);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Create a cache sync instance
|
|
261
|
+
*/
|
|
262
|
+
export function createCacheSync(config) {
|
|
263
|
+
return new CacheSync(config);
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=cacheSync.js.map
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed Coordinator for Brainy 3.0
|
|
3
|
+
* Provides leader election, consensus, and coordination for distributed instances
|
|
4
|
+
*/
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { NetworkTransport } from './networkTransport.js';
|
|
7
|
+
export interface NodeInfo {
|
|
8
|
+
id: string;
|
|
9
|
+
address: string;
|
|
10
|
+
port: number;
|
|
11
|
+
lastSeen: number;
|
|
12
|
+
status: 'active' | 'inactive' | 'suspected';
|
|
13
|
+
state?: 'follower' | 'candidate' | 'leader';
|
|
14
|
+
lastHeartbeat?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface CoordinatorConfig {
|
|
17
|
+
nodeId?: string;
|
|
18
|
+
address?: string;
|
|
19
|
+
port?: number;
|
|
20
|
+
heartbeatInterval?: number;
|
|
21
|
+
electionTimeout?: number;
|
|
22
|
+
nodes?: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface ConsensusState {
|
|
25
|
+
term: number;
|
|
26
|
+
votedFor: string | null;
|
|
27
|
+
leader: string | null;
|
|
28
|
+
state: 'follower' | 'candidate' | 'leader';
|
|
29
|
+
}
|
|
30
|
+
export interface RaftMessage {
|
|
31
|
+
type: 'requestVote' | 'voteResponse' | 'appendEntries' | 'appendResponse';
|
|
32
|
+
term: number;
|
|
33
|
+
from: string;
|
|
34
|
+
to?: string;
|
|
35
|
+
data?: any;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Distributed Coordinator implementing Raft-like consensus
|
|
39
|
+
*/
|
|
40
|
+
export declare class DistributedCoordinator extends EventEmitter {
|
|
41
|
+
private nodeId;
|
|
42
|
+
private nodes;
|
|
43
|
+
private consensusState;
|
|
44
|
+
private heartbeatInterval;
|
|
45
|
+
private electionTimeout;
|
|
46
|
+
private electionTimer?;
|
|
47
|
+
private heartbeatTimer?;
|
|
48
|
+
private isRunning;
|
|
49
|
+
private networkTransport?;
|
|
50
|
+
private votesReceived;
|
|
51
|
+
private currentTerm;
|
|
52
|
+
private lastLogIndex;
|
|
53
|
+
private lastLogTerm;
|
|
54
|
+
private logEntries;
|
|
55
|
+
private transport;
|
|
56
|
+
private pendingMigrations;
|
|
57
|
+
private committedMigrations;
|
|
58
|
+
constructor(config?: CoordinatorConfig);
|
|
59
|
+
/**
|
|
60
|
+
* Start the coordinator
|
|
61
|
+
*/
|
|
62
|
+
start(networkTransport?: NetworkTransport): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Setup network message handlers
|
|
65
|
+
*/
|
|
66
|
+
private setupNetworkHandlers;
|
|
67
|
+
/**
|
|
68
|
+
* Stop the coordinator
|
|
69
|
+
*/
|
|
70
|
+
stop(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Register additional nodes
|
|
73
|
+
*/
|
|
74
|
+
registerNodes(nodes: string[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* Become a follower
|
|
77
|
+
*/
|
|
78
|
+
private becomeFollower;
|
|
79
|
+
/**
|
|
80
|
+
* Become a candidate and start election
|
|
81
|
+
*/
|
|
82
|
+
private becomeCandidate;
|
|
83
|
+
/**
|
|
84
|
+
* Become the leader
|
|
85
|
+
*/
|
|
86
|
+
private becomeLeader;
|
|
87
|
+
/**
|
|
88
|
+
* Request votes from all nodes
|
|
89
|
+
*/
|
|
90
|
+
private requestVotes;
|
|
91
|
+
/**
|
|
92
|
+
* Handle vote request from another node
|
|
93
|
+
*/
|
|
94
|
+
private handleVoteRequest;
|
|
95
|
+
/**
|
|
96
|
+
* Handle vote response
|
|
97
|
+
*/
|
|
98
|
+
private handleVoteResponse;
|
|
99
|
+
/**
|
|
100
|
+
* Check if we have majority votes
|
|
101
|
+
*/
|
|
102
|
+
private checkVoteMajority;
|
|
103
|
+
/**
|
|
104
|
+
* Handle append entries (heartbeat) from leader
|
|
105
|
+
*/
|
|
106
|
+
private handleAppendEntries;
|
|
107
|
+
/**
|
|
108
|
+
* Handle append response from follower
|
|
109
|
+
*/
|
|
110
|
+
private handleAppendResponse;
|
|
111
|
+
/**
|
|
112
|
+
* Send heartbeat to followers
|
|
113
|
+
*/
|
|
114
|
+
private sendHeartbeat;
|
|
115
|
+
/**
|
|
116
|
+
* Start heartbeat timer as leader
|
|
117
|
+
*/
|
|
118
|
+
private startHeartbeat;
|
|
119
|
+
/**
|
|
120
|
+
* Reset election timeout
|
|
121
|
+
*/
|
|
122
|
+
private resetElectionTimeout;
|
|
123
|
+
/**
|
|
124
|
+
* Check if log is up to date
|
|
125
|
+
*/
|
|
126
|
+
private isLogUpToDate;
|
|
127
|
+
/**
|
|
128
|
+
* Check log consistency
|
|
129
|
+
*/
|
|
130
|
+
private checkLogConsistency;
|
|
131
|
+
/**
|
|
132
|
+
* Append log entries
|
|
133
|
+
*/
|
|
134
|
+
private appendLogEntries;
|
|
135
|
+
/**
|
|
136
|
+
* Get last log index
|
|
137
|
+
*/
|
|
138
|
+
private getLastLogIndex;
|
|
139
|
+
/**
|
|
140
|
+
* Get last log term
|
|
141
|
+
*/
|
|
142
|
+
private getLastLogTerm;
|
|
143
|
+
/**
|
|
144
|
+
* Generate a unique node ID
|
|
145
|
+
*/
|
|
146
|
+
private generateNodeId;
|
|
147
|
+
/**
|
|
148
|
+
* Get current leader
|
|
149
|
+
*/
|
|
150
|
+
getLeader(): string | null;
|
|
151
|
+
/**
|
|
152
|
+
* Propose a shard migration to the cluster
|
|
153
|
+
*/
|
|
154
|
+
proposeMigration(migration: {
|
|
155
|
+
shardId: string;
|
|
156
|
+
fromNode: string;
|
|
157
|
+
toNode: string;
|
|
158
|
+
migrationId: string;
|
|
159
|
+
}): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* Get migration status
|
|
162
|
+
*/
|
|
163
|
+
getMigrationStatus(migrationId: string): Promise<'pending' | 'committed' | 'rejected'>;
|
|
164
|
+
/**
|
|
165
|
+
* Check if this node is the leader
|
|
166
|
+
*/
|
|
167
|
+
isLeader(): boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Get all nodes in the cluster
|
|
170
|
+
*/
|
|
171
|
+
getNodes(): NodeInfo[];
|
|
172
|
+
/**
|
|
173
|
+
* Get cluster health status
|
|
174
|
+
*/
|
|
175
|
+
getHealth(): {
|
|
176
|
+
healthy: boolean;
|
|
177
|
+
leader: string | null;
|
|
178
|
+
nodes: number;
|
|
179
|
+
activeNodes: number;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Propose a command to the cluster
|
|
183
|
+
*/
|
|
184
|
+
proposeCommand(command: any): Promise<void>;
|
|
185
|
+
/**
|
|
186
|
+
* Get current state
|
|
187
|
+
*/
|
|
188
|
+
getState(): ConsensusState;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Create a coordinator instance
|
|
192
|
+
*/
|
|
193
|
+
export declare function createCoordinator(config?: CoordinatorConfig): DistributedCoordinator;
|