github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/serial_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 "fmt" 19 "math" 20 "strconv" 21 "strings" 22 "sync" 23 "sync/atomic" 24 "time" 25 26 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 27 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 28 . "github.com/whtcorpsinc/check" 29 "github.com/whtcorpsinc/errors" 30 "github.com/whtcorpsinc/failpoint" 31 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 32 "github.com/whtcorpsinc/milevadb/causetstore/mockstore/cluster" 33 "github.com/whtcorpsinc/milevadb/config" 34 "github.com/whtcorpsinc/milevadb/dbs" 35 dbsutil "github.com/whtcorpsinc/milevadb/dbs/soliton" 36 "github.com/whtcorpsinc/milevadb/ekv" 37 "github.com/whtcorpsinc/milevadb/errno" 38 "github.com/whtcorpsinc/milevadb/petri" 39 "github.com/whtcorpsinc/milevadb/schemareplicant" 40 "github.com/whtcorpsinc/milevadb/soliton/admin" 41 "github.com/whtcorpsinc/milevadb/soliton/defCauslate" 42 "github.com/whtcorpsinc/milevadb/soliton/gcutil" 43 "github.com/whtcorpsinc/milevadb/soliton/mock" 44 . "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 45 "github.com/whtcorpsinc/milevadb/soliton/testkit" 46 "github.com/whtcorpsinc/milevadb/spacetime" 47 "github.com/whtcorpsinc/milevadb/spacetime/autoid" 48 "github.com/whtcorpsinc/milevadb/stochastik" 49 "github.com/whtcorpsinc/milevadb/stochastikctx" 50 "github.com/whtcorpsinc/milevadb/stochastikctx/variable" 51 ) 52 53 // Make it serial because config is modified in test cases. 54 var _ = SerialSuites(&testSerialSuite{}) 55 56 type testSerialSuite struct { 57 CommonHandleSuite 58 causetstore ekv.CausetStorage 59 cluster cluster.Cluster 60 dom *petri.Petri 61 } 62 63 func (s *testSerialSuite) SetUpSuite(c *C) { 64 stochastik.SetSchemaLease(200 * time.Millisecond) 65 stochastik.DisableStats4Test() 66 config.UFIDelateGlobal(func(conf *config.Config) { 67 // Test for add/drop primary key. 68 conf.AlterPrimaryKey = false 69 }) 70 71 dbs.SetWaitTimeWhenErrorOccurred(1 * time.Microsecond) 72 73 var err error 74 s.causetstore, err = mockstore.NewMockStore( 75 mockstore.WithClusterInspector(func(c cluster.Cluster) { 76 mockstore.BootstrapWithSingleStore(c) 77 s.cluster = c 78 }), 79 ) 80 c.Assert(err, IsNil) 81 82 s.dom, err = stochastik.BootstrapStochastik(s.causetstore) 83 c.Assert(err, IsNil) 84 } 85 86 func (s *testSerialSuite) TearDownSuite(c *C) { 87 if s.dom != nil { 88 s.dom.Close() 89 } 90 if s.causetstore != nil { 91 s.causetstore.Close() 92 } 93 } 94 95 func (s *testSerialSuite) TestChangeMaxIndexLength(c *C) { 96 tk := testkit.NewTestKitWithInit(c, s.causetstore) 97 98 defer config.RestoreFunc()() 99 config.UFIDelateGlobal(func(conf *config.Config) { 100 conf.MaxIndexLength = config.DefMaxOfMaxIndexLength 101 }) 102 103 tk.MustInterDirc("drop causet if exists t;") 104 tk.MustInterDirc("drop causet if exists t1;") 105 106 tk.MustInterDirc("create causet t (c1 varchar(3073), index(c1)) charset = ascii;") 107 tk.MustInterDirc(fmt.Sprintf("create causet t1 (c1 varchar(%d), index(c1)) charset = ascii;", config.DefMaxOfMaxIndexLength)) 108 _, err := tk.InterDirc(fmt.Sprintf("create causet t2 (c1 varchar(%d), index(c1)) charset = ascii;", config.DefMaxOfMaxIndexLength+1)) 109 c.Assert(err.Error(), Equals, "[dbs:1071]Specified key was too long; max key length is 12288 bytes") 110 tk.MustInterDirc("drop causet t, t1") 111 } 112 113 func (s *testSerialSuite) TestPrimaryKey(c *C) { 114 tk := testkit.NewTestKit(c, s.causetstore) 115 tk.MustInterDirc("use test") 116 tk.MustInterDirc("set @@milevadb_enable_clustered_index = 0") 117 118 tk.MustInterDirc("create causet primary_key_test (a int, b varchar(10))") 119 tk.MustInterDirc("create causet primary_key_test_1 (a int, b varchar(10), primary key(a))") 120 _, err := tk.InterDirc("alter causet primary_key_test add primary key(a)") 121 c.Assert(dbs.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue) 122 _, err = tk.InterDirc("alter causet primary_key_test drop primary key") 123 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when alter-primary-key is false") 124 // for "drop index `primary` on ..." syntax 125 _, err = tk.InterDirc("drop index `primary` on primary_key_test") 126 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when alter-primary-key is false") 127 _, err = tk.InterDirc("drop index `primary` on primary_key_test_1") 128 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when alter-primary-key is false") 129 130 // Change the value of AlterPrimaryKey. 131 tk.MustInterDirc("create causet primary_key_test1 (a int, b varchar(10), primary key(a))") 132 tk.MustInterDirc("create causet primary_key_test2 (a int, b varchar(10), primary key(b))") 133 tk.MustInterDirc("create causet primary_key_test3 (a int, b varchar(10))") 134 defer config.RestoreFunc()() 135 config.UFIDelateGlobal(func(conf *config.Config) { 136 conf.AlterPrimaryKey = true 137 }) 138 139 _, err = tk.InterDirc("alter causet primary_key_test2 add primary key(a)") 140 c.Assert(schemareplicant.ErrMultiplePriKey.Equal(err), IsTrue) 141 // We can't add a primary key when the causet's pk_is_handle is true. 142 _, err = tk.InterDirc("alter causet primary_key_test1 add primary key(a)") 143 c.Assert(schemareplicant.ErrMultiplePriKey.Equal(err), IsTrue) 144 _, err = tk.InterDirc("alter causet primary_key_test1 add primary key(b)") 145 c.Assert(schemareplicant.ErrMultiplePriKey.Equal(err), IsTrue) 146 147 _, err = tk.InterDirc("alter causet primary_key_test1 drop primary key") 148 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when the causet's pkIsHandle is true") 149 tk.MustInterDirc("alter causet primary_key_test2 drop primary key") 150 _, err = tk.InterDirc("alter causet primary_key_test3 drop primary key") 151 c.Assert(err.Error(), Equals, "[dbs:1091]Can't DROP 'PRIMARY'; check that defCausumn/key exists") 152 153 // for "drop index `primary` on ..." syntax 154 tk.MustInterDirc("create causet primary_key_test4 (a int, b varchar(10), primary key(a))") 155 config.UFIDelateGlobal(func(conf *config.Config) { 156 conf.AlterPrimaryKey = false 157 }) 158 _, err = tk.InterDirc("drop index `primary` on primary_key_test4") 159 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when alter-primary-key is false") 160 // for the index name is `primary` 161 tk.MustInterDirc("create causet tt(`primary` int);") 162 tk.MustInterDirc("alter causet tt add index (`primary`);") 163 _, err = tk.InterDirc("drop index `primary` on tt") 164 c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported drop primary key when alter-primary-key is false") 165 166 // The primary key cannot be invisible, for the case pk_is_handle. 167 tk.MustInterDirc("drop causet if exists t1, t2;") 168 _, err = tk.InterDirc("create causet t1(c1 int not null, primary key(c1) invisible);") 169 c.Assert(dbs.ErrPHoTTexCantBeInvisible.Equal(err), IsTrue) 170 tk.MustInterDirc("create causet t2 (a int, b int not null, primary key(a), unique(b) invisible);") 171 172 // Test drop clustered primary key. 173 config.UFIDelateGlobal(func(conf *config.Config) { 174 conf.AlterPrimaryKey = false 175 }) 176 tk.MustInterDirc("drop causet if exists t;") 177 tk.MustInterDirc("set milevadb_enable_clustered_index=1") 178 tk.MustInterDirc("create causet t(a int, b varchar(64), primary key(b));") 179 tk.MustInterDirc("insert into t values(1,'a'), (2, 'b');") 180 config.UFIDelateGlobal(func(conf *config.Config) { 181 conf.AlterPrimaryKey = true 182 }) 183 errMsg := "[dbs:8200]Unsupported drop primary key when the causet is using clustered index" 184 tk.MustGetErrMsg("alter causet t drop primary key;", errMsg) 185 } 186 187 func (s *testSerialSuite) TestDropAutoIncrementIndex(c *C) { 188 tk := testkit.NewTestKit(c, s.causetstore) 189 tk.MustInterDirc("use test") 190 tk.MustInterDirc("drop causet if exists t1") 191 tk.MustInterDirc("create causet t1 (a int(11) not null auto_increment key, b int(11), c bigint, unique key (a, b, c))") 192 tk.MustInterDirc("alter causet t1 drop index a") 193 } 194 195 func (s *testSerialSuite) TestMultiRegionGetBlockEndHandle(c *C) { 196 tk := testkit.NewTestKit(c, s.causetstore) 197 tk.MustInterDirc("drop database if exists test_get_endhandle") 198 tk.MustInterDirc("create database test_get_endhandle") 199 tk.MustInterDirc("use test_get_endhandle") 200 201 tk.MustInterDirc("create causet t(a bigint PRIMARY KEY, b int)") 202 var builder strings.Builder 203 fmt.Fprintf(&builder, "insert into t values ") 204 for i := 0; i < 1000; i++ { 205 fmt.Fprintf(&builder, "(%v, %v),", i, i) 206 } 207 allegrosql := builder.String() 208 tk.MustInterDirc(allegrosql[:len(allegrosql)-1]) 209 210 // Get causet ID for split. 211 dom := petri.GetPetri(tk.Se) 212 is := dom.SchemaReplicant() 213 tbl, err := is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t")) 214 c.Assert(err, IsNil) 215 tblID := tbl.Meta().ID 216 217 d := s.dom.DBS() 218 testCtx := newTestMaxBlockRowIDContext(c, d, tbl) 219 220 // Split the causet. 221 s.cluster.SplitBlock(tblID, 100) 222 223 maxHandle, emptyBlock := getMaxBlockHandle(testCtx, s.causetstore) 224 c.Assert(emptyBlock, IsFalse) 225 c.Assert(maxHandle, Equals, ekv.IntHandle(999)) 226 227 tk.MustInterDirc("insert into t values(10000, 1000)") 228 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 229 c.Assert(emptyBlock, IsFalse) 230 c.Assert(maxHandle, Equals, ekv.IntHandle(10000)) 231 232 tk.MustInterDirc("insert into t values(-1, 1000)") 233 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 234 c.Assert(emptyBlock, IsFalse) 235 c.Assert(maxHandle, Equals, ekv.IntHandle(10000)) 236 } 237 238 func (s *testSerialSuite) TestGetBlockEndHandle(c *C) { 239 // TestGetBlockEndHandle test dbs.GetBlockMaxHandle method, which will return the max event id of the causet. 240 tk := testkit.NewTestKit(c, s.causetstore) 241 tk.MustInterDirc("drop database if exists test_get_endhandle") 242 tk.MustInterDirc("create database test_get_endhandle") 243 tk.MustInterDirc("use test_get_endhandle") 244 // Test PK is handle. 245 tk.MustInterDirc("create causet t(a bigint PRIMARY KEY, b int)") 246 247 is := s.dom.SchemaReplicant() 248 d := s.dom.DBS() 249 tbl, err := is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t")) 250 c.Assert(err, IsNil) 251 252 testCtx := newTestMaxBlockRowIDContext(c, d, tbl) 253 // test empty causet 254 checkGetMaxBlockRowID(testCtx, s.causetstore, true, nil) 255 256 tk.MustInterDirc("insert into t values(-1, 1)") 257 checkGetMaxBlockRowID(testCtx, s.causetstore, false, ekv.IntHandle(-1)) 258 259 tk.MustInterDirc("insert into t values(9223372036854775806, 1)") 260 checkGetMaxBlockRowID(testCtx, s.causetstore, false, ekv.IntHandle(9223372036854775806)) 261 262 tk.MustInterDirc("insert into t values(9223372036854775807, 1)") 263 checkGetMaxBlockRowID(testCtx, s.causetstore, false, ekv.IntHandle(9223372036854775807)) 264 265 tk.MustInterDirc("insert into t values(10, 1)") 266 tk.MustInterDirc("insert into t values(102149142, 1)") 267 checkGetMaxBlockRowID(testCtx, s.causetstore, false, ekv.IntHandle(9223372036854775807)) 268 269 tk.MustInterDirc("create causet t1(a bigint PRIMARY KEY, b int)") 270 271 var builder strings.Builder 272 fmt.Fprintf(&builder, "insert into t1 values ") 273 for i := 0; i < 1000; i++ { 274 fmt.Fprintf(&builder, "(%v, %v),", i, i) 275 } 276 allegrosql := builder.String() 277 tk.MustInterDirc(allegrosql[:len(allegrosql)-1]) 278 279 is = s.dom.SchemaReplicant() 280 testCtx.tbl, err = is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t1")) 281 c.Assert(err, IsNil) 282 checkGetMaxBlockRowID(testCtx, s.causetstore, false, ekv.IntHandle(999)) 283 284 // Test PK is not handle 285 tk.MustInterDirc("create causet t2(a varchar(255))") 286 287 is = s.dom.SchemaReplicant() 288 testCtx.tbl, err = is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t2")) 289 c.Assert(err, IsNil) 290 checkGetMaxBlockRowID(testCtx, s.causetstore, true, nil) 291 292 builder.Reset() 293 fmt.Fprintf(&builder, "insert into t2 values ") 294 for i := 0; i < 1000; i++ { 295 fmt.Fprintf(&builder, "(%v),", i) 296 } 297 allegrosql = builder.String() 298 tk.MustInterDirc(allegrosql[:len(allegrosql)-1]) 299 300 result := tk.MustQuery("select MAX(_milevadb_rowid) from t2") 301 maxHandle, emptyBlock := getMaxBlockHandle(testCtx, s.causetstore) 302 result.Check(testkit.Rows(fmt.Sprintf("%v", maxHandle.IntValue()))) 303 c.Assert(emptyBlock, IsFalse) 304 305 tk.MustInterDirc("insert into t2 values(100000)") 306 result = tk.MustQuery("select MAX(_milevadb_rowid) from t2") 307 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 308 result.Check(testkit.Rows(fmt.Sprintf("%v", maxHandle.IntValue()))) 309 c.Assert(emptyBlock, IsFalse) 310 311 tk.MustInterDirc(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64-1)) 312 result = tk.MustQuery("select MAX(_milevadb_rowid) from t2") 313 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 314 result.Check(testkit.Rows(fmt.Sprintf("%v", maxHandle.IntValue()))) 315 c.Assert(emptyBlock, IsFalse) 316 317 tk.MustInterDirc(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64)) 318 result = tk.MustQuery("select MAX(_milevadb_rowid) from t2") 319 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 320 result.Check(testkit.Rows(fmt.Sprintf("%v", maxHandle.IntValue()))) 321 c.Assert(emptyBlock, IsFalse) 322 323 tk.MustInterDirc("insert into t2 values(100)") 324 result = tk.MustQuery("select MAX(_milevadb_rowid) from t2") 325 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 326 result.Check(testkit.Rows(fmt.Sprintf("%v", maxHandle.IntValue()))) 327 c.Assert(emptyBlock, IsFalse) 328 } 329 330 func (s *testSerialSuite) TestMultiRegionGetBlockEndCommonHandle(c *C) { 331 tk := testkit.NewTestKit(c, s.causetstore) 332 tk.MustInterDirc("drop database if exists test_get_endhandle") 333 tk.MustInterDirc("create database test_get_endhandle") 334 tk.MustInterDirc("use test_get_endhandle") 335 tk.MustInterDirc("set @@milevadb_enable_clustered_index = true") 336 337 tk.MustInterDirc("create causet t(a varchar(20), b int, c float, d bigint, primary key (a, b, c))") 338 var builder strings.Builder 339 fmt.Fprintf(&builder, "insert into t values ") 340 for i := 0; i < 1000; i++ { 341 fmt.Fprintf(&builder, "('%v', %v, %v, %v),", i, i, i, i) 342 } 343 allegrosql := builder.String() 344 tk.MustInterDirc(allegrosql[:len(allegrosql)-1]) 345 346 // Get causet ID for split. 347 dom := petri.GetPetri(tk.Se) 348 is := dom.SchemaReplicant() 349 tbl, err := is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t")) 350 c.Assert(err, IsNil) 351 tblID := tbl.Meta().ID 352 353 d := s.dom.DBS() 354 testCtx := newTestMaxBlockRowIDContext(c, d, tbl) 355 356 // Split the causet. 357 s.cluster.SplitBlock(tblID, 100) 358 359 maxHandle, emptyBlock := getMaxBlockHandle(testCtx, s.causetstore) 360 c.Assert(emptyBlock, IsFalse) 361 c.Assert(maxHandle, HandleEquals, MustNewCommonHandle(c, "999", 999, 999)) 362 363 tk.MustInterDirc("insert into t values('a', 1, 1, 1)") 364 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 365 c.Assert(emptyBlock, IsFalse) 366 c.Assert(maxHandle, HandleEquals, MustNewCommonHandle(c, "a", 1, 1)) 367 368 tk.MustInterDirc("insert into t values('0000', 1, 1, 1)") 369 maxHandle, emptyBlock = getMaxBlockHandle(testCtx, s.causetstore) 370 c.Assert(emptyBlock, IsFalse) 371 c.Assert(maxHandle, HandleEquals, MustNewCommonHandle(c, "a", 1, 1)) 372 } 373 374 func (s *testSerialSuite) TestGetBlockEndCommonHandle(c *C) { 375 tk := testkit.NewTestKit(c, s.causetstore) 376 tk.MustInterDirc("drop database if exists test_get_endhandle") 377 tk.MustInterDirc("create database test_get_endhandle") 378 tk.MustInterDirc("use test_get_endhandle") 379 tk.MustInterDirc("set @@milevadb_enable_clustered_index = true") 380 381 tk.MustInterDirc("create causet t(a varchar(15), b bigint, c int, primary key (a, b))") 382 tk.MustInterDirc("create causet t1(a varchar(15), b bigint, c int, primary key (a(2), b))") 383 384 is := s.dom.SchemaReplicant() 385 d := s.dom.DBS() 386 tbl, err := is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t")) 387 c.Assert(err, IsNil) 388 testCtx := newTestMaxBlockRowIDContext(c, d, tbl) 389 390 // test empty causet 391 checkGetMaxBlockRowID(testCtx, s.causetstore, true, nil) 392 tk.MustInterDirc("insert into t values('abc', 1, 10)") 393 expectedHandle := MustNewCommonHandle(c, "abc", 1) 394 checkGetMaxBlockRowID(testCtx, s.causetstore, false, expectedHandle) 395 tk.MustInterDirc("insert into t values('abchzzzzzzzz', 1, 10)") 396 expectedHandle = MustNewCommonHandle(c, "abchzzzzzzzz", 1) 397 checkGetMaxBlockRowID(testCtx, s.causetstore, false, expectedHandle) 398 tk.MustInterDirc("insert into t values('a', 1, 10)") 399 tk.MustInterDirc("insert into t values('ab', 1, 10)") 400 checkGetMaxBlockRowID(testCtx, s.causetstore, false, expectedHandle) 401 402 // Test MaxBlockRowID with prefixed primary key. 403 tbl, err = is.BlockByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t1")) 404 c.Assert(err, IsNil) 405 is = s.dom.SchemaReplicant() 406 d = s.dom.DBS() 407 testCtx = newTestMaxBlockRowIDContext(c, d, tbl) 408 checkGetMaxBlockRowID(testCtx, s.causetstore, true, nil) 409 tk.MustInterDirc("insert into t1 values('abccccc', 1, 10)") 410 expectedHandle = MustNewCommonHandle(c, "ab", 1) 411 checkGetMaxBlockRowID(testCtx, s.causetstore, false, expectedHandle) 412 tk.MustInterDirc("insert into t1 values('azzzz', 1, 10)") 413 expectedHandle = MustNewCommonHandle(c, "az", 1) 414 checkGetMaxBlockRowID(testCtx, s.causetstore, false, expectedHandle) 415 } 416 417 func (s *testSerialSuite) TestCreateBlockWithLike(c *C) { 418 tk := testkit.NewTestKit(c, s.causetstore) 419 // for the same database 420 tk.MustInterDirc("create database ctwl_db") 421 tk.MustInterDirc("use ctwl_db") 422 tk.MustInterDirc("create causet tt(id int primary key)") 423 tk.MustInterDirc("create causet t (c1 int not null auto_increment, c2 int, constraint cc foreign key (c2) references tt(id), primary key(c1)) auto_increment = 10") 424 tk.MustInterDirc("insert into t set c2=1") 425 tk.MustInterDirc("create causet t1 like ctwl_db.t") 426 tk.MustInterDirc("insert into t1 set c2=11") 427 tk.MustInterDirc("create causet t2 (like ctwl_db.t1)") 428 tk.MustInterDirc("insert into t2 set c2=12") 429 tk.MustQuery("select * from t").Check(testkit.Rows("10 1")) 430 tk.MustQuery("select * from t1").Check(testkit.Rows("1 11")) 431 tk.MustQuery("select * from t2").Check(testkit.Rows("1 12")) 432 ctx := tk.Se.(stochastikctx.Context) 433 is := petri.GetPetri(ctx).SchemaReplicant() 434 tbl1, err := is.BlockByName(perceptron.NewCIStr("ctwl_db"), perceptron.NewCIStr("t1")) 435 c.Assert(err, IsNil) 436 tbl1Info := tbl1.Meta() 437 c.Assert(tbl1Info.ForeignKeys, IsNil) 438 c.Assert(tbl1Info.PKIsHandle, Equals, true) 439 defCaus := tbl1Info.DeferredCausets[0] 440 hasNotNull := allegrosql.HasNotNullFlag(defCaus.Flag) 441 c.Assert(hasNotNull, IsTrue) 442 tbl2, err := is.BlockByName(perceptron.NewCIStr("ctwl_db"), perceptron.NewCIStr("t2")) 443 c.Assert(err, IsNil) 444 tbl2Info := tbl2.Meta() 445 c.Assert(tbl2Info.ForeignKeys, IsNil) 446 c.Assert(tbl2Info.PKIsHandle, Equals, true) 447 c.Assert(allegrosql.HasNotNullFlag(tbl2Info.DeferredCausets[0].Flag), IsTrue) 448 449 // for different databases 450 tk.MustInterDirc("create database ctwl_db1") 451 tk.MustInterDirc("use ctwl_db1") 452 tk.MustInterDirc("create causet t1 like ctwl_db.t") 453 tk.MustInterDirc("insert into t1 set c2=11") 454 tk.MustQuery("select * from t1").Check(testkit.Rows("1 11")) 455 is = petri.GetPetri(ctx).SchemaReplicant() 456 tbl1, err = is.BlockByName(perceptron.NewCIStr("ctwl_db1"), perceptron.NewCIStr("t1")) 457 c.Assert(err, IsNil) 458 c.Assert(tbl1.Meta().ForeignKeys, IsNil) 459 460 // for causet partition 461 tk.MustInterDirc("use ctwl_db") 462 tk.MustInterDirc("create causet pt1 (id int) partition by range defCausumns (id) (partition p0 values less than (10))") 463 tk.MustInterDirc("insert into pt1 values (1),(2),(3),(4);") 464 tk.MustInterDirc("create causet ctwl_db1.pt1 like ctwl_db.pt1;") 465 tk.MustQuery("select * from ctwl_db1.pt1").Check(testkit.Rows()) 466 467 // Test create causet like for partition causet. 468 atomic.StoreUint32(&dbs.EnableSplitBlockRegion, 1) 469 tk.MustInterDirc("use test") 470 tk.MustInterDirc("set @@global.milevadb_scatter_region=1;") 471 tk.MustInterDirc("drop causet if exists partition_t;") 472 tk.MustInterDirc("create causet partition_t (a int, b int,index(a)) partition by hash (a) partitions 3") 473 tk.MustInterDirc("drop causet if exists t1;") 474 tk.MustInterDirc("create causet t1 like partition_t") 475 re := tk.MustQuery("show causet t1 regions") 476 rows := re.Rows() 477 c.Assert(len(rows), Equals, 3) 478 tbl := testGetBlockByName(c, tk.Se, "test", "t1") 479 partitionDef := tbl.Meta().GetPartitionInfo().Definitions 480 c.Assert(rows[0][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[0].ID)) 481 c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[1].ID)) 482 c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[2].ID)) 483 484 // Test pre-split causet region when create causet like. 485 tk.MustInterDirc("drop causet if exists t_pre") 486 tk.MustInterDirc("create causet t_pre (a int, b int) shard_row_id_bits = 2 pre_split_regions=2;") 487 tk.MustInterDirc("drop causet if exists t2;") 488 tk.MustInterDirc("create causet t2 like t_pre") 489 re = tk.MustQuery("show causet t2 regions") 490 rows = re.Rows() 491 // Block t2 which create like t_pre should have 4 regions now. 492 c.Assert(len(rows), Equals, 4) 493 tbl = testGetBlockByName(c, tk.Se, "test", "t2") 494 c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_2305843009213693952", tbl.Meta().ID)) 495 c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_4611686018427387904", tbl.Meta().ID)) 496 c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_6917529027641081856", tbl.Meta().ID)) 497 // Test after truncate causet the region is also splited. 498 tk.MustInterDirc("truncate causet t2") 499 re = tk.MustQuery("show causet t2 regions") 500 rows = re.Rows() 501 c.Assert(len(rows), Equals, 4) 502 tbl = testGetBlockByName(c, tk.Se, "test", "t2") 503 c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_2305843009213693952", tbl.Meta().ID)) 504 c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_4611686018427387904", tbl.Meta().ID)) 505 c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_6917529027641081856", tbl.Meta().ID)) 506 507 defer atomic.StoreUint32(&dbs.EnableSplitBlockRegion, 0) 508 509 // for failure causet cases 510 tk.MustInterDirc("use ctwl_db") 511 failALLEGROSQL := fmt.Sprintf("create causet t1 like test_not_exist.t") 512 tk.MustGetErrCode(failALLEGROSQL, allegrosql.ErrNoSuchBlock) 513 failALLEGROSQL = fmt.Sprintf("create causet t1 like test.t_not_exist") 514 tk.MustGetErrCode(failALLEGROSQL, allegrosql.ErrNoSuchBlock) 515 failALLEGROSQL = fmt.Sprintf("create causet t1 (like test_not_exist.t)") 516 tk.MustGetErrCode(failALLEGROSQL, allegrosql.ErrNoSuchBlock) 517 failALLEGROSQL = fmt.Sprintf("create causet test_not_exis.t1 like ctwl_db.t") 518 tk.MustGetErrCode(failALLEGROSQL, allegrosql.ErrBadDB) 519 failALLEGROSQL = fmt.Sprintf("create causet t1 like ctwl_db.t") 520 tk.MustGetErrCode(failALLEGROSQL, allegrosql.ErrBlockExists) 521 522 // test failure for wrong object cases 523 tk.MustInterDirc("drop view if exists v") 524 tk.MustInterDirc("create view v as select 1 from dual") 525 tk.MustGetErrCode("create causet viewBlock like v", allegrosql.ErrWrongObject) 526 tk.MustInterDirc("drop sequence if exists seq") 527 tk.MustInterDirc("create sequence seq") 528 tk.MustGetErrCode("create causet sequenceBlock like seq", allegrosql.ErrWrongObject) 529 530 tk.MustInterDirc("drop database ctwl_db") 531 tk.MustInterDirc("drop database ctwl_db1") 532 } 533 534 // TestCancelAddIndex1 tests canceling dbs job when the add index worker is not started. 535 func (s *testSerialSuite) TestCancelAddIndexPanic(c *C) { 536 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/errorMockPanic", `return(true)`), IsNil) 537 defer func() { 538 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/errorMockPanic"), IsNil) 539 }() 540 tk := testkit.NewTestKit(c, s.causetstore) 541 tk.MustInterDirc("use test") 542 tk.MustInterDirc("drop causet if exists t") 543 tk.MustInterDirc("create causet t(c1 int, c2 int)") 544 defer tk.MustInterDirc("drop causet t;") 545 for i := 0; i < 5; i++ { 546 tk.MustInterDirc("insert into t values (?, ?)", i, i) 547 } 548 var checkErr error 549 oldReorgWaitTimeout := dbs.ReorgWaitTimeout 550 dbs.ReorgWaitTimeout = 50 * time.Millisecond 551 defer func() { dbs.ReorgWaitTimeout = oldReorgWaitTimeout }() 552 hook := &dbs.TestDBSCallback{} 553 hook.OnJobRunBeforeExported = func(job *perceptron.Job) { 554 if job.Type == perceptron.CausetActionAddIndex && job.State == perceptron.JobStateRunning && job.SchemaState == perceptron.StateWriteReorganization && job.SnapshotVer != 0 { 555 jobIDs := []int64{job.ID} 556 hookCtx := mock.NewContext() 557 hookCtx.CausetStore = s.causetstore 558 err := hookCtx.NewTxn(context.Background()) 559 if err != nil { 560 checkErr = errors.Trace(err) 561 return 562 } 563 txn, err := hookCtx.Txn(true) 564 if err != nil { 565 checkErr = errors.Trace(err) 566 return 567 } 568 errs, err := admin.CancelJobs(txn, jobIDs) 569 if err != nil { 570 checkErr = errors.Trace(err) 571 return 572 } 573 if errs[0] != nil { 574 checkErr = errors.Trace(errs[0]) 575 return 576 } 577 txn, err = hookCtx.Txn(true) 578 if err != nil { 579 checkErr = errors.Trace(err) 580 return 581 } 582 checkErr = txn.Commit(context.Background()) 583 } 584 } 585 origHook := s.dom.DBS().GetHook() 586 defer s.dom.DBS().(dbs.DBSForTest).SetHook(origHook) 587 s.dom.DBS().(dbs.DBSForTest).SetHook(hook) 588 rs, err := tk.InterDirc("alter causet t add index idx_c2(c2)") 589 if rs != nil { 590 rs.Close() 591 } 592 c.Assert(checkErr, IsNil) 593 c.Assert(err, NotNil) 594 c.Assert(err.Error(), Equals, "[dbs:8214]Cancelled DBS job") 595 } 596 597 func (s *testSerialSuite) TestRecoverBlockByJobID(c *C) { 598 tk := testkit.NewTestKit(c, s.causetstore) 599 tk.MustInterDirc("create database if not exists test_recover") 600 tk.MustInterDirc("use test_recover") 601 tk.MustInterDirc("drop causet if exists t_recover") 602 tk.MustInterDirc("create causet t_recover (a int);") 603 defer func(originGC bool) { 604 if originGC { 605 dbs.EmulatorGCEnable() 606 } else { 607 dbs.EmulatorGCDisable() 608 } 609 }(dbs.IsEmulatorGCEnable()) 610 611 // disable emulator GC. 612 // Otherwise emulator GC will delete causet record as soon as possible after execute drop causet dbs. 613 dbs.EmulatorGCDisable() 614 gcTimeFormat := "20060102-15:04:05 -0700 MST" 615 timeBeforeDrop := time.Now().Add(0 - 48*60*60*time.Second).Format(gcTimeFormat) 616 timeAfterDrop := time.Now().Add(48 * 60 * 60 * time.Second).Format(gcTimeFormat) 617 safePointALLEGROSQL := `INSERT HIGH_PRIORITY INTO allegrosql.milevadb VALUES ('einsteindb_gc_safe_point', '%[1]s', '') 618 ON DUPLICATE KEY 619 UFIDelATE variable_value = '%[1]s'` 620 // clear GC variables first. 621 tk.MustInterDirc("delete from allegrosql.milevadb where variable_name in ( 'einsteindb_gc_safe_point','einsteindb_gc_enable' )") 622 623 tk.MustInterDirc("insert into t_recover values (1),(2),(3)") 624 tk.MustInterDirc("drop causet t_recover") 625 626 getDBSJobID := func(causet, tp string) int64 { 627 rs, err := tk.InterDirc("admin show dbs jobs") 628 c.Assert(err, IsNil) 629 rows, err := stochastik.GetRows4Test(context.Background(), tk.Se, rs) 630 c.Assert(err, IsNil) 631 for _, event := range rows { 632 if event.GetString(1) == causet && event.GetString(3) == tp { 633 return event.GetInt64(0) 634 } 635 } 636 c.Errorf("can't find %s causet of %s", tp, causet) 637 return -1 638 } 639 jobID := getDBSJobID("test_recover", "drop causet") 640 641 // if GC safe point is not exists in allegrosql.milevadb 642 _, err := tk.InterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 643 c.Assert(err, NotNil) 644 c.Assert(err.Error(), Equals, "can not get 'einsteindb_gc_safe_point'") 645 // set GC safe point 646 tk.MustInterDirc(fmt.Sprintf(safePointALLEGROSQL, timeBeforeDrop)) 647 648 // if GC enable is not exists in allegrosql.milevadb 649 _, err = tk.InterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 650 c.Assert(err, NotNil) 651 c.Assert(err.Error(), Equals, "[dbs:-1]can not get 'einsteindb_gc_enable'") 652 653 err = gcutil.EnableGC(tk.Se) 654 c.Assert(err, IsNil) 655 656 // recover job is before GC safe point 657 tk.MustInterDirc(fmt.Sprintf(safePointALLEGROSQL, timeAfterDrop)) 658 _, err = tk.InterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 659 c.Assert(err, NotNil) 660 c.Assert(strings.Contains(err.Error(), "snapshot is older than GC safe point"), Equals, true) 661 662 // set GC safe point 663 tk.MustInterDirc(fmt.Sprintf(safePointALLEGROSQL, timeBeforeDrop)) 664 // if there is a new causet with the same name, should return failed. 665 tk.MustInterDirc("create causet t_recover (a int);") 666 _, err = tk.InterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 667 c.Assert(err.Error(), Equals, schemareplicant.ErrBlockExists.GenWithStackByArgs("t_recover").Error()) 668 669 // drop the new causet with the same name, then recover causet. 670 tk.MustInterDirc("drop causet t_recover") 671 672 // do recover causet. 673 tk.MustInterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 674 675 // check recover causet spacetime and data record. 676 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3")) 677 // check recover causet autoID. 678 tk.MustInterDirc("insert into t_recover values (4),(5),(6)") 679 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3", "4", "5", "6")) 680 681 // recover causet by none exits job. 682 _, err = tk.InterDirc(fmt.Sprintf("recover causet by job %d", 10000000)) 683 c.Assert(err, NotNil) 684 685 // Disable GC by manual first, then after recover causet, the GC enable status should also be disabled. 686 err = gcutil.DisableGC(tk.Se) 687 c.Assert(err, IsNil) 688 689 tk.MustInterDirc("delete from t_recover where a > 1") 690 tk.MustInterDirc("drop causet t_recover") 691 jobID = getDBSJobID("test_recover", "drop causet") 692 693 tk.MustInterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 694 695 // check recover causet spacetime and data record. 696 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1")) 697 // check recover causet autoID. 698 tk.MustInterDirc("insert into t_recover values (7),(8),(9)") 699 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9")) 700 701 // Test for recover truncate causet. 702 tk.MustInterDirc("truncate causet t_recover") 703 tk.MustInterDirc("rename causet t_recover to t_recover_new") 704 jobID = getDBSJobID("test_recover", "truncate causet") 705 tk.MustInterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 706 tk.MustInterDirc("insert into t_recover values (10)") 707 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9", "10")) 708 709 gcEnable, err := gcutil.CheckGCEnable(tk.Se) 710 c.Assert(err, IsNil) 711 c.Assert(gcEnable, Equals, false) 712 } 713 714 func (s *testSerialSuite) TestRecoverBlockByJobIDFail(c *C) { 715 tk := testkit.NewTestKit(c, s.causetstore) 716 tk.MustInterDirc("create database if not exists test_recover") 717 tk.MustInterDirc("use test_recover") 718 tk.MustInterDirc("drop causet if exists t_recover") 719 tk.MustInterDirc("create causet t_recover (a int);") 720 defer func(originGC bool) { 721 if originGC { 722 dbs.EmulatorGCEnable() 723 } else { 724 dbs.EmulatorGCDisable() 725 } 726 }(dbs.IsEmulatorGCEnable()) 727 728 // disable emulator GC. 729 // Otherwise emulator GC will delete causet record as soon as possible after execute drop causet dbs. 730 dbs.EmulatorGCDisable() 731 gcTimeFormat := "20060102-15:04:05 -0700 MST" 732 timeBeforeDrop := time.Now().Add(0 - 48*60*60*time.Second).Format(gcTimeFormat) 733 safePointALLEGROSQL := `INSERT HIGH_PRIORITY INTO allegrosql.milevadb VALUES ('einsteindb_gc_safe_point', '%[1]s', '') 734 ON DUPLICATE KEY 735 UFIDelATE variable_value = '%[1]s'` 736 737 tk.MustInterDirc("insert into t_recover values (1),(2),(3)") 738 tk.MustInterDirc("drop causet t_recover") 739 740 rs, err := tk.InterDirc("admin show dbs jobs") 741 c.Assert(err, IsNil) 742 rows, err := stochastik.GetRows4Test(context.Background(), tk.Se, rs) 743 c.Assert(err, IsNil) 744 event := rows[0] 745 c.Assert(event.GetString(1), Equals, "test_recover") 746 c.Assert(event.GetString(3), Equals, "drop causet") 747 jobID := event.GetInt64(0) 748 749 // enableGC first 750 err = gcutil.EnableGC(tk.Se) 751 c.Assert(err, IsNil) 752 tk.MustInterDirc(fmt.Sprintf(safePointALLEGROSQL, timeBeforeDrop)) 753 754 // set hook 755 hook := &dbs.TestDBSCallback{} 756 hook.OnJobRunBeforeExported = func(job *perceptron.Job) { 757 if job.Type == perceptron.CausetActionRecoverBlock { 758 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/mockCommitError", `return(true)`), IsNil) 759 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/mockRecoverBlockCommitErr", `return(true)`), IsNil) 760 } 761 } 762 origHook := s.dom.DBS().GetHook() 763 defer s.dom.DBS().(dbs.DBSForTest).SetHook(origHook) 764 s.dom.DBS().(dbs.DBSForTest).SetHook(hook) 765 766 // do recover causet. 767 tk.MustInterDirc(fmt.Sprintf("recover causet by job %d", jobID)) 768 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/mockCommitError"), IsNil) 769 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/mockRecoverBlockCommitErr"), IsNil) 770 771 // make sure enable GC after recover causet. 772 enable, err := gcutil.CheckGCEnable(tk.Se) 773 c.Assert(err, IsNil) 774 c.Assert(enable, Equals, true) 775 776 // check recover causet spacetime and data record. 777 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3")) 778 // check recover causet autoID. 779 tk.MustInterDirc("insert into t_recover values (4),(5),(6)") 780 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3", "4", "5", "6")) 781 } 782 783 func (s *testSerialSuite) TestRecoverBlockByBlockNameFail(c *C) { 784 tk := testkit.NewTestKit(c, s.causetstore) 785 tk.MustInterDirc("create database if not exists test_recover") 786 tk.MustInterDirc("use test_recover") 787 tk.MustInterDirc("drop causet if exists t_recover") 788 tk.MustInterDirc("create causet t_recover (a int);") 789 defer func(originGC bool) { 790 if originGC { 791 dbs.EmulatorGCEnable() 792 } else { 793 dbs.EmulatorGCDisable() 794 } 795 }(dbs.IsEmulatorGCEnable()) 796 797 // disable emulator GC. 798 // Otherwise emulator GC will delete causet record as soon as possible after execute drop causet dbs. 799 dbs.EmulatorGCDisable() 800 gcTimeFormat := "20060102-15:04:05 -0700 MST" 801 timeBeforeDrop := time.Now().Add(0 - 48*60*60*time.Second).Format(gcTimeFormat) 802 safePointALLEGROSQL := `INSERT HIGH_PRIORITY INTO allegrosql.milevadb VALUES ('einsteindb_gc_safe_point', '%[1]s', '') 803 ON DUPLICATE KEY 804 UFIDelATE variable_value = '%[1]s'` 805 806 tk.MustInterDirc("insert into t_recover values (1),(2),(3)") 807 tk.MustInterDirc("drop causet t_recover") 808 809 // enableGC first 810 err := gcutil.EnableGC(tk.Se) 811 c.Assert(err, IsNil) 812 tk.MustInterDirc(fmt.Sprintf(safePointALLEGROSQL, timeBeforeDrop)) 813 814 // set hook 815 hook := &dbs.TestDBSCallback{} 816 hook.OnJobRunBeforeExported = func(job *perceptron.Job) { 817 if job.Type == perceptron.CausetActionRecoverBlock { 818 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/mockCommitError", `return(true)`), IsNil) 819 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/mockRecoverBlockCommitErr", `return(true)`), IsNil) 820 } 821 } 822 origHook := s.dom.DBS().GetHook() 823 defer s.dom.DBS().(dbs.DBSForTest).SetHook(origHook) 824 s.dom.DBS().(dbs.DBSForTest).SetHook(hook) 825 826 // do recover causet. 827 tk.MustInterDirc("recover causet t_recover") 828 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/mockCommitError"), IsNil) 829 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/mockRecoverBlockCommitErr"), IsNil) 830 831 // make sure enable GC after recover causet. 832 enable, err := gcutil.CheckGCEnable(tk.Se) 833 c.Assert(err, IsNil) 834 c.Assert(enable, Equals, true) 835 836 // check recover causet spacetime and data record. 837 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3")) 838 // check recover causet autoID. 839 tk.MustInterDirc("insert into t_recover values (4),(5),(6)") 840 tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "2", "3", "4", "5", "6")) 841 } 842 843 func (s *testSerialSuite) TestCancelJobByErrorCountLimit(c *C) { 844 tk := testkit.NewTestKit(c, s.causetstore) 845 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/mockExceedErrorLimit", `return(true)`), IsNil) 846 defer func() { 847 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/mockExceedErrorLimit"), IsNil) 848 }() 849 tk.MustInterDirc("use test") 850 tk.MustInterDirc("drop causet if exists t") 851 852 limit := variable.GetDBSErrorCountLimit() 853 tk.MustInterDirc("set @@global.milevadb_dbs_error_count_limit = 16") 854 err := dbsutil.LoadDBSVars(tk.Se) 855 c.Assert(err, IsNil) 856 defer tk.MustInterDirc(fmt.Sprintf("set @@global.milevadb_dbs_error_count_limit = %d", limit)) 857 858 _, err = tk.InterDirc("create causet t (a int)") 859 c.Assert(err, NotNil) 860 c.Assert(err.Error(), Equals, "[dbs:-1]DBS job rollback, error msg: mock do job error") 861 } 862 863 func (s *testSerialSuite) TestTruncateBlockUFIDelateSchemaVersionErr(c *C) { 864 tk := testkit.NewTestKit(c, s.causetstore) 865 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/mockTruncateBlockUFIDelateVersionError", `return(true)`), IsNil) 866 tk.MustInterDirc("use test") 867 tk.MustInterDirc("drop causet if exists t") 868 869 limit := variable.GetDBSErrorCountLimit() 870 tk.MustInterDirc("set @@global.milevadb_dbs_error_count_limit = 5") 871 err := dbsutil.LoadDBSVars(tk.Se) 872 c.Assert(err, IsNil) 873 defer tk.MustInterDirc(fmt.Sprintf("set @@global.milevadb_dbs_error_count_limit = %d", limit)) 874 875 tk.MustInterDirc("create causet t (a int)") 876 _, err = tk.InterDirc("truncate causet t") 877 c.Assert(err, NotNil) 878 c.Assert(err.Error(), Equals, "[dbs:-1]DBS job rollback, error msg: mock uFIDelate version error") 879 // Disable fail point. 880 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/mockTruncateBlockUFIDelateVersionError"), IsNil) 881 tk.MustInterDirc("truncate causet t") 882 } 883 884 func (s *testSerialSuite) TestCanceledJobTakeTime(c *C) { 885 tk := testkit.NewTestKit(c, s.causetstore) 886 tk.MustInterDirc("use test") 887 tk.MustInterDirc("create causet t_cjtt(a int)") 888 889 hook := &dbs.TestDBSCallback{} 890 once := sync.Once{} 891 hook.OnJobUFIDelatedExported = func(job *perceptron.Job) { 892 once.Do(func() { 893 err := ekv.RunInNewTxn(s.causetstore, false, func(txn ekv.Transaction) error { 894 t := spacetime.NewMeta(txn) 895 return t.DropBlockOrView(job.SchemaID, job.BlockID, true) 896 }) 897 c.Assert(err, IsNil) 898 }) 899 } 900 origHook := s.dom.DBS().GetHook() 901 s.dom.DBS().(dbs.DBSForTest).SetHook(hook) 902 defer s.dom.DBS().(dbs.DBSForTest).SetHook(origHook) 903 904 originalWT := dbs.GetWaitTimeWhenErrorOccurred() 905 dbs.SetWaitTimeWhenErrorOccurred(1 * time.Second) 906 defer func() { dbs.SetWaitTimeWhenErrorOccurred(originalWT) }() 907 startTime := time.Now() 908 tk.MustGetErrCode("alter causet t_cjtt add defCausumn b int", allegrosql.ErrNoSuchBlock) 909 sub := time.Since(startTime) 910 c.Assert(sub, Less, dbs.GetWaitTimeWhenErrorOccurred()) 911 } 912 913 func (s *testSerialSuite) TestBlockLocksEnable(c *C) { 914 tk := testkit.NewTestKit(c, s.causetstore) 915 tk.MustInterDirc("use test") 916 tk.MustInterDirc("drop causet if exists t1") 917 defer tk.MustInterDirc("drop causet if exists t1") 918 tk.MustInterDirc("create causet t1 (a int)") 919 920 // Test for enable causet dagger config. 921 defer config.RestoreFunc()() 922 config.UFIDelateGlobal(func(conf *config.Config) { 923 conf.EnableBlockLock = false 924 }) 925 926 tk.MustInterDirc("dagger blocks t1 write") 927 checkBlockLock(c, tk.Se, "test", "t1", perceptron.BlockLockNone) 928 } 929 930 func (s *testSerialSuite) TestAutoRandom(c *C) { 931 tk := testkit.NewTestKit(c, s.causetstore) 932 tk.MustInterDirc("create database if not exists auto_random_db") 933 defer tk.MustInterDirc("drop database if exists auto_random_db") 934 tk.MustInterDirc("use auto_random_db") 935 databaseName, blockName := "auto_random_db", "t" 936 tk.MustInterDirc("drop causet if exists t") 937 tk.MustInterDirc("set @@allow_auto_random_explicit_insert = true") 938 939 assertInvalidAutoRandomErr := func(allegrosql string, errMsg string, args ...interface{}) { 940 _, err := tk.InterDirc(allegrosql) 941 c.Assert(err, NotNil) 942 c.Assert(err.Error(), Equals, dbs.ErrInvalidAutoRandom.GenWithStackByArgs(fmt.Sprintf(errMsg, args...)).Error()) 943 } 944 945 assertPKIsNotHandle := func(allegrosql, errDefCaus string) { 946 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomPKisNotHandleErrMsg, errDefCaus) 947 } 948 assertAlterValue := func(allegrosql string) { 949 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomAlterErrMsg) 950 } 951 assertDecreaseBitErr := func(allegrosql string) { 952 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomDecreaseBitErrMsg) 953 } 954 assertWithAutoInc := func(allegrosql string) { 955 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomIncompatibleWithAutoIncErrMsg) 956 } 957 assertOverflow := func(allegrosql, defCausName string, maxAutoRandBits, actualAutoRandBits uint64) { 958 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomOverflowErrMsg, maxAutoRandBits, actualAutoRandBits, defCausName) 959 } 960 assertMaxOverflow := func(allegrosql, defCausName string, autoRandBits uint64) { 961 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomOverflowErrMsg, autoid.MaxAutoRandomBits, autoRandBits, defCausName) 962 } 963 assertModifyDefCausType := func(allegrosql string) { 964 tk.MustGetErrCode(allegrosql, errno.ErrUnsupportedDBSOperation) 965 } 966 assertDefault := func(allegrosql string) { 967 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomIncompatibleWithDefaultValueErrMsg) 968 } 969 assertNonPositive := func(allegrosql string) { 970 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomNonPositive) 971 } 972 assertBigIntOnly := func(allegrosql, defCausType string) { 973 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomOnNonBigIntDeferredCauset, defCausType) 974 } 975 assertAddDeferredCauset := func(allegrosql, defCausName string) { 976 { 977 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomAlterAddDeferredCauset, defCausName, databaseName, blockName) 978 } 979 } 980 mustInterDircAndDrop := func(allegrosql string, fns ...func()) { 981 tk.MustInterDirc(allegrosql) 982 for _, f := range fns { 983 f() 984 } 985 tk.MustInterDirc("drop causet t") 986 } 987 988 ConfigTestUtils.SetupAutoRandomTestConfig() 989 defer ConfigTestUtils.RestoreAutoRandomTestConfig() 990 991 // Only bigint defCausumn can set auto_random 992 assertBigIntOnly("create causet t (a char primary key auto_random(3), b int)", "char") 993 assertBigIntOnly("create causet t (a varchar(255) primary key auto_random(3), b int)", "varchar") 994 assertBigIntOnly("create causet t (a timestamp primary key auto_random(3), b int)", "timestamp") 995 996 // PKIsHandle, but auto_random is defined on non-primary key. 997 assertPKIsNotHandle("create causet t (a bigint auto_random (3) primary key, b bigint auto_random (3))", "b") 998 assertPKIsNotHandle("create causet t (a bigint auto_random (3), b bigint auto_random(3), primary key(a))", "b") 999 assertPKIsNotHandle("create causet t (a bigint auto_random (3), b bigint auto_random(3) primary key)", "a") 1000 1001 // PKIsNotHandle: no primary key. 1002 assertPKIsNotHandle("create causet t (a bigint auto_random(3), b int)", "a") 1003 // PKIsNotHandle: primary key is not a single defCausumn. 1004 assertPKIsNotHandle("create causet t (a bigint auto_random(3), b bigint, primary key (a, b))", "a") 1005 assertPKIsNotHandle("create causet t (a bigint auto_random(3), b int, c char, primary key (a, c))", "a") 1006 1007 // PKIsNotHandle: causet is created when alter-primary-key = true. 1008 config.UFIDelateGlobal(func(conf *config.Config) { 1009 conf.AlterPrimaryKey = true 1010 }) 1011 assertPKIsNotHandle("create causet t (a bigint auto_random(3) primary key, b int)", "a") 1012 assertPKIsNotHandle("create causet t (a bigint auto_random(3) primary key, b int)", "a") 1013 assertPKIsNotHandle("create causet t (a int, b bigint auto_random(3) primary key)", "b") 1014 config.UFIDelateGlobal(func(conf *config.Config) { 1015 conf.AlterPrimaryKey = false 1016 }) 1017 1018 // Can not set auto_random along with auto_increment. 1019 assertWithAutoInc("create causet t (a bigint auto_random(3) primary key auto_increment)") 1020 assertWithAutoInc("create causet t (a bigint primary key auto_increment auto_random(3))") 1021 assertWithAutoInc("create causet t (a bigint auto_increment primary key auto_random(3))") 1022 assertWithAutoInc("create causet t (a bigint auto_random(3) auto_increment, primary key (a))") 1023 1024 // Can not set auto_random along with default. 1025 assertDefault("create causet t (a bigint auto_random primary key default 3)") 1026 assertDefault("create causet t (a bigint auto_random(2) primary key default 5)") 1027 mustInterDircAndDrop("create causet t (a bigint auto_random primary key)", func() { 1028 assertDefault("alter causet t modify defCausumn a bigint auto_random default 3") 1029 assertDefault("alter causet t alter defCausumn a set default 3") 1030 }) 1031 1032 // Overflow data type max length. 1033 assertMaxOverflow("create causet t (a bigint auto_random(64) primary key)", "a", 64) 1034 assertMaxOverflow("create causet t (a bigint auto_random(16) primary key)", "a", 16) 1035 mustInterDircAndDrop("create causet t (a bigint auto_random(5) primary key)", func() { 1036 assertMaxOverflow("alter causet t modify a bigint auto_random(64)", "a", 64) 1037 assertMaxOverflow("alter causet t modify a bigint auto_random(16)", "a", 16) 1038 }) 1039 1040 assertNonPositive("create causet t (a bigint auto_random(0) primary key)") 1041 tk.MustGetErrMsg("create causet t (a bigint auto_random(-1) primary key)", 1042 `[BerolinaSQL:1064]You have an error in your ALLEGROALLEGROSQL syntax; check the manual that corresponds to your MilevaDB version for the right syntax to use line 1 defCausumn 38 near "-1) primary key)" `) 1043 1044 // Basic usage. 1045 mustInterDircAndDrop("create causet t (a bigint auto_random(1) primary key)") 1046 mustInterDircAndDrop("create causet t (a bigint auto_random(4) primary key)") 1047 mustInterDircAndDrop("create causet t (a bigint auto_random(15) primary key)") 1048 mustInterDircAndDrop("create causet t (a bigint primary key auto_random(4))") 1049 mustInterDircAndDrop("create causet t (a bigint auto_random(4), primary key (a))") 1050 1051 // Increase auto_random bits. 1052 mustInterDircAndDrop("create causet t (a bigint auto_random(5) primary key)", func() { 1053 tk.MustInterDirc("alter causet t modify a bigint auto_random(8)") 1054 tk.MustInterDirc("alter causet t modify a bigint auto_random(10)") 1055 tk.MustInterDirc("alter causet t modify a bigint auto_random(12)") 1056 }) 1057 1058 // Auto_random can occur multiple times like other defCausumn attributes. 1059 mustInterDircAndDrop("create causet t (a bigint auto_random(3) auto_random(2) primary key)") 1060 mustInterDircAndDrop("create causet t (a bigint, b bigint auto_random(3) primary key auto_random(2))") 1061 mustInterDircAndDrop("create causet t (a bigint auto_random(1) auto_random(2) auto_random(3), primary key (a))") 1062 1063 // Add/drop the auto_random attribute is not allowed. 1064 mustInterDircAndDrop("create causet t (a bigint auto_random(3) primary key)", func() { 1065 assertAlterValue("alter causet t modify defCausumn a bigint") 1066 assertAlterValue("alter causet t modify defCausumn a bigint auto_random(0)") 1067 assertAlterValue("alter causet t change defCausumn a b bigint") 1068 }) 1069 mustInterDircAndDrop("create causet t (a bigint, b char, c bigint auto_random(3), primary key(c))", func() { 1070 assertAlterValue("alter causet t modify defCausumn c bigint") 1071 assertAlterValue("alter causet t change defCausumn c d bigint") 1072 }) 1073 mustInterDircAndDrop("create causet t (a bigint primary key)", func() { 1074 assertAlterValue("alter causet t modify defCausumn a bigint auto_random(3)") 1075 }) 1076 mustInterDircAndDrop("create causet t (a bigint, b bigint, primary key(a, b))", func() { 1077 assertAlterValue("alter causet t modify defCausumn a bigint auto_random(3)") 1078 assertAlterValue("alter causet t modify defCausumn b bigint auto_random(3)") 1079 }) 1080 1081 // Add auto_random defCausumn is not allowed. 1082 mustInterDircAndDrop("create causet t (a bigint)", func() { 1083 assertAddDeferredCauset("alter causet t add defCausumn b int auto_random", "b") 1084 assertAddDeferredCauset("alter causet t add defCausumn b bigint auto_random", "b") 1085 assertAddDeferredCauset("alter causet t add defCausumn b bigint auto_random primary key", "b") 1086 }) 1087 mustInterDircAndDrop("create causet t (a bigint, b bigint primary key)", func() { 1088 assertAddDeferredCauset("alter causet t add defCausumn c int auto_random", "c") 1089 assertAddDeferredCauset("alter causet t add defCausumn c bigint auto_random", "c") 1090 assertAddDeferredCauset("alter causet t add defCausumn c bigint auto_random primary key", "c") 1091 }) 1092 1093 // Decrease auto_random bits is not allowed. 1094 mustInterDircAndDrop("create causet t (a bigint auto_random(10) primary key)", func() { 1095 assertDecreaseBitErr("alter causet t modify defCausumn a bigint auto_random(6)") 1096 }) 1097 mustInterDircAndDrop("create causet t (a bigint auto_random(10) primary key)", func() { 1098 assertDecreaseBitErr("alter causet t modify defCausumn a bigint auto_random(1)") 1099 }) 1100 1101 originStep := autoid.GetStep() 1102 autoid.SetStep(1) 1103 // Increase auto_random bits but it will overlap with incremental bits. 1104 mustInterDircAndDrop("create causet t (a bigint unsigned auto_random(5) primary key)", func() { 1105 const alterTryCnt, rebaseOffset = 3, 1 1106 insertALLEGROSQL := fmt.Sprintf("insert into t values (%d)", ((1<<(64-10))-1)-rebaseOffset-alterTryCnt) 1107 tk.MustInterDirc(insertALLEGROSQL) 1108 // Try to rebase to 0..0011..1111 (54 `1`s). 1109 tk.MustInterDirc("alter causet t modify a bigint unsigned auto_random(6)") 1110 tk.MustInterDirc("alter causet t modify a bigint unsigned auto_random(10)") 1111 assertOverflow("alter causet t modify a bigint unsigned auto_random(11)", "a", 10, 11) 1112 }) 1113 autoid.SetStep(originStep) 1114 1115 // Modifying the field type of a auto_random defCausumn is not allowed. 1116 // Here the throw error is `ERROR 8200 (HY000): Unsupported modify defCausumn: length 11 is less than origin 20`, 1117 // instead of `ERROR 8216 (HY000): Invalid auto random: modifying the auto_random defCausumn type is not supported` 1118 // Because the origin defCausumn is `bigint`, it can not change to any other defCausumn type in MilevaDB limitation. 1119 mustInterDircAndDrop("create causet t (a bigint primary key auto_random(3), b int)", func() { 1120 assertModifyDefCausType("alter causet t modify defCausumn a int auto_random(3)") 1121 assertModifyDefCausType("alter causet t modify defCausumn a mediumint auto_random(3)") 1122 assertModifyDefCausType("alter causet t modify defCausumn a smallint auto_random(3)") 1123 tk.MustInterDirc("alter causet t modify defCausumn b int") 1124 tk.MustInterDirc("alter causet t modify defCausumn b bigint") 1125 tk.MustInterDirc("alter causet t modify defCausumn a bigint auto_random(3)") 1126 }) 1127 1128 // Test show warnings when create auto_random causet. 1129 assertShowWarningCorrect := func(allegrosql string, times int) { 1130 mustInterDircAndDrop(allegrosql, func() { 1131 note := fmt.Sprintf(autoid.AutoRandomAvailableAllocTimesNote, times) 1132 result := fmt.Sprintf("Note|1105|%s", note) 1133 tk.MustQuery("show warnings").Check(RowsWithSep("|", result)) 1134 c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0)) 1135 }) 1136 } 1137 assertShowWarningCorrect("create causet t (a bigint auto_random(15) primary key)", 281474976710655) 1138 assertShowWarningCorrect("create causet t (a bigint unsigned auto_random(15) primary key)", 562949953421311) 1139 assertShowWarningCorrect("create causet t (a bigint auto_random(1) primary key)", 4611686018427387903) 1140 1141 // Test insert into auto_random defCausumn explicitly is not allowed by default. 1142 assertExplicitInsertDisallowed := func(allegrosql string) { 1143 assertInvalidAutoRandomErr(allegrosql, autoid.AutoRandomExplicitInsertDisabledErrMsg) 1144 } 1145 tk.MustInterDirc("set @@allow_auto_random_explicit_insert = false") 1146 mustInterDircAndDrop("create causet t (a bigint auto_random primary key)", func() { 1147 assertExplicitInsertDisallowed("insert into t values (1)") 1148 assertExplicitInsertDisallowed("insert into t values (3)") 1149 tk.MustInterDirc("insert into t values()") 1150 }) 1151 tk.MustInterDirc("set @@allow_auto_random_explicit_insert = true") 1152 mustInterDircAndDrop("create causet t (a bigint auto_random primary key)", func() { 1153 tk.MustInterDirc("insert into t values(1)") 1154 tk.MustInterDirc("insert into t values(3)") 1155 tk.MustInterDirc("insert into t values()") 1156 }) 1157 } 1158 1159 func (s *testSerialSuite) TestAutoRandomExchangePartition(c *C) { 1160 tk := testkit.NewTestKit(c, s.causetstore) 1161 tk.MustInterDirc("create database if not exists auto_random_db") 1162 defer tk.MustInterDirc("drop database if exists auto_random_db") 1163 1164 ConfigTestUtils.SetupAutoRandomTestConfig() 1165 defer ConfigTestUtils.RestoreAutoRandomTestConfig() 1166 1167 tk.MustInterDirc("use auto_random_db") 1168 1169 tk.MustInterDirc("drop causet if exists e1, e2, e3, e4;") 1170 1171 tk.MustInterDirc("create causet e1 (a bigint primary key auto_random(3)) partition by hash(a) partitions 1;") 1172 1173 tk.MustInterDirc("create causet e2 (a bigint primary key);") 1174 tk.MustGetErrCode("alter causet e1 exchange partition p0 with causet e2;", errno.ErrBlocksDifferentMetadata) 1175 1176 tk.MustInterDirc("create causet e3 (a bigint primary key auto_random(2));") 1177 tk.MustGetErrCode("alter causet e1 exchange partition p0 with causet e3;", errno.ErrBlocksDifferentMetadata) 1178 tk.MustInterDirc("insert into e1 values (), (), ()") 1179 1180 tk.MustInterDirc("create causet e4 (a bigint primary key auto_random(3));") 1181 tk.MustInterDirc("insert into e4 values ()") 1182 tk.MustInterDirc("alter causet e1 exchange partition p0 with causet e4;") 1183 1184 tk.MustQuery("select count(*) from e1").Check(testkit.Rows("1")) 1185 tk.MustInterDirc("insert into e1 values ()") 1186 tk.MustQuery("select count(*) from e1").Check(testkit.Rows("2")) 1187 1188 tk.MustQuery("select count(*) from e4").Check(testkit.Rows("3")) 1189 tk.MustInterDirc("insert into e4 values ()") 1190 tk.MustQuery("select count(*) from e4").Check(testkit.Rows("4")) 1191 } 1192 1193 func (s *testSerialSuite) TestAutoRandomIncBitsIncrementAndOffset(c *C) { 1194 tk := testkit.NewTestKit(c, s.causetstore) 1195 tk.MustInterDirc("create database if not exists auto_random_db") 1196 defer tk.MustInterDirc("drop database if exists auto_random_db") 1197 tk.MustInterDirc("use auto_random_db") 1198 tk.MustInterDirc("drop causet if exists t") 1199 1200 ConfigTestUtils.SetupAutoRandomTestConfig() 1201 defer ConfigTestUtils.RestoreAutoRandomTestConfig() 1202 1203 recreateBlock := func() { 1204 tk.MustInterDirc("drop causet if exists t") 1205 tk.MustInterDirc("create causet t (a bigint auto_random(6) primary key)") 1206 } 1207 truncateBlock := func() { 1208 _, _ = tk.InterDirc("delete from t") 1209 } 1210 insertBlock := func() { 1211 tk.MustInterDirc("insert into t values ()") 1212 } 1213 assertIncBitsValues := func(values ...int) { 1214 mask := strings.Repeat("1", 64-1-6) 1215 allegrosql := fmt.Sprintf(`select a & b'%s' from t order by a & b'%s' asc`, mask, mask) 1216 vs := make([]string, len(values)) 1217 for i, value := range values { 1218 vs[i] = strconv.Itoa(value) 1219 } 1220 tk.MustQuery(allegrosql).Check(testkit.Rows(vs...)) 1221 } 1222 1223 const truncate, recreate = true, false 1224 expect := func(vs ...int) []int { return vs } 1225 testCase := []struct { 1226 setupCausetAction bool // truncate or recreate 1227 increment int // @@auto_increment_increment 1228 offset int // @@auto_increment_offset 1229 results []int // the implicit allocated auto_random incremental-bit part of values 1230 }{ 1231 {recreate, 5, 10, expect(10, 15, 20)}, 1232 {recreate, 2, 10, expect(10, 12, 14)}, 1233 {truncate, 5, 10, expect(15, 20, 25)}, 1234 {truncate, 10, 10, expect(30, 40, 50)}, 1235 {truncate, 5, 10, expect(55, 60, 65)}, 1236 } 1237 for _, tc := range testCase { 1238 switch tc.setupCausetAction { 1239 case recreate: 1240 recreateBlock() 1241 case truncate: 1242 truncateBlock() 1243 } 1244 tk.Se.GetStochastikVars().AutoIncrementIncrement = tc.increment 1245 tk.Se.GetStochastikVars().AutoIncrementOffset = tc.offset 1246 for range tc.results { 1247 insertBlock() 1248 } 1249 assertIncBitsValues(tc.results...) 1250 } 1251 } 1252 1253 func (s *testSerialSuite) TestModifyingDeferredCauset4NewDefCauslations(c *C) { 1254 defCauslate.SetNewDefCauslationEnabledForTest(true) 1255 defer defCauslate.SetNewDefCauslationEnabledForTest(false) 1256 1257 tk := testkit.NewTestKit(c, s.causetstore) 1258 tk.MustInterDirc("create database dct") 1259 tk.MustInterDirc("use dct") 1260 tk.MustInterDirc("create causet t(b varchar(10) defCauslate utf8_bin, c varchar(10) defCauslate utf8_general_ci) defCauslate utf8_bin") 1261 // DeferredCauset defCauslation can be changed as long as there is no index defined. 1262 tk.MustInterDirc("alter causet t modify b varchar(10) defCauslate utf8_general_ci") 1263 tk.MustInterDirc("alter causet t modify c varchar(10) defCauslate utf8_bin") 1264 tk.MustInterDirc("alter causet t modify c varchar(10) defCauslate utf8_unicode_ci") 1265 tk.MustInterDirc("alter causet t charset utf8 defCauslate utf8_general_ci") 1266 tk.MustInterDirc("alter causet t convert to charset utf8 defCauslate utf8_bin") 1267 tk.MustInterDirc("alter causet t convert to charset utf8 defCauslate utf8_unicode_ci") 1268 tk.MustInterDirc("alter causet t convert to charset utf8 defCauslate utf8_general_ci") 1269 tk.MustInterDirc("alter causet t modify b varchar(10) defCauslate utf8_unicode_ci") 1270 tk.MustInterDirc("alter causet t modify b varchar(10) defCauslate utf8_bin") 1271 1272 tk.MustInterDirc("alter causet t add index b_idx(b)") 1273 tk.MustInterDirc("alter causet t add index c_idx(c)") 1274 tk.MustGetErrMsg("alter causet t modify b varchar(10) defCauslate utf8_general_ci", "[dbs:8200]Unsupported modifying defCauslation of defCausumn 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it.") 1275 tk.MustGetErrMsg("alter causet t modify c varchar(10) defCauslate utf8_bin", "[dbs:8200]Unsupported modifying defCauslation of defCausumn 'c' from 'utf8_general_ci' to 'utf8_bin' when index is defined on it.") 1276 tk.MustGetErrMsg("alter causet t modify c varchar(10) defCauslate utf8_unicode_ci", "[dbs:8200]Unsupported modifying defCauslation of defCausumn 'c' from 'utf8_general_ci' to 'utf8_unicode_ci' when index is defined on it.") 1277 tk.MustGetErrMsg("alter causet t convert to charset utf8 defCauslate utf8_general_ci", "[dbs:8200]Unsupported converting defCauslation of defCausumn 'b' from 'utf8_bin' to 'utf8_general_ci' when index is defined on it.") 1278 // Change to a compatible defCauslation is allowed. 1279 tk.MustInterDirc("alter causet t modify c varchar(10) defCauslate utf8mb4_general_ci") 1280 // Change the default defCauslation of causet is allowed. 1281 tk.MustInterDirc("alter causet t defCauslate utf8mb4_general_ci") 1282 tk.MustInterDirc("alter causet t charset utf8mb4 defCauslate utf8mb4_bin") 1283 tk.MustInterDirc("alter causet t charset utf8mb4 defCauslate utf8mb4_unicode_ci") 1284 // Change the default defCauslation of database is allowed. 1285 tk.MustInterDirc("alter database dct charset utf8mb4 defCauslate utf8mb4_general_ci") 1286 } 1287 1288 func (s *testSerialSuite) TestForbidUnsupportedDefCauslations(c *C) { 1289 defCauslate.SetNewDefCauslationEnabledForTest(true) 1290 defer defCauslate.SetNewDefCauslationEnabledForTest(false) 1291 tk := testkit.NewTestKit(c, s.causetstore) 1292 1293 mustGetUnsupportedDefCauslation := func(allegrosql string, defCausl string) { 1294 tk.MustGetErrMsg(allegrosql, fmt.Sprintf("[dbs:1273]Unsupported defCauslation when new defCauslation is enabled: '%s'", defCausl)) 1295 } 1296 // Test default defCauslation of database. 1297 mustGetUnsupportedDefCauslation("create database ucd charset utf8mb4 defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1298 mustGetUnsupportedDefCauslation("create database ucd charset utf8 defCauslate utf8_roman_ci", "utf8_roman_ci") 1299 tk.MustInterDirc("create database ucd") 1300 mustGetUnsupportedDefCauslation("alter database ucd charset utf8mb4 defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1301 mustGetUnsupportedDefCauslation("alter database ucd defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1302 1303 // Test default defCauslation of causet. 1304 tk.MustInterDirc("use ucd") 1305 mustGetUnsupportedDefCauslation("create causet t(a varchar(20)) charset utf8mb4 defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1306 mustGetUnsupportedDefCauslation("create causet t(a varchar(20)) defCauslate utf8_roman_ci", "utf8_roman_ci") 1307 tk.MustInterDirc("create causet t(a varchar(20)) defCauslate utf8mb4_general_ci") 1308 mustGetUnsupportedDefCauslation("alter causet t default defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1309 mustGetUnsupportedDefCauslation("alter causet t convert to charset utf8mb4 defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1310 1311 // Test defCauslation of defCausumns. 1312 mustGetUnsupportedDefCauslation("create causet t1(a varchar(20)) defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1313 mustGetUnsupportedDefCauslation("create causet t1(a varchar(20)) charset utf8 defCauslate utf8_roman_ci", "utf8_roman_ci") 1314 tk.MustInterDirc("create causet t1(a varchar(20))") 1315 mustGetUnsupportedDefCauslation("alter causet t1 modify a varchar(20) defCauslate utf8mb4_roman_ci", "utf8mb4_roman_ci") 1316 mustGetUnsupportedDefCauslation("alter causet t1 modify a varchar(20) charset utf8 defCauslate utf8_roman_ci", "utf8_roman_ci") 1317 mustGetUnsupportedDefCauslation("alter causet t1 modify a varchar(20) charset utf8 defCauslate utf8_roman_ci", "utf8_roman_ci") 1318 1319 // TODO(bb7133): fix the following cases by setting charset from defCauslate firstly. 1320 // mustGetUnsupportedDefCauslation("create database ucd defCauslate utf8mb4_unicode_ci", errMsgUnsupportedUnicodeCI) 1321 // mustGetUnsupportedDefCauslation("alter causet t convert to defCauslate utf8mb4_unicode_ci", "utf8mb4_unicode_ci") 1322 } 1323 1324 func (s *testSerialSuite) TestInvisibleIndex(c *C) { 1325 tk := testkit.NewTestKit(c, s.causetstore) 1326 1327 tk.MustInterDirc("use test") 1328 tk.MustInterDirc("drop causet if exists t,t1,t2,t3,t4,t5,t6") 1329 1330 // The DBS memex related to invisible index. 1331 showIndexes := "select index_name, is_visible from information_schema.statistics where block_schema = 'test' and block_name = 't'" 1332 // 1. Create causet with invisible index 1333 tk.MustInterDirc("create causet t (a int, b int, unique (a) invisible)") 1334 tk.MustQuery(showIndexes).Check(testkit.Rows("a NO")) 1335 tk.MustInterDirc("insert into t values (1, 2)") 1336 tk.MustQuery("select * from t").Check(testkit.Rows("1 2")) 1337 // 2. Drop invisible index 1338 tk.MustInterDirc("alter causet t drop index a") 1339 tk.MustQuery(showIndexes).Check(testkit.Rows()) 1340 tk.MustInterDirc("insert into t values (3, 4)") 1341 tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4")) 1342 // 3. Add an invisible index 1343 tk.MustInterDirc("alter causet t add index (b) invisible") 1344 tk.MustQuery(showIndexes).Check(testkit.Rows("b NO")) 1345 tk.MustInterDirc("insert into t values (5, 6)") 1346 tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6")) 1347 // 4. Drop it 1348 tk.MustInterDirc("alter causet t drop index b") 1349 tk.MustQuery(showIndexes).Check(testkit.Rows()) 1350 tk.MustInterDirc("insert into t values (7, 8)") 1351 tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8")) 1352 // 5. Create a multiple-defCausumn invisible index 1353 tk.MustInterDirc("alter causet t add index a_b(a, b) invisible") 1354 tk.MustQuery(showIndexes).Check(testkit.Rows("a_b NO", "a_b NO")) 1355 tk.MustInterDirc("insert into t values (9, 10)") 1356 tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8", "9 10")) 1357 // 6. Drop it 1358 tk.MustInterDirc("alter causet t drop index a_b") 1359 tk.MustQuery(showIndexes).Check(testkit.Rows()) 1360 tk.MustInterDirc("insert into t values (11, 12)") 1361 tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "3 4", "5 6", "7 8", "9 10", "11 12")) 1362 1363 defer config.RestoreFunc()() 1364 config.UFIDelateGlobal(func(conf *config.Config) { 1365 conf.AlterPrimaryKey = true 1366 }) 1367 1368 // Limitation: Primary key cannot be invisible index 1369 tk.MustGetErrCode("create causet t1 (a int, primary key (a) invisible)", errno.ErrPHoTTexCantBeInvisible) 1370 tk.MustGetErrCode("create causet t1 (a int, b int, primary key (a, b) invisible)", errno.ErrPHoTTexCantBeInvisible) 1371 tk.MustInterDirc("create causet t1 (a int, b int)") 1372 tk.MustGetErrCode("alter causet t1 add primary key(a) invisible", errno.ErrPHoTTexCantBeInvisible) 1373 tk.MustGetErrCode("alter causet t1 add primary key(a, b) invisible", errno.ErrPHoTTexCantBeInvisible) 1374 1375 // Implicit primary key cannot be invisible index 1376 // Create a implicit primary key 1377 tk.MustGetErrCode("create causet t2(a int not null, unique (a) invisible)", errno.ErrPHoTTexCantBeInvisible) 1378 // DeferredCauset `a` become implicit primary key after DBS memex on itself 1379 tk.MustInterDirc("create causet t2(a int not null)") 1380 tk.MustGetErrCode("alter causet t2 add unique (a) invisible", errno.ErrPHoTTexCantBeInvisible) 1381 tk.MustInterDirc("create causet t3(a int, unique index (a) invisible)") 1382 tk.MustGetErrCode("alter causet t3 modify defCausumn a int not null", errno.ErrPHoTTexCantBeInvisible) 1383 // Only first unique defCausumn can be implicit primary 1384 tk.MustInterDirc("create causet t4(a int not null, b int not null, unique (a), unique (b) invisible)") 1385 showIndexes = "select index_name, is_visible from information_schema.statistics where block_schema = 'test' and block_name = 't4'" 1386 tk.MustQuery(showIndexes).Check(testkit.Rows("a YES", "b NO")) 1387 tk.MustInterDirc("insert into t4 values (1, 2)") 1388 tk.MustQuery("select * from t4").Check(testkit.Rows("1 2")) 1389 tk.MustGetErrCode("create causet t5(a int not null, b int not null, unique (b) invisible, unique (a))", errno.ErrPHoTTexCantBeInvisible) 1390 // DeferredCauset `b` become implicit primary key after DBS memex on other defCausumns 1391 tk.MustInterDirc("create causet t5(a int not null, b int not null, unique (a), unique (b) invisible)") 1392 tk.MustGetErrCode("alter causet t5 drop index a", errno.ErrPHoTTexCantBeInvisible) 1393 tk.MustGetErrCode("alter causet t5 modify defCausumn a int null", errno.ErrPHoTTexCantBeInvisible) 1394 // If these is a explicit primary key, no key will become implicit primary key 1395 tk.MustInterDirc("create causet t6 (a int not null, b int, unique (a) invisible, primary key(b))") 1396 showIndexes = "select index_name, is_visible from information_schema.statistics where block_schema = 'test' and block_name = 't6'" 1397 tk.MustQuery(showIndexes).Check(testkit.Rows("a NO", "PRIMARY YES")) 1398 tk.MustInterDirc("insert into t6 values (1, 2)") 1399 tk.MustQuery("select * from t6").Check(testkit.Rows("1 2")) 1400 tk.MustGetErrCode("alter causet t6 drop primary key", errno.ErrPHoTTexCantBeInvisible) 1401 res := tk.MustQuery("show index from t6 where Key_name='PRIMARY';") 1402 c.Check(len(res.Rows()), Equals, 1) 1403 } 1404 1405 func (s *testSerialSuite) TestCreateClusteredIndex(c *C) { 1406 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1407 tk.Se.GetStochastikVars().EnableClusteredIndex = true 1408 tk.MustInterDirc("CREATE TABLE t1 (a int primary key, b int)") 1409 tk.MustInterDirc("CREATE TABLE t2 (a varchar(255) primary key, b int)") 1410 tk.MustInterDirc("CREATE TABLE t3 (a int, b int, c int, primary key (a, b))") 1411 tk.MustInterDirc("CREATE TABLE t4 (a int, b int, c int)") 1412 ctx := tk.Se.(stochastikctx.Context) 1413 is := petri.GetPetri(ctx).SchemaReplicant() 1414 tbl, err := is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t1")) 1415 c.Assert(err, IsNil) 1416 c.Assert(tbl.Meta().PKIsHandle, IsTrue) 1417 c.Assert(tbl.Meta().IsCommonHandle, IsFalse) 1418 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t2")) 1419 c.Assert(err, IsNil) 1420 c.Assert(tbl.Meta().IsCommonHandle, IsTrue) 1421 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t3")) 1422 c.Assert(err, IsNil) 1423 c.Assert(tbl.Meta().IsCommonHandle, IsTrue) 1424 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t4")) 1425 c.Assert(err, IsNil) 1426 c.Assert(tbl.Meta().IsCommonHandle, IsFalse) 1427 1428 config.UFIDelateGlobal(func(conf *config.Config) { 1429 conf.AlterPrimaryKey = true 1430 }) 1431 tk.MustInterDirc("CREATE TABLE t5 (a varchar(255) primary key, b int)") 1432 tk.MustInterDirc("CREATE TABLE t6 (a int, b int, c int, primary key (a, b))") 1433 is = petri.GetPetri(ctx).SchemaReplicant() 1434 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t5")) 1435 c.Assert(err, IsNil) 1436 c.Assert(tbl.Meta().IsCommonHandle, IsFalse) 1437 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t6")) 1438 c.Assert(err, IsNil) 1439 c.Assert(tbl.Meta().IsCommonHandle, IsFalse) 1440 config.UFIDelateGlobal(func(conf *config.Config) { 1441 conf.AlterPrimaryKey = false 1442 }) 1443 1444 tk.MustInterDirc("CREATE TABLE t21 like t2") 1445 tk.MustInterDirc("CREATE TABLE t31 like t3") 1446 is = petri.GetPetri(ctx).SchemaReplicant() 1447 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t21")) 1448 c.Assert(err, IsNil) 1449 c.Assert(tbl.Meta().IsCommonHandle, IsTrue) 1450 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t31")) 1451 c.Assert(err, IsNil) 1452 c.Assert(tbl.Meta().IsCommonHandle, IsTrue) 1453 1454 tk.Se.GetStochastikVars().EnableClusteredIndex = false 1455 tk.MustInterDirc("CREATE TABLE t7 (a varchar(255) primary key, b int)") 1456 is = petri.GetPetri(ctx).SchemaReplicant() 1457 tbl, err = is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t7")) 1458 c.Assert(err, IsNil) 1459 c.Assert(tbl.Meta().IsCommonHandle, IsFalse) 1460 } 1461 1462 func (s *testSerialSuite) TestCreateBlockNoBlock(c *C) { 1463 tk := testkit.NewTestKitWithInit(c, s.causetstore) 1464 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/dbs/checkTenantCheckAllVersionsWaitTime", `return(true)`), IsNil) 1465 defer func() { 1466 c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/dbs/checkTenantCheckAllVersionsWaitTime"), IsNil) 1467 }() 1468 save := variable.GetDBSErrorCountLimit() 1469 variable.SetDBSErrorCountLimit(1) 1470 defer func() { 1471 variable.SetDBSErrorCountLimit(save) 1472 }() 1473 1474 tk.MustInterDirc("drop causet if exists t") 1475 _, err := tk.InterDirc("create causet t(a int)") 1476 c.Assert(err, NotNil) 1477 }