-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker-pool.js
More file actions
323 lines (284 loc) · 8.31 KB
/
worker-pool.js
File metadata and controls
323 lines (284 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Persistent Worker Pool for Bao encoding.
*
* Keeps workers alive across multiple encode calls for better amortized performance.
* Workers are created once and reused, eliminating startup overhead for batch processing.
*/
'use strict';
const { Worker } = require('worker_threads');
const path = require('path');
const os = require('os');
const CHUNK_LEN = 1024;
const HASH_SIZE = 32;
// Singleton pool instance
let poolInstance = null;
class PersistentWorkerPool {
/**
* Create a persistent worker pool.
* @param {number} numWorkers - Number of worker threads
*/
constructor(numWorkers = null) {
this.numWorkers = numWorkers || Math.min(os.cpus().length, 8);
this.workers = [];
this.workerReady = [];
this.taskId = 0;
this.pendingTasks = new Map();
this.initialized = false;
this.workerPath = path.join(__dirname, 'bao-rust-worker.js');
this.totalTasksProcessed = 0;
}
/**
* Initialize all worker threads.
* @returns {Promise<boolean>}
*/
async init() {
if (this.initialized) return true;
const workerPromises = [];
for (let i = 0; i < this.numWorkers; i++) {
workerPromises.push(this._createWorker(i));
}
try {
await Promise.all(workerPromises);
this.initialized = true;
return true;
} catch (err) {
console.error('Failed to initialize worker pool:', err);
await this.shutdown();
return false;
}
}
/**
* Create a single worker.
* @private
*/
_createWorker(workerId) {
return new Promise((resolve, reject) => {
const worker = new Worker(this.workerPath, {
workerData: { workerId }
});
const timeout = setTimeout(() => {
reject(new Error(`Worker ${workerId} init timeout`));
}, 10000);
worker.on('message', (msg) => {
if (msg.type === 'ready') {
clearTimeout(timeout);
this.workers[workerId] = worker;
this.workerReady[workerId] = true;
this._setupWorkerHandlers(worker, workerId);
resolve(worker);
} else if (msg.type === 'error' && !this.workers[workerId]) {
clearTimeout(timeout);
reject(new Error(msg.error));
}
});
worker.on('error', (err) => {
clearTimeout(timeout);
reject(err);
});
});
}
/**
* Setup message handlers for a worker.
* @private
*/
_setupWorkerHandlers(worker, workerId) {
worker.on('message', (msg) => {
if (msg.type === 'result' || msg.type === 'error') {
const task = this.pendingTasks.get(msg.taskId);
if (task) {
this.pendingTasks.delete(msg.taskId);
this.workerReady[workerId] = true;
this.totalTasksProcessed++;
if (msg.type === 'result') {
task.resolve(msg);
} else {
task.reject(new Error(msg.error));
}
}
}
});
worker.on('error', (err) => {
console.error(`Worker ${workerId} error:`, err);
this.workerReady[workerId] = false;
});
}
/**
* Execute a task on an available worker.
* @param {Object} message - Task message to send
* @returns {Promise<Object>} Task result
*/
async executeTask(message) {
if (!this.initialized) {
throw new Error('Worker pool not initialized. Call init() first.');
}
// Atomically claim an available worker
let workerId = -1;
while (workerId === -1) {
for (let i = 0; i < this.numWorkers; i++) {
if (this.workerReady[i]) {
this.workerReady[i] = false; // Atomically claim this worker
workerId = i;
break;
}
}
// If no worker available, wait for one and retry
if (workerId === -1) {
await this._waitForWorker();
}
}
return this._sendTask(workerId, message);
}
/**
* Execute batch chunk CVs across all workers in parallel.
* @param {Uint8Array} data - Data to process
* @param {number} startIndex - Starting chunk index
* @returns {Promise<Uint8Array[]>} Array of CVs
*/
async batchChunkCVsParallel(data, startIndex = 0) {
if (!this.initialized) {
throw new Error('Worker pool not initialized. Call init() first.');
}
const totalChunks = Math.floor(data.length / CHUNK_LEN);
if (totalChunks === 0) {
return [];
}
// Divide work among workers
const chunksPerWorker = Math.ceil(totalChunks / this.numWorkers);
const tasks = [];
for (let i = 0; i < this.numWorkers && i * chunksPerWorker < totalChunks; i++) {
const workerStartChunk = i * chunksPerWorker;
const workerEndChunk = Math.min(workerStartChunk + chunksPerWorker, totalChunks);
const workerNumChunks = workerEndChunk - workerStartChunk;
const dataStart = workerStartChunk * CHUNK_LEN;
const dataEnd = workerEndChunk * CHUNK_LEN;
const workerData = data.slice(dataStart, dataEnd);
// Capture buffer reference immediately - slice() creates new ArrayBuffer
const workerBuffer = workerData.buffer;
tasks.push({
workerId: i,
startChunk: workerStartChunk,
numChunks: workerNumChunks,
buffer: workerBuffer // Store the sliced buffer directly
});
}
// Send all tasks in parallel
const taskPromises = tasks.map(task => {
return this._sendTask(task.workerId, {
type: 'batchChunkCVs',
data: task.buffer,
startIndex: startIndex + task.startChunk,
numChunks: task.numChunks,
transfer: [task.buffer]
}).then(result => ({
startChunk: task.startChunk,
cvs: new Uint8Array(result.cvs)
}));
});
// Wait for all workers to complete
const results = await Promise.all(taskPromises);
// Combine results in order
const allCVs = [];
results.sort((a, b) => a.startChunk - b.startChunk);
for (const result of results) {
for (let i = 0; i < result.cvs.length; i += HASH_SIZE) {
allCVs.push(result.cvs.slice(i, i + HASH_SIZE));
}
}
return allCVs;
}
/**
* Send task to a specific worker.
* @private
*/
_sendTask(workerId, message) {
return new Promise((resolve, reject) => {
const taskId = this.taskId++;
message.taskId = taskId;
this.pendingTasks.set(taskId, { resolve, reject, workerId });
this.workers[workerId].postMessage(message, message.transfer || []);
});
}
/**
* Wait for a worker to become available.
* @private
*/
async _waitForWorker() {
while (true) {
for (let i = 0; i < this.numWorkers; i++) {
if (this.workerReady[i]) return;
}
await new Promise(resolve => setTimeout(resolve, 10));
}
}
/**
* Get pool statistics.
* @returns {Object} Pool stats
*/
getStats() {
return {
numWorkers: this.numWorkers,
initialized: this.initialized,
totalTasksProcessed: this.totalTasksProcessed,
activeWorkers: this.workers.filter(w => w !== null).length,
readyWorkers: this.workerReady.filter(r => r).length
};
}
/**
* Shutdown all workers.
*/
async shutdown() {
const shutdownPromises = this.workers.map((worker, i) => {
if (worker) {
return new Promise(resolve => {
let exited = false;
worker.postMessage({ type: 'shutdown' });
worker.once('exit', () => {
exited = true;
resolve();
});
setTimeout(() => {
if (!exited) {
worker.terminate();
}
resolve();
}, 1000);
});
}
return Promise.resolve();
});
await Promise.all(shutdownPromises);
this.pendingTasks.clear();
this.workers = [];
this.workerReady = [];
this.initialized = false;
poolInstance = null;
}
}
/**
* Get or create singleton worker pool.
* @param {number} numWorkers - Number of workers (only used on first call)
* @returns {Promise<PersistentWorkerPool>}
*/
async function getWorkerPool(numWorkers) {
if (!poolInstance) {
poolInstance = new PersistentWorkerPool(numWorkers);
await poolInstance.init();
}
return poolInstance;
}
/**
* Shutdown the singleton pool if it exists.
*/
async function shutdownPool() {
if (poolInstance) {
await poolInstance.shutdown();
poolInstance = null;
}
}
module.exports = {
PersistentWorkerPool,
getWorkerPool,
shutdownPool,
CHUNK_LEN,
HASH_SIZE
};