github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/table_split_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 dbs_test
    15  
    16  import (
    17  	"context"
    18  	"sync/atomic"
    19  	"time"
    20  
    21  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/milevadb/blockcodec"
    24  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb"
    25  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    26  	"github.com/whtcorpsinc/milevadb/dbs"
    27  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    28  	"github.com/whtcorpsinc/milevadb/stochastik"
    29  )
    30  
    31  type testDBSBlockSplitSuite struct{}
    32  
    33  var _ = Suite(&testDBSBlockSplitSuite{})
    34  
    35  func (s *testDBSBlockSplitSuite) TestBlockSplit(c *C) {
    36  	causetstore, err := mockstore.NewMockStore()
    37  	c.Assert(err, IsNil)
    38  	defer causetstore.Close()
    39  	stochastik.SetSchemaLease(100 * time.Millisecond)
    40  	stochastik.DisableStats4Test()
    41  	atomic.StoreUint32(&dbs.EnableSplitBlockRegion, 1)
    42  	dom, err := stochastik.BootstrapStochastik(causetstore)
    43  	c.Assert(err, IsNil)
    44  	tk := testkit.NewTestKit(c, causetstore)
    45  	tk.MustInterDirc("use test")
    46  	// Synced split causet region.
    47  	tk.MustInterDirc("set global milevadb_scatter_region = 1")
    48  	tk.MustInterDirc(`create causet t_part (a int key) partition by range(a) (
    49  		partition p0 values less than (10),
    50  		partition p1 values less than (20)
    51  	)`)
    52  	defer dom.Close()
    53  	atomic.StoreUint32(&dbs.EnableSplitBlockRegion, 0)
    54  	schemaReplicant := dom.SchemaReplicant()
    55  	c.Assert(schemaReplicant, NotNil)
    56  	t, err := schemaReplicant.BlockByName(perceptron.NewCIStr("allegrosql"), perceptron.NewCIStr("milevadb"))
    57  	c.Assert(err, IsNil)
    58  	checkRegionStartWithBlockID(c, t.Meta().ID, causetstore.(ekvStore))
    59  
    60  	t, err = schemaReplicant.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t_part"))
    61  	c.Assert(err, IsNil)
    62  	pi := t.Meta().GetPartitionInfo()
    63  	c.Assert(pi, NotNil)
    64  	for _, def := range pi.Definitions {
    65  		checkRegionStartWithBlockID(c, def.ID, causetstore.(ekvStore))
    66  	}
    67  }
    68  
    69  type ekvStore interface {
    70  	GetRegionCache() *einsteindb.RegionCache
    71  }
    72  
    73  func checkRegionStartWithBlockID(c *C, id int64, causetstore ekvStore) {
    74  	regionStartKey := blockcodec.EncodeBlockPrefix(id)
    75  	var loc *einsteindb.KeyLocation
    76  	var err error
    77  	cache := causetstore.GetRegionCache()
    78  	loc, err = cache.LocateKey(einsteindb.NewBackoffer(context.Background(), 5000), regionStartKey)
    79  	c.Assert(err, IsNil)
    80  	// Region cache may be out of date, so we need to drop this expired region and load it again.
    81  	cache.InvalidateCachedRegion(loc.Region)
    82  	c.Assert([]byte(loc.StartKey), BytesEquals, []byte(regionStartKey))
    83  }