github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/stochastikctx/variable/varsutil_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
    15  
    16  import (
    17  	"encoding/json"
    18  	"reflect"
    19  	"time"
    20  
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    23  	. "github.com/whtcorpsinc/check"
    24  	"github.com/whtcorpsinc/milevadb/config"
    25  	"github.com/whtcorpsinc/milevadb/ekv"
    26  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    27  	"github.com/whtcorpsinc/milevadb/types"
    28  )
    29  
    30  var _ = Suite(&testVarsutilSuite{})
    31  
    32  type testVarsutilSuite struct {
    33  }
    34  
    35  func (s *testVarsutilSuite) TestMilevaDBOptOn(c *C) {
    36  	defer testleak.AfterTest(c)()
    37  	tbl := []struct {
    38  		val string
    39  		on  bool
    40  	}{
    41  		{"ON", true},
    42  		{"on", true},
    43  		{"On", true},
    44  		{"1", true},
    45  		{"off", false},
    46  		{"No", false},
    47  		{"0", false},
    48  		{"1.1", false},
    49  		{"", false},
    50  	}
    51  	for _, t := range tbl {
    52  		on := MilevaDBOptOn(t.val)
    53  		c.Assert(on, Equals, t.on)
    54  	}
    55  }
    56  
    57  func (s *testVarsutilSuite) TestNewStochastikVars(c *C) {
    58  	defer testleak.AfterTest(c)()
    59  	vars := NewStochastikVars()
    60  
    61  	c.Assert(vars.IndexJoinBatchSize, Equals, DefIndexJoinBatchSize)
    62  	c.Assert(vars.IndexLookupSize, Equals, DefIndexLookupSize)
    63  	c.Assert(vars.indexLookupConcurrency, Equals, ConcurrencyUnset)
    64  	c.Assert(vars.indexSerialScanConcurrency, Equals, DefIndexSerialScanConcurrency)
    65  	c.Assert(vars.indexLookupJoinConcurrency, Equals, ConcurrencyUnset)
    66  	c.Assert(vars.hashJoinConcurrency, Equals, DefMilevaDBHashJoinConcurrency)
    67  	c.Assert(vars.IndexLookupConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    68  	c.Assert(vars.IndexSerialScanConcurrency(), Equals, DefIndexSerialScanConcurrency)
    69  	c.Assert(vars.IndexLookupJoinConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    70  	c.Assert(vars.HashJoinConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    71  	c.Assert(vars.AllowBatchCop, Equals, DefMilevaDBAllowBatchCop)
    72  	c.Assert(vars.AllowBCJ, Equals, DefOptBCJ)
    73  	c.Assert(vars.projectionConcurrency, Equals, ConcurrencyUnset)
    74  	c.Assert(vars.hashAggPartialConcurrency, Equals, ConcurrencyUnset)
    75  	c.Assert(vars.hashAggFinalConcurrency, Equals, ConcurrencyUnset)
    76  	c.Assert(vars.windowConcurrency, Equals, ConcurrencyUnset)
    77  	c.Assert(vars.distALLEGROSQLScanConcurrency, Equals, DefDistALLEGROSQLScanConcurrency)
    78  	c.Assert(vars.ProjectionConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    79  	c.Assert(vars.HashAggPartialConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    80  	c.Assert(vars.HashAggFinalConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    81  	c.Assert(vars.WindowConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
    82  	c.Assert(vars.DistALLEGROSQLScanConcurrency(), Equals, DefDistALLEGROSQLScanConcurrency)
    83  	c.Assert(vars.InterlockingDirectorateConcurrency, Equals, DefInterlockingDirectorateConcurrency)
    84  	c.Assert(vars.MaxChunkSize, Equals, DefMaxChunkSize)
    85  	c.Assert(vars.DMLBatchSize, Equals, DefDMLBatchSize)
    86  	c.Assert(vars.MemQuotaQuery, Equals, config.GetGlobalConfig().MemQuotaQuery)
    87  	c.Assert(vars.MemQuotaHashJoin, Equals, int64(DefMilevaDBMemQuotaHashJoin))
    88  	c.Assert(vars.MemQuotaMergeJoin, Equals, int64(DefMilevaDBMemQuotaMergeJoin))
    89  	c.Assert(vars.MemQuotaSort, Equals, int64(DefMilevaDBMemQuotaSort))
    90  	c.Assert(vars.MemQuotaTopn, Equals, int64(DefMilevaDBMemQuotaTopn))
    91  	c.Assert(vars.MemQuotaIndexLookupReader, Equals, int64(DefMilevaDBMemQuotaIndexLookupReader))
    92  	c.Assert(vars.MemQuotaIndexLookupJoin, Equals, int64(DefMilevaDBMemQuotaIndexLookupJoin))
    93  	c.Assert(vars.MemQuotaNestedLoopApply, Equals, int64(DefMilevaDBMemQuotaNestedLoopApply))
    94  	c.Assert(vars.EnableRadixJoin, Equals, DefMilevaDBUseRadixJoin)
    95  	c.Assert(vars.AllowWriteRowID, Equals, DefOptWriteRowID)
    96  	c.Assert(vars.MilevaDBOptJoinReorderThreshold, Equals, DefMilevaDBOptJoinReorderThreshold)
    97  	c.Assert(vars.EnableFastAnalyze, Equals, DefMilevaDBUseFastAnalyze)
    98  	c.Assert(vars.FoundInCausetCache, Equals, DefMilevaDBFoundInCausetCache)
    99  	c.Assert(vars.AllowAutoRandExplicitInsert, Equals, DefMilevaDBAllowAutoRandExplicitInsert)
   100  	c.Assert(vars.ShardAllocateStep, Equals, int64(DefMilevaDBShardAllocateStep))
   101  	c.Assert(vars.EnableChangeDeferredCausetType, Equals, DefMilevaDBChangeDeferredCausetType)
   102  
   103  	assertFieldsGreaterThanZero(c, reflect.ValueOf(vars.MemQuota))
   104  	assertFieldsGreaterThanZero(c, reflect.ValueOf(vars.BatchSize))
   105  }
   106  
   107  func assertFieldsGreaterThanZero(c *C, val reflect.Value) {
   108  	for i := 0; i < val.NumField(); i++ {
   109  		fieldVal := val.Field(i)
   110  		c.Assert(fieldVal.Int(), Greater, int64(0))
   111  	}
   112  }
   113  
   114  func (s *testVarsutilSuite) TestVarsutil(c *C) {
   115  	defer testleak.AfterTest(c)()
   116  	v := NewStochastikVars()
   117  	v.GlobalVarsAccessor = NewMockGlobalAccessor()
   118  
   119  	err := SetStochastikSystemVar(v, "autocommit", types.NewStringCauset("1"))
   120  	c.Assert(err, IsNil)
   121  	val, err := GetStochastikSystemVar(v, "autocommit")
   122  	c.Assert(err, IsNil)
   123  	c.Assert(val, Equals, "1")
   124  	c.Assert(SetStochastikSystemVar(v, "autocommit", types.Causet{}), NotNil)
   125  
   126  	// 0 converts to OFF
   127  	err = SetStochastikSystemVar(v, "foreign_key_checks", types.NewStringCauset("0"))
   128  	c.Assert(err, IsNil)
   129  	val, err = GetStochastikSystemVar(v, "foreign_key_checks")
   130  	c.Assert(err, IsNil)
   131  	c.Assert(val, Equals, "OFF")
   132  
   133  	// 1/ON is not supported (generates a warning and sets to OFF)
   134  	err = SetStochastikSystemVar(v, "foreign_key_checks", types.NewStringCauset("1"))
   135  	c.Assert(err, IsNil)
   136  	val, err = GetStochastikSystemVar(v, "foreign_key_checks")
   137  	c.Assert(err, IsNil)
   138  	c.Assert(val, Equals, "OFF")
   139  
   140  	err = SetStochastikSystemVar(v, "sql_mode", types.NewStringCauset("strict_trans_blocks"))
   141  	c.Assert(err, IsNil)
   142  	val, err = GetStochastikSystemVar(v, "sql_mode")
   143  	c.Assert(err, IsNil)
   144  	c.Assert(val, Equals, "STRICT_TRANS_TABLES")
   145  	c.Assert(v.StrictALLEGROSQLMode, IsTrue)
   146  	SetStochastikSystemVar(v, "sql_mode", types.NewStringCauset(""))
   147  	c.Assert(v.StrictALLEGROSQLMode, IsFalse)
   148  
   149  	err = SetStochastikSystemVar(v, "character_set_connection", types.NewStringCauset("utf8"))
   150  	c.Assert(err, IsNil)
   151  	err = SetStochastikSystemVar(v, "defCauslation_connection", types.NewStringCauset("utf8_general_ci"))
   152  	c.Assert(err, IsNil)
   153  	charset, defCauslation := v.GetCharsetInfo()
   154  	c.Assert(charset, Equals, "utf8")
   155  	c.Assert(defCauslation, Equals, "utf8_general_ci")
   156  
   157  	c.Assert(SetStochastikSystemVar(v, "character_set_results", types.Causet{}), IsNil)
   158  
   159  	// Test case for time_zone stochastik variable.
   160  	tests := []struct {
   161  		input        string
   162  		expect       string
   163  		compareValue bool
   164  		diff         time.Duration
   165  		err          error
   166  	}{
   167  		{"Europe/Helsinki", "Europe/Helsinki", true, -2 * time.Hour, nil},
   168  		{"US/Eastern", "US/Eastern", true, 5 * time.Hour, nil},
   169  		//TODO: Check it out and reopen this case.
   170  		//{"SYSTEM", "Local", false, 0},
   171  		{"+10:00", "", true, -10 * time.Hour, nil},
   172  		{"-6:00", "", true, 6 * time.Hour, nil},
   173  		{"+14:00", "", true, -14 * time.Hour, nil},
   174  		{"-12:59", "", true, 12*time.Hour + 59*time.Minute, nil},
   175  		{"+14:01", "", false, -14 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("+14:01")},
   176  		{"-13:00", "", false, 13 * time.Hour, ErrUnknownTimeZone.GenWithStackByArgs("-13:00")},
   177  	}
   178  	for _, tt := range tests {
   179  		err = SetStochastikSystemVar(v, TimeZone, types.NewStringCauset(tt.input))
   180  		if tt.err != nil {
   181  			c.Assert(err, NotNil)
   182  			continue
   183  		}
   184  
   185  		c.Assert(err, IsNil)
   186  		c.Assert(v.TimeZone.String(), Equals, tt.expect)
   187  		if tt.compareValue {
   188  			SetStochastikSystemVar(v, TimeZone, types.NewStringCauset(tt.input))
   189  			t1 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
   190  			t2 := time.Date(2000, 1, 1, 0, 0, 0, 0, v.TimeZone)
   191  			c.Assert(t2.Sub(t1), Equals, tt.diff)
   192  		}
   193  	}
   194  	err = SetStochastikSystemVar(v, TimeZone, types.NewStringCauset("6:00"))
   195  	c.Assert(err, NotNil)
   196  	c.Assert(terror.ErrorEqual(err, ErrUnknownTimeZone), IsTrue)
   197  
   198  	// Test case for allegrosql mode.
   199  	for str, mode := range allegrosql.Str2ALLEGROSQLMode {
   200  		SetStochastikSystemVar(v, "sql_mode", types.NewStringCauset(str))
   201  		if modeParts, exists := allegrosql.CombinationALLEGROSQLMode[str]; exists {
   202  			for _, part := range modeParts {
   203  				mode |= allegrosql.Str2ALLEGROSQLMode[part]
   204  			}
   205  		}
   206  		c.Assert(v.ALLEGROSQLMode, Equals, mode)
   207  	}
   208  
   209  	err = SetStochastikSystemVar(v, "milevadb_opt_broadcast_join", types.NewStringCauset("1"))
   210  	c.Assert(err, IsNil)
   211  	err = SetStochastikSystemVar(v, "milevadb_allow_batch_cop", types.NewStringCauset("0"))
   212  	c.Assert(terror.ErrorEqual(err, ErrWrongValueForVar), IsTrue)
   213  	err = SetStochastikSystemVar(v, "milevadb_opt_broadcast_join", types.NewStringCauset("0"))
   214  	c.Assert(err, IsNil)
   215  	err = SetStochastikSystemVar(v, "milevadb_allow_batch_cop", types.NewStringCauset("0"))
   216  	c.Assert(err, IsNil)
   217  	err = SetStochastikSystemVar(v, "milevadb_opt_broadcast_join", types.NewStringCauset("1"))
   218  	c.Assert(terror.ErrorEqual(err, ErrWrongValueForVar), IsTrue)
   219  
   220  	// Combined sql_mode
   221  	SetStochastikSystemVar(v, "sql_mode", types.NewStringCauset("REAL_AS_FLOAT,ANSI_QUOTES"))
   222  	c.Assert(v.ALLEGROSQLMode, Equals, allegrosql.ModeRealAsFloat|allegrosql.ModeANSIQuotes)
   223  
   224  	// Test case for milevadb_index_serial_scan_concurrency.
   225  	c.Assert(v.IndexSerialScanConcurrency(), Equals, DefIndexSerialScanConcurrency)
   226  	SetStochastikSystemVar(v, MilevaDBIndexSerialScanConcurrency, types.NewStringCauset("4"))
   227  	c.Assert(v.IndexSerialScanConcurrency(), Equals, 4)
   228  
   229  	// Test case for milevadb_batch_insert.
   230  	c.Assert(v.BatchInsert, IsFalse)
   231  	SetStochastikSystemVar(v, MilevaDBBatchInsert, types.NewStringCauset("1"))
   232  	c.Assert(v.BatchInsert, IsTrue)
   233  
   234  	c.Assert(v.InitChunkSize, Equals, 32)
   235  	c.Assert(v.MaxChunkSize, Equals, 1024)
   236  	err = SetStochastikSystemVar(v, MilevaDBMaxChunkSize, types.NewStringCauset("2"))
   237  	c.Assert(err, NotNil)
   238  	err = SetStochastikSystemVar(v, MilevaDBInitChunkSize, types.NewStringCauset("1024"))
   239  	c.Assert(err, NotNil)
   240  
   241  	// Test case for MilevaDBConfig stochastik variable.
   242  	err = SetStochastikSystemVar(v, MilevaDBConfig, types.NewStringCauset("abc"))
   243  	c.Assert(terror.ErrorEqual(err, ErrReadOnly), IsTrue)
   244  	val, err = GetStochastikSystemVar(v, MilevaDBConfig)
   245  	c.Assert(err, IsNil)
   246  	bVal, err := json.MarshalIndent(config.GetGlobalConfig(), "", "\t")
   247  	c.Assert(err, IsNil)
   248  	c.Assert(val, Equals, string(bVal))
   249  
   250  	SetStochastikSystemVar(v, MilevaDBEnableStreaming, types.NewStringCauset("1"))
   251  	val, err = GetStochastikSystemVar(v, MilevaDBEnableStreaming)
   252  	c.Assert(err, IsNil)
   253  	c.Assert(val, Equals, "1")
   254  	c.Assert(v.EnableStreaming, Equals, true)
   255  	SetStochastikSystemVar(v, MilevaDBEnableStreaming, types.NewStringCauset("0"))
   256  	val, err = GetStochastikSystemVar(v, MilevaDBEnableStreaming)
   257  	c.Assert(err, IsNil)
   258  	c.Assert(val, Equals, "0")
   259  	c.Assert(v.EnableStreaming, Equals, false)
   260  
   261  	c.Assert(v.OptimizerSelectivityLevel, Equals, DefMilevaDBOptimizerSelectivityLevel)
   262  	SetStochastikSystemVar(v, MilevaDBOptimizerSelectivityLevel, types.NewIntCauset(1))
   263  	c.Assert(v.OptimizerSelectivityLevel, Equals, 1)
   264  
   265  	err = SetStochastikSystemVar(v, MilevaDBDBSReorgWorkerCount, types.NewIntCauset(-1))
   266  	c.Assert(terror.ErrorEqual(err, ErrWrongValueForVar), IsTrue)
   267  
   268  	SetStochastikSystemVar(v, MilevaDBDBSReorgWorkerCount, types.NewIntCauset(int64(maxDBSReorgWorkerCount)+1))
   269  	c.Assert(terror.ErrorEqual(err, ErrWrongValueForVar), IsTrue)
   270  
   271  	err = SetStochastikSystemVar(v, MilevaDBRetryLimit, types.NewStringCauset("3"))
   272  	c.Assert(err, IsNil)
   273  	val, err = GetStochastikSystemVar(v, MilevaDBRetryLimit)
   274  	c.Assert(err, IsNil)
   275  	c.Assert(val, Equals, "3")
   276  	c.Assert(v.RetryLimit, Equals, int64(3))
   277  
   278  	c.Assert(v.EnableBlockPartition, Equals, "")
   279  	err = SetStochastikSystemVar(v, MilevaDBEnableBlockPartition, types.NewStringCauset("on"))
   280  	c.Assert(err, IsNil)
   281  	val, err = GetStochastikSystemVar(v, MilevaDBEnableBlockPartition)
   282  	c.Assert(err, IsNil)
   283  	c.Assert(val, Equals, "on")
   284  	c.Assert(v.EnableBlockPartition, Equals, "on")
   285  
   286  	c.Assert(v.MilevaDBOptJoinReorderThreshold, Equals, DefMilevaDBOptJoinReorderThreshold)
   287  	err = SetStochastikSystemVar(v, MilevaDBOptJoinReorderThreshold, types.NewIntCauset(5))
   288  	c.Assert(err, IsNil)
   289  	val, err = GetStochastikSystemVar(v, MilevaDBOptJoinReorderThreshold)
   290  	c.Assert(err, IsNil)
   291  	c.Assert(val, Equals, "5")
   292  	c.Assert(v.MilevaDBOptJoinReorderThreshold, Equals, 5)
   293  
   294  	err = SetStochastikSystemVar(v, MilevaDBCheckMb4ValueInUTF8, types.NewStringCauset("1"))
   295  	c.Assert(err, IsNil)
   296  	val, err = GetStochastikSystemVar(v, MilevaDBCheckMb4ValueInUTF8)
   297  	c.Assert(err, IsNil)
   298  	c.Assert(val, Equals, "1")
   299  	c.Assert(config.GetGlobalConfig().CheckMb4ValueInUTF8, Equals, true)
   300  	err = SetStochastikSystemVar(v, MilevaDBCheckMb4ValueInUTF8, types.NewStringCauset("0"))
   301  	c.Assert(err, IsNil)
   302  	val, err = GetStochastikSystemVar(v, MilevaDBCheckMb4ValueInUTF8)
   303  	c.Assert(err, IsNil)
   304  	c.Assert(val, Equals, "0")
   305  	c.Assert(config.GetGlobalConfig().CheckMb4ValueInUTF8, Equals, false)
   306  
   307  	SetStochastikSystemVar(v, MilevaDBLowResolutionTSO, types.NewStringCauset("1"))
   308  	val, err = GetStochastikSystemVar(v, MilevaDBLowResolutionTSO)
   309  	c.Assert(err, IsNil)
   310  	c.Assert(val, Equals, "1")
   311  	c.Assert(v.LowResolutionTSO, Equals, true)
   312  	SetStochastikSystemVar(v, MilevaDBLowResolutionTSO, types.NewStringCauset("0"))
   313  	val, err = GetStochastikSystemVar(v, MilevaDBLowResolutionTSO)
   314  	c.Assert(err, IsNil)
   315  	c.Assert(val, Equals, "0")
   316  	c.Assert(v.LowResolutionTSO, Equals, false)
   317  
   318  	c.Assert(v.CorrelationThreshold, Equals, 0.9)
   319  	err = SetStochastikSystemVar(v, MilevaDBOptCorrelationThreshold, types.NewStringCauset("0"))
   320  	c.Assert(err, IsNil)
   321  	val, err = GetStochastikSystemVar(v, MilevaDBOptCorrelationThreshold)
   322  	c.Assert(err, IsNil)
   323  	c.Assert(val, Equals, "0")
   324  	c.Assert(v.CorrelationThreshold, Equals, float64(0))
   325  
   326  	c.Assert(v.CPUFactor, Equals, 3.0)
   327  	err = SetStochastikSystemVar(v, MilevaDBOptCPUFactor, types.NewStringCauset("5.0"))
   328  	c.Assert(err, IsNil)
   329  	val, err = GetStochastikSystemVar(v, MilevaDBOptCPUFactor)
   330  	c.Assert(err, IsNil)
   331  	c.Assert(val, Equals, "5.0")
   332  	c.Assert(v.CPUFactor, Equals, 5.0)
   333  
   334  	c.Assert(v.CopCPUFactor, Equals, 3.0)
   335  	err = SetStochastikSystemVar(v, MilevaDBOptCopCPUFactor, types.NewStringCauset("5.0"))
   336  	c.Assert(err, IsNil)
   337  	val, err = GetStochastikSystemVar(v, MilevaDBOptCopCPUFactor)
   338  	c.Assert(err, IsNil)
   339  	c.Assert(val, Equals, "5.0")
   340  	c.Assert(v.CopCPUFactor, Equals, 5.0)
   341  
   342  	c.Assert(v.CopTiFlashConcurrencyFactor, Equals, 24.0)
   343  	err = SetStochastikSystemVar(v, MilevaDBOptTiFlashConcurrencyFactor, types.NewStringCauset("5.0"))
   344  	c.Assert(err, IsNil)
   345  	val, err = GetStochastikSystemVar(v, MilevaDBOptTiFlashConcurrencyFactor)
   346  	c.Assert(err, IsNil)
   347  	c.Assert(val, Equals, "5.0")
   348  	c.Assert(v.CopCPUFactor, Equals, 5.0)
   349  
   350  	c.Assert(v.NetworkFactor, Equals, 1.0)
   351  	err = SetStochastikSystemVar(v, MilevaDBOptNetworkFactor, types.NewStringCauset("3.0"))
   352  	c.Assert(err, IsNil)
   353  	val, err = GetStochastikSystemVar(v, MilevaDBOptNetworkFactor)
   354  	c.Assert(err, IsNil)
   355  	c.Assert(val, Equals, "3.0")
   356  	c.Assert(v.NetworkFactor, Equals, 3.0)
   357  
   358  	c.Assert(v.ScanFactor, Equals, 1.5)
   359  	err = SetStochastikSystemVar(v, MilevaDBOptScanFactor, types.NewStringCauset("3.0"))
   360  	c.Assert(err, IsNil)
   361  	val, err = GetStochastikSystemVar(v, MilevaDBOptScanFactor)
   362  	c.Assert(err, IsNil)
   363  	c.Assert(val, Equals, "3.0")
   364  	c.Assert(v.ScanFactor, Equals, 3.0)
   365  
   366  	c.Assert(v.DescScanFactor, Equals, 3.0)
   367  	err = SetStochastikSystemVar(v, MilevaDBOptDescScanFactor, types.NewStringCauset("5.0"))
   368  	c.Assert(err, IsNil)
   369  	val, err = GetStochastikSystemVar(v, MilevaDBOptDescScanFactor)
   370  	c.Assert(err, IsNil)
   371  	c.Assert(val, Equals, "5.0")
   372  	c.Assert(v.DescScanFactor, Equals, 5.0)
   373  
   374  	c.Assert(v.SeekFactor, Equals, 20.0)
   375  	err = SetStochastikSystemVar(v, MilevaDBOptSeekFactor, types.NewStringCauset("50.0"))
   376  	c.Assert(err, IsNil)
   377  	val, err = GetStochastikSystemVar(v, MilevaDBOptSeekFactor)
   378  	c.Assert(err, IsNil)
   379  	c.Assert(val, Equals, "50.0")
   380  	c.Assert(v.SeekFactor, Equals, 50.0)
   381  
   382  	c.Assert(v.MemoryFactor, Equals, 0.001)
   383  	err = SetStochastikSystemVar(v, MilevaDBOptMemoryFactor, types.NewStringCauset("1.0"))
   384  	c.Assert(err, IsNil)
   385  	val, err = GetStochastikSystemVar(v, MilevaDBOptMemoryFactor)
   386  	c.Assert(err, IsNil)
   387  	c.Assert(val, Equals, "1.0")
   388  	c.Assert(v.MemoryFactor, Equals, 1.0)
   389  
   390  	c.Assert(v.DiskFactor, Equals, 1.5)
   391  	err = SetStochastikSystemVar(v, MilevaDBOptDiskFactor, types.NewStringCauset("1.1"))
   392  	c.Assert(err, IsNil)
   393  	val, err = GetStochastikSystemVar(v, MilevaDBOptDiskFactor)
   394  	c.Assert(err, IsNil)
   395  	c.Assert(val, Equals, "1.1")
   396  	c.Assert(v.DiskFactor, Equals, 1.1)
   397  
   398  	c.Assert(v.ConcurrencyFactor, Equals, 3.0)
   399  	err = SetStochastikSystemVar(v, MilevaDBOptConcurrencyFactor, types.NewStringCauset("5.0"))
   400  	c.Assert(err, IsNil)
   401  	val, err = GetStochastikSystemVar(v, MilevaDBOptConcurrencyFactor)
   402  	c.Assert(err, IsNil)
   403  	c.Assert(val, Equals, "5.0")
   404  	c.Assert(v.ConcurrencyFactor, Equals, 5.0)
   405  
   406  	SetStochastikSystemVar(v, MilevaDBReplicaRead, types.NewStringCauset("follower"))
   407  	val, err = GetStochastikSystemVar(v, MilevaDBReplicaRead)
   408  	c.Assert(err, IsNil)
   409  	c.Assert(val, Equals, "follower")
   410  	c.Assert(v.GetReplicaRead(), Equals, ekv.ReplicaReadFollower)
   411  	SetStochastikSystemVar(v, MilevaDBReplicaRead, types.NewStringCauset("leader"))
   412  	val, err = GetStochastikSystemVar(v, MilevaDBReplicaRead)
   413  	c.Assert(err, IsNil)
   414  	c.Assert(val, Equals, "leader")
   415  	c.Assert(v.GetReplicaRead(), Equals, ekv.ReplicaReadLeader)
   416  	SetStochastikSystemVar(v, MilevaDBReplicaRead, types.NewStringCauset("leader-and-follower"))
   417  	val, err = GetStochastikSystemVar(v, MilevaDBReplicaRead)
   418  	c.Assert(err, IsNil)
   419  	c.Assert(val, Equals, "leader-and-follower")
   420  	c.Assert(v.GetReplicaRead(), Equals, ekv.ReplicaReadMixed)
   421  
   422  	err = SetStochastikSystemVar(v, MilevaDBEnableStmtSummary, types.NewStringCauset("on"))
   423  	c.Assert(err, IsNil)
   424  	val, err = GetStochastikSystemVar(v, MilevaDBEnableStmtSummary)
   425  	c.Assert(err, IsNil)
   426  	c.Assert(val, Equals, "1")
   427  
   428  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryRefreshInterval, types.NewStringCauset("10"))
   429  	c.Assert(err, IsNil)
   430  	val, err = GetStochastikSystemVar(v, MilevaDBStmtSummaryRefreshInterval)
   431  	c.Assert(err, IsNil)
   432  	c.Assert(val, Equals, "10")
   433  
   434  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryHistorySize, types.NewStringCauset("10"))
   435  	c.Assert(err, IsNil)
   436  	val, err = GetStochastikSystemVar(v, MilevaDBStmtSummaryHistorySize)
   437  	c.Assert(err, IsNil)
   438  	c.Assert(val, Equals, "10")
   439  
   440  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryMaxStmtCount, types.NewStringCauset("10"))
   441  	c.Assert(err, IsNil)
   442  	val, err = GetStochastikSystemVar(v, MilevaDBStmtSummaryMaxStmtCount)
   443  	c.Assert(err, IsNil)
   444  	c.Assert(val, Equals, "10")
   445  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryMaxStmtCount, types.NewStringCauset("a"))
   446  	c.Assert(err, ErrorMatches, ".*Incorrect argument type to variable 'milevadb_stmt_summary_max_stmt_count'")
   447  
   448  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryMaxALLEGROSQLLength, types.NewStringCauset("10"))
   449  	c.Assert(err, IsNil)
   450  	val, err = GetStochastikSystemVar(v, MilevaDBStmtSummaryMaxALLEGROSQLLength)
   451  	c.Assert(err, IsNil)
   452  	c.Assert(val, Equals, "10")
   453  	err = SetStochastikSystemVar(v, MilevaDBStmtSummaryMaxALLEGROSQLLength, types.NewStringCauset("a"))
   454  	c.Assert(err, ErrorMatches, ".*Incorrect argument type to variable 'milevadb_stmt_summary_max_sql_length'")
   455  
   456  	err = SetStochastikSystemVar(v, MilevaDBFoundInCausetCache, types.NewStringCauset("1"))
   457  	c.Assert(err, IsNil)
   458  	val, err = GetStochastikSystemVar(v, MilevaDBFoundInCausetCache)
   459  	c.Assert(err, IsNil)
   460  	c.Assert(val, Equals, "0")
   461  	c.Assert(v.systems[MilevaDBFoundInCausetCache], Equals, "1")
   462  
   463  	err = SetStochastikSystemVar(v, MilevaDBEnableChangeDeferredCausetType, types.NewStringCauset("on"))
   464  	c.Assert(err, IsNil)
   465  	val, err = GetStochastikSystemVar(v, MilevaDBEnableChangeDeferredCausetType)
   466  	c.Assert(err, IsNil)
   467  	c.Assert(val, Equals, "1")
   468  	c.Assert(v.systems[MilevaDBEnableChangeDeferredCausetType], Equals, "1")
   469  }
   470  
   471  func (s *testVarsutilSuite) TestSetOverflowBehave(c *C) {
   472  	ddRegWorker := maxDBSReorgWorkerCount + 1
   473  	SetDBSReorgWorkerCounter(ddRegWorker)
   474  	c.Assert(maxDBSReorgWorkerCount, Equals, GetDBSReorgWorkerCounter())
   475  
   476  	dbsReorgBatchSize := MaxDBSReorgBatchSize + 1
   477  	SetDBSReorgBatchSize(dbsReorgBatchSize)
   478  	c.Assert(MaxDBSReorgBatchSize, Equals, GetDBSReorgBatchSize())
   479  	dbsReorgBatchSize = MinDBSReorgBatchSize - 1
   480  	SetDBSReorgBatchSize(dbsReorgBatchSize)
   481  	c.Assert(MinDBSReorgBatchSize, Equals, GetDBSReorgBatchSize())
   482  
   483  	val := milevadbOptInt64("a", 1)
   484  	c.Assert(val, Equals, int64(1))
   485  	val2 := milevadbOptFloat64("b", 1.2)
   486  	c.Assert(val2, Equals, 1.2)
   487  }
   488  
   489  func (s *testVarsutilSuite) TestValidate(c *C) {
   490  	v := NewStochastikVars()
   491  	v.GlobalVarsAccessor = NewMockGlobalAccessor()
   492  	v.TimeZone = time.UTC
   493  
   494  	tests := []struct {
   495  		key   string
   496  		value string
   497  		error bool
   498  	}{
   499  		{MilevaDBAutoAnalyzeStartTime, "15:04", false},
   500  		{MilevaDBAutoAnalyzeStartTime, "15:04 -0700", false},
   501  		{DelayKeyWrite, "ON", false},
   502  		{DelayKeyWrite, "OFF", false},
   503  		{DelayKeyWrite, "ALL", false},
   504  		{DelayKeyWrite, "3", true},
   505  		{ForeignKeyChecks, "3", true},
   506  		{MaxSpRecursionDepth, "256", false},
   507  		{StochastikTrackGtids, "OFF", false},
   508  		{StochastikTrackGtids, "OWN_GTID", false},
   509  		{StochastikTrackGtids, "ALL_GTIDS", false},
   510  		{StochastikTrackGtids, "ON", true},
   511  		{EnforceGtidConsistency, "OFF", false},
   512  		{EnforceGtidConsistency, "ON", false},
   513  		{EnforceGtidConsistency, "WARN", false},
   514  		{QueryCacheType, "OFF", false},
   515  		{QueryCacheType, "ON", false},
   516  		{QueryCacheType, "DEMAND", false},
   517  		{QueryCacheType, "3", true},
   518  		{SecureAuth, "1", false},
   519  		{SecureAuth, "3", true},
   520  		{MyISAMUseMmap, "ON", false},
   521  		{MyISAMUseMmap, "OFF", false},
   522  		{MilevaDBEnableBlockPartition, "ON", false},
   523  		{MilevaDBEnableBlockPartition, "OFF", false},
   524  		{MilevaDBEnableBlockPartition, "AUTO", false},
   525  		{MilevaDBEnableBlockPartition, "UN", true},
   526  		{MilevaDBOptCorrelationExpFactor, "a", true},
   527  		{MilevaDBOptCorrelationExpFactor, "-10", true},
   528  		{MilevaDBOptCorrelationThreshold, "a", true},
   529  		{MilevaDBOptCorrelationThreshold, "-2", true},
   530  		{MilevaDBOptCPUFactor, "a", true},
   531  		{MilevaDBOptCPUFactor, "-2", true},
   532  		{MilevaDBOptTiFlashConcurrencyFactor, "-2", true},
   533  		{MilevaDBOptCopCPUFactor, "a", true},
   534  		{MilevaDBOptCopCPUFactor, "-2", true},
   535  		{MilevaDBOptNetworkFactor, "a", true},
   536  		{MilevaDBOptNetworkFactor, "-2", true},
   537  		{MilevaDBOptScanFactor, "a", true},
   538  		{MilevaDBOptScanFactor, "-2", true},
   539  		{MilevaDBOptDescScanFactor, "a", true},
   540  		{MilevaDBOptDescScanFactor, "-2", true},
   541  		{MilevaDBOptSeekFactor, "a", true},
   542  		{MilevaDBOptSeekFactor, "-2", true},
   543  		{MilevaDBOptMemoryFactor, "a", true},
   544  		{MilevaDBOptMemoryFactor, "-2", true},
   545  		{MilevaDBOptDiskFactor, "a", true},
   546  		{MilevaDBOptDiskFactor, "-2", true},
   547  		{MilevaDBOptConcurrencyFactor, "a", true},
   548  		{MilevaDBOptConcurrencyFactor, "-2", true},
   549  		{TxnIsolation, "READ-UNCOMMITTED", true},
   550  		{MilevaDBInitChunkSize, "a", true},
   551  		{MilevaDBInitChunkSize, "-1", true},
   552  		{MilevaDBMaxChunkSize, "a", true},
   553  		{MilevaDBMaxChunkSize, "-1", true},
   554  		{MilevaDBOptJoinReorderThreshold, "a", true},
   555  		{MilevaDBOptJoinReorderThreshold, "-1", true},
   556  		{MilevaDBReplicaRead, "invalid", true},
   557  		{MilevaDBTxnMode, "invalid", true},
   558  		{MilevaDBTxnMode, "pessimistic", false},
   559  		{MilevaDBTxnMode, "optimistic", false},
   560  		{MilevaDBTxnMode, "", false},
   561  		{MilevaDBIsolationReadEngines, "", true},
   562  		{MilevaDBIsolationReadEngines, "einsteindb", false},
   563  		{MilevaDBIsolationReadEngines, "EinsteinDB,tiflash", false},
   564  		{MilevaDBIsolationReadEngines, "   einsteindb,   tiflash  ", false},
   565  		{MilevaDBShardAllocateStep, "ad", true},
   566  		{MilevaDBShardAllocateStep, "-123", false},
   567  		{MilevaDBShardAllocateStep, "128", false},
   568  		{MilevaDBEnableAmendPessimisticTxn, "0", false},
   569  		{MilevaDBEnableAmendPessimisticTxn, "1", false},
   570  		{MilevaDBEnableAmendPessimisticTxn, "256", true},
   571  	}
   572  
   573  	for _, t := range tests {
   574  		_, err := ValidateSetSystemVar(v, t.key, t.value, ScopeGlobal)
   575  		if t.error {
   576  			c.Assert(err, NotNil, Commentf("%v got err=%v", t, err))
   577  		} else {
   578  			c.Assert(err, IsNil, Commentf("%v got err=%v", t, err))
   579  		}
   580  	}
   581  
   582  }
   583  
   584  func (s *testVarsutilSuite) TestValidateStmtSummary(c *C) {
   585  	v := NewStochastikVars()
   586  	v.GlobalVarsAccessor = NewMockGlobalAccessor()
   587  	v.TimeZone = time.UTC
   588  
   589  	tests := []struct {
   590  		key   string
   591  		value string
   592  		error bool
   593  		scope ScopeFlag
   594  	}{
   595  		{MilevaDBEnableStmtSummary, "a", true, ScopeStochastik},
   596  		{MilevaDBEnableStmtSummary, "-1", true, ScopeStochastik},
   597  		{MilevaDBEnableStmtSummary, "", false, ScopeStochastik},
   598  		{MilevaDBEnableStmtSummary, "", true, ScopeGlobal},
   599  		{MilevaDBStmtSummaryInternalQuery, "a", true, ScopeStochastik},
   600  		{MilevaDBStmtSummaryInternalQuery, "-1", true, ScopeStochastik},
   601  		{MilevaDBStmtSummaryInternalQuery, "", false, ScopeStochastik},
   602  		{MilevaDBStmtSummaryInternalQuery, "", true, ScopeGlobal},
   603  		{MilevaDBStmtSummaryRefreshInterval, "a", true, ScopeStochastik},
   604  		{MilevaDBStmtSummaryRefreshInterval, "", false, ScopeStochastik},
   605  		{MilevaDBStmtSummaryRefreshInterval, "", true, ScopeGlobal},
   606  		{MilevaDBStmtSummaryRefreshInterval, "0", true, ScopeGlobal},
   607  		{MilevaDBStmtSummaryRefreshInterval, "99999999999", true, ScopeGlobal},
   608  		{MilevaDBStmtSummaryHistorySize, "a", true, ScopeStochastik},
   609  		{MilevaDBStmtSummaryHistorySize, "", false, ScopeStochastik},
   610  		{MilevaDBStmtSummaryHistorySize, "", true, ScopeGlobal},
   611  		{MilevaDBStmtSummaryHistorySize, "0", false, ScopeGlobal},
   612  		{MilevaDBStmtSummaryHistorySize, "-1", true, ScopeGlobal},
   613  		{MilevaDBStmtSummaryHistorySize, "99999999", true, ScopeGlobal},
   614  		{MilevaDBStmtSummaryMaxStmtCount, "a", true, ScopeStochastik},
   615  		{MilevaDBStmtSummaryMaxStmtCount, "", false, ScopeStochastik},
   616  		{MilevaDBStmtSummaryMaxStmtCount, "", true, ScopeGlobal},
   617  		{MilevaDBStmtSummaryMaxStmtCount, "0", true, ScopeGlobal},
   618  		{MilevaDBStmtSummaryMaxStmtCount, "99999999", true, ScopeGlobal},
   619  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "a", true, ScopeStochastik},
   620  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "", false, ScopeStochastik},
   621  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "", true, ScopeGlobal},
   622  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "0", false, ScopeGlobal},
   623  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "-1", true, ScopeGlobal},
   624  		{MilevaDBStmtSummaryMaxALLEGROSQLLength, "99999999999", true, ScopeGlobal},
   625  	}
   626  
   627  	for _, t := range tests {
   628  		_, err := ValidateSetSystemVar(v, t.key, t.value, t.scope)
   629  		if t.error {
   630  			c.Assert(err, NotNil, Commentf("%v got err=%v", t, err))
   631  		} else {
   632  			c.Assert(err, IsNil, Commentf("%v got err=%v", t, err))
   633  		}
   634  	}
   635  }
   636  
   637  func (s *testVarsutilSuite) TestConcurrencyVariables(c *C) {
   638  	defer testleak.AfterTest(c)()
   639  	vars := NewStochastikVars()
   640  	vars.GlobalVarsAccessor = NewMockGlobalAccessor()
   641  
   642  	wdConcurrency := 2
   643  	c.Assert(vars.windowConcurrency, Equals, ConcurrencyUnset)
   644  	c.Assert(vars.WindowConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
   645  	err := SetStochastikSystemVar(vars, MilevaDBWindowConcurrency, types.NewIntCauset(int64(wdConcurrency)))
   646  	c.Assert(err, IsNil)
   647  	c.Assert(vars.windowConcurrency, Equals, wdConcurrency)
   648  	c.Assert(vars.WindowConcurrency(), Equals, wdConcurrency)
   649  
   650  	c.Assert(vars.indexLookupConcurrency, Equals, ConcurrencyUnset)
   651  	c.Assert(vars.IndexLookupConcurrency(), Equals, DefInterlockingDirectorateConcurrency)
   652  	exeConcurrency := DefInterlockingDirectorateConcurrency + 1
   653  	err = SetStochastikSystemVar(vars, MilevaDBInterlockingDirectorateConcurrency, types.NewIntCauset(int64(exeConcurrency)))
   654  	c.Assert(err, IsNil)
   655  	c.Assert(vars.indexLookupConcurrency, Equals, ConcurrencyUnset)
   656  	c.Assert(vars.IndexLookupConcurrency(), Equals, exeConcurrency)
   657  	c.Assert(vars.WindowConcurrency(), Equals, wdConcurrency)
   658  }