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] Leadership transfer #4145

Merged
merged 1 commit into from May 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions server/raft.go
Expand Up @@ -1368,15 +1368,6 @@ func (n *raft) StepDown(preferred ...string) error {
if maybeLeader != noLeader {
n.debug("Selected %q for new leader", maybeLeader)
prop.push(newEntry(EntryLeaderTransfer, []byte(maybeLeader)))
time.AfterFunc(250*time.Millisecond, func() {
n.RLock()
stillLeader := n.state == Leader
n.RUnlock()
// If we are still the leader force a stepdown.
if stillLeader {
stepdown.push(noLeader)
}
})
} else {
// Force us to stepdown here.
n.debug("Stepping down")
Expand Down Expand Up @@ -2234,11 +2225,20 @@ func (n *raft) runAsLeader() {
continue
}
n.sendAppendEntry(entries)

// If this is us sending out a leadership transfer stepdown inline here.
if b.Type == EntryLeaderTransfer {
n.prop.recycle(&es)
n.debug("Stepping down due to leadership transfer")
n.switchToFollower(noLeader)
return
}
// We need to re-create `entries` because there is a reference
// to it in the node's pae map.
entries = nil
}
n.prop.recycle(&es)

case <-hb.C:
if n.notActive() {
n.sendHeartbeat()
Expand Down