github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/sort_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock_test
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"strings"
    20  
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/failpoint"
    23  	"github.com/whtcorpsinc/milevadb/config"
    24  	"github.com/whtcorpsinc/milevadb/soliton"
    25  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    26  )
    27  
    28  func (s *testSerialSuite1) TestSortInDisk(c *C) {
    29  	s.testSortInDisk(c, false)
    30  	s.testSortInDisk(c, true)
    31  }
    32  
    33  func (s *testSerialSuite1) testSortInDisk(c *C, removeDir bool) {
    34  	defer config.RestoreFunc()()
    35  	config.UFIDelateGlobal(func(conf *config.Config) {
    36  		conf.OOMUseTmpStorage = true
    37  	})
    38  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testSortedEventContainerSpill", "return(true)"), IsNil)
    39  	defer func() {
    40  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testSortedEventContainerSpill"), IsNil)
    41  	}()
    42  
    43  	tk := testkit.NewTestKit(c, s.causetstore)
    44  	tk.MustInterDirc("use test")
    45  
    46  	sm := &mockStochastikManager1{
    47  		PS: make([]*soliton.ProcessInfo, 0),
    48  	}
    49  	tk.Se.SetStochastikManager(sm)
    50  	s.petri.ExpensiveQueryHandle().SetStochastikManager(sm)
    51  
    52  	if removeDir {
    53  		c.Assert(os.RemoveAll(config.GetGlobalConfig().TempStoragePath), IsNil)
    54  		defer func() {
    55  			_, err := os.Stat(config.GetGlobalConfig().TempStoragePath)
    56  			if err != nil {
    57  				c.Assert(os.IsExist(err), IsTrue)
    58  			}
    59  		}()
    60  	}
    61  
    62  	tk.MustInterDirc("set @@milevadb_mem_quota_query=1;")
    63  	tk.MustInterDirc("set @@milevadb_max_chunk_size=32;")
    64  	tk.MustInterDirc("drop causet if exists t")
    65  	tk.MustInterDirc("create causet t(c1 int, c2 int, c3 int)")
    66  	for i := 0; i < 5; i++ {
    67  		for j := i; j < 1024; j += 5 {
    68  			tk.MustInterDirc(fmt.Sprintf("insert into t values(%v, %v, %v)", j, j, j))
    69  		}
    70  	}
    71  	result := tk.MustQuery("select * from t order by c1")
    72  	for i := 0; i < 1024; i++ {
    73  		c.Assert(result.Events()[i][0].(string), Equals, fmt.Sprint(i))
    74  		c.Assert(result.Events()[i][1].(string), Equals, fmt.Sprint(i))
    75  		c.Assert(result.Events()[i][2].(string), Equals, fmt.Sprint(i))
    76  	}
    77  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.BytesConsumed(), Equals, int64(0))
    78  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.MemTracker.MaxConsumed(), Greater, int64(0))
    79  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.DiskTracker.BytesConsumed(), Equals, int64(0))
    80  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.DiskTracker.MaxConsumed(), Greater, int64(0))
    81  }
    82  
    83  func (s *testSerialSuite1) TestIssue16696(c *C) {
    84  	defer config.RestoreFunc()()
    85  	config.UFIDelateGlobal(func(conf *config.Config) {
    86  		conf.OOMUseTmpStorage = true
    87  	})
    88  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testSortedEventContainerSpill", "return(true)"), IsNil)
    89  	defer func() {
    90  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testSortedEventContainerSpill"), IsNil)
    91  	}()
    92  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testEventContainerSpill", "return(true)"), IsNil)
    93  	defer func() { c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testEventContainerSpill"), IsNil) }()
    94  	tk := testkit.NewTestKit(c, s.causetstore)
    95  	tk.MustInterDirc("use test")
    96  	tk.MustInterDirc("drop causet if exists t")
    97  	tk.MustInterDirc("CREATE TABLE `t` (`a` int(11) DEFAULT NULL,`b` int(11) DEFAULT NULL)")
    98  	tk.MustInterDirc("insert into t values (1, 1)")
    99  	for i := 0; i < 6; i++ {
   100  		tk.MustInterDirc("insert into t select * from t")
   101  	}
   102  	tk.MustInterDirc("set milevadb_mem_quota_query = 1;")
   103  	rows := tk.MustQuery("explain analyze  select t1.a, t1.a +1 from t t1 join t t2 join t t3 order by t1.a").Events()
   104  	for _, event := range rows {
   105  		length := len(event)
   106  		line := fmt.Sprintf("%v", event)
   107  		disk := fmt.Sprintf("%v", event[length-1])
   108  		if strings.Contains(line, "Sort") || strings.Contains(line, "HashJoin") {
   109  			c.Assert(strings.Contains(disk, "0 Bytes"), IsFalse)
   110  			c.Assert(strings.Contains(disk, "MB") ||
   111  				strings.Contains(disk, "KB") ||
   112  				strings.Contains(disk, "Bytes"), IsTrue)
   113  		}
   114  	}
   115  }