From e58df44c4ab22d4f929d7736110c8ef9c5d60990 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 12 Nov 2021 15:03:04 +0800 Subject: [PATCH 01/24] add mock --- dumpling/export/sql.go | 15 +++++++++++++++ dumpling/export/sql_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 4b0203a1665da..ebf8aef3fe164 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -92,6 +92,21 @@ func ShowCreateTable(db *sql.Conn, database, table string) (string, error) { return oneRow[1], nil } +// ShowCreatePolicy constructs the create policy SQL for a specified table +// returns (createPoilicySQL, error) +func ShowCreatePolicy(db *sql.Conn, database, policy string) (string, error) { + var oneRow [2]string + handleOneRow := func(rows *sql.Rows) error { + return rows.Scan(&oneRow[0], &oneRow[1]) + } + query := fmt.Sprintf("SHOW CREATE PLACEMENT POLICY `%s`.`%s`", escapeString(database), escapeString(policy)) + err := simpleQuery(db, query, handleOneRow) + if err != nil { + return "", errors.Annotatef(err, "sql: %s", query) + } + return oneRow[1], nil +} + // ShowCreateView constructs the create view SQL for a specified view // returns (createFakeTableSQL, createViewSQL, error) func ShowCreateView(db *sql.Conn, database, view string) (createFakeTableSQL string, createRealViewSQL string, err error) { diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index b6bd835d27e35..11b6ebe3cac11 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -340,6 +340,30 @@ func TestShowCreateView(t *testing.T) { require.NoError(t, mock.ExpectationsWereMet()) } +func TestShowCreatePolicy(t *testing.T) { + t.Parallel() + + db, mock, err := sqlmock.New() + require.NoError(t, err) + defer func() { + require.NoError(t, db.Close()) + }() + + conn, err := db.Conn(context.Background()) + require.NoError(t, err) + + mock.ExpectQuery("SHOW CREATE PLACEMENT POLICY `test`.`policy_x`"). + WillReturnRows(sqlmock.NewRows([]string{"Policy", "Create Policy"}). + AddRow("policy_x", "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1")) + + createPolicySQL, err := ShowCreatePolicy(conn, "test", "policy_x") + require.NoError(t, err) + require.Equal(t, "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1", createPolicySQL) + require.NoError(t, mock.ExpectationsWereMet()) + +} + + func TestGetSuitableRows(t *testing.T) { t.Parallel() From 1d2638afa0b77cc9d225f0f0213239f2ef717e15 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 18:52:25 +0800 Subject: [PATCH 02/24] complete function --- Makefile | 2 +- dumpling/export/dump.go | 18 ++++++++++++++++++ dumpling/export/sql.go | 25 ++++++++++++++++++++++--- dumpling/export/sql_test.go | 2 +- dumpling/export/task.go | 22 +++++++++++++++++++++- dumpling/export/writer.go | 2 ++ 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 0e5e9879816fd..1ab375a2f161b 100644 --- a/Makefile +++ b/Makefile @@ -363,7 +363,7 @@ data_parsers: tools/bin/vfsgendev br/pkg/lightning/mydump/parser_generated.go br tools/bin/vfsgendev -source='"github.com/pingcap/tidb/br/pkg/lightning/web".Res' && mv res_vfsdata.go br/pkg/lightning/web/ build_dumpling: - $(DUMPLING_GOBUILD) $(RACE_FLAG) -tags codes -o $(DUMPLING_BIN) dumpling/cmd/dumpling/main.go + GOOS=linux $(DUMPLING_GOBUILD) $(RACE_FLAG) -tags codes -o $(DUMPLING_BIN) dumpling/cmd/dumpling/main.go dumpling_unit_test: export DUMPLING_ARGS=$$($(DUMPLING_PACKAGES)) dumpling_unit_test: failpoint-enable diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 39637aa4f1b89..514972e25a5c0 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -298,6 +298,24 @@ func (d *Dumper) startWriters(tctx *tcontext.Context, wg *errgroup.Group, taskCh func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskChan chan<- Task) error { conf := d.conf allTables := conf.Tables + policyNames, err := ListAllPlacementPolicyNames(metaConn) + if err != nil { + return err + } + // policy should be created before database + for _, policy := range policyNames { + createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) + tctx.L().Info(fmt.Sprintf("%s aaaaa", createPolicySQL),zap.String("database", "")) + if err != nil { + return err + } + task := NewTaskPolicyMeta(policy, createPolicySQL) + ctxDone := d.sendTaskToChan(tctx, task, taskChan) + if ctxDone { + return tctx.Err() + } + } + for dbName, tables := range allTables { if !conf.NoSchemas { createDatabaseSQL, err := ShowCreateDatabase(metaConn, dbName) diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index ebf8aef3fe164..bf158024a132c 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -92,14 +92,14 @@ func ShowCreateTable(db *sql.Conn, database, table string) (string, error) { return oneRow[1], nil } -// ShowCreatePolicy constructs the create policy SQL for a specified table +// ShowCreatePlacementPolicy constructs the create policy SQL for a specified table // returns (createPoilicySQL, error) -func ShowCreatePolicy(db *sql.Conn, database, policy string) (string, error) { +func ShowCreatePlacementPolicy(db *sql.Conn, policy string) (string, error) { var oneRow [2]string handleOneRow := func(rows *sql.Rows) error { return rows.Scan(&oneRow[0], &oneRow[1]) } - query := fmt.Sprintf("SHOW CREATE PLACEMENT POLICY `%s`.`%s`", escapeString(database), escapeString(policy)) + query := fmt.Sprintf("SHOW CREATE PLACEMENT POLICY %s", escapeString(policy)) err := simpleQuery(db, query, handleOneRow) if err != nil { return "", errors.Annotatef(err, "sql: %s", query) @@ -294,6 +294,25 @@ func ListAllDatabasesTables(tctx *tcontext.Context, db *sql.Conn, databaseNames return dbTables, nil } +func ListAllPlacementPolicyNames(db *sql.Conn) ([]string, error) { + var policyList []string + var policy string + const query = "select distinct policy_name from information_schema.placement_rules where policy_name is not null;" + rows, err := db.QueryContext(context.Background(), query) + if err != nil { + return policyList, errors.Annotatef(err, "sql: %s", query) + } + defer rows.Close() + for rows.Next() { + err := rows.Scan(&policy) + if err != nil { + return policyList, errors.Trace(err) + } + policyList = append(policyList, policy) + } + return policyList, nil +} + // SelectVersion gets the version information from the database server func SelectVersion(db *sql.DB) (string, error) { var versionInfo string diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index 11b6ebe3cac11..27388346bdcec 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -356,7 +356,7 @@ func TestShowCreatePolicy(t *testing.T) { WillReturnRows(sqlmock.NewRows([]string{"Policy", "Create Policy"}). AddRow("policy_x", "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1")) - createPolicySQL, err := ShowCreatePolicy(conn, "test", "policy_x") + createPolicySQL, err := ShowCreatePlacementPolicy(conn, "test", "policy_x") require.NoError(t, err) require.Equal(t, "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1", createPolicySQL) require.NoError(t, mock.ExpectationsWereMet()) diff --git a/dumpling/export/task.go b/dumpling/export/task.go index 8cfec59b24859..4e323babc40c3 100644 --- a/dumpling/export/task.go +++ b/dumpling/export/task.go @@ -4,7 +4,7 @@ package export import "fmt" -// Task is a file dump task for dumpling, it could either be dumping database/table/view metadata, table data +// Task is a file dump task for dumpling, it could either be dumping database/table/view/policy metadata, table data type Task interface { // Brief is the brief for a dumping task Brief() string @@ -34,6 +34,13 @@ type TaskViewMeta struct { CreateViewSQL string } +// TaskPolicyMeta is a dumping view metadata task +type TaskPolicyMeta struct { + Task + PolicyName string + CreatePolicySQL string +} + // TaskTableData is a dumping table data task type TaskTableData struct { Task @@ -70,6 +77,14 @@ func NewTaskViewMeta(dbName, tblName, createTableSQL, createViewSQL string) *Tas } } +// NewTaskPolicyMeta returns a new dumping placement policy metadata task +func NewTaskPolicyMeta(policyName, createPolicySQL string) *TaskPolicyMeta { + return &TaskPolicyMeta{ + PolicyName: policyName, + CreatePolicySQL: createPolicySQL, + } +} + // NewTaskTableData returns a new dumping table data task func NewTaskTableData(meta TableMeta, data TableDataIR, currentChunk, totalChunks int) *TaskTableData { return &TaskTableData{ @@ -95,6 +110,11 @@ func (t *TaskViewMeta) Brief() string { return fmt.Sprintf("meta of view '%s'.'%s'", t.DatabaseName, t.ViewName) } +// Brief implements task.Brief +func (t *TaskPolicyMeta) Brief() string { + return fmt.Sprintf("meta of placement policy '%s'", t.PolicyName) +} + // Brief implements task.Brief func (t *TaskTableData) Brief() string { db, tbl := t.Meta.DatabaseName(), t.Meta.TableName() diff --git a/dumpling/export/writer.go b/dumpling/export/writer.go index 4d8d5301db929..7520ff670462f 100644 --- a/dumpling/export/writer.go +++ b/dumpling/export/writer.go @@ -99,6 +99,8 @@ func (w *Writer) handleTask(task Task) error { return w.WriteTableMeta(t.DatabaseName, t.TableName, t.CreateTableSQL) case *TaskViewMeta: return w.WriteViewMeta(t.DatabaseName, t.ViewName, t.CreateTableSQL, t.CreateViewSQL) + case *TaskPolicyMeta: + return w.WritePolicyMeta(t.PolicyName, t.CreatePolicySQL) case *TaskTableData: err := w.WriteTableData(t.Meta, t.Data, t.ChunkIndex) if err != nil { From 20bd53656da6442e0d9e43da573366466ea87658 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 18:54:46 +0800 Subject: [PATCH 03/24] fix lint check --- dumpling/export/dump.go | 1 - dumpling/export/sql_test.go | 1 - dumpling/export/task.go | 6 +++--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 514972e25a5c0..ed58ee5647406 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -305,7 +305,6 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC // policy should be created before database for _, policy := range policyNames { createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) - tctx.L().Info(fmt.Sprintf("%s aaaaa", createPolicySQL),zap.String("database", "")) if err != nil { return err } diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index 27388346bdcec..081658244432b 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -363,7 +363,6 @@ func TestShowCreatePolicy(t *testing.T) { } - func TestGetSuitableRows(t *testing.T) { t.Parallel() diff --git a/dumpling/export/task.go b/dumpling/export/task.go index 4e323babc40c3..a9e5874fa350b 100644 --- a/dumpling/export/task.go +++ b/dumpling/export/task.go @@ -37,7 +37,7 @@ type TaskViewMeta struct { // TaskPolicyMeta is a dumping view metadata task type TaskPolicyMeta struct { Task - PolicyName string + PolicyName string CreatePolicySQL string } @@ -80,8 +80,8 @@ func NewTaskViewMeta(dbName, tblName, createTableSQL, createViewSQL string) *Tas // NewTaskPolicyMeta returns a new dumping placement policy metadata task func NewTaskPolicyMeta(policyName, createPolicySQL string) *TaskPolicyMeta { return &TaskPolicyMeta{ - PolicyName: policyName, - CreatePolicySQL: createPolicySQL, + PolicyName: policyName, + CreatePolicySQL: createPolicySQL, } } From 37b13ec098208109c26239750fd653f4ceec45a0 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 18:58:33 +0800 Subject: [PATCH 04/24] fix UT --- dumpling/export/sql.go | 2 +- dumpling/export/sql_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index bf158024a132c..0d8698b3a19e1 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -99,7 +99,7 @@ func ShowCreatePlacementPolicy(db *sql.Conn, policy string) (string, error) { handleOneRow := func(rows *sql.Rows) error { return rows.Scan(&oneRow[0], &oneRow[1]) } - query := fmt.Sprintf("SHOW CREATE PLACEMENT POLICY %s", escapeString(policy)) + query := fmt.Sprintf("SHOW CREATE PLACEMENT POLICY `%s`", escapeString(policy)) err := simpleQuery(db, query, handleOneRow) if err != nil { return "", errors.Annotatef(err, "sql: %s", query) diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index 081658244432b..1003b870d1a57 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -352,11 +352,11 @@ func TestShowCreatePolicy(t *testing.T) { conn, err := db.Conn(context.Background()) require.NoError(t, err) - mock.ExpectQuery("SHOW CREATE PLACEMENT POLICY `test`.`policy_x`"). + mock.ExpectQuery("SHOW CREATE PLACEMENT POLICY `policy_x`"). WillReturnRows(sqlmock.NewRows([]string{"Policy", "Create Policy"}). AddRow("policy_x", "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1")) - createPolicySQL, err := ShowCreatePlacementPolicy(conn, "test", "policy_x") + createPolicySQL, err := ShowCreatePlacementPolicy(conn, "policy_x") require.NoError(t, err) require.Equal(t, "CREATE PLACEMENT POLICY `policy_x` LEARNERS=1", createPolicySQL) require.NoError(t, mock.ExpectationsWereMet()) From eda00c5d2059b59bd5c97aa30997c87563577829 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 19:49:33 +0800 Subject: [PATCH 05/24] add test case --- Makefile | 2 +- .../result/x-placement-policy-create.sql | 3 +++ .../result/x1-placement-policy-create.sql | 3 +++ dumpling/tests/placement_policy/run.sh | 26 +++++++++++++++++++ dumpling/tests/views/run.sh | 2 +- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 dumpling/tests/placement_policy/result/x-placement-policy-create.sql create mode 100644 dumpling/tests/placement_policy/result/x1-placement-policy-create.sql create mode 100644 dumpling/tests/placement_policy/run.sh diff --git a/Makefile b/Makefile index 1ab375a2f161b..0e5e9879816fd 100644 --- a/Makefile +++ b/Makefile @@ -363,7 +363,7 @@ data_parsers: tools/bin/vfsgendev br/pkg/lightning/mydump/parser_generated.go br tools/bin/vfsgendev -source='"github.com/pingcap/tidb/br/pkg/lightning/web".Res' && mv res_vfsdata.go br/pkg/lightning/web/ build_dumpling: - GOOS=linux $(DUMPLING_GOBUILD) $(RACE_FLAG) -tags codes -o $(DUMPLING_BIN) dumpling/cmd/dumpling/main.go + $(DUMPLING_GOBUILD) $(RACE_FLAG) -tags codes -o $(DUMPLING_BIN) dumpling/cmd/dumpling/main.go dumpling_unit_test: export DUMPLING_ARGS=$$($(DUMPLING_PACKAGES)) dumpling_unit_test: failpoint-enable diff --git a/dumpling/tests/placement_policy/result/x-placement-policy-create.sql b/dumpling/tests/placement_policy/result/x-placement-policy-create.sql new file mode 100644 index 0000000000000..7ed1dc6b55c92 --- /dev/null +++ b/dumpling/tests/placement_policy/result/x-placement-policy-create.sql @@ -0,0 +1,3 @@ +/*!40101 SET NAMES binary*/; +/*T![placement] SET PLACEMENT_CHECKS = 0*/; +CREATE PLACEMENT POLICY `x` PRIMARY_REGION="cn-east-1" REGIONS="cn-east-1,cn-east"; diff --git a/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql b/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql new file mode 100644 index 0000000000000..656d53b51774b --- /dev/null +++ b/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql @@ -0,0 +1,3 @@ +/*!40101 SET NAMES binary*/; +/*T![placement] SET PLACEMENT_CHECKS = 0*/; +CREATE PLACEMENT POLICY `x1` FOLLOWERS=4; diff --git a/dumpling/tests/placement_policy/run.sh b/dumpling/tests/placement_policy/run.sh new file mode 100644 index 0000000000000..20623a19fb101 --- /dev/null +++ b/dumpling/tests/placement_policy/run.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# +# Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. + +set -eu + +export DUMPLING_TEST_PORT=4000 + +run_sql "drop database if exists policy" +run_sql "drop placement policy if exists x" +run_sql "drop placement policy if exists x1" +run_sql "create database policy" + +export DUMPLING_TEST_DATABASE="policy" + +run_sql 'CREATE PLACEMENT POLICY x PRIMARY_REGION="cn-east-1" REGIONS="cn-east-1,cn-east";' +run_sql 'CREATE PLACEMENT POLICY x1 FOLLOWERS=4;' + +run_dumpling + +file_should_exist "$DUMPLING_OUTPUT_DIR/policy-schema-create.sql" +file_should_exist "$DUMPLING_OUTPUT_DIR/x-placement-policy-create.sql" +file_should_exist "$DUMPLING_OUTPUT_DIR/x1-placement-policy-create.sql" + +diff "$DUMPLING_BASE_NAME/result/x-placement-policy-create.sql" "$DUMPLING_OUTPUT_DIR/x-placement-policy-create.sql" +diff "$DUMPLING_BASE_NAME/result/x1-placement-policy-create.sql" "$DUMPLING_OUTPUT_DIR/x1-placement-policy-create.sql" diff --git a/dumpling/tests/views/run.sh b/dumpling/tests/views/run.sh index b97513543db5c..b2d33167e13b6 100644 --- a/dumpling/tests/views/run.sh +++ b/dumpling/tests/views/run.sh @@ -20,7 +20,7 @@ file_not_exist "$DUMPLING_OUTPUT_DIR/views.v-schema-view.sql" rm -rf $DUMPLING_OUTPUT_DIR run_dumpling --no-views=false -#diff "$DUMPLING_BASE_NAME/data/views-schema-create.sql" "$DUMPLING_OUTPUT_DIR/views-schema-create.sql" +#diff "$DUMPLING_BASE_NAME/data/policy-schema-create.sql" "$DUMPLING_OUTPUT_DIR/policy-schema-create.sql" diff "$DUMPLING_BASE_NAME/data/views.v-schema.sql" "$DUMPLING_OUTPUT_DIR/views.v-schema.sql" diff "$DUMPLING_BASE_NAME/data/views.v-schema-view.sql" "$DUMPLING_OUTPUT_DIR/views.v-schema-view.sql" file_not_exist "$DUMPLING_OUTPUT_DIR/views.v.000000000.sql" From 788fbc86a18df1f1e42845740907de4f6d2cf306 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 19:52:00 +0800 Subject: [PATCH 06/24] fix mistake --- dumpling/tests/views/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/tests/views/run.sh b/dumpling/tests/views/run.sh index b2d33167e13b6..b97513543db5c 100644 --- a/dumpling/tests/views/run.sh +++ b/dumpling/tests/views/run.sh @@ -20,7 +20,7 @@ file_not_exist "$DUMPLING_OUTPUT_DIR/views.v-schema-view.sql" rm -rf $DUMPLING_OUTPUT_DIR run_dumpling --no-views=false -#diff "$DUMPLING_BASE_NAME/data/policy-schema-create.sql" "$DUMPLING_OUTPUT_DIR/policy-schema-create.sql" +#diff "$DUMPLING_BASE_NAME/data/views-schema-create.sql" "$DUMPLING_OUTPUT_DIR/views-schema-create.sql" diff "$DUMPLING_BASE_NAME/data/views.v-schema.sql" "$DUMPLING_OUTPUT_DIR/views.v-schema.sql" diff "$DUMPLING_BASE_NAME/data/views.v-schema-view.sql" "$DUMPLING_OUTPUT_DIR/views.v-schema-view.sql" file_not_exist "$DUMPLING_OUTPUT_DIR/views.v.000000000.sql" From 28785e60c8fdade235231a7407b320b7a842db73 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 23 Nov 2021 20:17:55 +0800 Subject: [PATCH 07/24] fix old test case --- dumpling/export/dump_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dumpling/export/dump_test.go b/dumpling/export/dump_test.go index 1626908f511e6..c76c8715236d5 100644 --- a/dumpling/export/dump_test.go +++ b/dumpling/export/dump_test.go @@ -26,9 +26,12 @@ func TestDumpBlock(t *testing.T) { require.NoError(t, db.Close()) }() - mock.ExpectQuery(fmt.Sprintf("SHOW CREATE DATABASE `%s`", escapeString(database))). - WillReturnRows(sqlmock.NewRows([]string{"Database", "Create Database"}). - AddRow("test", "CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */")) + mock.ExpectQuery("select distinct policy_name from information_schema.placement_rules where policy_name is not null;"). + WillReturnRows(sqlmock.NewRows([]string{"policy_name"}). + AddRow("x")) + mock.ExpectQuery("SHOW CREATE PLACEMENT POLICY `x`"). + WillReturnRows(sqlmock.NewRows([]string{"Policy", "Create Policy"}). + AddRow("x", "CREATE PLACEMENT POLICY `x` FOLLOWERS=4")) tctx, cancel := tcontext.Background().WithLogger(appLogger).WithCancel() defer cancel() @@ -54,7 +57,6 @@ func TestDumpBlock(t *testing.T) { // simulate taskChan is full taskChan := make(chan Task, 1) taskChan <- &TaskDatabaseMeta{} - d.conf.Tables = DatabaseTables{}.AppendTable(database, nil) require.ErrorIs(t, d.dumpDatabases(writerCtx, conn, taskChan), context.Canceled) require.ErrorIs(t, wg.Wait(), writerErr) } From 0ef7fa1757fbe2d10f3a9bec251fb48ffe015893 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 24 Nov 2021 10:31:41 +0800 Subject: [PATCH 08/24] fix compatibility --- dumpling/export/dump.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index ed58ee5647406..c13209a08e0c4 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -298,20 +298,23 @@ func (d *Dumper) startWriters(tctx *tcontext.Context, wg *errgroup.Group, taskCh func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskChan chan<- Task) error { conf := d.conf allTables := conf.Tables - policyNames, err := ListAllPlacementPolicyNames(metaConn) - if err != nil { - return err - } + // policy should be created before database - for _, policy := range policyNames { - createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) + if conf.ServerInfo.ServerType == version.ServerTypeTiDB { + policyNames, err := ListAllPlacementPolicyNames(metaConn) if err != nil { return err } - task := NewTaskPolicyMeta(policy, createPolicySQL) - ctxDone := d.sendTaskToChan(tctx, task, taskChan) - if ctxDone { - return tctx.Err() + for _, policy := range policyNames { + createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) + if err != nil { + return err + } + task := NewTaskPolicyMeta(policy, createPolicySQL) + ctxDone := d.sendTaskToChan(tctx, task, taskChan) + if ctxDone { + return tctx.Err() + } } } From c7ca8cdf158b86ff1caf224ce4e4aa0cd41720c3 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 24 Nov 2021 10:47:32 +0800 Subject: [PATCH 09/24] restore dumpblock test --- dumpling/export/dump_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dumpling/export/dump_test.go b/dumpling/export/dump_test.go index c76c8715236d5..284cec9435017 100644 --- a/dumpling/export/dump_test.go +++ b/dumpling/export/dump_test.go @@ -26,12 +26,9 @@ func TestDumpBlock(t *testing.T) { require.NoError(t, db.Close()) }() - mock.ExpectQuery("select distinct policy_name from information_schema.placement_rules where policy_name is not null;"). - WillReturnRows(sqlmock.NewRows([]string{"policy_name"}). - AddRow("x")) - mock.ExpectQuery("SHOW CREATE PLACEMENT POLICY `x`"). - WillReturnRows(sqlmock.NewRows([]string{"Policy", "Create Policy"}). - AddRow("x", "CREATE PLACEMENT POLICY `x` FOLLOWERS=4")) + mock.ExpectQuery(fmt.Sprintf("SHOW CREATE DATABASE `%s`", escapeString(database))). + WillReturnRows(sqlmock.NewRows([]string{"Database", "Create Database"}). + AddRow("test", "CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */")) tctx, cancel := tcontext.Background().WithLogger(appLogger).WithCancel() defer cancel() @@ -57,6 +54,8 @@ func TestDumpBlock(t *testing.T) { // simulate taskChan is full taskChan := make(chan Task, 1) taskChan <- &TaskDatabaseMeta{} + d.conf.Tables = DatabaseTables{}.AppendTable(database, nil) + d.conf.ServerInfo.ServerType = version.ServerTypeMySQL require.ErrorIs(t, d.dumpDatabases(writerCtx, conn, taskChan), context.Canceled) require.ErrorIs(t, wg.Wait(), writerErr) } From c43507cd7b99521ced9ec23d3d7838d0d67835c9 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 24 Nov 2021 11:38:41 +0800 Subject: [PATCH 10/24] add anno --- dumpling/export/dump.go | 3 ++- .../placement_policy/result/x-placement-policy-create.sql | 2 +- .../placement_policy/result/x1-placement-policy-create.sql | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index c13209a08e0c4..d8c300553414c 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -310,7 +310,8 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC if err != nil { return err } - task := NewTaskPolicyMeta(policy, createPolicySQL) + wrappedCreatePolicySQL := fmt.Sprintf("/*T![placement] %s */", createPolicySQL) + task := NewTaskPolicyMeta(policy, wrappedCreatePolicySQL) ctxDone := d.sendTaskToChan(tctx, task, taskChan) if ctxDone { return tctx.Err() diff --git a/dumpling/tests/placement_policy/result/x-placement-policy-create.sql b/dumpling/tests/placement_policy/result/x-placement-policy-create.sql index 7ed1dc6b55c92..0b68c742ee1cf 100644 --- a/dumpling/tests/placement_policy/result/x-placement-policy-create.sql +++ b/dumpling/tests/placement_policy/result/x-placement-policy-create.sql @@ -1,3 +1,3 @@ /*!40101 SET NAMES binary*/; /*T![placement] SET PLACEMENT_CHECKS = 0*/; -CREATE PLACEMENT POLICY `x` PRIMARY_REGION="cn-east-1" REGIONS="cn-east-1,cn-east"; +/*T![placement] CREATE PLACEMENT POLICY `x` PRIMARY_REGION="cn-east-1" REGIONS="cn-east-1,cn-east" */; diff --git a/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql b/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql index 656d53b51774b..d2dafe29b2a85 100644 --- a/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql +++ b/dumpling/tests/placement_policy/result/x1-placement-policy-create.sql @@ -1,3 +1,3 @@ /*!40101 SET NAMES binary*/; /*T![placement] SET PLACEMENT_CHECKS = 0*/; -CREATE PLACEMENT POLICY `x1` FOLLOWERS=4; +/*T![placement] CREATE PLACEMENT POLICY `x1` FOLLOWERS=4 */; From 0aea48a90b85edbd8d8b66ed68515d7be30a1264 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 24 Nov 2021 14:27:25 +0800 Subject: [PATCH 11/24] skip error when encounter sequence --- dumpling/tests/basic/run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/dumpling/tests/basic/run.sh b/dumpling/tests/basic/run.sh index 6caccce221433..20b82a5925cf0 100644 --- a/dumpling/tests/basic/run.sh +++ b/dumpling/tests/basic/run.sh @@ -90,6 +90,7 @@ echo "expected 2, actual ${actual}" # Test for tidb_mem_quota_query configuration export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return" +run_sql "drop sequence if exists basic.s" run_dumpling > ${DUMPLING_OUTPUT_DIR}/dumpling.log actual=$(grep -w "tidb_mem_quota_query == 1073741824" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l) echo "expected 1, actual ${actual}" From 47d733eae218f03b770ed180196b3e3d80584336 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 24 Nov 2021 14:49:14 +0800 Subject: [PATCH 12/24] debug in remote ci --- dumpling/tests/basic/run.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dumpling/tests/basic/run.sh b/dumpling/tests/basic/run.sh index 20b82a5925cf0..481c7bea2e116 100644 --- a/dumpling/tests/basic/run.sh +++ b/dumpling/tests/basic/run.sh @@ -89,12 +89,12 @@ echo "expected 2, actual ${actual}" [ "$actual" = 2 ] # Test for tidb_mem_quota_query configuration -export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return" +#export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return" run_sql "drop sequence if exists basic.s" -run_dumpling > ${DUMPLING_OUTPUT_DIR}/dumpling.log -actual=$(grep -w "tidb_mem_quota_query == 1073741824" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l) -echo "expected 1, actual ${actual}" -[ "$actual" = 1 ] +run_dumpling +#actual=$(grep -w "tidb_mem_quota_query == 1073741824" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l) +#echo "expected 1, actual ${actual}" +#[ "$actual" = 1 ] export GO_FAILPOINTS="" From e2adebc098c630c9b71a25025a51a3335f5e74fd Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 11:56:49 +0800 Subject: [PATCH 13/24] change the tidb version --- dumpling/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/install.sh b/dumpling/install.sh index e7eb9a8e2f687..697a5e88f68bd 100644 --- a/dumpling/install.sh +++ b/dumpling/install.sh @@ -17,7 +17,7 @@ cd $pwd/tidb-lightning && make cd $pwd mv tidb-lightning/bin/tidb-lightning bin/ -TIDB_TAG="v4.0.4" +TIDB_TAG="v5.3.0" # download tidb-server git clone -b $TIDB_TAG https://github.com/pingcap/tidb cd $pwd/tidb && make From 13aafdf001ce27582e02bf24c13f0b85cb41717d Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 12:02:47 +0800 Subject: [PATCH 14/24] change tidb version to master --- dumpling/install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/install.sh b/dumpling/install.sh index 697a5e88f68bd..e51ee861b099c 100644 --- a/dumpling/install.sh +++ b/dumpling/install.sh @@ -17,7 +17,7 @@ cd $pwd/tidb-lightning && make cd $pwd mv tidb-lightning/bin/tidb-lightning bin/ -TIDB_TAG="v5.3.0" +TIDB_TAG="master" # download tidb-server git clone -b $TIDB_TAG https://github.com/pingcap/tidb cd $pwd/tidb && make From b975983baf40d455d4d8ee01caf53c4b887a1ae6 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 12:12:46 +0800 Subject: [PATCH 15/24] fix basic test for easy debug --- dumpling/tests/basic/run.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/dumpling/tests/basic/run.sh b/dumpling/tests/basic/run.sh index 481c7bea2e116..d801970643b75 100644 --- a/dumpling/tests/basic/run.sh +++ b/dumpling/tests/basic/run.sh @@ -89,12 +89,11 @@ echo "expected 2, actual ${actual}" [ "$actual" = 2 ] # Test for tidb_mem_quota_query configuration -#export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return" -run_sql "drop sequence if exists basic.s" -run_dumpling -#actual=$(grep -w "tidb_mem_quota_query == 1073741824" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l) -#echo "expected 1, actual ${actual}" -#[ "$actual" = 1 ] +export GO_FAILPOINTS="github.com/pingcap/tidb/dumpling/export/PrintTiDBMemQuotaQuery=1*return" +run_dumpling | tee ${DUMPLING_OUTPUT_DIR}/dumpling.log +actual=$(grep -w "tidb_mem_quota_query == 1073741824" ${DUMPLING_OUTPUT_DIR}/dumpling.log|wc -l) +echo "expected 1, actual ${actual}" +[ "$actual" = 1 ] export GO_FAILPOINTS="" From 0f51610136a38e20d2452e80209685913e7cf16b Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 14:18:40 +0800 Subject: [PATCH 16/24] Update dumpling/tests/placement_policy/run.sh Co-authored-by: kennytm --- dumpling/tests/placement_policy/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/tests/placement_policy/run.sh b/dumpling/tests/placement_policy/run.sh index 20623a19fb101..cd68bc0caa333 100644 --- a/dumpling/tests/placement_policy/run.sh +++ b/dumpling/tests/placement_policy/run.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. +# Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0. set -eu From 6f7d245567214b14f5fe2bffc31dbb35195db40b Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 17:02:44 +0800 Subject: [PATCH 17/24] skip the unsupport --- dumpling/export/dump.go | 30 +++++++++++++++++------------- dumpling/export/sql.go | 3 ++- dumpling/export/sql_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index d8c300553414c..fa98ada96aab5 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -8,6 +8,7 @@ import ( "database/sql" "encoding/hex" "fmt" + "github.com/go-sql-driver/mysql" "math/big" "sort" "strconv" @@ -300,22 +301,25 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC allTables := conf.Tables // policy should be created before database - if conf.ServerInfo.ServerType == version.ServerTypeTiDB { - policyNames, err := ListAllPlacementPolicyNames(metaConn) + policyNames, err := ListAllPlacementPolicyNames(metaConn) + if err != nil { + if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { + // some old tidb version and other server type doesn't support placement rules, we can skip it. + tctx.L().Warn("fail to dump placement policy, maybe the server doesn't support it", log.ShortError(err)) + } else { + return err + } + } + for _, policy := range policyNames { + createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) if err != nil { return err } - for _, policy := range policyNames { - createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) - if err != nil { - return err - } - wrappedCreatePolicySQL := fmt.Sprintf("/*T![placement] %s */", createPolicySQL) - task := NewTaskPolicyMeta(policy, wrappedCreatePolicySQL) - ctxDone := d.sendTaskToChan(tctx, task, taskChan) - if ctxDone { - return tctx.Err() - } + wrappedCreatePolicySQL := fmt.Sprintf("/*T![placement] %s */", createPolicySQL) + task := NewTaskPolicyMeta(policy, wrappedCreatePolicySQL) + ctxDone := d.sendTaskToChan(tctx, task, taskChan) + if ctxDone { + return tctx.Err() } } diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 0d8698b3a19e1..69ba4104c6d51 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -300,7 +300,8 @@ func ListAllPlacementPolicyNames(db *sql.Conn) ([]string, error) { const query = "select distinct policy_name from information_schema.placement_rules where policy_name is not null;" rows, err := db.QueryContext(context.Background(), query) if err != nil { - return policyList, errors.Annotatef(err, "sql: %s", query) +// return policyList, errors.Annotatef(err, "sql: %s", query) + return policyList, err } defer rows.Close() for rows.Next() { diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index 1003b870d1a57..43af3e53f1ccf 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -9,6 +9,7 @@ import ( "encoding/csv" "encoding/json" "fmt" + "github.com/go-sql-driver/mysql" "io" "os" "path" @@ -363,6 +364,36 @@ func TestShowCreatePolicy(t *testing.T) { } +func TestListPolicyNames(t *testing.T) { + t.Parallel() + + db, mock, err := sqlmock.New() + require.NoError(t, err) + defer func() { + require.NoError(t, db.Close()) + }() + + conn, err := db.Conn(context.Background()) + require.NoError(t, err) + + mock.ExpectQuery("select distinct policy_name from information_schema.placement_rules where policy_name is not null;"). + WillReturnRows(sqlmock.NewRows([]string{ "policy_name"}). + AddRow("policy_x")) + policies, err := ListAllPlacementPolicyNames(conn) + require.NoError(t, err) + require.Equal(t, []string{"policy_x"}, policies) + require.NoError(t, mock.ExpectationsWereMet()) + + // some old tidb version doesn't support placement rules returns error + expectedErr := &mysql.MySQLError{Number: ErrNoSuchTable, Message: "Table 'information_schema.placement_rules' doesn't exist"} + mock.ExpectExec("select distinct policy_name from information_schema.placement_rules where policy_name is not null;"). + WillReturnError(expectedErr) + policies, err = ListAllPlacementPolicyNames(conn) + if mysqlErr, ok := err.(*mysql.MySQLError); ok { + require.Equal(t, mysqlErr.Number, ErrNoSuchTable) + } +} + func TestGetSuitableRows(t *testing.T) { t.Parallel() From 778be145823264f223596f19c2ff6d3d355b396c Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 17:22:08 +0800 Subject: [PATCH 18/24] fix --- dumpling/export/dump.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index fa98ada96aab5..31db08f395305 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -301,25 +301,27 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC allTables := conf.Tables // policy should be created before database - policyNames, err := ListAllPlacementPolicyNames(metaConn) - if err != nil { - if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { - // some old tidb version and other server type doesn't support placement rules, we can skip it. - tctx.L().Warn("fail to dump placement policy, maybe the server doesn't support it", log.ShortError(err)) - } else { - return err - } - } - for _, policy := range policyNames { - createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) + if conf.ServerInfo.ServerType == version.ServerTypeTiDB { + policyNames, err := ListAllPlacementPolicyNames(metaConn) if err != nil { - return err + if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { + // some old tidb version and other server type doesn't support placement rules, we can skip it. + tctx.L().Warn("fail to dump placement policy, maybe the server doesn't support it", log.ShortError(err)) + } else { + return err + } } - wrappedCreatePolicySQL := fmt.Sprintf("/*T![placement] %s */", createPolicySQL) - task := NewTaskPolicyMeta(policy, wrappedCreatePolicySQL) - ctxDone := d.sendTaskToChan(tctx, task, taskChan) - if ctxDone { - return tctx.Err() + for _, policy := range policyNames { + createPolicySQL, err := ShowCreatePlacementPolicy(metaConn, policy) + if err != nil { + return err + } + wrappedCreatePolicySQL := fmt.Sprintf("/*T![placement] %s */", createPolicySQL) + task := NewTaskPolicyMeta(policy, wrappedCreatePolicySQL) + ctxDone := d.sendTaskToChan(tctx, task, taskChan) + if ctxDone { + return tctx.Err() + } } } From 1ab82da20df8adeb8867a85adfab599e78398e04 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 25 Nov 2021 17:34:46 +0800 Subject: [PATCH 19/24] fix lint --- dumpling/export/dump.go | 1 + dumpling/export/sql.go | 2 +- dumpling/export/sql_test.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 31db08f395305..51e20646df5ef 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -301,6 +301,7 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC allTables := conf.Tables // policy should be created before database + // placement policy in other server type can be different, so we only handle the tidb server if conf.ServerInfo.ServerType == version.ServerTypeTiDB { policyNames, err := ListAllPlacementPolicyNames(metaConn) if err != nil { diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 69ba4104c6d51..8c88eb94ddb3c 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -300,7 +300,7 @@ func ListAllPlacementPolicyNames(db *sql.Conn) ([]string, error) { const query = "select distinct policy_name from information_schema.placement_rules where policy_name is not null;" rows, err := db.QueryContext(context.Background(), query) if err != nil { -// return policyList, errors.Annotatef(err, "sql: %s", query) + // return policyList, errors.Annotatef(err, "sql: %s", query) return policyList, err } defer rows.Close() diff --git a/dumpling/export/sql_test.go b/dumpling/export/sql_test.go index 43af3e53f1ccf..6a69c7dc5c164 100644 --- a/dumpling/export/sql_test.go +++ b/dumpling/export/sql_test.go @@ -377,7 +377,7 @@ func TestListPolicyNames(t *testing.T) { require.NoError(t, err) mock.ExpectQuery("select distinct policy_name from information_schema.placement_rules where policy_name is not null;"). - WillReturnRows(sqlmock.NewRows([]string{ "policy_name"}). + WillReturnRows(sqlmock.NewRows([]string{"policy_name"}). AddRow("policy_x")) policies, err := ListAllPlacementPolicyNames(conn) require.NoError(t, err) From 4fe1e5f9e607827352ec8a98e8c3a1815d0c8270 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 26 Nov 2021 23:10:19 +0800 Subject: [PATCH 20/24] fix error --- dumpling/export/sql.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 8c88eb94ddb3c..0d8698b3a19e1 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -300,8 +300,7 @@ func ListAllPlacementPolicyNames(db *sql.Conn) ([]string, error) { const query = "select distinct policy_name from information_schema.placement_rules where policy_name is not null;" rows, err := db.QueryContext(context.Background(), query) if err != nil { - // return policyList, errors.Annotatef(err, "sql: %s", query) - return policyList, err + return policyList, errors.Annotatef(err, "sql: %s", query) } defer rows.Close() for rows.Next() { From a774f6bd3a97479cbc08659edfe2a43747ecc2fc Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 29 Nov 2021 16:04:06 +0800 Subject: [PATCH 21/24] Update dumpling/export/dump.go Co-authored-by: Chunzhu Li --- dumpling/export/dump.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 51e20646df5ef..e8d9fda455196 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -305,7 +305,8 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC if conf.ServerInfo.ServerType == version.ServerTypeTiDB { policyNames, err := ListAllPlacementPolicyNames(metaConn) if err != nil { - if mysqlErr, ok := err.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { + errCause := errors.Cause(err) + if mysqlErr, ok := errCause.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { // some old tidb version and other server type doesn't support placement rules, we can skip it. tctx.L().Warn("fail to dump placement policy, maybe the server doesn't support it", log.ShortError(err)) } else { From 3d8bd891e3a1bb36bc217f56bfdb4f49059648ce Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 29 Nov 2021 16:04:54 +0800 Subject: [PATCH 22/24] Update dumpling/export/sql.go Co-authored-by: Chunzhu Li --- dumpling/export/sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/export/sql.go b/dumpling/export/sql.go index 0d8698b3a19e1..1fb5688338b2e 100644 --- a/dumpling/export/sql.go +++ b/dumpling/export/sql.go @@ -310,7 +310,7 @@ func ListAllPlacementPolicyNames(db *sql.Conn) ([]string, error) { } policyList = append(policyList, policy) } - return policyList, nil + return policyList, errors.Annotatef(rows.Err(), "sql: %s", query) } // SelectVersion gets the version information from the database server From f52b86390dfb53fa0fef4706431b43d02c9ffcdf Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 29 Nov 2021 16:07:15 +0800 Subject: [PATCH 23/24] Update dumpling/export/dump.go Co-authored-by: Chunzhu Li --- dumpling/export/dump.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index e8d9fda455196..5bbe55e3f3b64 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -308,7 +308,7 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC errCause := errors.Cause(err) if mysqlErr, ok := errCause.(*mysql.MySQLError); ok && mysqlErr.Number == ErrNoSuchTable { // some old tidb version and other server type doesn't support placement rules, we can skip it. - tctx.L().Warn("fail to dump placement policy, maybe the server doesn't support it", log.ShortError(err)) + tctx.L().Debug("cannot dump placement policy, maybe the server doesn't support it", log.ShortError(err)) } else { return err } From c028f54782deca860706764f21c31c3166669403 Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 29 Nov 2021 16:08:25 +0800 Subject: [PATCH 24/24] fix error info --- dumpling/export/dump.go | 2 +- dumpling/tests/placement_policy/run.sh | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/dumpling/export/dump.go b/dumpling/export/dump.go index 5bbe55e3f3b64..de7eddcfa3b44 100755 --- a/dumpling/export/dump.go +++ b/dumpling/export/dump.go @@ -310,7 +310,7 @@ func (d *Dumper) dumpDatabases(tctx *tcontext.Context, metaConn *sql.Conn, taskC // some old tidb version and other server type doesn't support placement rules, we can skip it. tctx.L().Debug("cannot dump placement policy, maybe the server doesn't support it", log.ShortError(err)) } else { - return err + tctx.L().Warn("fail to dump placement policy: ", log.ShortError(err)) } } for _, policy := range policyNames { diff --git a/dumpling/tests/placement_policy/run.sh b/dumpling/tests/placement_policy/run.sh index cd68bc0caa333..b7653fc921378 100644 --- a/dumpling/tests/placement_policy/run.sh +++ b/dumpling/tests/placement_policy/run.sh @@ -24,3 +24,7 @@ file_should_exist "$DUMPLING_OUTPUT_DIR/x1-placement-policy-create.sql" diff "$DUMPLING_BASE_NAME/result/x-placement-policy-create.sql" "$DUMPLING_OUTPUT_DIR/x-placement-policy-create.sql" diff "$DUMPLING_BASE_NAME/result/x1-placement-policy-create.sql" "$DUMPLING_OUTPUT_DIR/x1-placement-policy-create.sql" + +run_sql "drop database if exists policy" +run_sql "drop placement policy if exists x" +run_sql "drop placement policy if exists x1"