1
0
Fork 0

Add leadership election

Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
Emile Vauge 2016-08-18 13:03:10 +02:00
parent 5a0440d6f8
commit bea5ad3f13
No known key found for this signature in database
GPG key ID: D808B4C167352E59
7 changed files with 138 additions and 29 deletions

View file

@ -94,8 +94,8 @@ func (d *Datastore) watchChanges() error {
// Begin creates a transaction with the KV store.
func (d *Datastore) Begin() (*Transaction, error) {
value := uuid.NewV4().String()
remoteLock, err := d.kv.NewLock(d.lockKey, &store.LockOptions{TTL: 20 * time.Second, Value: []byte(value)})
id := uuid.NewV4().String()
remoteLock, err := d.kv.NewLock(d.lockKey, &store.LockOptions{TTL: 20 * time.Second, Value: []byte(id)})
if err != nil {
return nil, err
}
@ -119,8 +119,8 @@ func (d *Datastore) Begin() (*Transaction, error) {
// we got the lock! Now make sure we are synced with KV store
operation := func() error {
meta := d.get()
if meta.Lock != value {
return fmt.Errorf("Object lock value: expected %s, got %s", value, meta.Lock)
if meta.Lock != id {
return fmt.Errorf("Object lock value: expected %s, got %s", id, meta.Lock)
}
return nil
}