diff --git a/session/BUILD.bazel b/session/BUILD.bazel index 2fb534927ee60..209ed174ca505 100644 --- a/session/BUILD.bazel +++ b/session/BUILD.bazel @@ -5,6 +5,7 @@ go_library( srcs = [ "advisory_locks.go", "bootstrap.go", + "mock_bootstrap.go", "nontransactional.go", "session.go", "testutil.go", #keep diff --git a/session/bootstrap.go b/session/bootstrap.go index 454719f0241c2..e5c37ac9f319e 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -1093,6 +1093,9 @@ func upgrade(s Session) { if isNull { upgradeToVer99Before(s) } + + // It is only used in test. + addMockBootstrapVersionForTest() for _, upgrade := range bootstrapVersion { upgrade(s, ver) } diff --git a/session/bootstraptest/BUILD.bazel b/session/bootstraptest/BUILD.bazel index 2e7b4a3ae6ed1..8a2ba4090ff50 100644 --- a/session/bootstraptest/BUILD.bazel +++ b/session/bootstraptest/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 5, + shard_count = 6, deps = [ "//config", "//meta", diff --git a/session/bootstraptest/bootstrap_upgrade_test.go b/session/bootstraptest/bootstrap_upgrade_test.go index 474261955f8f0..0e603e8759205 100644 --- a/session/bootstraptest/bootstrap_upgrade_test.go +++ b/session/bootstraptest/bootstrap_upgrade_test.go @@ -16,6 +16,7 @@ package bootstraptest_test import ( "context" + "fmt" "strconv" "strings" "testing" @@ -220,3 +221,62 @@ func TestUpgradeVersion75(t *testing.T) { require.Equal(t, "host", strings.ToLower(row.GetString(0))) require.Equal(t, "char(255)", strings.ToLower(row.GetString(1))) } + +func TestUpgradeVersionMockLatest(t *testing.T) { + *session.WithMockUpgrade = true + + store, dom := session.CreateStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + seV := session.CreateSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(session.CurrentBootstrapVersion - 1) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + session.MustExec(t, seV, fmt.Sprintf("update mysql.tidb set variable_value='%d' where variable_name='tidb_server_version'", session.CurrentBootstrapVersion-1)) + session.UnsetStoreBootstrapped(store.UUID()) + ver, err := session.GetBootstrapVersion(seV) + require.NoError(t, err) + require.Equal(t, session.CurrentBootstrapVersion-1, ver) + dom.Close() + domLatestV, err := session.BootstrapSession(store) + require.NoError(t, err) + defer domLatestV.Close() + + seLatestV := session.CreateSessionAndSetID(t, store) + ver, err = session.GetBootstrapVersion(seLatestV) + require.NoError(t, err) + require.Equal(t, session.CurrentBootstrapVersion+1, ver) + + tk := testkit.NewTestKit(t, store) + tk.MustQuery("show create table mysql.mock_sys_t").Check(testkit.Rows( + "mock_sys_t CREATE TABLE `mock_sys_t` (\n" + + " `c1` int(11) DEFAULT NULL,\n" + + " `c2` int(11) NOT NULL,\n" + + " `c11` char(10) DEFAULT NULL,\n" + + " `c4` bigint(20) DEFAULT NULL,\n" + + " `mayNullCol` bigint(20) NOT NULL DEFAULT '1',\n" + + " KEY `fk_c1` (`c1`),\n" + + " UNIQUE KEY `idx_uc2` (`c2`),\n" + + " KEY `idx_c2` (`c2`),\n" + + " KEY `idx_v` (`c1`) /*!80000 INVISIBLE */,\n" + + " KEY `rename_idx2` (`c1`)\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) + tk.MustQuery("show create table mysql.mock_sys_partition").Check(testkit.Rows( + "mock_sys_partition CREATE TABLE `mock_sys_partition` (\n" + + " `c1` int(11) NOT NULL,\n" + + " `c2` int(11) DEFAULT NULL,\n" + + " `c3` int(11) DEFAULT NULL,\n" + + " UNIQUE KEY `c3_index` (`c1`),\n" + + " PRIMARY KEY (`c1`) /*T![clustered_index] NONCLUSTERED */\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\n" + + "PARTITION BY RANGE (`c1`)\n" + + "(PARTITION `p0` VALUES LESS THAN (1024),\n" + + " PARTITION `p1` VALUES LESS THAN (2048),\n" + + " PARTITION `p2` VALUES LESS THAN (3072),\n" + + " PARTITION `p3` VALUES LESS THAN (4096),\n" + + " PARTITION `p4` VALUES LESS THAN (7096))")) +} diff --git a/session/mock_bootstrap.go b/session/mock_bootstrap.go new file mode 100644 index 0000000000000..c4e0e24414809 --- /dev/null +++ b/session/mock_bootstrap.go @@ -0,0 +1,118 @@ +// Copyright 2023 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Copyright 2013 The ql Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSES/QL-LICENSE file. + +package session + +import ( + "flag" + "time" + + "github.com/pingcap/tidb/util/logutil" + "go.uber.org/zap" +) + +// WithMockUpgrade is a flag identify whether tests run with mock upgrading. +var WithMockUpgrade = flag.Bool("with-mock-upgrade", false, "whether tests run with mock upgrade") + +var allDDLs = []string{ + "create unique index c3_index on mock_sys_partition (c1)", + "alter table mock_sys_partition add primary key c3_index (c1)", + "alter table mock_sys_t add primary key idx_pc2 (c2)", + "alter table mock_sys_t drop primary key", + "alter table mock_sys_t add unique index idx_uc2 (c2)", + "alter table mock_sys_t add index idx_c2(c2)", + "alter table mock_sys_t add column c4 bigint", + "create table test_create_table(a int)", + "drop table test_create_table", + "create database test_create_db", + "drop database test_create_db", + "alter table mock_sys_t drop column c3", + "alter table mock_sys_t_rebase auto_increment = 6000", + "alter table mock_sys_t_auto shard_row_id_bits = 5", + "alter table mock_sys_t modify column c11 mediumint", + "alter table mock_sys_t modify column c11 int", + "alter table mock_sys_t add column mayNullCol bigint default 1", + "alter table mock_sys_t modify column mayNullCol bigint default 1 not null", + "alter table mock_sys_t modify column c11 char(10)", + "alter table mock_sys_t add constraint fk foreign key a(c1) references mock_sys_t_ref(c1)", + "alter table mock_sys_t drop foreign key fk", + "rename table mock_sys_t_rename1 to mock_sys_t_rename11", + "rename table mock_sys_t_rename11 to mock_sys_t_rename111, mock_sys_t_rename2 to mock_sys_t_rename22", + "alter table mock_sys_t_cs convert to charset utf8mb4", + "alter database mock_sys_db_coll charset utf8mb4 collate utf8mb4_bin", + "alter table mock_sys_partition truncate partition p3", + "alter table mock_sys_t add column c41 bigint, add column c42 bigint", + "alter table mock_sys_t drop column c41, drop column c42", + "alter table mock_sys_t add index idx_v(c1)", + "alter table mock_sys_t alter index idx_v invisible", + //"alter table mock_sys_partition exchange partition p0 with table mock_sys_t_partition2", + "alter table mock_sys_partition add partition (partition p6 values less than (8192))", + "alter table mock_sys_partition drop partition p6", + "alter table mock_sys_t add index mul_idx1(c1), add index mul_idx2(c1)", + "alter table mock_sys_t drop index mul_idx1, drop index mul_idx2", + "alter database mock_sys_db_placement placement policy = 'alter_x'", + "alter table mock_sys_t add index rename_idx1(c1)", + "alter table mock_sys_t rename index rename_idx1 to rename_idx2", +} + +var mockLatestVer = currentBootstrapVersion + 1 + +func mockUpgradeToVerLatest(s Session, ver int64) { + logutil.BgLogger().Info("mock upgrade to ver latest", zap.Int64("old ver", ver), zap.Int64("mock latest ver", mockLatestVer)) + if ver >= mockLatestVer { + return + } + mustExecute(s, "use mysql") + mustExecute(s, `create table if not exists mock_sys_partition( + c1 int, c2 int, c3 int + ) + partition by range( c1 ) ( + partition p0 values less than (1024), + partition p1 values less than (2048), + partition p2 values less than (3072), + partition p3 values less than (4096), + partition p4 values less than (7096) + );`) + mustExecute(s, `create table if not exists mock_sys_t( + c1 int, c2 int, c3 int, c11 tinyint, index fk_c1(c1) + );`) + mustExecute(s, "create table mock_sys_t_rebase(c1 bigint auto_increment primary key, c2 bigint);") + mustExecute(s, "create table mock_sys_t_auto(c1 int not null auto_increment unique) shard_row_id_bits = 0") + mustExecute(s, "create table mock_sys_t_ref (c1 int key, c2 int, c3 int, c11 tinyint);") + mustExecute(s, "create table mock_sys_t_rename1(c1 bigint, c2 bigint);") + mustExecute(s, "create table mock_sys_t_rename2(c1 bigint, c2 bigint);") + mustExecute(s, "create table mock_sys_t_cs(a varchar(10)) charset utf8") + mustExecute(s, "create database mock_sys_db_coll default charset utf8 collate utf8_bin") + mustExecute(s, "create table mock_sys_t_partition2(c1 int, c2 int, c3 int)") + mustExecute(s, "set @@tidb_enable_exchange_partition=1") + mustExecute(s, "create placement policy alter_x PRIMARY_REGION=\"cn-east-1\", REGIONS=\"cn-east-1\";") + mustExecute(s, "create database mock_sys_db_placement") + for _, sql := range allDDLs { + mustExecute(s, sql) + logutil.BgLogger().Info("mock upgrade exec", zap.String("sql", sql)) + time.Sleep(20 * time.Millisecond) + } +} + +func addMockBootstrapVersionForTest() { + if !*WithMockUpgrade { + return + } + bootstrapVersion = append(bootstrapVersion, mockUpgradeToVerLatest) + currentBootstrapVersion++ +}