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

executor: add historical stats test and make it enabled #40664

Merged
merged 55 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
6c5abe1
add test
Yisaer Jan 17, 2023
cd6e3c5
Merge branch 'master' into add_historical_stats_test_2
Yisaer Jan 17, 2023
61419d3
fix
Yisaer Jan 17, 2023
7c92b72
Merge branch 'add_historical_stats_test_2' of https://github.com/Yisa…
Yisaer Jan 17, 2023
41dd041
Merge remote-tracking branch 'upstream/master' into add_historical_st…
Yisaer Jan 18, 2023
804fcdd
Merge branch 'master' into add_historical_stats_test_2
Yisaer Jan 19, 2023
f3a19bb
Merge branch 'master' into add_historical_stats_test_2
hawkingrei Jan 19, 2023
666ef41
Merge branch 'master' into add_historical_stats_test_2
hawkingrei Jan 22, 2023
435ad6f
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
f17fbff
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
61391df
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
a6e3e65
Merge branch 'add_historical_stats_test_2' of https://github.com/Yisa…
Yisaer Jan 28, 2023
72ed604
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
98624c1
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
8db6a1e
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
bf6ab7f
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
348795c
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
067bba4
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
24b99d0
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
9116b9f
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 28, 2023
3cae04a
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
17de54e
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
c8fef37
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
ffeceaa
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
9b6d551
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
7111073
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
0743fc8
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
a658ef1
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
4fbba77
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
865b72c
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
b0ca849
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
f81b886
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
8e8cfc9
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
12c89fc
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
4de138f
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
0ebf58a
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
c0e6038
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 29, 2023
38d4e36
Merge branch 'add_historical_stats_test_2' of https://github.com/Yisa…
Yisaer Jan 29, 2023
a3c6e94
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
f26c373
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
c99d7a1
Merge branch 'master' into add_historical_stats_test_2
Yisaer Jan 30, 2023
775f39a
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
9c202af
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
ca4e4d5
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
4864080
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
3665657
Merge branch 'add_historical_stats_test_2' of https://github.com/Yisa…
Yisaer Jan 30, 2023
4345a19
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
3da9bf2
fix 40843
Yisaer Jan 30, 2023
1556b6f
:Merge branch 'add_historical_stats_test_2' of https://github.com/Yis…
Yisaer Jan 30, 2023
e029a5f
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
708ac3a
fix
Yisaer Jan 30, 2023
dadfffd
Merge branch 'add_historical_stats_test_2' of https://github.com/Yisa…
Yisaer Jan 30, 2023
3155f4f
fix
Yisaer Jan 30, 2023
bb9f77b
Merge branch 'master' into add_historical_stats_test_2
ti-chi-bot Jan 30, 2023
bb18116
fix
Yisaer Jan 30, 2023
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
46 changes: 46 additions & 0 deletions executor/historical_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,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,6 +184,51 @@ func TestGCHistoryStatsAfterDropTable(t *testing.T) {
tableInfo.Meta().ID)).Check(testkit.Rows("0"))
}

func TestAssertHistoricalStatsAfterAlterTable(t *testing.T) {
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) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
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