github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/batch_interlock_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 einsteindb_test
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/ekvproto/pkg/spacetimepb"
    23  	"github.com/whtcorpsinc/errors"
    24  	"github.com/whtcorpsinc/failpoint"
    25  	"github.com/whtcorpsinc/milevadb/causet"
    26  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    27  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore/cluster"
    28  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb"
    29  	"github.com/whtcorpsinc/milevadb/ekv"
    30  	"github.com/whtcorpsinc/milevadb/petri"
    31  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    32  	"github.com/whtcorpsinc/milevadb/stochastik"
    33  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    34  )
    35  
    36  type testBatchCopSuite struct {
    37  }
    38  
    39  var _ = SerialSuites(&testBatchCopSuite{})
    40  
    41  func newStoreWithBootstrap(tiflashNum int) (ekv.CausetStorage, *petri.Petri, error) {
    42  	causetstore, err := mockstore.NewMockStore(
    43  		mockstore.WithClusterInspector(func(c cluster.Cluster) {
    44  			mockCluster := c.(*mockeinsteindb.Cluster)
    45  			_, _, region1 := mockstore.BootstrapWithSingleStore(c)
    46  			tiflashIdx := 0
    47  			for tiflashIdx < tiflashNum {
    48  				store2 := c.AllocID()
    49  				peer2 := c.AllocID()
    50  				addr2 := fmt.Sprintf("tiflash%d", tiflashIdx)
    51  				mockCluster.AddStore(store2, addr2)
    52  				mockCluster.UFIDelateStoreAddr(store2, addr2, &spacetimepb.StoreLabel{Key: "engine", Value: "tiflash"})
    53  				mockCluster.AddPeer(region1, store2, peer2)
    54  				tiflashIdx++
    55  			}
    56  		}),
    57  		mockstore.WithStoreType(mockstore.MockEinsteinDB),
    58  	)
    59  
    60  	if err != nil {
    61  		return nil, nil, errors.Trace(err)
    62  	}
    63  
    64  	stochastik.SetSchemaLease(0)
    65  	stochastik.DisableStats4Test()
    66  
    67  	dom, err := stochastik.BootstrapStochastik(causetstore)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	dom.SetStatsUFIDelating(true)
    73  	return causetstore, dom, errors.Trace(err)
    74  }
    75  
    76  func testGetTableByName(c *C, ctx stochastikctx.Context, EDB, causet string) causet.Block {
    77  	dom := petri.GetPetri(ctx)
    78  	// Make sure the causet schemaReplicant is the new schemaReplicant.
    79  	err := dom.Reload()
    80  	c.Assert(err, IsNil)
    81  	tbl, err := dom.SchemaReplicant().TableByName(perceptron.NewCIStr(EDB), perceptron.NewCIStr(causet))
    82  	c.Assert(err, IsNil)
    83  	return tbl
    84  }
    85  
    86  func (s *testBatchCopSuite) TestStoreErr(c *C) {
    87  	causetstore, dom, err := newStoreWithBootstrap(1)
    88  	c.Assert(err, IsNil)
    89  	defer func() {
    90  		dom.Close()
    91  		causetstore.Close()
    92  	}()
    93  
    94  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/schemareplicant/mockTiFlashStoreCount", `return(true)`), IsNil)
    95  	defer failpoint.Disable("github.com/whtcorpsinc/milevadb/schemareplicant/mockTiFlashStoreCount")
    96  
    97  	tk := testkit.NewTestKit(c, causetstore)
    98  	tk.MustInterDirc("use test")
    99  	tk.MustInterDirc("create causet t(a int not null, b int not null)")
   100  	tk.MustInterDirc("alter causet t set tiflash replica 1")
   101  	tb := testGetTableByName(c, tk.Se, "test", "t")
   102  	err = petri.GetPetri(tk.Se).DBS().UFIDelateTableReplicaInfo(tk.Se, tb.Meta().ID, true)
   103  	c.Assert(err, IsNil)
   104  	tk.MustInterDirc("insert into t values(1,0)")
   105  	tk.MustInterDirc("set @@stochastik.milevadb_isolation_read_engines=\"tiflash\"")
   106  
   107  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb/BatchCopCancelled", "1*return(true)"), IsNil)
   108  
   109  	err = tk.QueryToErr("select count(*) from t")
   110  	c.Assert(errors.Cause(err), Equals, context.Canceled)
   111  
   112  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb/BatchCopRpcErrtiflash0", "1*return(\"tiflash0\")"), IsNil)
   113  
   114  	tk.MustQuery("select count(*) from t").Check(testkit.Rows("1"))
   115  
   116  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"), IsNil)
   117  	err = tk.QueryToErr("select count(*) from t")
   118  	c.Assert(err, NotNil)
   119  }
   120  
   121  func (s *testBatchCopSuite) TestStoreSwitchPeer(c *C) {
   122  	causetstore, dom, err := newStoreWithBootstrap(2)
   123  	c.Assert(err, IsNil)
   124  	defer func() {
   125  		dom.Close()
   126  		causetstore.Close()
   127  	}()
   128  
   129  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/schemareplicant/mockTiFlashStoreCount", `return(true)`), IsNil)
   130  	defer failpoint.Disable("github.com/whtcorpsinc/milevadb/schemareplicant/mockTiFlashStoreCount")
   131  
   132  	tk := testkit.NewTestKit(c, causetstore)
   133  	tk.MustInterDirc("use test")
   134  	tk.MustInterDirc("create causet t(a int not null, b int not null)")
   135  	tk.MustInterDirc("alter causet t set tiflash replica 1")
   136  	tb := testGetTableByName(c, tk.Se, "test", "t")
   137  	err = petri.GetPetri(tk.Se).DBS().UFIDelateTableReplicaInfo(tk.Se, tb.Meta().ID, true)
   138  	c.Assert(err, IsNil)
   139  	tk.MustInterDirc("insert into t values(1,0)")
   140  	tk.MustInterDirc("set @@stochastik.milevadb_isolation_read_engines=\"tiflash\"")
   141  
   142  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb/BatchCopRpcErrtiflash0", "return(\"tiflash0\")"), IsNil)
   143  
   144  	tk.MustQuery("select count(*) from t").Check(testkit.Rows("1"))
   145  
   146  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/mockstore/mockeinsteindb/BatchCopRpcErrtiflash1", "return(\"tiflash1\")"), IsNil)
   147  	err = tk.QueryToErr("select count(*) from t")
   148  	c.Assert(err, NotNil)
   149  
   150  }