Make sure revokeLeadership is called if establishLeadership errors

This commit is contained in:
Preetha Appan 2018-02-21 12:33:22 -06:00
parent d1ede2c93d
commit f59abcc394
No known key found for this signature in database
GPG Key ID: 9F7C19990A50EAFC
1 changed files with 7 additions and 0 deletions

View File

@ -123,6 +123,11 @@ RECONCILE:
if !establishedLeader { if !establishedLeader {
if err := s.establishLeadership(); err != nil { if err := s.establishLeadership(); err != nil {
s.logger.Printf("[ERR] consul: failed to establish leadership: %v", err) s.logger.Printf("[ERR] consul: failed to establish leadership: %v", err)
// Immediately revoke leadership since we didn't successfully
// establish leadership.
if err := s.revokeLeadership(); err != nil {
s.logger.Printf("[ERR] consul: failed to revoke leadership: %v", err)
}
goto WAIT goto WAIT
} }
establishedLeader = true establishedLeader = true
@ -178,6 +183,7 @@ WAIT:
// previously inflight transactions have been committed and that our // previously inflight transactions have been committed and that our
// state is up-to-date. // state is up-to-date.
func (s *Server) establishLeadership() error { func (s *Server) establishLeadership() error {
defer metrics.MeasureSince([]string{"consul", "leader", "establish_leadership"}, time.Now())
// This will create the anonymous token and master token (if that is // This will create the anonymous token and master token (if that is
// configured). // configured).
if err := s.initializeACL(); err != nil { if err := s.initializeACL(); err != nil {
@ -213,6 +219,7 @@ func (s *Server) establishLeadership() error {
// revokeLeadership is invoked once we step down as leader. // revokeLeadership is invoked once we step down as leader.
// This is used to cleanup any state that may be specific to a leader. // This is used to cleanup any state that may be specific to a leader.
func (s *Server) revokeLeadership() error { func (s *Server) revokeLeadership() error {
defer metrics.MeasureSince([]string{"consul", "leader", "revoke_leadership"}, time.Now())
// Disable the tombstone GC, since it is only useful as a leader // Disable the tombstone GC, since it is only useful as a leader
s.tombstoneGC.SetEnabled(false) s.tombstoneGC.SetEnabled(false)