Browse Source

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.
Anthony Romano 8 years ago
parent
commit
d1ae4cd5bd
1 changed files with 5 additions and 1 deletions
  1. 5 1
      clientv3/concurrency/election.go

+ 5 - 1
clientv3/concurrency/election.go

@@ -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