-
Notifications
You must be signed in to change notification settings - Fork 6
/
canary.go
216 lines (179 loc) · 5.15 KB
/
canary.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
package canary
import (
"time"
"code.cloudfoundry.org/lager"
"database/sql"
"errors"
"replication-canary/models"
)
//go:generate counterfeiter . SQLClient
type SQLClient interface {
Setup(*sql.DB) error
Write(*sql.DB, time.Time) error
Check(*sql.DB, time.Time) (bool, error)
Cleanup(*sql.DB) error
}
//go:generate counterfeiter . Healthchecker
type Healthchecker interface {
Healthy(*models.NamedConnection) (bool, error)
}
type Canary struct {
sqlClient SQLClient
healthchecker Healthchecker
writeReadDelay time.Duration
logger lager.Logger
}
func NewCanary(
sqlClient SQLClient,
healthchecker Healthchecker,
writeReadDelay time.Duration,
logger lager.Logger,
) *Canary {
return &Canary{
sqlClient: sqlClient,
healthchecker: healthchecker,
writeReadDelay: writeReadDelay,
logger: logger,
}
}
func (c *Canary) Setup(writeConn *models.NamedConnection) error {
return c.sqlClient.Setup(writeConn.Connection)
}
// Chirp returns (true,nil) when we know replication succeeded
// Chirp returns (false,nil) when we know know replication failed
// Chirp returns (false,error) when we cannot determine if replication succeeded
// Chirp will never return (true,error)
func (c *Canary) Chirp(
conns []*models.NamedConnection,
writeConn *models.NamedConnection,
timestamp time.Time,
) (bool, error) {
c.logger.Debug("Beginning chirp", lager.Data{"timestamp": timestamp})
err := c.checkWriteConn(timestamp, writeConn)
if err != nil {
return false, err
}
err = c.Write(timestamp, writeConn)
if err != nil {
return false, err
}
time.Sleep(c.writeReadDelay)
replicationSuccess, err := c.Read(conns, timestamp)
cleanupErr := c.cleanup(timestamp, writeConn)
if err != nil {
return false, err
}
if cleanupErr != nil {
return false, cleanupErr
}
return replicationSuccess, nil
}
func (c *Canary) checkWriteConn(timestamp time.Time, writeConn *models.NamedConnection) error {
c.logger.Debug(
"Canary checking galera status for write connection",
lager.Data{"timestamp": timestamp, "node": writeConn.Name},
)
writeHealthy, err := c.healthchecker.Healthy(writeConn)
if err != nil {
c.logger.Debug("Canary checking galera status errored", lager.Data{
"node": writeConn.Name,
"errorMessage": err.Error(),
})
return err
}
if !writeHealthy {
c.logger.Debug(
"Canary found write connection's galera was unhealthy",
lager.Data{"node": writeConn.Name},
)
return errors.New("write connection's galera is unhealthy")
}
return nil
}
// this function is exported for use in creating a write binary for exploring
func (c *Canary) Write(timestamp time.Time, writeConn *models.NamedConnection) error {
c.logger.Debug(
"Canary beginning write",
lager.Data{"timestamp": timestamp, "node": writeConn.Name},
)
err := c.sqlClient.Write(writeConn.Connection, timestamp)
if err != nil {
return err
}
c.logger.Debug(
"Canary completed write",
lager.Data{"timestamp": timestamp, "node": writeConn.Name},
)
return nil
}
// this function is exported for use in creating a write binary for exploring
// already tested in the Chirp functions test
func (c *Canary) Read(conns []*models.NamedConnection, timestamp time.Time) (bool, error) {
c.logger.Debug("Canary beginning reads", lager.Data{"timestamp": timestamp})
// if there are more than 0 (false,nil) then we return 'false'
var errs []error
for _, conn := range conns {
c.logger.Debug(
"Canary checking galera status for read connection",
lager.Data{"timestamp": timestamp, "node": conn.Name},
)
connHealthy, err := c.healthchecker.Healthy(conn)
if err != nil {
c.logger.Debug("Canary checking galera status errored", lager.Data{
"node": conn.Name,
"errorMessage": err.Error(),
})
errs = append(errs, err)
continue
}
if !connHealthy {
c.logger.Debug(
"Canary found read connection's galera was unhealthy",
lager.Data{"node": conn.Name},
)
continue
}
c.logger.Debug(
"Canary beginning check",
lager.Data{"timestamp": timestamp, "node": conn.Name},
)
ok, err := c.sqlClient.Check(conn.Connection, timestamp)
if !ok && err == nil {
c.logger.Debug(
"Canary detected replication failure",
lager.Data{"timestamp": timestamp, "node": conn.Name},
)
return false, nil
}
c.logger.Debug(
"Canary finished check",
lager.Data{"timestamp": timestamp, "node": conn.Name},
)
if err != nil {
c.logger.Debug("Canary errored during read", lager.Data{
"timestamp": timestamp,
"errorMessage": err.Error(),
"node": conn.Name,
})
errs = append(errs, err)
}
}
c.logger.Debug("Canary completed reads", lager.Data{"timestamp": timestamp})
// If any of the Checks is non-deterministic, we are non-deterministic
if len(errs) > 0 {
return false, errs[0]
}
return true, nil
}
func (c *Canary) cleanup(timestamp time.Time, writeConn *models.NamedConnection) error {
c.logger.Debug(
"Canary beginning cleanup",
lager.Data{"timestamp": timestamp, "node": writeConn.Name},
)
err := c.sqlClient.Cleanup(writeConn.Connection)
c.logger.Debug(
"Canary completed cleanup",
lager.Data{"timestamp": timestamp, "node": writeConn.Name, "err": err},
)
return err
}