Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl, statistics: fix stats meta missing when creating many tables at once #38301

Merged
merged 15 commits into from
Feb 2, 2023
Merged
1 change: 1 addition & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func (d *ddl) RegisterStatsHandle(h *handle.Handle) {

// asyncNotifyEvent will notify the ddl event to outside world, say statistic handle. When the channel is full, we may
// give up notify and log it.
// TODO: 1. real async; 2. exponential backoff.
func asyncNotifyEvent(d *ddlCtx, e *util.Event) {
if d.ddlEventCh != nil {
if d.lease == 0 {
Expand Down
2 changes: 1 addition & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ type sessionPool interface {
func NewHandle(ctx sessionctx.Context, lease time.Duration, pool sessionPool, tracker sessionctx.SysProcTracker, serverIDGetter func() uint64) (*Handle, error) {
cfg := config.GetGlobalConfig()
handle := &Handle{
ddlEventCh: make(chan *util.Event, 100),
ddlEventCh: make(chan *util.Event, 1000),
listHead: &SessionStatsCollector{mapper: make(tableDeltaMap), rateMap: make(errorRateDeltaMap)},
idxUsageListHead: &SessionIndexUsageCollector{mapper: make(indexUsageMap)},
pool: pool,
Expand Down
7 changes: 5 additions & 2 deletions statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ func needDumpStatsDelta(h *Handle, id int64, item variable.TableDelta, currentTi
}
tbl, ok := h.statsCache.Load().(statsCache).Get(id)
if !ok {
// TODO: do we need to return true here?
// No need to dump if the stats is invalid.
return false
}
Expand Down Expand Up @@ -540,9 +541,11 @@ func (h *Handle) dumpTableStatCountToKV(id int64, delta variable.TableDelta) (up
updateStatsMeta := func(id int64) error {
var err error
if delta.Delta < 0 {
_, err = exec.ExecuteInternal(ctx, "update mysql.stats_meta set version = %?, count = count - %?, modify_count = modify_count + %? where table_id = %? and count >= %?", startTS, -delta.Delta, delta.Count, id, -delta.Delta)
_, err = exec.ExecuteInternal(ctx, "insert into mysql.stats_meta (version, table_id, modify_count, count) values (%?, %?, %?, 0) on duplicate key "+
"update modify_count = modify_count + values(modify_count), count = greatest(count - %?, 0)", startTS, id, delta.Count, -delta.Delta)
} else {
_, err = exec.ExecuteInternal(ctx, "update mysql.stats_meta set version = %?, count = count + %?, modify_count = modify_count + %? where table_id = %?", startTS, delta.Delta, delta.Count, id)
_, err = exec.ExecuteInternal(ctx, "insert into mysql.stats_meta (version, table_id, modify_count, count) values (%?, %?, %?, %?) on duplicate key "+
"update modify_count = modify_count + values(modify_count), count = count + values(count)", startTS, id, delta.Count, delta.Delta)
}
statsVer = startTS
return errors.Trace(err)
Expand Down