mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
// Copyright 2016 CoreOS, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
package recipe
|
|
|
|
import (
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
v3 "github.com/coreos/etcd/clientv3"
|
|
"github.com/coreos/etcd/etcdserver"
|
|
"github.com/coreos/etcd/storage/storagepb"
|
|
)
|
|
|
|
type Election struct {
|
|
client *v3.Client
|
|
ctx context.Context
|
|
|
|
keyPrefix string
|
|
leaderKey *EphemeralKV
|
|
}
|
|
|
|
// NewElection returns a new election on a given key prefix.
|
|
func NewElection(client *v3.Client, keyPrefix string) *Election {
|
|
return &Election{client, context.TODO(), keyPrefix, nil}
|
|
}
|
|
|
|
// Volunteer puts a value as eligible for the election. It blocks until
|
|
// it is elected or an error occurs (cannot withdraw candidacy)
|
|
func (e *Election) Volunteer(val string) error {
|
|
if e.leaderKey != nil {
|
|
return e.leaderKey.Put(val)
|
|
}
|
|
myKey, err := NewUniqueEphemeralKV(e.client, e.keyPrefix, val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = e.waitLeadership(myKey); err != nil {
|
|
return err
|
|
}
|
|
e.leaderKey = myKey
|
|
return nil
|
|
}
|
|
|
|
// Resign lets a leader start a new election.
|
|
func (e *Election) Resign() (err error) {
|
|
if e.leaderKey != nil {
|
|
err = e.leaderKey.Delete()
|
|
e.leaderKey = nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Leader returns the leader value for the current election.
|
|
func (e *Election) Leader() (string, error) {
|
|
resp, err := e.client.Get(e.ctx, e.keyPrefix, v3.WithFirstCreate()...)
|
|
if err != nil {
|
|
return "", err
|
|
} else if len(resp.Kvs) == 0 {
|
|
// no leader currently elected
|
|
return "", etcdserver.ErrNoLeader
|
|
}
|
|
return string(resp.Kvs[0].Value), nil
|
|
}
|
|
|
|
// Wait waits for a leader to be elected, returning the leader value.
|
|
func (e *Election) Wait() (string, error) {
|
|
resp, err := e.client.Get(e.ctx, e.keyPrefix, v3.WithFirstCreate()...)
|
|
if err != nil {
|
|
return "", err
|
|
} else if len(resp.Kvs) != 0 {
|
|
// leader already exists
|
|
return string(resp.Kvs[0].Value), nil
|
|
}
|
|
_, err = WaitPrefixEvents(
|
|
e.client,
|
|
e.keyPrefix,
|
|
resp.Header.Revision,
|
|
[]storagepb.Event_EventType{storagepb.PUT})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return e.Wait()
|
|
}
|
|
|
|
func (e *Election) waitLeadership(tryKey *EphemeralKV) error {
|
|
opts := append(v3.WithLastCreate(), v3.WithRev(tryKey.Revision()-1))
|
|
resp, err := e.client.Get(e.ctx, e.keyPrefix, opts...)
|
|
if err != nil {
|
|
return err
|
|
} else if len(resp.Kvs) == 0 {
|
|
// nothing before tryKey => have leadership
|
|
return nil
|
|
}
|
|
_, err = WaitEvents(
|
|
e.client,
|
|
string(resp.Kvs[0].Key),
|
|
tryKey.Revision(),
|
|
[]storagepb.Event_EventType{storagepb.DELETE})
|
|
return err
|
|
}
|