mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: ensure the right leader is picked in watcher results for campaign and observe impls.
Signed-off-by: Cristian Ferretti <jcferretti2020@gmail.com>
This commit is contained in:
parent
260dd77ed4
commit
7e419db4f3
@ -73,7 +73,7 @@ func (e *Election) Campaign(ctx context.Context, val string) error {
|
|||||||
k := fmt.Sprintf("%s%x", e.keyPrefix, s.Lease())
|
k := fmt.Sprintf("%s%x", e.keyPrefix, s.Lease())
|
||||||
txn := client.Txn(ctx).If(v3.Compare(v3.CreateRevision(k), "=", 0))
|
txn := client.Txn(ctx).If(v3.Compare(v3.CreateRevision(k), "=", 0))
|
||||||
txn = txn.Then(v3.OpPut(k, val, v3.WithLease(s.Lease())))
|
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()
|
resp, err := txn.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -195,13 +195,15 @@ func (e *Election) observe(ctx context.Context, ch chan<- v3.GetResponse) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// only accept puts; a delete will make observe() spin
|
// only accept puts; a delete will make observe() spin
|
||||||
|
// find the smaller create revision among the puts
|
||||||
for _, ev := range wr.Events {
|
for _, ev := range wr.Events {
|
||||||
if ev.Type == mvccpb.PUT {
|
if ev.Type == mvccpb.PUT {
|
||||||
|
if kv == nil || ev.Kv.CreateRevision < kv.CreateRevision {
|
||||||
hdr, kv = &wr.Header, ev.Kv
|
hdr, kv = &wr.Header, ev.Kv
|
||||||
// may have multiple revs; hdr.rev = the last rev
|
// may have multiple revs; hdr.rev = the last rev
|
||||||
// set to kv's rev in case batch has multiple Puts
|
// set to kv's rev in case batch has multiple Puts
|
||||||
hdr.Revision = kv.ModRevision
|
hdr.Revision = kv.ModRevision
|
||||||
break
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user