bulltrackers-module 1.0.295 → 1.0.297

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.
@@ -1,10 +1,6 @@
1
1
  /**
2
2
  * @fileoverview Executor for "Standard" (per-user) calculations.
3
- * UPDATED: Implements Batch Flushing to prevent OOM on large datasets.
4
- * UPDATED: Implements "Circuit Breaker" to fail fast on high error rates.
5
- * UPDATED: Implements "Adaptive Flushing" based on V8 Heap usage.
6
- * UPDATED: Manages incremental sharding states.
7
- * UPDATED: Implements 'isInitialWrite' flag for robust cleanup.
3
+ * UPDATED: Tracks IO Operations (Reads/Writes) for Cost Analysis.
8
4
  */
9
5
  const { normalizeName } = require('../utils/utils');
10
6
  const { streamPortfolioData, streamHistoryData, getPortfolioPartRefs } = require('../utils/data_loader');
@@ -20,7 +16,7 @@ class StandardExecutor {
20
16
  const dStr = date.toISOString().slice(0, 10);
21
17
  const logger = deps.logger;
22
18
 
23
- // 1. Prepare Yesterdays Data if needed
19
+ // 1. Prepare Yesterdays Data (Counts as Read Ops)
24
20
  const fullRoot = { ...rootData };
25
21
  if (calcs.some(c => c.isHistorical)) {
26
22
  const prev = new Date(date); prev.setUTCDate(prev.getUTCDate() - 1);
@@ -28,22 +24,17 @@ class StandardExecutor {
28
24
  fullRoot.yesterdayPortfolioRefs = await getPortfolioPartRefs(config, deps, prevStr);
29
25
  }
30
26
 
31
- // 2. Initialize Instances
32
27
  const state = {};
33
28
  for (const c of calcs) {
34
29
  try {
35
30
  const inst = new c.class();
36
31
  inst.manifest = c;
37
- // Ensure internal storage exists for flushing
38
32
  inst.results = {};
39
33
  state[normalizeName(c.name)] = inst;
40
34
  logger.log('INFO', `${c.name} calculation running for ${dStr}`);
41
- } catch (e) {
42
- logger.log('WARN', `Failed to init ${c.name}`);
43
- }
35
+ } catch (e) { logger.log('WARN', `Failed to init ${c.name}`); }
44
36
  }
45
37
 
46
- // 3. Stream, Process & Batch Flush
47
38
  return await StandardExecutor.streamAndProcess(dStr, state, passName, config, deps, fullRoot, rootData.portfolioRefs, rootData.historyRefs, fetchedDeps, previousFetchedDeps, skipStatusWrite);
48
39
  }
49
40
 
