1
0
mirror of https://github.com/docker/docs.git synced 2026-10-31 10:34:26 +03:00

Add ErrKeyModified error to etcd and zookeeper

Signed-off-by: Alexandre Beslic <abronan@docker.com>
This commit is contained in:
Alexandre Beslic
2015-05-27 08:02:54 -07:00
parent 97e54273e3
commit 6a92628ddb
3 changed files with 22 additions and 6 deletions

View File

@@ -343,7 +343,7 @@ func (l *consulLock) Unlock() error {
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Consul) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error) {
if previous != nil {
if previous == nil {
return false, nil, ErrPreviousNotSpecified
}
@@ -372,7 +372,7 @@ func (s *Consul) AtomicPut(key string, value []byte, previous *KVPair, options *
// AtomicDelete deletes a value at "key" if the key has not
// been modified in the meantime, throws an error if this is the case
func (s *Consul) AtomicDelete(key string, previous *KVPair) (bool, error) {
if previous != nil {
if previous == nil {
return false, ErrPreviousNotSpecified
}

View File

@@ -253,12 +253,17 @@ func (s *Etcd) WatchTree(prefix string, stopCh <-chan struct{}) (<-chan []*KVPai
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Etcd) AtomicPut(key string, value []byte, previous *KVPair, options *WriteOptions) (bool, *KVPair, error) {
if previous != nil {
if previous == nil {
return false, nil, ErrPreviousNotSpecified
}
meta, err := s.client.CompareAndSwap(normalize(key), string(value), 0, "", previous.LastIndex)
if err != nil {
if etcdError, ok := err.(*etcd.EtcdError); ok {
if etcdError.ErrorCode == 101 { // Compare failed
return false, nil, ErrKeyModified
}
}
return false, nil, err
}
return true, &KVPair{Key: key, Value: value, LastIndex: meta.Node.ModifiedIndex}, nil
@@ -267,12 +272,17 @@ func (s *Etcd) AtomicPut(key string, value []byte, previous *KVPair, options *Wr
// AtomicDelete deletes a value at "key" if the key has not
// been modified in the meantime, throws an error if this is the case
func (s *Etcd) AtomicDelete(key string, previous *KVPair) (bool, error) {
if previous != nil {
if previous == nil {
return false, ErrPreviousNotSpecified
}
_, err := s.client.CompareAndDelete(normalize(key), "", previous.LastIndex)
if err != nil {
if etcdError, ok := err.(*etcd.EtcdError); ok {
if etcdError.ErrorCode == 101 { // Compare failed
return false, ErrKeyModified
}
}
return false, err
}
return true, nil

View File

@@ -215,12 +215,15 @@ func (s *Zookeeper) DeleteTree(prefix string) error {
// AtomicPut put a value at "key" if the key has not been
// modified in the meantime, throws an error if this is the case
func (s *Zookeeper) AtomicPut(key string, value []byte, previous *KVPair, _ *WriteOptions) (bool, *KVPair, error) {
if previous != nil {
if previous == nil {
return false, nil, ErrPreviousNotSpecified
}
meta, err := s.client.Set(normalize(key), value, int32(previous.LastIndex))
if err != nil {
if err == zk.ErrBadVersion {
return false, nil, ErrKeyModified
}
return false, nil, err
}
return true, &KVPair{Key: key, Value: value, LastIndex: uint64(meta.Version)}, nil
@@ -229,12 +232,15 @@ func (s *Zookeeper) AtomicPut(key string, value []byte, previous *KVPair, _ *Wri
// AtomicDelete deletes a value at "key" if the key has not
// been modified in the meantime, throws an error if this is the case
func (s *Zookeeper) AtomicDelete(key string, previous *KVPair) (bool, error) {
if previous != nil {
if previous == nil {
return false, ErrPreviousNotSpecified
}
err := s.client.Delete(normalize(key), int32(previous.LastIndex))
if err != nil {
if err == zk.ErrBadVersion {
return false, ErrKeyModified
}
return false, err
}
return true, nil