From 516ebdb49e51e8c6c1f63b4ebded03823fed2b61 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 20 May 2014 13:53:47 -0700 Subject: [PATCH] fix(store): synchronize access to CurrentIndex --- store/store.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/store/store.go b/store/store.go index 5a5c3ba2f..359c35954 100644 --- a/store/store.go +++ b/store/store.go @@ -199,15 +199,15 @@ func getCompareFailCause(n *node, which int, prevValue string, prevIndex uint64) func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64, value string, expireTime time.Time) (*Event, error) { + s.worldLock.Lock() + defer s.worldLock.Unlock() + nodePath = path.Clean(path.Join("/", nodePath)) // we do not allow the user to change "/" if nodePath == "/" { return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex) } - s.worldLock.Lock() - defer s.worldLock.Unlock() - n, err := s.internalGet(nodePath) if err != nil { @@ -252,15 +252,15 @@ func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint // Delete deletes the node at the given path. // If the node is a directory, recursive must be true to delete it. func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) { + s.worldLock.Lock() + defer s.worldLock.Unlock() + nodePath = path.Clean(path.Join("/", nodePath)) // we do not allow the user to change "/" if nodePath == "/" { return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex) } - s.worldLock.Lock() - defer s.worldLock.Unlock() - // recursive implies dir if recursive == true { dir = true @@ -350,12 +350,12 @@ func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex ui } func (s *store) Watch(key string, recursive, stream bool, sinceIndex uint64) (*Watcher, error) { - key = path.Clean(path.Join("/", key)) - nextIndex := s.CurrentIndex + 1 - s.worldLock.RLock() defer s.worldLock.RUnlock() + key = path.Clean(path.Join("/", key)) + nextIndex := s.CurrentIndex + 1 + var w *Watcher var err *etcdErr.Error @@ -402,15 +402,15 @@ func (s *store) walk(nodePath string, walkFunc func(prev *node, component string // If the node is a file, the value and the ttl can be updated. // If the node is a directory, only the ttl can be updated. func (s *store) Update(nodePath string, newValue string, expireTime time.Time) (*Event, error) { + s.worldLock.Lock() + defer s.worldLock.Unlock() + nodePath = path.Clean(path.Join("/", nodePath)) // we do not allow the user to change "/" if nodePath == "/" { return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex) } - s.worldLock.Lock() - defer s.worldLock.Unlock() - currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1 n, err := s.internalGet(nodePath)