Skip to content
This repository has been archived by the owner on Apr 14, 2019. It is now read-only.

Commit

Permalink
wait for the new master to become active
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Aug 9, 2017
1 parent 2f2538e commit 582f3f7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pgha/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (e *Election) Start() {
if !e.status.IsMaster() {
log.Info("Promoting slave to master")
execRepmgrPromote(60)
isMaster := e.WaitForMasterSwitch(60, 1)
if !isMaster {
log.Fatal("Master promotion timeout")
}
}
e.status.SetConsulStatus(true, LeaderCode, "leader")
<-electionChan
Expand Down Expand Up @@ -138,6 +142,21 @@ func (e *Election) GetLeaderWithRetry(retry int, wait int) (string, error) {
return leader, err
}

func (e *Election) WaitForMasterSwitch(retry int, wait int) bool {
var leader bool
for retry > 0 {
leader = e.status.IsMaster()
if !leader {
retry--
time.Sleep(time.Duration(wait) * time.Second)
} else {
return leader
}
}

return leader
}

func (e *Election) Fallback() error {
e.lockChan <- struct{}{}
return e.consulLock.Unlock()
Expand Down

0 comments on commit 582f3f7

Please sign in to comment.