Skip to content

Commit

Permalink
Merge pull request #804 from xiangli-cmu/fix_curr_index_race
Browse files Browse the repository at this point in the history
fix(store): synchronize access to CurrentIndex
  • Loading branch information
xiang90 committed May 20, 2014
2 parents 1e7a7b1 + 516ebdb commit 16d89c9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16d89c9

Please sign in to comment.