/
count_machines.go
370 lines (318 loc) · 10.5 KB
/
count_machines.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
package scale
import (
"context"
"errors"
"fmt"
"strings"
"github.com/samber/lo"
"github.com/sourcegraph/conc/pool"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
const maxConcurrentActions = 5
func runMachinesScaleCount(ctx context.Context, appName string, appConfig *appconfig.Config, expectedGroupCounts map[string]int, maxPerRegion int) error {
io := iostreams.FromContext(ctx)
flapsClient := flaps.FromContext(ctx)
ctx = appconfig.WithConfig(ctx, appConfig)
apiClient := fly.ClientFromContext(ctx)
machines, _, err := flapsClient.ListFlyAppsMachines(ctx)
if err != nil {
return err
}
var latestCompleteRelease fly.Release
switch releases, err := apiClient.GetAppReleasesMachines(ctx, appName, "complete", 1); {
case err != nil:
return err
case len(releases) == 0:
return fmt.Errorf("this app has no complete releases. Run `fly deploy` to create one and rerun this command")
default:
latestCompleteRelease = releases[0]
}
var regions []string
if v := flag.GetRegion(ctx); v != "" {
regions = strings.Split(v, ",")
}
if len(regions) == 0 {
regions = lo.Uniq(lo.Map(machines, func(m *fly.Machine, _ int) string { return m.Region }))
if len(regions) == 0 {
regions = []string{appConfig.PrimaryRegion}
}
}
volumes, err := flapsClient.GetVolumes(ctx)
if err != nil {
return err
}
defaultGuest, err := flag.GetMachineGuest(ctx, nil)
if err != nil {
return err
}
defaults := newDefaults(appConfig, latestCompleteRelease, machines, volumes,
flag.GetString(ctx, "from-snapshot"), flag.GetBool(ctx, "with-new-volumes"), defaultGuest)
actions, err := computeActions(machines, expectedGroupCounts, regions, maxPerRegion, defaults)
if err != nil {
return err
}
if len(actions) == 0 {
fmt.Fprintf(io.Out, "App already scaled to desired state. No need for changes\n")
return nil
}
fmt.Fprintf(io.Out, "App '%s' is going to be scaled according to this plan:\n", appName)
for _, action := range actions {
fmt.Fprintf(io.Out, "%+4d machines for group '%s' on region '%s' of size '%s'\n",
action.Delta, action.GroupName, action.Region, action.MachineSize())
volumesToReuse := len(action.Volumes)
volumesToCreate := action.VolumesDelta()
switch {
case volumesToReuse > 0 && volumesToCreate > 0:
fmt.Fprintf(io.Out, "%+4d volumes and %d unattached volumes assigned to group '%s' in region '%s'\n", volumesToCreate, volumesToReuse, action.GroupName, action.Region)
case volumesToReuse > 0:
fmt.Fprintf(io.Out, "% 4d unattached volumes to be assigned to group '%s' in region '%s'\n", volumesToReuse, action.GroupName, action.Region)
case volumesToCreate > 0:
fmt.Fprintf(io.Out, "%+4d volumes for group '%s' in region '%s'\n", volumesToCreate, action.GroupName, action.Region)
}
}
if !flag.GetYes(ctx) {
switch confirmed, err := prompt.Confirmf(ctx, "Scale app %s?", appName); {
case err == nil:
if !confirmed {
return nil
}
case prompt.IsNonInteractive(err):
return prompt.NonInteractiveError("--yes flag must be specified when not running interactively")
default:
return err
}
}
// XXX: Don't acquire the leases until the user confirms it wants to execute any action
// The downside is that AcquireLeases has the side effect of fetching an updated copy of machine config
// that we don't use here, but it also updates the `LeaseNonce` field of the original machine which we rely on
_, releaseFunc, err := mach.AcquireLeases(ctx, machines)
defer releaseFunc() // It's important to call the release func even in case of errors
if err != nil {
return err
}
updatePool := pool.New().
WithErrors().
WithMaxGoroutines(maxConcurrentActions).
WithContext(ctx)
fmt.Fprintf(io.Out, "Executing scale plan\n")
for _, action := range actions {
action := action
switch {
case action.Delta > 0:
for i := 0; i < action.Delta; i++ {
updatePool.Go(func(ctx context.Context) error {
m, err := launchMachine(ctx, action, i)
if err != nil {
return err
}
fmt.Fprintf(io.Out, " Created %s group:%s region:%s size:%s",
m.ID, action.GroupName, action.Region, m.Config.Guest.ToSize(),
)
if len(m.Config.Mounts) > 0 {
fmt.Fprintf(io.Out, " volume:%s", m.Config.Mounts[0].Volume)
}
fmt.Fprintln(io.Out)
return nil
})
}
case action.Delta < 0:
for i := 0; i > action.Delta; i-- {
updatePool.Go(func(ctx context.Context) error {
m := action.Machines[-i]
err := destroyMachine(ctx, m)
if err != nil {
return err
}
fmt.Fprintf(io.Out, " Destroyed %s group:%s region:%s size:%s\n", m.ID, action.GroupName, action.Region, m.Config.Guest.ToSize())
return nil
})
}
}
}
return updatePool.Wait()
}
func launchMachine(ctx context.Context, action *planItem, idx int) (*fly.Machine, error) {
flapsClient := flaps.FromContext(ctx)
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
input := helpers.Clone(*action.LaunchMachineInput)
if len(input.Config.Mounts) > 0 {
var volume *fly.Volume
switch {
case idx < len(action.Volumes):
volume = action.Volumes[idx]
case action.CreateVolumeRequest != nil:
cvr := action.CreateVolumeRequest
fmt.Fprintf(io.Out, " Creating volume %s region:%s", colorize.Bold(cvr.Name), cvr.Region)
if cvr.SizeGb != nil {
fmt.Fprintf(io.Out, " size:%dGiB", *cvr.SizeGb)
}
if cvr.SnapshotID != nil {
fmt.Fprintf(io.Out, " from-snapshot:%s", colorize.Bold(*cvr.SnapshotID))
}
fmt.Fprintln(io.Out)
var err error
volume, err = flapsClient.CreateVolume(ctx, *cvr)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Launching the machine requires a volume but there is no volume to attach or create")
}
input.Config.Mounts[0].Volume = volume.ID
}
return flapsClient.Launch(ctx, input)
}
func destroyMachine(ctx context.Context, machine *fly.Machine) error {
flapsClient := flaps.FromContext(ctx)
input := fly.RemoveMachineInput{
ID: machine.ID,
Kill: true,
}
return flapsClient.Destroy(ctx, input, machine.LeaseNonce)
}
type planItem struct {
GroupName string
Region string
// The number of machines to add or remove
Delta int
Machines []*fly.Machine
LaunchMachineInput *fly.LaunchMachineInput
// Volumes to reuse
Volumes []*fly.Volume
// Input used to create new volumes
CreateVolumeRequest *fly.CreateVolumeRequest
}
func (pi *planItem) VolumesDelta() int {
if pi.CreateVolumeRequest == nil {
return 0
}
return pi.Delta - len(pi.Volumes)
}
func (pi *planItem) MachineSize() string {
if pi.Delta > 0 {
return pi.LaunchMachineInput.Config.Guest.ToSize()
}
if len(pi.Machines) > 0 {
return pi.Machines[0].Config.Guest.ToSize()
}
if guest := pi.LaunchMachineInput.Config.Guest; guest != nil {
return guest.ToSize()
}
return ""
}
func computeActions(machines []*fly.Machine, expectedGroupCounts map[string]int, regions []string, maxPerRegion int, defaults *defaultValues) ([]*planItem, error) {
actions := make([]*planItem, 0)
seenGroups := make(map[string]bool)
machineGroups := lo.GroupBy(machines, func(m *fly.Machine) string {
return m.ProcessGroup()
})
for groupName, groupMachines := range machineGroups {
expected, ok := expectedGroupCounts[groupName]
// Ignore the group if it is not expected to change
if !ok {
continue
}
seenGroups[groupName] = true
perRegionMachines := lo.GroupBy(groupMachines, func(m *fly.Machine) string {
return m.Region
})
currentPerRegionCount := lo.MapEntries(perRegionMachines, func(k string, v []*fly.Machine) (string, int) {
return k, len(v)
})
regionDiffs, err := convergeGroupCounts(expected, currentPerRegionCount, regions, maxPerRegion)
if err != nil {
return nil, err
}
mConfig := groupMachines[0].Config
// Nullify standbys, no point on having more than one
mConfig.Standbys = nil
for region, delta := range regionDiffs {
actions = append(actions, &planItem{
GroupName: groupName,
Region: region,
Delta: delta,
Machines: perRegionMachines[region],
LaunchMachineInput: &fly.LaunchMachineInput{Region: region, Config: mConfig},
Volumes: defaults.PopAvailableVolumes(mConfig, region, delta),
CreateVolumeRequest: defaults.CreateVolumeRequest(mConfig, region, delta),
})
}
}
// Fill in the groups without existing machines
for groupName, expected := range expectedGroupCounts {
if seenGroups[groupName] {
continue
}
mConfig, err := defaults.ToMachineConfig(groupName)
if err != nil {
return nil, err
}
regionDiffs, err := convergeGroupCounts(expected, nil, regions, maxPerRegion)
if err != nil {
return nil, err
}
for region, delta := range regionDiffs {
actions = append(actions, &planItem{
GroupName: groupName,
Region: region,
Delta: delta,
LaunchMachineInput: &fly.LaunchMachineInput{Region: region, Config: mConfig},
Volumes: defaults.PopAvailableVolumes(mConfig, region, delta),
CreateVolumeRequest: defaults.CreateVolumeRequest(mConfig, region, delta),
})
}
}
return actions, nil
}
var MaxPerRegionError = errors.New("the number of regions by the maximum machines per region is fewer than the expected total")
func convergeGroupCounts(expectedTotal int, current map[string]int, regions []string, maxPerRegion int) (map[string]int, error) {
diffs := make(map[string]int)
if len(regions) == 0 {
regions = lo.Keys(current)
}
if maxPerRegion >= 0 {
if len(regions)*maxPerRegion < expectedTotal {
return nil, MaxPerRegionError
}
// Compute the diff to any region with more machines than the maximum allowed
for _, region := range regions {
c := current[region]
if c > maxPerRegion {
diffs[region] = maxPerRegion - c
}
}
}
diff := expectedTotal
for _, region := range regions {
diff -= (current[region] + diffs[region])
}
idx := 0
for diff > 0 {
region := regions[idx%(len(regions))]
if maxPerRegion < 0 || current[region]+diffs[region] < maxPerRegion {
diffs[region]++
diff--
}
idx++
}
// Iterate regions in reverse order because the region list
// tend to have the primary region first
idx = -1
for diff < 0 {
region := regions[-idx%(len(regions))]
if current[region]+diffs[region] > 0 {
diffs[region]--
diff++
}
idx--
}
return diffs, nil
}