concurrency: only delete on election resignation if create revision matches

Addresses a case where two clients share the same lease. A client resigns but
disconnects / crashes and doesn't realize it. Another client reuses the
lease and gets leadership with a new key. The old client comes back and
tries to resign again, revoking the new leadership of the new client.
This commit is contained in:
Anthony Romano 2017-03-30 11:14:56 -07:00
parent 4b5bb7f212
commit d1ae4cd5bd

View File

@ -115,7 +115,11 @@ func (e *Election) Resign(ctx context.Context) (err error) {
return nil
}
client := e.session.Client()
_, err = client.Delete(ctx, e.leaderKey)
cmp := v3.Compare(v3.CreateRevision(e.leaderKey), "=", e.leaderRev)
resp, err := client.Txn(ctx).If(cmp).Then(v3.OpDelete(e.leaderKey)).Commit()
if err == nil {
e.hdr = resp.Header
}
e.leaderKey = ""
e.leaderSession = nil
return err