This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcontext-local.js
343 lines (301 loc) · 10.7 KB
/
context-local.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
// Copyright 2016 Luca-SAS, licensed under the Apache License 2.0
'use strict';
var child_process = require('child_process');
var fs = require('fs');
var os = require('os');
var url = require('url');
var zlib = require('zlib');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var uuid = require('uuid');
var dataset = require('./dataset.js');
var Task = require('./task.js');
var start = process.hrtime();
var memory = Number(process.env.SKALE_MEMORY);
module.exports = Context;
function Context(args) {
if (!(this instanceof Context))
return new Context(args);
this.contextId = uuid.v4();
var nworker = Number(process.env.SKALE_WORKERS) || (os.cpus().length - 1) || 1;
var tmp = process.env.SKALE_TMP || '/tmp';
var self = this;
log('workers:', nworker);
this.env = {};
this.worker = new Array(nworker);
for (var i = 0; i < nworker; i++) {
this.worker[i] = new Worker(i);
}
this.jobId = 0;
process.title = 'skale-master';
this.basedir = tmp + '/skale/' + this.contextId + '/';
mkdirp.sync(this.basedir + 'tmp');
mkdirp.sync(this.basedir + 'stream');
this.datasetIdCounter = 0; // global dataset id counter
this.parallelize = function (localArray, nPartitions) {return dataset.parallelize(this, localArray, nPartitions);};
this.range = function (start, end, step, nPartitions) {return dataset.range(this, start, end, step, nPartitions);};
this.lineStream = function (stream, config) {return new dataset.Stream(this, stream, 'line', config);};
this.objectStream = function (stream, config) {return new dataset.Stream(this, stream, 'object', config);};
this.log = log;
this.end = function () {
rimraf(self.basedir, function (err) {
if (err) log('remove', err);
});
for (var i = 0; i < self.worker.length; i++) {
try {
self.worker[i].child.disconnect();
} catch(err) {
console.log(err);
}
}
};
this.getReadStream = function (fileObj, opt, done) {
done(null, fs.createReadStream(fileObj.path, opt));
};
this.getReadStreamSync = function (fileObj, opt) {
return fs.createReadStream(fileObj.path, opt);
};
this.textFile = function (file, opt /*, nPartitions*/) {
opt = opt || {};
var u = url.parse(file);
if (u.protocol === 's3:')
return new dataset.TextS3(this, file.slice(5), opt);
if (u.protocol === 'wasb:')
return new dataset.TextAzure(this, file.slice(7), opt);
return new dataset.TextLocal(this, file, opt);
};
this.runTask = function (task, callback) {
function getLeastBusyWorkerId(/* preferredLocation */) {
var wid, ntask;
for (var i = 0; i < self.worker.length; i++) {
if ((ntask == undefined) || (ntask > self.worker[i].ntask)) {
ntask = self.worker[i].ntask;
wid = i;
}
}
return wid;
}
function serialize(task) {
var pleft;
var pright;
var nodeId;
var p = task.pid;
var node = task.nodes[task.datasetId];
var part = node.shufflePartitions ? node.shufflePartitions[p] : node.partitions[p];
var pindex = {};
// Walk through dataset ancestors to keep partition dependencies
while (part) {
pindex[part.datasetId] = p;
node = task.nodes[part.parentDatasetId];
if (!node) break;
p = part.parentPartitionIndex;
part = node.shufflePartitions ? node.shufflePartitions[p] : node.partitions[p];
}
return str = JSON.stringify(task, function(key, value) {
if (key == 'sc') return undefined;
if (key == 'dependencies') {
var dep = [];
for (var i = 0; i < value.length; i++) dep[i] = value[i].id;
return dep;
}
if (key === 'pleft') pleft = value;
else if (key === 'pright') pright = value;
else if (key === 'id') nodeId = value;
// For shufflePartitions (not cartesian), return only the ones used by the task.
if (key === 'files' && ! value.path) {
var v = {};
v[pindex[nodeId]] = value[pindex[nodeId]];
return v;
}
// For cartesian shufflePartitions, return only the ones used by the task
if (key === 'shufflePartitions' && value[0] && value[0].files && value[0].files.path) {
var p1 = Math.floor(task.pid / pright);
var p2 = task.pid % pright + pleft;
v = {};
v[task.pid] = value[task.pid];
v[p1] = value[p1];
v[p2] = value[p2];
return v;
}
return (typeof value === 'function' ) ? value.toString() : value;
});
}
var wid = getLeastBusyWorkerId(task.nodes[task.datasetId].getPreferedLocation(task.pid));
// Init some environment on the worker the first time we send it a task
if (!this.worker[wid].init) {
this.worker[wid].init = true;
task.env = this.env;
}
var str = serialize(task);
this.worker[wid].ntask++;
//log('task size for worker ' + wid + ':', str.length);
//log('task', str);
if (str.length > 1000000) {
zlib.gzip(str, {chunkSize: 65536}, function (err, res) {
var filename = task.basedir + 'task-' + uuid.v4() + '.gz';
fs.writeFile(filename, res, function (err) {
if (err) throw new Error(err);
rpc('runztask', wid, filename, function (err, res) {
self.worker[wid].ntask--;
callback(err, res, task);
});
});
});
} else {
rpc('runTask', wid, str, function(err, res) {
self.worker[wid].ntask--;
callback(err, res, task);
});
}
};
this.runJob = function (opt, root, action, callback) {
var jobId = this.jobId++;
//this.getWorkers(function () {
findShuffleStages(function(shuffleStages) {
if (shuffleStages.length == 0) runResultStage();
else {
var cnt = 0;
runShuffleStage(shuffleStages[cnt], shuffleDone);
}
function shuffleDone() {
if (++cnt < shuffleStages.length) {
runShuffleStage(shuffleStages[cnt], shuffleDone);
} else runResultStage();
}
});
//});
function runShuffleStage(stage, done) {
findNodes(stage, function(nodes) {
var pid = 0, tasks = [], i, j, node;
var index = 0, busy = 0, complete = 0, totalFiles = 0, totalSize = 0;
stage.shufflePartitions = {};
for (i = 0; i < stage.dependencies.length; i++) {
node = stage.dependencies[i];
for (j = 0; j < node.nPartitions; j++) {
stage.shufflePartitions[pid++] = new dataset.Partition(stage.id, pid, node.id, node.partitions[j].partitionIndex);
}
}
stage.nShufflePartitions = pid;
for (i = 0; i < stage.nShufflePartitions; i++) {
tasks.push(new Task({
basedir: self.basedir,
jobId: jobId,
nodes: nodes,
datasetId: stage.id,
pid: i
}));
}
function runNext() {
while (busy < nworker && index < tasks.length) {
busy++;
self.runTask(tasks[index++], function (err, res, task) {
stage.shufflePartitions[res.pid].files = res.files;
busy--;
complete++;
var n = 0, size = 0;
for (var f in res.files) {
n++;
size += res.files[f].size;
}
totalFiles += n;
totalSize += size;
log('part', task.pid, 'from worker-' + res.workerId,
'(' + complete + '/' + tasks.length + '), shuffle out:', n, 'files,', (size/(1<<20)).toFixed(3), 'MB');
if (complete === tasks.length) {
log('pre-shuffle stage done, output:', totalFiles, 'files,', (totalSize / (1 << 20)).toFixed(3), 'MB');
stage.executed = true;
return done();
}
runNext();
});
}
}
log('start shuffle stage, partitions:', stage.nShufflePartitions);
runNext();
});
}
function runResultStage() {
findNodes(root, function(nodes) {
var tasks = [];
log('start result stage, partitions:', root.nPartitions);
for (var i = 0; i < root.nPartitions; i++) {
tasks.push(new Task({
basedir: self.basedir,
jobId: jobId,
nodes: nodes,
datasetId: root.id,
pid: i,
action: action
}));
}
callback({id: jobId}, tasks);
});
}
function findNodes(node, done) {
var nodes = {};
interruptibleTreewalk(node, function cin(n, done) {
done(n.shuffling && n.executed);
}, function cout(n, done) {
n.getPartitions(function() {
if (nodes[n.id] == undefined) nodes[n.id] = n;
done();
});
}, function() {done(nodes);});
}
function findShuffleStages(callback) {
var stages = [];
interruptibleTreewalk(root, function cin(n, done) {
if (n.shuffling && !n.executed) stages.unshift(n);
done(n.shuffling && n.executed); // stage boundary are shuffle nodes
}, function cout(n, done) {done();}, function() {callback(stages);});
}
function interruptibleTreewalk(n, cin, cout, done) {
cin(n, function(uturn) { // if uturn equals true the subtree under node won't be treewalked
if (!uturn) {
var nDependencies = 0;
for (var i = 0; i < n.dependencies.length; i++)
interruptibleTreewalk(n.dependencies[i], cin, cout, function() {
if (++nDependencies == n.dependencies.length) cout(n, done);
});
if (n.dependencies.length === 0) cout(n, done);
} else cout(n, done);
});
}
};
function rpc(cmd, workerNum, args, callback) {
self.worker[workerNum].request({cmd: cmd, args: args}, callback);
}
}
function Worker(index) {
this.index = index;
this.reqid = 1;
this.reqcb = {};
this.ntask = 0;
this.child = child_process.fork(__dirname + '/worker-local.js', [index, memory], {
execArgv: memory ? ['--max_old_space_size=' + memory] : undefined
});
var self = this;
this.child.on('message', function(msg) {
if (self.reqcb[msg.id]) {
self.reqcb[msg.id](msg.error, msg.result);
delete self.reqcb[msg.id];
}
});
}
Worker.prototype.send = function(msg) {
this.child.send(msg);
};
Worker.prototype.request = function (req, done) {
this.reqcb[this.reqid] = done;
this.child.send({id: this.reqid++, req: req});
};
if (process.env.SKALE_DEBUG) {
var log = function() {
var args = Array.prototype.slice.call(arguments);
var elapsed = process.hrtime(start);
args.unshift('[master ' + (elapsed[0] + elapsed[1] / 1e9).toFixed(3) + 's]');
console.error.apply(null, args);
};
} else {
log = function () {};
}