This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
fix consistency bug #128
Merged
Merged
fix consistency bug #128
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/bin/sh | ||
|
||
set -eu | ||
cur=$(cd `dirname $0`; pwd) | ||
|
||
DB_NAME="mysql_consistency" | ||
TABLE_NAME="t" | ||
|
||
# drop database on mysql | ||
run_sql "drop database if exists \`$DB_NAME\`;" | ||
|
||
# build data on mysql | ||
run_sql "create database $DB_NAME;" | ||
run_sql "create table $DB_NAME.$TABLE_NAME (a int(255));" | ||
|
||
# insert 100 records | ||
run_sql "insert into $DB_NAME.$TABLE_NAME values $(seq -s, 100 | sed 's/,*$//g' | sed "s/[0-9]*/('1')/g");" | ||
|
||
# dumping with consistency flush | ||
export DUMPLING_TEST_DATABASE=$DB_NAME | ||
export GO_FAILPOINTS="github.com/pingcap/dumpling/v4/export/ConsistencyCheck=return(\"5s\")" | ||
run_dumpling & | ||
# wait dumpling process to start to sleep | ||
sleep 2 | ||
|
||
# record metadata info | ||
metadata=`run_sql "show master status;"` | ||
metaLog=`echo $metadata | awk -F 'File:' '{print $2}' | awk '{print $1}'` | ||
metaPos=`echo $metadata | awk -F 'Position:' '{print $2}' | awk '{print $1}'` | ||
metaGTID=`echo $metadata | awk -F 'Executed_Gtid_Set:' '{print $2}' | awk '{print $1}'` | ||
# insert 100 more records, test whether dumpling will dump these data out | ||
run_sql "insert into $DB_NAME.$TABLE_NAME values $(seq -s, 100 | sed 's/,*$//g' | sed "s/[0-9]*/('1')/g");" | ||
|
||
wait | ||
|
||
# check data record count | ||
cnt=`grep -o "(1)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.0.sql|wc -l` | ||
echo "1st records count is ${cnt}" | ||
[ $cnt = 100 ] | ||
|
||
# check metadata | ||
echo "metaLog: $metaLog" | ||
echo "metaPos: $metaPos" | ||
echo "metaGTID: $metaGTID" | ||
if [ $metaLog != "" ]; then | ||
[ `grep -o "Log: $metaLog" ${DUMPLING_OUTPUT_DIR}/metadata|wc -l` ] | ||
fi | ||
if [ $metaPos != "" ]; then | ||
[ `grep -o "Pos: $metaPos" ${DUMPLING_OUTPUT_DIR}/metadata|wc -l` ] | ||
fi | ||
if [ $metaGTID != "" ]; then | ||
[ `grep -o "GTID: $metaGTID" ${DUMPLING_OUTPUT_DIR}/metadata|wc -l` ] | ||
fi | ||
|
||
# test dumpling normally | ||
export GO_FAILPOINTS="" | ||
run_dumpling | ||
cnt=`grep -o "(1)" ${DUMPLING_OUTPUT_DIR}/${DB_NAME}.${TABLE_NAME}.0.sql|wc -l` | ||
echo "2nd records count is ${cnt}" | ||
[ $cnt = 200 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,22 +1,27 @@ | ||||||
package export | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"database/sql" | ||||||
"errors" | ||||||
"fmt" | ||||||
) | ||||||
|
||||||
func NewConsistencyController(conf *Config, session *sql.DB) (ConsistencyController, error) { | ||||||
func NewConsistencyController(ctx context.Context, conf *Config, session *sql.DB) (ConsistencyController, error) { | ||||||
resolveAutoConsistency(conf) | ||||||
conn, err := session.Conn(ctx) | ||||||
if err != nil { | ||||||
return nil, err | ||||||
} | ||||||
switch conf.Consistency { | ||||||
case "flush": | ||||||
return &ConsistencyFlushTableWithReadLock{ | ||||||
serverType: conf.ServerInfo.ServerType, | ||||||
db: session, | ||||||
conn: conn, | ||||||
}, nil | ||||||
case "lock": | ||||||
return &ConsistencyLockDumpingTables{ | ||||||
db: session, | ||||||
conn: conn, | ||||||
allTables: conf.Tables, | ||||||
}, nil | ||||||
case "snapshot": | ||||||
|
@@ -48,33 +53,30 @@ func (c *ConsistencyNone) TearDown() error { | |||||
|
||||||
type ConsistencyFlushTableWithReadLock struct { | ||||||
serverType ServerType | ||||||
db *sql.DB | ||||||
conn *sql.Conn | ||||||
} | ||||||
|
||||||
func (c *ConsistencyFlushTableWithReadLock) Setup() error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about
Suggested change
|
||||||
if c.serverType == ServerTypeTiDB { | ||||||
return withStack(errors.New("'flush table with read lock' cannot be used to ensure the consistency in TiDB")) | ||||||
} | ||||||
return FlushTableWithReadLock(c.db) | ||||||
return FlushTableWithReadLock(c.conn) | ||||||
} | ||||||
|
||||||
func (c *ConsistencyFlushTableWithReadLock) TearDown() error { | ||||||
err := c.db.Ping() | ||||||
if err != nil { | ||||||
return withStack(errors.New("ConsistencyFlushTableWithReadLock lost database connection")) | ||||||
} | ||||||
return UnlockTables(c.db) | ||||||
defer c.conn.Close() | ||||||
return UnlockTables(c.conn) | ||||||
} | ||||||
|
||||||
type ConsistencyLockDumpingTables struct { | ||||||
db *sql.DB | ||||||
conn *sql.Conn | ||||||
allTables DatabaseTables | ||||||
} | ||||||
|
||||||
func (c *ConsistencyLockDumpingTables) Setup() error { | ||||||
for dbName, tables := range c.allTables { | ||||||
for _, table := range tables { | ||||||
err := LockTables(c.db, dbName, table.Name) | ||||||
err := LockTables(c.conn, dbName, table.Name) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
@@ -84,11 +86,8 @@ func (c *ConsistencyLockDumpingTables) Setup() error { | |||||
} | ||||||
|
||||||
func (c *ConsistencyLockDumpingTables) TearDown() error { | ||||||
err := c.db.Ping() | ||||||
if err != nil { | ||||||
return withStack(errors.New("ConsistencyLockDumpingTables lost database connection")) | ||||||
} | ||||||
return UnlockTables(c.db) | ||||||
defer c.conn.Close() | ||||||
return UnlockTables(c.conn) | ||||||
} | ||||||
|
||||||
const showMasterStatusFieldNum = 5 | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |||||||||||||||||||||
"github.com/pingcap/dumpling/v4/log" | ||||||||||||||||||||||
|
||||||||||||||||||||||
_ "github.com/go-sql-driver/mysql" | ||||||||||||||||||||||
"github.com/pingcap/failpoint" | ||||||||||||||||||||||
pd "github.com/pingcap/pd/v4/client" | ||||||||||||||||||||||
"go.uber.org/zap" | ||||||||||||||||||||||
"golang.org/x/sync/errgroup" | ||||||||||||||||||||||
|
@@ -133,7 +134,7 @@ func Dump(pCtx context.Context, conf *Config) (err error) { | |||||||||||||||||||||
conn.Close() | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
conCtrl, err := NewConsistencyController(conf, pool) | ||||||||||||||||||||||
conCtrl, err := NewConsistencyController(ctx, conf, pool) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
@@ -146,10 +147,6 @@ func Dump(pCtx context.Context, conf *Config) (err error) { | |||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if err = conCtrl.TearDown(); err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// for other consistencies, we should get table list after consistency is set up and GlobalMetaData is cached | ||||||||||||||||||||||
// for other consistencies, record snapshot after whole tables are locked. The recorded meta info is exactly the locked snapshot. | ||||||||||||||||||||||
if conf.Consistency != "lock" { | ||||||||||||||||||||||
|
@@ -165,6 +162,20 @@ func Dump(pCtx context.Context, conf *Config) (err error) { | |||||||||||||||||||||
connectPool.releaseConn(conn) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if err = conCtrl.TearDown(); err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
failpoint.Inject("ConsistencyCheck", func(val failpoint.Value) { | ||||||||||||||||||||||
interval, err := time.ParseDuration(val.(string)) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
log.Warn("inject failpoint ConsistencyCheck failed", zap.Reflect("value", val), zap.Error(err)) | ||||||||||||||||||||||
} else { | ||||||||||||||||||||||
log.Info("start to sleep for failpoint ConsistencyCheck", zap.Duration("sleepTime", interval)) | ||||||||||||||||||||||
time.Sleep(interval) | ||||||||||||||||||||||
} | ||||||||||||||||||||||
}) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
var writer Writer | ||||||||||||||||||||||
switch strings.ToLower(conf.FileType) { | ||||||||||||||||||||||
case "sql": | ||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,7 @@ func SelectAllFromTable(conf *Config, db *sql.Conn, database, table string) (Tab | |
} | ||
|
||
func SelectFromSql(conf *Config, db *sql.Conn) (TableDataIR, error) { | ||
log.Info("dump data from sql", zap.String("sql", conf.Sql)) | ||
rows, err := db.QueryContext(context.Background(), conf.Sql) | ||
if err != nil { | ||
return nil, withStack(errors.WithMessage(err, conf.Sql)) | ||
|
@@ -295,18 +296,18 @@ func GetUniqueIndexName(db *sql.Conn, database, table string) (string, error) { | |
return colName, nil | ||
} | ||
|
||
func FlushTableWithReadLock(db *sql.DB) error { | ||
_, err := db.Exec("FLUSH TABLES WITH READ LOCK") | ||
func FlushTableWithReadLock(db *sql.Conn) error { | ||
_, err := db.ExecContext(context.Background(), "FLUSH TABLES WITH READ LOCK") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoiding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed, PTAL again |
||
return withStack(err) | ||
} | ||
|
||
func LockTables(db *sql.DB, database, table string) error { | ||
_, err := db.Exec(fmt.Sprintf("LOCK TABLES `%s`.`%s` READ", escapeString(database), escapeString(table))) | ||
func LockTables(db *sql.Conn, database, table string) error { | ||
_, err := db.ExecContext(context.Background(), fmt.Sprintf("LOCK TABLES `%s`.`%s` READ", escapeString(database), escapeString(table))) | ||
return withStack(err) | ||
} | ||
|
||
func UnlockTables(db *sql.DB) error { | ||
_, err := db.Exec("UNLOCK TABLES") | ||
func UnlockTables(db *sql.Conn) error { | ||
_, err := db.ExecContext(context.Background(), "UNLOCK TABLES") | ||
return withStack(err) | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.