From a13454e386564a1000f64feb89a68a049cabf65a Mon Sep 17 00:00:00 2001 From: xuyifan <675434007@qq.com> Date: Tue, 3 Jan 2023 19:49:12 +0800 Subject: [PATCH] kill auto analyze when gracefully shutting down --- 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 4c1528dd1e78a..1fad475964152 100644 --- a/server/server.go +++ b/server/server.go @@ -790,6 +790,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.") @@ -804,12 +815,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 5d95e6bacce99..ac55fa5847106 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -822,6 +822,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() }