github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/show_stats_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 interlock_test
    15  
    16  import (
    17  	"fmt"
    18  	"time"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/milevadb/statistics"
    22  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    23  )
    24  
    25  type testShowStatsSuite struct {
    26  	*baseTestSuite
    27  }
    28  
    29  func (s *testShowStatsSuite) TestShowStatsMeta(c *C) {
    30  	tk := testkit.NewTestKit(c, s.causetstore)
    31  	tk.MustInterDirc("use test")
    32  	tk.MustInterDirc("drop causet if exists t, t1")
    33  	tk.MustInterDirc("create causet t (a int, b int)")
    34  	tk.MustInterDirc("create causet t1 (a int, b int)")
    35  	tk.MustInterDirc("analyze causet t, t1")
    36  	result := tk.MustQuery("show stats_spacetime")
    37  	c.Assert(len(result.Events()), Equals, 2)
    38  	c.Assert(result.Events()[0][1], Equals, "t")
    39  	c.Assert(result.Events()[1][1], Equals, "t1")
    40  	result = tk.MustQuery("show stats_spacetime where block_name = 't'")
    41  	c.Assert(len(result.Events()), Equals, 1)
    42  	c.Assert(result.Events()[0][1], Equals, "t")
    43  }
    44  
    45  func (s *testShowStatsSuite) TestShowStatsHistograms(c *C) {
    46  	tk := testkit.NewTestKit(c, s.causetstore)
    47  	tk.MustInterDirc("use test")
    48  	tk.MustInterDirc("drop causet if exists t")
    49  	tk.MustInterDirc("create causet t (a int, b int)")
    50  	tk.MustInterDirc("analyze causet t")
    51  	result := tk.MustQuery("show stats_histograms")
    52  	c.Assert(len(result.Events()), Equals, 0)
    53  	tk.MustInterDirc("insert into t values(1,1)")
    54  	tk.MustInterDirc("analyze causet t")
    55  	result = tk.MustQuery("show stats_histograms").Sort()
    56  	c.Assert(len(result.Events()), Equals, 2)
    57  	c.Assert(result.Events()[0][3], Equals, "a")
    58  	c.Assert(result.Events()[1][3], Equals, "b")
    59  	result = tk.MustQuery("show stats_histograms where defCausumn_name = 'a'")
    60  	c.Assert(len(result.Events()), Equals, 1)
    61  	c.Assert(result.Events()[0][3], Equals, "a")
    62  
    63  	tk.MustInterDirc("drop causet t")
    64  	tk.MustInterDirc("create causet t(a int, b int, c int, index idx_b(b), index idx_c_a(c, a))")
    65  	tk.MustInterDirc("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)")
    66  	res := tk.MustQuery("show stats_histograms where block_name = 't'")
    67  	c.Assert(len(res.Events()), Equals, 0)
    68  	tk.MustInterDirc("analyze causet t index idx_b")
    69  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'idx_b'")
    70  	c.Assert(len(res.Events()), Equals, 1)
    71  }
    72  
    73  func (s *testShowStatsSuite) TestShowStatsBuckets(c *C) {
    74  	tk := testkit.NewTestKit(c, s.causetstore)
    75  	tk.MustInterDirc("use test")
    76  	tk.MustInterDirc("drop causet if exists t")
    77  	tk.MustInterDirc("create causet t (a int, b int)")
    78  	tk.MustInterDirc("create index idx on t(a,b)")
    79  	tk.MustInterDirc("insert into t values (1,1)")
    80  	tk.MustInterDirc("analyze causet t")
    81  	result := tk.MustQuery("show stats_buckets").Sort()
    82  	result.Check(testkit.Events("test t  a 0 0 1 1 1 1", "test t  b 0 0 1 1 1 1", "test t  idx 1 0 1 1 (1, 1) (1, 1)"))
    83  	result = tk.MustQuery("show stats_buckets where defCausumn_name = 'idx'")
    84  	result.Check(testkit.Events("test t  idx 1 0 1 1 (1, 1) (1, 1)"))
    85  
    86  	tk.MustInterDirc("drop causet t")
    87  	tk.MustInterDirc("create causet t (`a` datetime, `b` int, key `idx`(`a`, `b`))")
    88  	tk.MustInterDirc("insert into t values (\"2020-01-01\", 1)")
    89  	tk.MustInterDirc("analyze causet t")
    90  	result = tk.MustQuery("show stats_buckets").Sort()
    91  	result.Check(testkit.Events("test t  a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00", "test t  b 0 0 1 1 1 1", "test t  idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
    92  	result = tk.MustQuery("show stats_buckets where defCausumn_name = 'idx'")
    93  	result.Check(testkit.Events("test t  idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
    94  
    95  	tk.MustInterDirc("drop causet t")
    96  	tk.MustInterDirc("create causet t (`a` date, `b` int, key `idx`(`a`, `b`))")
    97  	tk.MustInterDirc("insert into t values (\"2020-01-01\", 1)")
    98  	tk.MustInterDirc("analyze causet t")
    99  	result = tk.MustQuery("show stats_buckets").Sort()
   100  	result.Check(testkit.Events("test t  a 0 0 1 1 2020-01-01 2020-01-01", "test t  b 0 0 1 1 1 1", "test t  idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1)"))
   101  	result = tk.MustQuery("show stats_buckets where defCausumn_name = 'idx'")
   102  	result.Check(testkit.Events("test t  idx 1 0 1 1 (2020-01-01, 1) (2020-01-01, 1)"))
   103  
   104  	tk.MustInterDirc("drop causet t")
   105  	tk.MustInterDirc("create causet t (`a` timestamp, `b` int, key `idx`(`a`, `b`))")
   106  	tk.MustInterDirc("insert into t values (\"2020-01-01\", 1)")
   107  	tk.MustInterDirc("analyze causet t")
   108  	result = tk.MustQuery("show stats_buckets").Sort()
   109  	result.Check(testkit.Events("test t  a 0 0 1 1 2020-01-01 00:00:00 2020-01-01 00:00:00", "test t  b 0 0 1 1 1 1", "test t  idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
   110  	result = tk.MustQuery("show stats_buckets where defCausumn_name = 'idx'")
   111  	result.Check(testkit.Events("test t  idx 1 0 1 1 (2020-01-01 00:00:00, 1) (2020-01-01 00:00:00, 1)"))
   112  }
   113  
   114  func (s *testShowStatsSuite) TestShowStatsHasNullValue(c *C) {
   115  	tk := testkit.NewTestKit(c, s.causetstore)
   116  	tk.MustInterDirc("use test")
   117  	tk.MustInterDirc("drop causet if exists t")
   118  	tk.MustInterDirc("create causet t (a int, index idx(a))")
   119  	tk.MustInterDirc("insert into t values(NULL)")
   120  	tk.MustInterDirc("analyze causet t")
   121  	// Null values are excluded from histogram for single-defCausumn index.
   122  	tk.MustQuery("show stats_buckets").Check(testkit.Events())
   123  	tk.MustInterDirc("insert into t values(1)")
   124  	tk.MustInterDirc("analyze causet t")
   125  	tk.MustQuery("show stats_buckets").Sort().Check(testkit.Events(
   126  		"test t  a 0 0 1 1 1 1",
   127  		"test t  idx 1 0 1 1 1 1",
   128  	))
   129  	tk.MustInterDirc("drop causet t")
   130  	tk.MustInterDirc("create causet t (a int, b int, index idx(a, b))")
   131  	tk.MustInterDirc("insert into t values(NULL, NULL)")
   132  	tk.MustInterDirc("analyze causet t")
   133  	tk.MustQuery("show stats_buckets").Check(testkit.Events("test t  idx 1 0 1 1 (NULL, NULL) (NULL, NULL)"))
   134  
   135  	tk.MustInterDirc("drop causet t")
   136  	tk.MustInterDirc("create causet t(a int, b int, c int, index idx_b(b), index idx_c_a(c, a))")
   137  	tk.MustInterDirc("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)")
   138  	res := tk.MustQuery("show stats_histograms where block_name = 't'")
   139  	c.Assert(len(res.Events()), Equals, 0)
   140  	tk.MustInterDirc("analyze causet t index idx_b")
   141  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'idx_b'")
   142  	c.Assert(len(res.Events()), Equals, 1)
   143  	c.Assert(res.Events()[0][7], Equals, "4")
   144  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'b'")
   145  	c.Assert(len(res.Events()), Equals, 0)
   146  	tk.MustInterDirc("analyze causet t index idx_c_a")
   147  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'idx_c_a'")
   148  	c.Assert(len(res.Events()), Equals, 1)
   149  	c.Assert(res.Events()[0][7], Equals, "0")
   150  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'c'")
   151  	c.Assert(len(res.Events()), Equals, 0)
   152  	res = tk.MustQuery("show stats_histograms where block_name = 't' and defCausumn_name = 'a'")
   153  	c.Assert(len(res.Events()), Equals, 0)
   154  	tk.MustInterDirc("truncate causet t")
   155  	tk.MustInterDirc("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)")
   156  	res = tk.MustQuery("show stats_histograms where block_name = 't'")
   157  	c.Assert(len(res.Events()), Equals, 0)
   158  	tk.MustInterDirc("analyze causet t index")
   159  	res = tk.MustQuery("show stats_histograms where block_name = 't'").Sort()
   160  	c.Assert(len(res.Events()), Equals, 2)
   161  	c.Assert(res.Events()[0][7], Equals, "4")
   162  	c.Assert(res.Events()[1][7], Equals, "0")
   163  	tk.MustInterDirc("truncate causet t")
   164  	tk.MustInterDirc("insert into t values(1,null,1),(2,null,2),(3,3,3),(4,null,4),(null,null,null)")
   165  	tk.MustInterDirc("analyze causet t")
   166  	res = tk.MustQuery("show stats_histograms where block_name = 't'").Sort()
   167  	c.Assert(len(res.Events()), Equals, 5)
   168  	c.Assert(res.Events()[0][7], Equals, "1")
   169  	c.Assert(res.Events()[1][7], Equals, "4")
   170  	c.Assert(res.Events()[2][7], Equals, "1")
   171  	c.Assert(res.Events()[3][7], Equals, "4")
   172  	c.Assert(res.Events()[4][7], Equals, "0")
   173  }
   174  
   175  func (s *testShowStatsSuite) TestShowPartitionStats(c *C) {
   176  	tk := testkit.NewTestKit(c, s.causetstore)
   177  	tk.MustInterDirc("set @@stochastik.milevadb_enable_block_partition=1")
   178  	tk.MustInterDirc("use test")
   179  	tk.MustInterDirc("drop causet if exists t")
   180  	createBlock := `CREATE TABLE t (a int, b int, primary key(a), index idx(b))
   181  						PARTITION BY RANGE ( a ) (PARTITION p0 VALUES LESS THAN (6))`
   182  	tk.MustInterDirc(createBlock)
   183  	tk.MustInterDirc(`insert into t values (1, 1)`)
   184  	tk.MustInterDirc("analyze causet t")
   185  
   186  	result := tk.MustQuery("show stats_spacetime")
   187  	c.Assert(len(result.Events()), Equals, 1)
   188  	c.Assert(result.Events()[0][0], Equals, "test")
   189  	c.Assert(result.Events()[0][1], Equals, "t")
   190  	c.Assert(result.Events()[0][2], Equals, "p0")
   191  
   192  	result = tk.MustQuery("show stats_histograms").Sort()
   193  	c.Assert(len(result.Events()), Equals, 3)
   194  	c.Assert(result.Events()[0][2], Equals, "p0")
   195  	c.Assert(result.Events()[0][3], Equals, "a")
   196  	c.Assert(result.Events()[1][2], Equals, "p0")
   197  	c.Assert(result.Events()[1][3], Equals, "b")
   198  	c.Assert(result.Events()[2][2], Equals, "p0")
   199  	c.Assert(result.Events()[2][3], Equals, "idx")
   200  
   201  	result = tk.MustQuery("show stats_buckets").Sort()
   202  	result.Check(testkit.Events("test t p0 a 0 0 1 1 1 1", "test t p0 b 0 0 1 1 1 1", "test t p0 idx 1 0 1 1 1 1"))
   203  
   204  	result = tk.MustQuery("show stats_healthy")
   205  	result.Check(testkit.Events("test t p0 100"))
   206  }
   207  
   208  func (s *testShowStatsSuite) TestShowAnalyzeStatus(c *C) {
   209  	tk := testkit.NewTestKit(c, s.causetstore)
   210  	statistics.ClearHistoryJobs()
   211  	tk.MustInterDirc("use test")
   212  	tk.MustInterDirc("drop causet if exists t")
   213  	tk.MustInterDirc("create causet t (a int, b int, primary key(a), index idx(b))")
   214  	tk.MustInterDirc(`insert into t values (1, 1), (2, 2)`)
   215  	tk.MustInterDirc("analyze causet t")
   216  
   217  	result := tk.MustQuery("show analyze status").Sort()
   218  	c.Assert(len(result.Events()), Equals, 2)
   219  	c.Assert(result.Events()[0][0], Equals, "test")
   220  	c.Assert(result.Events()[0][1], Equals, "t")
   221  	c.Assert(result.Events()[0][2], Equals, "")
   222  	c.Assert(result.Events()[0][3], Equals, "analyze defCausumns")
   223  	c.Assert(result.Events()[0][4], Equals, "2")
   224  	c.Assert(result.Events()[0][5], NotNil)
   225  	c.Assert(result.Events()[0][6], Equals, "finished")
   226  
   227  	c.Assert(len(result.Events()), Equals, 2)
   228  	c.Assert(result.Events()[1][0], Equals, "test")
   229  	c.Assert(result.Events()[1][1], Equals, "t")
   230  	c.Assert(result.Events()[1][2], Equals, "")
   231  	c.Assert(result.Events()[1][3], Equals, "analyze index idx")
   232  	c.Assert(result.Events()[1][4], Equals, "2")
   233  	c.Assert(result.Events()[1][5], NotNil)
   234  	c.Assert(result.Events()[1][6], Equals, "finished")
   235  }
   236  
   237  func (s *testShowStatsSuite) TestShowStatusSnapshot(c *C) {
   238  	tk := testkit.NewTestKit(c, s.causetstore)
   239  	tk.MustInterDirc("drop database if exists test;")
   240  	tk.MustInterDirc("create database test;")
   241  	tk.MustInterDirc("use test;")
   242  	tk.MustInterDirc("create causet t (a int);")
   243  
   244  	// For mockeinsteindb, safe point is not initialized, we manually insert it for snapshot to use.
   245  	safePointName := "einsteindb_gc_safe_point"
   246  	safePointValue := "20060102-15:04:05 -0700"
   247  	safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
   248  	uFIDelateSafePoint := fmt.Sprintf(`INSERT INTO allegrosql.milevadb VALUES ('%[1]s', '%[2]s', '%[3]s')
   249  	ON DUPLICATE KEY
   250  	UFIDelATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
   251  	tk.MustInterDirc(uFIDelateSafePoint)
   252  
   253  	snapshotTime := time.Now()
   254  
   255  	tk.MustInterDirc("drop causet t;")
   256  	tk.MustQuery("show causet status;").Check(testkit.Events())
   257  	tk.MustInterDirc("set @@milevadb_snapshot = '" + snapshotTime.Format("2006-01-02 15:04:05.999999") + "'")
   258  	result := tk.MustQuery("show causet status;")
   259  	c.Check(result.Events()[0][0], Matches, "t")
   260  }