github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/2pc_slow_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  //go:build !race
    15  // +build !race
    16  
    17  package einsteindb
    18  
    19  import (
    20  	"context"
    21  	"sync/atomic"
    22  
    23  	. "github.com/whtcorpsinc/check"
    24  	"github.com/whtcorpsinc/milevadb/ekv"
    25  )
    26  
    27  // TestCommitMultipleRegions tests commit multiple regions.
    28  // The test takes too long under the race detector.
    29  func (s *testCommitterSuite) TestCommitMultipleRegions(c *C) {
    30  	m := make(map[string]string)
    31  	for i := 0; i < 100; i++ {
    32  		k, v := randKV(10, 10)
    33  		m[k] = v
    34  	}
    35  	s.mustCommit(c, m)
    36  
    37  	// Test big values.
    38  	m = make(map[string]string)
    39  	for i := 0; i < 50; i++ {
    40  		k, v := randKV(11, txnCommitBatchSize/7)
    41  		m[k] = v
    42  	}
    43  	s.mustCommit(c, m)
    44  }
    45  
    46  func (s *testTiclientSuite) TestSplitRegionIn2PC(c *C) {
    47  	if *WithEinsteinDB {
    48  		c.Skip("scatter will timeout with single node EinsteinDB")
    49  	}
    50  	const preSplitThresholdInTest = 500
    51  	old := atomic.LoadUint32(&preSplitDetectThreshold)
    52  	defer atomic.StoreUint32(&preSplitDetectThreshold, old)
    53  	atomic.StoreUint32(&preSplitDetectThreshold, preSplitThresholdInTest)
    54  
    55  	old = atomic.LoadUint32(&preSplitSizeThreshold)
    56  	defer atomic.StoreUint32(&preSplitSizeThreshold, old)
    57  	atomic.StoreUint32(&preSplitSizeThreshold, 5000)
    58  
    59  	bo := NewBackofferWithVars(context.Background(), 1, nil)
    60  	checkKeyRegion := func(bo *Backoffer, start, end []byte, checker Checker) {
    61  		// Check regions after split.
    62  		loc1, err := s.causetstore.regionCache.LocateKey(bo, start)
    63  		c.Assert(err, IsNil)
    64  		loc2, err := s.causetstore.regionCache.LocateKey(bo, end)
    65  		c.Assert(err, IsNil)
    66  		c.Assert(loc1.Region.id, checker, loc2.Region.id)
    67  	}
    68  	mode := []string{"optimistic", "pessimistic"}
    69  	var (
    70  		startKey []byte
    71  		endKey   []byte
    72  	)
    73  	ctx := context.Background()
    74  	for _, m := range mode {
    75  		if m == "optimistic" {
    76  			startKey = encodeKey(s.prefix, s08d("key", 0))
    77  			endKey = encodeKey(s.prefix, s08d("key", preSplitThresholdInTest))
    78  		} else {
    79  			startKey = encodeKey(s.prefix, s08d("pkey", 0))
    80  			endKey = encodeKey(s.prefix, s08d("pkey", preSplitThresholdInTest))
    81  		}
    82  		// Check before test.
    83  		checkKeyRegion(bo, startKey, endKey, Equals)
    84  		txn := s.beginTxn(c)
    85  		if m == "pessimistic" {
    86  			txn.SetOption(ekv.Pessimistic, true)
    87  			lockCtx := &ekv.LockCtx{}
    88  			lockCtx.ForUFIDelateTS = txn.startTS
    89  			keys := make([]ekv.Key, 0, preSplitThresholdInTest)
    90  			for i := 0; i < preSplitThresholdInTest; i++ {
    91  				keys = append(keys, encodeKey(s.prefix, s08d("pkey", i)))
    92  			}
    93  			err := txn.LockKeys(ctx, lockCtx, keys...)
    94  			c.Assert(err, IsNil)
    95  			checkKeyRegion(bo, startKey, endKey, Not(Equals))
    96  		}
    97  		var err error
    98  		for i := 0; i < preSplitThresholdInTest; i++ {
    99  			if m == "optimistic" {
   100  				err = txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
   101  			} else {
   102  				err = txn.Set(encodeKey(s.prefix, s08d("pkey", i)), valueBytes(i))
   103  			}
   104  			c.Assert(err, IsNil)
   105  		}
   106  		err = txn.Commit(context.Background())
   107  		c.Assert(err, IsNil)
   108  		// Check region split after test.
   109  		checkKeyRegion(bo, startKey, endKey, Not(Equals))
   110  	}
   111  }