-
Notifications
You must be signed in to change notification settings - Fork 220
/
leasable_machine.go
481 lines (438 loc) · 14.7 KB
/
leasable_machine.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package machine
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/jpillora/backoff"
"github.com/morikuni/aec"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/terminal"
"golang.org/x/exp/maps"
)
type LeasableMachine interface {
Machine() *api.Machine
HasLease() bool
AcquireLease(context.Context, time.Duration) error
RefreshLease(context.Context, time.Duration) error
ReleaseLease(context.Context) error
StartBackgroundLeaseRefresh(context.Context, time.Duration, time.Duration)
Update(context.Context, api.LaunchMachineInput) error
Start(context.Context) error
Destroy(context.Context, bool) error
WaitForState(context.Context, string, time.Duration, string, bool) error
WaitForSmokeChecksToPass(context.Context, string) error
WaitForHealthchecksToPass(context.Context, time.Duration, string) error
WaitForEventTypeAfterType(context.Context, string, string, time.Duration, bool) (*api.MachineEvent, error)
FormattedMachineId() string
GetMinIntervalAndMinGracePeriod() (time.Duration, time.Duration)
}
type leasableMachine struct {
flapsClient *flaps.Client
io *iostreams.IOStreams
colorize *iostreams.ColorScheme
machine *api.Machine
leaseNonce string
leaseRefreshCancelFunc context.CancelFunc
destroyed bool
}
func NewLeasableMachine(flapsClient *flaps.Client, io *iostreams.IOStreams, machine *api.Machine) LeasableMachine {
return &leasableMachine{
flapsClient: flapsClient,
io: io,
colorize: io.ColorScheme(),
machine: machine,
leaseNonce: machine.LeaseNonce,
}
}
func (lm *leasableMachine) Update(ctx context.Context, input api.LaunchMachineInput) error {
if lm.IsDestroyed() {
return fmt.Errorf("error cannot update machine %s that was already destroyed", lm.machine.ID)
}
if !lm.HasLease() {
return fmt.Errorf("no current lease for machine %s", lm.machine.ID)
}
input.ID = lm.machine.ID
updateMachine, err := lm.flapsClient.Update(ctx, input, lm.leaseNonce)
if err != nil {
return err
}
lm.machine = updateMachine
return nil
}
func (lm *leasableMachine) Destroy(ctx context.Context, kill bool) error {
if lm.IsDestroyed() {
return nil
}
input := api.RemoveMachineInput{
ID: lm.machine.ID,
Kill: kill,
}
err := lm.flapsClient.Destroy(ctx, input, lm.leaseNonce)
if err != nil {
return err
}
lm.destroyed = true
return nil
}
func (lm *leasableMachine) FormattedMachineId() string {
res := lm.Machine().ID
if lm.Machine().Config.Metadata == nil {
return res
}
procGroup := lm.Machine().ProcessGroup()
if procGroup == "" || lm.Machine().IsFlyAppsReleaseCommand() || lm.Machine().IsFlyAppsConsole() {
return res
}
return fmt.Sprintf("%s [%s]", res, procGroup)
}
func (lm *leasableMachine) logClearLinesAbove(count int) {
if lm.io.IsInteractive() {
builder := aec.EmptyBuilder
str := builder.Up(uint(count)).EraseLine(aec.EraseModes.All).ANSI
fmt.Fprint(lm.io.ErrOut, str.String())
}
}
func (lm *leasableMachine) logStatusWaiting(desired, prefix string) {
if prefix != "" {
prefix += " "
}
fmt.Fprintf(lm.io.ErrOut, " %sWaiting for %s to have state: %s\n",
prefix,
lm.colorize.Bold(lm.FormattedMachineId()),
lm.colorize.Yellow(desired),
)
}
func (lm *leasableMachine) logStatusFinished(current string) {
fmt.Fprintf(lm.io.ErrOut, " Machine %s has state: %s\n",
lm.colorize.Bold(lm.FormattedMachineId()),
lm.colorize.Green(current),
)
}
func (lm *leasableMachine) logHealthCheckStatus(status *api.HealthCheckStatus, prefix string) {
if status == nil {
return
}
resColor := lm.colorize.Green
if status.Passing != status.Total {
resColor = lm.colorize.Yellow
}
if prefix != "" {
prefix += " "
}
fmt.Fprintf(lm.io.ErrOut, " %sWaiting for %s to become healthy: %s\n",
prefix,
lm.colorize.Bold(lm.FormattedMachineId()),
resColor(fmt.Sprintf("%d/%d", status.Passing, status.Total)),
)
}
func (lm *leasableMachine) Start(ctx context.Context) error {
if lm.IsDestroyed() {
return fmt.Errorf("error cannot start machine %s that was already destroyed", lm.machine.ID)
}
if lm.HasLease() {
return fmt.Errorf("error cannot start machine %s because it has a lease", lm.machine.ID)
}
lm.logStatusWaiting(api.MachineStateStarted, "")
_, err := lm.flapsClient.Start(ctx, lm.machine.ID, "")
if err != nil {
return err
}
return nil
}
// resolveTimeoutContext returns a context that will timeout, with the possibility of an untimed context on time=0 if
// the flag allowInfinite is set to true.
func resolveTimeoutContext(ctx context.Context, timeout time.Duration, allowInfinite bool) (context.Context, context.CancelFunc, time.Duration) {
// This situation is a bug, so we'll just go with vaguely normal behavior.
if !allowInfinite && timeout == 0 {
terminal.Warnf("resolveTimeoutContext: allowInfinite is false and timeout is 0, setting timeout to 2 minutes\n")
timeout = 2 * time.Minute
}
if timeout != 0 {
// If we have a timeout, put it on the context.
waitCtx, cancel := context.WithTimeout(ctx, timeout)
return waitCtx, cancel, timeout
} else {
// We'll set a timeout of 2 minutes for flaps, knowing that it will keep
// polling until the machine is in the desired state. (this is just so we don't keep spamming the API)
// We'll keep polling because the actual stopping mechanism is the context, which we haven't set a timeout on.
return ctx, func() {}, 2 * time.Minute
}
}
func (lm *leasableMachine) WaitForState(ctx context.Context, desiredState string, timeout time.Duration, logPrefix string, allowInfinite bool) error {
waitCtx, cancel, timeout := resolveTimeoutContext(ctx, timeout, allowInfinite)
defer cancel()
b := &backoff.Backoff{
Min: 500 * time.Millisecond,
Max: 2 * time.Second,
Factor: 2,
Jitter: true,
}
lm.logClearLinesAbove(1)
lm.logStatusWaiting(desiredState, logPrefix)
for {
err := lm.flapsClient.Wait(waitCtx, lm.Machine(), desiredState, timeout)
notFoundResponse := false
if err != nil {
var flapsErr *flaps.FlapsError
if errors.As(err, &flapsErr) {
notFoundResponse = flapsErr.ResponseStatusCode == http.StatusNotFound
}
}
switch {
case errors.Is(waitCtx.Err(), context.Canceled):
return err
case errors.Is(waitCtx.Err(), context.DeadlineExceeded):
return fmt.Errorf("timeout reached waiting for machine to %s %w", desiredState, err)
case notFoundResponse && desiredState != api.MachineStateDestroyed:
return err
case !notFoundResponse && err != nil:
time.Sleep(b.Duration())
continue
}
lm.logClearLinesAbove(1)
lm.logStatusFinished(desiredState)
return nil
}
}
func (lm *leasableMachine) isConstantlyRestarting(machine *api.Machine) bool {
var ev *api.MachineEvent
for _, mev := range machine.Events {
if mev.Type == "exit" {
ev = mev
break
}
}
if ev == nil {
return false
}
return !ev.Request.ExitEvent.RequestedStop &&
ev.Request.ExitEvent.Restarting &&
ev.Request.RestartCount > 1 &&
ev.Request.ExitEvent.ExitCode != 0
}
func (lm *leasableMachine) WaitForSmokeChecksToPass(ctx context.Context, logPrefix string) error {
waitCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
b := &backoff.Backoff{
Min: 500 * time.Millisecond,
Max: 2 * time.Second,
Factor: 2,
Jitter: true,
}
fmt.Fprintf(lm.io.ErrOut, " %s Checking that %s is up and running\n",
logPrefix,
lm.colorize.Bold(lm.FormattedMachineId()),
)
for {
machine, err := lm.flapsClient.Get(waitCtx, lm.Machine().ID)
switch {
case errors.Is(waitCtx.Err(), context.Canceled):
return err
case errors.Is(waitCtx.Err(), context.DeadlineExceeded):
return nil
case err != nil:
return fmt.Errorf("error getting machine %s from api: %w", lm.Machine().ID, err)
}
switch {
case lm.isConstantlyRestarting(machine):
return fmt.Errorf("the app appears to be crashing")
default:
time.Sleep(b.Duration())
}
}
}
func (lm *leasableMachine) WaitForHealthchecksToPass(ctx context.Context, timeout time.Duration, logPrefix string) error {
if len(lm.Machine().Checks) == 0 {
return nil
}
waitCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
checkDefs := maps.Values(lm.Machine().Config.Checks)
for _, s := range lm.Machine().Config.Services {
checkDefs = append(checkDefs, s.Checks...)
}
shortestInterval := 120 * time.Second
for _, c := range checkDefs {
if c.Interval != nil && c.Interval.Duration < shortestInterval {
shortestInterval = c.Interval.Duration
}
}
b := &backoff.Backoff{
Min: shortestInterval / 2,
Max: 2 * shortestInterval,
Factor: 2,
Jitter: true,
}
printedFirst := false
for {
updateMachine, err := lm.flapsClient.Get(waitCtx, lm.Machine().ID)
switch {
case errors.Is(waitCtx.Err(), context.Canceled):
return err
case errors.Is(waitCtx.Err(), context.DeadlineExceeded):
return fmt.Errorf("timeout reached waiting for healthchecks to pass for machine %s %w", lm.Machine().ID, err)
case err != nil:
return fmt.Errorf("error getting machine %s from api: %w", lm.Machine().ID, err)
case !updateMachine.AllHealthChecks().AllPassing():
if !printedFirst || lm.io.IsInteractive() {
lm.logClearLinesAbove(1)
lm.logHealthCheckStatus(updateMachine.AllHealthChecks(), logPrefix)
printedFirst = true
}
time.Sleep(b.Duration())
continue
}
lm.logClearLinesAbove(1)
lm.logHealthCheckStatus(updateMachine.AllHealthChecks(), logPrefix)
return nil
}
}
// waits for an eventType1 type event to show up after we see a eventType2 event, and returns it
func (lm *leasableMachine) WaitForEventTypeAfterType(ctx context.Context, eventType1, eventType2 string, timeout time.Duration, allowInfinite bool) (*api.MachineEvent, error) {
waitCtx, cancel, _ := resolveTimeoutContext(ctx, timeout, allowInfinite)
defer cancel()
b := &backoff.Backoff{
Min: 500 * time.Millisecond,
Max: 2 * time.Second,
Factor: 2,
Jitter: true,
}
lm.logClearLinesAbove(1)
fmt.Fprintf(lm.io.ErrOut, " Waiting for %s to get %s event\n",
lm.colorize.Bold(lm.FormattedMachineId()),
lm.colorize.Yellow(eventType1),
)
for {
updateMachine, err := lm.flapsClient.Get(waitCtx, lm.Machine().ID)
switch {
case errors.Is(waitCtx.Err(), context.Canceled):
return nil, err
case errors.Is(waitCtx.Err(), context.DeadlineExceeded):
return nil, fmt.Errorf("timeout reached waiting for healthchecks to pass for machine %s %w", lm.Machine().ID, err)
case err != nil:
return nil, fmt.Errorf("error getting machine %s from api: %w", lm.Machine().ID, err)
}
exitEvent := updateMachine.GetLatestEventOfTypeAfterType(eventType1, eventType2)
if exitEvent != nil {
return exitEvent, nil
} else {
time.Sleep(b.Duration())
}
}
}
func (lm *leasableMachine) Machine() *api.Machine {
return lm.machine
}
func (lm *leasableMachine) HasLease() bool {
return lm.leaseNonce != ""
}
func (lm *leasableMachine) IsDestroyed() bool {
return lm.destroyed
}
func (lm *leasableMachine) AcquireLease(ctx context.Context, duration time.Duration) error {
if lm.HasLease() {
return nil
}
seconds := int(duration.Seconds())
lease, err := lm.flapsClient.AcquireLease(ctx, lm.machine.ID, &seconds)
if err != nil {
return err
}
if lease.Status != "success" {
return fmt.Errorf("did not acquire lease for machine %s status: %s code: %s message: %s", lm.machine.ID, lease.Status, lease.Code, lease.Message)
}
if lease.Data == nil {
return fmt.Errorf("missing data from lease response for machine %s, assuming not successful", lm.machine.ID)
}
lm.leaseNonce = lease.Data.Nonce
return nil
}
func (lm *leasableMachine) RefreshLease(ctx context.Context, duration time.Duration) error {
seconds := int(duration.Seconds())
refreshedLease, err := lm.flapsClient.RefreshLease(ctx, lm.machine.ID, &seconds, lm.leaseNonce)
if err != nil {
return err
}
if refreshedLease.Status != "success" {
return fmt.Errorf("did not acquire lease for machine %s status: %s code: %s message: %s", lm.machine.ID, refreshedLease.Status, refreshedLease.Code, refreshedLease.Message)
} else if refreshedLease.Data == nil {
return fmt.Errorf("missing data from lease response for machine %s, assuming not successful", lm.machine.ID)
} else if refreshedLease.Data.Nonce != lm.leaseNonce {
return fmt.Errorf("unexpectedly received a new nonce when trying to refresh lease on machine %s", lm.machine.ID)
}
return nil
}
func (lm *leasableMachine) StartBackgroundLeaseRefresh(ctx context.Context, leaseDuration time.Duration, delayBetween time.Duration) {
ctx, lm.leaseRefreshCancelFunc = context.WithCancel(ctx)
go lm.refreshLeaseUntilCanceled(ctx, leaseDuration, delayBetween)
}
func (lm *leasableMachine) refreshLeaseUntilCanceled(ctx context.Context, duration time.Duration, delayBetween time.Duration) {
var (
err error
b = &backoff.Backoff{
Min: delayBetween - 20*time.Millisecond,
Max: delayBetween + 20*time.Millisecond,
Jitter: true,
}
)
for {
err = lm.RefreshLease(ctx, duration)
switch {
case errors.Is(err, context.Canceled):
return
case err != nil:
terminal.Warnf("error refreshing lease for machine %s: %v\n", lm.machine.ID, err)
}
time.Sleep(b.Duration())
}
}
func (lm *leasableMachine) ReleaseLease(ctx context.Context) error {
nonce := lm.leaseNonce
lm.resetLease()
if nonce == "" {
return nil
}
// when context is canceled, take 500ms to attempt to release the leases
contextWasAlreadyCanceled := errors.Is(ctx.Err(), context.Canceled)
if contextWasAlreadyCanceled {
var cancel context.CancelFunc
cancelTimeout := 500 * time.Millisecond
ctx, cancel = context.WithTimeout(context.TODO(), cancelTimeout)
terminal.Infof("detected canceled context and allowing %s to release machine %s lease\n", cancelTimeout, lm.FormattedMachineId())
defer cancel()
}
err := lm.flapsClient.ReleaseLease(ctx, lm.machine.ID, nonce)
contextTimedOutOrCanceled := errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)
if err != nil && (!contextWasAlreadyCanceled || !contextTimedOutOrCanceled) {
terminal.Warnf("failed to release lease for machine %s: %v\n", lm.machine.ID, err)
return err
}
return nil
}
func (lm *leasableMachine) resetLease() {
lm.leaseNonce = ""
if lm.leaseRefreshCancelFunc != nil {
lm.leaseRefreshCancelFunc()
}
}
func (lm *leasableMachine) GetMinIntervalAndMinGracePeriod() (time.Duration, time.Duration) {
minInterval := 60 * time.Second
checkDefs := maps.Values(lm.Machine().Config.Checks)
for _, s := range lm.Machine().Config.Services {
checkDefs = append(checkDefs, s.Checks...)
}
minGracePeriod := time.Second
for _, c := range checkDefs {
if c.Interval != nil && c.Interval.Duration < minInterval {
minInterval = c.Interval.Duration
}
if c.GracePeriod != nil && c.GracePeriod.Duration < minGracePeriod {
minGracePeriod = c.GracePeriod.Duration
}
}
return minInterval, minGracePeriod
}