mirror of
https://github.com/kubernetes/client-go.git
synced 2025-08-31 06:34:50 +00:00
enhance leaderelection code
Kubernetes-commit: 9fe2a5a430055657186491c23e85cc489e401bc7
This commit is contained in:
committed by
Kubernetes Publisher
parent
037e6c200e
commit
7d6d732419
@@ -130,10 +130,6 @@ type LeaderCallbacks struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LeaderElector is a leader election client.
|
// LeaderElector is a leader election client.
|
||||||
//
|
|
||||||
// possible future methods:
|
|
||||||
// * (le *LeaderElector) IsLeader()
|
|
||||||
// * (le *LeaderElector) GetLeader()
|
|
||||||
type LeaderElector struct {
|
type LeaderElector struct {
|
||||||
config LeaderElectionConfig
|
config LeaderElectionConfig
|
||||||
// internal bookkeeping
|
// internal bookkeeping
|
||||||
@@ -259,14 +255,14 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
|
|||||||
le.observedTime = time.Now()
|
le.observedTime = time.Now()
|
||||||
}
|
}
|
||||||
if le.observedTime.Add(le.config.LeaseDuration).After(now.Time) &&
|
if le.observedTime.Add(le.config.LeaseDuration).After(now.Time) &&
|
||||||
oldLeaderElectionRecord.HolderIdentity != le.config.Lock.Identity() {
|
!le.IsLeader() {
|
||||||
glog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
|
glog.V(4).Infof("lock is held by %v and has not yet expired", oldLeaderElectionRecord.HolderIdentity)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. We're going to try to update. The leaderElectionRecord is set to it's default
|
// 3. We're going to try to update. The leaderElectionRecord is set to it's default
|
||||||
// here. Let's correct it before updating.
|
// here. Let's correct it before updating.
|
||||||
if oldLeaderElectionRecord.HolderIdentity == le.config.Lock.Identity() {
|
if le.IsLeader() {
|
||||||
leaderElectionRecord.AcquireTime = oldLeaderElectionRecord.AcquireTime
|
leaderElectionRecord.AcquireTime = oldLeaderElectionRecord.AcquireTime
|
||||||
leaderElectionRecord.LeaderTransitions = oldLeaderElectionRecord.LeaderTransitions
|
leaderElectionRecord.LeaderTransitions = oldLeaderElectionRecord.LeaderTransitions
|
||||||
} else {
|
} else {
|
||||||
@@ -283,12 +279,12 @@ func (le *LeaderElector) tryAcquireOrRenew() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LeaderElector) maybeReportTransition() {
|
func (le *LeaderElector) maybeReportTransition() {
|
||||||
if l.observedRecord.HolderIdentity == l.reportedLeader {
|
if le.observedRecord.HolderIdentity == le.reportedLeader {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.reportedLeader = l.observedRecord.HolderIdentity
|
le.reportedLeader = le.observedRecord.HolderIdentity
|
||||||
if l.config.Callbacks.OnNewLeader != nil {
|
if le.config.Callbacks.OnNewLeader != nil {
|
||||||
go l.config.Callbacks.OnNewLeader(l.reportedLeader)
|
go le.config.Callbacks.OnNewLeader(le.reportedLeader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user