Skip to content

Commit

Permalink
statistics: avoid oom when to gc large stats_history (#48430) (#48489)
Browse files Browse the repository at this point in the history
close #48431
  • Loading branch information
ti-chi-bot authored Nov 10, 2023
1 parent 52c6053 commit ac6bc7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions executor/historical_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ func TestAssertHistoricalStatsAfterAlterTable(t *testing.T) {
}

func TestGCOutdatedHistoryStats(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer failpoint.Disable("github.com/pingcap/tidb/domain/sendHistoricalStats")
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/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 statistics/handle/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ func (h *Handle) gcTableStats(is infoschema.InfoSchema, physicalID int64) error
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 (h *Handle) ClearOutdatedHistoryStats() error {
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
Expand All @@ -172,15 +180,19 @@ func (h *Handle) ClearOutdatedHistoryStats() 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 = exec.ExecuteInternal(ctx, 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 = exec.ExecuteInternal(ctx, 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 = exec.ExecuteInternal(ctx, 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 = exec.ExecuteInternal(ctx, sql, variable.HistoricalStatsDuration.Load().Seconds())
logutil.BgLogger().Info("clear outdated historical stats")
return err
}
return nil
}
Expand Down

0 comments on commit ac6bc7a

Please sign in to comment.