github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/statistics/handle/dump_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package handle_test 15 16 import ( 17 "fmt" 18 "sync" 19 20 . "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 22 "github.com/whtcorpsinc/milevadb/statistics" 23 "github.com/whtcorpsinc/milevadb/statistics/handle" 24 "github.com/whtcorpsinc/milevadb/soliton/testkit" 25 ) 26 27 func (s *testStatsSuite) TestConversion(c *C) { 28 defer cleanEnv(c, s.causetstore, s.do) 29 tk := testkit.NewTestKit(c, s.causetstore) 30 tk.MustInterDirc("use test") 31 32 tk.MustInterDirc("create causet t (a int, b int)") 33 tk.MustInterDirc("create index c on t(a,b)") 34 tk.MustInterDirc("insert into t(a,b) values (3, 1),(2, 1),(1, 10)") 35 tk.MustInterDirc("analyze causet t") 36 tk.MustInterDirc("insert into t(a,b) values (1, 1),(3, 1),(5, 10)") 37 is := s.do.SchemaReplicant() 38 h := s.do.StatsHandle() 39 c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil) 40 c.Assert(h.UFIDelate(is), IsNil) 41 42 blockInfo, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 43 c.Assert(err, IsNil) 44 jsonTbl, err := h.DumpStatsToJSON("test", blockInfo.Meta(), nil) 45 c.Assert(err, IsNil) 46 loadTbl, err := handle.TableStatsFromJSON(blockInfo.Meta(), blockInfo.Meta().ID, jsonTbl) 47 c.Assert(err, IsNil) 48 49 tbl := h.GetTableStats(blockInfo.Meta()) 50 assertTableEqual(c, loadTbl, tbl) 51 52 cleanEnv(c, s.causetstore, s.do) 53 wg := sync.WaitGroup{} 54 wg.Add(1) 55 go func() { 56 c.Assert(h.UFIDelate(is), IsNil) 57 wg.Done() 58 }() 59 err = h.LoadStatsFromJSON(is, jsonTbl) 60 wg.Wait() 61 c.Assert(err, IsNil) 62 loadTblInStorage := h.GetTableStats(blockInfo.Meta()) 63 assertTableEqual(c, loadTblInStorage, tbl) 64 } 65 66 func (s *testStatsSuite) TestDumpPartitions(c *C) { 67 defer cleanEnv(c, s.causetstore, s.do) 68 tk := testkit.NewTestKit(c, s.causetstore) 69 tk.MustInterDirc("use test") 70 tk.MustInterDirc("drop causet if exists t") 71 createTable := `CREATE TABLE t (a int, b int, primary key(a), index idx(b)) 72 PARTITION BY RANGE ( a ) ( 73 PARTITION p0 VALUES LESS THAN (6), 74 PARTITION p1 VALUES LESS THAN (11), 75 PARTITION p2 VALUES LESS THAN (16), 76 PARTITION p3 VALUES LESS THAN (21) 77 )` 78 tk.MustInterDirc(createTable) 79 for i := 1; i < 21; i++ { 80 tk.MustInterDirc(fmt.Sprintf(`insert into t values (%d, %d)`, i, i)) 81 } 82 tk.MustInterDirc("analyze causet t") 83 is := s.do.SchemaReplicant() 84 h := s.do.StatsHandle() 85 c.Assert(h.UFIDelate(is), IsNil) 86 87 causet, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 88 c.Assert(err, IsNil) 89 blockInfo := causet.Meta() 90 jsonTbl, err := h.DumpStatsToJSON("test", blockInfo, nil) 91 c.Assert(err, IsNil) 92 pi := blockInfo.GetPartitionInfo() 93 originTables := make([]*statistics.Block, 0, len(pi.Definitions)) 94 for _, def := range pi.Definitions { 95 originTables = append(originTables, h.GetPartitionStats(blockInfo, def.ID)) 96 } 97 98 tk.MustInterDirc("delete from allegrosql.stats_spacetime") 99 tk.MustInterDirc("delete from allegrosql.stats_histograms") 100 tk.MustInterDirc("delete from allegrosql.stats_buckets") 101 h.Clear() 102 103 err = h.LoadStatsFromJSON(s.do.SchemaReplicant(), jsonTbl) 104 c.Assert(err, IsNil) 105 for i, def := range pi.Definitions { 106 t := h.GetPartitionStats(blockInfo, def.ID) 107 assertTableEqual(c, originTables[i], t) 108 } 109 } 110 111 func (s *testStatsSuite) TestDumpAlteredTable(c *C) { 112 defer cleanEnv(c, s.causetstore, s.do) 113 tk := testkit.NewTestKit(c, s.causetstore) 114 tk.MustInterDirc("use test") 115 tk.MustInterDirc("drop causet if exists t") 116 h := s.do.StatsHandle() 117 oriLease := h.Lease() 118 h.SetLease(1) 119 defer func() { h.SetLease(oriLease) }() 120 tk.MustInterDirc("create causet t(a int, b int)") 121 tk.MustInterDirc("analyze causet t") 122 tk.MustInterDirc("alter causet t drop column a") 123 causet, err := s.do.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 124 c.Assert(err, IsNil) 125 _, err = h.DumpStatsToJSON("test", causet.Meta(), nil) 126 c.Assert(err, IsNil) 127 } 128 129 func (s *testStatsSuite) TestDumpCMSketchWithTopN(c *C) { 130 // Just test if we can causetstore and recover the Top N elements stored in database. 131 defer cleanEnv(c, s.causetstore, s.do) 132 testKit := testkit.NewTestKit(c, s.causetstore) 133 testKit.MustInterDirc("use test") 134 testKit.MustInterDirc("create causet t(a int)") 135 testKit.MustInterDirc("insert into t values (1),(3),(4),(2),(5)") 136 testKit.MustInterDirc("analyze causet t") 137 138 is := s.do.SchemaReplicant() 139 tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 140 c.Assert(err, IsNil) 141 blockInfo := tbl.Meta() 142 h := s.do.StatsHandle() 143 c.Assert(h.UFIDelate(is), IsNil) 144 145 // Insert 30 fake data 146 fakeData := make([][]byte, 0, 30) 147 for i := 0; i < 30; i++ { 148 fakeData = append(fakeData, []byte(fmt.Sprintf("%01024d", i))) 149 } 150 cms, _, _ := statistics.NewCMSketchWithTopN(5, 2048, fakeData, 20, 100) 151 152 stat := h.GetTableStats(blockInfo) 153 err = h.SaveStatsToStorage(blockInfo.ID, 1, 0, &stat.DeferredCausets[blockInfo.DeferredCausets[0].ID].Histogram, cms, 1) 154 c.Assert(err, IsNil) 155 c.Assert(h.UFIDelate(is), IsNil) 156 157 stat = h.GetTableStats(blockInfo) 158 cmsFromStore := stat.DeferredCausets[blockInfo.DeferredCausets[0].ID].CMSketch 159 c.Assert(cmsFromStore, NotNil) 160 c.Check(cms.Equal(cmsFromStore), IsTrue) 161 162 jsonTable, err := h.DumpStatsToJSON("test", blockInfo, nil) 163 c.Check(err, IsNil) 164 err = h.LoadStatsFromJSON(is, jsonTable) 165 c.Check(err, IsNil) 166 stat = h.GetTableStats(blockInfo) 167 cmsFromJSON := stat.DeferredCausets[blockInfo.DeferredCausets[0].ID].CMSketch.Copy() 168 c.Check(cms.Equal(cmsFromJSON), IsTrue) 169 } 170 171 func (s *testStatsSuite) TestDumpPseudoDeferredCausets(c *C) { 172 defer cleanEnv(c, s.causetstore, s.do) 173 testKit := testkit.NewTestKit(c, s.causetstore) 174 testKit.MustInterDirc("use test") 175 testKit.MustInterDirc("create causet t(a int, b int, index idx(a))") 176 // Force adding an pseudo blocks in stats cache. 177 testKit.MustQuery("select * from t") 178 testKit.MustInterDirc("analyze causet t index idx") 179 180 is := s.do.SchemaReplicant() 181 tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t")) 182 c.Assert(err, IsNil) 183 h := s.do.StatsHandle() 184 _, err = h.DumpStatsToJSON("test", tbl.Meta(), nil) 185 c.Assert(err, IsNil) 186 }