英文:
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
-
在代码库中,
ResumeElection
API 只在electionServer) Resign
和electionServer) Proclaim
中被调用。 -
有一些注释
// Resign 允许领导者开始新的选举。
func (e *Election) Resign(ctx context.Context) (err error) {
// Proclaim 允许领导者宣布一个新的值,而无需进行另一次选举。
func (e *Election) Proclaim(ctx context.Context, val string) error {
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
。
英文:
-
In the code base,
ResumeElection
API is invoked only inelectionServer) Resign
andelectionServer) Proclaim
. -
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 {
- 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论