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

Fix multiple leader election #1338

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions core/pkg/ingress/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ type statusSync struct {
// workqueue used to keep in sync the status IP/s
// in the Ingress rules
syncQueue *task.Queue

runLock *sync.Mutex
}

// Run starts the loop to keep the status in sync
func (s statusSync) Run(stopCh <-chan struct{}) {
go wait.Forever(s.elector.Run, 0)
go s.elector.Run()
go wait.Forever(s.update, updateInterval)
go s.syncQueue.Run(time.Second, stopCh)
<-stopCh
Expand Down Expand Up @@ -140,9 +138,6 @@ func (s statusSync) Shutdown() {
}

func (s *statusSync) sync(key interface{}) error {
s.runLock.Lock()
defer s.runLock.Unlock()

if s.syncQueue.IsShuttingDown() {
glog.V(2).Infof("skipping Ingress status update (shutting down in progress)")
return nil
Expand All @@ -162,18 +157,6 @@ func (s *statusSync) sync(key interface{}) error {
return nil
}

// callback invoked function when a new leader is elected
func (s *statusSync) callback(leader string) {
if s.syncQueue.IsShuttingDown() {
return
}

glog.V(2).Infof("new leader elected (%v)", leader)
if leader == s.pod.Name {
glog.V(2).Infof("I am the new status update leader")
}
}

func (s statusSync) keyfunc(input interface{}) (interface{}, error) {
return input, nil
}
Expand All @@ -186,9 +169,9 @@ func NewStatusSyncer(config Config) Sync {
}

st := statusSync{
pod: pod,
runLock: &sync.Mutex{},
Config: config,
pod: pod,

Config: config,
}
st.syncQueue = task.NewCustomTaskQueue(st.sync, st.keyfunc)

Expand All @@ -201,10 +184,13 @@ func NewStatusSyncer(config Config) Sync {

callbacks := leaderelection.LeaderCallbacks{
OnStartedLeading: func(stop <-chan struct{}) {
st.callback(pod.Name)
glog.V(2).Infof("I am the new status update leader")
},
OnStoppedLeading: func() {
st.callback("")
glog.V(2).Infof("I am not status update leader anymore")
},
OnNewLeader: func(identity string) {
glog.Infof("new leader elected: %v", identity)
},
}

Expand All @@ -220,16 +206,17 @@ func NewStatusSyncer(config Config) Sync {
ConfigMapMeta: meta_v1.ObjectMeta{Namespace: pod.Namespace, Name: electionID},
Client: config.Client.CoreV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: electionID,
Identity: pod.Name,
EventRecorder: recorder,
},
}

ttl := 30 * time.Second
le, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock,
LeaseDuration: 30 * time.Second,
RenewDeadline: 15 * time.Second,
RetryPeriod: 5 * time.Second,
LeaseDuration: ttl,
RenewDeadline: ttl / 2,
RetryPeriod: ttl / 4,
Callbacks: callbacks,
})

Expand Down
6 changes: 1 addition & 5 deletions core/pkg/ingress/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package status
import (
"os"
"sort"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -245,7 +244,6 @@ func buildStatusSync() statusSync {
"lable_sig": "foo_pod",
},
},
runLock: &sync.Mutex{},
syncQueue: task.NewTaskQueue(fakeSynFn),
Config: Config{
Client: buildSimpleClientSet(),
Expand Down Expand Up @@ -328,9 +326,7 @@ func TestStatusActions(t *testing.T) {
}

func TestCallback(t *testing.T) {
fk := buildStatusSync()
// do nothing
fk.callback("foo_base_pod")
buildStatusSync()
}

func TestKeyfunc(t *testing.T) {
Expand Down