From 4d2d2cabb9b0dcaa16ffd17e6d53a745740f3468 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Thu, 7 Apr 2016 13:36:46 -0700 Subject: [PATCH] etcdserver: fix race on consistent index --- etcdserver/consistent_index.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/etcdserver/consistent_index.go b/etcdserver/consistent_index.go index e63126692..a387b7573 100644 --- a/etcdserver/consistent_index.go +++ b/etcdserver/consistent_index.go @@ -14,12 +14,20 @@ package etcdserver +import ( + "sync/atomic" +) + // consistentIndex represents the offset of an entry in a consistent replica log. // It implements the storage.ConsistentIndexGetter interface. // It is always set to the offset of current entry before executing the entry, // so ConsistentWatchableKV could get the consistent index from it. 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)) +}