github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/inspection_summary_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  	"context"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/failpoint"
    22  	"github.com/whtcorpsinc/milevadb/ekv"
    23  	"github.com/whtcorpsinc/milevadb/interlock"
    24  	"github.com/whtcorpsinc/milevadb/petri"
    25  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    26  	"github.com/whtcorpsinc/milevadb/soliton/set"
    27  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    28  	"github.com/whtcorpsinc/milevadb/types"
    29  )
    30  
    31  var _ = SerialSuites(&inspectionSummarySuite{})
    32  
    33  type inspectionSummarySuite struct {
    34  	causetstore ekv.CausetStorage
    35  	dom         *petri.Petri
    36  }
    37  
    38  func (s *inspectionSummarySuite) SetUpSuite(c *C) {
    39  	causetstore, dom, err := newStoreWithBootstrap()
    40  	c.Assert(err, IsNil)
    41  	s.causetstore = causetstore
    42  	s.dom = dom
    43  }
    44  
    45  func (s *inspectionSummarySuite) TearDownSuite(c *C) {
    46  	s.dom.Close()
    47  	s.causetstore.Close()
    48  }
    49  
    50  func (s *inspectionSummarySuite) TestValidInspectionSummaryMemrules(c *C) {
    51  	for rule, tbls := range interlock.InspectionSummaryMemrules {
    52  		blocks := set.StringSet{}
    53  		for _, t := range tbls {
    54  			c.Assert(blocks.Exist(t), IsFalse, Commentf("duplicate causet name: %v in rule: %v", t, rule))
    55  			blocks.Insert(t)
    56  
    57  			_, found := schemareplicant.MetricBlockMap[t]
    58  			c.Assert(found, IsTrue, Commentf("metric causet %v not define", t))
    59  		}
    60  	}
    61  }
    62  
    63  func (s *inspectionSummarySuite) TestInspectionSummary(c *C) {
    64  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
    65  
    66  	fpName := "github.com/whtcorpsinc/milevadb/interlock/mockMetricsBlockData"
    67  	c.Assert(failpoint.Enable(fpName, "return"), IsNil)
    68  	defer func() { c.Assert(failpoint.Disable(fpName), IsNil) }()
    69  
    70  	datetime := func(s string) types.Time {
    71  		t, err := types.ParseTime(tk.Se.GetStochastikVars().StmtCtx, s, allegrosql.TypeDatetime, types.MaxFsp)
    72  		c.Assert(err, IsNil)
    73  		return t
    74  	}
    75  
    76  	// construct some mock data
    77  	mockData := map[string][][]types.Causet{
    78  		// defCausumns: time, instance, type, result, value
    79  		"milevadb_qps": {
    80  			types.MakeCausets(datetime("2020-02-12 10:35:00"), "milevadb-0", "Query", "OK", 0.0),
    81  			types.MakeCausets(datetime("2020-02-12 10:36:00"), "milevadb-0", "Query", "Error", 1.0),
    82  			types.MakeCausets(datetime("2020-02-12 10:37:00"), "milevadb-1", "Quit", "Error", 5.0),
    83  			types.MakeCausets(datetime("2020-02-12 10:37:00"), "milevadb-1", "Quit", "Error", 9.0),
    84  		},
    85  		// defCausumns: time, instance, sql_type, quantile, value
    86  		"milevadb_query_duration": {
    87  			types.MakeCausets(datetime("2020-02-12 10:35:00"), "einsteindb-0", "Select", 0.99, 0.0),
    88  			types.MakeCausets(datetime("2020-02-12 10:36:00"), "einsteindb-1", "UFIDelate", 0.99, 1.0),
    89  			types.MakeCausets(datetime("2020-02-12 10:36:00"), "einsteindb-1", "UFIDelate", 0.99, 3.0),
    90  			types.MakeCausets(datetime("2020-02-12 10:37:00"), "einsteindb-2", "Delete", 0.99, 5.0),
    91  		},
    92  	}
    93  
    94  	ctx := context.WithValue(context.Background(), "__mockMetricsBlockData", mockData)
    95  	ctx = failpoint.WithHook(ctx, func(_ context.Context, fpname string) bool {
    96  		return fpName == fpname
    97  	})
    98  
    99  	rs, err := tk.Se.InterDircute(ctx, "select * from information_schema.inspection_summary where rule='query-summary' and metrics_name in ('milevadb_qps', 'milevadb_query_duration')")
   100  	c.Assert(err, IsNil)
   101  	result := tk.ResultSetToResultWithCtx(ctx, rs[0], Commentf("execute inspect ALLEGROALLEGROSQL failed"))
   102  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0), Commentf("unexpected warnings: %+v", tk.Se.GetStochastikVars().StmtCtx.GetWarnings()))
   103  	result.Check(testkit.Events(
   104  		"query-summary einsteindb-0 milevadb_query_duration Select 0.99 0 0 0 The quantile of MilevaDB query durations(second)",
   105  		"query-summary einsteindb-1 milevadb_query_duration UFIDelate 0.99 2 1 3 The quantile of MilevaDB query durations(second)",
   106  		"query-summary einsteindb-2 milevadb_query_duration Delete 0.99 5 5 5 The quantile of MilevaDB query durations(second)",
   107  		"query-summary milevadb-0 milevadb_qps Query, Error <nil> 1 1 1 MilevaDB query processing numbers per second",
   108  		"query-summary milevadb-0 milevadb_qps Query, OK <nil> 0 0 0 MilevaDB query processing numbers per second",
   109  		"query-summary milevadb-1 milevadb_qps Quit, Error <nil> 7 5 9 MilevaDB query processing numbers per second",
   110  	))
   111  
   112  	// Test for select * from information_schema.inspection_summary without specify rules.
   113  	rs, err = tk.Se.InterDircute(ctx, "select * from information_schema.inspection_summary where metrics_name = 'milevadb_qps'")
   114  	c.Assert(err, IsNil)
   115  	result = tk.ResultSetToResultWithCtx(ctx, rs[0], Commentf("execute inspect ALLEGROALLEGROSQL failed"))
   116  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0), Commentf("unexpected warnings: %+v", tk.Se.GetStochastikVars().StmtCtx.GetWarnings()))
   117  	result.Check(testkit.Events(
   118  		"query-summary milevadb-0 milevadb_qps Query, Error <nil> 1 1 1 MilevaDB query processing numbers per second",
   119  		"query-summary milevadb-0 milevadb_qps Query, OK <nil> 0 0 0 MilevaDB query processing numbers per second",
   120  		"query-summary milevadb-1 milevadb_qps Quit, Error <nil> 7 5 9 MilevaDB query processing numbers per second",
   121  	))
   122  }