diff --git a/domain/domain.go b/domain/domain.go index 10ba703364bf7..af1e46a0f0cbf 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -86,7 +86,7 @@ type Domain struct { sysVarCache sysVarCache // replaces GlobalVariableCache slowQuery *topNSlowQueries expensiveQueryHandle *expensivequery.Handle - wg sync.WaitGroup + wg util.WaitGroupWrapper statsUpdating atomicutil.Int32 cancel context.CancelFunc indexUsageSyncLease time.Duration @@ -1298,8 +1298,7 @@ func (do *Domain) UpdateTableStatsLoop(ctx sessionctx.Context) error { do.ddl.RegisterStatsHandle(statsHandle) // Negative stats lease indicates that it is in test, it does not need update. if do.statsLease >= 0 { - do.wg.Add(1) - go do.loadStatsWorker() + do.wg.Run(do.loadStatsWorker) } owner := do.newOwnerManager(handle.StatsPrompt, handle.StatsOwnerKey) if do.indexUsageSyncLease > 0 { @@ -1309,15 +1308,12 @@ func (do *Domain) UpdateTableStatsLoop(ctx sessionctx.Context) error { if do.statsLease <= 0 { return nil } - do.wg.Add(1) do.SetStatsUpdating(true) - go do.updateStatsWorker(ctx, owner) + do.wg.Run(func() { do.updateStatsWorker(ctx, owner) }) if RunAutoAnalyze { - do.wg.Add(1) - go do.autoAnalyzeWorker(owner) + do.wg.Run(func() { do.autoAnalyzeWorker(owner) }) } - do.wg.Add(1) - go do.gcAnalyzeHistory(owner) + do.wg.Run(func() { do.gcAnalyzeHistory(owner) }) return nil } @@ -1356,7 +1352,6 @@ func (do *Domain) loadStatsWorker() { loadTicker := time.NewTicker(lease) defer func() { loadTicker.Stop() - do.wg.Done() logutil.BgLogger().Info("loadStatsWorker exited.") }() statsHandle := do.StatsHandle() @@ -1434,7 +1429,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) gcStatsTicker.Stop() deltaUpdateTicker.Stop() do.SetStatsUpdating(false) - do.wg.Done() logutil.BgLogger().Info("updateStatsWorker exited.") }() for { @@ -1492,7 +1486,6 @@ func (do *Domain) autoAnalyzeWorker(owner owner.Manager) { analyzeTicker := time.NewTicker(do.statsLease) defer func() { analyzeTicker.Stop() - do.wg.Done() logutil.BgLogger().Info("autoAnalyzeWorker exited.") }() for { @@ -1514,7 +1507,6 @@ func (do *Domain) gcAnalyzeHistory(owner owner.Manager) { gcTicker := time.NewTicker(gcInterval) defer func() { gcTicker.Stop() - do.wg.Done() logutil.BgLogger().Info("gcAnalyzeHistory exited.") }() for { diff --git a/statistics/handle/handle_hist.go b/statistics/handle/handle_hist.go index e846c4568a57f..53ae365f8ce8b 100644 --- a/statistics/handle/handle_hist.go +++ b/statistics/handle/handle_hist.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/statistics" + "github.com/pingcap/tidb/util" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/sqlexec" "go.uber.org/zap" @@ -136,7 +137,7 @@ type StatsReaderContext struct { } // SubLoadWorker loads hist data for each column -func (h *Handle) SubLoadWorker(ctx sessionctx.Context, exit chan struct{}, exitWg *sync.WaitGroup) { +func (h *Handle) SubLoadWorker(ctx sessionctx.Context, exit chan struct{}, exitWg *util.WaitGroupWrapper) { readerCtx := &StatsReaderContext{} defer func() { exitWg.Done()