Skip to content

Commit

Permalink
executor: add historical stats test and make it enabled (#40664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisaer authored Jan 30, 2023
1 parent 86a6694 commit 91686a3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
19 changes: 18 additions & 1 deletion domain/historical_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ package domain

import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

var (
Expand All @@ -35,7 +38,21 @@ type HistoricalStatsWorker struct {

// SendTblToDumpHistoricalStats send tableID to worker to dump historical stats
func (w *HistoricalStatsWorker) SendTblToDumpHistoricalStats(tableID int64) {
w.tblCH <- tableID
send := enableDumpHistoricalStats.Load()
failpoint.Inject("sendHistoricalStats", func(val failpoint.Value) {
if val.(bool) {
send = true
}
})
if !send {
return
}
select {
case w.tblCH <- tableID:
return
default:
logutil.BgLogger().Warn("discard dump historical stats task", zap.Int64("table-id", tableID))
}
}

// DumpHistoricalStats dump stats by given tableID
Expand Down
59 changes: 59 additions & 0 deletions executor/historical_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/statistics/handle"
Expand All @@ -30,6 +31,8 @@ import (
)

func TestRecordHistoryStatsAfterAnalyze(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer failpoint.Disable("github.com/pingcap/tidb/domain/sendHistoricalStats")
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
Expand Down Expand Up @@ -150,6 +153,8 @@ func TestRecordHistoryStatsMetaAfterAnalyze(t *testing.T) {
}

func TestGCHistoryStatsAfterDropTable(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer 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 All @@ -174,6 +179,7 @@ func TestGCHistoryStatsAfterDropTable(t *testing.T) {
tableInfo.Meta().ID)).Check(testkit.Rows("1"))
// drop the table and gc stats
tk.MustExec("drop table t")
is = dom.InfoSchema()
h.GCStats(is, 0)

// assert stats_history tables delete the record of dropped table
Expand All @@ -183,7 +189,56 @@ func TestGCHistoryStatsAfterDropTable(t *testing.T) {
tableInfo.Meta().ID)).Check(testkit.Rows("0"))
}

func TestAssertHistoricalStatsAfterAlterTable(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer 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")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b varchar(10),c int, KEY `idx` (`c`))")
tk.MustExec("analyze table test.t")
is := dom.InfoSchema()
tableInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
// dump historical stats
h := dom.StatsHandle()
hsWorker := dom.GetHistoricalStatsWorker()
tblID := hsWorker.GetOneHistoricalStatsTable()
err = hsWorker.DumpHistoricalStats(tblID, h)
require.Nil(t, err)

time.Sleep(1 * time.Second)
snapshot := oracle.GoTimeToTS(time.Now())
jsTable, err := h.DumpHistoricalStatsBySnapshot("test", tableInfo.Meta(), snapshot)
require.NoError(t, err)
require.NotNil(t, jsTable)
require.NotEqual(t, jsTable.Version, uint64(0))
originVersion := jsTable.Version

// assert historical stats non-change after drop column
tk.MustExec("alter table t drop column b")
h.GCStats(is, 0)
snapshot = oracle.GoTimeToTS(time.Now())
jsTable, err = h.DumpHistoricalStatsBySnapshot("test", tableInfo.Meta(), snapshot)
require.NoError(t, err)
require.NotNil(t, jsTable)
require.Equal(t, jsTable.Version, originVersion)

// assert historical stats non-change after drop index
tk.MustExec("alter table t drop index idx")
h.GCStats(is, 0)
snapshot = oracle.GoTimeToTS(time.Now())
jsTable, err = h.DumpHistoricalStatsBySnapshot("test", tableInfo.Meta(), snapshot)
require.NoError(t, err)
require.NotNil(t, jsTable)
require.Equal(t, jsTable.Version, originVersion)
}

func TestGCOutdatedHistoryStats(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer 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 Expand Up @@ -219,6 +274,8 @@ func TestGCOutdatedHistoryStats(t *testing.T) {
}

func TestPartitionTableHistoricalStats(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer 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 Expand Up @@ -246,6 +303,8 @@ PARTITION p0 VALUES LESS THAN (6)
}

func TestDumpHistoricalStatsByTable(t *testing.T) {
failpoint.Enable("github.com/pingcap/tidb/domain/sendHistoricalStats", "return(true)")
defer 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
2 changes: 1 addition & 1 deletion executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func TestSetVar(t *testing.T) {
tk.MustQuery("select @@tidb_enable_tso_follower_proxy").Check(testkit.Rows("0"))
require.Error(t, tk.ExecToErr("set tidb_enable_tso_follower_proxy = 1"))

tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("0"))
tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("1"))
tk.MustExec("set global tidb_enable_historical_stats = 1")
tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("1"))
tk.MustExec("set global tidb_enable_historical_stats = 0")
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ var defaultSysVars = []*SysVar{
return nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableTelemetry, Value: BoolToOnOff(DefTiDBEnableTelemetry), Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnableHistoricalStats, Value: Off, Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnableHistoricalStats, Value: On, Type: TypeBool},
/* tikv gc metrics */
{Scope: ScopeGlobal, Name: TiDBGCEnable, Value: On, Type: TypeBool, GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return getTiDBTableValue(s, "tikv_gc_enable", On)
Expand Down
7 changes: 5 additions & 2 deletions statistics/handle/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func (h *Handle) GCStats(is infoschema.InfoSchema, ddlLease time.Duration) error
if err := h.gcTableStats(is, row.GetInt64(0)); err != nil {
return errors.Trace(err)
}
if err := h.gcHistoryStatsFromKV(row.GetInt64(0)); err != nil {
return errors.Trace(err)
_, existed := is.TableByID(row.GetInt64(0))
if !existed {
if err := h.gcHistoryStatsFromKV(row.GetInt64(0)); err != nil {
return errors.Trace(err)
}
}
}
if err := h.ClearOutdatedHistoryStats(); err != nil {
Expand Down

0 comments on commit 91686a3

Please sign in to comment.