Skip to content

Conversation

iamritikbhardwaj
Copy link

Fixes [[#7160]
This pull request addresses a bug in the LeaderElector class, specifically in the canBecomeLeader() method. Previously, the method used the local leaderElectionConfig.getLeaseDuration() value to determine if a lease had expired.

This logic is flawed in scenarios where multiple leader election participants use different leaseDuration values. According to Kubernetes leader election semantics, clients should evaluate lease expiration based on the duration stored in the Lease object itself (leaseDurationSeconds), not their own configuration.

This discrepancy can lead to premature leadership takeovers and split-brain behavior. For example:

  • One client (LE2) runs with leaseDuration = 30s, retryPeriod = 20s.
  • Another client (LE1) runs with leaseDuration = 10s, retryPeriod = 1s.

Even if LE2 is correctly renewing the lease within its 30-second window, LE1 may incorrectly assume the lease has expired after 10 seconds, simply because it's using its own local config to calculate expiry.

Fix

The method now uses the lease duration published in the lease record:

Duration.ofSeconds(leaderElectionRecord.getLeaseDurationSeconds())

This ensures all clients interpret lease expiry based on the same source of truth — the current lease holder’s declared configuration — which aligns with how Kubernetes itself handles leader election.


Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • The code I am contributing is licensed under the Apache 2.0 License
  • A CHANGELOG entry has been added for this change
  • Unit or integration tests have been added or updated
  • Documentation and/or Javadocs have been added or updated where relevant
  • No new issues or regressions are reported by SonarCloud
  • The change has been tested on Kubernetes
  • The change has been tested on OpenShift

…,getLeaseDurationSeconds() in canBecomeLeader
@shawkins
Copy link
Contributor

The method now uses the lease duration published in the lease record:

The code change does not match this description.

Also a test case is needed.

@iamritikbhardwaj
Copy link
Author

Okay then, i wiil read it again carefully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants