Skip to content

Commit

Permalink
domain: move async load stats into single goroutine (#58302)
Browse files Browse the repository at this point in the history
close #58303
  • Loading branch information
hawkingrei authored and Rustin170506 committed Jan 17, 2025
1 parent ffe10c7 commit 66e0bb5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,7 @@ func (do *Domain) UpdateTableStatsLoop(ctx, initStatsCtx sessionctx.Context) err
return nil
}
do.SetStatsUpdating(true)
do.wg.Run(do.asyncLoadHistogram, "asyncLoadHistogram")
// 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.
Expand Down Expand Up @@ -2522,6 +2523,33 @@ func (do *Domain) loadStatsWorker() {
if err != nil {
logutil.BgLogger().Warn("update stats info failed", zap.Error(err))
}
case <-do.exit:
return
}
}
}

func (do *Domain) asyncLoadHistogram() {
defer util.Recover(metrics.LabelDomain, "asyncLoadStats", nil, false)
lease := do.statsLease
if lease == 0 {
lease = 3 * time.Second
}
cleanupTicker := time.NewTicker(lease)
defer func() {
cleanupTicker.Stop()
logutil.BgLogger().Info("asyncLoadStats exited.")
}()
select {
case <-do.StatsHandle().InitStatsDone:
case <-do.exit: // It may happen that before initStatsDone, tidb receive Ctrl+C
return
}
statsHandle := do.StatsHandle()
var err error
for {
select {
case <-cleanupTicker.C:
err = statsHandle.LoadNeededHistograms(do.InfoSchema())
if err != nil {
logutil.BgLogger().Warn("load histograms failed", zap.Error(err))
Expand Down

0 comments on commit 66e0bb5

Please sign in to comment.