github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/dbconn/utils.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package dbconn 15 16 import ( 17 "fmt" 18 "net" 19 "strconv" 20 "strings" 21 22 "github.com/pingcap/failpoint" 23 "github.com/pingcap/tidb/pkg/parser" 24 tmysql "github.com/pingcap/tidb/pkg/parser/mysql" 25 "github.com/pingcap/tiflow/dm/pkg/conn" 26 tcontext "github.com/pingcap/tiflow/dm/pkg/context" 27 "github.com/pingcap/tiflow/dm/pkg/log" 28 "github.com/pingcap/tiflow/dm/pkg/terror" 29 "go.uber.org/atomic" 30 "go.uber.org/zap" 31 ) 32 33 // GetSchemaCreateSQL gets table create sql by 'show create table schema.table'. 34 func GetSchemaCreateSQL(tctx *tcontext.Context, conn *DBConn, schemaID string) (sql string, err error) { 35 querySQL := fmt.Sprintf("SHOW CREATE schema %s", schemaID) 36 var schema, createStr string 37 38 rows, err := conn.QuerySQL(tctx, nil, querySQL) 39 if err != nil { 40 return "", terror.DBErrorAdapt(err, conn.Scope(), terror.ErrDBDriverError) 41 } 42 43 defer rows.Close() 44 if rows.Next() { 45 if scanErr := rows.Scan(&schema, &createStr); scanErr != nil { 46 return "", terror.DBErrorAdapt(scanErr, conn.Scope(), terror.ErrDBDriverError) 47 } 48 } else { 49 return "", terror.ErrSyncerDownstreamTableNotFound.Generate(schema) 50 } 51 52 if err = rows.Close(); err != nil { 53 return "", terror.DBErrorAdapt(rows.Err(), conn.Scope(), terror.ErrDBDriverError) 54 } 55 return createStr, nil 56 } 57 58 // GetTableCreateSQL gets table create sql by 'show create table schema.table'. 59 func GetTableCreateSQL(tctx *tcontext.Context, conn *DBConn, tableID string) (sql string, err error) { 60 querySQL := fmt.Sprintf("SHOW CREATE TABLE %s", tableID) 61 var table, createStr string 62 63 rows, err := conn.QuerySQL(tctx, nil, querySQL) 64 if err != nil { 65 return "", terror.DBErrorAdapt(err, conn.Scope(), terror.ErrDBDriverError) 66 } 67 68 defer rows.Close() 69 if rows.Next() { 70 if scanErr := rows.Scan(&table, &createStr); scanErr != nil { 71 return "", terror.DBErrorAdapt(scanErr, conn.Scope(), terror.ErrDBDriverError) 72 } 73 } else { 74 return "", terror.ErrSyncerDownstreamTableNotFound.Generate(tableID) 75 } 76 77 if err = rows.Close(); err != nil { 78 return "", terror.DBErrorAdapt(rows.Err(), conn.Scope(), terror.ErrDBDriverError) 79 } 80 return createStr, nil 81 } 82 83 func GetParserForConn(tctx *tcontext.Context, dbConn *DBConn) (*parser.Parser, error) { 84 sqlMode, err := getSessionVariable(tctx, dbConn, "sql_mode") 85 if err != nil { 86 return nil, err 87 } 88 return conn.GetParserFromSQLModeStr(sqlMode) 89 } 90 91 //nolint:unparam 92 func getSessionVariable(tctx *tcontext.Context, conn *DBConn, variable string) (value string, err error) { 93 failpoint.Inject("GetSessionVariableFailed", func(val failpoint.Value) { 94 items := strings.Split(val.(string), ",") 95 if len(items) != 2 { 96 log.L().Fatal("failpoint GetSessionVariableFailed's value is invalid", zap.String("val", val.(string))) 97 } 98 variableName := items[0] 99 errCode, err1 := strconv.ParseUint(items[1], 10, 16) 100 if err1 != nil { 101 log.L().Fatal("failpoint GetSessionVariableFailed's value is invalid", zap.String("val", val.(string))) 102 } 103 if variable == variableName { 104 err = tmysql.NewErr(uint16(errCode)) 105 log.L().Warn("GetSessionVariable failed", zap.String("variable", variable), zap.String("failpoint", "GetSessionVariableFailed"), zap.Error(err)) 106 failpoint.Return("", terror.DBErrorAdapt(err, conn.Scope(), terror.ErrDBDriverError)) 107 } 108 }) 109 template := "SHOW VARIABLES LIKE '%s'" 110 query := fmt.Sprintf(template, variable) 111 rows, err := conn.QuerySQL(tctx, nil, query) 112 if err != nil { 113 return "", terror.DBErrorAdapt(err, conn.Scope(), terror.ErrDBDriverError) 114 } 115 defer rows.Close() 116 if rows.Next() { 117 if err = rows.Scan(&variable, &value); err != nil { 118 return "", terror.DBErrorAdapt(err, conn.Scope(), terror.ErrDBDriverError) 119 } 120 } 121 if err = rows.Close(); err != nil { 122 return "", terror.DBErrorAdapt(rows.Err(), conn.Scope(), terror.ErrDBDriverError) 123 } 124 return value, nil 125 } 126 127 // TCPConnWithIOCounter is a wrapper of net.TCPConn with counter that accumulates 128 // the bytes this connection reads/writes. 129 type TCPConnWithIOCounter struct { 130 *net.TCPConn 131 c *atomic.Uint64 132 } 133 134 // NewTCPConnWithIOCounter creates a new TCPConnWithIOCounter. 135 func NewTCPConnWithIOCounter(conn *net.TCPConn, c *atomic.Uint64) net.Conn { 136 return &TCPConnWithIOCounter{ 137 TCPConn: conn, 138 c: c, 139 } 140 } 141 142 func (t *TCPConnWithIOCounter) Read(b []byte) (n int, err error) { 143 n, err = t.TCPConn.Read(b) 144 t.c.Add(uint64(n)) 145 return n, err 146 } 147 148 func (t *TCPConnWithIOCounter) Write(b []byte) (n int, err error) { 149 n, err = t.TCPConn.Write(b) 150 t.c.Add(uint64(n)) 151 return n, err 152 }