Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Add raft_voter_contact()
Browse files Browse the repository at this point in the history
This returns the number of voting nodes that are recently in contact
with the leader, to allow determining if the cluster is currently in a
degraded / at risk state.
  • Loading branch information
ralight committed Jan 25, 2024
1 parent d67cffd commit 19742e8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion include/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,9 @@ struct raft
raft_index round_index; /* Target of the current round. */
raft_time round_start; /* Start of current round. */
void *requests[2]; /* Outstanding client requests. */
uint64_t reserved[8]; /* Future use */
uint32_t voter_contacts; /* Current number of voting nodes we are in contact with */
uint32_t reserved2; /* Future use */
uint64_t reserved[7]; /* Future use */
} leader_state;
};

Expand Down Expand Up @@ -981,6 +983,18 @@ RAFT_API raft_index raft_last_index(struct raft *r);
*/
RAFT_API raft_index raft_last_applied(struct raft *r);

/**
* Return the number of servers that the leader has recently been in contact
* with.
*
* Valid values will always be >= 1, because a leader is always in contact with
* itself.
* Returns 0 on a leader that has not yet had chance to calculate a valid
* value.
* Returns -1 if called on a follower.
*/
RAFT_API int raft_voter_contacts(struct raft *r);

/**
* Common fields across client request types.
* `req_id`, `client_id` and `unique_id` are currently unused.
Expand Down
9 changes: 9 additions & 0 deletions src/raft.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ const char *raft_errmsg(struct raft *r)
return r->errmsg;
}

int raft_voter_contacts(struct raft *r)

Check warning on line 194 in src/raft.c

View check run for this annotation

Codecov / codecov/patch

src/raft.c#L194

Added line #L194 was not covered by tests
{
if(r->state == RAFT_LEADER){
return (int)r->leader_state.voter_contacts;

Check warning on line 197 in src/raft.c

View check run for this annotation

Codecov / codecov/patch

src/raft.c#L197

Added line #L197 was not covered by tests
}else{
return -1;

Check warning on line 199 in src/raft.c

View check run for this annotation

Codecov / codecov/patch

src/raft.c#L199

Added line #L199 was not covered by tests
}
}

int raft_bootstrap(struct raft *r, const struct raft_configuration *conf)
{
int rv;
Expand Down
1 change: 1 addition & 0 deletions src/tick.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static bool checkContactQuorum(struct raft *r)
contacts++;
}
}
r->leader_state.voter_contacts = contacts;

return contacts > configurationVoterCount(&r->configuration) / 2;
}
Expand Down

0 comments on commit 19742e8

Please sign in to comment.