MCPcopy
hub / github.com/github/gh-ost / GetMasterKeyFromSlaveStatus

Function GetMasterKeyFromSlaveStatus

go/mysql/utils.go:88–130  ·  view source on GitHub ↗
(dbVersion string, connectionConfig *ConnectionConfig)

Source from the content-addressed store, hash-verified

86}
87
88func GetMasterKeyFromSlaveStatus(dbVersion string, connectionConfig *ConnectionConfig) (masterKey *InstanceKey, err error) {
89 currentUri := connectionConfig.GetDBUri("information_schema")
90 // This function is only called once, okay to not have a cached connection pool
91 db, err := gosql.Open("mysql", currentUri)
92 if err != nil {
93 return nil, err
94 }
95 defer db.Close()
96
97 showReplicaStatusQuery := fmt.Sprintf("show %s", ReplicaTermFor(dbVersion, `slave status`))
98 err = sqlutils.QueryRowsMap(db, showReplicaStatusQuery, func(rowMap sqlutils.RowMap) error {
99 // We wish to recognize the case where the topology's master actually has replication configuration.
100 // This can happen when a DBA issues a `RESET SLAVE` instead of `RESET SLAVE ALL`.
101
102 // An empty log file indicates this is a master:
103 if rowMap.GetString(ReplicaTermFor(dbVersion, "Master_Log_File")) == "" {
104 return nil
105 }
106
107 ioRunningTerm := ReplicaTermFor(dbVersion, "Slave_IO_Running")
108 sqlRunningTerm := ReplicaTermFor(dbVersion, "Slave_SQL_Running")
109 slaveIORunning := rowMap.GetString(ioRunningTerm)
110 slaveSQLRunning := rowMap.GetString(sqlRunningTerm)
111
112 if slaveIORunning != "Yes" || slaveSQLRunning != "Yes" {
113 return fmt.Errorf("replication on %+v is broken: %s: %s, %s: %s. Please make sure replication runs before using gh-ost",
114 connectionConfig.Key,
115 ioRunningTerm,
116 slaveIORunning,
117 sqlRunningTerm,
118 slaveSQLRunning,
119 )
120 }
121
122 masterKey = &InstanceKey{
123 Hostname: rowMap.GetString(ReplicaTermFor(dbVersion, "Master_Host")),
124 Port: rowMap.GetInt(ReplicaTermFor(dbVersion, "Master_Port")),
125 }
126 return nil
127 })
128
129 return masterKey, err
130}
131
132func GetMasterConnectionConfigSafe(dbVersion string, connectionConfig *ConnectionConfig, visitedKeys *InstanceKeyMap, allowMasterMaster bool) (masterConfig *ConnectionConfig, err error) {
133 log.Debugf("Looking for %s on %+v", ReplicaTermFor(dbVersion, "master"), connectionConfig.Key)

Callers 2

restartReplicationMethod · 0.92

Calls 4

ReplicaTermForFunction · 0.85
GetDBUriMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…