mirror of
https://github.com/kubernetes/client-go.git
synced 2025-06-26 15:12:06 +00:00
Merge pull request #57932 from atlassian/cancellable-leader-election
Automatic merge from submit-queue (batch tested with PRs 65256, 64236, 64919, 64879, 57932). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Cancellable leader election **What this PR does / why we need it**: Adds ability to cancel leader election. Useful in integration tests where the whole app is started and stopped in each test. **Special notes for your reviewer**: I used the `context` package - it is impossible/hard to achieve the same behaviour with just channels without spawning additional goroutines but it is trivial with `context`. See `acquire()` and `renew()` methods. **Release note**: ```release-note NONE ``` /kind enhancement /sig api-machinery Kubernetes-commit: 571b9beac5bdaa65fb581bead7464926fa81cdbf
This commit is contained in:
commit
037e6c200e
@ -49,6 +49,7 @@ limitations under the License.
|
|||||||
package leaderelection
|
package leaderelection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
@ -119,7 +120,7 @@ type LeaderElectionConfig struct {
|
|||||||
// * OnChallenge()
|
// * OnChallenge()
|
||||||
type LeaderCallbacks struct {
|
type LeaderCallbacks struct {
|
||||||
// OnStartedLeading is called when a LeaderElector client starts leading
|
// OnStartedLeading is called when a LeaderElector client starts leading
|
||||||
OnStartedLeading func(stop <-chan struct{})
|
OnStartedLeading func(context.Context)
|
||||||
// OnStoppedLeading is called when a LeaderElector client stops leading
|
// OnStoppedLeading is called when a LeaderElector client stops leading
|
||||||
OnStoppedLeading func()
|
OnStoppedLeading func()
|
||||||
// OnNewLeader is called when the client observes a leader that is
|
// OnNewLeader is called when the client observes a leader that is
|
||||||
@ -145,26 +146,28 @@ type LeaderElector struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the leader election loop
|
// Run starts the leader election loop
|
||||||
func (le *LeaderElector) Run() {
|
func (le *LeaderElector) Run(ctx context.Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
runtime.HandleCrash()
|
runtime.HandleCrash()
|
||||||
le.config.Callbacks.OnStoppedLeading()
|
le.config.Callbacks.OnStoppedLeading()
|
||||||
}()
|
}()
|
||||||
le.acquire()
|
if !le.acquire(ctx) {
|
||||||
stop := make(chan struct{})
|
return // ctx signalled done
|
||||||
go le.config.Callbacks.OnStartedLeading(stop)
|
}
|
||||||
le.renew()
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
close(stop)
|
defer cancel()
|
||||||
|
go le.config.Callbacks.OnStartedLeading(ctx)
|
||||||
|
le.renew(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunOrDie starts a client with the provided config or panics if the config
|
// RunOrDie starts a client with the provided config or panics if the config
|
||||||
// fails to validate.
|
// fails to validate.
|
||||||
func RunOrDie(lec LeaderElectionConfig) {
|
func RunOrDie(ctx context.Context, lec LeaderElectionConfig) {
|
||||||
le, err := NewLeaderElector(lec)
|
le, err := NewLeaderElector(lec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
le.Run()
|
le.Run(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLeader returns the identity of the last observed leader or returns the empty string if
|
// GetLeader returns the identity of the last observed leader or returns the empty string if
|
||||||
@ -178,13 +181,16 @@ func (le *LeaderElector) IsLeader() bool {
|
|||||||
return le.observedRecord.HolderIdentity == le.config.Lock.Identity()
|
return le.observedRecord.HolderIdentity == le.config.Lock.Identity()
|
||||||
}
|
}
|
||||||
|
|
||||||
// acquire loops calling tryAcquireOrRenew and returns immediately when tryAcquireOrRenew succeeds.
|
// acquire loops calling tryAcquireOrRenew and returns true immediately when tryAcquireOrRenew succeeds.
|
||||||
func (le *LeaderElector) acquire() {
|
// Returns false if ctx signals done.
|
||||||
stop := make(chan struct{})
|
func (le *LeaderElector) acquire(ctx context.Context) bool {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
succeeded := false
|
||||||
desc := le.config.Lock.Describe()
|
desc := le.config.Lock.Describe()
|
||||||
glog.Infof("attempting to acquire leader lease %v...", desc)
|
glog.Infof("attempting to acquire leader lease %v...", desc)
|
||||||
wait.JitterUntil(func() {
|
wait.JitterUntil(func() {
|
||||||
succeeded := le.tryAcquireOrRenew()
|
succeeded = le.tryAcquireOrRenew()
|
||||||
le.maybeReportTransition()
|
le.maybeReportTransition()
|
||||||
if !succeeded {
|
if !succeeded {
|
||||||
glog.V(4).Infof("failed to acquire lease %v", desc)
|
glog.V(4).Infof("failed to acquire lease %v", desc)
|
||||||
@ -192,17 +198,21 @@ func (le *LeaderElector) acquire() {
|
|||||||
}
|
}
|
||||||
le.config.Lock.RecordEvent("became leader")
|
le.config.Lock.RecordEvent("became leader")
|
||||||
glog.Infof("successfully acquired lease %v", desc)
|
glog.Infof("successfully acquired lease %v", desc)
|
||||||
close(stop)
|
cancel()
|
||||||
}, le.config.RetryPeriod, JitterFactor, true, stop)
|
}, le.config.RetryPeriod, JitterFactor, true, ctx.Done())
|
||||||
|
return succeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
// renew loops calling tryAcquireOrRenew and returns immediately when tryAcquireOrRenew fails.
|
// renew loops calling tryAcquireOrRenew and returns immediately when tryAcquireOrRenew fails or ctx signals done.
|
||||||
func (le *LeaderElector) renew() {
|
func (le *LeaderElector) renew(ctx context.Context) {
|
||||||
stop := make(chan struct{})
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
wait.Until(func() {
|
wait.Until(func() {
|
||||||
err := wait.Poll(le.config.RetryPeriod, le.config.RenewDeadline, func() (bool, error) {
|
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline)
|
||||||
|
defer timeoutCancel()
|
||||||
|
err := wait.PollImmediateUntil(le.config.RetryPeriod, func() (bool, error) {
|
||||||
return le.tryAcquireOrRenew(), nil
|
return le.tryAcquireOrRenew(), nil
|
||||||
})
|
}, timeoutCtx.Done())
|
||||||
le.maybeReportTransition()
|
le.maybeReportTransition()
|
||||||
desc := le.config.Lock.Describe()
|
desc := le.config.Lock.Describe()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -211,8 +221,8 @@ func (le *LeaderElector) renew() {
|
|||||||
}
|
}
|
||||||
le.config.Lock.RecordEvent("stopped leading")
|
le.config.Lock.RecordEvent("stopped leading")
|
||||||
glog.Infof("failed to renew lease %v: %v", desc, err)
|
glog.Infof("failed to renew lease %v: %v", desc, err)
|
||||||
close(stop)
|
cancel()
|
||||||
}, 0, stop)
|
}, 0, ctx.Done())
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryAcquireOrRenew tries to acquire a leader lease if it is not already acquired,
|
// tryAcquireOrRenew tries to acquire a leader lease if it is not already acquired,
|
||||||
|
Loading…
Reference in New Issue
Block a user