github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/reorg_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
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/milevadb/causet/blocks"
    23  	"github.com/whtcorpsinc/milevadb/ekv"
    24  	. "github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    25  	"github.com/whtcorpsinc/milevadb/spacetime"
    26  	"github.com/whtcorpsinc/milevadb/types"
    27  )
    28  
    29  type testCtxKeyType int
    30  
    31  func (k testCtxKeyType) String() string {
    32  	return "test_ctx_key"
    33  }
    34  
    35  const testCtxKey testCtxKeyType = 0
    36  
    37  func (s *testDBSSuite) TestReorg(c *C) {
    38  	causetstore := testCreateStore(c, "test_reorg")
    39  	defer causetstore.Close()
    40  
    41  	d := testNewDBSAndStart(
    42  		context.Background(),
    43  		c,
    44  		WithStore(causetstore),
    45  		WithLease(testLease),
    46  	)
    47  	defer d.Stop()
    48  
    49  	time.Sleep(testLease)
    50  
    51  	ctx := testNewContext(d)
    52  
    53  	ctx.SetValue(testCtxKey, 1)
    54  	c.Assert(ctx.Value(testCtxKey), Equals, 1)
    55  	ctx.ClearValue(testCtxKey)
    56  
    57  	err := ctx.NewTxn(context.Background())
    58  	c.Assert(err, IsNil)
    59  	txn, err := ctx.Txn(true)
    60  	c.Assert(err, IsNil)
    61  	err = txn.Set([]byte("a"), []byte("b"))
    62  	c.Assert(err, IsNil)
    63  	err = txn.Rollback()
    64  	c.Assert(err, IsNil)
    65  
    66  	err = ctx.NewTxn(context.Background())
    67  	c.Assert(err, IsNil)
    68  	txn, err = ctx.Txn(true)
    69  	c.Assert(err, IsNil)
    70  	err = txn.Set([]byte("a"), []byte("b"))
    71  	c.Assert(err, IsNil)
    72  	err = txn.Commit(context.Background())
    73  	c.Assert(err, IsNil)
    74  
    75  	rowCount := int64(10)
    76  	handle := s.NewHandle().Int(100).Common("a", 100, "string")
    77  	f := func() error {
    78  		d.generalWorker().reorgCtx.setRowCount(rowCount)
    79  		d.generalWorker().reorgCtx.setNextHandle(handle)
    80  		time.Sleep(1*ReorgWaitTimeout + 100*time.Millisecond)
    81  		return nil
    82  	}
    83  	job := &perceptron.Job{
    84  		ID:          1,
    85  		SnapshotVer: 1, // Make sure it is not zero. So the reorgInfo's first is false.
    86  	}
    87  	err = ctx.NewTxn(context.Background())
    88  	c.Assert(err, IsNil)
    89  	txn, err = ctx.Txn(true)
    90  	c.Assert(err, IsNil)
    91  	m := spacetime.NewMeta(txn)
    92  	rInfo := &reorgInfo{
    93  		Job: job,
    94  	}
    95  	mockTbl := blocks.MockBlockFromMeta(&perceptron.BlockInfo{IsCommonHandle: s.IsCommonHandle})
    96  	err = d.generalWorker().runReorgJob(m, rInfo, mockTbl.Meta(), d.lease, f)
    97  	c.Assert(err, NotNil)
    98  
    99  	// The longest to wait for 5 seconds to make sure the function of f is returned.
   100  	for i := 0; i < 1000; i++ {
   101  		time.Sleep(5 * time.Millisecond)
   102  		err = d.generalWorker().runReorgJob(m, rInfo, mockTbl.Meta(), d.lease, f)
   103  		if err == nil {
   104  			c.Assert(job.RowCount, Equals, rowCount)
   105  			c.Assert(d.generalWorker().reorgCtx.rowCount, Equals, int64(0))
   106  
   107  			// Test whether reorgInfo's Handle is uFIDelate.
   108  			err = txn.Commit(context.Background())
   109  			c.Assert(err, IsNil)
   110  			err = ctx.NewTxn(context.Background())
   111  			c.Assert(err, IsNil)
   112  
   113  			m = spacetime.NewMeta(txn)
   114  			info, err1 := getReorgInfo(d.dbsCtx, m, job, mockTbl)
   115  			c.Assert(err1, IsNil)
   116  			c.Assert(info.StartHandle, HandleEquals, handle)
   117  			_, doneHandle := d.generalWorker().reorgCtx.getRowCountAndHandle()
   118  			c.Assert(doneHandle, IsNil)
   119  			break
   120  		}
   121  	}
   122  	c.Assert(err, IsNil)
   123  
   124  	job = &perceptron.Job{
   125  		ID:          2,
   126  		SchemaID:    1,
   127  		Type:        perceptron.CausetActionCreateSchema,
   128  		Args:        []interface{}{perceptron.NewCIStr("test")},
   129  		SnapshotVer: 1, // Make sure it is not zero. So the reorgInfo's first is false.
   130  	}
   131  
   132  	var info *reorgInfo
   133  	startHandle := s.NewHandle().Int(1).Common(100, "string")
   134  	endHandle := s.NewHandle().Int(0).Common(101, "string")
   135  	err = ekv.RunInNewTxn(d.causetstore, false, func(txn ekv.Transaction) error {
   136  		t := spacetime.NewMeta(txn)
   137  		var err1 error
   138  		info, err1 = getReorgInfo(d.dbsCtx, t, job, mockTbl)
   139  		c.Assert(err1, IsNil)
   140  		err1 = info.UFIDelateReorgMeta(txn, startHandle, endHandle, 1)
   141  		c.Assert(err1, IsNil)
   142  		return nil
   143  	})
   144  	c.Assert(err, IsNil)
   145  
   146  	err = ekv.RunInNewTxn(d.causetstore, false, func(txn ekv.Transaction) error {
   147  		t := spacetime.NewMeta(txn)
   148  		var err1 error
   149  		info, err1 = getReorgInfo(d.dbsCtx, t, job, mockTbl)
   150  		c.Assert(err1, IsNil)
   151  		c.Assert(info.StartHandle, HandleEquals, startHandle)
   152  		c.Assert(info.EndHandle, HandleEquals, endHandle)
   153  		return nil
   154  	})
   155  	c.Assert(err, IsNil)
   156  
   157  	d.Stop()
   158  	err = d.generalWorker().runReorgJob(m, rInfo, mockTbl.Meta(), d.lease, func() error {
   159  		time.Sleep(4 * testLease)
   160  		return nil
   161  	})
   162  	c.Assert(err, NotNil)
   163  	txn, err = ctx.Txn(true)
   164  	c.Assert(err, IsNil)
   165  	err = txn.Commit(context.Background())
   166  	c.Assert(err, IsNil)
   167  	s.RerunWithCommonHandleEnabled(c, s.TestReorg)
   168  }
   169  
   170  func (s *testDBSSuite) TestReorgTenant(c *C) {
   171  	causetstore := testCreateStore(c, "test_reorg_tenant")
   172  	defer causetstore.Close()
   173  
   174  	d1 := testNewDBSAndStart(
   175  		context.Background(),
   176  		c,
   177  		WithStore(causetstore),
   178  		WithLease(testLease),
   179  	)
   180  	defer d1.Stop()
   181  
   182  	ctx := testNewContext(d1)
   183  
   184  	testCheckTenant(c, d1, true)
   185  
   186  	d2 := testNewDBSAndStart(
   187  		context.Background(),
   188  		c,
   189  		WithStore(causetstore),
   190  		WithLease(testLease),
   191  	)
   192  	defer d2.Stop()
   193  
   194  	dbInfo := testSchemaInfo(c, d1, "test")
   195  	testCreateSchema(c, ctx, d1, dbInfo)
   196  
   197  	tblInfo := testBlockInfo(c, d1, "t", 3)
   198  	testCreateBlock(c, ctx, d1, dbInfo, tblInfo)
   199  	t := testGetBlock(c, d1, dbInfo.ID, tblInfo.ID)
   200  
   201  	num := 10
   202  	for i := 0; i < num; i++ {
   203  		_, err := t.AddRecord(ctx, types.MakeCausets(i, i, i))
   204  		c.Assert(err, IsNil)
   205  	}
   206  
   207  	txn, err := ctx.Txn(true)
   208  	c.Assert(err, IsNil)
   209  	err = txn.Commit(context.Background())
   210  	c.Assert(err, IsNil)
   211  
   212  	tc := &TestDBSCallback{}
   213  	tc.onJobRunBefore = func(job *perceptron.Job) {
   214  		if job.SchemaState == perceptron.StateDeleteReorganization {
   215  			d1.Stop()
   216  		}
   217  	}
   218  
   219  	d1.SetHook(tc)
   220  
   221  	testDropSchema(c, ctx, d1, dbInfo)
   222  
   223  	err = ekv.RunInNewTxn(d1.causetstore, false, func(txn ekv.Transaction) error {
   224  		t := spacetime.NewMeta(txn)
   225  		EDB, err1 := t.GetDatabase(dbInfo.ID)
   226  		c.Assert(err1, IsNil)
   227  		c.Assert(EDB, IsNil)
   228  		return nil
   229  	})
   230  	c.Assert(err, IsNil)
   231  }