3

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.

  • Hey, did you find what `ResumeElection` is meant for? Then answer is a bit hard to understand. Thanks – Matteo Nov 08 '22 at 09:31

1 Answers1

2
  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.

Frank Wang
  • 788
  • 7
  • 23
  • Thanks for the answer, I have few doubts: Suppose I don't want to resign, have leader affinity of some sort, the only way to loose leadership is through network latency (leads to session expiration). Also suppose this whole election runs in inf for loop then how often leader should fire resume election function.? Currently I am triggering new election only in case session expired or some error occurs then I am starting fresh in both leader as well as in follower. And also what exactly proclaim anounces, I mean in the code which is referred as value. – greendragons Jan 29 '22 at 07:47
  • @greendragons. So your new question is "how often leader should fire resume election function?" It depends on raft heartbeat, I thought you don't need to care about it. – Frank Wang Jan 29 '22 at 07:56
  • So I can simply put resume election in that for loop (when leadership is acquired) without bothering about the frequency (currently there are no delays/sleep or after time there). – greendragons Jan 29 '22 at 07:58
  • @greendragons . Anther question "what exactly proclaim anounces". As we know , etcd serve as a kv-store. The value is v of kv. – Frank Wang Jan 29 '22 at 07:59
  • Ok looks like some value which leader announces and other listen. I think this is not my use case. Previously I assumed this value has something to do with election itself. – greendragons Jan 29 '22 at 08:00
  • @greendragons. Yes, "So I can simply" you can. BTW, can you set my answer as accept? – Frank Wang Jan 29 '22 at 08:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241511/discussion-between-greendragons-and-frank-wang). – greendragons Jan 29 '22 at 08:02