github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/request_builder_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 allegrosql
    15  
    16  import (
    17  	"os"
    18  	"testing"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/fidelpb/go-fidelpb"
    23  	"github.com/whtcorpsinc/milevadb/blockcodec"
    24  	"github.com/whtcorpsinc/milevadb/ekv"
    25  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    26  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    27  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    28  	"github.com/whtcorpsinc/milevadb/soliton/disk"
    29  	"github.com/whtcorpsinc/milevadb/soliton/logutil"
    30  	"github.com/whtcorpsinc/milevadb/soliton/memory"
    31  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    32  	"github.com/whtcorpsinc/milevadb/soliton/ranger"
    33  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    34  	"github.com/whtcorpsinc/milevadb/statistics"
    35  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    36  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    37  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    38  	"github.com/whtcorpsinc/milevadb/types"
    39  )
    40  
    41  var _ = Suite(&testSuite{})
    42  
    43  func TestT(t *testing.T) {
    44  	CustomVerboseFlag = true
    45  	logLevel := os.Getenv("log_level")
    46  	logutil.InitLogger(logutil.NewLogConfig(logLevel, logutil.DefaultLogFormat, "", logutil.EmptyFileLogConfig, false))
    47  	TestingT(t)
    48  }
    49  
    50  var _ = Suite(&testSuite{})
    51  
    52  type testSuite struct {
    53  	sctx stochastikctx.Context
    54  }
    55  
    56  func (s *testSuite) SetUpSuite(c *C) {
    57  	ctx := mock.NewContext()
    58  	ctx.GetStochastikVars().StmtCtx = &stmtctx.StatementContext{
    59  		MemTracker:  memory.NewTracker(-1, -1),
    60  		DiskTracker: disk.NewTracker(-1, -1),
    61  	}
    62  	ctx.CausetStore = &mock.CausetStore{
    63  		Client: &mock.Client{
    64  			MockResponse: &mockResponse{
    65  				ctx:   ctx,
    66  				batch: 1,
    67  				total: 2,
    68  			},
    69  		},
    70  	}
    71  	s.sctx = ctx
    72  }
    73  
    74  func (s *testSuite) TearDownSuite(c *C) {
    75  }
    76  
    77  func (s *testSuite) SetUpTest(c *C) {
    78  	testleak.BeforeTest()
    79  	ctx := s.sctx.(*mock.Context)
    80  	causetstore := ctx.CausetStore.(*mock.CausetStore)
    81  	causetstore.Client = &mock.Client{
    82  		MockResponse: &mockResponse{
    83  			ctx:   ctx,
    84  			batch: 1,
    85  			total: 2,
    86  		},
    87  	}
    88  }
    89  
    90  func (s *testSuite) TearDownTest(c *C) {
    91  	testleak.AfterTest(c)()
    92  }
    93  
    94  type handleRange struct {
    95  	start int64
    96  	end   int64
    97  }
    98  
    99  func (s *testSuite) getExpectedRanges(tid int64, hrs []*handleRange) []ekv.KeyRange {
   100  	krs := make([]ekv.KeyRange, 0, len(hrs))
   101  	for _, hr := range hrs {
   102  		low := codec.EncodeInt(nil, hr.start)
   103  		high := codec.EncodeInt(nil, hr.end)
   104  		high = ekv.Key(high).PrefixNext()
   105  		startKey := blockcodec.EncodeRowKey(tid, low)
   106  		endKey := blockcodec.EncodeRowKey(tid, high)
   107  		krs = append(krs, ekv.KeyRange{StartKey: startKey, EndKey: endKey})
   108  	}
   109  	return krs
   110  }
   111  
   112  func (s *testSuite) TestBlockHandlesToKVRanges(c *C) {
   113  	handles := []ekv.Handle{ekv.IntHandle(0), ekv.IntHandle(2), ekv.IntHandle(3), ekv.IntHandle(4), ekv.IntHandle(5),
   114  		ekv.IntHandle(10), ekv.IntHandle(11), ekv.IntHandle(100), ekv.IntHandle(9223372036854775806), ekv.IntHandle(9223372036854775807)}
   115  
   116  	// Build expected key ranges.
   117  	hrs := make([]*handleRange, 0, len(handles))
   118  	hrs = append(hrs, &handleRange{start: 0, end: 0})
   119  	hrs = append(hrs, &handleRange{start: 2, end: 5})
   120  	hrs = append(hrs, &handleRange{start: 10, end: 11})
   121  	hrs = append(hrs, &handleRange{start: 100, end: 100})
   122  	hrs = append(hrs, &handleRange{start: 9223372036854775806, end: 9223372036854775807})
   123  
   124  	// Build key ranges.
   125  	expect := s.getExpectedRanges(1, hrs)
   126  	actual := BlockHandlesToKVRanges(1, handles)
   127  
   128  	// Compare key ranges and expected key ranges.
   129  	c.Assert(len(actual), Equals, len(expect))
   130  	for i := range actual {
   131  		c.Assert(actual[i].StartKey, DeepEquals, expect[i].StartKey)
   132  		c.Assert(actual[i].EndKey, DeepEquals, expect[i].EndKey)
   133  	}
   134  }
   135  
   136  func (s *testSuite) TestBlockRangesToKVRanges(c *C) {
   137  	ranges := []*ranger.Range{
   138  		{
   139  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   140  			HighVal: []types.Causet{types.NewIntCauset(2)},
   141  		},
   142  		{
   143  			LowVal:      []types.Causet{types.NewIntCauset(2)},
   144  			HighVal:     []types.Causet{types.NewIntCauset(4)},
   145  			LowExclude:  true,
   146  			HighExclude: true,
   147  		},
   148  		{
   149  			LowVal:      []types.Causet{types.NewIntCauset(4)},
   150  			HighVal:     []types.Causet{types.NewIntCauset(19)},
   151  			HighExclude: true,
   152  		},
   153  		{
   154  			LowVal:     []types.Causet{types.NewIntCauset(19)},
   155  			HighVal:    []types.Causet{types.NewIntCauset(32)},
   156  			LowExclude: true,
   157  		},
   158  		{
   159  			LowVal:     []types.Causet{types.NewIntCauset(34)},
   160  			HighVal:    []types.Causet{types.NewIntCauset(34)},
   161  			LowExclude: true,
   162  		},
   163  	}
   164  
   165  	actual := BlockRangesToKVRanges(13, ranges, nil)
   166  	expect := []ekv.KeyRange{
   167  		{
   168  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   169  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   170  		},
   171  		{
   172  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   173  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   174  		},
   175  		{
   176  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   177  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x13},
   178  		},
   179  		{
   180  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14},
   181  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21},
   182  		},
   183  		{
   184  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   185  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   186  		},
   187  	}
   188  	for i := 0; i < len(actual); i++ {
   189  		c.Assert(actual[i], DeepEquals, expect[i])
   190  	}
   191  }
   192  
   193  func (s *testSuite) TestIndexRangesToKVRanges(c *C) {
   194  	ranges := []*ranger.Range{
   195  		{
   196  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   197  			HighVal: []types.Causet{types.NewIntCauset(2)},
   198  		},
   199  		{
   200  			LowVal:      []types.Causet{types.NewIntCauset(2)},
   201  			HighVal:     []types.Causet{types.NewIntCauset(4)},
   202  			LowExclude:  true,
   203  			HighExclude: true,
   204  		},
   205  		{
   206  			LowVal:      []types.Causet{types.NewIntCauset(4)},
   207  			HighVal:     []types.Causet{types.NewIntCauset(19)},
   208  			HighExclude: true,
   209  		},
   210  		{
   211  			LowVal:     []types.Causet{types.NewIntCauset(19)},
   212  			HighVal:    []types.Causet{types.NewIntCauset(32)},
   213  			LowExclude: true,
   214  		},
   215  		{
   216  			LowVal:     []types.Causet{types.NewIntCauset(34)},
   217  			HighVal:    []types.Causet{types.NewIntCauset(34)},
   218  			LowExclude: true,
   219  		},
   220  	}
   221  
   222  	expect := []ekv.KeyRange{
   223  		{
   224  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   225  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   226  		},
   227  		{
   228  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   229  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   230  		},
   231  		{
   232  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   233  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x13},
   234  		},
   235  		{
   236  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14},
   237  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21},
   238  		},
   239  		{
   240  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   241  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   242  		},
   243  	}
   244  
   245  	actual, err := IndexRangesToKVRanges(new(stmtctx.StatementContext), 12, 15, ranges, nil)
   246  	c.Assert(err, IsNil)
   247  	for i := range actual {
   248  		c.Assert(actual[i], DeepEquals, expect[i])
   249  	}
   250  }
   251  
   252  func (s *testSuite) TestRequestBuilder1(c *C) {
   253  	ranges := []*ranger.Range{
   254  		{
   255  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   256  			HighVal: []types.Causet{types.NewIntCauset(2)},
   257  		},
   258  		{
   259  			LowVal:      []types.Causet{types.NewIntCauset(2)},
   260  			HighVal:     []types.Causet{types.NewIntCauset(4)},
   261  			LowExclude:  true,
   262  			HighExclude: true,
   263  		},
   264  		{
   265  			LowVal:      []types.Causet{types.NewIntCauset(4)},
   266  			HighVal:     []types.Causet{types.NewIntCauset(19)},
   267  			HighExclude: true,
   268  		},
   269  		{
   270  			LowVal:     []types.Causet{types.NewIntCauset(19)},
   271  			HighVal:    []types.Causet{types.NewIntCauset(32)},
   272  			LowExclude: true,
   273  		},
   274  		{
   275  			LowVal:     []types.Causet{types.NewIntCauset(34)},
   276  			HighVal:    []types.Causet{types.NewIntCauset(34)},
   277  			LowExclude: true,
   278  		},
   279  	}
   280  
   281  	actual, err := (&RequestBuilder{}).SetBlockRanges(12, ranges, nil).
   282  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   283  		SetDesc(false).
   284  		SetKeepOrder(false).
   285  		SetFromStochastikVars(variable.NewStochastikVars()).
   286  		Build()
   287  	c.Assert(err, IsNil)
   288  	expect := &ekv.Request{
   289  		Tp:      103,
   290  		StartTs: 0x0,
   291  		Data:    []uint8{0x18, 0x0, 0x20, 0x0, 0x40, 0x0, 0x5a, 0x0},
   292  		KeyRanges: []ekv.KeyRange{
   293  			{
   294  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   295  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   296  			},
   297  			{
   298  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   299  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   300  			},
   301  			{
   302  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   303  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x13},
   304  			},
   305  			{
   306  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14},
   307  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21},
   308  			},
   309  			{
   310  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   311  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   312  			},
   313  		},
   314  		Cacheable:      true,
   315  		KeepOrder:      false,
   316  		Desc:           false,
   317  		Concurrency:    variable.DefDistALLEGROSQLScanConcurrency,
   318  		IsolationLevel: 0,
   319  		Priority:       0,
   320  		NotFillCache:   false,
   321  		SyncLog:        false,
   322  		Streaming:      false,
   323  		ReplicaRead:    ekv.ReplicaReadLeader,
   324  	}
   325  	c.Assert(actual, DeepEquals, expect)
   326  }
   327  
   328  func (s *testSuite) TestRequestBuilder2(c *C) {
   329  	ranges := []*ranger.Range{
   330  		{
   331  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   332  			HighVal: []types.Causet{types.NewIntCauset(2)},
   333  		},
   334  		{
   335  			LowVal:      []types.Causet{types.NewIntCauset(2)},
   336  			HighVal:     []types.Causet{types.NewIntCauset(4)},
   337  			LowExclude:  true,
   338  			HighExclude: true,
   339  		},
   340  		{
   341  			LowVal:      []types.Causet{types.NewIntCauset(4)},
   342  			HighVal:     []types.Causet{types.NewIntCauset(19)},
   343  			HighExclude: true,
   344  		},
   345  		{
   346  			LowVal:     []types.Causet{types.NewIntCauset(19)},
   347  			HighVal:    []types.Causet{types.NewIntCauset(32)},
   348  			LowExclude: true,
   349  		},
   350  		{
   351  			LowVal:     []types.Causet{types.NewIntCauset(34)},
   352  			HighVal:    []types.Causet{types.NewIntCauset(34)},
   353  			LowExclude: true,
   354  		},
   355  	}
   356  
   357  	actual, err := (&RequestBuilder{}).SetIndexRanges(new(stmtctx.StatementContext), 12, 15, ranges).
   358  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   359  		SetDesc(false).
   360  		SetKeepOrder(false).
   361  		SetFromStochastikVars(variable.NewStochastikVars()).
   362  		Build()
   363  	c.Assert(err, IsNil)
   364  	expect := &ekv.Request{
   365  		Tp:      103,
   366  		StartTs: 0x0,
   367  		Data:    []uint8{0x18, 0x0, 0x20, 0x0, 0x40, 0x0, 0x5a, 0x0},
   368  		KeyRanges: []ekv.KeyRange{
   369  			{
   370  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   371  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   372  			},
   373  			{
   374  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3},
   375  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   376  			},
   377  			{
   378  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4},
   379  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x13},
   380  			},
   381  			{
   382  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14},
   383  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21},
   384  			},
   385  			{
   386  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   387  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x23},
   388  			},
   389  		},
   390  		Cacheable:      true,
   391  		KeepOrder:      false,
   392  		Desc:           false,
   393  		Concurrency:    variable.DefDistALLEGROSQLScanConcurrency,
   394  		IsolationLevel: 0,
   395  		Priority:       0,
   396  		NotFillCache:   false,
   397  		SyncLog:        false,
   398  		Streaming:      false,
   399  		ReplicaRead:    ekv.ReplicaReadLeader,
   400  	}
   401  	c.Assert(actual, DeepEquals, expect)
   402  }
   403  
   404  func (s *testSuite) TestRequestBuilder3(c *C) {
   405  	handles := []ekv.Handle{ekv.IntHandle(0), ekv.IntHandle(2), ekv.IntHandle(3), ekv.IntHandle(4),
   406  		ekv.IntHandle(5), ekv.IntHandle(10), ekv.IntHandle(11), ekv.IntHandle(100)}
   407  
   408  	actual, err := (&RequestBuilder{}).SetBlockHandles(15, handles).
   409  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   410  		SetDesc(false).
   411  		SetKeepOrder(false).
   412  		SetFromStochastikVars(variable.NewStochastikVars()).
   413  		Build()
   414  	c.Assert(err, IsNil)
   415  	expect := &ekv.Request{
   416  		Tp:      103,
   417  		StartTs: 0x0,
   418  		Data:    []uint8{0x18, 0x0, 0x20, 0x0, 0x40, 0x0, 0x5a, 0x0},
   419  		KeyRanges: []ekv.KeyRange{
   420  			{
   421  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   422  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   423  			},
   424  			{
   425  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
   426  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6},
   427  			},
   428  			{
   429  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa},
   430  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc},
   431  			},
   432  			{
   433  				StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64},
   434  				EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x65},
   435  			},
   436  		},
   437  		Cacheable:      true,
   438  		KeepOrder:      false,
   439  		Desc:           false,
   440  		Concurrency:    variable.DefDistALLEGROSQLScanConcurrency,
   441  		IsolationLevel: 0,
   442  		Priority:       0,
   443  		NotFillCache:   false,
   444  		SyncLog:        false,
   445  		Streaming:      false,
   446  		ReplicaRead:    ekv.ReplicaReadLeader,
   447  	}
   448  	c.Assert(actual, DeepEquals, expect)
   449  }
   450  
   451  func (s *testSuite) TestRequestBuilder4(c *C) {
   452  	keyRanges := []ekv.KeyRange{
   453  		{
   454  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   455  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   456  		},
   457  		{
   458  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
   459  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6},
   460  		},
   461  		{
   462  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa},
   463  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc},
   464  		},
   465  		{
   466  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64},
   467  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x65},
   468  		},
   469  	}
   470  
   471  	actual, err := (&RequestBuilder{}).SetKeyRanges(keyRanges).
   472  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   473  		SetDesc(false).
   474  		SetKeepOrder(false).
   475  		SetStreaming(true).
   476  		SetFromStochastikVars(variable.NewStochastikVars()).
   477  		Build()
   478  	c.Assert(err, IsNil)
   479  	expect := &ekv.Request{
   480  		Tp:             103,
   481  		StartTs:        0x0,
   482  		Data:           []uint8{0x18, 0x0, 0x20, 0x0, 0x40, 0x0, 0x5a, 0x0},
   483  		KeyRanges:      keyRanges,
   484  		Cacheable:      true,
   485  		KeepOrder:      false,
   486  		Desc:           false,
   487  		Concurrency:    variable.DefDistALLEGROSQLScanConcurrency,
   488  		IsolationLevel: 0,
   489  		Priority:       0,
   490  		Streaming:      true,
   491  		NotFillCache:   false,
   492  		SyncLog:        false,
   493  		ReplicaRead:    ekv.ReplicaReadLeader,
   494  	}
   495  	c.Assert(actual, DeepEquals, expect)
   496  }
   497  
   498  func (s *testSuite) TestRequestBuilder5(c *C) {
   499  	keyRanges := []ekv.KeyRange{
   500  		{
   501  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
   502  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   503  		},
   504  		{
   505  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
   506  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6},
   507  		},
   508  		{
   509  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa},
   510  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc},
   511  		},
   512  		{
   513  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64},
   514  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x65},
   515  		},
   516  	}
   517  
   518  	actual, err := (&RequestBuilder{}).SetKeyRanges(keyRanges).
   519  		SetAnalyzeRequest(&fidelpb.AnalyzeReq{}).
   520  		SetKeepOrder(true).
   521  		SetConcurrency(15).
   522  		Build()
   523  	c.Assert(err, IsNil)
   524  	expect := &ekv.Request{
   525  		Tp:             104,
   526  		StartTs:        0x0,
   527  		Data:           []uint8{0x8, 0x0, 0x18, 0x0, 0x20, 0x0},
   528  		KeyRanges:      keyRanges,
   529  		KeepOrder:      true,
   530  		Desc:           false,
   531  		Concurrency:    15,
   532  		IsolationLevel: ekv.RC,
   533  		Priority:       1,
   534  		NotFillCache:   true,
   535  		SyncLog:        false,
   536  		Streaming:      false,
   537  	}
   538  	c.Assert(actual, DeepEquals, expect)
   539  }
   540  
   541  func (s *testSuite) TestRequestBuilder6(c *C) {
   542  	keyRanges := []ekv.KeyRange{
   543  		{
   544  			StartKey: ekv.Key{0x00, 0x01},
   545  			EndKey:   ekv.Key{0x02, 0x03},
   546  		},
   547  	}
   548  
   549  	concurrency := 10
   550  
   551  	actual, err := (&RequestBuilder{}).SetKeyRanges(keyRanges).
   552  		SetChecksumRequest(&fidelpb.ChecksumRequest{}).
   553  		SetConcurrency(concurrency).
   554  		Build()
   555  	c.Assert(err, IsNil)
   556  
   557  	expect := &ekv.Request{
   558  		Tp:             105,
   559  		StartTs:        0x0,
   560  		Data:           []uint8{0x10, 0x0, 0x18, 0x0},
   561  		KeyRanges:      keyRanges,
   562  		KeepOrder:      false,
   563  		Desc:           false,
   564  		Concurrency:    concurrency,
   565  		IsolationLevel: 0,
   566  		Priority:       0,
   567  		NotFillCache:   true,
   568  		SyncLog:        false,
   569  		Streaming:      false,
   570  	}
   571  
   572  	c.Assert(actual, DeepEquals, expect)
   573  }
   574  
   575  func (s *testSuite) TestRequestBuilder7(c *C) {
   576  	vars := variable.NewStochastikVars()
   577  	vars.SetReplicaRead(ekv.ReplicaReadFollower)
   578  
   579  	concurrency := 10
   580  
   581  	actual, err := (&RequestBuilder{}).
   582  		SetFromStochastikVars(vars).
   583  		SetConcurrency(concurrency).
   584  		Build()
   585  	c.Assert(err, IsNil)
   586  
   587  	expect := &ekv.Request{
   588  		Tp:             0,
   589  		StartTs:        0x0,
   590  		KeepOrder:      false,
   591  		Desc:           false,
   592  		Concurrency:    concurrency,
   593  		IsolationLevel: 0,
   594  		Priority:       0,
   595  		NotFillCache:   false,
   596  		SyncLog:        false,
   597  		Streaming:      false,
   598  		ReplicaRead:    ekv.ReplicaReadFollower,
   599  	}
   600  
   601  	c.Assert(actual, DeepEquals, expect)
   602  }
   603  
   604  func (s *testSuite) TestRequestBuilder8(c *C) {
   605  	sv := variable.NewStochastikVars()
   606  	sv.SnapshotschemaReplicant = schemareplicant.MockSchemaReplicantWithSchemaVer(nil, 10000)
   607  	actual, err := (&RequestBuilder{}).
   608  		SetFromStochastikVars(sv).
   609  		Build()
   610  	c.Assert(err, IsNil)
   611  	expect := &ekv.Request{
   612  		Tp:             0,
   613  		StartTs:        0x0,
   614  		Data:           []uint8(nil),
   615  		Concurrency:    variable.DefDistALLEGROSQLScanConcurrency,
   616  		IsolationLevel: 0,
   617  		Priority:       0,
   618  		MemTracker:     (*memory.Tracker)(nil),
   619  		ReplicaRead:    0x1,
   620  		SchemaVar:      10000,
   621  	}
   622  	c.Assert(actual, DeepEquals, expect)
   623  }
   624  
   625  func (s *testSuite) TestBlockRangesToKVRangesWithFbs(c *C) {
   626  	ranges := []*ranger.Range{
   627  		{
   628  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   629  			HighVal: []types.Causet{types.NewIntCauset(4)},
   630  		},
   631  	}
   632  	hist := statistics.NewHistogram(1, 30, 30, 0, types.NewFieldType(allegrosql.TypeLonglong), chunk.InitialCapacity, 0)
   633  	for i := 0; i < 10; i++ {
   634  		hist.Bounds.AppendInt64(0, int64(i))
   635  		hist.Bounds.AppendInt64(0, int64(i+2))
   636  		hist.Buckets = append(hist.Buckets, statistics.Bucket{Repeat: 10, Count: int64(i + 30)})
   637  	}
   638  	fb := statistics.NewQueryFeedback(0, hist, 0, false)
   639  	lower, upper := types.NewIntCauset(2), types.NewIntCauset(3)
   640  	fb.Feedback = []statistics.Feedback{
   641  		{Lower: &lower, Upper: &upper, Count: 1, Repeat: 1},
   642  	}
   643  	actual := BlockRangesToKVRanges(0, ranges, fb)
   644  	expect := []ekv.KeyRange{
   645  		{
   646  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   647  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x72, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5},
   648  		},
   649  	}
   650  	for i := 0; i < len(actual); i++ {
   651  		c.Assert(actual[i], DeepEquals, expect[i])
   652  	}
   653  }
   654  
   655  func (s *testSuite) TestIndexRangesToKVRangesWithFbs(c *C) {
   656  	ranges := []*ranger.Range{
   657  		{
   658  			LowVal:  []types.Causet{types.NewIntCauset(1)},
   659  			HighVal: []types.Causet{types.NewIntCauset(4)},
   660  		},
   661  	}
   662  	hist := statistics.NewHistogram(1, 30, 30, 0, types.NewFieldType(allegrosql.TypeLonglong), chunk.InitialCapacity, 0)
   663  	for i := 0; i < 10; i++ {
   664  		hist.Bounds.AppendInt64(0, int64(i))
   665  		hist.Bounds.AppendInt64(0, int64(i+2))
   666  		hist.Buckets = append(hist.Buckets, statistics.Bucket{Repeat: 10, Count: int64(i + 30)})
   667  	}
   668  	fb := statistics.NewQueryFeedback(0, hist, 0, false)
   669  	lower, upper := types.NewIntCauset(2), types.NewIntCauset(3)
   670  	fb.Feedback = []statistics.Feedback{
   671  		{Lower: &lower, Upper: &upper, Count: 1, Repeat: 1},
   672  	}
   673  	actual, err := IndexRangesToKVRanges(new(stmtctx.StatementContext), 0, 0, ranges, fb)
   674  	c.Assert(err, IsNil)
   675  	expect := []ekv.KeyRange{
   676  		{
   677  			StartKey: ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
   678  			EndKey:   ekv.Key{0x74, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x69, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5},
   679  		},
   680  	}
   681  	for i := 0; i < len(actual); i++ {
   682  		c.Assert(actual[i], DeepEquals, expect[i])
   683  	}
   684  }
   685  
   686  func (s *testSuite) TestScanLimitConcurrency(c *C) {
   687  	vars := variable.NewStochastikVars()
   688  	for _, tt := range []struct {
   689  		tp          fidelpb.InterDircType
   690  		limit       uint64
   691  		concurrency int
   692  	}{
   693  		{fidelpb.InterDircType_TypeBlockScan, 1, 1},
   694  		{fidelpb.InterDircType_TypeIndexScan, 1, 1},
   695  		{fidelpb.InterDircType_TypeBlockScan, 1000000, vars.Concurrency.DistALLEGROSQLScanConcurrency()},
   696  		{fidelpb.InterDircType_TypeIndexScan, 1000000, vars.Concurrency.DistALLEGROSQLScanConcurrency()},
   697  	} {
   698  		firstInterDirc := &fidelpb.InterlockingDirectorate{Tp: tt.tp}
   699  		switch tt.tp {
   700  		case fidelpb.InterDircType_TypeBlockScan:
   701  			firstInterDirc.TblScan = &fidelpb.BlockScan{}
   702  		case fidelpb.InterDircType_TypeIndexScan:
   703  			firstInterDirc.IdxScan = &fidelpb.IndexScan{}
   704  		}
   705  		limitInterDirc := &fidelpb.InterlockingDirectorate{Tp: fidelpb.InterDircType_TypeLimit, Limit: &fidelpb.Limit{Limit: tt.limit}}
   706  		posetPosetDag := &fidelpb.PosetDagRequest{InterlockingDirectorates: []*fidelpb.InterlockingDirectorate{firstInterDirc, limitInterDirc}}
   707  		actual, err := (&RequestBuilder{}).
   708  			SetPosetDagRequest(posetPosetDag).
   709  			SetFromStochastikVars(vars).
   710  			Build()
   711  		c.Assert(err, IsNil)
   712  		c.Assert(actual.Concurrency, Equals, tt.concurrency)
   713  	}
   714  }