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

try to perform a leadership transfer when leaving #11376

Merged
merged 2 commits into from
Oct 21, 2021

Conversation

dhiaayachi
Copy link
Contributor

This is a port of #11242 (1.8.x) for main

@vercel vercel bot temporarily deployed to Preview – consul-ui-staging October 21, 2021 02:23 Inactive
@vercel vercel bot temporarily deployed to Preview – consul October 21, 2021 02:23 Inactive
@dhiaayachi dhiaayachi merged commit f424faf into main Oct 21, 2021
@dhiaayachi dhiaayachi deleted the leadership-transfer-onleave branch October 21, 2021 16:44
@@ -0,0 +1,3 @@
```release-note:enhancement
try to transfer leadership to another server when leaving.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about something like this?

Suggested change
try to transfer leadership to another server when leaving.
raft: Consul leaders will attempt to transfer leadership to another server as part of gracefully leaving the cluster.

s.logger.Error("failed to remove ourself as a Raft peer", "error", err)
leadershipTransferVersion := version.Must(version.NewVersion(LeaderTransferMinVersion))
removeServer := false
if ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion); !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be if ok ?

Suggested change
if ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion); !ok {
if ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion); ok {

Comment on lines +999 to +1020
leadershipTransferVersion := version.Must(version.NewVersion(LeaderTransferMinVersion))
removeServer := false
if ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion); !ok {
// Transfer leadership to another node then leave the cluster
future := s.raft.LeadershipTransfer()
if err := future.Error(); err != nil {
s.logger.Error("failed to transfer leadership, removing the server", "error", err)
// leadership transfer failed, fallback to removing the server from raft
removeServer = true
} else {
// we are not leader anymore, continue the flow to leave as follower
isLeader = false
}
} else {
// Leadership transfer is not available in the current version, fallback to removing the server from raft
removeServer = true
}
if removeServer {
future := s.raft.RemoveServer(raft.ServerID(s.config.NodeID), 0, 0)
if err := future.Error(); err != nil {
s.logger.Error("failed to remove ourself as raft peer", "error", err)
}
Copy link
Contributor

@dnephin dnephin Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two high level questions/suggestions:

  1. Do these comments really help readability? Generally I try to only write docstring comments unless there is something very unusual, and everything here looks pretty expected. Personally I think it reads better without the comments.
  2. Can we extract any of this to a function to remove the nested else statements, and stateful variables?
func (s *Server) leadershipTransfer() (success bool) {
	leadershipTransferVersion := version.Must(version.NewVersion(LeaderTransferMinVersion))

	ok, _ := ServersInDCMeetMinimumVersion(s, s.config.Datacenter, leadershipTransferVersion)
	if !ok {
		return false
	}
	
	future := s.raft.LeadershipTransfer()
	if err := future.Error(); err != nil {
		s.logger.Error("failed to transfer leadership, removing the server", "error", err)
		return false
	}
	return true
}

Then in the caller:

if !s.leadershipTransfer() {

	future := s.raft.RemoveServer(raft.ServerID(s.config.NodeID), 0, 0)
	if err := future.Error(); err != nil {
		s.logger.Error("failed to remove ourself as raft peer", "error", err)
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason of the comments is because I did not find a good way of removing the nested if else, I will attempt the extract in the new PR.

@hc-github-team-consul-core
Copy link
Contributor

🍒 If backport labels were added before merging, cherry-picking will start automatically.

To retroactively trigger a backport after merging, add backport labels and re-run https://circleci.com/gh/hashicorp/consul/480806.

@hc-github-team-consul-core
Copy link
Contributor

🍒✅ Cherry pick of commit f424faf onto release/1.9.x succeeded!

hc-github-team-consul-core pushed a commit that referenced this pull request Oct 21, 2021
* try to perform a leadership transfer when leaving

* add a changelog
dhiaayachi added a commit that referenced this pull request Oct 21, 2021
@hc-github-team-consul-core
Copy link
Contributor

🍒 If backport labels were added before merging, cherry-picking will start automatically.

To retroactively trigger a backport after merging, add backport labels and re-run https://circleci.com/gh/hashicorp/consul/481009.

@hc-github-team-consul-core
Copy link
Contributor

🍒✅ Cherry pick of commit f424faf onto release/1.10.x succeeded!

hc-github-team-consul-core pushed a commit that referenced this pull request Oct 21, 2021
* try to perform a leadership transfer when leaving

* add a changelog
@hc-github-team-consul-core
Copy link
Contributor

🍒❌ Cherry pick of commit f424faf onto release/1.9.x failed! Build Log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants