diff --git a/store/store.go b/store/store.go index 0451cc6e7..08d585056 100644 --- a/store/store.go +++ b/store/store.go @@ -215,30 +215,30 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint // If both of the prevValue and prevIndex are given, we will test both of them. // Command will be executed, only if both of the tests are successful. - if n.Compare(prevValue, prevIndex) { - // update etcd index - s.CurrentIndex++ - - e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex) - eNode := e.Node - - eNode.PrevValue = n.Value - - // if test succeed, write the value - n.Write(value, s.CurrentIndex) - n.UpdateTTL(expireTime) - - eNode.Value = value - eNode.Expiration, eNode.TTL = n.ExpirationAndTTL() - - s.WatcherHub.notify(e) - s.Stats.Inc(CompareAndSwapSuccess) - return e, nil + if !n.Compare(prevValue, prevIndex) { + cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) + s.Stats.Inc(CompareAndSwapFail) + return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex) } - cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) - s.Stats.Inc(CompareAndSwapFail) - return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex) + // update etcd index + s.CurrentIndex++ + + e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex) + eNode := e.Node + + eNode.PrevValue = n.Value + + // if test succeed, write the value + n.Write(value, s.CurrentIndex) + n.UpdateTTL(expireTime) + + eNode.Value = value + eNode.Expiration, eNode.TTL = n.ExpirationAndTTL() + + s.WatcherHub.notify(e) + s.Stats.Inc(CompareAndSwapSuccess) + return e, nil } // Delete function deletes the node at the given path. @@ -316,28 +316,28 @@ func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex ui // If both of the prevValue and prevIndex are given, we will test both of them. // Command will be executed, only if both of the tests are successful. - if n.Compare(prevValue, prevIndex) { - // update etcd index - s.CurrentIndex++ - - e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex) - - callback := func(path string) { // notify function - // notify the watchers with deleted set true - s.WatcherHub.notifyWatchers(e, path, true) - } - - // delete a key-value pair, no error should happen - n.Remove(false, false, callback) - - s.WatcherHub.notify(e) - s.Stats.Inc(CompareAndDeleteSuccess) - return e, nil + if !n.Compare(prevValue, prevIndex) { + cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) + s.Stats.Inc(CompareAndDeleteFail) + return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex) } - cause := fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex) - s.Stats.Inc(CompareAndDeleteFail) - return nil, etcdErr.NewError(etcdErr.EcodeTestFailed, cause, s.CurrentIndex) + // update etcd index + s.CurrentIndex++ + + e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex) + + callback := func(path string) { // notify function + // notify the watchers with deleted set true + s.WatcherHub.notifyWatchers(e, path, true) + } + + // delete a key-value pair, no error should happen + n.Remove(false, false, callback) + + s.WatcherHub.notify(e) + s.Stats.Inc(CompareAndDeleteSuccess) + return e, nil } func (s *store) Watch(key string, recursive bool, sinceIndex uint64) (<-chan *Event, error) {