Browse Source

store: fix index data race

Xiang Li 11 năm trước cách đây
mục cha
commit
8ea840c19a
2 tập tin đã thay đổi với 10 bổ sung8 xóa
  1. 8 8
      store/node.go
  2. 2 0
      store/store.go

+ 8 - 8
store/node.go

@@ -101,7 +101,7 @@ func (n *node) IsDir() bool {
 // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
 func (n *node) Read() (string, *etcdErr.Error) {
 	if n.IsDir() {
-		return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
+		return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.CurrentIndex)
 	}
 
 	return n.Value, nil
@@ -111,7 +111,7 @@ func (n *node) Read() (string, *etcdErr.Error) {
 // If the receiver node is a directory, a "Not A File" error will be returned.
 func (n *node) Write(value string, index uint64) *etcdErr.Error {
 	if n.IsDir() {
-		return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.Index())
+		return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.CurrentIndex)
 	}
 
 	n.Value = value
@@ -143,7 +143,7 @@ func (n *node) ExpirationAndTTL() (*time.Time, int64) {
 // If the receiver node is not a directory, a "Not A Directory" error will be returned.
 func (n *node) List() ([]*node, *etcdErr.Error) {
 	if !n.IsDir() {
-		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
+		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.CurrentIndex)
 	}
 
 	nodes := make([]*node, len(n.Children))
@@ -161,7 +161,7 @@ func (n *node) List() ([]*node, *etcdErr.Error) {
 // On success, it returns the file node
 func (n *node) GetChild(name string) (*node, *etcdErr.Error) {
 	if !n.IsDir() {
-		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, n.store.Index())
+		return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, n.store.CurrentIndex)
 	}
 
 	child, ok := n.Children[name]
@@ -179,7 +179,7 @@ func (n *node) GetChild(name string) (*node, *etcdErr.Error) {
 // error will be returned
 func (n *node) Add(child *node) *etcdErr.Error {
 	if !n.IsDir() {
-		return etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.Index())
+		return etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.CurrentIndex)
 	}
 
 	_, name := path.Split(child.Path)
@@ -187,7 +187,7 @@ func (n *node) Add(child *node) *etcdErr.Error {
 	_, ok := n.Children[name]
 
 	if ok {
-		return etcdErr.NewError(etcdErr.EcodeNodeExist, "", n.store.Index())
+		return etcdErr.NewError(etcdErr.EcodeNodeExist, "", n.store.CurrentIndex)
 	}
 
 	n.Children[name] = child
@@ -201,13 +201,13 @@ func (n *node) Remove(dir, recursive bool, callback func(path string)) *etcdErr.
 	if n.IsDir() {
 		if !dir {
 			// cannot delete a directory without recursive set to true
-			return etcdErr.NewError(etcdErr.EcodeNotFile, n.Path, n.store.Index())
+			return etcdErr.NewError(etcdErr.EcodeNotFile, n.Path, n.store.CurrentIndex)
 		}
 
 		if len(n.Children) != 0 && !recursive {
 			// cannot delete a directory if it is not empty and the operation
 			// is not recursive
-			return etcdErr.NewError(etcdErr.EcodeDirNotEmpty, n.Path, n.store.Index())
+			return etcdErr.NewError(etcdErr.EcodeDirNotEmpty, n.Path, n.store.CurrentIndex)
 		}
 	}
 

+ 2 - 0
store/store.go

@@ -94,6 +94,8 @@ func (s *store) Version() int {
 
 // Retrieves current of the store
 func (s *store) Index() uint64 {
+	s.worldLock.RLock()
+	defer s.worldLock.RUnlock()
 	return s.CurrentIndex
 }