This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
797 lines (696 loc) · 29.9 KB
/
server.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
module.exports.create = function(port, log) {
var express = require('express');
var _ = require('underscore');
var async = require('async');
var request = require('request');
var DROP_PROBABILITY = 0.0;
var DROPPED_ERROR = "DROPPEDPACKET";
var TIMEOUT_ERROR = "ETIMEDOUT";
var PEERS_DOWN = {accepted: false, message: "PEERS ARE DOWN"};
var REQUEST_TIMEOUT = 2000;
var NUM_PEERS = 0;
var PEERS = {};
var NO_VALUE = "__0xDEADBEEF";
var TEST_VALUE = "__0xBEEFDEAD";
var VALUE_NOT_FOUND = "NO VALUE FOUND";
var LEARN_TIMEOUT = 2000;
// Create the app server
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
/****** UTILITY FUNCTIONS ******/
// Whether or not we should drop a packet
var shouldDrop = function() {
var random = Math.random();
return DROP_PROBABILITY >= random;
}
// Packet dropping middleware
function dropPacket(req, res, next) {
if (shouldDrop()) {
log("Dropping packet on purpose");
next(DROPPED_ERROR);
}
else {
next();
}
}
// Figure out if a proposal is greater than another
var greaterThan = function(left, right) {
// A proposal is greater than or equal to another if:
// 1. it is the same (both the number and peer index are equal)
// 2. The number is higher
// 3. The number is the same but the peer is higher
if (left.number > right.number) {
return true;
}
else if (left.number === right.number && left.peer > right.peer) {
return true;
}
else if (left.number === right.number && left.peer === right.peer) {
return true;
}
else {
return false;
}
};
// Create a local version of a peer,
// setting up an easy way to send it data
// with a default timeout
var createPeer = function(port) {
var host = "http://localhost:" + port;
return {
send: function(path, content, callback) {
request.post(
{
url: host + path,
json: content,
timeout: REQUEST_TIMEOUT
},
function(err, response) {
callback = callback || function() {};
if(shouldDrop()) {
log("Dropping packet on purpose");
callback(null, {body: {error: DROPPED_ERROR}});
}
else {
callback(err, response);
}
}
);
},
port: port
};
}
/*******************************/
/*******************************/
/*******************************/
/*******************************/
/******* PAXOS FUNCTIONS *******/
/*******************************/
// Paxos
// For each (name, instance) pair,
// store the latest proposal we've
// promised
var RECEIVED_PROPOSALS = {};
// For each (name, instance) pair,
// store the latest (proposal, value)
// we've accepted.
var RECEIVED_ACCEPTS = {};
// For each (name, instance) pair,
// store what value we have learnt
var FULLY_LEARNT_VALUES = {};
// For each (name, instance) pair,
// keep track of how many times we've
// learnt some value, so we know
// when a majority of acceptors
// has accepted a single value
var TENTATIVELY_FULLY_LEARNT_VALUES = {};
var FULLY_LEARNT_LISTENERS = {};
// For each name, store what instance
// number we think we're on
var CURRENT_INSTANCE = {};
// For each (name, instance) pair, track
// what proposal number we should use next
var PROPOSAL_COUNTER = {};
var handlePropose = function(name, instance, proposal) {
if (RECEIVED_ACCEPTS[name].hasOwnProperty(instance)) {
// We have already accepted something for this
// instance
var accepted = RECEIVED_ACCEPTS[name][instance];
var acceptedProposal = accepted.proposal;
var acceptedValue = accepted.value;
// We also need to get our minimum promised proposal
// number
var promisedProposal = RECEIVED_PROPOSALS[name][instance].proposal;
log(" ", "previously accepted:", promisedProposal, "--", acceptedValue);
if (greaterThan(proposal, promisedProposal)) {
// The proposal is higher than our accepted proposal,
// so we respond with the previously accepted proposal,
// and the value
// However, we also need to note this new proposal,
// because we promised not to accept anything less than it
RECEIVED_PROPOSALS[name][instance].proposal = proposal;
log(" ", "promising (already accepted)", proposal, " -- ", acceptedValue);
return {
name: name,
peer: port,
promise: true,
highestProposal: promisedProposal,
value: acceptedValue
};
}
else {
// This proposal number is less, so we reject it,
// noting our current highest proposal number and the
// value we already accepted
log(" ", "rejecting (already accepted)", promisedProposal, " -- ", acceptedValue);
return {
name: name,
peer: port,
promise: false,
highestProposal: promisedProposal,
value: acceptedValue
};
}
}
else if (RECEIVED_PROPOSALS[name].hasOwnProperty(instance)) {
// We have received a previous proposal for this
// instance, but we haven't accepted any value yet
var promisedProposal = RECEIVED_PROPOSALS[name][instance].proposal;
log(" ", "previously promised:", promisedProposal);
if (greaterThan(proposal, promisedProposal)) {
// The proposal is igher than our previously
// promised proposal, so we promise not to accept
// anything higher than it
RECEIVED_PROPOSALS[name][instance].proposal = proposal;
log(" ", "promising", proposal);
return {
name: name,
peer: port,
promise: true,
highestProposal: proposal,
value: NO_VALUE
};
}
else {
// This proposal number is less than the one
// we promised, so we reject it, and return
// the one we promised
log(" ", "rejecting", proposal);
return {
name: name,
peer: port,
promise: false,
highestProposal: promisedProposal,
value: NO_VALUE
};
}
}
else {
// This is a brand new instance for us,
// so we can do whatever we want
// Store that this is the highest proposal number
// we've seen
RECEIVED_PROPOSALS[name][instance] = {
proposal: proposal,
value: NO_VALUE
};
log(" ", "first proposal, accepting", proposal);
// And return a promise saying we accept it
return {
name: name,
peer: port,
promise: true,
highestProposal: proposal,
value: NO_VALUE
};
}
};
var handleAccept = function(name, instance, proposal, value) {
var promisedProposal = RECEIVED_PROPOSALS[name][instance].proposal;
if (greaterThan(proposal, promisedProposal)) {
// We haven't promised a higher proposal,
// so we can still accept this request
log(" ", "beginning to accept");
if (RECEIVED_ACCEPTS[name].hasOwnProperty(instance)) {
// We're checking to see if we already accepted
// something previously. This is just for logging
// purposes
var acceptedProposal = RECEIVED_ACCEPTS[name][instance].proposal;
var acceptedValue = RECEIVED_ACCEPTS[name][instance].value;
log(" ", "previously accepted:", acceptedProposal, acceptedValue);
}
// Store the accepted proposal and value
// in the accept store
RECEIVED_ACCEPTS[name][instance] = {
proposal: proposal,
value: value
};
// Now we need to send out learn requests
_.each(PEERS, function(peer) {
peer.send(
"/learn",
{
name: name,
instance: instance,
value: value,
peer: port
}
)
});
return {
accepted: true,
peer: port
};
log(" ", "accepted:", proposal, value);
}
else {
// We've already promised to another higher
// proposal, so we just do nothing
log(" ", "rejected:", promisedProposal);
return {
accepted: false,
peer: port
};
}
};
var handleLearn = function(name, instance, value, peer) {
if (FULLY_LEARNT_VALUES[name].hasOwnProperty(instance)) {
// We've already fully learned a value,
// because we received a quorum for it
log(" ", "ignoring, previously fully learned:", name, FULLY_LEARNT_VALUES[name][instance]);
return;
}
// Track how many acceptors accepted
// a particular value
var numAcceptors = 0;
// Get the learnt information for this particular instance (or initialize it)
var learned = TENTATIVELY_FULLY_LEARNT_VALUES[name][instance] = (TENTATIVELY_FULLY_LEARNT_VALUES[name][instance] || {});
if (learned.hasOwnProperty(value)) {
// We've already seen this value for this instance, so
// we just increment the value
numAcceptors = (++learned[value]);
}
else {
// First time we've seen this value, so we set it to 1
numAcceptors = learned[value] = 1;
}
if (numAcceptors >= Math.floor((NUM_PEERS / 2) + 1)) {
// More than half the acceptors have accepted
// this particular value, so we have now fully
// learnt it
log(" ", "fully learned: (", name, ",", instance, ",", value, ")");
FULLY_LEARNT_VALUES[name][instance] = value;
if (instance >= CURRENT_INSTANCE[name]) {
// The instance number is higher than the one
// we have locally, which could happen if we got
// out of sync. As such, we set our own instance
// number to one higher.
log(" ", "setting instance:", instance + 1);
CURRENT_INSTANCE[name] = instance + 1;
}
if (FULLY_LEARNT_LISTENERS[name][instance] !== null) {
_.each(FULLY_LEARNT_LISTENERS[name][instance], function(listener) {
log(" ", "dispatching to listener");
listener(value);
});
FULLY_LEARNT_LISTENERS[name][instance] = null;
}
}
};
var initiateAccept = function(name, instance, proposal, value, originalValue, finalResponse) {
// Create a set of tasks, where each task is sending the ACCEPT
// message to a specific peer
var acceptTasks = {};
_.each(PEERS, function(peer) {
log("SEND ACCEPT(", name, ",", instance, ",", proposal, ",", value, ")", "from", port);
acceptTasks[peer.port] = function(done) {
peer.send(
"/accept",
{
name: name,
instance: instance,
proposal: proposal,
value: value
},
function(err, response) {
var received = null;
if (err && err.code === TIMEOUT_ERROR) {
// If we received a timeout, then
// simply mark this, and keep going
log("RECEIVED accept-response timeout from", peer.port);
received = TIMEOUT_ERROR;
}
else if (response.body.error === DROPPED_ERROR) {
// If we received a drop message response,
// then simply mark this, and keep going
log("RECEIVED accept-response drop from", peer.port);
received = DROPPED_ERROR;
}
else {
// If we received an actual value, then
// simply mark this, and keep going
log("RECEIVED accept-response from", peer.port);
received = response.body;
}
done(null, received);
}
);
};
});
// Execute all the ACCEPT tasks
async.parallel(acceptTasks, function(err, accepted) {
// Note how many people promised
var numAccepted = 0;
var numPeersDown = 0;
_.each(accepted, function(response) {
if (response === TIMEOUT_ERROR || response === DROPPED_ERROR) {
// Let's count how many people died on us, so we know
// whether we should keep trying or if this is a catastrophic
// failure
numPeersDown++;
return;
}
else if (response.accepted) {
numAccepted++;
}
});
// If less than a majority accepted,
// then we start over
if (numAccepted >= Math.floor(NUM_PEERS / 2 + 1)) {
log("majority accepted", accepted);
finalResponse(null, {accepted: true, instance: instance});
}
else if (numPeersDown >= Math.floor(NUM_PEERS / 2 + 1)) {
// This is a catastrophic failure, let's cut our losses
// More than half our peers seem to be down, so we just
// respond to the client immediately
finalResponse(PEERS_DOWN);
}
else {
log("majority rejected", accepted);
initiateProposal(name, instance, originalValue, finalResponse);
}
});
};
var initiateProposal = function(name, instance, originalValue, finalResponse) {
var value = originalValue;
var number = PROPOSAL_COUNTER[instance] = (PROPOSAL_COUNTER[instance] || 0) + 1;
var proposal = {
peer: port,
number: number
};
// Create a set of tasks, where each task is sending the PROPOSE
// message to a specific peer
var proposeTasks = {};
_.each(PEERS, function(peer) {
proposeTasks[peer.port] = function(done) {
log("SEND PROPOSE(", name, ",", instance, ",", proposal, ")", "from", port);
peer.send(
"/propose",
{
name: name,
instance: instance,
proposal: proposal
},
function(err, response) {
var received = null;
if (err && err.code === TIMEOUT_ERROR) {
// If we received a timeout, then
// simply mark this, and keep going
log("RECEIVED propose-response timeout from", peer.port);
received = TIMEOUT_ERROR;
}
else if (response.body.error === DROPPED_ERROR) {
// If we received a drop message response,
// then simply mark this, and keep going
log("RECEIVED propose-response drop from", peer.port);
received = DROPPED_ERROR;
}
else {
// If we received an actual value, then
// simply mark this, and keep going
log("RECEIVED propose-response from", peer.port);
received = response.body;
}
done(null, received);
}
);
};
});
// Execute all the PROPOSE tasks
async.parallel(proposeTasks, function(err, received) {
// Keep track of the highest overall proposal
var highestProposal = { number: -1, peer: -1};
// Keep track of the highest proposal that had a value
// attached, and the value
var highestProposalWithValue = { number: -1, peer: -1};
var newValue = null;
// Note how many people promised
var numPromised = 0;
var numPeersDown = 0;
_.each(received, function(response) {
if (response === TIMEOUT_ERROR || response === DROPPED_ERROR) {
// Let's count how many people died on us, so we know
// whether we should keep trying or if this is a catastrophic
// failure
numPeersDown++;
return;
}
else if (response.promise) {
// OK, they promised to uphold our proposal
numPromised++;
if (response.value !== NO_VALUE) {
// This response had a value, so we see if it is greater than
// our previous proposal
if (greaterThan(response.highestProposal, highestProposalWithValue)) {
highestProposalWithValue = response.highestProposal;
value = response.value;
log("Switching to value", value, "from", highestProposalWithValue.peer);
}
}
}
else {
// They rejected our proposal, and so we note what proposal
// they return so we can set ours up
console.log(response);
if (greaterThan(response.highestProposal, highestProposal)) {
highestProposal = response.highestProposal;
}
}
});
if (numPromised >= Math.floor(NUM_PEERS / 2 + 1)) {
// The proposal was accepted by a majority - hurrah!
// We now send the ACCEPT requests to each acceptor.
log("Proposal accepted by majority");
if (value !== originalValue && originalValue !== TEST_VALUE) {
// If we changed values, we still need to try and store
// the original value the user sent us,
// so we simply go again
initiateProposal(name, CURRENT_INSTANCE[name]++, originalValue, finalResponse);
// We reset the final response in the case where the value changed,
// so that we only respond for the original request coming in from the
// client, not for this intermediate value we are storing
finalResponse = function() {};
}
else if (value !== originalValue && originalValue === TEST_VALUE) {
// If the original value is null and the proposal was promised,
// then we don't want to initiate a new proposal for the TEST_VALUE value,
// and we don't want to do anything for the new value, as we
// will simply learn it
finalResponse = function() {};
}
else if (originalValue === TEST_VALUE && value === TEST_VALUE) {
// If this is a test value (because we're trying to force a learn cycle),
// and our value was accepted just like that, then we simply
// return that there is no value, and short circuit
finalResponse(null, VALUE_NOT_FOUND);
return;
}
// Initiate the ACCEPT phase, but if we changed
initiateAccept(name, instance, proposal, value, originalValue, finalResponse);
}
else if (numPeersDown >= Math.floor(NUM_PEERS / 2 + 1)) {
// This is a catastrophic failure, let's cut our losses
// More than half our peers seem to be down, so we just
// respond to the client immediately
finalResponse(PEERS_DOWN);
}
else {
// We failed to update because somebody beat us in terms
// of proposal numbers, so we just try again with a higher
// proposal number
var newNumber = highestProposal.number + 1;
log("Proposal rejected, setting new proposal number:", newNumber);
PROPOSAL_COUNTER[instance] = newNumber;
initiateProposal(name, instance, originalValue, finalResponse);
}
});
};
var initializeStorageForName = function(name) {
// If we haven't seen this name before,
// then we initialize all our storage for it
if (!RECEIVED_PROPOSALS.hasOwnProperty(name)) {
log("INITIALIZED STORAGE FOR", name);
RECEIVED_PROPOSALS[name] = {};
RECEIVED_ACCEPTS[name] = {};
FULLY_LEARNT_VALUES[name] = {};
TENTATIVELY_FULLY_LEARNT_VALUES[name] = {};
CURRENT_INSTANCE[name] = 1;
FULLY_LEARNT_LISTENERS[name] = {};
}
};
/*******************************/
/*******************************/
/*******************************/
// Routes
app.post('/setup', function(req, res) {
log("received SETUP");
var peerPorts = req.body.peers;
for(var i = 0; i < peerPorts.length; i++) {
var peerPort = peerPorts[i];
PEERS[peerPort] = createPeer(peerPort);
}
// Store the number of peers
NUM_PEERS = peerPorts.length;
res.send();
});
app.post('/drop', function(req, res) {
log("Drop probability changed from", DROP_PROBABILITY, "to", req.body.probability);
DROP_PROBABILITY = req.body.probability;
res.send();
});
app.post('/store', function(req, res) {
var data = req.body;
var name = data.name;
var value = data.value;
log("Received STORE(", name, ",", value, ")");
// Initialize our storage for this name
initializeStorageForName(name);
// Get the next proposal number and build the proposal
var instance = CURRENT_INSTANCE[name]++;
initiateProposal(name, instance, value, function(err, result) {
if (err) {
// If there was an error, let's make it as if this never
// never happened
CURRENT_INSTANCE[name]--;
delete PROPOSAL_COUNTER[name];
res.json(err);
}
else {
res.json(result);
}
});
});
app.post('/fetch', function(req, res) {
var data = req.body;
var name = data.name;
var instance = data.instance;
log("Received FETCH(", name, ",", instance, ")");
// Initialize our storage for this name
initializeStorageForName(name);
if (FULLY_LEARNT_VALUES[name][instance] !== null && FULLY_LEARNT_VALUES[name][instance] !== undefined) {
log("FETCH SHORTCIRCUIT")
// If we have a fully learnt value, we don't need to
// do a paxos round
res.json({
name: name,
instance: instance,
value: FULLY_LEARNT_VALUES[name][instance]
});
}
else {
// We need to queue up a listener when we've fully learnt the
// value
var listeners = FULLY_LEARNT_LISTENERS[name][instance] = (FULLY_LEARNT_LISTENERS[name][instance] || []);
listeners.push(function(value) {
if (res !== null) {
log("FETCH listener invoked!");
res.json({
found: true,
name: name,
instance: instance,
value: value
});
}
else {
log("FETCH listener invoked too late!")
}
});
setTimeout(LEARN_TIMEOUT, function() {
log("FETCH timeout!");
res.json({
found: false,
name: name,
instance: instance,
message: "Timeout"
});
res = null;
});
// OK, we don't have a value, so we initiate a round for this
// (name, instance) pair.
initiateProposal(name, instance, TEST_VALUE, function(err, result) {
if (err) {
res.json(err);
}
else {
if (result === VALUE_NOT_FOUND) {
res.send({found: false}, 404)
}
else {
res.send({found: false, message: "WTF: " + result}, 500);
}
}
res = null;
});
}
});
app.post('/propose', dropPacket, function(req, res) {
var data = req.body;
var name = data.name;
var instance = data.instance;
var proposal = data.proposal;
log("RECEIVE PROPOSE(", name, ",", instance, ",", proposal, ")");
// Initialize our storage for this name
initializeStorageForName(name);
var result = handlePropose(name, instance, proposal);
if (shouldDrop()) {
log("Dropping packet on purpose");
throw DROPPED_ERROR;
}
else {
res.json(result);
}
log("END PROPOSE");
});
app.post('/accept', dropPacket, function(req, res) {
var data = req.body;
var name = data.name;
var instance = data.instance;
var proposal = data.proposal;
var value = data.value;
log("RECEIVE ACCEPT(", name, ",", instance, ",", proposal, ",", value, ")");
// Initialize our storage for this name
initializeStorageForName(name);
var result = handleAccept(name, instance, proposal, value);
if (shouldDrop()) {
log("Dropping packet on purpose");
throw DROPPED_ERROR;
}
else {
res.json(result);
}
log("END ACCEPT");
});
app.post('/learn', dropPacket, function(req, res) {
var data = req.body;
var name = data.name;
var instance = data.instance;
var value = data.value;
var peer = data.peer;
log("RECEIVE LEARN(", name, ",", instance, ",", value, ",", peer, ")");
// Initialize our storage for this name
initializeStorageForName(name);
var result = handleLearn(name, instance, value, peer);
if (shouldDrop()) {
log("Dropping packet on purpose");
throw DROPPED_ERROR;
}
else {
res.json(result);
}
log("END LEARN");
});
app.listen(port, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
}