-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathutils.go
More file actions
369 lines (330 loc) · 13 KB
/
Copy pathutils.go
File metadata and controls
369 lines (330 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright 2022 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package mysql
import (
"context"
gosql "database/sql"
"database/sql/driver"
"fmt"
"strings"
"sync"
"time"
"github.com/github/gh-ost/go/sql"
gomysql "github.com/go-sql-driver/mysql"
"github.com/openark/golib/log"
"github.com/openark/golib/sqlutils"
)
const (
MaxTableNameLength = 64
MaxDBPoolConnections = 3
)
type ReplicationLagResult struct {
Key InstanceKey
Lag time.Duration
Err error
}
type Trigger struct {
Name string
Event string
Statement string
Timing string
}
func NewNoReplicationLagResult() *ReplicationLagResult {
return &ReplicationLagResult{Lag: 0, Err: nil}
}
func (rlg *ReplicationLagResult) HasLag() bool {
return rlg.Lag > 0
}
// knownDBs is a DB cache by uri
var knownDBs map[string]*gosql.DB = make(map[string]*gosql.DB)
var knownDBsMutex = &sync.Mutex{}
// initConnector wraps a driver.Connector to run a fixed set of statements on
// every newly established connection (e.g. setting the transaction isolation
// level), which the DSN-param mechanism can't express portably.
type initConnector struct {
driver.Connector
statements []string
}
func (c *initConnector) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.Connector.Connect(ctx)
if err != nil {
return nil, err
}
execer, ok := conn.(driver.ExecerContext)
if !ok {
conn.Close()
return nil, fmt.Errorf("mysql: driver connection does not implement driver.ExecerContext")
}
for _, stmt := range c.statements {
if _, err := execer.ExecContext(ctx, stmt, nil); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
}
// OpenDB opens a MySQL connection pool for the given DSN. A transaction_isolation
// param is applied via the SQL-standard "SET SESSION TRANSACTION ISOLATION LEVEL"
// statement on each new connection rather than being passed to the driver as a
// system variable:
// - "transaction_isolation" doesn't exist on MariaDB < 11.1, while
// - "tx_isolation" doesn't exist on MySQL 8.0+ anymore, so no single variable name is portable.
// The standard statement is accepted by every supported MySQL and MariaDB version.
func OpenDB(mysql_uri string) (*gosql.DB, error) {
cfg, err := gomysql.ParseDSN(mysql_uri)
if err != nil {
return nil, err
}
var statements []string
if iso := strings.Trim(cfg.Params["transaction_isolation"], `"`); iso != "" {
delete(cfg.Params, "transaction_isolation")
statements = append(statements, "SET SESSION TRANSACTION ISOLATION LEVEL "+strings.ReplaceAll(iso, "-", " "))
}
connector, err := gomysql.NewConnector(cfg)
if err != nil {
return nil, err
}
if len(statements) > 0 {
connector = &initConnector{Connector: connector, statements: statements}
}
return gosql.OpenDB(connector), nil
}
func GetDB(migrationUuid string, mysql_uri string) (db *gosql.DB, exists bool, err error) {
cacheKey := migrationUuid + ":" + mysql_uri
knownDBsMutex.Lock()
defer knownDBsMutex.Unlock()
if db, exists = knownDBs[cacheKey]; !exists {
db, err = OpenDB(mysql_uri)
if err != nil {
return nil, false, err
}
db.SetMaxOpenConns(MaxDBPoolConnections)
db.SetMaxIdleConns(MaxDBPoolConnections)
knownDBs[cacheKey] = db
}
return db, exists, nil
}
// GetReplicationLagFromSlaveStatus returns replication lag for a given db; via SHOW SLAVE STATUS
func GetReplicationLagFromSlaveStatus(dbVersion string, informationSchemaDb *gosql.DB) (replicationLag time.Duration, err error) {
showReplicaStatusQuery := fmt.Sprintf("show %s", ReplicaTermFor(dbVersion, `slave status`))
err = sqlutils.QueryRowsMap(informationSchemaDb, showReplicaStatusQuery, func(m sqlutils.RowMap) error {
ioRunningTerm := ReplicaTermFor(dbVersion, "Slave_IO_Running")
sqlRunningTerm := ReplicaTermFor(dbVersion, "Slave_SQL_Running")
slaveIORunning := m.GetString(ioRunningTerm)
slaveSQLRunning := m.GetString(sqlRunningTerm)
secondsBehindMaster := m.GetNullInt64(ReplicaTermFor(dbVersion, "Seconds_Behind_Master"))
if !secondsBehindMaster.Valid {
return fmt.Errorf("replication not running; %s=%+v, %s=%+v", ioRunningTerm, slaveIORunning, sqlRunningTerm, slaveSQLRunning)
}
replicationLag = time.Duration(secondsBehindMaster.Int64) * time.Second
return nil
})
return replicationLag, err
}
func GetMasterKeyFromSlaveStatus(dbVersion string, connectionConfig *ConnectionConfig) (masterKey *InstanceKey, err error) {
return getMasterKeyFromSlaveStatus(dbVersion, connectionConfig, OpenDB)
}
func getMasterKeyFromSlaveStatus(dbVersion string, connectionConfig *ConnectionConfig, openDB func(string) (*gosql.DB, error)) (masterKey *InstanceKey, err error) {
currentUri := connectionConfig.GetDBUri("information_schema")
// This function is only called once, okay to not have a cached connection pool
db, err := openDB(currentUri)
if err != nil {
return nil, err
}
defer db.Close()
if err := db.QueryRow(`select @@global.version`).Scan(&dbVersion); err != nil {
return nil, err
}
showReplicaStatusQuery := fmt.Sprintf("show %s", ReplicaTermFor(dbVersion, `slave status`))
err = sqlutils.QueryRowsMap(db, showReplicaStatusQuery, func(rowMap sqlutils.RowMap) error {
// We wish to recognize the case where the topology's master actually has replication configuration.
// This can happen when a DBA issues a `RESET SLAVE` instead of `RESET SLAVE ALL`.
// An empty log file indicates this is a master:
if rowMap.GetString(ReplicaTermFor(dbVersion, "Master_Log_File")) == "" {
return nil
}
ioRunningTerm := ReplicaTermFor(dbVersion, "Slave_IO_Running")
sqlRunningTerm := ReplicaTermFor(dbVersion, "Slave_SQL_Running")
slaveIORunning := rowMap.GetString(ioRunningTerm)
slaveSQLRunning := rowMap.GetString(sqlRunningTerm)
if slaveIORunning != "Yes" || slaveSQLRunning != "Yes" {
return fmt.Errorf("replication on %+v is broken: %s: %s, %s: %s. Please make sure replication runs before using gh-ost",
connectionConfig.Key,
ioRunningTerm,
slaveIORunning,
sqlRunningTerm,
slaveSQLRunning,
)
}
masterKey = &InstanceKey{
Hostname: rowMap.GetString(ReplicaTermFor(dbVersion, "Master_Host")),
Port: rowMap.GetInt(ReplicaTermFor(dbVersion, "Master_Port")),
}
return nil
})
return masterKey, err
}
func GetMasterConnectionConfigSafe(dbVersion string, connectionConfig *ConnectionConfig, visitedKeys *InstanceKeyMap, allowMasterMaster bool) (masterConfig *ConnectionConfig, err error) {
return getMasterConnectionConfigSafe(dbVersion, connectionConfig, visitedKeys, allowMasterMaster, OpenDB)
}
func getMasterConnectionConfigSafe(dbVersion string, connectionConfig *ConnectionConfig, visitedKeys *InstanceKeyMap, allowMasterMaster bool, openDB func(string) (*gosql.DB, error)) (masterConfig *ConnectionConfig, err error) {
log.Debugf("Looking for %s on %+v", ReplicaTermFor(dbVersion, "master"), connectionConfig.Key)
masterKey, err := getMasterKeyFromSlaveStatus(dbVersion, connectionConfig, openDB)
if err != nil {
return nil, err
}
if masterKey == nil {
return connectionConfig, nil
}
if !masterKey.IsValid() {
return connectionConfig, nil
}
masterConfig = connectionConfig.DuplicateCredentials(*masterKey)
if err := masterConfig.RegisterTLSConfig(); err != nil {
return nil, err
}
log.Debugf("%s of %+v is %+v", ReplicaTermFor(dbVersion, "master"), connectionConfig.Key, masterConfig.Key)
if visitedKeys.HasKey(masterConfig.Key) {
if allowMasterMaster {
return connectionConfig, nil
}
return nil, fmt.Errorf("there seems to be a master-master setup at %+v. This is unsupported. Bailing out", masterConfig.Key)
}
visitedKeys.AddKey(masterConfig.Key)
return getMasterConnectionConfigSafe(dbVersion, masterConfig, visitedKeys, allowMasterMaster, openDB)
}
func GetReplicationBinlogCoordinates(dbVersion string, db *gosql.DB, gtid bool) (readBinlogCoordinates, executeBinlogCoordinates BinlogCoordinates, err error) {
if gtid && IsMariaDB(dbVersion) {
return getMariaDBReplicationGTIDCoordinates(db)
}
showReplicaStatusQuery := fmt.Sprintf("show %s", ReplicaTermFor(dbVersion, `slave status`))
err = sqlutils.QueryRowsMap(db, showReplicaStatusQuery, func(m sqlutils.RowMap) error {
if gtid {
executeBinlogCoordinates, err = NewGTIDBinlogCoordinates(MySQLFlavor, m.GetString("Executed_Gtid_Set"))
if err != nil {
return err
}
readBinlogCoordinates, err = NewGTIDBinlogCoordinates(MySQLFlavor, m.GetString("Retrieved_Gtid_Set"))
if err != nil {
return err
}
} else {
readBinlogCoordinates = NewFileBinlogCoordinates(
m.GetString(ReplicaTermFor(dbVersion, "Master_Log_File")),
m.GetInt64(ReplicaTermFor(dbVersion, "Read_Master_Log_Pos")),
)
executeBinlogCoordinates = NewFileBinlogCoordinates(
m.GetString(ReplicaTermFor(dbVersion, "Relay_Master_Log_File")),
m.GetInt64(ReplicaTermFor(dbVersion, "Exec_Master_Log_Pos")),
)
}
return nil
})
return readBinlogCoordinates, executeBinlogCoordinates, err
}
func GetSelfBinlogCoordinates(dbVersion string, db *gosql.DB, gtid bool) (selfBinlogCoordinates BinlogCoordinates, err error) {
if gtid && IsMariaDB(dbVersion) {
// MariaDB does not expose a GTID column in SHOW MASTER STATUS; the
// executed GTID position of this server's own binary log is in
// @@global.gtid_binlog_pos.
var gtidBinlogPos string
if err = db.QueryRow(`select @@global.gtid_binlog_pos`).Scan(>idBinlogPos); err != nil {
return nil, err
}
return NewGTIDBinlogCoordinates(MariaDBFlavor, gtidBinlogPos)
}
binaryLogStatusTerm := ReplicaTermFor(dbVersion, "master status")
err = sqlutils.QueryRowsMap(db, fmt.Sprintf("show %s", binaryLogStatusTerm), func(m sqlutils.RowMap) error {
if gtid {
selfBinlogCoordinates, err = NewGTIDBinlogCoordinates(MySQLFlavor, m.GetString("Executed_Gtid_Set"))
} else {
selfBinlogCoordinates = NewFileBinlogCoordinates(
m.GetString("File"),
m.GetInt64("Position"),
)
}
return nil
})
return selfBinlogCoordinates, err
}
// getMariaDBReplicationGTIDCoordinates reports the IO/SQL thread GTID positions
// of a MariaDB replica. MariaDB has no Executed_Gtid_Set/Retrieved_Gtid_Set
// columns: the IO thread position is in SHOW SLAVE STATUS's Gtid_IO_Pos, and the
// applied position is in @@global.gtid_slave_pos.
func getMariaDBReplicationGTIDCoordinates(db *gosql.DB) (readBinlogCoordinates, executeBinlogCoordinates BinlogCoordinates, err error) {
err = sqlutils.QueryRowsMap(db, "show slave status", func(m sqlutils.RowMap) error {
readBinlogCoordinates, err = NewGTIDBinlogCoordinates(MariaDBFlavor, m.GetString("Gtid_IO_Pos"))
return err
})
if err != nil {
return readBinlogCoordinates, executeBinlogCoordinates, err
}
var gtidSlavePos string
if err = db.QueryRow(`select @@global.gtid_slave_pos`).Scan(>idSlavePos); err != nil {
return readBinlogCoordinates, executeBinlogCoordinates, err
}
executeBinlogCoordinates, err = NewGTIDBinlogCoordinates(MariaDBFlavor, gtidSlavePos)
return readBinlogCoordinates, executeBinlogCoordinates, err
}
// GetInstanceKey reads hostname and port on given DB
func GetInstanceKey(db *gosql.DB) (instanceKey *InstanceKey, err error) {
instanceKey = &InstanceKey{}
err = db.QueryRow(`select @@global.hostname, @@global.port`).Scan(&instanceKey.Hostname, &instanceKey.Port)
return instanceKey, err
}
// GetTableColumns reads column list from given table
func GetTableColumns(db *gosql.DB, databaseName, tableName string) (*sql.ColumnList, *sql.ColumnList, error) {
query := fmt.Sprintf(`
show columns from %s.%s
`,
sql.EscapeName(databaseName),
sql.EscapeName(tableName),
)
columnNames := []string{}
virtualColumnNames := []string{}
err := sqlutils.QueryRowsMap(db, query, func(rowMap sqlutils.RowMap) error {
columnName := rowMap.GetString("Field")
columnNames = append(columnNames, columnName)
if strings.Contains(rowMap.GetString("Extra"), " GENERATED") {
log.Debugf("%s is a generated column", columnName)
virtualColumnNames = append(virtualColumnNames, columnName)
}
return nil
})
if err != nil {
return nil, nil, err
}
if len(columnNames) == 0 {
return nil, nil, log.Errorf("found 0 columns on %s.%s. Bailing out",
sql.EscapeName(databaseName),
sql.EscapeName(tableName),
)
}
return sql.NewColumnList(columnNames), sql.NewColumnList(virtualColumnNames), nil
}
// Kill executes a KILL QUERY by connection id
func Kill(db *gosql.DB, connectionID string) error {
_, err := db.Exec(`KILL QUERY %s`, connectionID)
return err
}
// GetTriggers reads trigger list from given table
func GetTriggers(db *gosql.DB, databaseName, tableName string) (triggers []Trigger, err error) {
query := `select trigger_name as name, event_manipulation as event, action_statement as statement, action_timing as timing
from information_schema.triggers
where trigger_schema = ? and event_object_table = ?`
err = sqlutils.QueryRowsMap(db, query, func(rowMap sqlutils.RowMap) error {
triggers = append(triggers, Trigger{
Name: rowMap.GetString("name"),
Event: rowMap.GetString("event"),
Statement: rowMap.GetString("statement"),
Timing: rowMap.GetString("timing"),
})
return nil
}, databaseName, tableName)
if err != nil {
return nil, err
}
return triggers, nil
}