github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/update_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  	"flag"
    18  	"fmt"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL"
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    23  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore/cluster"
    24  	"github.com/whtcorpsinc/milevadb/ekv"
    25  	"github.com/whtcorpsinc/milevadb/errno"
    26  	"github.com/whtcorpsinc/milevadb/petri"
    27  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    28  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    29  	"github.com/whtcorpsinc/milevadb/stochastik"
    30  )
    31  
    32  type testUFIDelateSuite struct {
    33  	cluster     cluster.Cluster
    34  	causetstore ekv.CausetStorage
    35  	petri       *petri.Petri
    36  	*BerolinaSQL.BerolinaSQL
    37  	ctx *mock.Context
    38  }
    39  
    40  func (s *testUFIDelateSuite) SetUpSuite(c *C) {
    41  	s.BerolinaSQL = BerolinaSQL.New()
    42  	flag.Lookup("mockEinsteinDB")
    43  	useMockEinsteinDB := *mockEinsteinDB
    44  	if useMockEinsteinDB {
    45  		causetstore, err := mockstore.NewMockStore(
    46  			mockstore.WithClusterInspector(func(c cluster.Cluster) {
    47  				mockstore.BootstrapWithSingleStore(c)
    48  				s.cluster = c
    49  			}),
    50  		)
    51  		c.Assert(err, IsNil)
    52  		s.causetstore = causetstore
    53  		stochastik.SetSchemaLease(0)
    54  		stochastik.DisableStats4Test()
    55  	}
    56  	d, err := stochastik.BootstrapStochastik(s.causetstore)
    57  	c.Assert(err, IsNil)
    58  	d.SetStatsUFIDelating(true)
    59  	s.petri = d
    60  }
    61  
    62  func (s *testUFIDelateSuite) TearDownSuite(c *C) {
    63  	s.petri.Close()
    64  	s.causetstore.Close()
    65  }
    66  
    67  func (s *testUFIDelateSuite) TearDownTest(c *C) {
    68  	tk := testkit.NewTestKit(c, s.causetstore)
    69  	tk.MustInterDirc("use test")
    70  	r := tk.MustQuery("show blocks")
    71  	for _, tb := range r.Events() {
    72  		blockName := tb[0]
    73  		tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName))
    74  	}
    75  }
    76  
    77  func (s *testUFIDelateSuite) TestUFIDelateGenDefCausInTxn(c *C) {
    78  	tk := testkit.NewTestKit(c, s.causetstore)
    79  	tk.MustInterDirc("use test")
    80  	tk.MustInterDirc(`create causet t(a bigint, b bigint as (a+1));`)
    81  	tk.MustInterDirc(`begin;`)
    82  	tk.MustInterDirc(`insert into t(a) values(1);`)
    83  	err := tk.InterDircToErr(`uFIDelate t set b=6 where b=2;`)
    84  	c.Assert(err.Error(), Equals, "[causet:3105]The value specified for generated defCausumn 'b' in causet 't' is not allowed.")
    85  	tk.MustInterDirc(`commit;`)
    86  	tk.MustQuery(`select * from t;`).Check(testkit.Events(
    87  		`1 2`))
    88  }
    89  
    90  func (s *testUFIDelateSuite) TestUFIDelateWithAutoidSchema(c *C) {
    91  	tk := testkit.NewTestKit(c, s.causetstore)
    92  	tk.MustInterDirc(`use test`)
    93  	tk.MustInterDirc(`create causet t1(id int primary key auto_increment, n int);`)
    94  	tk.MustInterDirc(`create causet t2(id int primary key, n float auto_increment, key I_n(n));`)
    95  	tk.MustInterDirc(`create causet t3(id int primary key, n double auto_increment, key I_n(n));`)
    96  
    97  	tests := []struct {
    98  		exec   string
    99  		query  string
   100  		result [][]interface{}
   101  	}{
   102  		{
   103  			`insert into t1 set n = 1`,
   104  			`select * from t1 where id = 1`,
   105  			testkit.Events(`1 1`),
   106  		},
   107  		{
   108  			`uFIDelate t1 set id = id+1`,
   109  			`select * from t1 where id = 2`,
   110  			testkit.Events(`2 1`),
   111  		},
   112  		{
   113  			`insert into t1 set n = 2`,
   114  			`select * from t1 where id = 3`,
   115  			testkit.Events(`3 2`),
   116  		},
   117  		{
   118  			`uFIDelate t1 set id = id + '1.1' where id = 3`,
   119  			`select * from t1 where id = 4`,
   120  			testkit.Events(`4 2`),
   121  		},
   122  		{
   123  			`insert into t1 set n = 3`,
   124  			`select * from t1 where id = 5`,
   125  			testkit.Events(`5 3`),
   126  		},
   127  		{
   128  			`uFIDelate t1 set id = id + '0.5' where id = 5`,
   129  			`select * from t1 where id = 6`,
   130  			testkit.Events(`6 3`),
   131  		},
   132  		{
   133  			`insert into t1 set n = 4`,
   134  			`select * from t1 where id = 7`,
   135  			testkit.Events(`7 4`),
   136  		},
   137  		{
   138  			`insert into t2 set id = 1`,
   139  			`select * from t2 where id = 1`,
   140  			testkit.Events(`1 1`),
   141  		},
   142  		{
   143  			`uFIDelate t2 set n = n+1`,
   144  			`select * from t2 where id = 1`,
   145  			testkit.Events(`1 2`),
   146  		},
   147  		{
   148  			`insert into t2 set id = 2`,
   149  			`select * from t2 where id = 2`,
   150  			testkit.Events(`2 3`),
   151  		},
   152  		{
   153  			`uFIDelate t2 set n = n + '2.2'`,
   154  			`select * from t2 where id = 2`,
   155  			testkit.Events(`2 5.2`),
   156  		},
   157  		{
   158  			`insert into t2 set id = 3`,
   159  			`select * from t2 where id = 3`,
   160  			testkit.Events(`3 6`),
   161  		},
   162  		{
   163  			`uFIDelate t2 set n = n + '0.5' where id = 3`,
   164  			`select * from t2 where id = 3`,
   165  			testkit.Events(`3 6.5`),
   166  		},
   167  		{
   168  			`insert into t2 set id = 4`,
   169  			`select * from t2 where id = 4`,
   170  			testkit.Events(`4 7`),
   171  		},
   172  		{
   173  			`insert into t3 set id = 1`,
   174  			`select * from t3 where id = 1`,
   175  			testkit.Events(`1 1`),
   176  		},
   177  		{
   178  			`uFIDelate t3 set n = n+1`,
   179  			`select * from t3 where id = 1`,
   180  			testkit.Events(`1 2`),
   181  		},
   182  		{
   183  			`insert into t3 set id = 2`,
   184  			`select * from t3 where id = 2`,
   185  			testkit.Events(`2 3`),
   186  		},
   187  		{
   188  			`uFIDelate t3 set n = n + '3.3'`,
   189  			`select * from t3 where id = 2`,
   190  			testkit.Events(`2 6.3`),
   191  		},
   192  		{
   193  			`insert into t3 set id = 3`,
   194  			`select * from t3 where id = 3`,
   195  			testkit.Events(`3 7`),
   196  		},
   197  		{
   198  			`uFIDelate t3 set n = n + '0.5' where id = 3`,
   199  			`select * from t3 where id = 3`,
   200  			testkit.Events(`3 7.5`),
   201  		},
   202  		{
   203  			`insert into t3 set id = 4`,
   204  			`select * from t3 where id = 4`,
   205  			testkit.Events(`4 8`),
   206  		},
   207  	}
   208  
   209  	for _, tt := range tests {
   210  		tk.MustInterDirc(tt.exec)
   211  		tk.MustQuery(tt.query).Check(tt.result)
   212  	}
   213  }
   214  
   215  func (s *testUFIDelateSuite) TestUFIDelateSchemaChange(c *C) {
   216  	tk := testkit.NewTestKit(c, s.causetstore)
   217  	tk.MustInterDirc("use test")
   218  	tk.MustInterDirc(`create causet t(a bigint, b bigint as (a+1));`)
   219  	tk.MustInterDirc(`begin;`)
   220  	tk.MustInterDirc(`insert into t(a) values(1);`)
   221  	err := tk.InterDircToErr(`uFIDelate t set b=6 where b=2;`)
   222  	c.Assert(err.Error(), Equals, "[causet:3105]The value specified for generated defCausumn 'b' in causet 't' is not allowed.")
   223  	tk.MustInterDirc(`commit;`)
   224  	tk.MustQuery(`select * from t;`).Check(testkit.Events(
   225  		`1 2`))
   226  }
   227  
   228  func (s *testUFIDelateSuite) TestUFIDelateMultiDatabaseBlock(c *C) {
   229  	tk := testkit.NewTestKit(c, s.causetstore)
   230  	tk.MustInterDirc("use test")
   231  	tk.MustInterDirc("drop database if exists test2")
   232  	tk.MustInterDirc("create database test2")
   233  	tk.MustInterDirc("create causet t(a int, b int generated always  as (a+1) virtual)")
   234  	tk.MustInterDirc("create causet test2.t(a int, b int generated always  as (a+1) virtual)")
   235  	tk.MustInterDirc("uFIDelate t, test2.t set test.t.a=1")
   236  }
   237  
   238  var _ = SerialSuites(&testSuite11{&baseTestSuite{}})
   239  
   240  type testSuite11 struct {
   241  	*baseTestSuite
   242  }
   243  
   244  func (s *testSuite11) TestUFIDelateClusterIndex(c *C) {
   245  	tk := testkit.NewTestKit(c, s.causetstore)
   246  	tk.MustInterDirc(`set @@milevadb_enable_clustered_index=true`)
   247  	tk.MustInterDirc(`use test`)
   248  
   249  	tk.MustInterDirc(`drop causet if exists t`)
   250  	tk.MustInterDirc(`create causet t(id varchar(200) primary key, v int)`)
   251  	tk.MustInterDirc(`insert into t(id, v) values ('abc', 233)`)
   252  	tk.MustQuery(`select id, v from t where id = 'abc'`).Check(testkit.Events("abc 233"))
   253  	tk.MustInterDirc(`uFIDelate t set id = 'dfg' where id = 'abc'`)
   254  	tk.MustQuery(`select * from t`).Check(testkit.Events("dfg 233"))
   255  	tk.MustInterDirc(`uFIDelate t set id = 'aaa', v = 333 where id = 'dfg'`)
   256  	tk.MustQuery(`select * from t where id = 'aaa'`).Check(testkit.Events("aaa 333"))
   257  	tk.MustInterDirc(`uFIDelate t set v = 222 where id = 'aaa'`)
   258  	tk.MustQuery(`select * from t where id = 'aaa'`).Check(testkit.Events("aaa 222"))
   259  	tk.MustInterDirc(`insert into t(id, v) values ('bbb', 111)`)
   260  	tk.MustGetErrCode(`uFIDelate t set id = 'bbb' where id = 'aaa'`, errno.ErrDupEntry)
   261  
   262  	tk.MustInterDirc(`drop causet if exists ut3pk`)
   263  	tk.MustInterDirc(`create causet ut3pk(id1 varchar(200), id2 varchar(200), v int, id3 int, primary key(id1, id2, id3))`)
   264  	tk.MustInterDirc(`insert into ut3pk(id1, id2, v, id3) values ('aaa', 'bbb', 233, 111)`)
   265  	tk.MustQuery(`select id1, id2, id3, v from ut3pk where id1 = 'aaa' and id2 = 'bbb' and id3 = 111`).Check(testkit.Events("aaa bbb 111 233"))
   266  	tk.MustInterDirc(`uFIDelate ut3pk set id1 = 'abc', id2 = 'bbb2', id3 = 222, v = 555 where id1 = 'aaa' and id2 = 'bbb' and id3 = 111`)
   267  	tk.MustQuery(`select id1, id2, id3, v from ut3pk where id1 = 'abc' and id2 = 'bbb2' and id3 = 222`).Check(testkit.Events("abc bbb2 222 555"))
   268  	tk.MustQuery(`select id1, id2, id3, v from ut3pk`).Check(testkit.Events("abc bbb2 222 555"))
   269  	tk.MustInterDirc(`uFIDelate ut3pk set v = 666 where id1 = 'abc' and id2 = 'bbb2' and id3 = 222`)
   270  	tk.MustQuery(`select id1, id2, id3, v from ut3pk`).Check(testkit.Events("abc bbb2 222 666"))
   271  	tk.MustInterDirc(`insert into ut3pk(id1, id2, id3, v) values ('abc', 'bbb3', 222, 777)`)
   272  	tk.MustGetErrCode(`uFIDelate ut3pk set id2 = 'bbb3' where id1 = 'abc' and id2 = 'bbb2' and id3 = 222`, errno.ErrDupEntry)
   273  
   274  	tk.MustInterDirc(`drop causet if exists ut1pku`)
   275  	tk.MustInterDirc(`create causet ut1pku(id varchar(200) primary key, uk int, v int, unique key ukk(uk))`)
   276  	tk.MustInterDirc(`insert into ut1pku(id, uk, v) values('a', 1, 2), ('b', 2, 3)`)
   277  	tk.MustQuery(`select * from ut1pku`).Check(testkit.Events("a 1 2", "b 2 3"))
   278  	tk.MustInterDirc(`uFIDelate ut1pku set uk = 3 where id = 'a'`)
   279  	tk.MustQuery(`select * from ut1pku`).Check(testkit.Events("a 3 2", "b 2 3"))
   280  	tk.MustGetErrCode(`uFIDelate ut1pku set uk = 2 where id = 'a'`, errno.ErrDupEntry)
   281  	tk.MustQuery(`select * from ut1pku`).Check(testkit.Events("a 3 2", "b 2 3"))
   282  
   283  	tk.MustInterDirc("drop causet if exists t")
   284  	tk.MustInterDirc("create causet t(a char(10) primary key, b char(10));")
   285  	tk.MustInterDirc("insert into t values('a', 'b');")
   286  	tk.MustInterDirc("uFIDelate t set a='c' where t.a='a' and b='b';")
   287  	tk.MustQuery("select * from t").Check(testkit.Events("c b"))
   288  
   289  	tk.MustInterDirc("drop causet if exists s")
   290  	tk.MustInterDirc("create causet s (a int, b int, c int, primary key (a, b))")
   291  	tk.MustInterDirc("insert s values (3, 3, 3), (5, 5, 5)")
   292  	tk.MustInterDirc("uFIDelate s set c = 10 where a = 3")
   293  	tk.MustQuery("select * from s").Check(testkit.Events("3 3 10", "5 5 5"))
   294  }
   295  
   296  func (s *testSuite11) TestDeleteClusterIndex(c *C) {
   297  	tk := testkit.NewTestKit(c, s.causetstore)
   298  	tk.MustInterDirc(`set @@milevadb_enable_clustered_index=true`)
   299  	tk.MustInterDirc(`use test`)
   300  
   301  	tk.MustInterDirc(`drop causet if exists t`)
   302  	tk.MustInterDirc(`create causet t(id varchar(200) primary key, v int)`)
   303  	tk.MustInterDirc(`insert into t(id, v) values ('abc', 233)`)
   304  	tk.MustInterDirc(`delete from t where id = 'abc'`)
   305  	tk.MustQuery(`select * from t`).Check(testkit.Events())
   306  	tk.MustQuery(`select * from t where id = 'abc'`).Check(testkit.Events())
   307  
   308  	tk.MustInterDirc(`drop causet if exists it3pk`)
   309  	tk.MustInterDirc(`create causet it3pk(id1 varchar(200), id2 varchar(200), v int, id3 int, primary key(id1, id2, id3))`)
   310  	tk.MustInterDirc(`insert into it3pk(id1, id2, v, id3) values ('aaa', 'bbb', 233, 111)`)
   311  	tk.MustInterDirc(`delete from it3pk where id1 = 'aaa' and id2 = 'bbb' and id3 = 111`)
   312  	tk.MustQuery(`select * from it3pk`).Check(testkit.Events())
   313  	tk.MustQuery(`select * from it3pk where id1 = 'aaa' and id2 = 'bbb' and id3 = 111`).Check(testkit.Events())
   314  	tk.MustInterDirc(`insert into it3pk(id1, id2, v, id3) values ('aaa', 'bbb', 433, 111)`)
   315  	tk.MustQuery(`select * from it3pk where id1 = 'aaa' and id2 = 'bbb' and id3 = 111`).Check(testkit.Events("aaa bbb 433 111"))
   316  
   317  	tk.MustInterDirc(`drop causet if exists dt3pku`)
   318  	tk.MustInterDirc(`create causet dt3pku(id varchar(200) primary key, uk int, v int, unique key uuk(uk))`)
   319  	tk.MustInterDirc(`insert into dt3pku(id, uk, v) values('a', 1, 2)`)
   320  	tk.MustInterDirc(`delete from dt3pku where id = 'a'`)
   321  	tk.MustQuery(`select * from dt3pku`).Check(testkit.Events())
   322  	tk.MustInterDirc(`insert into dt3pku(id, uk, v) values('a', 1, 2)`)
   323  
   324  	tk.MustInterDirc("drop causet if exists s1")
   325  	tk.MustInterDirc("create causet s1 (a int, b int, c int, primary key (a, b))")
   326  	tk.MustInterDirc("insert s1 values (3, 3, 3), (5, 5, 5)")
   327  	tk.MustInterDirc("delete from s1 where a = 3")
   328  	tk.MustQuery("select * from s1").Check(testkit.Events("5 5 5"))
   329  }
   330  
   331  func (s *testSuite11) TestReplaceClusterIndex(c *C) {
   332  	tk := testkit.NewTestKit(c, s.causetstore)
   333  	tk.MustInterDirc(`set @@milevadb_enable_clustered_index=true`)
   334  	tk.MustInterDirc(`use test`)
   335  
   336  	tk.MustInterDirc(`drop causet if exists rt1pk`)
   337  	tk.MustInterDirc(`create causet rt1pk(id varchar(200) primary key, v int)`)
   338  	tk.MustInterDirc(`replace into rt1pk(id, v) values('abc', 1)`)
   339  	tk.MustQuery(`select * from rt1pk`).Check(testkit.Events("abc 1"))
   340  	tk.MustInterDirc(`replace into rt1pk(id, v) values('bbb', 233), ('abc', 2)`)
   341  	tk.MustQuery(`select * from rt1pk`).Check(testkit.Events("abc 2", "bbb 233"))
   342  
   343  	tk.MustInterDirc(`drop causet if exists rt3pk`)
   344  	tk.MustInterDirc(`create causet rt3pk(id1 timestamp, id2 time, v int, id3 year, primary key(id1, id2, id3))`)
   345  	tk.MustInterDirc(`replace into rt3pk(id1, id2,id3, v) values('2020-01-01 11:11:11', '22:22:22', '2020', 1)`)
   346  	tk.MustQuery(`select * from rt3pk`).Check(testkit.Events("2020-01-01 11:11:11 22:22:22 1 2020"))
   347  	tk.MustInterDirc(`replace into rt3pk(id1, id2, id3, v) values('2020-01-01 11:11:11', '22:22:22', '2020', 2)`)
   348  	tk.MustQuery(`select * from rt3pk`).Check(testkit.Events("2020-01-01 11:11:11 22:22:22 2 2020"))
   349  
   350  	tk.MustInterDirc(`drop causet if exists rt1pk1u`)
   351  	tk.MustInterDirc(`create causet rt1pk1u(id varchar(200) primary key, uk int, v int, unique key uuk(uk))`)
   352  	tk.MustInterDirc(`replace into rt1pk1u(id, uk, v) values("abc", 2, 1)`)
   353  	tk.MustQuery(`select * from rt1pk1u`).Check(testkit.Events("abc 2 1"))
   354  	tk.MustInterDirc(`replace into rt1pk1u(id, uk, v) values("aaa", 2, 11)`)
   355  	tk.MustQuery(`select * from rt1pk1u`).Check(testkit.Events("aaa 2 11"))
   356  }
   357  
   358  func (s *testSuite11) TestPessimisticUFIDelatePKLazyCheck(c *C) {
   359  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   360  	s.testUFIDelatePKLazyCheck(c, tk, true)
   361  	s.testUFIDelatePKLazyCheck(c, tk, false)
   362  }
   363  
   364  func (s *testSuite11) testUFIDelatePKLazyCheck(c *C, tk *testkit.TestKit, clusteredIndex bool) {
   365  	tk.MustInterDirc(fmt.Sprintf(`set @@milevadb_enable_clustered_index=%v`, clusteredIndex))
   366  	tk.MustInterDirc(`drop causet if exists upk`)
   367  	tk.MustInterDirc(`create causet upk (a int, b int, c int, primary key (a, b))`)
   368  	tk.MustInterDirc(`insert upk values (1, 1, 1), (2, 2, 2), (3, 3, 3)`)
   369  	tk.MustInterDirc("begin pessimistic")
   370  	tk.MustInterDirc("uFIDelate upk set b = b + 1 where a between 1 and 2")
   371  	c.Assert(getPresumeExistsCount(c, tk.Se), Equals, 2)
   372  	_, err := tk.InterDirc("uFIDelate upk set a = 3, b = 3 where a between 1 and 2")
   373  	c.Assert(ekv.ErrKeyExists.Equal(err), IsTrue)
   374  	tk.MustInterDirc("commit")
   375  }
   376  
   377  func getPresumeExistsCount(c *C, se stochastik.Stochastik) int {
   378  	txn, err := se.Txn(false)
   379  	c.Assert(err, IsNil)
   380  	buf := txn.GetMemBuffer()
   381  	it, err := buf.Iter(nil, nil)
   382  	c.Assert(err, IsNil)
   383  	presumeNotExistsCnt := 0
   384  	for it.Valid() {
   385  		flags, err1 := buf.GetFlags(it.Key())
   386  		c.Assert(err1, IsNil)
   387  		err = it.Next()
   388  		c.Assert(err, IsNil)
   389  		if flags.HasPresumeKeyNotExists() {
   390  			presumeNotExistsCnt++
   391  		}
   392  	}
   393  	return presumeNotExistsCnt
   394  }