Skip to content

Commit

Permalink
executor: fix fast analyze test (pingcap#13275)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored and jackysp committed Nov 8, 2019
1 parent ccbdfdc commit ab1c939
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions executor/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,16 @@ type testFastAnalyze struct {
store kv.Storage
dom *domain.Domain
cluster *mocktikv.Cluster
cli *regionProperityClient
}

func (s *testFastAnalyze) SetUpSuite(c *C) {
cli := &regionProperityClient{}
hijackClient := func(c tikv.Client) tikv.Client {
cli.Client = c
return cli
}
s.cli = cli

var err error
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.store, err = mockstore.NewMockTikvStore(
mockstore.WithHijackClient(hijackClient),
mockstore.WithCluster(s.cluster),
)
s.store, err = mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
session.DisableStats4Test()
session.SetSchemaLease(0)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
}
Expand All @@ -426,8 +417,8 @@ type regionProperityClient struct {
tikv.Client
mu struct {
sync.Mutex
regionID uint64
count int64
failedOnce bool
count int64
}
}

Expand All @@ -437,41 +428,52 @@ func (c *regionProperityClient) SendRequest(ctx context.Context, addr string, re
defer c.mu.Unlock()
c.mu.count++
// Mock failure once.
if req.DebugGetRegionProperties().RegionId == c.mu.regionID {
c.mu.regionID = 0
if !c.mu.failedOnce {
c.mu.failedOnce = true
return &tikvrpc.Response{}, nil
}
}
return c.Client.SendRequest(ctx, addr, req, timeout)
}

func (c *regionProperityClient) setFailRegion(regionID uint64) {
c.mu.Lock()
c.mu.regionID = regionID
c.mu.Unlock()
}

func (s *testFastAnalyze) TestFastAnalyzeRetryRowCount(c *C) {
tk := testkit.NewTestKit(c, s.store)
cli := &regionProperityClient{}
hijackClient := func(c tikv.Client) tikv.Client {
cli.Client = c
return cli
}

cluster := mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(cluster)
mvccStore := mocktikv.MustNewMVCCStore()
store, err := mockstore.NewMockTikvStore(
mockstore.WithHijackClient(hijackClient),
mockstore.WithCluster(cluster),
mockstore.WithMVCCStore(mvccStore),
)
c.Assert(err, IsNil)
dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)

tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists retry_row_count")
tk.MustExec("create table retry_row_count(a int primary key)")
c.Assert(s.dom.StatsHandle().Update(s.dom.InfoSchema()), IsNil)
tk.MustExec("set @@session.tidb_enable_fast_analyze=1")
tk.MustExec("set @@session.tidb_build_stats_concurrency=1")
tblInfo, err := s.dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("retry_row_count"))
tblInfo, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("retry_row_count"))
c.Assert(err, IsNil)
tid := tblInfo.Meta().ID
// construct 6 regions split by {6, 12, 18, 24, 30}
splitKeys := generateTableSplitKeyForInt(tid, []int{6, 12, 18, 24, 30})
regionIDs := manipulateCluster(s.cluster, splitKeys)
c.Assert(dom.StatsHandle().Update(dom.InfoSchema()), IsNil)
tk.MustExec("set @@session.tidb_enable_fast_analyze=1")
tk.MustExec("set @@session.tidb_build_stats_concurrency=1")
for i := 0; i < 30; i++ {
tk.MustExec(fmt.Sprintf("insert into retry_row_count values (%d)", i))
}
s.cli.setFailRegion(regionIDs[4])
cluster.SplitTable(mvccStore, tid, 6)
// Flush the region cache first.
tk.MustQuery("select * from retry_row_count")
tk.MustExec("analyze table retry_row_count")
// 4 regions will be sampled, and it will retry the last failed region.
c.Assert(s.cli.mu.count, Equals, int64(5))
c.Assert(cli.mu.count, Equals, int64(5))
row := tk.MustQuery(`show stats_meta where db_name = "test" and table_name = "retry_row_count"`).Rows()[0]
c.Assert(row[5], Equals, "30")
}
Expand Down

0 comments on commit ab1c939

Please sign in to comment.