Skip to content

Commit

Permalink
stats: fix unstable test (pingcap#10953)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored Jun 27, 2019
1 parent 98274cc commit 0d563f1
Show file tree
Hide file tree
Showing 33 changed files with 159 additions and 147 deletions.
2 changes: 1 addition & 1 deletion bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *testSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func setupIntegrationSuite(s *testIntegrationSuite, c *C) {
)
c.Assert(err, IsNil)
session.SetSchemaLease(s.lease)
session.SetStatsLease(0)
session.DisableStats4Test()
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)

Expand Down
2 changes: 1 addition & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func setUpSuite(s *testDBSuite, c *C) {

s.lease = 100 * time.Millisecond
session.SetSchemaLease(s.lease)
session.SetStatsLease(0)
session.DisableStats4Test()
s.schemaName = "test_db"
s.autoIDStep = autoid.GetStep()
ddl.WaitTimeWhenErrorOccured = 0
Expand Down
2 changes: 1 addition & 1 deletion ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type testSerialSuite struct {

func (s *testSerialSuite) SetUpSuite(c *C) {
session.SetSchemaLease(200 * time.Millisecond)
session.SetStatsLease(0)
session.DisableStats4Test()

ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond
var err error
Expand Down
2 changes: 1 addition & 1 deletion ddl/table_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *testDDLTableSplitSuite) TestTableSplit(c *C) {
c.Assert(err, IsNil)
defer store.Close()
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 1)
dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down
7 changes: 5 additions & 2 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,11 @@ func (do *Domain) UpdateTableStatsLoop(ctx sessionctx.Context) error {
statsHandle := handle.NewHandle(ctx, do.statsLease)
atomic.StorePointer(&do.statsHandle, unsafe.Pointer(statsHandle))
do.ddl.RegisterEventCh(statsHandle.DDLEventCh())
do.wg.Add(1)
go do.loadStatsWorker()
// Negative stats lease indicates that it is in test, it does not need update.
if do.statsLease >= 0 {
do.wg.Add(1)
go do.loadStatsWorker()
}
if do.statsLease <= 0 {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *testSuite1) TestAnalyzeFastSample(c *C) {
)
c.Assert(err, IsNil)
var dom *domain.Domain
session.SetStatsLease(0)
session.DisableStats4Test()
session.SetSchemaLease(0)
dom, err = session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *testSuite1) TestFastAnalyze(c *C) {
)
c.Assert(err, IsNil)
var dom *domain.Domain
session.SetStatsLease(0)
session.DisableStats4Test()
session.SetSchemaLease(0)
dom, err = session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down
8 changes: 4 additions & 4 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (s *testSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -3727,7 +3727,7 @@ func (s *testSuite2) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -3778,7 +3778,7 @@ func (s *testSuite3) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -3829,7 +3829,7 @@ func (s *testSuite4) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion executor/seqtest/seq_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *seqTestSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down
4 changes: 2 additions & 2 deletions executor/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (s *testSuite3) TestDropStats(c *C) {
c.Assert(statsTbl.Pseudo, IsFalse)

testKit.MustExec("drop stats t")
h.Update(is)
c.Assert(h.Update(is), IsNil)
statsTbl = h.GetTableStats(tableInfo)
c.Assert(statsTbl.Pseudo, IsTrue)

Expand All @@ -422,7 +422,7 @@ func (s *testSuite3) TestDropStats(c *C) {

h.SetLease(1)
testKit.MustExec("drop stats t")
h.Update(is)
c.Assert(h.Update(is), IsNil)
statsTbl = h.GetTableStats(tableInfo)
c.Assert(statsTbl.Pseudo, IsTrue)
h.SetLease(0)
Expand Down
2 changes: 1 addition & 1 deletion executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *testUpdateSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
}
d, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
Expand Down
18 changes: 9 additions & 9 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *testTableSuite) SetUpSuite(c *C) {
var err error
s.store, err = mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
session.SetStatsLease(0)
session.DisableStats4Test()
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -140,23 +140,23 @@ func (s *testTableSuite) TestDataForTableStatsField(c *C) {
tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check(
testkit.Rows("0 0 0 0"))
tk.MustExec(`insert into t(c, d, e) values(1, 2, "c"), (2, 3, "d"), (3, 4, "e")`)
h.DumpStatsDeltaToKV(handle.DumpAll)
h.Update(is)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check(
testkit.Rows("3 17 51 3"))
tk.MustExec(`insert into t(c, d, e) values(4, 5, "f")`)
h.DumpStatsDeltaToKV(handle.DumpAll)
h.Update(is)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check(
testkit.Rows("4 17 68 4"))
tk.MustExec("delete from t where c >= 3")
h.DumpStatsDeltaToKV(handle.DumpAll)
h.Update(is)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check(
testkit.Rows("2 17 34 2"))
tk.MustExec("delete from t where c=3")
h.DumpStatsDeltaToKV(handle.DumpAll)
h.Update(is)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(is), IsNil)
tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check(
testkit.Rows("2 17 34 2"))
}
Expand Down
14 changes: 7 additions & 7 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *testAnalyzeSuite) TestCBOWithoutAnalyze(c *C) {
c.Assert(h.HandleDDLEvent(<-h.DDLEventCh()), IsNil)
testKit.MustExec("insert into t1 values (1), (2), (3), (4), (5), (6)")
testKit.MustExec("insert into t2 values (1), (2), (3), (4), (5), (6)")
h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(dom.InfoSchema()), IsNil)
testKit.MustQuery("explain select * from t1, t2 where t1.a = t2.a").Check(testkit.Rows(
"HashLeftJoin_8 7.49 root inner join, inner:TableReader_15, equal:[eq(test.t1.a, test.t2.a)]",
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *testAnalyzeSuite) TestTableDual(c *C) {
testKit.MustExec("insert into t values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)")
c.Assert(h.HandleDDLEvent(<-h.DDLEventCh()), IsNil)

h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(dom.InfoSchema()), IsNil)

testKit.MustQuery(`explain select * from t where 1 = 0`).Check(testkit.Rows(
Expand Down Expand Up @@ -226,12 +226,12 @@ func (s *testAnalyzeSuite) TestEstimation(c *C) {
testKit.MustExec("insert into t select * from t")
h := dom.StatsHandle()
h.HandleDDLEvent(<-h.DDLEventCh())
h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
testKit.MustExec("analyze table t")
for i := 1; i <= 8; i++ {
testKit.MustExec("delete from t where a = ?", i)
}
h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(dom.InfoSchema()), IsNil)
testKit.MustQuery("explain select count(*) from t group by a").Check(testkit.Rows(
"HashAgg_9 2.00 root group by:col_1, funcs:count(col_0)",
Expand Down Expand Up @@ -568,12 +568,12 @@ func (s *testAnalyzeSuite) TestOutdatedAnalyze(c *C) {
}
h := dom.StatsHandle()
h.HandleDDLEvent(<-h.DDLEventCh())
h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
testKit.MustExec("analyze table t")
testKit.MustExec("insert into t select * from t")
testKit.MustExec("insert into t select * from t")
testKit.MustExec("insert into t select * from t")
h.DumpStatsDeltaToKV(handle.DumpAll)
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(dom.InfoSchema()), IsNil)
statistics.RatioOfPseudoEstimate.Store(10.0)
testKit.MustQuery("explain select * from t where a <= 5 and b <= 5").Check(testkit.Rows(
Expand Down Expand Up @@ -753,7 +753,7 @@ func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
}

session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()

dom, err := session.BootstrapSession(store)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type testCacheSuite struct {
func (s *testCacheSuite) SetUpSuite(c *C) {
store, err := mockstore.NewMockTikvStore()
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
c.Assert(err, IsNil)
s.domain, err = session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -276,7 +276,7 @@ func (s *testCacheSuite) TestAbnormalMySQLTable(c *C) {
c.Assert(err, IsNil)
defer store.Close()
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()

dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func mustExec(c *C, se session.Session, sql string) {
func newStore(c *C, dbPath string) (*domain.Domain, kv.Storage) {
store, err := mockstore.NewMockTikvStore()
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
c.Assert(err, IsNil)
dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion server/statistics_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (ds *testDumpStatsSuite) startServer(c *C) {
var err error
ds.store, err = mockstore.NewMockTikvStore(mockstore.WithMVCCStore(mvccStore))
c.Assert(err, IsNil)
session.SetStatsLease(0)
session.DisableStats4Test()
ds.domain, err = session.BootstrapSession(ds.store)
c.Assert(err, IsNil)
ds.domain.SetStatsUpdating(true)
Expand Down
2 changes: 1 addition & 1 deletion server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (ts *TidbTestSuite) SetUpSuite(c *C) {
metrics.RegisterMetrics()
var err error
ts.store, err = mockstore.NewMockTikvStore()
session.SetStatsLease(0)
session.DisableStats4Test()
c.Assert(err, IsNil)
ts.domain, err = session.BootstrapSession(ts.store)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion session/isolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *testIsolationSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)

Expand Down
2 changes: 1 addition & 1 deletion session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *testPessimisticSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
}
Expand Down
4 changes: 2 additions & 2 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *testSessionSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.SetStatsLease(0)
session.DisableStats4Test()
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -1703,7 +1703,7 @@ func (s *testSchemaSuite) SetUpSuite(c *C) {
s.store = store
s.lease = 20 * time.Millisecond
session.SetSchemaLease(s.lease)
session.SetStatsLease(0)
session.DisableStats4Test()
dom, err := session.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.dom = dom
Expand Down
5 changes: 5 additions & 0 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func SetStatsLease(lease time.Duration) {
statsLease = lease
}

// DisableStats4Test disables the stats for tests.
func DisableStats4Test() {
statsLease = -1
}

// Parse parses a query string to raw ast.StmtNode.
func Parse(ctx sessionctx.Context, src string) ([]ast.StmtNode, error) {
logutil.BgLogger().Debug("compiling", zap.String("source", src))
Expand Down
2 changes: 1 addition & 1 deletion statistics/cmsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (s *testStatisticsSuite) TestMergeCMSketch4IncrementalAnalyze(c *C) {
for key, val := range rMap {
lMap[key] += val
}
lSketch.MergeCMSketch4IncrementalAnalyze(rSketch)
c.Assert(lSketch.MergeCMSketch4IncrementalAnalyze(rSketch), IsNil)
avg, err = averageAbsoluteError(lSketch, lMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
Expand Down
Loading

0 comments on commit 0d563f1

Please sign in to comment.