理解 etcd 的 Leader 选举 API

huangapple go评论79阅读模式
英文:

Understanding etcd Leader election APIs

问题

我正在尝试理解etcd选举API提供的各种功能以及它们在语义上的含义。

在官方文档中,对每个函数的作用只是简单地提到了一下,并没有提供示例。
例如,我们有以下方法:

func (e *Election) Campaign(ctx context.Context, val string) error

据我理解,Campaign用于将节点注册为选举中的领导者,并且在达到这一目标或发生错误之前会一直阻塞。

func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election

对于这个函数,我不确定为什么我们需要恢复选举,假设节点已经是领导者了。也许是因为我对这里使用的语义不够理解。

既然我们必须使用一个共同的选举标识符,为什么不只使用一个选举就足够了呢?

最后,我的使用场景是运行多个节点,并且只允许领导者执行一些计算/更新操作,在什么情况下我需要创建新的选举,如果只有一个选举不够的话。

英文:

I am trying to understand various functions provided by etcd election api and what they mean semantically.

In their official documentation very briefly mentioned about what each function does, and no examples are provided.
For example we have methods:

func (e *Election) Campaign(ctx context.Context, val string) error

As per my understanding campaign is used to enroll node for leadership in an election, and it is blocked until that is achieved or some error occurs.

func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election

This one I am not sure of why we need to resume election, suppose node is already a leader.
Maybe my lack of understanding is due to semantics used here.

Since we have to use a common election identifier why don't just single election suffice?

Finally my use case is to run several nodes and only allow leader to make carry out some computation/updates, under what circumstances I'd have to create new elections if single election won't be enough.

答案1

得分: 2

  1. 在代码库中,ResumeElection API 只在 electionServer) ResignelectionServer) Proclaim 中被调用。

  2. 有一些注释

// Resign 允许领导者开始新的选举。
func (e *Election) Resign(ctx context.Context) (err error) {

// Proclaim 允许领导者宣布一个新的值,而无需进行另一次选举。
func (e *Election) Proclaim(ctx context.Context, val string) error {
  1. ResumeElection 的实现只是返回一个结构体。
// ResumeElection 使用已知的领导者初始化选举。
func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election {
	return &Election{
		keyPrefix:     pfx,
		session:       s,
		leaderKey:     leaderKey,
		leaderRev:     leaderRev,
		leaderSession: s,
	}
}

所以,回答你的问题是:ResumeElection 的对立面是 NewElection,而不是 Campaign

此外,当客户端已经有一个领导者,并且想要改变集群的状态(设置一个变量或放弃领导权),它会调用 ResumeElection 而不是 Campaign

英文:
  1. In the code base, ResumeElection API is invoked only in electionServer) Resign and electionServer) Proclaim.

  2. There is some comment

// Resign lets a leader start a new election.
func (e *Election) Resign(ctx context.Context) (err error) {

// Proclaim lets the leader announce a new value without another election.
func (e *Election) Proclaim(ctx context.Context, val string) error {
  1. Implement of ResumeElection is just return a struct.
// ResumeElection initializes an election with a known leader.
func ResumeElection(s *Session, pfx string, leaderKey string, leaderRev int64) *Election {
	return &Election{
		keyPrefix:     pfx,
		session:       s,
		leaderKey:     leaderKey,
		leaderRev:     leaderRev,
		leaderSession: s,
	}
}

So, answer to your question is : the opposite side of ResumeElection is NewElection, instead of Campaign.

The more, when client already have a leader, and want change state of cluster(set a variable or give up leadership). it will invoke ResumeElection instead of Campaign.

huangapple
  • 本文由 发表于 2022年1月29日 03:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/70899475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定