230 lines
6.2 KiB
Go
230 lines
6.2 KiB
Go
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
|
|
|
|
package restore_test
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pingcap/kvproto/pkg/metapb"
|
|
"github.com/pingcap/tidb/br/pkg/gluetidb"
|
|
"github.com/pingcap/tidb/br/pkg/metautil"
|
|
"github.com/pingcap/tidb/br/pkg/mock"
|
|
"github.com/pingcap/tidb/br/pkg/restore"
|
|
"github.com/pingcap/tidb/parser/model"
|
|
"github.com/pingcap/tidb/parser/mysql"
|
|
"github.com/pingcap/tidb/parser/types"
|
|
"github.com/pingcap/tidb/tablecodec"
|
|
"github.com/stretchr/testify/require"
|
|
pd "github.com/tikv/pd/client"
|
|
"google.golang.org/grpc/keepalive"
|
|
)
|
|
|
|
var mc *mock.Cluster
|
|
|
|
var defaultKeepaliveCfg = keepalive.ClientParameters{
|
|
Time: 3 * time.Second,
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
|
|
func TestCreateTables(t *testing.T) {
|
|
m := mc
|
|
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
|
|
require.NoError(t, err)
|
|
|
|
info, err := m.Domain.GetSnapshotInfoSchema(math.MaxUint64)
|
|
require.NoError(t, err)
|
|
dbSchema, isExist := info.SchemaByName(model.NewCIStr("test"))
|
|
require.True(t, isExist)
|
|
|
|
client.SetBatchDdlSize(1)
|
|
tables := make([]*metautil.Table, 4)
|
|
intField := types.NewFieldType(mysql.TypeLong)
|
|
intField.Charset = "binary"
|
|
for i := len(tables) - 1; i >= 0; i-- {
|
|
tables[i] = &metautil.Table{
|
|
DB: dbSchema,
|
|
Info: &model.TableInfo{
|
|
ID: int64(i),
|
|
Name: model.NewCIStr("test" + strconv.Itoa(i)),
|
|
Columns: []*model.ColumnInfo{{
|
|
ID: 1,
|
|
Name: model.NewCIStr("id"),
|
|
FieldType: *intField,
|
|
State: model.StatePublic,
|
|
}},
|
|
Charset: "utf8mb4",
|
|
Collate: "utf8mb4_bin",
|
|
},
|
|
}
|
|
}
|
|
rules, newTables, err := client.CreateTables(m.Domain, tables, 0)
|
|
require.NoError(t, err)
|
|
// make sure tables and newTables have same order
|
|
for i, tbl := range tables {
|
|
require.Equal(t, tbl.Info.Name, newTables[i].Name)
|
|
}
|
|
for _, nt := range newTables {
|
|
require.Regexp(t, "test[0-3]", nt.Name.String())
|
|
}
|
|
oldTableIDExist := make(map[int64]bool)
|
|
newTableIDExist := make(map[int64]bool)
|
|
for _, tr := range rules.Data {
|
|
oldTableID := tablecodec.DecodeTableID(tr.GetOldKeyPrefix())
|
|
require.False(t, oldTableIDExist[oldTableID], "table rule duplicate old table id")
|
|
oldTableIDExist[oldTableID] = true
|
|
|
|
newTableID := tablecodec.DecodeTableID(tr.GetNewKeyPrefix())
|
|
require.False(t, newTableIDExist[newTableID], "table rule duplicate new table id")
|
|
newTableIDExist[newTableID] = true
|
|
}
|
|
|
|
for i := 0; i < len(tables); i++ {
|
|
require.True(t, oldTableIDExist[int64(i)], "table rule does not exist")
|
|
}
|
|
}
|
|
|
|
func TestIsOnline(t *testing.T) {
|
|
m := mc
|
|
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, client.IsOnline())
|
|
client.EnableOnline()
|
|
require.True(t, client.IsOnline())
|
|
}
|
|
|
|
func TestPreCheckTableClusterIndex(t *testing.T) {
|
|
m := mc
|
|
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
|
|
require.NoError(t, err)
|
|
|
|
info, err := m.Domain.GetSnapshotInfoSchema(math.MaxUint64)
|
|
require.NoError(t, err)
|
|
dbSchema, isExist := info.SchemaByName(model.NewCIStr("test"))
|
|
require.True(t, isExist)
|
|
|
|
tables := make([]*metautil.Table, 4)
|
|
intField := types.NewFieldType(mysql.TypeLong)
|
|
intField.Charset = "binary"
|
|
for i := len(tables) - 1; i >= 0; i-- {
|
|
tables[i] = &metautil.Table{
|
|
DB: dbSchema,
|
|
Info: &model.TableInfo{
|
|
ID: int64(i),
|
|
Name: model.NewCIStr("test" + strconv.Itoa(i)),
|
|
Columns: []*model.ColumnInfo{{
|
|
ID: 1,
|
|
Name: model.NewCIStr("id"),
|
|
FieldType: *intField,
|
|
State: model.StatePublic,
|
|
}},
|
|
Charset: "utf8mb4",
|
|
Collate: "utf8mb4_bin",
|
|
},
|
|
}
|
|
}
|
|
_, _, err = client.CreateTables(m.Domain, tables, 0)
|
|
require.NoError(t, err)
|
|
|
|
// exist different tables
|
|
tables[1].Info.IsCommonHandle = true
|
|
err = client.PreCheckTableClusterIndex(tables, nil, m.Domain)
|
|
require.Error(t, err)
|
|
require.Regexp(t, `.*@@tidb_enable_clustered_index should be ON \(backup table = true, created table = false\).*`, err.Error())
|
|
|
|
// exist different DDLs
|
|
jobs := []*model.Job{{
|
|
ID: 5,
|
|
Type: model.ActionCreateTable,
|
|
SchemaName: "test",
|
|
Query: "",
|
|
BinlogInfo: &model.HistoryInfo{
|
|
TableInfo: &model.TableInfo{
|
|
Name: model.NewCIStr("test1"),
|
|
IsCommonHandle: true,
|
|
},
|
|
},
|
|
}}
|
|
err = client.PreCheckTableClusterIndex(nil, jobs, m.Domain)
|
|
require.Error(t, err)
|
|
require.Regexp(t, `.*@@tidb_enable_clustered_index should be ON \(backup table = true, created table = false\).*`, err.Error())
|
|
|
|
// should pass pre-check cluster index
|
|
tables[1].Info.IsCommonHandle = false
|
|
jobs[0].BinlogInfo.TableInfo.IsCommonHandle = false
|
|
require.Nil(t, client.PreCheckTableClusterIndex(tables, jobs, m.Domain))
|
|
}
|
|
|
|
type fakePDClient struct {
|
|
pd.Client
|
|
stores []*metapb.Store
|
|
}
|
|
|
|
func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([]*metapb.Store, error) {
|
|
return append([]*metapb.Store{}, fpdc.stores...), nil
|
|
}
|
|
|
|
func TestPreCheckTableTiFlashReplicas(t *testing.T) {
|
|
m := mc
|
|
mockStores := []*metapb.Store{
|
|
{
|
|
Id: 1,
|
|
Labels: []*metapb.StoreLabel{
|
|
{
|
|
Key: "engine",
|
|
Value: "tiflash",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Id: 2,
|
|
Labels: []*metapb.StoreLabel{
|
|
{
|
|
Key: "engine",
|
|
Value: "tiflash",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
client, err := restore.NewRestoreClient(gluetidb.New(), fakePDClient{
|
|
stores: mockStores,
|
|
}, m.Storage, nil, defaultKeepaliveCfg)
|
|
require.NoError(t, err)
|
|
|
|
tables := make([]*metautil.Table, 4)
|
|
for i := 0; i < len(tables); i++ {
|
|
tiflashReplica := &model.TiFlashReplicaInfo{
|
|
Count: uint64(i),
|
|
}
|
|
if i == 0 {
|
|
tiflashReplica = nil
|
|
}
|
|
|
|
tables[i] = &metautil.Table{
|
|
DB: nil,
|
|
Info: &model.TableInfo{
|
|
ID: int64(i),
|
|
Name: model.NewCIStr("test" + strconv.Itoa(i)),
|
|
TiFlashReplica: tiflashReplica,
|
|
},
|
|
}
|
|
}
|
|
ctx := context.Background()
|
|
require.Nil(t, client.PreCheckTableTiFlashReplica(ctx, tables))
|
|
|
|
for i := 0; i < len(tables); i++ {
|
|
if i == 0 || i > 2 {
|
|
require.Nil(t, tables[i].Info.TiFlashReplica)
|
|
} else {
|
|
require.NotNil(t, tables[i].Info.TiFlashReplica)
|
|
obtainCount := int(tables[i].Info.TiFlashReplica.Count)
|
|
require.Equal(t, i, obtainCount)
|
|
}
|
|
}
|
|
}
|