Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial draft for addressing memory is too high error #3884

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/command/scale/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scale
import (
"context"
"fmt"

"github.com/samber/lo"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
Expand Down Expand Up @@ -64,8 +63,12 @@ func v2ScaleVM(ctx context.Context, appName, group, sizeName string, memoryMB in
Region: machine.Region,
Config: machine.Config,
}

if err := mach.Update(ctx, machine, input); err != nil {
return nil, err
fix_err := err.(mach.InvalidConfigErr).AttemptFix( ctx, machine, input )
if fix_err!=nil{
return nil, err
}
}
}

Expand Down
75 changes: 75 additions & 0 deletions internal/machine/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"fmt"
"slices"
"time"
"github.com/superfly/flyctl/internal/prompt"


fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/flapsutil"
Expand All @@ -30,6 +32,7 @@
if input != nil && input.Config != nil && input.Config.Guest != nil {
var invalidConfigErr InvalidConfigErr
invalidConfigErr.guest = input.Config.Guest

// Check that there's a valid number of CPUs
validNumCpus, ok := cpusPerKind[input.Config.Guest.CPUKind]
if !ok {
Expand Down Expand Up @@ -197,3 +200,75 @@
func (e InvalidConfigErr) Error() string {
return string(e.Reason)
}




func (e InvalidConfigErr) AttemptFix( ctx context.Context, m *fly.Machine, input *fly.LaunchMachineInput ) (error) {
unsuccessfull := "Unsuccessful at fixing the error attempt!"
switch e.Reason {
case memoryTooHigh:

// Get correct CPU count
required_cpu_count, err := getRequiredCPUForMemoryIncrease(e)
if err==nil{
combo := string(fmt.Sprintf("%s-cpu-%dx",e.guest.CPUKind,required_cpu_count))

Check failure on line 215 in internal/machine/update.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

// Prompt
fmt.Println("")
prompt_str := fmt.Sprintf("WARNING! \"Machine %s\": %s!\n > A memory of %dMiB requires %d CPU cores, "+
"which can be accomodated by a \"%s\" VM size.\n > Would you like to scale your \"Machine %s\"'s VM size to \" %s\"?"+
"\n ? Agreeing will update your \"Machine %s\"'s VM size to \"%s\" size, and proceed with scaling its memory to the requested %dMiB",
m.ID,
e.Description(),
e.guest.MemoryMB,
required_cpu_count,
combo,
m.ID,
combo,
m.ID,
combo,
e.guest.MemoryMB)

yesScaleCPUFirst, _ := prompt.Confirm(ctx, prompt_str)
if yesScaleCPUFirst{
// Update CPU count!
input.Config.Guest.CPUs = required_cpu_count
err:=Update( ctx, m, input)
if err!=nil{
return err
}else{
// Return fly.VMSize to remain compatible with v1 scale app signature
return nil
}
}
}else{
return err
}

}
return fmt.Errorf( unsuccessfull )
}

func getRequiredCPUForMemoryIncrease( e InvalidConfigErr ) (int, error) {
fmt.Println( fly.MAX_MEMORY_MB_PER_SHARED_CPU )

var required_cpu_count int
// Get cpu count whose max_memory can accommodate the requested memory increase
if e.guest.CPUKind == "shared" {
required_cpu_count = e.guest.MemoryMB / fly.MAX_MEMORY_MB_PER_SHARED_CPU
}else if e.guest.CPUKind == "performance"{ // performance
required_cpu_count = e.guest.MemoryMB / fly.MAX_MEMORY_MB_PER_CPU
}

// Check if cpu count is valid for the cpu kind
validNumCpus, ok := cpusPerKind[e.guest.CPUKind]
if !ok {
return 0, fmt.Errorf( string( invalidCpuKind ) )
} else if !slices.Contains(validNumCpus, required_cpu_count) {
return 0, fmt.Errorf( string( invalidNumCPUs ) )
}

return required_cpu_count, nil
}

Loading