-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhandler.js
More file actions
502 lines (431 loc) · 18.8 KB
/
handler.js
File metadata and controls
502 lines (431 loc) · 18.8 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env node
// Executor of 'jobs' using the Redis task status notification mechanism
const tracer = process.env.HF_VAR_ENABLE_TRACING === "1" ? require("./tracing.js")("hyperflow-job-executor"): undefined;
const { spawn } = require('child_process');
const redis = require('redis');
const fs = require('fs');
const log4js = require('log4js');
const pidtree = require('pidtree');
var pidusage = require('pidusage');
const si = require('systeminformation');
const path = require('path');
const { readEnv } = require('read-env');
const shortid = require('shortid');
const RemoteJobConnector = require('./connector');
const {
procfs,
ProcfsError,
} = require('@stroncium/procfs');
const handlerId = shortid.generate();
/*
** Function handleJob
** Parameters:
** - taskId: unique task identifier
** - rcl: redis client
*/
async function handleJob(taskId, rcl, message) {
// Configure remote job worker
let wfId = taskId.split(':')[1];
let connector = new RemoteJobConnector(rcl, wfId);
// time interval (ms) at which to probe and log metrics
const probeInterval = process.env.HF_VAR_PROBE_INTERVAL || 2000;
// **Experimental**: add job info to Redis "hf_all_jobs" set
var allJobsMember = taskId + "#" + process.env.HF_LOG_NODE_NAME + "#" +
process.env.HF_VAR_COLLOCATION_TYPE + "#" + process.env.HF_VAR_COLLOCATION_SIZE;
rcl.sadd("hf_all_jobs", allJobsMember, function(err, ret) { if (err) console.log(err); });
// increment task acquisition counter
async function acquireTask(rcl, taskId) {
return new Promise(function (resolve, reject) {
rcl.incr(taskId + '_acqCount', function(err, reply) {
(err) ? reject(err) : resolve(reply);
});
});
}
// get job message from Redis
var getJobMessage = async function (rcl, taskId, timeout) {
return new Promise(function (resolve, reject) {
const jobMsgKey = taskId + "_msg";
rcl.brpoplpush(jobMsgKey, jobMsgKey, timeout, function (err, reply) {
err ? reject(err): resolve(reply);
});
});
}
// send notification about job completion to Redis
// 'code' is the job's exit code
var notifyJobCompletion = async function (rcl, taskId, code) {
return connector.notifyJobCompletion(taskId, code);
}
// check if job has already completed
var hasCompleted = async function(rcl, taskId) {
return new Promise((resolve, reject) => {
let wfId = taskId.split(':')[1];
var key = "wf:" + wfId + ":completedTasks";
rcl.sismember(key, taskId, function(err, hasCompleted) {
err ? reject(err): resolve(hasCompleted);
});
});
}
var pids = {} // pids of the entire pid tree (in case the main process starts child processes)
var jm; // parsed job message
// logging basic process info from the procfs
logProcInfo = function (pid) {
// log process command line
try {
let cmdInfo = { "pid": pid, "name": jm["name"], "command": procfs.processCmdline(pid) };
logger.info("command:", JSON.stringify(cmdInfo));
} catch (error) {
if (error.code === ProcfsError.ERR_NOT_FOUND) {
console.error(`process ${pid} does not exist`);
}
}
// periodically log process IO
logProcIO = function (pid) {
try {
let ioInfo = procfs.processIo(pid);
ioInfo.pid = pid;
ioInfo.name = jm["name"];
logger.info("IO:", JSON.stringify(ioInfo));
setTimeout(() => logProcIO(pid), probeInterval);
} catch (error) {
if (error.code === ProcfsError.ERR_NOT_FOUND) {
console.error(`process ${pid} does not exist (this is okay)`);
}
}
}
logProcIO(pid);
logProcNetDev = function (pid) {
try {
let netDevInfo = procfs.processNetDev(pid);
//netDevInfo.pid = pid;
//netDevInfo.name = jm["name"];
logger.info("NetDev: pid:", pid, JSON.stringify(netDevInfo));
setTimeout(() => logProcNetDev(pid), probeInterval);
} catch (error) {
if (error.code === ProcfsError.ERR_NOT_FOUND) {
//console.error(`process ${pid} does not exist (this is okay)`);
}
}
}
logProcNetDev(pid);
logPidUsage = function(pid) {
pidusage(pid, function (err, stats) {
if (err) {
console.error(`pidusage error ${err.code} for process ${pid}`);
return;
}
//console.log(stats);
// => {
// cpu: 10.0, // percentage (from 0 to 100*vcore)
// memory: 357306368, // bytes
// ppid: 312, // PPID
// pid: 727, // PID
// ctime: 867000, // ms user + system time
// elapsed: 6650000, // ms since the start of the process
// timestamp: 864000000 // ms since epoch
// }
logger.info("Procusage: pid:", pid, JSON.stringify(stats));
setTimeout(() => logPidUsage(pid), probeInterval);
});
}
logPidUsage(pid);
}
var numRetries = process.env.HF_VAR_NUMBER_OF_RETRIES || 1;
var backoffSeed = process.env.HF_VAR_BACKOFF_SEED || 10;
// Rewrite command args by adding path to input directory if one is defined
function addDirToCommand(jobIns, args, dir) {
let newArgs = args;
let changed = false;
jobIns.forEach(function(jobInput) {
if (jobInput.workflow_input) { // this is a workflow input
newArgs.forEach(function(arg, idx) {
if (arg === jobInput.name) {
newArgs[idx] = path.join(dir, arg);
changed = true;
}
});
}
});
if (changed) {
console.log("INPUT_DIR provided, command rewritten:", jm["executable"], newArgs);
logger.info("INPUT_DIR provided, command rewritten:", jm["executable"], newArgs);
}
return newArgs;
}
async function executeJob(jm, attempt) {
return new Promise((resolve, reject) => {
if (process.env.HF_VAR_DRY_RUN) {
console.log("DRY RUN...")
return resolve(0);
}
var stdoutStream, stderrStream;
let options = {};
if (jm.shell) {
options = {shell: true};
}
let commandArgs = jm["args"];
let jobIns = jm["inputs"];
if (inputDir) {
commandArgs = addDirToCommand(jobIns, commandArgs, inputDir);
}
const cmd = spawn(jm["executable"], commandArgs, options);
let targetPid = cmd.pid;
cmd.stdout.pipe(stdoutLog);
cmd.stderr.pipe(stderrLog);
logProcInfo(targetPid);
logger.info('job started:', jm["name"]);
var sysinfo = {};
// log system information
si.cpu().
then(data => {
sysinfo.cpu = data;
}).
then(si.mem).
then(data => {
sysinfo.mem = data;
}).
then(data => logger.info("Sysinfo:", JSON.stringify(sysinfo))).
catch(err => console.err(error));
//console.log(Date.now(), 'job started');
// make sure info about all child processes is logged (checks periodically for new pids)
var allpids = {}
addPidTree = function (pid) {
pidtree(targetPid, function (err, pids) {
//console.log(pids)
if (!pids) return;
pids.map(p => {
if (!allpids[p]) {
allpids[p] = "ok";
logProcInfo(p);
}
});
setTimeout(() => addPidTree(pid), 1000);
});
}
addPidTree(targetPid);
// redirect process' stdout to a file
let stdoutRedir = jm["stdout"] || jm["stdoutAppend"];
if (stdoutRedir) {
let f = jm["stdout"] ? 'w' : 'a'; // truncate or append file
stdoutStream = fs.createWriteStream(stdoutRedir, {flags: f});
cmd.stdout.pipe(stdoutStream);
}
// redirect process' stderr to a file
let stderrRedir = jm["stderr"] || jm["stderrAppend"];
if (stderrRedir) {
let f = jm["stderr"] ? 'w' : 'a'; // truncate or append file
stderrStream = fs.createWriteStream(stderrRedir, {flags: f});
cmd.stderr.pipe(stderrStream);
}
cmd.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
cmd.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
cmd.on('close', async(code) => {
if (code != 0) {
logger.info("job failed (try " + attempt + "): '" + jm["executable"], jm["args"].join(' ') + "'");
} else {
logger.info('job successful (try ' + attempt + '):', jm["name"]);
}
logger.info('job exit code:', code);
// retry the job
if (code !=0 && numRetries-attempt > 0) {
logger.info('Retrying job, number of retries left:', numRetries-attempt);
cmd.removeAllListeners();
// need to recreate write streams to log files for the retried job
stdoutLog = fs.createWriteStream(stdoutfilename, {flags: 'a'});
stderrLog = fs.createWriteStream(stderrfilename, {flags: 'a'});
var factor = attempt > 5 ? 5: attempt;
var backoffDelay = Math.floor(Math.random() * backoffSeed * factor);
logger.info('Backoff delay:', backoffDelay, 's');
setTimeout(function() { resolve(executeJob(jm, attempt+1)); }, backoffDelay*1000);
} else {
resolve(code);
}
});
});
}
async function waitForInputs(jobInputFiles, maxRetries) {
return new Promise((resolve, reject) => {
let files = jobInputFiles.map(file => file.path).slice();
var filesToWatch = jobInputFiles.slice();
var filesReady = [];
var numRetries = 0;
var checkFiles = function() {
var filesChecked = 0;
var nFilesLeft = filesToWatch.length;
if (numRetries > maxRetries) {
logger.info("Error waiting for input files", files);
return reject("Error waiting for input files", files);
}
//logger.info("Waiting for input files: (" + numRetries + ")", files);
logger.info('waitingForFiles (' + numRetries + '): { "timestamp":', Date.now() +
', "waitingForFiles":', JSON.stringify(filesToWatch.map(f => f.path)) + ', "filesReady":',
JSON.stringify(filesReady), "}");
numRetries++;
var filesFoundIdx = [];
filesToWatch.forEach((file, i) => {
let fileStats;
try {
fileStats = fs.statSync(file.path);
} catch (err) { }
if (fileStats && (!file.size || file.size == fileStats.size)) {
filesChecked++;
filesReady.push({"file": file.path, "readTime": Date.now()});
filesFoundIdx.push(i);
delete filesToWatch[i];
}
});
filesToWatch = filesToWatch.filter(f => { return f; });
if (filesFoundIdx.length) {
//filesToWatch.forEach((_, i) => filesToWatch.splice(i, 1));
logger.info('filesReady (' + numRetries + '): { "timestamp":', Date.now() +
', "waitingForFiles":', JSON.stringify(filesToWatch) + ', "filesReady":',
JSON.stringify(filesReady), "}");
}
if (filesToWatch.length == 0) {
logger.info("All input files ready!");
return resolve();
} else {
const t = Math.pow(2, numRetries)+1000;
setTimeout(() => {
checkFiles();
}, t);
}
}
checkFiles();
});
}
// check if working directory is set
if (process.env.HF_VAR_WORK_DIR) {
process.chdir(process.env.HF_VAR_WORK_DIR);
} else if (fs.existsSync("/work_dir")) {
process.chdir("/work_dir");
}
var workDir = process.cwd();
var logDir = process.env.HF_VAR_LOG_DIR || (workDir + "/logs-hf");
var inputDir = process.env.HF_VAR_INPUT_DIR;
var outputDir = process.env.HF_VAR_OUTPUT_DIR;
// make sure log directory is created
try { fs.mkdirSync(logDir); } catch (err) {}
fs.statSync(logDir);
const loglevel = process.env.HF_VAR_LOG_LEVEL || 'info';
const logfilename = logDir + '/task-' + taskId.replace(/:/g, '__') + '@' + handlerId + '.log';
const stdoutfilename = logDir + '/task-' + taskId.replace(/:/g, '__') + '@' + handlerId + '__stdout.log';
const stderrfilename = logDir + '/task-' + taskId.replace(/:/g, '__') + '@' + handlerId + '__stderr.log';
var stdoutLog = fs.createWriteStream(stdoutfilename, {flags: 'w'});
var stderrLog = fs.createWriteStream(stderrfilename, {flags: 'w'});
const enableNethogs = process.env.HF_VAR_ENABLE_NETHOGS=="1";
const nethogsfilename = logDir + '/task-' + taskId.replace(/:/g, '__') + '@' + handlerId + '__nethogs.log';
log4js.configure({
appenders: { hftrace: { type: 'file', filename: logfilename} },
categories: { default: { appenders: ['hftrace'], level: loglevel } }
});
const logger = log4js.getLogger('hftrace');
// log all environment variables starting with HF_LOG_
const envLog = readEnv("HF_LOG");
logger.info("Environment variables (HF_LOG):", JSON.stringify(envLog));
//var rcl = redis.createClient(redisUrl);
logger.info('handler started, (ID: ' + handlerId + ')');
// 0. Detect multiple task acquisitions
let totalAcq = await acquireTask(rcl, taskId);
if (totalAcq > 1) {
let beforeAcq = totalAcq - 1;
logger.warn('Task was already acquired', beforeAcq.toString(), 'times');
}
// 1. Check if this job has already been completed -- useful in Kubernetes
// where sometimes a succesful job can be restarted for unknown reason
var jobHasCompleted = await hasCompleted(rcl, taskId);
if (jobHasCompleted) {
logger.warn("Warning: unexpected restart of job", taskId,
"(already succesfully completed)!");
log4js.shutdown(function () { return 0; });
return;
}
// 2. Get job message from Redis if not given as an handeJob argument
if (message === null) {
let jobMessage = null;
try {
jobMessage = await getJobMessage(rcl, taskId, 0);
} catch (err) {
console.error(err);
logger.error(err);
throw err;
}
jm = JSON.parse(jobMessage);
} else {
jm = message
}
logger.info('jobMessage: ', JSON.stringify(jm))
console.log("Received job message:", JSON.stringify(jm));
// create arrays of input and output file names; if inpuDir/outputDir is present,
// add path to it (for files which are flagged as 'workflow_input'/'workflow_output')
var inputFiles = jm.inputs;
var outputFiles = jm.outputs;
inputFiles.forEach((input) => {
input.path = inputDir && input.workflow_input ? path.join(inputDir, input.name) : input.name;
});
outputFiles.forEach((output) => {
output.path = outputDir && output.workflow_output ? path.join(outputDir, output.name) : output.name;
});
// 3. Check/wait for input files (useful in some distributed file systems)
if (process.env.HF_VAR_WAIT_FOR_INPUT_FILES=="1" && jm.inputs && jm.inputs.length) {
try {
await waitForInputs(inputFiles, process.env.HF_VAR_FILE_WATCH_NUM_RETRIES || 10);
} catch(err) {
throw err;
}
}
// 4. turn on network IO monitoring using nethogs
let nethogs;
if (enableNethogs) {
var nethogsStream = fs.createWriteStream(nethogsfilename, {flags: 'w'});
nethogs = spawn("nethogs-wrapper.py");
nethogs.stdout.pipe(nethogsStream);
nethogs.on('error', function(err){
logger.error("nethogs execution error:", err);
});
}
// 5. Execute job
logger.info("Job command: '" + jm["executable"], jm["args"].join(' ') + "'");
let jobExitCode = await executeJob(jm, 1);
// Notify job completion to HyperFlow
try {
await notifyJobCompletion(rcl, taskId, jobExitCode);
//console.log(Date.now(), 'job ended');
} catch (err) {
console.error("Redis notification failed", err);
logger.error("Redis notification failed: " + err);
throw err;
}
// log info about input/output files
var getFileSizeObj = function (fileName, filePath) {
var size = -1;
try {
var stats = fs.statSync(filePath);
size = stats["size"];
} catch (err) { }
return {[fileName]: size};
}
var inputsLog = inputFiles.map(inFile => getFileSizeObj(inFile.name, inFile.path));
var outputsLog = outputFiles.map(outFile => getFileSizeObj(outFile.name, outFile.path));
logger.info("Job inputs:", JSON.stringify(inputsLog));
logger.info("Job outputs:", JSON.stringify(outputsLog));
// **Experimental**: remove job info from Redis "hf_all_jobs" set
rcl.srem("hf_all_jobs", allJobsMember, function (err, ret) { if (err) console.log(err); });
logger.info('handler finished, code=', jobExitCode);
// 6. Perform cleanup operations
if (nethogs !== undefined) {
nethogs.stdin.pause();
nethogs.kill();
}
log4js.shutdown(function (err) {
if (err !== undefined) {
logger.error("log4js shutdown error:", err);
}
});
pidusage.clear();
return jobExitCode;
}
exports.handleJob = handleJob;