clientv3/ordering: compare and update prevRev atomically
Several goroutines may call setPrevRev concurrently with different
revisions, all higher than prevRev. Previously all of these goroutines
could set prevRev, so prevRev may be replaced by older one.
If response's revision equals to prevRev, there's no need to call
setPrevRev.
@@ -42,11 +42,10 @@ func (kv *kvOrdering) getPrevRev() int64 {
}
func (kv *kvOrdering) setPrevRev(currRev int64) {
- prevRev := kv.getPrevRev()
- if currRev > prevRev {
kv.revMu.Lock()
+ defer kv.revMu.Unlock()
+ if currRev > kv.prevRev {
kv.prevRev = currRev
- kv.revMu.Unlock()
}
}
@@ -63,7 +62,9 @@ func (kv *kvOrdering) Get(ctx context.Context, key string, opts ...clientv3.OpOp
return nil, err
}
resp := r.Get()
- if resp.Header.Revision >= prevRev {
+ if resp.Header.Revision == prevRev {
+ return resp, nil
+ } else if resp.Header.Revision > prevRev {
kv.setPrevRev(resp.Header.Revision)
return resp, nil
}
