2016-05-01 18:36:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-05-01 18:36:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
gosql "database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2016-10-20 09:29:30 +00:00
|
|
|
"github.com/github/gh-ost/go/sql"
|
|
|
|
|
2016-05-01 18:36:36 +00:00
|
|
|
"github.com/outbrain/golib/log"
|
|
|
|
"github.com/outbrain/golib/sqlutils"
|
|
|
|
)
|
|
|
|
|
2016-07-27 07:59:46 +00:00
|
|
|
type ReplicationLagResult struct {
|
|
|
|
Key InstanceKey
|
|
|
|
Lag time.Duration
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2016-05-01 18:36:36 +00:00
|
|
|
// GetReplicationLag returns replication lag for a given connection config; either by explicit query
|
|
|
|
// or via SHOW SLAVE STATUS
|
|
|
|
func GetReplicationLag(connectionConfig *ConnectionConfig, replicationLagQuery string) (replicationLag time.Duration, err error) {
|
|
|
|
dbUri := connectionConfig.GetDBUri("information_schema")
|
|
|
|
var db *gosql.DB
|
|
|
|
if db, _, err = sqlutils.GetDB(dbUri); err != nil {
|
|
|
|
return replicationLag, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if replicationLagQuery != "" {
|
|
|
|
var floatLag float64
|
|
|
|
err = db.QueryRow(replicationLagQuery).Scan(&floatLag)
|
|
|
|
return time.Duration(int64(floatLag*1000)) * time.Millisecond, err
|
|
|
|
}
|
|
|
|
// No explicit replication lag query.
|
|
|
|
err = sqlutils.QueryRowsMap(db, `show slave status`, func(m sqlutils.RowMap) error {
|
|
|
|
secondsBehindMaster := m.GetNullInt64("Seconds_Behind_Master")
|
|
|
|
if !secondsBehindMaster.Valid {
|
2016-07-27 07:59:46 +00:00
|
|
|
return fmt.Errorf("replication not running")
|
2016-05-01 18:36:36 +00:00
|
|
|
}
|
|
|
|
replicationLag = time.Duration(secondsBehindMaster.Int64) * time.Second
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return replicationLag, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxReplicationLag concurrently checks for replication lag on given list of instance keys,
|
|
|
|
// each via GetReplicationLag
|
2016-07-27 07:59:46 +00:00
|
|
|
func GetMaxReplicationLag(baseConnectionConfig *ConnectionConfig, instanceKeyMap *InstanceKeyMap, replicationLagQuery string) (result *ReplicationLagResult) {
|
|
|
|
result = &ReplicationLagResult{Lag: 0}
|
2016-05-01 18:36:36 +00:00
|
|
|
if instanceKeyMap.Len() == 0 {
|
2016-07-27 07:59:46 +00:00
|
|
|
return result
|
2016-05-01 18:36:36 +00:00
|
|
|
}
|
2016-07-27 07:59:46 +00:00
|
|
|
lagResults := make(chan *ReplicationLagResult, instanceKeyMap.Len())
|
2016-05-01 18:36:36 +00:00
|
|
|
for key := range *instanceKeyMap {
|
|
|
|
connectionConfig := baseConnectionConfig.Duplicate()
|
|
|
|
connectionConfig.Key = key
|
2016-07-27 07:59:46 +00:00
|
|
|
result := &ReplicationLagResult{Key: connectionConfig.Key}
|
2016-05-01 18:36:36 +00:00
|
|
|
go func() {
|
2016-07-27 07:59:46 +00:00
|
|
|
result.Lag, result.Err = GetReplicationLag(connectionConfig, replicationLagQuery)
|
|
|
|
lagResults <- result
|
2016-05-01 18:36:36 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
for range *instanceKeyMap {
|
2016-07-27 07:59:46 +00:00
|
|
|
lagResult := <-lagResults
|
|
|
|
if lagResult.Err != nil {
|
|
|
|
result = lagResult
|
|
|
|
} else if lagResult.Lag.Nanoseconds() > result.Lag.Nanoseconds() {
|
|
|
|
result = lagResult
|
2016-05-01 18:36:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 07:59:46 +00:00
|
|
|
return result
|
2016-05-01 18:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetMasterKeyFromSlaveStatus(connectionConfig *ConnectionConfig) (masterKey *InstanceKey, err error) {
|
|
|
|
currentUri := connectionConfig.GetDBUri("information_schema")
|
|
|
|
db, _, err := sqlutils.GetDB(currentUri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = sqlutils.QueryRowsMap(db, `show slave status`, func(rowMap sqlutils.RowMap) error {
|
|
|
|
masterKey = &InstanceKey{
|
|
|
|
Hostname: rowMap.GetString("Master_Host"),
|
|
|
|
Port: rowMap.GetInt("Master_Port"),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return masterKey, err
|
|
|
|
}
|
|
|
|
|
2016-06-22 08:38:13 +00:00
|
|
|
func GetMasterConnectionConfigSafe(connectionConfig *ConnectionConfig, visitedKeys *InstanceKeyMap, allowMasterMaster bool) (masterConfig *ConnectionConfig, err error) {
|
2016-05-01 18:36:36 +00:00
|
|
|
log.Debugf("Looking for master on %+v", connectionConfig.Key)
|
|
|
|
|
|
|
|
masterKey, err := GetMasterKeyFromSlaveStatus(connectionConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if masterKey == nil {
|
|
|
|
return connectionConfig, nil
|
|
|
|
}
|
|
|
|
if !masterKey.IsValid() {
|
|
|
|
return connectionConfig, nil
|
|
|
|
}
|
|
|
|
masterConfig = connectionConfig.Duplicate()
|
|
|
|
masterConfig.Key = *masterKey
|
|
|
|
|
|
|
|
log.Debugf("Master of %+v is %+v", connectionConfig.Key, masterConfig.Key)
|
|
|
|
if visitedKeys.HasKey(masterConfig.Key) {
|
2016-06-22 08:38:13 +00:00
|
|
|
if allowMasterMaster {
|
|
|
|
return connectionConfig, nil
|
|
|
|
}
|
2016-05-01 18:36:36 +00:00
|
|
|
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)
|
2016-06-22 08:38:13 +00:00
|
|
|
return GetMasterConnectionConfigSafe(masterConfig, visitedKeys, allowMasterMaster)
|
2016-05-01 18:36:36 +00:00
|
|
|
}
|
2016-05-16 09:03:15 +00:00
|
|
|
|
2016-05-19 13:11:36 +00:00
|
|
|
func GetReplicationBinlogCoordinates(db *gosql.DB) (readBinlogCoordinates *BinlogCoordinates, executeBinlogCoordinates *BinlogCoordinates, err error) {
|
2016-05-16 09:03:15 +00:00
|
|
|
err = sqlutils.QueryRowsMap(db, `show slave status`, func(m sqlutils.RowMap) error {
|
|
|
|
readBinlogCoordinates = &BinlogCoordinates{
|
|
|
|
LogFile: m.GetString("Master_Log_File"),
|
|
|
|
LogPos: m.GetInt64("Read_Master_Log_Pos"),
|
|
|
|
}
|
2016-05-19 13:11:36 +00:00
|
|
|
executeBinlogCoordinates = &BinlogCoordinates{
|
|
|
|
LogFile: m.GetString("Relay_Master_Log_File"),
|
|
|
|
LogPos: m.GetInt64("Exec_Master_Log_Pos"),
|
|
|
|
}
|
2016-05-16 09:03:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
2016-05-19 13:11:36 +00:00
|
|
|
return readBinlogCoordinates, executeBinlogCoordinates, err
|
2016-05-16 09:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSelfBinlogCoordinates(db *gosql.DB) (selfBinlogCoordinates *BinlogCoordinates, err error) {
|
|
|
|
err = sqlutils.QueryRowsMap(db, `show master status`, func(m sqlutils.RowMap) error {
|
|
|
|
selfBinlogCoordinates = &BinlogCoordinates{
|
|
|
|
LogFile: m.GetString("File"),
|
|
|
|
LogPos: m.GetInt64("Position"),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return selfBinlogCoordinates, err
|
|
|
|
}
|
2016-06-19 15:55:37 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2016-10-20 09:29:30 +00:00
|
|
|
|
|
|
|
// GetTableColumns reads column list from given table
|
|
|
|
func GetTableColumns(db *gosql.DB, databaseName, tableName string) (*sql.ColumnList, error) {
|
|
|
|
query := fmt.Sprintf(`
|
|
|
|
show columns from %s.%s
|
|
|
|
`,
|
|
|
|
sql.EscapeName(databaseName),
|
|
|
|
sql.EscapeName(tableName),
|
|
|
|
)
|
|
|
|
columnNames := []string{}
|
|
|
|
err := sqlutils.QueryRowsMap(db, query, func(rowMap sqlutils.RowMap) error {
|
|
|
|
columnNames = append(columnNames, rowMap.GetString("Field"))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(columnNames) == 0 {
|
|
|
|
return nil, log.Errorf("Found 0 columns on %s.%s. Bailing out",
|
|
|
|
sql.EscapeName(databaseName),
|
|
|
|
sql.EscapeName(tableName),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return sql.NewColumnList(columnNames), nil
|
|
|
|
}
|