diff --git a/backend/controller/scaling/localscaling/local_scaling.go b/backend/controller/scaling/localscaling/local_scaling.go index 44b3c9673d..98080d9a1c 100644 --- a/backend/controller/scaling/localscaling/local_scaling.go +++ b/backend/controller/scaling/localscaling/local_scaling.go @@ -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 } @@ -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", diff --git a/cmd/ftl/cmd_serve.go b/cmd/ftl/cmd_serve.go index 61d11fa5a7..be7bea3748 100644 --- a/cmd/ftl/cmd_serve.go +++ b/cmd/ftl/cmd_serve.go @@ -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, @@ -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 { diff --git a/internal/model/keys.go b/internal/model/keys.go index 6202c2072f..5740e7267f 100644 --- a/internal/model/keys.go +++ b/internal/model/keys.go @@ -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) } @@ -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) } @@ -73,23 +74,12 @@ func parseKey[KT keyType[U], U any](key string, includesKind bool) (KT, error) { switch { case len(components) == 1: //style: [-] - - 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: [-]-- - - 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], "-") @@ -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) { @@ -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 }