Skip to content

Commit

Permalink
*: provide a option to wait for init stats to finish before providing…
Browse files Browse the repository at this point in the history
… service during startup (pingcap#43381) (pingcap#43445)

ref pingcap#42160, close pingcap#43385
  • Loading branch information
ti-chi-bot authored Dec 18, 2023
1 parent 859fade commit 10caef0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,11 @@ type Performance struct {
MemoryUsageAlarmRatio float64 `toml:"memory-usage-alarm-ratio" json:"memory-usage-alarm-ratio"`

EnableLoadFMSketch bool `toml:"enable-load-fmsketch" json:"enable-load-fmsketch"`

// If ForceInitStats is true, when tidb starts up, it doesn't provide service until init stats is finished.
// If ForceInitStats is false, tidb can provide service before init stats is finished. Note that during the period
// of init stats the optimizer may make bad decisions due to pseudo stats.
ForceInitStats bool `toml:"force-init-stats" json:"force-init-stats"`
}

// PlanCache is the PlanCache section of the config.
Expand Down Expand Up @@ -941,6 +946,7 @@ var defaultConf = Config{
EnableStatsCacheMemQuota: false,
RunAutoAnalyze: true,
EnableLoadFMSketch: false,
ForceInitStats: false,
},
ProxyProtocol: ProxyProtocol{
Networks: "",
Expand Down
3 changes: 3 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ max-txn-ttl = 3600000
# If you find the CPU used by GC is too high or GC is too frequent and impact your business you can increase this value.
gogc = 100

# Whether to wait for init stats to finish before providing service during startup
force-init-stats = false

[proxy-protocol]
# PROXY protocol acceptable client networks.
# Empty string means disable PROXY protocol, * means all networks.
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ tidb-max-reuse-column = 20
txn-total-size-limit=2000
tcp-no-delay = false
enable-load-fmsketch = true
force-init-stats = true
[tikv-client]
commit-timeout="41s"
max-batch-size=128
Expand Down Expand Up @@ -827,6 +828,7 @@ max_connections = 200
require.Equal(t, 10240, conf.Status.GRPCInitialWindowSize)
require.Equal(t, 40960, conf.Status.GRPCMaxSendMsgSize)
require.True(t, conf.Performance.EnableLoadFMSketch)
require.True(t, conf.Performance.ForceInitStats)

err = f.Truncate(0)
require.NoError(t, err)
Expand Down
23 changes: 16 additions & 7 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,20 @@ func (do *Domain) newOwnerManager(prompt, ownerKey string) owner.Manager {
return statsOwner
}

func (do *Domain) initStats() {
statsHandle := do.StatsHandle()
defer func() {
close(statsHandle.InitStatsDone)
}()
t := time.Now()
err := statsHandle.InitStats(do.InfoSchema())
if err != nil {
logutil.BgLogger().Error("init stats info failed", zap.Duration("take time", time.Since(t)), zap.Error(err))
} else {
logutil.BgLogger().Info("init stats info time", zap.Duration("take time", time.Since(t)))
}
}

func (do *Domain) loadStatsWorker() {
defer util.Recover(metrics.LabelDomain, "loadStatsWorker", nil, false)
lease := do.statsLease
Expand All @@ -1938,14 +1952,9 @@ func (do *Domain) loadStatsWorker() {
loadTicker.Stop()
logutil.BgLogger().Info("loadStatsWorker exited.")
}()
do.initStats()
statsHandle := do.StatsHandle()
t := time.Now()
err := statsHandle.InitStats(do.InfoSchema())
if err != nil {
logutil.BgLogger().Error("init stats info failed", zap.Duration("take time", time.Since(t)), zap.Error(err))
} else {
logutil.BgLogger().Info("init stats info time", zap.Duration("take time", time.Since(t)))
}
var err error
for {
select {
case <-loadTicker.C:
Expand Down
3 changes: 3 additions & 0 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type Handle struct {
serverIDGetter func() uint64
// tableLocked used to store locked tables
tableLocked []int64

InitStatsDone chan struct{}
}

// GetTableLockedAndClearForTest for unit test only
Expand Down Expand Up @@ -476,6 +478,7 @@ func NewHandle(ctx, initStatsCtx sessionctx.Context, lease time.Duration, pool s
pool: pool,
sysProcTracker: tracker,
serverIDGetter: serverIDGetter,
InitStatsDone: make(chan struct{}),
}
handle.initStatsCtx = initStatsCtx
handle.lease.Store(lease)
Expand Down
3 changes: 3 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ func main() {
close(exited)
})
topsql.SetupTopSQL()
if config.GetGlobalConfig().Performance.ForceInitStats {
<-dom.StatsHandle().InitStatsDone
}
terror.MustNil(svr.Run())
<-exited
syncLog()
Expand Down

0 comments on commit 10caef0

Please sign in to comment.