-
Notifications
You must be signed in to change notification settings - Fork 224
/
failover.go
395 lines (329 loc) · 11.1 KB
/
failover.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package postgres
import (
"context"
"fmt"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/docker/docker/pkg/ioutils"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/command/ssh"
"github.com/superfly/flyctl/internal/flag"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/watch"
"github.com/superfly/flyctl/iostreams"
)
func newFailover() *cobra.Command {
const (
short = "Failover to a new primary"
long = short + "\n"
usage = "failover"
)
cmd := command.New(usage, short, long, runFailover,
command.RequireSession,
command.RequireAppName,
)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runFailover(ctx context.Context) (err error) {
var (
MinPostgresHaVersion = "0.0.20"
MinPostgresFlexVersion = "0.0.3"
MinPostgresStandaloneVersion = "0.0.7"
io = iostreams.FromContext(ctx)
client = client.FromContext(ctx).API()
appName = appconfig.NameFromContext(ctx)
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
if !app.IsPostgresApp() {
return fmt.Errorf("app %s is not a Postgres app", app.Name)
}
if app.PlatformVersion != "machines" {
return fmt.Errorf("failover is only supported for machines apps")
}
ctx, err = apps.BuildContext(ctx, app)
if err != nil {
return err
}
machines, releaseFunc, err := mach.AcquireAllLeases(ctx)
defer releaseFunc(ctx, machines)
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
if err := hasRequiredVersionOnMachines(machines, MinPostgresHaVersion, MinPostgresFlexVersion, MinPostgresStandaloneVersion); err != nil {
return err
}
// You can not failerover for single node postgres
if len(machines) <= 1 {
return fmt.Errorf("failover is not available for standalone postgres")
}
leader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
if IsFlex(leader) {
if failoverErr := flexFailover(ctx, machines, app); failoverErr != nil {
if err := handleFlexFailoverFail(ctx, machines); err != nil {
fmt.Fprintf(io.ErrOut, "Failed to handle failover failure, please manually configure PG cluster primary")
}
return fmt.Errorf("Failed to run failover: %s", failoverErr)
} else {
return nil
}
}
flapsClient := flaps.FromContext(ctx)
dialer := agent.DialerFromContext(ctx)
pgclient := flypg.NewFromInstance(leader.PrivateIP, dialer)
fmt.Fprintf(io.Out, "Performing a failover\n")
if err := pgclient.Failover(ctx); err != nil {
return fmt.Errorf("failed to trigger failover %w", err)
}
// Wait until the leader lost its role
if err := retry.Do(
func() error {
var err error
leader, err = flapsClient.Get(ctx, leader.ID)
if err != nil {
return err
} else if machineRole(leader) == "leader" {
return fmt.Errorf("%s hasn't lost its leader role", leader.ID)
}
return nil
},
retry.Context(ctx), retry.Attempts(30), retry.Delay(1*time.Second), retry.DelayType(retry.FixedDelay),
); err != nil {
return err
}
// wait for health checks to pass
if err := watch.MachinesChecks(ctx, machines); err != nil {
return fmt.Errorf("failed to wait for health checks to pass: %w", err)
}
fmt.Fprintf(io.Out, "Failover complete\n")
return
}
func flexFailover(ctx context.Context, machines []*api.Machine, app *api.AppCompact) error {
if len(machines) < 3 {
return fmt.Errorf("Not enough machines to meet quorum requirements")
}
io := iostreams.FromContext(ctx)
oldLeader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Performing a failover\n")
primaryRegion := ""
candidates := make([]*api.Machine, 0)
for _, machine := range machines {
machinePrimaryRegion, ok := machine.Config.Env["PRIMARY_REGION"]
if !ok || machinePrimaryRegion == "" {
// Handle case where PRIMARY_REGION hasn't been set, or is empty.
return fmt.Errorf("Machine %s does not have a primary region configured", machine.ID)
}
if primaryRegion == "" {
primaryRegion = machinePrimaryRegion
}
// Ignore any machines residing outside of the primary region
if primaryRegion != machinePrimaryRegion {
return fmt.Errorf("Machines don't agree on a primary region. Cannot safely perform a failover until that's fixed")
}
// Ignore any machines residing outside of the primary region
if primaryRegion != machine.Region {
continue
}
// We don't need to consider the existing leader here.
if machine == oldLeader {
continue
}
candidates = append(candidates, machine)
}
if primaryRegion == "" {
return fmt.Errorf("Could not find primary region for app")
}
newLeader, err := pickNewLeader(ctx, app, candidates)
if err != nil {
return err
}
// Stop the leader
fmt.Println("Stopping current leader... ", oldLeader.ID)
machineStopInput := api.StopMachineInput{
ID: oldLeader.ID,
Signal: "SIGINT",
}
flapsClient := flaps.FromContext(ctx)
err = flapsClient.Stop(ctx, machineStopInput, oldLeader.LeaseNonce)
if err != nil {
return fmt.Errorf("could not stop pg leader %s: %w", oldLeader.ID, err)
}
fmt.Println("Starting new leader")
_, err = flapsClient.Start(ctx, newLeader.ID, newLeader.LeaseNonce)
if err != nil {
return err
}
fmt.Println("Promoting new leader... ", newLeader.ID)
err = ssh.SSHConnect(&ssh.SSHParams{
Ctx: ctx,
Org: app.Organization,
App: app.Name,
Username: "postgres",
Dialer: agent.DialerFromContext(ctx),
Cmd: "repmgr standby promote --siblings-follow -f /data/repmgr.conf",
Stdout: ioutils.NewWriteCloserWrapper(colorable.NewColorableStdout(), func() error { return nil }),
Stderr: ioutils.NewWriteCloserWrapper(colorable.NewColorableStderr(), func() error { return nil }),
Stdin: nil,
}, newLeader.PrivateIP)
if err != nil {
return fmt.Errorf("failed to promote machine %s: %s", newLeader.ID, err)
}
fmt.Println("Waiting 30 seconds for the old leader to stop...")
err = flapsClient.Wait(ctx, oldLeader, "stopped", time.Second*30)
if err != nil {
return err
}
// Restart the old leader
fmt.Fprintf(io.Out, "Restarting old leader... %s\n", oldLeader.ID)
mach, err := flapsClient.Start(ctx, oldLeader.ID, oldLeader.LeaseNonce)
if err != nil {
return fmt.Errorf("failed to start machine %s: %s", oldLeader.ID, err)
}
if mach.Status == "error" {
return fmt.Errorf("old leader %s could not be started: %s", oldLeader.ID, mach.Message)
}
fmt.Printf("Waiting for leadership to swap to %s...\n", newLeader.ID)
if err := retry.Do(
func() error {
oldLeader, err = flapsClient.Get(ctx, newLeader.ID)
if err != nil {
return err
}
if isLeader(oldLeader) {
return nil
} else {
return fmt.Errorf("Machine %s never became the leader", newLeader.ID)
}
},
retry.Context(ctx), retry.Attempts(60), retry.Delay(time.Second), retry.DelayType(retry.FixedDelay),
); err != nil {
return err
}
// wait for health checks to pass
if err := watch.MachinesChecks(ctx, machines); err != nil {
return fmt.Errorf("failed to wait for health checks to pass: %w", err)
}
fmt.Fprintf(io.Out, "Failover complete\n")
return nil
}
func handleFlexFailoverFail(ctx context.Context, machines []*api.Machine) (err error) {
io := iostreams.FromContext(ctx)
flapsClient := flaps.FromContext(ctx)
leader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
fmt.Fprintln(io.ErrOut, "Error promoting new leader, restarting existing leader")
fmt.Println("Waiting for old leader to finish stopping")
if err := retry.Do(
func() error {
leader, err = flapsClient.Get(ctx, leader.ID)
if err != nil {
return err
}
// Because of the fact that we have to handle a failover fail at any time,
// it's possible that the leader hasn't even been stopped yet before failure
// (due to pickNewLeader failing). If that happens, there's no reason to try and
// stop that machine again, just to start it.
if leader.State == "stopped" || leader.State == "started" {
return nil
} else if leader.State == "stopping" {
return fmt.Errorf("Old leader hasn't finished stopping")
} else {
return fmt.Errorf("Old leader is in an unexpected state: %s", leader.State)
}
},
retry.Context(ctx), retry.Attempts(60), retry.Delay(time.Second), retry.DelayType(retry.FixedDelay),
); err != nil {
return err
}
fmt.Println("Clearing existing machine lease...")
// Clear the existing lease on this machine
lease, err := flapsClient.FindLease(ctx, leader.ID)
if err != nil {
if !strings.Contains(err.Error(), " lease not found") {
return err
}
}
if err := flapsClient.ReleaseLease(ctx, leader.ID, lease.Data.Nonce); err != nil {
return err
}
fmt.Println("Trying to start old leader")
// Start the machine again
leader, err = flapsClient.Get(ctx, leader.ID)
if err != nil {
return err
}
mach, err := flapsClient.Start(ctx, leader.ID, leader.LeaseNonce)
if err != nil {
return err
}
if mach.Status == "error" {
return fmt.Errorf("old leader %s could not be started: %s", leader.ID, mach.Message)
}
fmt.Println("Old leader started succesfully")
return nil
}
func pickNewLeader(ctx context.Context, app *api.AppCompact, machinesWithinPrimaryRegion []*api.Machine) (*api.Machine, error) {
machineReasons := make(map[string]string)
for _, machine := range machinesWithinPrimaryRegion {
isValid := true
if isLeader(machine) {
isValid = false
machineReasons[machine.ID] = "already leader"
} else if !machine.AllHealthChecks().AllPassing() {
isValid = false
machineReasons[machine.ID] = "1+ health checks are not passing"
} else if !passesDryRun(ctx, app, machine) {
isValid = false
machineReasons[machine.ID] = fmt.Sprintf("Running a dry run of `repmgr standby switchover` failed. Try running `fly ssh console -u postgres -C 'repmgr standby switchover -f /data/repmgr.conf --dry-run' -s -a %s` for more information. This was most likely due to the requirements for quorum not being met.", app.Name)
}
if isValid {
return machine, nil
}
}
err := "no leader could be chosen. Here are the reasons why: \n"
for machineID, reason := range machineReasons {
err = fmt.Sprintf("%s%s: %s\n", err, machineID, reason)
}
err += "\nplease fix one or more of the above issues, and try again\n"
return nil, fmt.Errorf(err)
}
// Before doing anything that might mess up, it's useful to check if a dry run of the failover command will work, since that allows repmgr to do some checks
func passesDryRun(ctx context.Context, app *api.AppCompact, machine *api.Machine) bool {
err := ssh.SSHConnect(&ssh.SSHParams{
Ctx: ctx,
Org: app.Organization,
App: app.Name,
Username: "postgres",
Dialer: agent.DialerFromContext(ctx),
Cmd: "repmgr standby switchover -f /data/repmgr.conf --dry-run",
Stdout: nil,
Stderr: nil,
Stdin: nil,
}, machine.PrivateIP)
return err == nil
}