@soulcraft/brainy 0.37.0 → 0.39.0
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 +726 -1581
- package/dist/brainyData.d.ts +98 -1
- package/dist/coreTypes.d.ts +17 -0
- package/dist/distributed/configManager.d.ts +97 -0
- package/dist/distributed/domainDetector.d.ts +77 -0
- package/dist/distributed/hashPartitioner.d.ts +77 -0
- package/dist/distributed/healthMonitor.d.ts +110 -0
- package/dist/distributed/index.d.ts +10 -0
- package/dist/distributed/operationalModes.d.ts +104 -0
- package/dist/types/distributedTypes.d.ts +197 -0
- package/dist/types/distributedTypes.d.ts.map +1 -0
- package/dist/unified.js +2099 -12
- package/dist/unified.min.js +991 -991
- package/dist/utils/cacheAutoConfig.d.ts +63 -0
- package/dist/utils/cacheAutoConfig.d.ts.map +1 -0
- package/dist/utils/crypto.d.ts +25 -0
- package/dist/utils/crypto.d.ts.map +1 -0
- package/dist/utils/searchCache.d.ts +93 -0
- package/dist/utils/searchCache.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/brainyData.d.ts
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
* Main class that provides the vector database functionality
|
|
4
4
|
*/
|
|
5
5
|
import { HNSWOptimizedConfig } from './hnsw/hnswIndexOptimized.js';
|
|
6
|
-
import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
|
|
6
|
+
import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, SearchCursor, PaginatedSearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
|
|
7
7
|
import { NounType, VerbType } from './types/graphTypes.js';
|
|
8
8
|
import { WebSocketConnection } from './types/augmentations.js';
|
|
9
9
|
import { BrainyDataInterface } from './types/brainyDataInterface.js';
|
|
10
|
+
import { DistributedConfig } from './types/distributedTypes.js';
|
|
11
|
+
import { SearchCacheConfig } from './utils/searchCache.js';
|
|
10
12
|
export interface BrainyDataConfig {
|
|
11
13
|
/**
|
|
12
14
|
* HNSW index configuration
|
|
@@ -121,6 +123,11 @@ export interface BrainyDataConfig {
|
|
|
121
123
|
*/
|
|
122
124
|
verbose?: boolean;
|
|
123
125
|
};
|
|
126
|
+
/**
|
|
127
|
+
* Search result caching configuration
|
|
128
|
+
* Improves performance for repeated queries
|
|
129
|
+
*/
|
|
130
|
+
searchCache?: SearchCacheConfig;
|
|
124
131
|
/**
|
|
125
132
|
* Timeout configuration for async operations
|
|
126
133
|
* Controls how long operations wait before timing out
|
|
@@ -195,6 +202,11 @@ export interface BrainyDataConfig {
|
|
|
195
202
|
*/
|
|
196
203
|
updateIndex?: boolean;
|
|
197
204
|
};
|
|
205
|
+
/**
|
|
206
|
+
* Distributed mode configuration
|
|
207
|
+
* Enables coordination across multiple Brainy instances
|
|
208
|
+
*/
|
|
209
|
+
distributed?: DistributedConfig | boolean;
|
|
198
210
|
/**
|
|
199
211
|
* Cache configuration for optimizing search performance
|
|
200
212
|
* Controls how the system caches data for faster access
|
|
@@ -279,6 +291,8 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
279
291
|
private _dimensions;
|
|
280
292
|
private loggingConfig;
|
|
281
293
|
private defaultService;
|
|
294
|
+
private searchCache;
|
|
295
|
+
private cacheAutoConfigurator;
|
|
282
296
|
private timeoutConfig;
|
|
283
297
|
private retryConfig;
|
|
284
298
|
private cacheConfig;
|
|
@@ -289,6 +303,12 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
289
303
|
private remoteServerConfig;
|
|
290
304
|
private serverSearchConduit;
|
|
291
305
|
private serverConnection;
|
|
306
|
+
private distributedConfig;
|
|
307
|
+
private configManager;
|
|
308
|
+
private partitioner;
|
|
309
|
+
private operationalMode;
|
|
310
|
+
private domainDetector;
|
|
311
|
+
private healthMonitor;
|
|
292
312
|
/**
|
|
293
313
|
* Get the vector dimensions
|
|
294
314
|
*/
|
|
@@ -376,6 +396,20 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
376
396
|
* Loads existing data from storage if available
|
|
377
397
|
*/
|
|
378
398
|
init(): Promise<void>;
|
|
399
|
+
/**
|
|
400
|
+
* Initialize distributed mode
|
|
401
|
+
* Sets up configuration management, partitioning, and operational modes
|
|
402
|
+
*/
|
|
403
|
+
private initializeDistributedMode;
|
|
404
|
+
/**
|
|
405
|
+
* Handle distributed configuration updates
|
|
406
|
+
*/
|
|
407
|
+
private handleDistributedConfigUpdate;
|
|
408
|
+
/**
|
|
409
|
+
* Get distributed health status
|
|
410
|
+
* @returns Health status if distributed mode is enabled
|
|
411
|
+
*/
|
|
412
|
+
getHealthStatus(): any;
|
|
379
413
|
/**
|
|
380
414
|
* Connect to a remote Brainy server for search operations
|
|
381
415
|
* @param serverUrl WebSocket URL of the remote Brainy server
|
|
@@ -475,6 +509,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
475
509
|
searchByNounTypes(queryVectorOrData: Vector | any, k?: number, nounTypes?: string[] | null, options?: {
|
|
476
510
|
forceEmbed?: boolean;
|
|
477
511
|
service?: string;
|
|
512
|
+
offset?: number;
|
|
478
513
|
}): Promise<SearchResult<T>[]>;
|
|
479
514
|
/**
|
|
480
515
|
* Search for similar vectors
|
|
@@ -494,7 +529,31 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
494
529
|
verbDirection?: 'outgoing' | 'incoming' | 'both';
|
|
495
530
|
service?: string;
|
|
496
531
|
searchField?: string;
|
|
532
|
+
filter?: {
|
|
533
|
+
domain?: string;
|
|
534
|
+
};
|
|
535
|
+
offset?: number;
|
|
536
|
+
skipCache?: boolean;
|
|
497
537
|
}): Promise<SearchResult<T>[]>;
|
|
538
|
+
/**
|
|
539
|
+
* Search with cursor-based pagination for better performance on large datasets
|
|
540
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
541
|
+
* @param k Number of results to return
|
|
542
|
+
* @param options Additional options including cursor for pagination
|
|
543
|
+
* @returns Paginated search results with cursor for next page
|
|
544
|
+
*/
|
|
545
|
+
searchWithCursor(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
546
|
+
forceEmbed?: boolean;
|
|
547
|
+
nounTypes?: string[];
|
|
548
|
+
includeVerbs?: boolean;
|
|
549
|
+
service?: string;
|
|
550
|
+
searchField?: string;
|
|
551
|
+
filter?: {
|
|
552
|
+
domain?: string;
|
|
553
|
+
};
|
|
554
|
+
cursor?: SearchCursor;
|
|
555
|
+
skipCache?: boolean;
|
|
556
|
+
}): Promise<PaginatedSearchResult<T>>;
|
|
498
557
|
/**
|
|
499
558
|
* Search the local database for similar vectors
|
|
500
559
|
* @param queryVectorOrData Query vector or data to search for
|
|
@@ -509,6 +568,11 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
509
568
|
service?: string;
|
|
510
569
|
searchField?: string;
|
|
511
570
|
priorityFields?: string[];
|
|
571
|
+
filter?: {
|
|
572
|
+
domain?: string;
|
|
573
|
+
};
|
|
574
|
+
offset?: number;
|
|
575
|
+
skipCache?: boolean;
|
|
512
576
|
}): Promise<SearchResult<T>[]>;
|
|
513
577
|
/**
|
|
514
578
|
* Find entities similar to a given entity ID
|
|
@@ -683,6 +747,32 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
683
747
|
* Get the number of vectors in the database
|
|
684
748
|
*/
|
|
685
749
|
size(): number;
|
|
750
|
+
/**
|
|
751
|
+
* Get search cache statistics for performance monitoring
|
|
752
|
+
* @returns Cache statistics including hit rate and memory usage
|
|
753
|
+
*/
|
|
754
|
+
getCacheStats(): {
|
|
755
|
+
search: {
|
|
756
|
+
hits: number;
|
|
757
|
+
misses: number;
|
|
758
|
+
evictions: number;
|
|
759
|
+
hitRate: number;
|
|
760
|
+
size: number;
|
|
761
|
+
maxSize: number;
|
|
762
|
+
enabled: boolean;
|
|
763
|
+
};
|
|
764
|
+
searchMemoryUsage: number;
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* Clear search cache manually (useful for testing or memory management)
|
|
768
|
+
*/
|
|
769
|
+
clearCache(): void;
|
|
770
|
+
/**
|
|
771
|
+
* Adapt cache configuration based on current performance metrics
|
|
772
|
+
* This method analyzes usage patterns and automatically optimizes cache settings
|
|
773
|
+
* @private
|
|
774
|
+
*/
|
|
775
|
+
private adaptCacheConfiguration;
|
|
686
776
|
/**
|
|
687
777
|
* Get the number of nouns in the database (excluding verbs)
|
|
688
778
|
* This is used for statistics reporting to match the expected behavior in tests
|
|
@@ -829,6 +919,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
829
919
|
storeResults?: boolean;
|
|
830
920
|
service?: string;
|
|
831
921
|
searchField?: string;
|
|
922
|
+
offset?: number;
|
|
832
923
|
}): Promise<SearchResult<T>[]>;
|
|
833
924
|
/**
|
|
834
925
|
* Search both local and remote Brainy instances, combining the results
|
|
@@ -844,6 +935,7 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
844
935
|
localFirst?: boolean;
|
|
845
936
|
service?: string;
|
|
846
937
|
searchField?: string;
|
|
938
|
+
offset?: number;
|
|
847
939
|
}): Promise<SearchResult<T>[]>;
|
|
848
940
|
/**
|
|
849
941
|
* Check if the instance is connected to a remote server
|
|
@@ -994,5 +1086,10 @@ export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
|
994
1086
|
includeVerbs?: boolean;
|
|
995
1087
|
searchMode?: 'local' | 'remote' | 'combined';
|
|
996
1088
|
}): Promise<SearchResult<T>[]>;
|
|
1089
|
+
/**
|
|
1090
|
+
* Cleanup distributed resources
|
|
1091
|
+
* Should be called when shutting down the instance
|
|
1092
|
+
*/
|
|
1093
|
+
cleanup(): Promise<void>;
|
|
997
1094
|
}
|
|
998
1095
|
export { euclideanDistance, cosineDistance, manhattanDistance, dotProductDistance } from './utils/index.js';
|
package/dist/coreTypes.d.ts
CHANGED
|
@@ -22,6 +22,23 @@ export interface SearchResult<T = any> {
|
|
|
22
22
|
vector: Vector;
|
|
23
23
|
metadata?: T;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Cursor for pagination through search results
|
|
27
|
+
*/
|
|
28
|
+
export interface SearchCursor {
|
|
29
|
+
lastId: string;
|
|
30
|
+
lastScore: number;
|
|
31
|
+
position: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Paginated search result with cursor support
|
|
35
|
+
*/
|
|
36
|
+
export interface PaginatedSearchResult<T = any> {
|
|
37
|
+
results: SearchResult<T>[];
|
|
38
|
+
cursor?: SearchCursor;
|
|
39
|
+
hasMore: boolean;
|
|
40
|
+
totalEstimate?: number;
|
|
41
|
+
}
|
|
25
42
|
/**
|
|
26
43
|
* Distance function for comparing vectors
|
|
27
44
|
*/
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed Configuration Manager
|
|
3
|
+
* Manages shared configuration in S3 for distributed Brainy instances
|
|
4
|
+
*/
|
|
5
|
+
import { DistributedConfig, SharedConfig, InstanceInfo, InstanceRole } from '../types/distributedTypes.js';
|
|
6
|
+
import { StorageAdapter } from '../coreTypes.js';
|
|
7
|
+
export declare class DistributedConfigManager {
|
|
8
|
+
private config;
|
|
9
|
+
private instanceId;
|
|
10
|
+
private role;
|
|
11
|
+
private configPath;
|
|
12
|
+
private heartbeatInterval;
|
|
13
|
+
private configCheckInterval;
|
|
14
|
+
private instanceTimeout;
|
|
15
|
+
private storage;
|
|
16
|
+
private heartbeatTimer?;
|
|
17
|
+
private configWatchTimer?;
|
|
18
|
+
private lastConfigVersion;
|
|
19
|
+
private onConfigUpdate?;
|
|
20
|
+
constructor(storage: StorageAdapter, distributedConfig?: DistributedConfig, brainyMode?: {
|
|
21
|
+
readOnly?: boolean;
|
|
22
|
+
writeOnly?: boolean;
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Initialize the distributed configuration
|
|
26
|
+
*/
|
|
27
|
+
initialize(): Promise<SharedConfig>;
|
|
28
|
+
/**
|
|
29
|
+
* Load existing config or create new one
|
|
30
|
+
*/
|
|
31
|
+
private loadOrCreateConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Determine role based on configuration
|
|
34
|
+
* IMPORTANT: Role must be explicitly set - no automatic assignment based on order
|
|
35
|
+
*/
|
|
36
|
+
private determineRole;
|
|
37
|
+
/**
|
|
38
|
+
* Check if an instance is still alive
|
|
39
|
+
*/
|
|
40
|
+
private isInstanceAlive;
|
|
41
|
+
/**
|
|
42
|
+
* Register this instance in the shared config
|
|
43
|
+
*/
|
|
44
|
+
private registerInstance;
|
|
45
|
+
/**
|
|
46
|
+
* Save configuration with version increment
|
|
47
|
+
*/
|
|
48
|
+
private saveConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Start heartbeat to keep instance alive in config
|
|
51
|
+
*/
|
|
52
|
+
private startHeartbeat;
|
|
53
|
+
/**
|
|
54
|
+
* Update heartbeat and clean stale instances
|
|
55
|
+
*/
|
|
56
|
+
private updateHeartbeat;
|
|
57
|
+
/**
|
|
58
|
+
* Start watching for config changes
|
|
59
|
+
*/
|
|
60
|
+
private startConfigWatch;
|
|
61
|
+
/**
|
|
62
|
+
* Check for configuration updates
|
|
63
|
+
*/
|
|
64
|
+
private checkForConfigUpdates;
|
|
65
|
+
/**
|
|
66
|
+
* Load configuration from storage
|
|
67
|
+
*/
|
|
68
|
+
private loadConfig;
|
|
69
|
+
/**
|
|
70
|
+
* Get current configuration
|
|
71
|
+
*/
|
|
72
|
+
getConfig(): SharedConfig | null;
|
|
73
|
+
/**
|
|
74
|
+
* Get instance role
|
|
75
|
+
*/
|
|
76
|
+
getRole(): InstanceRole;
|
|
77
|
+
/**
|
|
78
|
+
* Get instance ID
|
|
79
|
+
*/
|
|
80
|
+
getInstanceId(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Set config update callback
|
|
83
|
+
*/
|
|
84
|
+
setOnConfigUpdate(callback: (config: SharedConfig) => void): void;
|
|
85
|
+
/**
|
|
86
|
+
* Get all active instances of a specific role
|
|
87
|
+
*/
|
|
88
|
+
getInstancesByRole(role: InstanceRole): InstanceInfo[];
|
|
89
|
+
/**
|
|
90
|
+
* Update instance metrics
|
|
91
|
+
*/
|
|
92
|
+
updateMetrics(metrics: Partial<InstanceInfo['metrics']>): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Cleanup resources
|
|
95
|
+
*/
|
|
96
|
+
cleanup(): Promise<void>;
|
|
97
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domain Detector
|
|
3
|
+
* Automatically detects and manages data domains for logical separation
|
|
4
|
+
*/
|
|
5
|
+
import { DomainMetadata } from '../types/distributedTypes.js';
|
|
6
|
+
export interface DomainPattern {
|
|
7
|
+
domain: string;
|
|
8
|
+
patterns: {
|
|
9
|
+
fields?: string[];
|
|
10
|
+
keywords?: string[];
|
|
11
|
+
regex?: RegExp;
|
|
12
|
+
};
|
|
13
|
+
priority?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare class DomainDetector {
|
|
16
|
+
private domainPatterns;
|
|
17
|
+
private customPatterns;
|
|
18
|
+
private domainStats;
|
|
19
|
+
/**
|
|
20
|
+
* Detect domain from data object
|
|
21
|
+
* @param data - The data object to analyze
|
|
22
|
+
* @returns The detected domain and metadata
|
|
23
|
+
*/
|
|
24
|
+
detectDomain(data: any): DomainMetadata;
|
|
25
|
+
/**
|
|
26
|
+
* Score a data object against a domain pattern
|
|
27
|
+
*/
|
|
28
|
+
private scorePattern;
|
|
29
|
+
/**
|
|
30
|
+
* Extract domain-specific metadata
|
|
31
|
+
*/
|
|
32
|
+
private extractDomainMetadata;
|
|
33
|
+
/**
|
|
34
|
+
* Calculate detection confidence
|
|
35
|
+
*/
|
|
36
|
+
private calculateConfidence;
|
|
37
|
+
/**
|
|
38
|
+
* Categorize price ranges
|
|
39
|
+
*/
|
|
40
|
+
private getPriceRange;
|
|
41
|
+
/**
|
|
42
|
+
* Categorize customer value
|
|
43
|
+
*/
|
|
44
|
+
private getValueCategory;
|
|
45
|
+
/**
|
|
46
|
+
* Categorize amount ranges
|
|
47
|
+
*/
|
|
48
|
+
private getAmountRange;
|
|
49
|
+
/**
|
|
50
|
+
* Add custom domain pattern
|
|
51
|
+
* @param pattern - Custom domain pattern to add
|
|
52
|
+
*/
|
|
53
|
+
addCustomPattern(pattern: DomainPattern): void;
|
|
54
|
+
/**
|
|
55
|
+
* Remove custom domain pattern
|
|
56
|
+
* @param domain - Domain to remove pattern for
|
|
57
|
+
*/
|
|
58
|
+
removeCustomPattern(domain: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* Update domain statistics
|
|
61
|
+
*/
|
|
62
|
+
private updateStats;
|
|
63
|
+
/**
|
|
64
|
+
* Get domain statistics
|
|
65
|
+
* @returns Map of domain to count
|
|
66
|
+
*/
|
|
67
|
+
getDomainStats(): Map<string, number>;
|
|
68
|
+
/**
|
|
69
|
+
* Clear domain statistics
|
|
70
|
+
*/
|
|
71
|
+
clearStats(): void;
|
|
72
|
+
/**
|
|
73
|
+
* Get all configured domains
|
|
74
|
+
* @returns Array of domain names
|
|
75
|
+
*/
|
|
76
|
+
getConfiguredDomains(): string[];
|
|
77
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash-based Partitioner
|
|
3
|
+
* Provides deterministic partitioning for distributed writes
|
|
4
|
+
*/
|
|
5
|
+
import { SharedConfig } from '../types/distributedTypes.js';
|
|
6
|
+
export declare class HashPartitioner {
|
|
7
|
+
private partitionCount;
|
|
8
|
+
private partitionPrefix;
|
|
9
|
+
constructor(config: SharedConfig);
|
|
10
|
+
/**
|
|
11
|
+
* Get partition for a given vector ID using deterministic hashing
|
|
12
|
+
* @param vectorId - The unique identifier of the vector
|
|
13
|
+
* @returns The partition path
|
|
14
|
+
*/
|
|
15
|
+
getPartition(vectorId: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Get partition with domain metadata (domain stored as metadata, not in path)
|
|
18
|
+
* @param vectorId - The unique identifier of the vector
|
|
19
|
+
* @param domain - The domain identifier (for metadata only)
|
|
20
|
+
* @returns The partition path
|
|
21
|
+
*/
|
|
22
|
+
getPartitionWithDomain(vectorId: string, domain?: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Get all partition paths
|
|
25
|
+
* @returns Array of all partition paths
|
|
26
|
+
*/
|
|
27
|
+
getAllPartitions(): string[];
|
|
28
|
+
/**
|
|
29
|
+
* Get partition index from partition path
|
|
30
|
+
* @param partitionPath - The partition path
|
|
31
|
+
* @returns The partition index
|
|
32
|
+
*/
|
|
33
|
+
getPartitionIndex(partitionPath: string): number;
|
|
34
|
+
/**
|
|
35
|
+
* Hash a string to a number for consistent partitioning
|
|
36
|
+
* @param str - The string to hash
|
|
37
|
+
* @returns A positive integer hash
|
|
38
|
+
*/
|
|
39
|
+
private hashString;
|
|
40
|
+
/**
|
|
41
|
+
* Get partitions for batch operations
|
|
42
|
+
* Groups vector IDs by their target partition
|
|
43
|
+
* @param vectorIds - Array of vector IDs
|
|
44
|
+
* @returns Map of partition to vector IDs
|
|
45
|
+
*/
|
|
46
|
+
getPartitionsForBatch(vectorIds: string[]): Map<string, string[]>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Affinity-based Partitioner
|
|
50
|
+
* Extends HashPartitioner to prefer certain partitions for a writer
|
|
51
|
+
* while maintaining correctness
|
|
52
|
+
*/
|
|
53
|
+
export declare class AffinityPartitioner extends HashPartitioner {
|
|
54
|
+
private preferredPartitions;
|
|
55
|
+
private instanceId;
|
|
56
|
+
constructor(config: SharedConfig, instanceId: string);
|
|
57
|
+
/**
|
|
58
|
+
* Calculate preferred partitions for this instance
|
|
59
|
+
*/
|
|
60
|
+
private calculatePreferredPartitions;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a partition is preferred for this instance
|
|
63
|
+
* @param partitionPath - The partition path
|
|
64
|
+
* @returns Whether this partition is preferred
|
|
65
|
+
*/
|
|
66
|
+
isPreferredPartition(partitionPath: string): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Get all preferred partitions for this instance
|
|
69
|
+
* @returns Array of preferred partition paths
|
|
70
|
+
*/
|
|
71
|
+
getPreferredPartitions(): string[];
|
|
72
|
+
/**
|
|
73
|
+
* Update preferred partitions based on new config
|
|
74
|
+
* @param config - The updated shared configuration
|
|
75
|
+
*/
|
|
76
|
+
updatePreferences(config: SharedConfig): void;
|
|
77
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Health Monitor
|
|
3
|
+
* Monitors and reports instance health in distributed deployments
|
|
4
|
+
*/
|
|
5
|
+
import { DistributedConfigManager } from './configManager.js';
|
|
6
|
+
export interface HealthMetrics {
|
|
7
|
+
vectorCount: number;
|
|
8
|
+
cacheHitRate: number;
|
|
9
|
+
memoryUsage: number;
|
|
10
|
+
cpuUsage?: number;
|
|
11
|
+
requestsPerSecond?: number;
|
|
12
|
+
averageLatency?: number;
|
|
13
|
+
errorRate?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface HealthStatus {
|
|
16
|
+
status: 'healthy' | 'degraded' | 'unhealthy';
|
|
17
|
+
instanceId: string;
|
|
18
|
+
role: string;
|
|
19
|
+
uptime: number;
|
|
20
|
+
lastCheck: string;
|
|
21
|
+
metrics: HealthMetrics;
|
|
22
|
+
warnings?: string[];
|
|
23
|
+
errors?: string[];
|
|
24
|
+
}
|
|
25
|
+
export declare class HealthMonitor {
|
|
26
|
+
private configManager;
|
|
27
|
+
private startTime;
|
|
28
|
+
private requestCount;
|
|
29
|
+
private errorCount;
|
|
30
|
+
private totalLatency;
|
|
31
|
+
private cacheHits;
|
|
32
|
+
private cacheMisses;
|
|
33
|
+
private vectorCount;
|
|
34
|
+
private checkInterval;
|
|
35
|
+
private healthCheckTimer?;
|
|
36
|
+
private metricsWindow;
|
|
37
|
+
private latencyWindow;
|
|
38
|
+
private windowSize;
|
|
39
|
+
constructor(configManager: DistributedConfigManager);
|
|
40
|
+
/**
|
|
41
|
+
* Start health monitoring
|
|
42
|
+
*/
|
|
43
|
+
start(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Stop health monitoring
|
|
46
|
+
*/
|
|
47
|
+
stop(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Update health status and metrics
|
|
50
|
+
*/
|
|
51
|
+
private updateHealth;
|
|
52
|
+
/**
|
|
53
|
+
* Collect current metrics
|
|
54
|
+
*/
|
|
55
|
+
private collectMetrics;
|
|
56
|
+
/**
|
|
57
|
+
* Calculate cache hit rate
|
|
58
|
+
*/
|
|
59
|
+
private calculateCacheHitRate;
|
|
60
|
+
/**
|
|
61
|
+
* Calculate requests per second
|
|
62
|
+
*/
|
|
63
|
+
private calculateRPS;
|
|
64
|
+
/**
|
|
65
|
+
* Calculate average latency
|
|
66
|
+
*/
|
|
67
|
+
private calculateAverageLatency;
|
|
68
|
+
/**
|
|
69
|
+
* Calculate error rate
|
|
70
|
+
*/
|
|
71
|
+
private calculateErrorRate;
|
|
72
|
+
/**
|
|
73
|
+
* Get CPU usage (simplified)
|
|
74
|
+
*/
|
|
75
|
+
private getCPUUsage;
|
|
76
|
+
/**
|
|
77
|
+
* Clean old entries from sliding windows
|
|
78
|
+
*/
|
|
79
|
+
private cleanWindows;
|
|
80
|
+
/**
|
|
81
|
+
* Record a request
|
|
82
|
+
* @param latency - Request latency in milliseconds
|
|
83
|
+
* @param error - Whether the request resulted in an error
|
|
84
|
+
*/
|
|
85
|
+
recordRequest(latency: number, error?: boolean): void;
|
|
86
|
+
/**
|
|
87
|
+
* Record cache access
|
|
88
|
+
* @param hit - Whether it was a cache hit
|
|
89
|
+
*/
|
|
90
|
+
recordCacheAccess(hit: boolean): void;
|
|
91
|
+
/**
|
|
92
|
+
* Update vector count
|
|
93
|
+
* @param count - New vector count
|
|
94
|
+
*/
|
|
95
|
+
updateVectorCount(count: number): void;
|
|
96
|
+
/**
|
|
97
|
+
* Get current health status
|
|
98
|
+
* @returns Health status object
|
|
99
|
+
*/
|
|
100
|
+
getHealthStatus(): HealthStatus;
|
|
101
|
+
/**
|
|
102
|
+
* Get health check endpoint data
|
|
103
|
+
* @returns JSON-serializable health data
|
|
104
|
+
*/
|
|
105
|
+
getHealthEndpointData(): Record<string, any>;
|
|
106
|
+
/**
|
|
107
|
+
* Reset metrics (useful for testing)
|
|
108
|
+
*/
|
|
109
|
+
resetMetrics(): void;
|
|
110
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Distributed module exports
|
|
3
|
+
*/
|
|
4
|
+
export { DistributedConfigManager } from './configManager.js';
|
|
5
|
+
export { HashPartitioner, AffinityPartitioner } from './hashPartitioner.js';
|
|
6
|
+
export { BaseOperationalMode, ReaderMode, WriterMode, HybridMode, OperationalModeFactory } from './operationalModes.js';
|
|
7
|
+
export { DomainDetector } from './domainDetector.js';
|
|
8
|
+
export { HealthMonitor } from './healthMonitor.js';
|
|
9
|
+
export type { HealthMetrics, HealthStatus } from './healthMonitor.js';
|
|
10
|
+
export type { DomainPattern } from './domainDetector.js';
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Operational Modes for Distributed Brainy
|
|
3
|
+
* Defines different modes with optimized caching strategies
|
|
4
|
+
*/
|
|
5
|
+
import { OperationalMode, CacheStrategy, InstanceRole } from '../types/distributedTypes.js';
|
|
6
|
+
/**
|
|
7
|
+
* Base operational mode
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class BaseOperationalMode implements OperationalMode {
|
|
10
|
+
abstract canRead: boolean;
|
|
11
|
+
abstract canWrite: boolean;
|
|
12
|
+
abstract canDelete: boolean;
|
|
13
|
+
abstract cacheStrategy: CacheStrategy;
|
|
14
|
+
/**
|
|
15
|
+
* Validate operation is allowed in this mode
|
|
16
|
+
*/
|
|
17
|
+
validateOperation(operation: 'read' | 'write' | 'delete'): void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Read-only mode optimized for query performance
|
|
21
|
+
*/
|
|
22
|
+
export declare class ReaderMode extends BaseOperationalMode {
|
|
23
|
+
canRead: boolean;
|
|
24
|
+
canWrite: boolean;
|
|
25
|
+
canDelete: boolean;
|
|
26
|
+
cacheStrategy: CacheStrategy;
|
|
27
|
+
/**
|
|
28
|
+
* Get optimized cache configuration for readers
|
|
29
|
+
*/
|
|
30
|
+
getCacheConfig(): {
|
|
31
|
+
hotCacheMaxSize: number;
|
|
32
|
+
hotCacheEvictionThreshold: number;
|
|
33
|
+
warmCacheTTL: number;
|
|
34
|
+
batchSize: number;
|
|
35
|
+
autoTune: boolean;
|
|
36
|
+
autoTuneInterval: number;
|
|
37
|
+
readOnly: boolean;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Write-only mode optimized for ingestion
|
|
42
|
+
*/
|
|
43
|
+
export declare class WriterMode extends BaseOperationalMode {
|
|
44
|
+
canRead: boolean;
|
|
45
|
+
canWrite: boolean;
|
|
46
|
+
canDelete: boolean;
|
|
47
|
+
cacheStrategy: CacheStrategy;
|
|
48
|
+
/**
|
|
49
|
+
* Get optimized cache configuration for writers
|
|
50
|
+
*/
|
|
51
|
+
getCacheConfig(): {
|
|
52
|
+
hotCacheMaxSize: number;
|
|
53
|
+
hotCacheEvictionThreshold: number;
|
|
54
|
+
warmCacheTTL: number;
|
|
55
|
+
batchSize: number;
|
|
56
|
+
autoTune: boolean;
|
|
57
|
+
writeOnly: boolean;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Hybrid mode that can both read and write
|
|
62
|
+
*/
|
|
63
|
+
export declare class HybridMode extends BaseOperationalMode {
|
|
64
|
+
canRead: boolean;
|
|
65
|
+
canWrite: boolean;
|
|
66
|
+
canDelete: boolean;
|
|
67
|
+
cacheStrategy: CacheStrategy;
|
|
68
|
+
private readWriteRatio;
|
|
69
|
+
/**
|
|
70
|
+
* Get balanced cache configuration
|
|
71
|
+
*/
|
|
72
|
+
getCacheConfig(): {
|
|
73
|
+
hotCacheMaxSize: number;
|
|
74
|
+
hotCacheEvictionThreshold: number;
|
|
75
|
+
warmCacheTTL: number;
|
|
76
|
+
batchSize: number;
|
|
77
|
+
autoTune: boolean;
|
|
78
|
+
autoTuneInterval: number;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Update cache strategy based on workload
|
|
82
|
+
* @param readCount - Number of recent reads
|
|
83
|
+
* @param writeCount - Number of recent writes
|
|
84
|
+
*/
|
|
85
|
+
updateWorkloadBalance(readCount: number, writeCount: number): void;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Factory for creating operational modes
|
|
89
|
+
*/
|
|
90
|
+
export declare class OperationalModeFactory {
|
|
91
|
+
/**
|
|
92
|
+
* Create operational mode based on role
|
|
93
|
+
* @param role - The instance role
|
|
94
|
+
* @returns The appropriate operational mode
|
|
95
|
+
*/
|
|
96
|
+
static createMode(role: InstanceRole): BaseOperationalMode;
|
|
97
|
+
/**
|
|
98
|
+
* Create mode with custom cache strategy
|
|
99
|
+
* @param role - The instance role
|
|
100
|
+
* @param customStrategy - Custom cache strategy overrides
|
|
101
|
+
* @returns The operational mode with custom strategy
|
|
102
|
+
*/
|
|
103
|
+
static createModeWithStrategy(role: InstanceRole, customStrategy: Partial<CacheStrategy>): BaseOperationalMode;
|
|
104
|
+
}
|