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

Improve autopilot shutdown to be idempotent #3908

Merged
merged 2 commits into from
Feb 21, 2018
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
20 changes: 20 additions & 0 deletions agent/consul/autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ type Autopilot struct {
clusterHealth OperatorHealthReply
clusterHealthLock sync.RWMutex

enabled bool
removeDeadCh chan struct{}
shutdownCh chan struct{}
shutdownLock sync.Mutex
waitGroup sync.WaitGroup
}

Expand All @@ -62,18 +64,36 @@ func NewAutopilot(logger *log.Logger, delegate Delegate, interval, healthInterva
}

func (a *Autopilot) Start() {
a.shutdownLock.Lock()
defer a.shutdownLock.Unlock()

// Nothing to do
if a.enabled {
return
}

a.shutdownCh = make(chan struct{})
a.waitGroup = sync.WaitGroup{}
a.clusterHealth = OperatorHealthReply{}

a.waitGroup.Add(2)
go a.run()
go a.serverHealthLoop()
a.enabled = true
}

func (a *Autopilot) Stop() {
a.shutdownLock.Lock()
defer a.shutdownLock.Unlock()

// Nothing to do
if !a.enabled {
return
}

close(a.shutdownCh)
a.waitGroup.Wait()
a.enabled = false
}

// run periodically looks for nonvoting servers to promote and dead servers to remove.
Expand Down
14 changes: 14 additions & 0 deletions agent/consul/autopilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ import (
"github.com/hashicorp/serf/serf"
)

func TestAutopilot_IdempotentShutdown(t *testing.T) {
dir1, s1 := testServerWithConfig(t, nil)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
retry.Run(t, func(r *retry.R) { r.Check(waitForLeader(s1)) })

s1.autopilot.Start()
s1.autopilot.Start()
s1.autopilot.Start()
s1.autopilot.Stop()
s1.autopilot.Stop()
s1.autopilot.Stop()
}

func TestAutopilot_CleanupDeadServer(t *testing.T) {
t.Parallel()
for i := 1; i <= 3; i++ {
Expand Down