Skip to content

Commit

Permalink
Add primitive record/replay features
Browse files Browse the repository at this point in the history
These currently need to be accessed in the JS console and record things
into localStorage.

Towards #8: add record (screencast) feature
  • Loading branch information
ongardie committed Jun 1, 2014
1 parent 95e92cd commit 9003192
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
14 changes: 7 additions & 7 deletions raft.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var stepDown = function(model, server, term) {
server.term = term;
server.state = 'follower';
server.votedFor = null;
if (server.electionAlarm < model.time || server.electionAlarm == Infinity) {
if (server.electionAlarm < model.time || server.electionAlarm == util.Inf) {
server.electionAlarm = makeElectionAlarm(model.time);
}
};
Expand Down Expand Up @@ -112,9 +112,9 @@ rules.becomeLeader = function(model, server) {
//console.log('server ' + server.id + ' is leader in term ' + server.term);
server.state = 'leader';
server.nextIndex = util.makeMap(server.peers, server.log.length + 1);
server.rpcDue = util.makeMap(server.peers, Infinity);
server.rpcDue = util.makeMap(server.peers, util.Inf);
server.heartbeatDue = util.makeMap(server.peers, 0);
server.electionAlarm = Infinity;
server.electionAlarm = util.Inf;
}
};

Expand Down Expand Up @@ -177,7 +177,7 @@ var handleRequestVoteReply = function(model, server, reply) {
stepDown(model, server, reply.term);
if (server.state == 'candidate' &&
server.term == reply.term) {
server.rpcDue[reply.from] = Infinity;
server.rpcDue[reply.from] = util.Inf;
server.voteGranted[reply.from] = reply.granted;
}
};
Expand Down Expand Up @@ -263,7 +263,7 @@ raft.update = function(model) {
model.messages.forEach(function(message) {
if (message.recvTime <= model.time)
deliver.push(message);
else if (message.recvTime < Infinity)
else if (message.recvTime < util.Inf)
keep.push(message);
});
model.messages = keep;
Expand Down Expand Up @@ -320,7 +320,7 @@ raft.spreadTimers = function(model) {
var timers = [];
model.servers.forEach(function(server) {
if (server.electionAlarm > model.time &&
server.electionAlarm < Infinity) {
server.electionAlarm < util.Inf) {
timers.push(server.electionAlarm);
}
});
Expand Down Expand Up @@ -351,7 +351,7 @@ raft.alignTimers = function(model) {
var timers = [];
model.servers.forEach(function(server) {
if (server.electionAlarm > model.time &&
server.electionAlarm < Infinity) {
server.electionAlarm < util.Inf) {
timers.push(server.electionAlarm);
}
});
Expand Down
19 changes: 18 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@
var playback;
var render = {};
var state;
var record;
var replay;

$(function() {

var ARC_WIDTH = 5;

var onReplayDone = undefined;
record = function(name) {
localStorage.setItem(name, state.exportToString());
};
replay = function(name, done) {
state.importFromString(localStorage.getItem(name));
render.update();
onReplayDone = done;
};

state = makeState({
servers: [],
messages: [],
Expand Down Expand Up @@ -460,7 +472,7 @@ render.messages = function(messagesSame) {
};

var relTime = function(time, now) {
if (time == Infinity)
if (time == util.Inf)
return 'infinity';
var sign = time > now ? '+' : '';
return sign + ((time - now) / 1e3).toFixed(3) + 'ms';
Expand Down Expand Up @@ -615,6 +627,11 @@ render.update = function() {
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;
Expand Down
20 changes: 19 additions & 1 deletion state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var makeState = function(initial) {
};
var self = {
current: initial,
_checkpoints: checkpoints,
getMaxTime: function() {
return maxTime;
},
Expand Down Expand Up @@ -51,6 +50,25 @@ var makeState = function(initial) {
}
},
updater: function() { return false; },
exportToString: function() {
return JSON.stringify({
checkpoints: checkpoints,
maxTime: maxTime,
});
},
importFromString: function(s) {
var o = JSON.parse(s);
checkpoints = o.checkpoints
maxTime = o.maxTime;
self.current = util.clone(checkpoints[0]);
self.current.time = 0;
},
clear: function() {
checkpoints = [];
self.current = initial;
self.current.time = 0;
maxTime = 0;
},
};
self.current.time = 0;
return self;
Expand Down
4 changes: 4 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

var util = {};

// Really big number. Infinity is problematic because
// JSON.stringify(Infinity) returns 'null'.
util.Inf = 1e300;

util.value = function(v) {
return function() { return v; };
};
Expand Down

0 comments on commit 9003192

Please sign in to comment.