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 (#43381) (#43447)

ref #42160, close #43385
  • Loading branch information
ti-chi-bot authored May 4, 2023
1 parent a646473 commit d207824
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ type Performance struct {
EnableLoadFMSketch bool `toml:"enable-load-fmsketch" json:"enable-load-fmsketch"`

LiteInitStats bool `toml:"lite-init-stats" json:"lite-init-stats"`

// 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 @@ -985,6 +990,7 @@ var defaultConf = Config{
RunAutoAnalyze: true,
EnableLoadFMSketch: false,
LiteInitStats: 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 @@ -282,6 +282,9 @@ gogc = 100
# Whether to use the lite mode of init stats.
lite-init-stats = false

# 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 @@ -742,6 +742,7 @@ tcp-no-delay = false
enable-load-fmsketch = true
plan-replayer-dump-worker-concurrency = 1
lite-init-stats = true
force-init-stats = true
[tikv-client]
commit-timeout="41s"
max-batch-size=128
Expand Down Expand Up @@ -834,6 +835,7 @@ max_connections = 200
require.Equal(t, 40960, conf.Status.GRPCMaxSendMsgSize)
require.True(t, conf.Performance.EnableLoadFMSketch)
require.True(t, conf.Performance.LiteInitStats)
require.True(t, conf.Performance.ForceInitStats)

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

func (do *Domain) loadStatsWorker() {
defer util.Recover(metrics.LabelDomain, "loadStatsWorker", nil, false)
lease := do.statsLease
if lease == 0 {
lease = 3 * time.Second
}
loadTicker := time.NewTicker(lease)
updStatsHealthyTicker := time.NewTicker(20 * lease)
func (do *Domain) initStats() {
statsHandle := do.StatsHandle()
defer func() {
loadTicker.Stop()
updStatsHealthyTicker.Stop()
logutil.BgLogger().Info("loadStatsWorker exited.")
close(statsHandle.InitStatsDone)
}()
statsHandle := do.StatsHandle()
t := time.Now()
liteInitStats := config.GetGlobalConfig().Performance.LiteInitStats
var err error
Expand All @@ -2176,6 +2167,24 @@ func (do *Domain) loadStatsWorker() {
} else {
logutil.BgLogger().Info("init stats info time", zap.Bool("lite", liteInitStats), zap.Duration("take time", time.Since(t)))
}
}

func (do *Domain) loadStatsWorker() {
defer util.Recover(metrics.LabelDomain, "loadStatsWorker", nil, false)
lease := do.statsLease
if lease == 0 {
lease = 3 * time.Second
}
loadTicker := time.NewTicker(lease)
updStatsHealthyTicker := time.NewTicker(20 * lease)
defer func() {
loadTicker.Stop()
updStatsHealthyTicker.Stop()
logutil.BgLogger().Info("loadStatsWorker exited.")
}()
do.initStats()
statsHandle := do.StatsHandle()
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 @@ -483,6 +485,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 @@ -259,6 +259,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 d207824

Please sign in to comment.