goetcdleader-election

Understanding etcd Leader election APIs


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.


Solution

    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.