Skip to content

Commit

Permalink
fix etcd lock, change ttl Update to CompareAndSwap
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Beslic <abronan@docker.com>
  • Loading branch information
abronan committed Jul 27, 2015
1 parent b68985b commit 261ee16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions store/etcd/etcd.go
Expand Up @@ -432,7 +432,7 @@ func (l *etcdLock) Lock() (<-chan struct{}, error) {
lastIndex = resp.Node.ModifiedIndex
}

_, err = l.client.CompareAndSwap(key, l.value, l.ttl, "", lastIndex)
l.last, err = l.client.CompareAndSwap(key, l.value, l.ttl, "", lastIndex)

if err == nil {
// Leader section
Expand Down Expand Up @@ -467,7 +467,7 @@ func (l *etcdLock) holdLock(key string, lockHeld chan struct{}, stopLocking chan
for {
select {
case <-update.C:
l.last, err = l.client.Update(key, l.value, l.ttl)
l.last, err = l.client.CompareAndSwap(key, l.value, l.ttl, "", l.last.Node.ModifiedIndex)
if err != nil {
return
}
Expand Down
5 changes: 5 additions & 0 deletions testutils/utils.go
Expand Up @@ -288,6 +288,11 @@ func testLockUnlock(t *testing.T, kv store.Store) {
err = lock.Unlock()
assert.NoError(t, err)

// Lock should succeed again
lockChan, err = lock.Lock()
assert.NoError(t, err)
assert.NotNil(t, lockChan)

// Get should work
pair, err = kv.Get(key)
assert.NoError(t, err)
Expand Down

0 comments on commit 261ee16

Please sign in to comment.