github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/allegrosql_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  	"context"
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/cznic/mathutil"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    25  	. "github.com/whtcorpsinc/check"
    26  	"github.com/whtcorpsinc/fidelpb/go-fidelpb"
    27  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb"
    28  	"github.com/whtcorpsinc/milevadb/ekv"
    29  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    30  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    31  	"github.com/whtcorpsinc/milevadb/soliton/execdetails"
    32  	"github.com/whtcorpsinc/milevadb/soliton/memory"
    33  	"github.com/whtcorpsinc/milevadb/statistics"
    34  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    35  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    36  	"github.com/whtcorpsinc/milevadb/types"
    37  )
    38  
    39  func (s *testSuite) createSelectNormal(batch, totalRows int, c *C, planIDs []int) (*selectResult, []*types.FieldType) {
    40  	request, err := (&RequestBuilder{}).SetKeyRanges(nil).
    41  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
    42  		SetDesc(false).
    43  		SetKeepOrder(false).
    44  		SetFromStochastikVars(variable.NewStochastikVars()).
    45  		SetMemTracker(memory.NewTracker(-1, -1)).
    46  		Build()
    47  	c.Assert(err, IsNil)
    48  
    49  	/// 4 int64 types.
    50  	defCausTypes := []*types.FieldType{
    51  		{
    52  			Tp:          allegrosql.TypeLonglong,
    53  			Flen:        allegrosql.MaxIntWidth,
    54  			Decimal:     0,
    55  			Flag:        allegrosql.BinaryFlag,
    56  			Charset:     charset.CharsetBin,
    57  			DefCauslate: charset.DefCauslationBin,
    58  		},
    59  	}
    60  	defCausTypes = append(defCausTypes, defCausTypes[0])
    61  	defCausTypes = append(defCausTypes, defCausTypes[0])
    62  	defCausTypes = append(defCausTypes, defCausTypes[0])
    63  
    64  	// Test Next.
    65  	var response SelectResult
    66  	if planIDs == nil {
    67  		response, err = Select(context.TODO(), s.sctx, request, defCausTypes, statistics.NewQueryFeedback(0, nil, 0, false))
    68  	} else {
    69  		response, err = SelectWithRuntimeStats(context.TODO(), s.sctx, request, defCausTypes, statistics.NewQueryFeedback(0, nil, 0, false), planIDs, 1)
    70  	}
    71  
    72  	c.Assert(err, IsNil)
    73  	result, ok := response.(*selectResult)
    74  	c.Assert(ok, IsTrue)
    75  	c.Assert(result.label, Equals, "posetPosetDag")
    76  	c.Assert(result.sqlType, Equals, "general")
    77  	c.Assert(result.rowLen, Equals, len(defCausTypes))
    78  
    79  	resp, ok := result.resp.(*mockResponse)
    80  	c.Assert(ok, IsTrue)
    81  	resp.total = totalRows
    82  	resp.batch = batch
    83  
    84  	return result, defCausTypes
    85  }
    86  
    87  func (s *testSuite) TestSelectNormal(c *C) {
    88  	response, defCausTypes := s.createSelectNormal(1, 2, c, nil)
    89  	response.Fetch(context.TODO())
    90  
    91  	// Test Next.
    92  	chk := chunk.New(defCausTypes, 32, 32)
    93  	numAllRows := 0
    94  	for {
    95  		err := response.Next(context.TODO(), chk)
    96  		c.Assert(err, IsNil)
    97  		numAllRows += chk.NumRows()
    98  		if chk.NumRows() == 0 {
    99  			break
   100  		}
   101  	}
   102  	c.Assert(numAllRows, Equals, 2)
   103  	err := response.Close()
   104  	c.Assert(err, IsNil)
   105  	c.Assert(response.memTracker.BytesConsumed(), Equals, int64(0))
   106  }
   107  
   108  func (s *testSuite) TestSelectMemTracker(c *C) {
   109  	response, defCausTypes := s.createSelectNormal(2, 6, c, nil)
   110  	response.Fetch(context.TODO())
   111  
   112  	// Test Next.
   113  	chk := chunk.New(defCausTypes, 3, 3)
   114  	err := response.Next(context.TODO(), chk)
   115  	c.Assert(err, IsNil)
   116  	c.Assert(chk.IsFull(), Equals, true)
   117  	err = response.Close()
   118  	c.Assert(err, IsNil)
   119  	c.Assert(response.memTracker.BytesConsumed(), Equals, int64(0))
   120  }
   121  
   122  func (s *testSuite) TestSelectNormalChunkSize(c *C) {
   123  	s.sctx.GetStochastikVars().EnableChunkRPC = false
   124  	response, defCausTypes := s.createSelectNormal(100, 1000000, c, nil)
   125  	response.Fetch(context.TODO())
   126  	s.testChunkSize(response, defCausTypes, c)
   127  	c.Assert(response.Close(), IsNil)
   128  	c.Assert(response.memTracker.BytesConsumed(), Equals, int64(0))
   129  }
   130  
   131  func (s *testSuite) TestSelectWithRuntimeStats(c *C) {
   132  	planIDs := []int{1, 2, 3}
   133  	response, defCausTypes := s.createSelectNormal(1, 2, c, planIDs)
   134  	if len(response.copCausetIDs) != len(planIDs) {
   135  		c.Fatal("invalid copCausetIDs")
   136  	}
   137  	for i := range planIDs {
   138  		if response.copCausetIDs[i] != planIDs[i] {
   139  			c.Fatal("invalid copCausetIDs")
   140  		}
   141  	}
   142  
   143  	response.Fetch(context.TODO())
   144  
   145  	// Test Next.
   146  	chk := chunk.New(defCausTypes, 32, 32)
   147  	numAllRows := 0
   148  	for {
   149  		err := response.Next(context.TODO(), chk)
   150  		c.Assert(err, IsNil)
   151  		numAllRows += chk.NumRows()
   152  		if chk.NumRows() == 0 {
   153  			break
   154  		}
   155  	}
   156  	c.Assert(numAllRows, Equals, 2)
   157  	err := response.Close()
   158  	c.Assert(err, IsNil)
   159  }
   160  
   161  func (s *testSuite) TestSelectResultRuntimeStats(c *C) {
   162  	basic := &execdetails.BasicRuntimeStats{}
   163  	basic.Record(time.Second, 20)
   164  	s1 := &selectResultRuntimeStats{
   165  		copRespTime:      []time.Duration{time.Second, time.Millisecond},
   166  		procKeys:         []int64{100, 200},
   167  		backoffSleep:     map[string]time.Duration{"RegionMiss": time.Millisecond},
   168  		totalProcessTime: time.Second,
   169  		totalWaitTime:    time.Second,
   170  		rpcStat:          einsteindb.NewRegionRequestRuntimeStats(),
   171  	}
   172  	s2 := *s1
   173  	stmtStats := execdetails.NewRuntimeStatsDefCausl()
   174  	stmtStats.RegisterStats(1, basic)
   175  	stmtStats.RegisterStats(1, s1)
   176  	stmtStats.RegisterStats(1, &s2)
   177  	stats := stmtStats.GetRootStats(1)
   178  	expect := "time:1s, loops:1, cop_task: {num: 4, max: 1s, min: 1ms, avg: 500.5ms, p95: 1s, max_proc_keys: 200, p95_proc_keys: 200, tot_proc: 2s, tot_wait: 2s, copr_cache_hit_ratio: 0.00}, backoff{RegionMiss: 2ms}"
   179  	c.Assert(stats.String(), Equals, expect)
   180  	// Test for idempotence.
   181  	c.Assert(stats.String(), Equals, expect)
   182  }
   183  
   184  func (s *testSuite) createSelectStreaming(batch, totalRows int, c *C) (*streamResult, []*types.FieldType) {
   185  	request, err := (&RequestBuilder{}).SetKeyRanges(nil).
   186  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   187  		SetDesc(false).
   188  		SetKeepOrder(false).
   189  		SetFromStochastikVars(variable.NewStochastikVars()).
   190  		SetStreaming(true).
   191  		Build()
   192  	c.Assert(err, IsNil)
   193  
   194  	/// 4 int64 types.
   195  	defCausTypes := []*types.FieldType{
   196  		{
   197  			Tp:          allegrosql.TypeLonglong,
   198  			Flen:        allegrosql.MaxIntWidth,
   199  			Decimal:     0,
   200  			Flag:        allegrosql.BinaryFlag,
   201  			Charset:     charset.CharsetBin,
   202  			DefCauslate: charset.DefCauslationBin,
   203  		},
   204  	}
   205  	defCausTypes = append(defCausTypes, defCausTypes[0])
   206  	defCausTypes = append(defCausTypes, defCausTypes[0])
   207  	defCausTypes = append(defCausTypes, defCausTypes[0])
   208  
   209  	s.sctx.GetStochastikVars().EnableStreaming = true
   210  
   211  	response, err := Select(context.TODO(), s.sctx, request, defCausTypes, statistics.NewQueryFeedback(0, nil, 0, false))
   212  	c.Assert(err, IsNil)
   213  	result, ok := response.(*streamResult)
   214  	c.Assert(ok, IsTrue)
   215  	c.Assert(result.rowLen, Equals, len(defCausTypes))
   216  
   217  	resp, ok := result.resp.(*mockResponse)
   218  	c.Assert(ok, IsTrue)
   219  	resp.total = totalRows
   220  	resp.batch = batch
   221  
   222  	return result, defCausTypes
   223  }
   224  
   225  func (s *testSuite) TestSelectStreaming(c *C) {
   226  	response, defCausTypes := s.createSelectStreaming(1, 2, c)
   227  	response.Fetch(context.TODO())
   228  
   229  	// Test Next.
   230  	chk := chunk.New(defCausTypes, 32, 32)
   231  	numAllRows := 0
   232  	for {
   233  		err := response.Next(context.TODO(), chk)
   234  		c.Assert(err, IsNil)
   235  		numAllRows += chk.NumRows()
   236  		if chk.NumRows() == 0 {
   237  			break
   238  		}
   239  	}
   240  	c.Assert(numAllRows, Equals, 2)
   241  	err := response.Close()
   242  	c.Assert(err, IsNil)
   243  }
   244  
   245  func (s *testSuite) TestSelectStreamingWithNextRaw(c *C) {
   246  	response, _ := s.createSelectStreaming(1, 2, c)
   247  	response.Fetch(context.TODO())
   248  	data, err := response.NextRaw(context.TODO())
   249  	c.Assert(err, IsNil)
   250  	c.Assert(len(data), Equals, 16)
   251  }
   252  
   253  func (s *testSuite) TestSelectStreamingChunkSize(c *C) {
   254  	response, defCausTypes := s.createSelectStreaming(100, 1000000, c)
   255  	response.Fetch(context.TODO())
   256  	s.testChunkSize(response, defCausTypes, c)
   257  	c.Assert(response.Close(), IsNil)
   258  }
   259  
   260  func (s *testSuite) testChunkSize(response SelectResult, defCausTypes []*types.FieldType, c *C) {
   261  	chk := chunk.New(defCausTypes, 32, 32)
   262  
   263  	err := response.Next(context.TODO(), chk)
   264  	c.Assert(err, IsNil)
   265  	c.Assert(chk.NumRows(), Equals, 32)
   266  
   267  	err = response.Next(context.TODO(), chk)
   268  	c.Assert(err, IsNil)
   269  	c.Assert(chk.NumRows(), Equals, 32)
   270  
   271  	chk.SetRequiredRows(1, 32)
   272  	err = response.Next(context.TODO(), chk)
   273  	c.Assert(err, IsNil)
   274  	c.Assert(chk.NumRows(), Equals, 1)
   275  
   276  	chk.SetRequiredRows(2, 32)
   277  	err = response.Next(context.TODO(), chk)
   278  	c.Assert(err, IsNil)
   279  	c.Assert(chk.NumRows(), Equals, 2)
   280  
   281  	chk.SetRequiredRows(17, 32)
   282  	err = response.Next(context.TODO(), chk)
   283  	c.Assert(err, IsNil)
   284  	c.Assert(chk.NumRows(), Equals, 17)
   285  
   286  	chk.SetRequiredRows(170, 32)
   287  	err = response.Next(context.TODO(), chk)
   288  	c.Assert(err, IsNil)
   289  	c.Assert(chk.NumRows(), Equals, 32)
   290  
   291  	chk.SetRequiredRows(32, 32)
   292  	err = response.Next(context.TODO(), chk)
   293  	c.Assert(err, IsNil)
   294  	c.Assert(chk.NumRows(), Equals, 32)
   295  
   296  	chk.SetRequiredRows(0, 32)
   297  	err = response.Next(context.TODO(), chk)
   298  	c.Assert(err, IsNil)
   299  	c.Assert(chk.NumRows(), Equals, 32)
   300  
   301  	chk.SetRequiredRows(-1, 32)
   302  	err = response.Next(context.TODO(), chk)
   303  	c.Assert(err, IsNil)
   304  	c.Assert(chk.NumRows(), Equals, 32)
   305  }
   306  
   307  func (s *testSuite) TestAnalyze(c *C) {
   308  	s.sctx.GetStochastikVars().EnableChunkRPC = false
   309  	request, err := (&RequestBuilder{}).SetKeyRanges(nil).
   310  		SetAnalyzeRequest(&fidelpb.AnalyzeReq{}).
   311  		SetKeepOrder(true).
   312  		Build()
   313  	c.Assert(err, IsNil)
   314  
   315  	response, err := Analyze(context.TODO(), s.sctx.GetClient(), request, ekv.DefaultVars, true)
   316  	c.Assert(err, IsNil)
   317  
   318  	result, ok := response.(*selectResult)
   319  	c.Assert(ok, IsTrue)
   320  	c.Assert(result.label, Equals, "analyze")
   321  	c.Assert(result.sqlType, Equals, "internal")
   322  
   323  	response.Fetch(context.TODO())
   324  
   325  	bytes, err := response.NextRaw(context.TODO())
   326  	c.Assert(err, IsNil)
   327  	c.Assert(len(bytes), Equals, 16)
   328  
   329  	err = response.Close()
   330  	c.Assert(err, IsNil)
   331  }
   332  
   333  func (s *testSuite) TestChecksum(c *C) {
   334  	s.sctx.GetStochastikVars().EnableChunkRPC = false
   335  	request, err := (&RequestBuilder{}).SetKeyRanges(nil).
   336  		SetChecksumRequest(&fidelpb.ChecksumRequest{}).
   337  		Build()
   338  	c.Assert(err, IsNil)
   339  
   340  	response, err := Checksum(context.TODO(), s.sctx.GetClient(), request, ekv.DefaultVars)
   341  	c.Assert(err, IsNil)
   342  
   343  	result, ok := response.(*selectResult)
   344  	c.Assert(ok, IsTrue)
   345  	c.Assert(result.label, Equals, "checksum")
   346  	c.Assert(result.sqlType, Equals, "general")
   347  
   348  	response.Fetch(context.TODO())
   349  
   350  	bytes, err := response.NextRaw(context.TODO())
   351  	c.Assert(err, IsNil)
   352  	c.Assert(len(bytes), Equals, 16)
   353  
   354  	err = response.Close()
   355  	c.Assert(err, IsNil)
   356  }
   357  
   358  // mockResponse implements ekv.Response interface.
   359  // Used only for test.
   360  type mockResponse struct {
   361  	count int
   362  	total int
   363  	batch int
   364  	ctx   stochastikctx.Context
   365  	sync.Mutex
   366  }
   367  
   368  // Close implements ekv.Response interface.
   369  func (resp *mockResponse) Close() error {
   370  	resp.Lock()
   371  	defer resp.Unlock()
   372  
   373  	resp.count = 0
   374  	return nil
   375  }
   376  
   377  // Next implements ekv.Response interface.
   378  func (resp *mockResponse) Next(ctx context.Context) (ekv.ResultSubset, error) {
   379  	resp.Lock()
   380  	defer resp.Unlock()
   381  
   382  	if resp.count >= resp.total {
   383  		return nil, nil
   384  	}
   385  	numRows := mathutil.Min(resp.batch, resp.total-resp.count)
   386  	resp.count += numRows
   387  
   388  	var chunks []fidelpb.Chunk
   389  	if !canUseChunkRPC(resp.ctx) {
   390  		causet := types.NewIntCauset(1)
   391  		bytes := make([]byte, 0, 100)
   392  		bytes, _ = codec.EncodeValue(nil, bytes, causet, causet, causet, causet)
   393  		chunks = make([]fidelpb.Chunk, numRows)
   394  		for i := range chunks {
   395  			chkData := make([]byte, len(bytes))
   396  			copy(chkData, bytes)
   397  			chunks[i] = fidelpb.Chunk{RowsData: chkData}
   398  		}
   399  	} else {
   400  		chunks = make([]fidelpb.Chunk, 0)
   401  		for numRows > 0 {
   402  			rows := mathutil.Min(numRows, 1024)
   403  			numRows -= rows
   404  
   405  			defCausTypes := make([]*types.FieldType, 4)
   406  			for i := 0; i < 4; i++ {
   407  				defCausTypes[i] = &types.FieldType{Tp: allegrosql.TypeLonglong}
   408  			}
   409  			chk := chunk.New(defCausTypes, numRows, numRows)
   410  
   411  			for rowOrdinal := 0; rowOrdinal < rows; rowOrdinal++ {
   412  				for defCausOrdinal := 0; defCausOrdinal < 4; defCausOrdinal++ {
   413  					chk.AppendInt64(defCausOrdinal, 123)
   414  				}
   415  			}
   416  
   417  			codec := chunk.NewCodec(defCausTypes)
   418  			buffer := codec.Encode(chk)
   419  			chunks = append(chunks, fidelpb.Chunk{RowsData: buffer})
   420  		}
   421  	}
   422  
   423  	respPB := &fidelpb.SelectResponse{
   424  		Chunks:       chunks,
   425  		OutputCounts: []int64{1},
   426  	}
   427  	if canUseChunkRPC(resp.ctx) {
   428  		respPB.EncodeType = fidelpb.EncodeType_TypeChunk
   429  	} else {
   430  		respPB.EncodeType = fidelpb.EncodeType_TypeDefault
   431  	}
   432  	respBytes, err := respPB.Marshal()
   433  	if err != nil {
   434  		panic(err)
   435  	}
   436  	return &mockResultSubset{respBytes}, nil
   437  }
   438  
   439  // mockResultSubset implements ekv.ResultSubset interface.
   440  // Used only for test.
   441  type mockResultSubset struct{ data []byte }
   442  
   443  // GetData implements ekv.ResultSubset interface.
   444  func (r *mockResultSubset) GetData() []byte { return r.data }
   445  
   446  // GetStartKey implements ekv.ResultSubset interface.
   447  func (r *mockResultSubset) GetStartKey() ekv.Key { return nil }
   448  
   449  // MemSize implements ekv.ResultSubset interface.
   450  func (r *mockResultSubset) MemSize() int64 { return int64(cap(r.data)) }
   451  
   452  // RespTime implements ekv.ResultSubset interface.
   453  func (r *mockResultSubset) RespTime() time.Duration { return 0 }
   454  
   455  func createSelectNormal(batch, totalRows int, ctx stochastikctx.Context) (*selectResult, []*types.FieldType) {
   456  	request, _ := (&RequestBuilder{}).SetKeyRanges(nil).
   457  		SetPosetDagRequest(&fidelpb.PosetDagRequest{}).
   458  		SetDesc(false).
   459  		SetKeepOrder(false).
   460  		SetFromStochastikVars(variable.NewStochastikVars()).
   461  		SetMemTracker(memory.NewTracker(-1, -1)).
   462  		Build()
   463  
   464  	/// 4 int64 types.
   465  	defCausTypes := []*types.FieldType{
   466  		{
   467  			Tp:          allegrosql.TypeLonglong,
   468  			Flen:        allegrosql.MaxIntWidth,
   469  			Decimal:     0,
   470  			Flag:        allegrosql.BinaryFlag,
   471  			Charset:     charset.CharsetBin,
   472  			DefCauslate: charset.DefCauslationBin,
   473  		},
   474  	}
   475  	defCausTypes = append(defCausTypes, defCausTypes[0])
   476  	defCausTypes = append(defCausTypes, defCausTypes[0])
   477  	defCausTypes = append(defCausTypes, defCausTypes[0])
   478  
   479  	// Test Next.
   480  	var response SelectResult
   481  	response, _ = Select(context.TODO(), ctx, request, defCausTypes, statistics.NewQueryFeedback(0, nil, 0, false))
   482  
   483  	result, _ := response.(*selectResult)
   484  	resp, _ := result.resp.(*mockResponse)
   485  	resp.total = totalRows
   486  	resp.batch = batch
   487  
   488  	return result, defCausTypes
   489  }
   490  
   491  func BenchmarkSelectResponseChunk_BigResponse(b *testing.B) {
   492  	for i := 0; i < b.N; i++ {
   493  		b.StopTimer()
   494  		s := &testSuite{}
   495  		s.SetUpSuite(nil)
   496  		s.sctx.GetStochastikVars().InitChunkSize = 32
   497  		s.sctx.GetStochastikVars().MaxChunkSize = 1024
   498  		selectResult, defCausTypes := createSelectNormal(4000, 20000, s.sctx)
   499  		selectResult.Fetch(context.TODO())
   500  		chk := chunk.NewChunkWithCapacity(defCausTypes, 1024)
   501  		b.StartTimer()
   502  		for {
   503  			err := selectResult.Next(context.TODO(), chk)
   504  			if err != nil {
   505  				panic(err)
   506  			}
   507  			if chk.NumRows() == 0 {
   508  				break
   509  			}
   510  			chk.Reset()
   511  		}
   512  		s.TearDownSuite(nil)
   513  	}
   514  }
   515  
   516  func BenchmarkSelectResponseChunk_SmallResponse(b *testing.B) {
   517  	for i := 0; i < b.N; i++ {
   518  		b.StopTimer()
   519  		s := &testSuite{}
   520  		s.SetUpSuite(nil)
   521  		s.sctx.GetStochastikVars().InitChunkSize = 32
   522  		s.sctx.GetStochastikVars().MaxChunkSize = 1024
   523  		selectResult, defCausTypes := createSelectNormal(32, 3200, s.sctx)
   524  		selectResult.Fetch(context.TODO())
   525  		chk := chunk.NewChunkWithCapacity(defCausTypes, 1024)
   526  		b.StartTimer()
   527  		for {
   528  			err := selectResult.Next(context.TODO(), chk)
   529  			if err != nil {
   530  				panic(err)
   531  			}
   532  			if chk.NumRows() == 0 {
   533  				break
   534  			}
   535  			chk.Reset()
   536  		}
   537  		s.TearDownSuite(nil)
   538  	}
   539  }