From 0f584f05cea5d854eaa3c87ad6a50c6105cb11e7 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 15 Apr 2024 11:06:05 +0800 Subject: [PATCH] domain: do not start Auto Analyze Worker until statistics initialization is complete (#52407) (#52512) close pingcap/tidb#52346 --- pkg/domain/domain.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/domain/domain.go b/pkg/domain/domain.go index 7932328491044..8d8abec3ad388 100644 --- a/pkg/domain/domain.go +++ b/pkg/domain/domain.go @@ -2271,9 +2271,26 @@ func (do *Domain) UpdateTableStatsLoop(ctx, initStatsCtx sessionctx.Context) err return nil } do.SetStatsUpdating(true) + // The stats updated worker doesn't require the stats initialization to be completed. + // This is because the updated worker's primary responsibilities are to update the change delta and handle DDL operations. + // These tasks do not interfere with or depend on the initialization process. do.wg.Run(func() { do.updateStatsWorker(ctx, owner) }, "updateStatsWorker") - do.wg.Run(func() { do.autoAnalyzeWorker(owner) }, "autoAnalyzeWorker") - do.wg.Run(func() { do.gcAnalyzeHistory(owner) }, "gcAnalyzeHistory") + // Wait for the stats worker to finish the initialization. + // Otherwise, we may start the auto analyze worker before the stats cache is initialized. + do.wg.Run( + func() { + <-do.StatsHandle().InitStatsDone + do.autoAnalyzeWorker(owner) + }, + "autoAnalyzeWorker", + ) + do.wg.Run( + func() { + <-do.StatsHandle().InitStatsDone + do.gcAnalyzeHistory(owner) + }, + "gcAnalyzeHistory", + ) return nil }