From d6491799d6e4e735c28530a7b49b822dce65ac5e Mon Sep 17 00:00:00 2001 From: Yifan Xu <30385241+xuyifangreeneyes@users.noreply.github.com> Date: Tue, 14 Feb 2023 16:04:09 +0800 Subject: [PATCH] domain, tidb-server: stop launching new auto analyze job when shutting down (#41346) close pingcap/tidb#41318 --- domain/domain.go | 10 +++++++++- tidb-server/main.go | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/domain/domain.go b/domain/domain.go index bd5d0f63c5698..557a209088672 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -150,6 +150,8 @@ type Domain struct { sync.Mutex sctxs map[sessionctx.Context]bool } + + stopAutoAnalyze atomicutil.Bool } type mdlCheckTableInfo struct { @@ -960,6 +962,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio jobsIdsMap: make(map[int64]string), }, } + do.stopAutoAnalyze.Store(false) do.wg = util.NewWaitGroupEnhancedWrapper("domain", do.exit, config.GetGlobalConfig().TiDBEnableExitCheck) do.SchemaValidator = NewSchemaValidator(ddlLease, do) do.expensiveQueryHandle = expensivequery.NewExpensiveQueryHandle(do.exit) @@ -2185,7 +2188,7 @@ func (do *Domain) autoAnalyzeWorker(owner owner.Manager) { for { select { case <-analyzeTicker.C: - if variable.RunAutoAnalyze.Load() && owner.IsOwner() { + if variable.RunAutoAnalyze.Load() && !do.stopAutoAnalyze.Load() && owner.IsOwner() { statsHandle.HandleAutoAnalyze(do.InfoSchema()) } case <-do.exit: @@ -2565,6 +2568,11 @@ func (do *Domain) TTLJobManager() *ttlworker.JobManager { return do.ttlJobManager.Load() } +// StopAutoAnalyze stops (*Domain).autoAnalyzeWorker to launch new auto analyze jobs. +func (do *Domain) StopAutoAnalyze() { + do.stopAutoAnalyze.Store(true) +} + func init() { initByLDFlagsForGlobalKill() telemetry.GetDomainInfoSchema = func(ctx sessionctx.Context) infoschema.InfoSchema { diff --git a/tidb-server/main.go b/tidb-server/main.go index 22f4ae9ae7271..68e46d977a418 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -858,6 +858,7 @@ func closeDomainAndStorage(storage kv.Storage, dom *domain.Domain) { } func cleanup(svr *server.Server, storage kv.Storage, dom *domain.Domain, graceful bool) { + dom.StopAutoAnalyze() if graceful { done := make(chan struct{}) svr.GracefulDown(context.Background(), done)