-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication.go
337 lines (280 loc) · 9.48 KB
/
replication.go
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
package db
import (
"agent/logger"
"database/sql"
"fmt"
"strings"
"time"
"github.com/jackc/pgtype"
)
type Replication struct {
ServerID *ServerID
// populated if current server is a replica
Replica *Replica
// following replicas
Replicas []*ReplicaClient
}
// populated on a replica
type Replica struct {
ApplicationName string // application_name:backend_start
PrimaryHost string
PrimaryConfigName string // ex GREEN
Status string
Lag pgtype.Interval
MeasuredAt int64
}
type ReplicaClient struct {
ApplicationName string // application_name:backend_start
ClientAddr sql.NullString
ClientHostname sql.NullString
ClientPort sql.NullInt32
BackendStart sql.NullTime
Backendxmin sql.NullInt64
State sql.NullString
WriteLag pgtype.Interval
FlushLag pgtype.Interval
ReplayLag pgtype.Interval
WriteLagBytes float64
FlushLagBytes float64
ReplayLagBytes float64
SyncPriority sql.NullInt32
SyncState sql.NullString
MeasuredAt int64
}
type ReplicationMonitor struct {
replicationChannel chan *Replication
metricsChannel chan []*Metric
postgresClients []*PostgresClient
}
func (m *ReplicationMonitor) Run(postgresClient *PostgresClient) {
replica := m.FindReplica(postgresClient)
replicas := m.FindReplicas(postgresClient)
replication := &Replication{
ServerID: postgresClient.serverID,
Replica: replica,
Replicas: replicas,
}
select {
case m.replicationChannel <- replication:
// sent
default:
logger.Warn("Dropping replication: channel buffer full")
}
m.ReportReplicationLagMetrics(postgresClient.serverID, replica, replicas)
}
func (m *ReplicationMonitor) ReportReplicationLagMetrics(serverID *ServerID, replica *Replica, replicaClients []*ReplicaClient) {
var replicationMetrics []*Metric
// send lag metrics
if replica != nil && isPgtypePresent(replica.Lag.Status) {
// sent if server is a replica
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.local.ms",
microToMilliseconds(replica.Lag.Microseconds),
"replica/standby/"+replica.ApplicationName,
*serverID,
replica.MeasuredAt,
))
}
// sent for all replicas of current server
for _, replicaClient := range replicaClients {
if replicaClient == nil {
continue
}
entity := "replica/standby/" + replicaClient.ApplicationName
// ms lag
if isPgtypePresent(replicaClient.WriteLag.Status) {
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.write.ms",
microToMilliseconds(replicaClient.WriteLag.Microseconds),
entity,
*serverID,
replicaClient.MeasuredAt,
))
}
if isPgtypePresent(replicaClient.FlushLag.Status) {
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.flush.ms",
microToMilliseconds(replicaClient.FlushLag.Microseconds),
entity,
*serverID,
replicaClient.MeasuredAt,
))
}
if isPgtypePresent(replicaClient.ReplayLag.Status) {
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.replay.ms",
microToMilliseconds(replicaClient.ReplayLag.Microseconds),
entity,
*serverID,
replicaClient.MeasuredAt,
))
}
// bytes lag
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.write.bytes",
replicaClient.WriteLagBytes,
entity,
*serverID,
replicaClient.MeasuredAt,
))
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.flush.bytes",
replicaClient.FlushLagBytes,
entity,
*serverID,
replicaClient.MeasuredAt,
))
replicationMetrics = append(replicationMetrics, NewMetric(
"replication.standby.lag.replay.bytes",
replicaClient.ReplayLagBytes,
entity,
*serverID,
replicaClient.MeasuredAt,
))
}
if len(replicationMetrics) > 0 {
select {
case m.metricsChannel <- replicationMetrics:
// sent
default:
logger.Warn("Dropping replication metrics: channel buffer full")
}
}
}
//
// queries
//
func (m *ReplicationMonitor) FindReplicas(postgresClient *PostgresClient) []*ReplicaClient {
var replicaClients []*ReplicaClient
// write, flush and replay lag are only useful for sync replication which is what heroku uses
query := `select application_name, client_addr, client_hostname, client_port,
backend_start, backend_xmin, state, write_lag, flush_lag, replay_lag,
pg_wal_lsn_diff(sent_lsn, write_lsn) as write_lag_bytes,
pg_wal_lsn_diff(write_lsn, flush_lsn) as flush_lag_bytes,
pg_wal_lsn_diff(flush_lsn, replay_lsn) as replay_lag_bytes,
sync_priority, sync_state from pg_stat_replication` + postgresMonitorQueryComment()
rows, err := postgresClient.client.Query(query)
if err != nil {
return []*ReplicaClient{}
}
defer rows.Close()
for rows.Next() {
var replicaClient ReplicaClient
var applicationName sql.NullString
err := rows.Scan(
&applicationName,
&replicaClient.ClientAddr,
&replicaClient.ClientHostname,
&replicaClient.ClientPort,
&replicaClient.BackendStart,
&replicaClient.Backendxmin,
&replicaClient.State,
&replicaClient.WriteLag,
&replicaClient.FlushLag,
&replicaClient.ReplayLag,
&replicaClient.WriteLagBytes,
&replicaClient.FlushLagBytes,
&replicaClient.ReplayLagBytes,
&replicaClient.SyncPriority,
&replicaClient.SyncState,
)
if err != nil {
continue
}
// set application name to application_name:backend_start to uniquely identify the standby
if applicationName.Valid {
if replicaClient.BackendStart.Valid {
replicaClient.ApplicationName = fmt.Sprintf("%s:%d", applicationName.String, replicaClient.BackendStart.Time.Unix())
} else {
replicaClient.ApplicationName = applicationName.String
}
} else {
replicaClient.ApplicationName = ""
}
replicaClient.MeasuredAt = time.Now().UTC().Unix()
replicaClients = append(replicaClients, &replicaClient)
}
return replicaClients
}
func (m *ReplicationMonitor) FindReplica(postgresClient *PostgresClient) *Replica {
var replica Replica
var connInfo string
// NOTE: PG 10 doesn't have sender_host available so we extract it from the conninfo
// PG 11 adds this field https://www.postgresql.org/docs/11/monitoring-stats.html#PG-STAT-WAL-RECEIVER-VIEW
query := `select status, conninfo from pg_stat_wal_receiver` + postgresMonitorQueryComment()
// or use pg_is_in_recovery() to determine if it's a replica
err := postgresClient.client.QueryRow(query).Scan(&replica.Status, &connInfo)
if err != nil || connInfo == "" {
return nil // return nil to not send replica with 0 lag
}
measuredAt := time.Now().UTC().Unix()
replica.MeasuredAt = measuredAt
lag := m.FindReplicationLag(postgresClient)
if lag != nil {
replica.Lag = *lag
}
primaryHost, applicationName := m.FindHostAndApplicationNameForPrimaryFromConnInfo(connInfo)
replica.PrimaryHost = primaryHost
replica.ApplicationName = applicationName
// match server host with client config name
for _, postgresClient := range m.postgresClients {
if postgresClient.host == replica.PrimaryHost {
replica.PrimaryConfigName = postgresClient.serverID.ConfigName
break
}
}
// get backend_start from pg_stat_activity for walreceiver process
walReceiverBackendStart := m.FindWalReceiverBackendStart(postgresClient)
// use backend_start with application name for unique application id
if walReceiverBackendStart.Valid {
replica.ApplicationName = fmt.Sprintf("%s:%d", replica.ApplicationName, walReceiverBackendStart.Time.Unix())
}
return &replica
}
func (m *ReplicationMonitor) FindWalReceiverBackendStart(postgresClient *PostgresClient) sql.NullTime {
query := "select backend_start from pg_stat_activity where backend_type = 'walreceiver'" + postgresMonitorQueryComment()
var backendStart sql.NullTime
err := postgresClient.client.QueryRow(query).Scan(&backendStart)
if err != nil {
return backendStart
}
return backendStart
}
// this lag is only useful on active dbs since inactive dbs will show an ever increasing lag
// because the primary is not writing/updating
func (m *ReplicationMonitor) FindReplicationLag(postgresClient *PostgresClient) *pgtype.Interval {
var lag pgtype.Interval
// "select extract(epoch from coalesce(now() - pg_last_xact_replay_timestamp(), 0 * INTERVAL '1 minute'))::int AS lag" - as seconds?
err := postgresClient.client.QueryRow("select now() - pg_last_xact_replay_timestamp() as lag" + postgresMonitorQueryComment()).Scan(&lag)
if err != nil {
return nil
}
return &lag
}
func (m *ReplicationMonitor) FindHostAndApplicationNameForPrimaryFromConnInfo(connInfo string) (string, string) {
// example connInfo => user=postgres passfile=/etc/postgresql/recovery_pgpass channel_binding=prefer dbname=replication host=ec2-123-456-789.compute-1.amazonaws.com port=5432 application_name=follower fallback_application_name=walreceiver sslmode=prefer sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=prefer krbsrvname=postgres target_session_attrs=any
host := ""
applicationName := ""
if connInfo == "" {
return host, applicationName
}
for _, info := range strings.Split(connInfo, " ") {
values := strings.Split(info, "=")
if len(values) > 1 {
key := values[0]
value := values[1]
if key == "host" {
host = value
} else if key == "application_name" {
applicationName = value
}
}
}
return host, applicationName
}
func isPgtypePresent(s pgtype.Status) bool {
return s == pgtype.Present // not null
}
func microToMilliseconds(microseconds int64) float64 {
return float64(microseconds / 1000)
}