Skip to content

Commit

Permalink
增加debug log的内容
Browse files Browse the repository at this point in the history
  • Loading branch information
huahuaxiaomuzhu committed Apr 23, 2022
1 parent b6789dc commit 9155bf9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ <h2></h2>
<ul class="dropdown-menu" role="menu">
</ul>
</div>

<div>
<ul id="debug-log">
</ul>
</div>
</body>
</html>
14 changes: 12 additions & 2 deletions raft.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ var BATCH_SIZE = 3;
//TODO 加入消息丢失的模拟
if(Math.random()>0.1){
model.messages.push(message);
model.debugLogs.unshift(`${JSON.stringify(message)} transferred successfully`)
if (model.debugLogs.length>9){
model.debugLogs.splice(-1,1);
}
}else{
model.messages.push({});
model.debugLogs.unshift(`${JSON.stringify(message)} missed!`)
if (model.debugLogs.length>9){
model.debugLogs.splice(-1,1);
}

}
};

Expand Down Expand Up @@ -190,10 +199,11 @@ var BATCH_SIZE = 3;
//处理拉票响应
var handleRequestVoteReply = function(model, server, reply) {
if (server.term < reply.term)
stepDown(model, server, reply.term); //如果接收到的 RPC 请求或响应中,任期号T > currentTerm,则令 currentTerm = T,并切换为跟随者状态(5.1 节)
stepDown(model, server, reply.term);
//如果接收到的 RPC 请求或响应中,任期号T > currentTerm,则令 currentTerm = T,并切换为跟随者状态(5.1 节)
if (server.state == 'candidate' &&
server.term == reply.term) {
server.rpcDue[reply.from] = util.Inf;
server.rpcDue[reply.from] = util.Inf; //收到reply代表的选票结果,暂时不允许RPC超时
server.voteGranted[reply.from] = reply.granted;
}
};
Expand Down
12 changes: 11 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ replay = function(name, done) {
state = makeState({
servers: [],
messages: [],
debugLogs:[]
});

var sliding = false;
Expand Down Expand Up @@ -398,7 +399,15 @@ render.logs = function() {
}
});
};

render.debugLogs=function () {
var debugListDom=$('#debug-log');
debugListDom.empty();
state.current.debugLogs.forEach(function (log, index) {
var liDom =$('<li></li>')
liDom.attr('id','log-'+index).append(log)
debugListDom.append(liDom)
})
}
render.messages = function(messagesSame) {
var messagesGroup = $('#messages', svg);
if (!messagesSame) {
Expand Down Expand Up @@ -621,6 +630,7 @@ render.update = function() {
render.clock();
render.servers(serversSame);
render.messages(messagesSame);
render.debugLogs();
if (!serversSame)
render.logs();
};
Expand Down
1 change: 1 addition & 0 deletions state.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var makeState = function(initial) {
};
var self = {
current: initial,

getMaxTime: function() {
return maxTime;
},
Expand Down

0 comments on commit 9155bf9

Please sign in to comment.