From 52c603bf90992e0f2f2fadc10cae027f7972346b Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 18 Jan 2023 20:09:50 +0800 Subject: [PATCH] server, tidb-server: kill auto analyze when gracefully shutting down (#40284) (#40302) close pingcap/tidb#40038 --- server/server.go | 18 ++++++++++++------ tidb-server/main.go | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/server.go b/server/server.go index ef9228cf79a04..bdf10420e88b4 100644 --- a/server/server.go +++ b/server/server.go @@ -721,6 +721,17 @@ func killConn(conn *clientConn) { } } +// KillSysProcesses kill sys processes such as auto analyze. +func (s *Server) KillSysProcesses() { + if s.dom == nil { + return + } + sysProcTracker := s.dom.SysProcTracker() + for connID := range sysProcTracker.GetSysProcessList() { + sysProcTracker.KillSysProcess(connID) + } +} + // KillAllConnections kills all connections when server is not gracefully shutdown. func (s *Server) KillAllConnections() { logutil.BgLogger().Info("[server] kill all connections.") @@ -735,12 +746,7 @@ func (s *Server) KillAllConnections() { killConn(conn) } - if s.dom != nil { - sysProcTracker := s.dom.SysProcTracker() - for connID := range sysProcTracker.GetSysProcessList() { - sysProcTracker.KillSysProcess(connID) - } - } + s.KillSysProcesses() } var gracefulCloseConnectionsTimeout = 15 * time.Second diff --git a/tidb-server/main.go b/tidb-server/main.go index 06e26eb2341c6..88b3c63ef8500 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -775,6 +775,9 @@ func cleanup(svr *server.Server, storage kv.Storage, dom *domain.Domain, gracefu if graceful { done := make(chan struct{}) svr.GracefulDown(context.Background(), done) + // Kill sys processes such as auto analyze. Otherwise, tidb-server cannot exit until auto analyze is finished. + // See https://github.com/pingcap/tidb/issues/40038 for details. + svr.KillSysProcesses() } else { svr.TryGracefulDown() }