Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29dc769

Browse files
committed
Allow claiming leadership in a single-peer scenario.
1 parent3647652 commit29dc769

File tree

1 file changed

+34
-22
lines changed
  • contrib/raftable/raft/src

1 file changed

+34
-22
lines changed

‎contrib/raftable/raft/src/raft.c‎

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,44 @@ static void raft_beat(raft_t r, int dst) {
499499
free(m);
500500
}
501501

502+
staticvoidraft_reset_bytes_acked(raft_tr) {
503+
for (inti=0;i<r->config.peernum_max;i++) {
504+
r->peers[i].acked.bytes=0;
505+
}
506+
}
507+
508+
staticvoidraft_reset_silent_time(raft_tr,intid) {
509+
for (inti=0;i<r->config.peernum_max;i++) {
510+
if ((i==id)|| (id==NOBODY)) {
511+
r->peers[i].silent_ms=0;
512+
}
513+
}
514+
}
515+
516+
// Returns true if we got the support of a majority and became the leader
517+
staticboolraft_become_leader(raft_tr) {
518+
if (r->votes*2>r->peernum) {
519+
// got the support of a majority
520+
r->role=LEADER;
521+
r->leader=r->me;
522+
raft_reset_bytes_acked(r);
523+
raft_reset_silent_time(r,NOBODY);
524+
raft_reset_timer(r);
525+
shout("became the leader\n");
526+
return true;
527+
}
528+
return false;
529+
}
530+
502531
staticvoidraft_claim(raft_tr) {
503532
assert(r->role==CANDIDATE);
504533
assert(r->leader==NOBODY);
505534

506535
r->votes=1;// vote for self
536+
if (raft_become_leader(r)) {
537+
// no need to send any messages, since we are alone
538+
return;
539+
}
507540

508541
raft_msg_claim_tm;
509542

@@ -995,20 +1028,6 @@ static void raft_handle_claim(raft_t r, raft_msg_claim_t *m) {
9951028
raft_send(r,candidate,&reply,sizeof(reply));
9961029
}
9971030

998-
staticvoidraft_reset_bytes_acked(raft_tr) {
999-
for (inti=0;i<r->config.peernum_max;i++) {
1000-
r->peers[i].acked.bytes=0;
1001-
}
1002-
}
1003-
1004-
staticvoidraft_reset_silent_time(raft_tr,intid) {
1005-
for (inti=0;i<r->config.peernum_max;i++) {
1006-
if ((i==id)|| (id==NOBODY)) {
1007-
r->peers[i].silent_ms=0;
1008-
}
1009-
}
1010-
}
1011-
10121031
staticvoidraft_handle_vote(raft_tr,raft_msg_vote_t*m) {
10131032
intsender=m->msg.from;
10141033
raft_peer_t*peer=r->peers+sender;
@@ -1022,14 +1041,7 @@ static void raft_handle_vote(raft_t r, raft_msg_vote_t *m) {
10221041
r->votes++;
10231042
}
10241043

1025-
if (r->votes*2>r->peernum) {
1026-
// got the support of a majority
1027-
r->role=LEADER;
1028-
r->leader=r->me;
1029-
raft_reset_bytes_acked(r);
1030-
raft_reset_silent_time(r,NOBODY);
1031-
raft_reset_timer(r);
1032-
}
1044+
raft_become_leader(r);
10331045
}
10341046

10351047
voidraft_handle_message(raft_tr,raft_msg_tm) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp