Skip to content

Commit

Permalink
use hex suffix in production, allow 0 suffixes otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Mar 13, 2024
1 parent 72a150e commit c523c89
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
4 changes: 2 additions & 2 deletions backend/controller/scaling/localscaling/local_scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewLocalScaling(portAllocator *bind.BindAllocator, controllerAddresses []*u
runners: map[model.RunnerKey]context.CancelFunc{},
portAllocator: portAllocator,
controllerAddresses: controllerAddresses,
prevRunnerSuffix: 0, // first runner will have a key of r-0001
prevRunnerSuffix: -1,
}, nil
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (l *LocalScaling) SetReplicas(ctx context.Context, replicas int, idleRunner
Key: model.NewLocalRunnerKey(keySuffix),
}

simpleName := fmt.Sprintf("runner%d", config.Key.Suffix)
simpleName := fmt.Sprintf("runner%d", keySuffix)
if err := kong.ApplyDefaults(&config, kong.Vars{
"deploymentdir": filepath.Join(l.cacheDir, "ftl-runner", simpleName, "deployments"),
"language": "go,kotlin",
Expand Down
4 changes: 2 additions & 2 deletions cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *serveCmd) Run(ctx context.Context) error {
i := i
config := controller.Config{
Bind: controllerAddresses[i],
Key: model.NewLocalControllerKey(i + 1),
Key: model.NewLocalControllerKey(i),
DSN: dsn,
AllowOrigins: s.AllowOrigins,
NoConsole: s.NoConsole,
Expand All @@ -112,7 +112,7 @@ func (s *serveCmd) Run(ctx context.Context) error {
return err
}

scope := fmt.Sprintf("controller%d", config.Key.Suffix)
scope := fmt.Sprintf("controller%d", i)
controllerCtx := log.ContextWithLogger(ctx, logger.Scope(scope))

wg.Go(func() error {
Expand Down
38 changes: 14 additions & 24 deletions internal/model/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"math/big"
"reflect"
"strconv"
"strings"
)

func NewRunnerKey(hostname string, port string) RunnerKey {
suffix, err := rand.Int(rand.Reader, big.NewInt(10000))
hash := make([]byte, 4)
_, err := rand.Read(hash)
if err != nil {
panic(err)
}
return keyType[runnerKey]{
Hostname: hostname,
Port: port,
Suffix: int(suffix.Int64()),
Suffix: fmt.Sprintf("%08x", hash),
}
}
func NewLocalRunnerKey(suffix int) RunnerKey {
return keyType[runnerKey]{
Suffix: suffix,
Suffix: fmt.Sprintf("%04d", suffix),
}
}
func ParseRunnerKey(key string) (RunnerKey, error) { return parseKey[RunnerKey](key, true) }
Expand All @@ -34,19 +33,21 @@ type runnerKey struct{}
type RunnerKey = keyType[runnerKey]

func NewControllerKey(hostname string, port string) ControllerKey {
suffix, err := rand.Int(rand.Reader, big.NewInt(10000))
hash := make([]byte, 4)
_, err := rand.Read(hash)
if err != nil {
panic(err)
}
return keyType[controllerKey]{
Hostname: hostname,
Port: port,
Suffix: int(suffix.Int64()),
Suffix: fmt.Sprintf("%08x", hash),
}
}

func NewLocalControllerKey(suffix int) ControllerKey {
return keyType[controllerKey]{
Suffix: suffix,
Suffix: fmt.Sprintf("%04d", suffix),
}
}
func ParseControllerKey(key string) (ControllerKey, error) { return parseKey[ControllerKey](key, true) }
Expand All @@ -73,23 +74,12 @@ func parseKey[KT keyType[U], U any](key string, includesKind bool) (KT, error) {
switch {
case len(components) == 1:
//style: [<kind>-]<suffix>

suffix, err := strconv.Atoi(components[len(components)-1])
if err != nil {
return KT{}, fmt.Errorf("invalid suffix for key: %s", key)
}

return KT{
Suffix: suffix,
Suffix: components[0],
}, nil
case len(components) >= 3:
//style: [<kind>-]<host>-<port>-<suffix>

suffix, err := strconv.Atoi(components[len(components)-1])
if err != nil {
return KT{}, fmt.Errorf("invalid suffix for key: %s", key)
}

suffix := components[len(components)-1]
port := components[len(components)-2]
host := strings.Join(components[:len(components)-2], "-")

Expand All @@ -109,7 +99,7 @@ func parseKey[KT keyType[U], U any](key string, includesKind bool) (KT, error) {
type keyType[T any] struct {
Hostname string
Port string
Suffix int
Suffix string
}

func (d keyType[T]) Value() (driver.Value, error) {
Expand Down Expand Up @@ -145,9 +135,9 @@ func (d keyType[T]) string(includeKind bool) string {
prefix = fmt.Sprintf("%s-", d.Kind())
}
if d.Hostname == "" {
return fmt.Sprintf("%s%04d", prefix, d.Suffix)
return fmt.Sprintf("%s%s", prefix, d.Suffix)
}
return fmt.Sprintf("%s%s-%s-%04d", prefix, d.Hostname, d.Port, d.Suffix)
return fmt.Sprintf("%s%s-%s-%s", prefix, d.Hostname, d.Port, d.Suffix)
}

func (d keyType[T]) MarshalText() ([]byte, error) { return []byte(d.String()), nil }
Expand Down

0 comments on commit c523c89

Please sign in to comment.