From 4b4f5be74adafbef0392cf56e1c8128d41748505 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Thu, 30 Mar 2017 18:20:22 -0700 Subject: [PATCH] concurrency: don't skip leader updates in Observe() The Get for the leader key will fetch based on the latest revision instead of the deletion revision, missing leader updates between the delete and the Get. Although it's usually safe to skip these updates since they're stale, it makes testing more difficult and in some cases the full leader update history is desirable. --- clientv3/concurrency/election.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/clientv3/concurrency/election.go b/clientv3/concurrency/election.go index 47f68f1b4..90c7c667c 100644 --- a/clientv3/concurrency/election.go +++ b/clientv3/concurrency/election.go @@ -161,20 +161,21 @@ func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) { client := e.session.Client() defer close(ch) + lastRev := int64(0) for { - resp, err := client.Get(ctx, e.keyPrefix, v3.WithFirstCreate()...) + opts := append(v3.WithFirstCreate(), v3.WithRev(lastRev)) + resp, err := client.Get(ctx, e.keyPrefix, opts...) if err != nil { return } var kv *mvccpb.KeyValue - cctx, cancel := context.WithCancel(ctx) if len(resp.Kvs) == 0 { + cctx, cancel := context.WithCancel(ctx) // wait for first key put on prefix opts := []v3.OpOption{v3.WithRev(resp.Header.Revision), v3.WithPrefix()} wch := client.Watch(cctx, e.keyPrefix, opts...) - for kv == nil { wr, ok := <-wch if !ok || wr.Err() != nil { @@ -189,10 +190,12 @@ func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) { } } } + cancel() } else { kv = resp.Kvs[0] } + cctx, cancel := context.WithCancel(ctx) wch := client.Watch(cctx, string(kv.Key), v3.WithRev(kv.ModRevision)) keyDeleted := false for !keyDeleted { @@ -202,6 +205,7 @@ func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) { } for _, ev := range wr.Events { if ev.Type == mvccpb.DELETE { + lastRev = ev.Kv.ModRevision keyDeleted = true break }