Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved election debug logs #516

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ func (r *Raft) liveBootstrap(configuration Configuration) error {

// runCandidate runs the main loop while in the candidate state.
func (r *Raft) runCandidate() {
r.logger.Info("entering candidate state", "node", r, "term", r.getCurrentTerm()+1)
term := r.getCurrentTerm() + 1
r.logger.Info("entering candidate state", "node", r, "term", term)
metrics.IncrCounter([]string{"raft", "state", "candidate"}, 1)

// Start vote for us, and set a timeout
Expand All @@ -298,7 +299,7 @@ func (r *Raft) runCandidate() {
// Tally the votes, need a simple majority
grantedVotes := 0
votesNeeded := r.quorumSize()
r.logger.Debug("votes", "needed", votesNeeded)
r.logger.Debug("calculated votes needed", "needed", votesNeeded, "term", term)

for r.getState() == Candidate {
r.mainThreadSaturation.sleeping()
Expand All @@ -312,7 +313,7 @@ func (r *Raft) runCandidate() {
r.mainThreadSaturation.working()
// Check if the term is greater than ours, bail
if vote.Term > r.getCurrentTerm() {
r.logger.Debug("newer term discovered, fallback to follower")
r.logger.Debug("newer term discovered, fallback to follower", "term", vote.Term)
r.setState(Follower)
r.setCurrentTerm(vote.Term)
return
Expand All @@ -326,7 +327,7 @@ func (r *Raft) runCandidate() {

// Check if we've become the leader
if grantedVotes >= votesNeeded {
r.logger.Info("election won", "tally", grantedVotes)
r.logger.Info("election won", "term", vote.Term, "tally", grantedVotes)
r.setState(Leader)
r.setLeader(r.localAddr, r.localID)
return
Expand Down Expand Up @@ -1829,7 +1830,8 @@ func (r *Raft) electSelf() <-chan *voteResult {
if err != nil {
r.logger.Error("failed to make requestVote RPC",
"target", peer,
"error", err)
"error", err,
"term", req.Term)
resp.Term = req.Term
resp.Granted = false
}
Expand All @@ -1841,6 +1843,7 @@ func (r *Raft) electSelf() <-chan *voteResult {
for _, server := range r.configurations.latest.Servers {
if server.Suffrage == Voter {
if server.ID == r.localID {
r.logger.Debug("voting for self", "term", req.Term, "id", r.localID)
// Persist a vote for ourselves
if err := r.persistVote(req.Term, req.RPCHeader.Addr); err != nil {
r.logger.Error("failed to persist vote", "error", err)
Expand All @@ -1856,6 +1859,7 @@ func (r *Raft) electSelf() <-chan *voteResult {
voterID: r.localID,
}
} else {
r.logger.Debug("asking for vote", "term", req.Term, "from", server.ID, "address", server.Address)
askPeer(server)
}
}
Expand Down