Browse Source

etcdserver: fix race on consistent index

Anthony Romano 9 years ago
parent
commit
4d2d2cabb9
1 changed files with 10 additions and 2 deletions
  1. 10 2
      etcdserver/consistent_index.go

+ 10 - 2
etcdserver/consistent_index.go

@@ -14,12 +14,20 @@
 
 
 package etcdserver
 package etcdserver
 
 
+import (
+	"sync/atomic"
+)
+
 // consistentIndex represents the offset of an entry in a consistent replica log.
 // consistentIndex represents the offset of an entry in a consistent replica log.
 // It implements the storage.ConsistentIndexGetter interface.
 // It implements the storage.ConsistentIndexGetter interface.
 // It is always set to the offset of current entry before executing the entry,
 // It is always set to the offset of current entry before executing the entry,
 // so ConsistentWatchableKV could get the consistent index from it.
 // so ConsistentWatchableKV could get the consistent index from it.
 type consistentIndex uint64
 type consistentIndex uint64
 
 
-func (i *consistentIndex) setConsistentIndex(v uint64) { *i = consistentIndex(v) }
+func (i *consistentIndex) setConsistentIndex(v uint64) {
+	atomic.StoreUint64((*uint64)(i), v)
+}
 
 
-func (i *consistentIndex) ConsistentIndex() uint64 { return uint64(*i) }
+func (i *consistentIndex) ConsistentIndex() uint64 {
+	return atomic.LoadUint64((*uint64)(i))
+}