-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_db.js
413 lines (357 loc) · 11.7 KB
/
redis_db.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
var uuid = require('node-uuid');
var _ = require('underscore');
var assert = require('assert').ok;
var bcrypt = require('bcrypt');
var redis = require("redis"),
client = redis.createClient();
var logging = require("./logging");
// TODO: Add data validation (i.e. make sure the input)
// matches specification for each of the API functions.
client.on("error", function (err) {
console.log("Error " + err);
});
// Functions to access Redis collections.
function k_input(job_id) {
return job_id + "_input";
}
// READ-ONLY, except by add_new_job.
// Useful if we need to restart a job for some reason.
function k_original_input(job_id) {
return job_id + "_originalInput";
}
function k_replication_factor(job_id) {
return job_id + "_replicationFactor";
}
function k_blurb(job_id) {
return job_id + "_blurb";
}
function k_work_queue(job_id) {
return job_id + "_workQueue";
}
function k_in_count(job_id) {
return job_id + "_inCount";
}
function k_out_count(job_id) {
return job_id + "_outCount";
}
function k_output(job_id, chunk_id) {
return job_id + "_intermediateOutput_" + chunk_id;
}
function k_final_output(job_id) {
return job_id + "_finalOutput";
}
function k_phase(job_id) {
return job_id + "_phase";
}
function k_map_code(job_id) {
return job_id + "_mapCode";
}
function k_reduce_code(job_id) {
return job_id + "_reduceCode";
}
function k_runnable() {
return "runnableTasks";
}
function k_user_password(email_address) {
return email_address + "_password";
}
function k_user_jobs(email_address) {
return email_address + "_jobs";
}
// Callback: function(err).
// err will be set to "EXISTS" if the email_address has already been taken.
function new_user(email_address, password, callback) {
assert(!!email_address);
assert(!!password);
assert(!!callback);
client.exists(k_user_password(email_address), function(err, exists) {
if (exists) { callback("EXISTS"); return; }
// Hash the password using a salt.
bcrypt.gen_salt(10, function(err, salt) {
bcrypt.encrypt(password, salt, function(err, hash) {
client.set(k_user_password(email_address), hash, callback);
});
});
});
}
// Callback: function(err, pw_success).
// err will be set to "NOT_EXISTS" if the email_address doesn't exist in the database.
function user_password_correct(email_address, test_password, callback) {
assert(!!email_address);
assert(!!test_password);
assert(!!callback);
client.exists(k_user_password(email_address), function(err, exists) {
if (!exists) { callback("NOT_EXISTS", null); return; }
client.get(k_user_password(email_address), function(err, password) {
bcrypt.compare(test_password, password, callback);
});
});
}
// Callback: function(err, jobs).
// err will be set to "NOT_EXISTS" if the email_address doesn't exist in the database.
function get_user_jobs(email_address, callback) {
assert(!!email_address);
assert(!!callback);
client.exists(k_user_password(email_address), function(err, exists) {
if (!exists) { callback("NOT_EXISTS", null); return; }
client.smembers(k_user_jobs(email_address), callback);
});
}
// Input_data: Array [{ "k":k, "v":v }]
// Callback: function(err, job_id)
function new_job(email_address, input_data, replication_factor, map_code, reduce_code, blurb, callback) {
assert(!!callback);
assert(!!email_address);
assert(!!input_data);
assert(!!replication_factor);
assert(!!map_code);
assert(!!reduce_code);
assert(!!blurb);
// Need to initialize the map_input and work_queue.
var job_id = uuid.v4();
input_data.forEach(function(item) {
var chunk_id = uuid.v4();
client.hset(k_input(job_id), chunk_id, JSON.stringify(item));
client.hset(k_original_input(job_id), chunk_id, JSON.stringify(item));
for (var i = 0; i < replication_factor; i++) {
client.lpush(k_work_queue(job_id), chunk_id);
}
});
client.set(k_in_count(job_id), input_data.length);
client.set(k_out_count(job_id), 0);
client.set(k_replication_factor(job_id), replication_factor);
client.set(k_phase(job_id), "Map");
client.set(k_map_code(job_id), map_code);
client.set(k_reduce_code(job_id), reduce_code);
client.set(k_blurb(job_id), blurb);
client.sadd(k_runnable(), job_id);
client.sadd(k_user_jobs(email_address), job_id);
callback(null, job_id);
}
// Callback: function(err, job_id)
function fetch_job(callback) {
assert(!!callback);
client.srandmember(k_runnable(), callback);
}
// Callback should be function (err, job_id, chunk_id, chunk, phase, map/reduce_code).
function dequeue_work(callback) {
assert(!!callback);
var getPhaseSpecificCode = function(job_id, chunk_id, chunk) {
client.get(k_phase(job_id), function(err, phase) {
if (phase == "Map") {
client.get(k_map_code(job_id), function(err, map_code) {
callback(err, job_id, chunk_id, chunk, "Map", map_code);
});
} else if (phase == "Reduce") {
client.get(k_reduce_code(job_id), function(err, reduce_code) {
callback(err, job_id, chunk_id, chunk, "Reduce", reduce_code);
});
} else if (phase == "Finished") {
callback(null, null, null, null, null);
} else {
console.error("Incorrect phase. Phase = " + phase);
assert(false);
}
});
};
fetch_job(function(err, job_id) {
if (!job_id) { callback(null, null, null, null, null); return; }
client.lpop(k_work_queue(job_id), function(err, chunk_id) {
if (!chunk_id) { callback(null, null, null, null, null); return; }
client.hget(k_input(job_id), chunk_id, function(err, chunk) {
assert(!!chunk);
getPhaseSpecificCode(job_id, chunk_id, JSON.parse(chunk));
});
});
});
}
// Callback: function(err, results).
function results(job_id, callback) {
client.lrange(k_final_output(job_id), 0, -1, function(err, results) {
var returnedResults = [];
results.forEach(function(item) {
returnedResults.push(JSON.parse(item));
});
callback(err, returnedResults);
});
}
// Callback: function(err, phase).
function phase(job_id, callback) {
client.get(k_phase(job_id), callback);
}
// Callback: function(err, blurb).
function blurb(job_id, callback) {
client.get(k_blurb(job_id), callback);
}
// Callback: function(err, in_count, out_count)
function completion_status_for_current_phase(job_id, callback) {
client.get(k_in_count(job_id), function(err, in_count) {
client.get(k_out_count(job_id), function(err, out_count) {
callback(err, in_count, out_count);
});
});
}
function enqueue_work(job_id, chunk_id) {
assert(!!job_id);
assert(!!chunk_id);
client.lpush(k_work_queue(job_id), chunk_id);
}
// TODO: Destroy job function.
function enqueue_result(job_id, chunk_id, result, server_callback) {
assert(!!job_id);
assert(!!chunk_id);
assert(!!result);
var groupByComplete = function(groupByData) {
// Reset the 'in' count and 'out' count.
var newInCount = Object.keys(groupByData).length;
client.set(k_in_count(job_id), newInCount);
client.set(k_out_count(job_id), 0);
client.get(k_replication_factor(job_id), function(err, replication_factor) {
// Set up the k_input, work_queue and phase, to reflect the transition to "Reduce" stage.
for (var key in groupByData) {
console.log("key in groupByData " + key);
var chunk = {'k': key, 'v': groupByData[key]};
var f = function(k) {
client.hset(k_input(job_id), k, JSON.stringify(chunk), function(err) {
for (var i = 0; i < replication_factor; i++) {
console.log("Adding to work queue " + k + "for reduce stage.");
client.lpush(k_work_queue(job_id), k);
}
});
};
f(key);
}
logging.reduceStart();
client.set(k_phase(job_id), "Reduce");
});
};
var mapComplete = function() {
logging.mapComplete();
client.hkeys(k_input(job_id), function(err, chunk_ids) {
client.del(k_input(job_id)); // Reset the input.
// Do the group by phase of the shuffle operation in memory.
var groupByData = {};
var outputProcessedCount = 0;
chunk_ids.forEach(function(chunk_id) {
client.lpop(k_output(job_id, chunk_id), function(err, response) {
client.del(k_output(job_id, chunk_id));
var keyValueArray = JSON.parse(response);
keyValueArray.forEach(function(keyValuePair) {
var key = keyValuePair['k'];
if (!(key in groupByData)) {
groupByData[key] = [];
}
groupByData[key].push(keyValuePair['v']);
});
if (++outputProcessedCount == chunk_ids.length) {
groupByComplete(groupByData);
}
});
});
});
};
var jobFinished = function() {
// Cleanup.
logging.jobComplete();
console.log("JOB FINISHED. PERFORMING CLEANUP.")
client.srem(k_runnable(), job_id);
client.set(k_phase(job_id), "Finished");
client.del(k_in_count(job_id));
client.del(k_out_count(job_id));
client.del(k_input(job_id));
client.del(k_work_queue(job_id));
client.del(k_replication_factor(job_id));
}
var reduceComplete = function() {
logging.reduceComplete();
client.hkeys(k_input(job_id), function(err, chunk_ids) {
var outputProcessedCount = 0;
chunk_ids.forEach(function(chunk_id) {
client.lpop(k_output(job_id, chunk_id), function(err, response) {
client.del(k_output(job_id, chunk_id));
var keyValueArray = JSON.parse(response);
keyValueArray.forEach(function(keyValuePair) {
client.lpush(k_final_output(job_id), JSON.stringify(keyValuePair));
});
});
if (++outputProcessedCount == chunk_ids.length) {
jobFinished();
}
});
});
};
var phaseComplete = function() {
console.log("PHASE COMPLETE");
client.get(k_phase(job_id), function(err, phase) {
if (phase == "Map") {
mapComplete();
} else if (phase == "Reduce") {
reduceComplete();
} else {
console.error("BAD PHASE");
assert(false);
}
});
};
var responsesValidated = function() {
client.incr(k_out_count(job_id), function(err, updated_out_count) {
client.get(k_in_count(job_id), function(err, in_count) {
if (updated_out_count == in_count) {
phaseComplete();
}
});
});
};
var conflictingResponses = function() {
console.log("Conflicting responses came back from clients.");
console.log("Conflicting response for chunk " + chunk_id);
client.del(k_output(job_id, chunk_id)); // Reset the output.
client.get(k_replication_factor(job_id), function(err, replication_factor) {
for (var i = 0; i < replication_factor; i++) {
enqueue_work(job_id, chunk_id); // Re-add the chunks to the work queue.
}
});
};
var checkResponses = function() {
client.lrange(k_output(job_id, chunk_id), 0, -1, function(err, responses) {
assert(responses.length);
var first_response = JSON.parse(responses[0]);
for (var i = 1; i < responses.length; i++) {
if (!_.isEqual(first_response, JSON.parse(responses[i]))) {
conflictingResponses();
return;
}
}
responsesValidated();
});
};
client.get(k_replication_factor(job_id), function(err, replication_factor) {
client.lpush(k_output(job_id, chunk_id), JSON.stringify(result), function(err, len) {
server_callback();
if (len == replication_factor) {
// We can now ensure all the responses received for this chunk are identical.
checkResponses();
}
});
});
}
function jobs_available(callback){
client.scard(k_runnable(), function(err, card){
callback(err, card > 0);
});
}
// Public API export.
exports.new_job = new_job;
exports.dequeue_work = dequeue_work;
exports.enqueue_work = enqueue_work;
exports.client = client;
exports.fetch_job = fetch_job;
exports.enqueue_result = enqueue_result;
exports.new_user = new_user;
exports.user_password_correct = user_password_correct;
exports.get_user_jobs = get_user_jobs;
exports.results = results;
exports.phase = phase;
exports.blurb = blurb;
exports.completion_status_for_current_phase = completion_status_for_current_phase;
exports.jobs_available = jobs_available;