github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/stochastikctx/variable/session_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 variable_test
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL"
    20  	"github.com/whtcorpsinc/BerolinaSQL/auth"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/milevadb/config"
    23  	"github.com/whtcorpsinc/milevadb/ekv"
    24  	"github.com/whtcorpsinc/milevadb/soliton/execdetails"
    25  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    26  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    27  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    28  	"github.com/whtcorpsinc/milevadb/types"
    29  )
    30  
    31  var _ = SerialSuites(&testStochastikSuite{})
    32  
    33  type testStochastikSuite struct {
    34  }
    35  
    36  func (*testStochastikSuite) TestSetSystemVariable(c *C) {
    37  	v := variable.NewStochastikVars()
    38  	v.GlobalVarsAccessor = variable.NewMockGlobalAccessor()
    39  	v.TimeZone = time.UTC
    40  	tests := []struct {
    41  		key   string
    42  		value interface{}
    43  		err   bool
    44  	}{
    45  		{variable.TxnIsolation, "SERIALIZABLE", true},
    46  		{variable.TimeZone, "xyz", true},
    47  		{variable.MilevaDBOptAggPushDown, "1", false},
    48  		{variable.MilevaDBOptDistinctAggPushDown, "1", false},
    49  		{variable.MilevaDBMemQuotaQuery, "1024", false},
    50  		{variable.MilevaDBMemQuotaHashJoin, "1024", false},
    51  		{variable.MilevaDBMemQuotaMergeJoin, "1024", false},
    52  		{variable.MilevaDBMemQuotaSort, "1024", false},
    53  		{variable.MilevaDBMemQuotaTopn, "1024", false},
    54  		{variable.MilevaDBMemQuotaIndexLookupReader, "1024", false},
    55  		{variable.MilevaDBMemQuotaIndexLookupJoin, "1024", false},
    56  		{variable.MilevaDBMemQuotaNestedLoopApply, "1024", false},
    57  		{variable.MilevaDBEnableStmtSummary, "1", false},
    58  	}
    59  	for _, t := range tests {
    60  		err := variable.SetStochastikSystemVar(v, t.key, types.NewCauset(t.value))
    61  		if t.err {
    62  			c.Assert(err, NotNil)
    63  		} else {
    64  			c.Assert(err, IsNil)
    65  		}
    66  	}
    67  }
    68  
    69  func (*testStochastikSuite) TestStochastik(c *C) {
    70  	ctx := mock.NewContext()
    71  
    72  	ss := ctx.GetStochastikVars().StmtCtx
    73  	c.Assert(ss, NotNil)
    74  
    75  	// For AffectedRows
    76  	ss.AddAffectedRows(1)
    77  	c.Assert(ss.AffectedRows(), Equals, uint64(1))
    78  	ss.AddAffectedRows(1)
    79  	c.Assert(ss.AffectedRows(), Equals, uint64(2))
    80  
    81  	// For RecordRows
    82  	ss.AddRecordRows(1)
    83  	c.Assert(ss.RecordRows(), Equals, uint64(1))
    84  	ss.AddRecordRows(1)
    85  	c.Assert(ss.RecordRows(), Equals, uint64(2))
    86  
    87  	// For FoundRows
    88  	ss.AddFoundRows(1)
    89  	c.Assert(ss.FoundRows(), Equals, uint64(1))
    90  	ss.AddFoundRows(1)
    91  	c.Assert(ss.FoundRows(), Equals, uint64(2))
    92  
    93  	// For UFIDelatedRows
    94  	ss.AddUFIDelatedRows(1)
    95  	c.Assert(ss.UFIDelatedRows(), Equals, uint64(1))
    96  	ss.AddUFIDelatedRows(1)
    97  	c.Assert(ss.UFIDelatedRows(), Equals, uint64(2))
    98  
    99  	// For TouchedRows
   100  	ss.AddTouchedRows(1)
   101  	c.Assert(ss.TouchedRows(), Equals, uint64(1))
   102  	ss.AddTouchedRows(1)
   103  	c.Assert(ss.TouchedRows(), Equals, uint64(2))
   104  
   105  	// For CopiedRows
   106  	ss.AddCopiedRows(1)
   107  	c.Assert(ss.CopiedRows(), Equals, uint64(1))
   108  	ss.AddCopiedRows(1)
   109  	c.Assert(ss.CopiedRows(), Equals, uint64(2))
   110  
   111  	// For last insert id
   112  	ctx.GetStochastikVars().SetLastInsertID(1)
   113  	c.Assert(ctx.GetStochastikVars().StmtCtx.LastInsertID, Equals, uint64(1))
   114  
   115  	ss.ResetForRetry()
   116  	c.Assert(ss.AffectedRows(), Equals, uint64(0))
   117  	c.Assert(ss.FoundRows(), Equals, uint64(0))
   118  	c.Assert(ss.UFIDelatedRows(), Equals, uint64(0))
   119  	c.Assert(ss.RecordRows(), Equals, uint64(0))
   120  	c.Assert(ss.TouchedRows(), Equals, uint64(0))
   121  	c.Assert(ss.CopiedRows(), Equals, uint64(0))
   122  	c.Assert(ss.WarningCount(), Equals, uint16(0))
   123  }
   124  
   125  func (*testStochastikSuite) TestSlowLogFormat(c *C) {
   126  	ctx := mock.NewContext()
   127  
   128  	seVar := ctx.GetStochastikVars()
   129  	c.Assert(seVar, NotNil)
   130  
   131  	seVar.User = &auth.UserIdentity{Username: "root", Hostname: "192.168.0.1"}
   132  	seVar.ConnectionInfo = &variable.ConnectionInfo{ClientIP: "192.168.0.1"}
   133  	seVar.ConnectionID = 1
   134  	seVar.CurrentDB = "test"
   135  	seVar.InRestrictedALLEGROSQL = true
   136  	txnTS := uint64(406649736972468225)
   137  	costTime := time.Second
   138  	execDetail := execdetails.InterDircDetails{
   139  		ProcessTime:   time.Second * time.Duration(2),
   140  		WaitTime:      time.Minute,
   141  		BackoffTime:   time.Millisecond,
   142  		RequestCount:  2,
   143  		TotalKeys:     10000,
   144  		ProcessedKeys: 20001,
   145  	}
   146  	statsInfos := make(map[string]uint64)
   147  	statsInfos["t1"] = 0
   148  	CausetTasks := &stmtctx.CausetTasksDetails{
   149  		NumCausetTasks:    10,
   150  		AvgProcessTime:    time.Second,
   151  		P90ProcessTime:    time.Second * 2,
   152  		MaxProcessAddress: "10.6.131.78",
   153  		MaxProcessTime:    time.Second * 3,
   154  		AvgWaitTime:       time.Millisecond * 10,
   155  		P90WaitTime:       time.Millisecond * 20,
   156  		MaxWaitTime:       time.Millisecond * 30,
   157  		MaxWaitAddress:    "10.6.131.79",
   158  		MaxBackoffTime:    make(map[string]time.Duration),
   159  		AvgBackoffTime:    make(map[string]time.Duration),
   160  		P90BackoffTime:    make(map[string]time.Duration),
   161  		TotBackoffTime:    make(map[string]time.Duration),
   162  		TotBackoffTimes:   make(map[string]int),
   163  		MaxBackoffAddress: make(map[string]string),
   164  	}
   165  
   166  	backoffs := []string{"rpcEinsteinDB", "rpcFIDel", "regionMiss"}
   167  	for _, backoff := range backoffs {
   168  		CausetTasks.MaxBackoffTime[backoff] = time.Millisecond * 200
   169  		CausetTasks.MaxBackoffAddress[backoff] = "127.0.0.1"
   170  		CausetTasks.AvgBackoffTime[backoff] = time.Millisecond * 200
   171  		CausetTasks.P90BackoffTime[backoff] = time.Millisecond * 200
   172  		CausetTasks.TotBackoffTime[backoff] = time.Millisecond * 200
   173  		CausetTasks.TotBackoffTimes[backoff] = 200
   174  	}
   175  
   176  	var memMax int64 = 2333
   177  	var diskMax int64 = 6666
   178  	resultFields := `# Txn_start_ts: 406649736972468225
   179  # User@Host: root[root] @ 192.168.0.1 [192.168.0.1]
   180  # Conn_ID: 1
   181  # InterDirc_retry_time: 5.1 InterDirc_retry_count: 3
   182  # Query_time: 1
   183  # Parse_time: 0.00000001
   184  # Compile_time: 0.00000001
   185  # Rewrite_time: 0.000000003 Preproc_subqueries: 2 Preproc_subqueries_time: 0.000000002
   186  # Optimize_time: 0.00000001
   187  # Wait_TS: 0.000000003
   188  # Process_time: 2 Wait_time: 60 Backoff_time: 0.001 Request_count: 2 Total_keys: 10000 Process_keys: 20001
   189  # EDB: test
   190  # Index_names: [t1:a,t2:b]
   191  # Is_internal: true
   192  # Digest: f94c76d7fa8f60e438118752bfbfb71fe9e1934888ac415ddd8625b121af124c
   193  # Stats: t1:pseudo
   194  # Num_cop_tasks: 10
   195  # Cop_proc_avg: 1 Cop_proc_p90: 2 Cop_proc_max: 3 Cop_proc_addr: 10.6.131.78
   196  # Cop_wait_avg: 0.01 Cop_wait_p90: 0.02 Cop_wait_max: 0.03 Cop_wait_addr: 10.6.131.79
   197  # Cop_backoff_regionMiss_total_times: 200 Cop_backoff_regionMiss_total_time: 0.2 Cop_backoff_regionMiss_max_time: 0.2 Cop_backoff_regionMiss_max_addr: 127.0.0.1 Cop_backoff_regionMiss_avg_time: 0.2 Cop_backoff_regionMiss_p90_time: 0.2
   198  # Cop_backoff_rpcFIDel_total_times: 200 Cop_backoff_rpcFIDel_total_time: 0.2 Cop_backoff_rpcFIDel_max_time: 0.2 Cop_backoff_rpcFIDel_max_addr: 127.0.0.1 Cop_backoff_rpcFIDel_avg_time: 0.2 Cop_backoff_rpcFIDel_p90_time: 0.2
   199  # Cop_backoff_rpcEinsteinDB_total_times: 200 Cop_backoff_rpcEinsteinDB_total_time: 0.2 Cop_backoff_rpcEinsteinDB_max_time: 0.2 Cop_backoff_rpcEinsteinDB_max_addr: 127.0.0.1 Cop_backoff_rpcEinsteinDB_avg_time: 0.2 Cop_backoff_rpcEinsteinDB_p90_time: 0.2
   200  # Mem_max: 2333
   201  # Disk_max: 6666
   202  # Prepared: true
   203  # Causet_from_cache: true
   204  # Has_more_results: true
   205  # KV_total: 10
   206  # FIDel_total: 11
   207  # Backoff_total: 12
   208  # Write_sql_response_total: 1
   209  # Succ: true`
   210  	allegrosql := "select * from t;"
   211  	_, digest := BerolinaSQL.NormalizeDigest(allegrosql)
   212  	logItems := &variable.SlowQueryLogItems{
   213  		TxnTS:                    txnTS,
   214  		ALLEGROALLEGROSQL:        allegrosql,
   215  		Digest:                   digest,
   216  		TimeTotal:                costTime,
   217  		TimeParse:                time.Duration(10),
   218  		TimeCompile:              time.Duration(10),
   219  		TimeOptimize:             time.Duration(10),
   220  		TimeWaitTS:               time.Duration(3),
   221  		IndexNames:               "[t1:a,t2:b]",
   222  		StatsInfos:               statsInfos,
   223  		CausetTasks:              CausetTasks,
   224  		InterDircDetail:          execDetail,
   225  		MemMax:                   memMax,
   226  		DiskMax:                  diskMax,
   227  		Prepared:                 true,
   228  		CausetFromCache:          true,
   229  		HasMoreResults:           true,
   230  		KVTotal:                  10 * time.Second,
   231  		FIDelTotal:               11 * time.Second,
   232  		BackoffTotal:             12 * time.Second,
   233  		WriteALLEGROSQLRespTotal: 1 * time.Second,
   234  		Succ:                     true,
   235  		RewriteInfo: variable.RewritePhaseInfo{
   236  			DurationRewrite:            3,
   237  			DurationPreprocessSubQuery: 2,
   238  			PreprocessSubQueries:       2,
   239  		},
   240  		InterDircRetryCount: 3,
   241  		InterDircRetryTime:  5*time.Second + time.Millisecond*100,
   242  	}
   243  	logString := seVar.SlowLogFormat(logItems)
   244  	c.Assert(logString, Equals, resultFields+"\n"+allegrosql)
   245  
   246  	seVar.CurrentDBChanged = true
   247  	logString = seVar.SlowLogFormat(logItems)
   248  	c.Assert(logString, Equals, resultFields+"\n"+"use test;\n"+allegrosql)
   249  	c.Assert(seVar.CurrentDBChanged, IsFalse)
   250  }
   251  
   252  func (*testStochastikSuite) TestIsolationRead(c *C) {
   253  	defer config.RestoreFunc()()
   254  	config.UFIDelateGlobal(func(conf *config.Config) {
   255  		conf.IsolationRead.Engines = []string{"tiflash", "milevadb"}
   256  	})
   257  	sessVars := variable.NewStochastikVars()
   258  	_, ok := sessVars.IsolationReadEngines[ekv.MilevaDB]
   259  	c.Assert(ok, Equals, true)
   260  	_, ok = sessVars.IsolationReadEngines[ekv.EinsteinDB]
   261  	c.Assert(ok, Equals, false)
   262  	_, ok = sessVars.IsolationReadEngines[ekv.TiFlash]
   263  	c.Assert(ok, Equals, true)
   264  }