@soulcraft/brainy 4.10.3 → 4.10.4
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/dist/brainy.js +50 -14
- package/dist/import/ImportCoordinator.js +243 -173
- package/dist/storage/adapters/azureBlobStorage.d.ts +15 -1
- package/dist/storage/adapters/azureBlobStorage.js +25 -0
- package/dist/storage/adapters/baseStorageAdapter.d.ts +13 -0
- package/dist/storage/adapters/baseStorageAdapter.js +26 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +14 -1
- package/dist/storage/adapters/fileSystemStorage.js +24 -0
- package/dist/storage/adapters/gcsStorage.d.ts +16 -1
- package/dist/storage/adapters/gcsStorage.js +26 -0
- package/dist/storage/adapters/memoryStorage.d.ts +14 -1
- package/dist/storage/adapters/memoryStorage.js +24 -0
- package/dist/storage/adapters/opfsStorage.d.ts +14 -1
- package/dist/storage/adapters/opfsStorage.js +24 -0
- package/dist/storage/adapters/r2Storage.d.ts +18 -1
- package/dist/storage/adapters/r2Storage.js +28 -0
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +15 -1
- package/dist/storage/adapters/s3CompatibleStorage.js +25 -0
- package/dist/storage/baseStorage.d.ts +24 -0
- package/dist/utils/adaptiveBackpressure.d.ts +17 -10
- package/dist/utils/adaptiveBackpressure.js +98 -48
- package/package.json +1 -1
|
@@ -32,12 +32,24 @@ export class AdaptiveBackpressure {
|
|
|
32
32
|
};
|
|
33
33
|
// Historical patterns for learning
|
|
34
34
|
this.patterns = [];
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
this.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
// Separate circuit breakers for read vs write operations
|
|
36
|
+
// This allows reads to continue even when writes are throttled
|
|
37
|
+
this.circuits = {
|
|
38
|
+
read: {
|
|
39
|
+
state: 'closed',
|
|
40
|
+
failures: 0,
|
|
41
|
+
openTime: 0,
|
|
42
|
+
threshold: 10, // More lenient for reads
|
|
43
|
+
timeout: 30000
|
|
44
|
+
},
|
|
45
|
+
write: {
|
|
46
|
+
state: 'closed',
|
|
47
|
+
failures: 0,
|
|
48
|
+
openTime: 0,
|
|
49
|
+
threshold: 5, // Stricter for writes
|
|
50
|
+
timeout: 30000
|
|
51
|
+
}
|
|
52
|
+
};
|
|
41
53
|
// Performance tracking
|
|
42
54
|
this.operationTimes = new Map();
|
|
43
55
|
this.completedOps = [];
|
|
@@ -46,11 +58,22 @@ export class AdaptiveBackpressure {
|
|
|
46
58
|
}
|
|
47
59
|
/**
|
|
48
60
|
* Request permission to proceed with an operation
|
|
61
|
+
* @param operationId Unique ID for this operation
|
|
62
|
+
* @param priority Priority level (higher = more important)
|
|
63
|
+
* @param operationType Type of operation (read or write) for circuit breaker isolation
|
|
49
64
|
*/
|
|
50
|
-
async requestPermission(operationId, priority = 1) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
65
|
+
async requestPermission(operationId, priority = 1, operationType = 'write') {
|
|
66
|
+
const circuit = this.circuits[operationType];
|
|
67
|
+
// Check circuit breaker for this operation type
|
|
68
|
+
if (this.isCircuitOpen(circuit)) {
|
|
69
|
+
// KEY: Allow reads even if write circuit is open
|
|
70
|
+
if (operationType === 'read' && this.circuits.write.state === 'open') {
|
|
71
|
+
// Write circuit is open but read circuit is fine - allow read
|
|
72
|
+
this.activeOperations.add(operationId);
|
|
73
|
+
this.operationTimes.set(operationId, Date.now());
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`Circuit breaker is open for ${operationType} operations - system is recovering`);
|
|
54
77
|
}
|
|
55
78
|
// Fast path for low load
|
|
56
79
|
if (this.activeOperations.size < this.maxConcurrent * 0.5 && this.queue.length === 0) {
|
|
@@ -84,8 +107,11 @@ export class AdaptiveBackpressure {
|
|
|
84
107
|
}
|
|
85
108
|
/**
|
|
86
109
|
* Release permission after operation completes
|
|
110
|
+
* @param operationId Unique ID for this operation
|
|
111
|
+
* @param success Whether the operation succeeded
|
|
112
|
+
* @param operationType Type of operation (read or write) for circuit breaker tracking
|
|
87
113
|
*/
|
|
88
|
-
releasePermission(operationId, success = true) {
|
|
114
|
+
releasePermission(operationId, success = true, operationType = 'write') {
|
|
89
115
|
// Remove from active operations
|
|
90
116
|
this.activeOperations.delete(operationId);
|
|
91
117
|
// Track completion time
|
|
@@ -99,19 +125,20 @@ export class AdaptiveBackpressure {
|
|
|
99
125
|
this.completedOps = this.completedOps.slice(-500);
|
|
100
126
|
}
|
|
101
127
|
}
|
|
102
|
-
// Track errors for circuit breaker
|
|
128
|
+
// Track errors for circuit breaker per operation type
|
|
129
|
+
const circuit = this.circuits[operationType];
|
|
103
130
|
if (!success) {
|
|
104
131
|
this.errorOps++;
|
|
105
|
-
|
|
106
|
-
// Check if we should open circuit
|
|
107
|
-
if (
|
|
108
|
-
this.openCircuit();
|
|
132
|
+
circuit.failures++;
|
|
133
|
+
// Check if we should open circuit for this operation type
|
|
134
|
+
if (circuit.failures >= circuit.threshold) {
|
|
135
|
+
this.openCircuit(circuit, operationType);
|
|
109
136
|
}
|
|
110
137
|
}
|
|
111
138
|
else {
|
|
112
139
|
// Reset circuit failures on success
|
|
113
|
-
if (
|
|
114
|
-
this.closeCircuit();
|
|
140
|
+
if (circuit.state === 'half-open') {
|
|
141
|
+
this.closeCircuit(circuit, operationType);
|
|
115
142
|
}
|
|
116
143
|
}
|
|
117
144
|
// Process queue if there are waiting operations
|
|
@@ -129,13 +156,14 @@ export class AdaptiveBackpressure {
|
|
|
129
156
|
this.adaptIfNeeded();
|
|
130
157
|
}
|
|
131
158
|
/**
|
|
132
|
-
* Check if circuit breaker is open
|
|
159
|
+
* Check if circuit breaker is open for a specific operation type
|
|
160
|
+
* @param circuit The circuit to check (read or write)
|
|
133
161
|
*/
|
|
134
|
-
isCircuitOpen() {
|
|
135
|
-
if (
|
|
162
|
+
isCircuitOpen(circuit) {
|
|
163
|
+
if (circuit.state === 'open') {
|
|
136
164
|
// Check if timeout has passed
|
|
137
|
-
if (Date.now() -
|
|
138
|
-
|
|
165
|
+
if (Date.now() - circuit.openTime > circuit.timeout) {
|
|
166
|
+
circuit.state = 'half-open';
|
|
139
167
|
this.logger.info('Circuit breaker entering half-open state');
|
|
140
168
|
return false;
|
|
141
169
|
}
|
|
@@ -144,26 +172,34 @@ export class AdaptiveBackpressure {
|
|
|
144
172
|
return false;
|
|
145
173
|
}
|
|
146
174
|
/**
|
|
147
|
-
* Open the circuit breaker
|
|
175
|
+
* Open the circuit breaker for a specific operation type
|
|
176
|
+
* @param circuit The circuit to open (read or write)
|
|
177
|
+
* @param operationType The operation type name for logging
|
|
148
178
|
*/
|
|
149
|
-
openCircuit() {
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
this.logger.warn(
|
|
154
|
-
// Reduce load immediately
|
|
155
|
-
|
|
179
|
+
openCircuit(circuit, operationType) {
|
|
180
|
+
if (circuit.state !== 'open') {
|
|
181
|
+
circuit.state = 'open';
|
|
182
|
+
circuit.openTime = Date.now();
|
|
183
|
+
this.logger.warn(`Circuit breaker opened for ${operationType} operations due to high error rate`);
|
|
184
|
+
// Reduce load immediately for write operations
|
|
185
|
+
if (operationType === 'write') {
|
|
186
|
+
this.maxConcurrent = Math.max(10, Math.floor(this.maxConcurrent * 0.3));
|
|
187
|
+
}
|
|
156
188
|
}
|
|
157
189
|
}
|
|
158
190
|
/**
|
|
159
|
-
* Close the circuit breaker
|
|
191
|
+
* Close the circuit breaker for a specific operation type
|
|
192
|
+
* @param circuit The circuit to close (read or write)
|
|
193
|
+
* @param operationType The operation type name for logging
|
|
160
194
|
*/
|
|
161
|
-
closeCircuit() {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
this.logger.info(
|
|
165
|
-
// Gradually increase capacity
|
|
166
|
-
|
|
195
|
+
closeCircuit(circuit, operationType) {
|
|
196
|
+
circuit.state = 'closed';
|
|
197
|
+
circuit.failures = 0;
|
|
198
|
+
this.logger.info(`Circuit breaker closed for ${operationType} - system recovered`);
|
|
199
|
+
// Gradually increase capacity for write operations
|
|
200
|
+
if (operationType === 'write') {
|
|
201
|
+
this.maxConcurrent = Math.min(500, Math.floor(this.maxConcurrent * 1.5));
|
|
202
|
+
}
|
|
167
203
|
}
|
|
168
204
|
/**
|
|
169
205
|
* Adapt configuration based on metrics
|
|
@@ -262,13 +298,8 @@ export class AdaptiveBackpressure {
|
|
|
262
298
|
// Allow queue depth to be 10 seconds worth of throughput
|
|
263
299
|
this.config.maxQueueDepth = Math.max(100, Math.min(10000, Math.floor(this.metrics.throughput * 10)));
|
|
264
300
|
}
|
|
265
|
-
//
|
|
266
|
-
|
|
267
|
-
this.circuitThreshold = Math.max(5, this.circuitThreshold - 1);
|
|
268
|
-
}
|
|
269
|
-
else if (this.metrics.errorRate > 0.05 && this.circuitThreshold < 20) {
|
|
270
|
-
this.circuitThreshold = Math.min(20, this.circuitThreshold + 1);
|
|
271
|
-
}
|
|
301
|
+
// Note: Circuit breaker thresholds are now fixed per operation type (read/write)
|
|
302
|
+
// and do not adapt dynamically to maintain predictable behavior
|
|
272
303
|
}
|
|
273
304
|
/**
|
|
274
305
|
* Predict future load based on patterns
|
|
@@ -303,10 +334,24 @@ export class AdaptiveBackpressure {
|
|
|
303
334
|
* Get current configuration and metrics
|
|
304
335
|
*/
|
|
305
336
|
getStatus() {
|
|
337
|
+
// Combined circuit status for backward compatibility
|
|
338
|
+
let circuitStatus = 'closed';
|
|
339
|
+
if (this.circuits.read.state === 'open' && this.circuits.write.state === 'open') {
|
|
340
|
+
circuitStatus = 'open';
|
|
341
|
+
}
|
|
342
|
+
else if (this.circuits.write.state === 'open') {
|
|
343
|
+
circuitStatus = 'write-circuit-open';
|
|
344
|
+
}
|
|
345
|
+
else if (this.circuits.read.state === 'open') {
|
|
346
|
+
circuitStatus = 'read-circuit-open';
|
|
347
|
+
}
|
|
348
|
+
else if (this.circuits.read.state === 'half-open' || this.circuits.write.state === 'half-open') {
|
|
349
|
+
circuitStatus = 'half-open';
|
|
350
|
+
}
|
|
306
351
|
return {
|
|
307
352
|
config: { ...this.config },
|
|
308
353
|
metrics: { ...this.metrics },
|
|
309
|
-
circuit:
|
|
354
|
+
circuit: circuitStatus,
|
|
310
355
|
maxConcurrent: this.maxConcurrent,
|
|
311
356
|
activeOps: this.activeOperations.size,
|
|
312
357
|
queueLength: this.queue.length
|
|
@@ -322,8 +367,13 @@ export class AdaptiveBackpressure {
|
|
|
322
367
|
this.completedOps = [];
|
|
323
368
|
this.errorOps = 0;
|
|
324
369
|
this.patterns = [];
|
|
325
|
-
|
|
326
|
-
this.
|
|
370
|
+
// Reset both circuit breakers
|
|
371
|
+
this.circuits.read.state = 'closed';
|
|
372
|
+
this.circuits.read.failures = 0;
|
|
373
|
+
this.circuits.read.openTime = 0;
|
|
374
|
+
this.circuits.write.state = 'closed';
|
|
375
|
+
this.circuits.write.failures = 0;
|
|
376
|
+
this.circuits.write.openTime = 0;
|
|
327
377
|
this.maxConcurrent = 100;
|
|
328
378
|
this.logger.info('Backpressure system reset to defaults');
|
|
329
379
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soulcraft/brainy",
|
|
3
|
-
"version": "4.10.
|
|
3
|
+
"version": "4.10.4",
|
|
4
4
|
"description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|