Skip to content

Commit

Permalink
client: ensure the right leader is picked in watcher results for camp…
Browse files Browse the repository at this point in the history
…aign and observe impls.

Signed-off-by: Cristian Ferretti <[email protected]>
  • Loading branch information
jcferretti committed Jun 14, 2024
1 parent 260dd77 commit 7e419db
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions client/v3/concurrency/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (e *Election) Campaign(ctx context.Context, val string) error {
k := fmt.Sprintf("%s%x", e.keyPrefix, s.Lease())
txn := client.Txn(ctx).If(v3.Compare(v3.CreateRevision(k), "=", 0))
txn = txn.Then(v3.OpPut(k, val, v3.WithLease(s.Lease())))
txn = txn.Else(v3.OpGet(k))
txn = txn.Else(v3.OpGet(k, v3.WithFirstCreate()...))
resp, err := txn.Commit()
if err != nil {
return err
Expand Down Expand Up @@ -195,13 +195,15 @@ func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) {
return
}
// only accept puts; a delete will make observe() spin
// find the smaller create revision among the puts
for _, ev := range wr.Events {
if ev.Type == mvccpb.PUT {
hdr, kv = &wr.Header, ev.Kv
// may have multiple revs; hdr.rev = the last rev
// set to kv's rev in case batch has multiple Puts
hdr.Revision = kv.ModRevision
break
if kv == nil || ev.Kv.CreateRevision < kv.CreateRevision {
hdr, kv = &wr.Header, ev.Kv
// may have multiple revs; hdr.rev = the last rev
// set to kv's rev in case batch has multiple Puts
hdr.Revision = kv.ModRevision
}
}
}
}
Expand Down

0 comments on commit 7e419db

Please sign in to comment.