Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 authored and ti-chi-bot committed Aug 24, 2021
1 parent a91674e commit 8332923
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 56 deletions.
6 changes: 3 additions & 3 deletions v4/export/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,11 @@ func extractTiDBRowIDFromDecodedKey(indexField, key string) (string, error) {
}

func getListTableTypeByConf(conf *Config) listTableType {
// use listTableByInfoSchema by default because it has more accurate information
listType := listTableByInfoSchema
// use listTableByShowTableStatus by default because it has better performance
listType := listTableByShowTableStatus
if conf.Consistency == consistencyTypeLock {
// for consistency lock, we need to build the tables to dump as soon as possible
listType = listTableByShowTableStatus
listType = listTableByInfoSchema
} else if conf.Consistency != consistencyTypeNone && matchMysqlBugversion(conf.ServerInfo) {
// For some buggy versions of mysql, we need a workaround to get a list of table names.
listType = listTableByShowFullTables
Expand Down
15 changes: 8 additions & 7 deletions v4/export/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,22 @@ func (s *testSQLSuite) TestDumpTableMeta(c *C) {

func (s *testSQLSuite) TestGetListTableTypeByConf(c *C) {
conf := defaultConfigForTest(c)
tctx := tcontext.Background().WithLogger(appLogger)
cases := []struct {
serverInfo ServerInfo
consistency string
expected listTableType
}{
{ParseServerInfo(tcontext.Background(), "5.7.25-TiDB-3.0.6"), consistencyTypeSnapshot, listTableByInfoSchema},
{ParseServerInfo(tctx, "5.7.25-TiDB-3.0.6"), consistencyTypeSnapshot, listTableByShowTableStatus},
// no bug version
{ParseServerInfo(tcontext.Background(), "8.0.2"), consistencyTypeLock, listTableByShowTableStatus},
{ParseServerInfo(tcontext.Background(), "8.0.2"), consistencyTypeFlush, listTableByInfoSchema},
{ParseServerInfo(tcontext.Background(), "8.0.23"), consistencyTypeNone, listTableByInfoSchema},
{ParseServerInfo(tctx, "8.0.2"), consistencyTypeLock, listTableByInfoSchema},
{ParseServerInfo(tctx, "8.0.2"), consistencyTypeFlush, listTableByShowTableStatus},
{ParseServerInfo(tctx, "8.0.23"), consistencyTypeNone, listTableByShowTableStatus},

// bug version
{ParseServerInfo(tcontext.Background(), "8.0.3"), consistencyTypeLock, listTableByShowTableStatus},
{ParseServerInfo(tcontext.Background(), "8.0.3"), consistencyTypeFlush, listTableByShowFullTables},
{ParseServerInfo(tcontext.Background(), "8.0.3"), consistencyTypeNone, listTableByInfoSchema},
{ParseServerInfo(tctx, "8.0.3"), consistencyTypeLock, listTableByInfoSchema},
{ParseServerInfo(tctx, "8.0.3"), consistencyTypeFlush, listTableByShowFullTables},
{ParseServerInfo(tctx, "8.0.3"), consistencyTypeNone, listTableByShowTableStatus},
}

for _, x := range cases {
Expand Down
56 changes: 10 additions & 46 deletions v4/export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,20 @@ func ListAllDatabasesTables(tctx *tcontext.Context, db *sql.Conn, databaseNames
dbTables[schema] = make([]*TableInfo, 0)
query := fmt.Sprintf("SHOW FULL TABLES FROM `%s` WHERE %s",
escapeString(schema), strings.Join(tableTypeConditions, " OR "))
rows, err := db.QueryContext(tctx, query)
if err != nil {
return nil, errors.Annotatef(err, "sql: %s", query)
}
results, err := GetColumnValuesAndIgonreColumnName(rows)
if err != nil {
return nil, errors.Annotatef(err, "sql: %s", query)
}
for _, oneRow := range results {
table = oneRow[0]
avgRowLength = 0 // can't get avgRowLength from the result of `show full tables` so hardcode to 0 here
if err = simpleQueryWithArgs(db, func(rows *sql.Rows) error {
var err2 error
tableType, err2 = ParseTableType(oneRow[1])
if err2 = rows.Scan(&table, &tableTypeStr); err != nil {
return errors.Trace(err2)
}
tableType, err2 = ParseTableType(tableTypeStr)
if err2 != nil {
return nil, errors.Trace(err2)
return errors.Trace(err2)
}
avgRowLength = 0 // can't get avgRowLength from the result of `show full tables` so hardcode to 0 here
dbTables[schema] = append(dbTables[schema], &TableInfo{table, avgRowLength, tableType})
return nil
}, query); err != nil {
return nil, errors.Annotatef(err, "sql: %s", query)
}
}
default:
Expand Down Expand Up @@ -583,39 +580,6 @@ func GetSpecifiedColumnValuesAndClose(rows *sql.Rows, columnName ...string) ([][
return strs, errors.Trace(rows.Err())
}

// GetColumnValuesAndIgonreColumnName get columns' values and ignore column name
func GetColumnValuesAndIgonreColumnName(rows *sql.Rows) ([][]string, error) {
if rows == nil {
return [][]string{}, nil
}
defer rows.Close()
results := [][]string{}
columns, err := rows.Columns()
if err != nil {
return nil, errors.Trace(err)
}
columnsLen := len(columns)
addr := make([]interface{}, columnsLen)
oneRow := make([]sql.NullString, columnsLen)
for i := range columns {
addr[i] = &oneRow[i]
}
for rows.Next() {
err := rows.Scan(addr...)
if err != nil {
return nil, errors.Trace(err)
}
tmpStr := make([]string, columnsLen)
for i, val := range oneRow {
if val.Valid {
tmpStr[i] = val.String
}
}
results = append(results, tmpStr)
}
return results, errors.Trace(rows.Err())
}

// GetPdAddrs gets PD address from TiDB
func GetPdAddrs(tctx *tcontext.Context, db *sql.DB) ([]string, error) {
query := "SELECT * FROM information_schema.cluster_info where type = 'pd';"
Expand Down

0 comments on commit 8332923

Please sign in to comment.