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

+6 -3
View File
@@ -287,7 +287,7 @@ func (tc *TestController) buildClient() {
func (tc *TestController) Shutdown() {
tc.shutdownOnce.Do(func() {
if tc.b != nil {
close(tc.b.ShutdownCh)
tc.b.ContextCancel()
}
tc.cancel()
@@ -492,10 +492,13 @@ func TestControllerConfig(t testing.TB, ctx context.Context, tc *TestController,
opts = new(TestControllerOpts)
}
ctxTest, cancel := context.WithCancel(context.Background())
// Base server
tc.b = base.NewServer(&base.Command{
Context: ctx,
ShutdownCh: make(chan struct{}),
Context: ctxTest,
ContextCancel: cancel,
ShutdownCh: make(chan struct{}),
})
// Get dev config, or use a provided one
+35
View File
@@ -423,6 +423,38 @@ func (w *Worker) Start() error {
return nil
}
func (w *Worker) hasActiveConnection() bool {
activeConnection := false
w.sessionManager.ForEachLocalSession(
func(s session.Session) bool {
conns := s.GetLocalConnections()
for _, v := range conns {
if v.Status == pbs.CONNECTIONSTATUS_CONNECTIONSTATUS_CONNECTED {
activeConnection = true
return false
}
}
return true
})
return activeConnection
}
// Graceful shutdown sets the worker state to "shutdown" and will wait to return until there
// are no longer any active connections.
func (w *Worker) GracefulShutdown() error {
const op = "worker.(Worker).GracefulShutdown"
event.WriteSysEvent(w.baseContext, op, "worker entering graceful shutdown")
w.operationalState.Store(server.ShutdownOperationalState)
// Wait for connections to drain
for w.hasActiveConnection() {
time.Sleep(time.Millisecond * 250)
}
event.WriteSysEvent(w.baseContext, op, "worker connections have drained")
return nil
}
// Shutdown shuts down the workers. skipListeners can be used to not stop
// listeners, useful for tests if we want to stop and start a worker. In order
// to create new listeners we'd have to migrate listener setup logic here --
@@ -433,9 +465,11 @@ func (w *Worker) Shutdown() error {
event.WriteSysEvent(w.baseContext, op, "already shut down, skipping")
return nil
}
event.WriteSysEvent(w.baseContext, op, "worker shutting down")
// Set state to shutdown
w.operationalState.Store(server.ShutdownOperationalState)
// Stop listeners first to prevent new connections to the
// controller.
defer w.started.Store(false)
@@ -479,6 +513,7 @@ func (w *Worker) Shutdown() error {
}
}
event.WriteSysEvent(w.baseContext, op, "worker finished shutting down")
return nil
}