Skip to content

Commit

Permalink
session: add mock upgrading code for testing (#43419)
Browse files Browse the repository at this point in the history
close #43418
  • Loading branch information
zimulala authored Apr 26, 2023
1 parent a3d165c commit 91bf358
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 1 deletion.
1 change: 1 addition & 0 deletions session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"advisory_locks.go",
"bootstrap.go",
"mock_bootstrap.go",
"nontransactional.go",
"session.go",
"testutil.go", #keep
Expand Down
3 changes: 3 additions & 0 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion session/bootstraptest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 5,
shard_count = 6,
deps = [
"//config",
"//meta",
Expand Down
60 changes: 60 additions & 0 deletions session/bootstraptest/bootstrap_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bootstraptest_test

import (
"context"
"fmt"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -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))"))
}
118 changes: 118 additions & 0 deletions session/mock_bootstrap.go
Original file line number Diff line number Diff line change
@@ -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++
}

0 comments on commit 91bf358

Please sign in to comment.