From a78b54e4a73264c46854269bcc9f735545b6bcd6 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 5 Jun 2024 12:46:25 +0800 Subject: [PATCH] statistics: upgrade stats timeout checkpoint after it timeouts (#52424) (#52438) close pingcap/tidb#52425 --- planner/core/plan_stats.go | 2 +- sessionctx/variable/session.go | 4 ++-- sessionctx/variable/sysvar.go | 2 +- statistics/handle/handle_hist.go | 2 ++ 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/planner/core/plan_stats.go b/planner/core/plan_stats.go index eaf3cf3120a0f..d5bb4929c6cd7 100644 --- a/planner/core/plan_stats.go +++ b/planner/core/plan_stats.go @@ -36,7 +36,7 @@ func (collectPredicateColumnsPoint) optimize(_ context.Context, plan LogicalPlan return plan, nil } predicateNeeded := variable.EnableColumnTracking.Load() - syncWait := plan.SCtx().GetSessionVars().StatsLoadSyncWait * time.Millisecond.Nanoseconds() + syncWait := plan.SCtx().GetSessionVars().StatsLoadSyncWait.Load() * time.Millisecond.Nanoseconds() histNeeded := syncWait > 0 predicateColumns, histNeededColumns := CollectColumnStatsUsage(plan, predicateNeeded, histNeeded) if len(predicateColumns) > 0 { diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index cc820f2b34d03..6b35461c83c7e 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -1196,7 +1196,7 @@ type SessionVars struct { ReadConsistency ReadConsistencyLevel // StatsLoadSyncWait indicates how long to wait for stats load before timeout. - StatsLoadSyncWait int64 + StatsLoadSyncWait atomic.Int64 // SysdateIsNow indicates whether Sysdate is an alias of Now function SysdateIsNow bool @@ -1710,7 +1710,6 @@ func NewSessionVars(hctx HookContext) *SessionVars { TMPTableSize: DefTiDBTmpTableMaxSize, MPPStoreFailTTL: DefTiDBMPPStoreFailTTL, Rng: mathutil.NewWithTime(), - StatsLoadSyncWait: StatsLoadSyncWait.Load(), EnableLegacyInstanceScope: DefEnableLegacyInstanceScope, RemoveOrderbyInSubquery: DefTiDBRemoveOrderbyInSubquery, EnableSkewDistinctAgg: DefTiDBSkewDistinctAgg, @@ -1762,6 +1761,7 @@ func NewSessionVars(hctx HookContext) *SessionVars { vars.DiskTracker = disk.NewTracker(memory.LabelForSession, -1) vars.MemTracker = memory.NewTracker(memory.LabelForSession, vars.MemQuotaQuery) vars.MemTracker.IsRootTrackerOfSess = true + vars.StatsLoadSyncWait.Store(StatsLoadSyncWait.Load()) for _, engine := range config.GetGlobalConfig().IsolationRead.Engines { switch engine { diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index f54fc0b8f2ba4..f9f0edf211ef4 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1941,7 +1941,7 @@ var defaultSysVars = []*SysVar{ }}, {Scope: ScopeGlobal | ScopeSession, Name: TiDBStatsLoadSyncWait, Value: strconv.Itoa(DefTiDBStatsLoadSyncWait), Type: TypeInt, MinValue: 0, MaxValue: math.MaxInt32, SetSession: func(s *SessionVars, val string) error { - s.StatsLoadSyncWait = TidbOptInt64(val, DefTiDBStatsLoadSyncWait) + s.StatsLoadSyncWait.Store(TidbOptInt64(val, DefTiDBStatsLoadSyncWait)) return nil }, GetGlobal: func(_ context.Context, s *SessionVars) (string, error) { diff --git a/statistics/handle/handle_hist.go b/statistics/handle/handle_hist.go index f33d0c6e7cd41..a06363466acbd 100644 --- a/statistics/handle/handle_hist.go +++ b/statistics/handle/handle_hist.go @@ -259,6 +259,7 @@ func (h *Handle) HandleOneTask(lastTask *NeededItemTask, readerCtx *StatsReaderC } return task, sr.Err case <-time.After(timeout): + task.ToTimeout.Add(time.Duration(h.mu.ctx.GetSessionVars().StatsLoadSyncWait.Load()) * time.Microsecond) return task, nil } } @@ -445,6 +446,7 @@ func (h *Handle) drainColTask(exit chan struct{}) (*NeededItemTask, error) { // if the task has already timeout, no sql is sync-waiting for it, // so do not handle it just now, put it to another channel with lower priority if time.Now().After(task.ToTimeout) { + task.ToTimeout.Add(time.Duration(h.mu.ctx.GetSessionVars().StatsLoadSyncWait.Load()) * time.Microsecond) h.writeToTimeoutChan(h.StatsLoad.TimeoutItemsCh, task) continue }