@@ -14,6 +14,12 @@ import (
14
14
"github.com/outbrain/golib/sqlutils"
15
15
)
16
16
17
+ type ReplicationLagResult struct {
18
+ Key InstanceKey
19
+ Lag time.Duration
20
+ Err error
21
+ }
22
+
17
23
// GetReplicationLag returns replication lag for a given connection config; either by explicit query
18
24
// or via SHOW SLAVE STATUS
19
25
func GetReplicationLag (connectionConfig * ConnectionConfig , replicationLagQuery string ) (replicationLag time.Duration , err error ) {
@@ -32,7 +38,7 @@ func GetReplicationLag(connectionConfig *ConnectionConfig, replicationLagQuery s
32
38
err = sqlutils .QueryRowsMap (db , `show slave status` , func (m sqlutils.RowMap ) error {
33
39
secondsBehindMaster := m .GetNullInt64 ("Seconds_Behind_Master" )
34
40
if ! secondsBehindMaster .Valid {
35
- return fmt .Errorf ("Replication not running on %+v" , connectionConfig . Key )
41
+ return fmt .Errorf ("replication not running" )
36
42
}
37
43
replicationLag = time .Duration (secondsBehindMaster .Int64 ) * time .Second
38
44
return nil
@@ -42,30 +48,30 @@ func GetReplicationLag(connectionConfig *ConnectionConfig, replicationLagQuery s
42
48
43
49
// GetMaxReplicationLag concurrently checks for replication lag on given list of instance keys,
44
50
// each via GetReplicationLag
45
- func GetMaxReplicationLag (baseConnectionConfig * ConnectionConfig , instanceKeyMap * InstanceKeyMap , replicationLagQuery string ) (replicationLag time.Duration , err error ) {
51
+ func GetMaxReplicationLag (baseConnectionConfig * ConnectionConfig , instanceKeyMap * InstanceKeyMap , replicationLagQuery string ) (result * ReplicationLagResult ) {
52
+ result = & ReplicationLagResult {Lag : 0 }
46
53
if instanceKeyMap .Len () == 0 {
47
- return 0 , nil
54
+ return result
48
55
}
49
- lagsChan := make (chan time.Duration , instanceKeyMap .Len ())
50
- errorsChan := make (chan error , instanceKeyMap .Len ())
56
+ lagResults := make (chan * ReplicationLagResult , instanceKeyMap .Len ())
51
57
for key := range * instanceKeyMap {
52
58
connectionConfig := baseConnectionConfig .Duplicate ()
53
59
connectionConfig .Key = key
60
+ result := & ReplicationLagResult {Key : connectionConfig .Key }
54
61
go func () {
55
- lag , err := GetReplicationLag (connectionConfig , replicationLagQuery )
56
- lagsChan <- lag
57
- errorsChan <- err
62
+ result .Lag , result .Err = GetReplicationLag (connectionConfig , replicationLagQuery )
63
+ lagResults <- result
58
64
}()
59
65
}
60
66
for range * instanceKeyMap {
61
- if lagError := <- errorsChan ; lagError != nil {
62
- err = lagError
63
- }
64
- if lag := <- lagsChan ; lag . Nanoseconds () > replicationLag .Nanoseconds () {
65
- replicationLag = lag
67
+ lagResult := <- lagResults
68
+ if lagResult . Err != nil {
69
+ result = lagResult
70
+ } else if lagResult . Lag . Nanoseconds () > result . Lag .Nanoseconds () {
71
+ result = lagResult
66
72
}
67
73
}
68
- return replicationLag , err
74
+ return result
69
75
}
70
76
71
77
func GetMasterKeyFromSlaveStatus (connectionConfig * ConnectionConfig ) (masterKey * InstanceKey , err error ) {
0 commit comments