Worker graceful shutdown (#2455)

* add graceful shutdown to worker
This commit is contained in:
Irena Rindos authored and GitHub committed 2022-09-19 09:59:18 -04:00
1 parent a2ef14283b
commit d951e1ebc1
16 files changed
+241 -229

No files matched your search

@@ -215,7 +215,7 @@ func TestReloadControllerDatabase(t *testing.T) {
require.NoError(t, row.Scan(&lock))
require.Equal(t, 1, lock)
close(cmd.ShutdownCh)
cmd.ShutdownCh <- struct{}{}
wg.Wait()
}
@@ -317,7 +317,7 @@ func TestReloadControllerDatabase_InvalidNewDatabaseState(t *testing.T) {
require.NoError(t, row.Scan(&lock))
require.Equal(t, 1, lock)
close(cmd.ShutdownCh)
cmd.ShutdownCh <- struct{}{}
wg.Wait()
}
@@ -167,7 +167,6 @@ func TestServer_ReloadListener(t *testing.T) {
testCertificateSerial("193080739105342897219784862820114567438786419504")
close(cmd.ShutdownCh)
cmd.ShutdownCh <- struct{}{}
wg.Wait()
}
+63 -47
View File
@@ -6,10 +6,10 @@ import (
"fmt"
"net"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"sync"
atm "sync/atomic"
"time"
"github.com/hashicorp/boundary/globals"
@@ -667,69 +667,83 @@ func (c *Command) StartWorker() error {
func (c *Command) WaitForInterrupt() int {
const op = "server.(Command).WaitForInterrupt"
// Wait for shutdown
shutdownTriggered := false
// Add a force-shutdown goroutine to consume another interrupt
abortForceShutdownCh := make(chan struct{})
defer close(abortForceShutdownCh)
var shutdownCompleted atm.Bool
shutdownTriggerCount := 0
runShutdownLogic := func() {
go func() {
shutdownCh := make(chan os.Signal, 4)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
for {
select {
case <-shutdownCh:
c.UI.Error("Forcing shutdown")
os.Exit(base.CommandUserError)
case <-c.ServerSideShutdownCh:
// Drain connections in case this is hit more than once
case <-abortForceShutdownCh:
// No-op, we just use this to shut down the goroutine
return
}
}
}()
if c.Config.Controller != nil && c.opsServer != nil {
c.opsServer.WaitIfHealthExists(c.Config.Controller.GracefulShutdownWaitDuration, c.UI)
var workerShutdownOnce sync.Once
workerShutdownFunc := func() {
if err := c.worker.Shutdown(); err != nil {
c.UI.Error(fmt.Errorf("Error shutting down worker: %w", err).Error())
}
// Do worker shutdown
if c.Config.Worker != nil {
if err := c.worker.Shutdown(); err != nil {
c.UI.Error(fmt.Errorf("Error shutting down worker: %w", err).Error())
}
}
workerGracefulShutdownFunc := func() {
if err := c.worker.GracefulShutdown(); err != nil {
c.UI.Error(fmt.Errorf("Error shutting down worker gracefully: %w", err).Error())
}
// Do controller shutdown
if c.Config.Controller != nil {
if err := c.controller.Shutdown(); err != nil {
c.UI.Error(fmt.Errorf("Error shutting down controller: %w", err).Error())
}
workerShutdownOnce.Do(workerShutdownFunc)
}
var controllerOnce sync.Once
controllerShutdownFunc := func() {
if err := c.controller.Shutdown(); err != nil {
c.UI.Error(fmt.Errorf("Error shutting down controller: %w", err).Error())
}
if c.opsServer != nil {
err := c.opsServer.Shutdown()
if err != nil {
c.UI.Error(fmt.Errorf("Error shutting down ops listeners: %w", err).Error())
c.UI.Error(fmt.Errorf("Failed to shutdown ops listeners: %w", err).Error())
}
}
shutdownTriggered = true
}
for !shutdownTriggered {
runShutdownLogic := func() {
switch {
case shutdownTriggerCount == 1:
c.ContextCancel()
go func() {
if c.Config.Controller != nil && c.opsServer != nil {
c.opsServer.WaitIfHealthExists(c.Config.Controller.GracefulShutdownWaitDuration, c.UI)
}
if c.Config.Worker != nil {
c.UI.Output("==> Boundary server graceful shutdown triggered, interrupt again to enter shutdown")
workerGracefulShutdownFunc()
} else {
c.UI.Output("==> Boundary server shutdown triggered, interrupt again to force")
}
if c.Config.Controller != nil {
controllerOnce.Do(controllerShutdownFunc)
}
shutdownCompleted.Store(true)
}()
case shutdownTriggerCount == 2 && c.Config.Worker != nil:
go func() {
if c.Config.Worker != nil {
workerShutdownOnce.Do(workerShutdownFunc)
}
if c.Config.Controller != nil {
controllerOnce.Do(controllerShutdownFunc)
}
shutdownCompleted.Store(true)
}()
case shutdownTriggerCount >= 2:
c.UI.Error("Forcing shutdown")
os.Exit(base.CommandCliError)
}
}
for !shutdownCompleted.Load() {
select {
case <-c.ServerSideShutdownCh:
c.UI.Output("==> Boundary server self-terminating")
shutdownTriggerCount++
runShutdownLogic()
case <-c.ShutdownCh:
c.UI.Output("==> Boundary server shutdown triggered, interrupt again to force")
shutdownTriggerCount++
runShutdownLogic()
case <-c.SighupCh:
@@ -784,6 +798,8 @@ func (c *Command) WaitForInterrupt() int {
buf := make([]byte, 32*1024*1024)
n := runtime.Stack(buf[:], true)
event.WriteSysEvent(context.TODO(), op, "goroutine trace", "stack", string(buf[:n]))
case <-time.After(10 * time.Millisecond):
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ func testServerCommand(t *testing.T, opts testServerCommandOpts) *Command {
require := require.New(t)
t.Helper()
cmd := &Command{
Server: base.NewServer(base.NewCommand(cli.NewMockUi())),
Server: base.NewServer(base.NewServerCommand(cli.NewMockUi())),
SighupCh: base.MakeSighupCh(),
startedCh: make(chan struct{}),
reloadedCh: make(chan struct{}, 5),
@@ -135,7 +135,6 @@ pollSecondController:
}
}
close(cmd.ShutdownCh)
cmd.ShutdownCh <- struct{}{}
wg.Wait()
}
@@ -98,13 +98,13 @@ func TestServer_ShutdownWorker(t *testing.T) {
t.Log("running initial send/recv test")
sConn.TestSendRecvAll(t)
// Now, shut the worker down.
close(workerCmd.ShutdownCh)
// Shutdown the worker and close the connection, as the worker will otherwise wait for it to close.
sConn.Close()
workerCmd.ShutdownCh <- struct{}{}
if <-workerCodeChan != 0 {
output := workerCmd.UI.(*cli.MockUi).ErrorWriter.String() + workerCmd.UI.(*cli.MockUi).OutputWriter.String()
require.FailNow(output, "command exited with non-zero error code")
}
// Connection should fail, and the session should be closed on the DB.
sConn.TestSendRecvFail(t)
sess.ExpectConnectionStateOnController(ctx, t, testController.Controller().ConnectionRepoFn, session.StatusClosed)
@@ -130,7 +130,7 @@ func TestServer_ReloadWorkerTags(t *testing.T) {
time.Sleep(10 * time.Second)
fetchWorkerTags("test", "foo", []string{"bar", "baz"})
close(cmd.ShutdownCh)
cmd.ShutdownCh <- struct{}{}
wg.Wait()
}