-
Notifications
You must be signed in to change notification settings - Fork 1
/
Conductor.js
427 lines (364 loc) · 12.3 KB
/
Conductor.js
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"use strict";
const Async = require('async');
const OS = require('os');
const Cluster = require('cluster');
const EventEmitter = require('events').EventEmitter;
const ShortId = require('shortid');
const Util = require('util');
/**
* Distributes job processing across multiple processes
*/
class Conductor extends EventEmitter {
/**
* Constructor
* @param options
*/
constructor(options) {
super();
options = options || {};
this.workerType = options.workerType || 'conductor_worker';
this.workerLimit = options.workerLimit || Math.min(4, OS.cpus().length);
this.jobsQueue = options.jobsQueue || [];
this.logging = options.logging !== undefined ? options.logging : true;
this.processing = false;
this.workerEnv = options.workerEnv || {};
this.lookups = options.lookups || {};
this.lastStats = {};
this.workerStatsReportCount = 0;
this.id = ShortId.generate();
this._workerIds = [];
// awaitable methods
this.start = Util.promisify(this.start.bind(this));
this.generateJobs = Util.promisify(this.generateJobs.bind(this));
}
//region Override These Methods
/* istanbul ignore next: must be overridden to be useful */
//noinspection JSMethodCanBeStatic
/**
* Generates the job list - you gotta implement this or use options.jobsQueue in the constructor
* @param callback
*/
generateJobs (callback) {
// TODO - implement on child class
// e.g. this.jobsQueue.push(job);
callback();
}
/**
* Hook point for custom master-worker messaging calls. Not necessarily needed
* @param id
* @param msg
*/
onWorkerMessage(id, msg) {
if (msg.cmd) {
const callback = msg.callback;
switch (msg.cmd) {
case "override-me":
this.sendMessageToWorker(id, "override-me", 'nope', callback);
return;
}
}
// If we got to here, the command is invalid
this.error('Unknown message from worker', id, msg);
/* istanbul ignore else: would cause a hang with no callback */
if (msg.callback) this.sendMessageToWorker(id, msg.cmd || "error", { error: "Unhandled command! Override onWorkerMessage?" }, msg.callback);
}
//endregion
//region Class Methods
/**
* Gets whether the job is still running
* @return {boolean}
*/
isProcessing() {
return this.processing;
}
/**
* Sends a message to a worker
* @param id
* @param commandName
* @param data
* @param callbackId
*/
sendMessageToWorker(id, commandName, data, callbackId) {
Cluster.workers[id].send({
workerId: id,
masterId: this.id,
cmd: commandName,
data: data,
callback: callbackId
});
}
/**
* Broadcasts a message to all workers.
* @param commandName
* @param data
*/
broadcastMessageToWorkers(commandName, data) {
this._workerIds.forEach((id) => {
this.sendMessageToWorker(id, commandName, data);
});
}
/**
* Shuts down all workers immediately
*/
abort() {
this.broadcastMessageToWorkers('!!ABORT!!', {});
}
/**
* Returns the lookup map with the given name
* @param name
* @return {{}|*}
*/
getLookup(name) {
return this.lookups[name] || {};
}
/**
* Returns the cached value (if defined) in the given lookup
* @param name
* @param key
* @return {*}
*/
getLookupValue(name, key) {
const lookup = this.getLookup(name);
if (lookup instanceof Map) {
return lookup.get(key);
} else {
return lookup[key];
}
}
/**
* Sets a lookup value in the given lookup
* @param name
* @param key
* @param value
*/
setLookupValue(name, key, value) {
const lookup = this.getLookup(name);
if (lookup instanceof Map) {
lookup.set(key, value);
} else {
lookup[key] = value;
}
}
/**
* Generates the job list and starts the workers
* @param callback - Calls back when the initial worker pool has been started
*/
start(callback) {
/* istanbul ignore else: out of scope */
if (Cluster.isMaster) {
this.processing = true;
Async.waterfall([
// Generate the job queue
(next) => this.generateJobs(next),
// Start workers
(next) => { this._startWorkers(); next(); }
], callback);
} else {
// Not master, don't conduct
callback(new Error('Not master'));
}
}
//endregion
//region Internal Methods
/**
* Gets the next job to process
* @return {*}
*/
_getNextJob() {
return this.jobsQueue.shift();
}
/**
* Logs to the output if logging is enabled
*/
log() {
if (this.logging) console.log.apply(console, [].slice.call(arguments)); // eslint-disable-line no-console
}
/**
* Logs to the output if logging is enabled
*/
error() {
if (this.logging) console.error.apply(console, [].slice.call(arguments)); // eslint-disable-line no-console
}
/**
* Sawns a new instance of a worker
*/
_startWorker() {
this.workerEnv.worker_type = this.workerType;
this.workerEnv.master_id = this.id;
const worker = Cluster.fork(this.workerEnv),
id = worker.id;
this._workerIds.push(id+"");
this.log(`> Started worker ${id}`);
// Message handler
Cluster.workers[id].on('message', (msg) => {
if (msg && msg.cmd) {
let job;
let lookupName;
let lookupKey;
let setLookupName;
let setLookupKey;
let setLookupValue;
let stats;
switch (msg.cmd) {
// Get task
case "getJob":
job = this._getNextJob();
this.log(`> Assigned worker ${id} job:`, job);
this.sendMessageToWorker(id, "getJob", job, msg.callback);
break;
// Check master for known value
case "lookup":
lookupName = msg.data.name;
lookupKey = msg.data.key;
this.sendMessageToWorker(
id,
"lookup",
{
name: lookupName,
key: lookupKey,
value: this.getLookupValue(lookupName, lookupKey)
}, msg.callback
);
break;
// Set master known value
case "setLookup":
setLookupName = msg.data.name;
setLookupKey = msg.data.key;
setLookupValue = msg.data.value;
this.setLookupValue(setLookupName, setLookupKey, setLookupValue);
this.sendMessageToWorker(id, "setLookup", null, msg.callback);
break;
case "stats":
stats = msg.data;
this._updateStatsForWorker(id, stats);
this.sendMessageToWorker(id, "stats", null, msg.callback);
break;
default:
this.onWorkerMessage(id, msg);
}
} else {
this.onWorkerMessage(id, msg);
}
});
// Exit strategy
Cluster.workers[id].on('exit', (code, signal) => this._onWorkerExit(id, code, signal));
}
/**
* Integrates a worker's metrics into the overall stats of the processor
* @param id
* @param stats
*/
_updateStatsForWorker(id, stats) {
this.lastStats[id] = stats;
this.workerStatsReportCount++;
// Let I/O settle before doing this, maybe we'll get other reports in before it fires?
setImmediate(() => this._reportStats());
}
/**
* Aggregates and emits the stats event
*/
_reportStats() {
const workersCount = Object.keys(Cluster.workers).length;
const submissionCount = this.workerStatsReportCount;
if (submissionCount >= workersCount) {
// Print out the report!
const stats = this.lastStats;
this.workerStatsReportCount = 0;
// Aggregate stats?
const agg = {
last: {},
diff: {}
};
let timeSpanSum = 0;
// worker => stats
Object.keys(stats).forEach((id) => {
timeSpanSum += stats[id].timeSpan;
Object.keys(stats[id].last).forEach((key) => {
if (agg.last[key] === undefined) {
agg.last[key] = stats[id].last[key];
} else {
agg.last[key] += stats[id].last[key];
}
});
Object.keys(stats[id].diff).forEach((key) => {
if (agg.diff[key] === undefined) {
agg.diff[key] = stats[id].diff[key];
} else {
agg.diff[key] += stats[id].diff[key];
}
});
});
/**
* Stats Event
* @event Conductor#stats
* @type {{raw: object, avgTimeSpan: number, agg: object}}
*/
this.emit('stats', { raw: stats, avgTimeSpan: timeSpanSum / submissionCount, agg: agg });
}
}
/**
* Handles the worker exit event
* @param id
* @param code
* @param signal
*/
_onWorkerExit(id, code, signal) {
// If the worker blew up, spawn another to replace it
this.log(`> Worker ${id} ended with code ${code} signal ${signal}`);
// Deregister this worker id from our id bucket
const index = this._workerIds.indexOf(""+id);
/* istanbul ignore else: out of scope */
if (index >= 0) {
this._workerIds.splice(index, 1);
}
if (code !== 0) {
/**
* Error Event - Occurs when a worker crashes or another bad thing happens
* @event Conductor#error
* @type {{type:string, workerId: number, code: number, signal: number}}
*/
this.emit('error', { type: 'worker_crash', workerId: id, code: code, signal: signal });
this._startWorker();
} else {
this._checkForCompletion();
}
this.emit('worker_exit', { id, code, signal });
}
/**
* Checks whether aall the workers are done and whether to bail
*/
_checkForCompletion() {
// Any other workers left?
const workerCount = Object.keys(Cluster.workers).length;
if (workerCount === 0) {
this.log(`> No more workers. Closing up shop.`);
this.processing = false;
/**
* Work Completed Event
* @event Conductor#end
* @type {null}
*/
this.emit('end');
clearInterval(this.completionTimer);
} else {
this.log(`> Waiting on ${workerCount} other workers to finish.`);
// Once a worker ends normally, then the light at the tunnel is in sight.
// To prevent whacky race conditions, lets set up a timer to recheck if we're done yet
// just in case something ends w/o setting workerCount to zero
if (this.completionTimer) {
clearInterval(this.completionTimer);
}
this.completionTimer = setInterval(() => this._checkForCompletion(), 500);
}
}
/**
* Starts the initial set of workers
*/
_startWorkers() {
for(let i = 0; i < this.workerLimit; i++) {
this._startWorker();
}
}
//endregion
}
module.exports = Conductor;