Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prioritized leader election #103442

Closed
wants to merge 1 commit into from

Conversation

howardjohn
Copy link
Contributor

@howardjohn howardjohn commented Jul 2, 2021

What type of PR is this?

/kind feature

What this PR does / why we need it:

This introduces a new concept to the leaderelection package, allowing
participants to declare a precedence order between certain clients.

Example use cases:

  • I have multiple versions of an application running concurrently in the
    cluster; I want the newest one to become the leader (cc @Monkeyanator)
  • I have multiple replicas with different resources. I want the largest
    replica to be the leader
  • I am implementing a cross-region controller (think multicluster
    service discovery) and the geographically closest region to be the
    leader (to avoid something like a US controller acting on a cluster in
    Asia, when I have a controller replica already running in Asia) (cc @JeremyOT)

Library users can define an arbitrary key to indicate whatever metadata
they choose. They may also choose to implement a KeyComparison function.
If present, this function will be used to determine if our key is higher
priority than the existing leader; if it is, we will acquire the lock
even though its already held by another.

Version skew compatibility: old clients will not set a key, nor will
they implement a KeyComparison function. If they see a lock with a key
set, they will gracefully ignore it, which is not an issue - this just
means they will never steal a lock. Because the key is empty, a
KeyComparison function must account for "" as a key value, but they
would need to do that regardless as the field is optional. As a result,
I do not expect any version skew issues.

Which issue(s) this PR fixes:

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Added a new `KeyComparison` feature to the client-go leaderelection package to allow prioritized leader elections.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 2, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @howardjohn. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 2, 2021
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jul 2, 2021
@nmittler
Copy link

nmittler commented Jul 2, 2021

@JeremyOT FYI

@BenTheElder
Copy link
Member

/ok-to-test
FYI I'm pretty sure you've been active enough to apply for https://github.com/kubernetes/community/blob/master/community-membership.md and get automatic ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 2, 2021
@dims
Copy link
Member

dims commented Jul 6, 2021

/assign @deads2k @sttts

@caesarxuchao
Copy link
Member

/triage accepted
/assgin @jiahuif

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jul 8, 2021
@jiahuif
Copy link
Member

jiahuif commented Jul 9, 2021

Could you add tests where KeyComparison is set?

This introduces a new concept to the `leaderelection` package, allowing
participants to declare a precedence order between certain clients.

Example use cases:
* I have multiple versions of an application running concurrently in the
cluster; I want the newest one to become the leader
* I have multiple replicas with different resources. I want the largest
replica to be the leader
* I am implementing a cross-region controller (think multicluster
service discovery) and the geographically closest region to be the
leader (to avoid something like a US controller acting on a cluster in
Asia, when I have a controller replica already running in Asia)

Library users can define an arbitrary key to indicate whatever metadata
they choose. They may also choose to implement a KeyComparison function.
If present, this function will be used to determine if our key is higher
priority than the existing leader; if it is, we will acquire the lock
even though its already held by another.

Version skew compatibility: old clients will not set a key, nor will
they implement a KeyComparison function. If they see a lock with a key
set, they will gracefully ignore it, which is not an issue - this just
means they will never steal a lock. Because the key is empty, a
KeyComparison function must account for `""` as a key value, but they
would need to do that regardless as the field is optional. As a result,
I do not expect any version skew issues.
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 12, 2021
@howardjohn
Copy link
Contributor Author

Could you add tests where KeyComparison is set?

Yes, I have added them and updated the PR

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: howardjohn
To complete the pull request process, please ask for approval from deads2k after the PR has been reviewed.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Jul 12, 2021

@howardjohn: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Rerun command
pull-kubernetes-unit 67fa728 link /test pull-kubernetes-unit
pull-kubernetes-integration 67fa728 link /test pull-kubernetes-integration

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@howardjohn
Copy link
Contributor Author

After adding tests, realized this doesn't work with Lease lock, since we need the API change to staging/src/k8s.io/api/coordination/v1/types.go. Possible options:

  1. Change the API
  2. Use an annotation to store the field
  3. Do not support Lease

I am open to any of the three; (1) seems right but I don't know how much we are able to modify v1 api

@howardjohn
Copy link
Contributor Author

@jiahuif any feedback on #103442 (comment)? If not I will go with (3)

@howardjohn
Copy link
Contributor Author

Ping on #103442 (comment)? Thanks!

@jiahuif
Copy link
Member

jiahuif commented Jul 23, 2021

@howardjohn Sorry for missing this out. option 3 will greatly limits the usefulness of this improvement because Lease is mostly used type of leader election. Because you are about to change a stable API, I believe it is better to write a KEP to purpose the feature in a more formal way. See https://github.com/kubernetes/enhancements/tree/master/keps

@jiahuif
Copy link
Member

jiahuif commented Jul 23, 2021

Also, preemptible leader lock is entirely new, which I believe requires a KEP anyways.

@howardjohn
Copy link
Contributor Author

@jiahuif I have opened kubernetes/enhancements#2835

@@ -120,3 +120,8 @@ func (cml *ConfigMapLock) Describe() string {
func (cml *ConfigMapLock) Identity() string {
return cml.LockConfig.Identity
}

// Identity returns the Identity of the lock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Identity returns the Identity of the lock
// Key returns the Key of the lock

// We will pre-empt the existing leader.
klog.V(4).Infof("lock is held by %v with key %v, but our key (%v) evicts it", oldLeaderElectionRecord.HolderIdentity, oldLeaderElectionRecord.HolderKey, le.config.Lock.Key())
} else {
klog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
klog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
klog.V(4).InfoS("lock is held and has not yet expired", "holderIdentity", oldLeaderElectionRecord.HolderIdentity)

@k8s-ci-robot
Copy link
Contributor

@howardjohn: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 17, 2021
@@ -101,6 +101,11 @@ func (ll *LeaseLock) Identity() string {
return ll.LockConfig.Identity
}

// Key returns the Key of the lock
func (ll *LeaseLock) Key() string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling Update() or Create() on the LeaseLock leaves out the HolderKey. Lease lock is different from the rest of the resource lock impls since it uses the lease spec instead of cramming all the fields in an annotation so we need to find a new place to put the key (putting HolderKey and priority as a concept in v1.Lease seems wrong but I'm not super clear on what lease gets used for in k8s besides kubelet heartbeats).

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 23, 2021
@dims
Copy link
Member

dims commented Jan 10, 2022

Is this PR still needed, please rebase if so (or we can close it?)

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Feb 9, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@nmittler
Copy link

@howardjohn is this still active? Should it be re-opened?

@howardjohn
Copy link
Contributor Author

@nmittler currently in Istio we have forked the leader election library. kubernetes/enhancements#2835 has been scope-creeped beyond the initial requirements, moving it beyond my bandwidth + expertise. If we can drop those additional requirements or get someone else involved as well I think we could progress this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet