Skip to content

Commit 1f0d8ff

Browse files
authored
domain: move async load stats into single goroutine (pingcap#58302) (pingcap#59142)
close pingcap#58303
1 parent d299c4f commit 1f0d8ff

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pkg/domain/domain.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,7 @@ func (do *Domain) UpdateTableStatsLoop(ctx, initStatsCtx sessionctx.Context) err
22882288
return nil
22892289
}
22902290
do.SetStatsUpdating(true)
2291+
do.wg.Run(do.asyncLoadHistogram, "asyncLoadHistogram")
22912292
// The stats updated worker doesn't require the stats initialization to be completed.
22922293
// This is because the updated worker's primary responsibilities are to update the change delta and handle DDL operations.
22932294
// These tasks do not interfere with or depend on the initialization process.
@@ -2409,6 +2410,33 @@ func (do *Domain) loadStatsWorker() {
24092410
if err != nil {
24102411
logutil.BgLogger().Warn("update stats info failed", zap.Error(err))
24112412
}
2413+
case <-do.exit:
2414+
return
2415+
}
2416+
}
2417+
}
2418+
2419+
func (do *Domain) asyncLoadHistogram() {
2420+
defer util.Recover(metrics.LabelDomain, "asyncLoadStats", nil, false)
2421+
lease := do.statsLease
2422+
if lease == 0 {
2423+
lease = 3 * time.Second
2424+
}
2425+
cleanupTicker := time.NewTicker(lease)
2426+
defer func() {
2427+
cleanupTicker.Stop()
2428+
logutil.BgLogger().Info("asyncLoadStats exited.")
2429+
}()
2430+
select {
2431+
case <-do.StatsHandle().InitStatsDone:
2432+
case <-do.exit: // It may happen that before initStatsDone, tidb receive Ctrl+C
2433+
return
2434+
}
2435+
statsHandle := do.StatsHandle()
2436+
var err error
2437+
for {
2438+
select {
2439+
case <-cleanupTicker.C:
24122440
err = statsHandle.LoadNeededHistograms()
24132441
if err != nil {
24142442
logutil.BgLogger().Warn("load histograms failed", zap.Error(err))

0 commit comments

Comments
 (0)