Browse Source

fix(store): synchronize access to CurrentIndex

Xiang Li 11 years ago
parent
commit
516ebdb49e
1 changed files with 12 additions and 12 deletions
  1. 12 12
      store/store.go

+ 12 - 12
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,
 func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
 	value string, expireTime time.Time) (*Event, error) {
 	value string, expireTime time.Time) (*Event, error) {
 
 
+	s.worldLock.Lock()
+	defer s.worldLock.Unlock()
+
 	nodePath = path.Clean(path.Join("/", nodePath))
 	nodePath = path.Clean(path.Join("/", nodePath))
 	// we do not allow the user to change "/"
 	// we do not allow the user to change "/"
 	if nodePath == "/" {
 	if nodePath == "/" {
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 	}
 	}
 
 
-	s.worldLock.Lock()
-	defer s.worldLock.Unlock()
-
 	n, err := s.internalGet(nodePath)
 	n, err := s.internalGet(nodePath)
 
 
 	if err != nil {
 	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.
 // Delete deletes the node at the given path.
 // If the node is a directory, recursive must be true to delete it.
 // If the node is a directory, recursive must be true to delete it.
 func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
 func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
+	s.worldLock.Lock()
+	defer s.worldLock.Unlock()
+
 	nodePath = path.Clean(path.Join("/", nodePath))
 	nodePath = path.Clean(path.Join("/", nodePath))
 	// we do not allow the user to change "/"
 	// we do not allow the user to change "/"
 	if nodePath == "/" {
 	if nodePath == "/" {
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 	}
 	}
 
 
-	s.worldLock.Lock()
-	defer s.worldLock.Unlock()
-
 	// recursive implies dir
 	// recursive implies dir
 	if recursive == true {
 	if recursive == true {
 		dir = 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) {
 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()
 	s.worldLock.RLock()
 	defer s.worldLock.RUnlock()
 	defer s.worldLock.RUnlock()
 
 
+	key = path.Clean(path.Join("/", key))
+	nextIndex := s.CurrentIndex + 1
+
 	var w *Watcher
 	var w *Watcher
 	var err *etcdErr.Error
 	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 file, the value and the ttl can be updated.
 // If the node is a directory, only 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) {
 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))
 	nodePath = path.Clean(path.Join("/", nodePath))
 	// we do not allow the user to change "/"
 	// we do not allow the user to change "/"
 	if nodePath == "/" {
 	if nodePath == "/" {
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 		return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
 	}
 	}
 
 
-	s.worldLock.Lock()
-	defer s.worldLock.Unlock()
-
 	currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
 	currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
 
 
 	n, err := s.internalGet(nodePath)
 	n, err := s.internalGet(nodePath)