-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
368 lines (337 loc) · 10 KB
/
script.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
/* jshint globalstrict: true */
/* jshint browser: true */
/* jshint devel: true */
/* jshint jquery: true */
/* global util */
/* global raft */
/* global pbft */
/* global makeState */
'use strict';
var playback;
var render = {};
var state;
var record;
var replay;
var protocol;
//temporary var to indicate current active protocol, should be removed and use (protocol) later
var activeProtocol;
util.activate = function () {
//show sliders
$('.row').show();
//hide select menu
$('#algorithm-selection').hide();
var onReplayDone = undefined;
record = function (name) {
localStorage.setItem(name, state.exportToString());
};
replay = function (name, done) {
state.importFromString(localStorage.getItem(name));
render.update();
onReplayDone = done;
};
// All protocols have servers and messages represented in two arrays; state
// object is general across all protocols.
state = makeState({
servers: [],
messages: [],
});
//if true add delay to promise message in order to simulate livelock
if (protocol == paxos) {
state.current.liveLock = false;
}
var sliding = false;
playback = function () {
var paused = false;
var pause = function () {
paused = true;
$('#time-icon')
.removeClass('glyphicon-time')
.addClass('glyphicon-pause');
$('#pause').attr('class', 'paused');
render.update();
};
var resume = function () {
if (paused) {
paused = false;
$('#time-icon')
.removeClass('glyphicon-pause')
.addClass('glyphicon-time');
$('#pause').attr('class', 'resumed');
render.update();
}
};
return {
pause: pause,
resume: resume,
toggle: function () {
if (paused)
resume();
else
pause();
},
isPaused: function () {
return paused;
},
};
}();
(function () {
if (protocol == pbft) {
/* PBFT server IDs begin at zero, and are used to determine at each node
* whether the node is the current primary. */
for (var i = 0; i < protocol.NUM_NODES; i += 1) {
state.current.servers.push(protocol.server(i, protocol.makePeers(protocol.NUM_NODES)));
}
} else {
/* Raft and Paxos are designed wih server indices beginning at 1. */
for (var i = 1; i <= protocol.NUM_SERVERS; i += 1) {
var peers = [];
for (var j = 1; j <= protocol.NUM_SERVERS; j += 1) {
if (i != j)
peers.push(j);
}
state.current.servers.push(protocol.server(i, peers));
}
}
})();
//hardcode 1st server to be client and the 2nd server to be proposer
(function () {
if (activeProtocol == 'paxos') {
state.current.servers.forEach(function (server) {
if (server.id == 1) {
server.state = 'client';
} else if (server.id == 2) {
server.state = 'proposer';
}
})
}
})();
var svg = $('svg');
// Ring is only rendered once, so no need to include it in render.update below.
render.ring = protocol.render.ring;
render.ring(svg);
protocol.appendServerInfo(state, svg);
var timeSlider;
render.clock = function () {
if (!sliding || activeProtocol == 'raft') {
timeSlider.slider('setAttribute', 'max', state.getMaxTime());
timeSlider.slider('setValue', state.current.time, false);
}
};
render.servers = protocol.render.servers;
render.logs = protocol.render.logs;
render.messages = protocol.render.messages;
// Transforms the simulation speed from a linear slider
// to a logarithmically scaling time factor.
var speedSliderTransform = function (v) {
v = Math.pow(10, v);
if (v < 1)
return 1;
else
return v;
};
var lastRenderedO = null;
var lastRenderedV = null;
render.update = function () {
// Same indicates both underlying object identity hasn't changed and its
// value hasn't changed.
var serversSame = false;
var messagesSame = false;
if (lastRenderedO == state.current) {
serversSame = util.equals(lastRenderedV.servers, state.current.servers);
messagesSame = util.equals(lastRenderedV.messages, state.current.messages);
}
lastRenderedO = state;
lastRenderedV = state.base();
render.clock();
render.servers(serversSame, svg);
render.messages(messagesSame, svg);
if (!serversSame) {
if (activeProtocol == 'pbft') {
render.logs(svg, state.current);
} else {
render.logs(svg);
}
}
};
// Time advancement, and translating wall clock time to a "model" time based
// on the scaling selected is done in the following function.
(function () {
var last = null;
var step = function (timestamp) {
if (!playback.isPaused() && last !== null && timestamp - last < 500) {
var wallMicrosElapsed = (timestamp - last) * 1000;
var speed = speedSliderTransform($('#speed').slider('getValue'));
var modelMicrosElapsed = wallMicrosElapsed / speed;
var modelMicros = state.current.time + modelMicrosElapsed;
state.seek(modelMicros);
if (modelMicros >= state.getMaxTime() && onReplayDone !== undefined) {
var f = onReplayDone;
onReplayDone = undefined;
f();
}
render.update();
}
last = timestamp;
window.requestAnimationFrame(step);
};
window.requestAnimationFrame(step);
})();
$(window).keyup(function (e) {
// Disable if modal is brought up.
if ($('.modal').hasClass('in')) {
return;
}
if (e.target.id == "title") {
return;
}
var leader = null;
if (protocol == raft) {
leader = protocol.getLeader();
}
if (e.keyCode == ' '.charCodeAt(0) ||
e.keyCode == 190) {// dot, emitted by Logitech remote
$('.modal').modal('hide');
playback.toggle();
} else if (e.keyCode == 'C'.charCodeAt(0)) {
if (activeProtocol == 'pbft'){
state.fork();
playback.pause();
pbft.clientRequest(state.current);
state.save();
playback.resume();
$('.modal').modal('hide');
}
//if paxos send clientRequestMessage
if (activeProtocol == 'paxos') {
state.fork();
playback.pause();
protocol.sendClientRequest(state.current);
state.save();
playback.resume();
$('.modal').modal('hide');
}
if (leader !== null) {
state.fork();
protocol.clientRequest(state.current, leader);
state.save();
render.update();
$('.modal').modal('hide');
}
} else if (e.keyCode == 'R'.charCodeAt(0)) {
if (leader !== null) {
state.fork();
protocol.stop(state.current, leader);
protocol.resume(state.current, leader);
state.save();
render.update();
$('.modal').modal('hide');
}
} else if (e.keyCode == 'T'.charCodeAt(0)) {
state.fork();
protocol.spreadTimers(state.current);
state.save();
render.update();
$('.modal').modal('hide');
} else if (e.keyCode == 'A'.charCodeAt(0)) {
state.fork();
protocol.alignTimers(state.current);
state.save();
render.update();
$('.modal').modal('hide');
} else if (e.keyCode == 'L'.charCodeAt(0)) {
state.fork();
playback.pause();
protocol.setupLogReplicationScenario(state.current);
state.save();
render.update();
$('.modal').modal('hide');
} else if (e.keyCode == 'B'.charCodeAt(0)) {
state.fork();
protocol.resumeAll(state.current);
state.save();
render.update();
$('.modal').modal('hide');
} else if (e.keyCode == 'F'.charCodeAt(0)) {
state.fork();
render.update();
$('.modal').modal('hide');
} else if (e.keyCode == 191 && e.shiftKey) { // question mark
playback.pause();
$('#modal-help').modal('show');
} else if (e.keyCode == 'Z'.charCodeAt(0)) {
state.fork();
util.resetStates(state.current);
state.save();
render.update();
$('#modal-help').modal('hide');
} else if (e.keyCode == 'X'.charCodeAt(0)) {
if (protocol == paxos) {
if (window.confirm("Do you want to enter livelock mode?")) {
if (state.current.liveLock == false) {
state.current.liveLock = true;
protocol.liveLockSenario(state.current);
} else {
state.current.liveLock = false;
}
var lock = document.getElementById("lock-icon");
if (lock.style.display === "none") {
lock.style.display = "block";
} else {
lock.style.display = "none";
}
}
}
}
});
$('#modal-details').on('show.bs.modal', function (e) {
playback.pause();
});
$("#speed").slider({
tooltip: 'always',
formater: function (value) {
return '1/' + speedSliderTransform(value).toFixed(0) + 'x';
},
reversed: true,
});
timeSlider = $('#time');
timeSlider.slider({
tooltip: 'always',
formater: function (value) {
return (value / 1e6).toFixed(3) + 's';
},
});
timeSlider.on('slideStart', function () {
playback.pause();
sliding = true;
});
timeSlider.on('slideStop', function () {
// If you click rather than drag, there won't be any slide events, so you
// have to seek and update here too.
state.seek(timeSlider.slider('getValue'));
sliding = false;
render.update();
});
timeSlider.on('slide', function () {
state.seek(timeSlider.slider('getValue'));
render.update();
});
$('#time-button')
.click(function () {
playback.toggle();
return false;
});
// Disabled for now, they don't seem to behave reliably.
// // enable tooltips
// $('[data-toggle="tooltip"]').tooltip();
state.updater = function (state) {
protocol.update(state.current);
var time = state.current.time;
var base = state.base(time);
state.current.time = base.time;
var same = util.equals(state.current, base);
state.current.time = time;
return !same;
};
state.init();
render.update();
};