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

statistics: avoid oom when to gc large stats_history (#48430) #48490

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/executor/historical_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ func TestAssertHistoricalStatsAfterAlterTable(t *testing.T) {
}

func TestGCOutdatedHistoryStats(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/pkg/domain/sendHistoricalStats", "return(true)")
defer failpoint.Disable("github.com/pingcap/tidb/pkg/domain/sendHistoricalStats")
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/domain/sendHistoricalStats", "return(true)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/domain/sendHistoricalStats"))
}()
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set global tidb_enable_historical_stats = 1")
Expand Down
24 changes: 18 additions & 6 deletions pkg/statistics/handle/storage/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ func DeleteTableStatsFromKV(sctx sessionctx.Context, statsIDs []int64) (err erro
return nil
}

func forCount(total int64, batch int64) int64 {
result := total / batch
if total%batch > 0 {
result++
}
return result
}

// ClearOutdatedHistoryStats clear outdated historical stats
func ClearOutdatedHistoryStats(sctx sessionctx.Context) error {
sql := "select count(*) from mysql.stats_meta_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND"
Expand All @@ -182,15 +190,19 @@ func ClearOutdatedHistoryStats(sctx sessionctx.Context) error {
}
count := rows[0].GetInt64(0)
if count > 0 {
sql = "delete from mysql.stats_meta_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND"
_, err = util.Exec(sctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
if err != nil {
for n := int64(0); n < forCount(count, int64(1000)); n++ {
sql = "delete from mysql.stats_meta_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND limit 1000"
_, err = util.Exec(sctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
if err != nil {
return err
}
}
for n := int64(0); n < forCount(count, int64(50)); n++ {
sql = "delete from mysql.stats_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND limit 50"
_, err = util.Exec(sctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
return err
}
sql = "delete from mysql.stats_history use index (idx_create_time) where create_time <= NOW() - INTERVAL %? SECOND"
_, err = util.Exec(sctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
logutil.BgLogger().Info("clear outdated historical stats")
return err
}
return nil
}
Expand Down