feat(raft-library): task 3.5 — election timeout, leader-lost, check-and-start; 6 tests
This commit is contained in:
parent
f81054930f
commit
85a7e325a8
3 changed files with 262 additions and 6 deletions
|
|
@ -224,16 +224,41 @@ int raft_on_vote_reply(raft_state_t *s, int from_idx, int granted, uint64_t term
|
|||
|
||||
void raft_on_election_timeout(raft_state_t *s)
|
||||
{
|
||||
(void)s;
|
||||
if (s->election_decided) {
|
||||
s->cbs->on_cancel_timeout(s->ctx);
|
||||
if (s->cbs->on_trace)
|
||||
s->cbs->on_trace(s->ctx, "election_timeout_noop", NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
s->role = RAFT_FOLLOWER;
|
||||
s->election_decided = 1;
|
||||
s->cbs->on_become_follower(s->ctx, s->term, -1);
|
||||
s->cbs->on_cancel_timeout(s->ctx);
|
||||
if (s->cbs->on_trace)
|
||||
s->cbs->on_trace(s->ctx, "election_timeout", NULL);
|
||||
}
|
||||
|
||||
void raft_on_leader_lost(raft_state_t *s)
|
||||
{
|
||||
(void)s;
|
||||
s->voted_for = -1;
|
||||
s->role = RAFT_FOLLOWER;
|
||||
s->cbs->on_start_election_after_delay(s->ctx,
|
||||
(uint32_t)s->my_priority * s->election_timeout_ms);
|
||||
if (s->cbs->on_trace)
|
||||
s->cbs->on_trace(s->ctx, "leader_lost", NULL);
|
||||
}
|
||||
|
||||
int raft_check_and_start_election(raft_state_t *s)
|
||||
{
|
||||
(void)s;
|
||||
return 0;
|
||||
if (s->voted_for != -1 && s->voted_for != s->self_idx) {
|
||||
if (s->cbs->on_trace)
|
||||
s->cbs->on_trace(s->ctx, "jitter_skip", NULL);
|
||||
return RAFT_SKIP_ELECTION;
|
||||
}
|
||||
|
||||
/* Precondition: raft_on_leader_lost sets role=FOLLOWER, so -EALREADY is unreachable here */
|
||||
int rc = raft_start_election(s);
|
||||
(void)rc;
|
||||
return RAFT_ELECTION_STARTED;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue