Skip to content

Commit

Permalink
statistics: avoid oom when to gc large stats_history (#48430) (#48490)
Browse files Browse the repository at this point in the history
close #48431
  • Loading branch information
ti-chi-bot authored Nov 9, 2023
1 parent 257df3c commit 963a6dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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

0 comments on commit 963a6dc

Please sign in to comment.