@@ -54,33 +45,34 @@ class StandardExecutor {
54
45
 
55
46
  if (streamingCalcs.length === 0) return { successUpdates: {}, failureReport: [] };
56
47
 
57
- logger.log('INFO', `[${passName}] Streaming for ${streamingCalcs.length} computations...`);
58
-
48
+ // [NEW] Calculate Total Read Ops for this execution context
49
+ // Each reference in the arrays corresponds to a document fetch
50
+ let totalReadOps = (portfolioRefs?.length || 0) + (historyRefs?.length || 0);
51
+ if (rootData.yesterdayPortfolioRefs) totalReadOps += rootData.yesterdayPortfolioRefs.length;
52
+ // Add +2 for Insights & Social (1 doc each)
53
+ totalReadOps += 2;
54
+
55
+ // Distribute read costs evenly among calculations (approximation)
56
+ const readOpsPerCalc = Math.ceil(totalReadOps / streamingCalcs.length);
57
+
59
58
  const executionStats = {};
60
59
  const shardIndexMap = {};
61
60
  const aggregatedSuccess = {};
62
61
  const aggregatedFailures = [];
63
-
64
- // [NEW] Global Error Tracking for Circuit Breaker
65
62
  const errorStats = { count: 0, total: 0 };
66
63
 
67
64
  Object.keys(state).forEach(name => {
68
65
  executionStats[name] = {
69
- processedUsers: 0,
70
- skippedUsers: 0,
71
- timings: { setup: 0, stream: 0, processing: 0 }
66
+ processedUsers: 0, skippedUsers: 0, timings: { setup: 0, stream: 0, processing: 0 }
72
67
  };
73
- shardIndexMap[name] = 0;
68
+ shardIndexMap[name] = 0;
74
69
  });
75
70
 
76
- // Track if we have performed a flush yet (for cleanup logic)
77
71
  let hasFlushed = false;
78
-
79
- const startSetup = performance.now();
80
72
  const cachedLoader = new CachedDataLoader(config, deps);
73
+ const startSetup = performance.now();
81
74
  await cachedLoader.loadMappings();
82
75
  const setupDuration = performance.now() - startSetup;
83
-
84
76
  Object.keys(executionStats).forEach(name => executionStats[name].timings.setup += setupDuration);
85
77
 
86
78
  const prevDate = new Date(dateStr + 'T00:00:00Z'); prevDate.setUTCDate(prevDate.getUTCDate() - 1);
@@ -93,8 +85,6 @@ class StandardExecutor {
93
85
  const tH_iter = (needsTradingHistory) ? streamHistoryData(config, deps, dateStr, historyRefs) : null;
94
86
 
95
87
  let yP_chunk = {}, tH_chunk = {};
96
-
97
- const MIN_BATCH_SIZE = 1000; // Minimum to process before checking stats
98
88
  let usersSinceLastFlush = 0;
99
89
 
100
90
  try {
@@ -106,52 +96,26 @@ class StandardExecutor {
106
96
  Object.keys(executionStats).forEach(name => executionStats[name].timings.stream += streamDuration);
107
97
 
108
98
  const chunkSize = Object.keys(tP_chunk).length;
109
-
110
99
  const startProcessing = performance.now();
111
100
 
112
- // [UPDATED] Collect execution results (success/failure counts)
113
- const promises = streamingCalcs.map(calc =>
101
+ const batchResults = await Promise.all(streamingCalcs.map(calc =>
114
102
  StandardExecutor.executePerUser(
115
103
  calc, calc.manifest, dateStr, tP_chunk, yP_chunk, tH_chunk,
116
104
  fetchedDeps, previousFetchedDeps, config, deps, cachedLoader,
117
105
  executionStats[normalizeName(calc.manifest.name)]
118
106
  )
119
- );
107
+ ));
120
108
 
121
- const batchResults = await Promise.all(promises);
122
109
  const procDuration = performance.now() - startProcessing;
123
-
124
110
  Object.keys(executionStats).forEach(name => executionStats[name].timings.processing += procDuration);
125
111
 
126
- // [NEW] Update Error Stats
127
- batchResults.forEach(r => {
128
- errorStats.total += (r.success + r.failures);
129
- errorStats.count += r.failures;
130
- });
131
-
132
- // [NEW] Circuit Breaker: Fail fast if error rate > 10% after processing 100+ items
133
- // We check total > 100 to avoid failing on the very first user if they happen to be bad.
134
- if (errorStats.total > 100 && (errorStats.count / errorStats.total) > 0.10) {
135
- const failRate = (errorStats.count / errorStats.total * 100).toFixed(1);
136
- throw new Error(`[Circuit Breaker] High failure rate detected (${failRate}%). Aborting batch to prevent silent data loss.`);
137
- }
112
+ batchResults.forEach(r => { errorStats.total += (r.success + r.failures); errorStats.count += r.failures; });
113
+ if (errorStats.total > 100 && (errorStats.count / errorStats.total) > 0.10) { throw new Error(`[Circuit Breaker] High failure rate detected.`); }
138
114
 
139
115
  usersSinceLastFlush += chunkSize;
140
-
141
- // [NEW] Adaptive Flushing (Memory Pressure Check)
142
116
  const heapStats = v8.getHeapStatistics();
143
- const heapUsedRatio = heapStats.used_heap_size / heapStats.heap_size_limit;
144
- const MEMORY_THRESHOLD = 0.70; // 70% of available RAM
145
- const COUNT_THRESHOLD = 5000;
146
-
147
- if (usersSinceLastFlush >= COUNT_THRESHOLD || heapUsedRatio > MEMORY_THRESHOLD) {
148
- const reason = heapUsedRatio > MEMORY_THRESHOLD ? `MEMORY_PRESSURE (${(heapUsedRatio*100).toFixed(0)}%)` : 'BATCH_LIMIT';
149
-
150
- logger.log('INFO', `[${passName}] 🛁 Flushing buffer after ${usersSinceLastFlush} users. Reason: ${reason}`);
151
-
152
- // [UPDATED] Pass isInitialWrite: true only on the first flush
117
+ if (usersSinceLastFlush >= 5000 || (heapStats.used_heap_size / heapStats.heap_size_limit) > 0.70) {
153
118
  const flushResult = await StandardExecutor.flushBuffer(state, dateStr, passName, config, deps, shardIndexMap, executionStats, 'INTERMEDIATE', true, !hasFlushed);
154
-
155
119
  hasFlushed = true;
156
120
  StandardExecutor.mergeReports(aggregatedSuccess, aggregatedFailures, flushResult);
157
121
  usersSinceLastFlush = 0;
@@ -161,22 +125,23 @@ class StandardExecutor {
161
125
  if (yP_iter && yP_iter.return) await yP_iter.return();
162
126
  if (tH_iter && tH_iter.return) await tH_iter.return();
163
127
  }
164
-
165
- logger.log('INFO', `[${passName}] Streaming complete. Performing final commit.`);
166
- // [UPDATED] If we never flushed in the loop, this is the initial write
128
+
167
129
  const finalResult = await StandardExecutor.flushBuffer(state, dateStr, passName, config, deps, shardIndexMap, executionStats, 'FINAL', skipStatusWrite, !hasFlushed);
168
-
169
130
  StandardExecutor.mergeReports(aggregatedSuccess, aggregatedFailures, finalResult);
170
131
 
132
+ // [NEW] Inject Read Ops into the final report
133
+ Object.values(aggregatedSuccess).forEach(update => {
134
+ if (!update.metrics.io) update.metrics.io = { reads: 0, writes: 0, deletes: 0 };
135
+ update.metrics.io.reads = readOpsPerCalc;
136
+ });
137
+
171
138
  return { successUpdates: aggregatedSuccess, failureReport: aggregatedFailures };
172
139
  }
173
140
 
174
141
  static async flushBuffer(state, dateStr, passName, config, deps, shardIndexMap, executionStats, mode, skipStatusWrite, isInitialWrite = false) {
175
142
  const transformedState = {};
176
-
177
143
  for (const [name, inst] of Object.entries(state)) {
178
144
  const rawResult = inst.results || {};
179
-
180
145
  const firstUser = Object.keys(rawResult)[0];
181
146
  let dataToCommit = rawResult;
182
147
 
@@ -199,43 +164,40 @@ class StandardExecutor {
199
164
  getResult: async () => dataToCommit,
200
165
  _executionStats: executionStats[name]
201
166
  };
202
-
203
- // Clear the memory immediately after preparing the commit
204
167
  inst.results = {};
205
168
  }
206
169
 
207
- // [UPDATED] Pass isInitialWrite to ResultCommitter
208
170
  const result = await commitResults(transformedState, dateStr, passName, config, deps, skipStatusWrite, {
209
- flushMode: mode,
210
- shardIndexes: shardIndexMap,
211
- isInitialWrite: isInitialWrite
171
+ flushMode: mode, shardIndexes: shardIndexMap, isInitialWrite: isInitialWrite
212
172
  });
213
173
 
214
- if (result.shardIndexes) {
215
- Object.assign(shardIndexMap, result.shardIndexes);
216
- }
217
-
174
+ if (result.shardIndexes) Object.assign(shardIndexMap, result.shardIndexes);
218
175
  return result;
219
176
  }
220
177
 
221
178
  static mergeReports(successAcc, failureAcc, newResult) {
222
179
  if (!newResult) return;
223
-
224
180
  for (const [name, update] of Object.entries(newResult.successUpdates)) {
225
181
  if (!successAcc[name]) {
226
182
  successAcc[name] = update;
227
183
  } else {
228
- if (update.metrics?.storage) {
229
- successAcc[name].metrics.storage.sizeBytes += (update.metrics.storage.sizeBytes || 0);
230
- successAcc[name].metrics.storage.keys += (update.metrics.storage.keys || 0);
231
- successAcc[name].metrics.storage.shardCount = Math.max(successAcc[name].metrics.storage.shardCount, update.metrics.storage.shardCount || 1);
232
- }
184
+ // Merge Storage metrics
185
+ successAcc[name].metrics.storage.sizeBytes += (update.metrics.storage.sizeBytes || 0);
186
+ successAcc[name].metrics.storage.keys += (update.metrics.storage.keys || 0);
187
+ successAcc[name].metrics.storage.shardCount = Math.max(successAcc[name].metrics.storage.shardCount, update.metrics.storage.shardCount || 1);
233
188
 
189
+ // [NEW] Merge IO Metrics
190
+ if (update.metrics.io) {
191
+ if (!successAcc[name].metrics.io) successAcc[name].metrics.io = { writes: 0, deletes: 0, reads: 0 };
192
+ successAcc[name].metrics.io.writes += (update.metrics.io.writes || 0);
193
+ successAcc[name].metrics.io.deletes += (update.metrics.io.deletes || 0);
194
+ }
195
+
196
+ // Merge timings
234
197
  if (update.metrics?.execution?.timings) {
235
198
  if (!successAcc[name].metrics.execution) successAcc[name].metrics.execution = { timings: { setup:0, stream:0, processing:0 }};
236
199
  const tDest = successAcc[name].metrics.execution.timings;
237
200
  const tSrc = update.metrics.execution.timings;
238
-
239
201
  tDest.setup += (tSrc.setup || 0);
240
202
  tDest.stream += (tSrc.stream || 0);
241
203
  tDest.processing += (tSrc.processing || 0);
@@ -243,12 +205,9 @@ class StandardExecutor {
243
205
  successAcc[name].hash = update.hash;
244
206
  }
245
207
  }
246
-
247
- if (newResult.failureReport) {
248
- failureAcc.push(...newResult.failureReport);
249
- }
208
+ if (newResult.failureReport) failureAcc.push(...newResult.failureReport);
250
209
  }
251
-
210
+
252
211
  static async executePerUser(calcInstance, metadata, dateStr, portfolioData, yesterdayPortfolioData, historyData, computedDeps, prevDeps, config, deps, loader, stats) {
253
212
  const { logger } = deps;
254
213
  const targetUserType = metadata.userType;
@@ -256,7 +215,6 @@ class StandardExecutor {
256
215
  const insights = metadata.rootDataDependencies?.includes('insights') ? { today: await loader.loadInsights(dateStr) } : null;
257
216
  const SCHEMAS = mathLayer.SCHEMAS;
258
217
 
259
- // [NEW] Track local batch success/failure
260
218
  let chunkSuccess = 0;
261
219
  let chunkFailures = 0;
262
220
 
@@ -2,7 +2,7 @@
2
2
  * FILENAME: computation-system/helpers/computation_worker.js
3
3
  * PURPOSE: Consumes tasks, executes logic, and signals Workflow upon Batch Completion.
4
4
  * UPDATED: Implements IAM Auth for Workflow Callbacks.
5
- * UPDATED: Implements Memory Heartbeat (Flight Recorder) for OOM detection.
5
+ * UPDATED: Implements Peak Memory Heartbeat and Resource Tier tracking.
6
6
  */
7
7
 
8
8
  const { executeDispatchTask } = require('../WorkflowOrchestrator.js');
@@ -11,6 +11,7 @@ const { StructuredLogger } = require('../logger/logger');
11
11
  const { recordRunAttempt } = require('../persistence/RunRecorder');
12
12
  const https = require('https');
13
13
  const { GoogleAuth } = require('google-auth-library');
14
+ const { normalizeName } = require('../utils/utils');
14
15
 
15
16
  let calculationPackage;
16
17
  try { calculationPackage = require('aiden-shared-calculations-unified');
@@ -20,15 +21,19 @@ const calculations = calculationPackage.calculations;
20
21
  const MAX_RETRIES = 3;
21
22
 
22
23
  /**
23
- * [NEW] Helper: Starts a background heartbeat to track memory usage.
24
- * This acts as a "Black Box Recorder". If the worker crashes (OOM),
25
- * the last written value will remain in Firestore for the Dispatcher to analyze.
24
+ * [UPDATED] Heartbeat now returns a closure to get the PEAK memory.
25
+ * This acts as a "Black Box Recorder".
26
26
  */
27
27
  function startMemoryHeartbeat(db, ledgerPath, intervalMs = 2000) {
28
+ let peakRss = 0;
29
+
28
30
  const getMemStats = () => {
29
31
  const mem = process.memoryUsage();
32
+ const rssMB = Math.round(mem.rss / 1024 / 1024);
33
+ if (rssMB > peakRss) peakRss = rssMB;
34
+
30
35
  return {
31
- rssMB: Math.round(mem.rss / 1024 / 1024), // Resident Set Size (OOM Killer Metric)
36
+ rssMB: rssMB,
32
37
  heapUsedMB: Math.round(mem.heapUsed / 1024 / 1024),
33
38
  timestamp: new Date()
34
39
  };
@@ -50,7 +55,10 @@ function startMemoryHeartbeat(db, ledgerPath, intervalMs = 2000) {
50
55
  // Unref so this timer doesn't prevent the process from exiting naturally
51
56
  timer.unref();
52
57
 
53
- return timer;
58
+ return {
59
+ timer,
60
+ getPeak: () => peakRss
61
+ };
54
62
  }
55
63
 
56
64
  /**
@@ -127,7 +135,9 @@ async function handleComputationTask(message, config, dependencies) {
127
135
 
128
136
  if (!data || data.action !== 'RUN_COMPUTATION_DATE') { return; }
129
137
 
130
- const { date, pass, computation, previousCategory, triggerReason, dispatchId, dependencyResultHashes, metaStatePath } = data;
138
+ // [UPDATED] Extract 'resources' from payload (set by Dispatcher)
139
+ const { date, pass, computation, previousCategory, triggerReason, dispatchId, dependencyResultHashes, metaStatePath, resources } = data;
140
+ const resourceTier = resources || 'standard'; // Default to standard
131
141
 
132
142
  if (!date || !pass || !computation) { logger.log('ERROR', `[Worker] Invalid payload.`, data); return; }
133
143
 
@@ -158,7 +168,7 @@ async function handleComputationTask(message, config, dependencies) {
158
168
  } catch (dlqErr) { logger.log('FATAL', `[Worker] Failed to write to DLQ`, dlqErr); }
159
169
  }
160
170
 
161
- logger.log('INFO', `[Worker] 📥 Received Task: ${computation} (${date}) [Attempt ${retryCount}/${MAX_RETRIES}]`);
171
+ logger.log('INFO', `[Worker] 📥 Received Task: ${computation} (${date}) [Attempt ${retryCount}/${MAX_RETRIES}] [Tier: ${resourceTier}]`);
162
172
 
163
173
  // 1. Update Status to IN_PROGRESS & Initialize Telemetry
164
174
  try {
@@ -172,12 +182,13 @@ async function handleComputationTask(message, config, dependencies) {
172
182
  } catch (leaseErr) {}
173
183
 
174
184
  // 2. START HEARTBEAT (The Flight Recorder)
175
- const heartbeatTimer = startMemoryHeartbeat(db, ledgerPath, 2000);
185
+ // [UPDATED] Using new logic to track peak
186
+ const heartbeatControl = startMemoryHeartbeat(db, ledgerPath, 2000);
176
187
 
177
188
  let computationManifest;
178
189
  try { computationManifest = getManifest(config.activeProductLines || [], calculations, runDependencies);
179
190
  } catch (manifestError) {
180
- clearInterval(heartbeatTimer); // Stop if we fail early
191
+ clearInterval(heartbeatControl.timer); // Stop if we fail early
181
192
  logger.log('FATAL', `[Worker] Failed to load Manifest: ${manifestError.message}`);
182
193
  return;
183
194
  }
@@ -191,7 +202,7 @@ async function handleComputationTask(message, config, dependencies) {
191
202
  const duration = Date.now() - startTime;
192
203
 
193
204
  // STOP HEARTBEAT ON SUCCESS
194
- clearInterval(heartbeatTimer);
205
+ clearInterval(heartbeatControl.timer);
195
206
 
196
207
  const failureReport = result?.updates?.failureReport || [];
197
208
  const successUpdates = result?.updates?.successUpdates || {};
@@ -203,20 +214,33 @@ async function handleComputationTask(message, config, dependencies) {
203
214
  else {
204
215
  if (Object.keys(successUpdates).length > 0) { logger.log('INFO', `[Worker] ✅ Stored: ${computation}`); }
205
216
  else { logger.log('WARN', `[Worker] ⚠️ Empty Result: ${computation}`); }
217
+
218
+ // Extract the metrics from the success update for the recorder
219
+ const calcUpdate = successUpdates[normalizeName(computation)] || {};
220
+ const finalMetrics = {
221
+ durationMs: duration,
222
+ peakMemoryMB: heartbeatControl.getPeak(),
223
+ io: calcUpdate.metrics?.io,
224
+ storage: calcUpdate.metrics?.storage,
225
+ execution: calcUpdate.metrics?.execution,
226
+ validation: calcUpdate.metrics?.validation,
227
+ composition: calcUpdate.composition
228
+ };
206
229
 
207
230
  await db.doc(ledgerPath).update({
208
231
  status: 'COMPLETED',
209
232
  completedAt: new Date()
210
233
  }).catch(() => {});
211
234
 
212
- await recordRunAttempt(db, { date, computation, pass }, 'SUCCESS', null, { durationMs: duration }, triggerReason);
235
+ // [UPDATED] Pass resourceTier and metrics to recordRunAttempt
236
+ await recordRunAttempt(db, { date, computation, pass }, 'SUCCESS', null, finalMetrics, triggerReason, resourceTier);
213
237
 
214
238
  const callbackUrl = await decrementAndCheck(db, metaStatePath, logger);
215
239
  if (callbackUrl) { await triggerWorkflowCallback(callbackUrl, 'SUCCESS', logger); }
216
240
  }
217
241
  } catch (err) {
218
242
  // STOP HEARTBEAT ON ERROR
219
- clearInterval(heartbeatTimer);
243
+ clearInterval(heartbeatControl.timer);
220
244
 
221
245
  // --- ERROR HANDLING ---
222
246
  const isDeterministicError = err.stage === 'SHARDING_LIMIT_EXCEEDED' ||
@@ -241,7 +265,7 @@ async function handleComputationTask(message, config, dependencies) {
241
265
  failedAt: new Date()
242
266
  }, { merge: true });
243
267
 
244
- await recordRunAttempt(db, { date, computation, pass }, 'FAILURE', { message: err.message, stage: err.stage || 'PERMANENT_FAIL' }, { durationMs: 0 }, triggerReason);
268
+ await recordRunAttempt(db, { date, computation, pass }, 'FAILURE', { message: err.message, stage: err.stage || 'PERMANENT_FAIL' }, { durationMs: 0, peakMemoryMB: heartbeatControl.getPeak() }, triggerReason, resourceTier);
245
269
 
246
270
  const callbackUrl = await decrementAndCheck(db, metaStatePath, logger);
247
271
  if (callbackUrl) { await triggerWorkflowCallback(callbackUrl, 'SUCCESS', logger); }
@@ -252,9 +276,9 @@ async function handleComputationTask(message, config, dependencies) {
252
276
  if (retryCount >= MAX_RETRIES) { throw err; }
253
277
 
254
278
  logger.log('ERROR', `[Worker] ❌ Crash: ${computation}: ${err.message}`);
255
- await recordRunAttempt(db, { date, computation, pass }, 'CRASH', { message: err.message, stack: err.stack, stage: 'SYSTEM_CRASH' }, { durationMs: 0 }, triggerReason);
279
+ await recordRunAttempt(db, { date, computation, pass }, 'CRASH', { message: err.message, stack: err.stack, stage: 'SYSTEM_CRASH' }, { durationMs: 0, peakMemoryMB: heartbeatControl.getPeak() }, triggerReason, resourceTier);
256
280
  throw err;
257
281
  }
258
282
  }
259
283
 
260
- module.exports = { handleComputationTask };
284
+ module.exports = { handleComputationTask };