Skip to content

Commit

Permalink
fix: use random port for runners
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Dec 12, 2024
1 parent 7927bb5 commit fe74d68
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
23 changes: 10 additions & 13 deletions backend/provisioner/scaling/localscaling/local_scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/TBD54566975/ftl/backend/controller/artefacts"
"github.com/TBD54566975/ftl/backend/provisioner/scaling"
"github.com/TBD54566975/ftl/backend/runner"
"github.com/TBD54566975/ftl/internal/bind"
"github.com/TBD54566975/ftl/common/plugin"
"github.com/TBD54566975/ftl/internal/dev"
"github.com/TBD54566975/ftl/internal/localdebug"
"github.com/TBD54566975/ftl/internal/log"
Expand All @@ -35,9 +35,7 @@ type localScaling struct {
// Module -> Deployments -> info
runners map[string]map[string]*deploymentInfo
// Module -> Port
debugPorts map[string]*localdebug.DebugInfo
// Module -> Port, most recent runner is present in the map
portAllocator *bind.BindAllocator
debugPorts map[string]*localdebug.DebugInfo
controllerAddresses []*url.URL
leaseAddress *url.URL

Expand Down Expand Up @@ -162,7 +160,7 @@ func (l *localScaling) GetEndpointForDeployment(ctx context.Context, module stri
if r, ok := dep.runner.Get(); ok {
return optional.Some(url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", r.host, r.port),
Host: fmt.Sprintf("%s:%d", r.host, r.port),
}), nil
}
return optional.None[url.URL](), nil
Expand All @@ -178,13 +176,12 @@ type deploymentInfo struct {
}
type runnerInfo struct {
cancelFunc context.CancelFunc
port string
port int
host string
}

func NewLocalScaling(
ctx context.Context,
portAllocator *bind.BindAllocator,
controllerAddresses []*url.URL,
leaseAddress *url.URL,
configPath string,
Expand All @@ -202,7 +199,6 @@ func NewLocalScaling(
lock: sync.Mutex{},
cacheDir: cacheDir,
runners: map[string]map[string]*deploymentInfo{},
portAllocator: portAllocator,
controllerAddresses: controllerAddresses,
leaseAddress: leaseAddress,
prevRunnerSuffix: -1,
Expand Down Expand Up @@ -277,13 +273,13 @@ func (l *localScaling) startRunner(ctx context.Context, deploymentKey model.Depl
debugPort = devEndpoint.debugPort
} else if ide, ok := l.ideSupport.Get(); ok {
var debug *localdebug.DebugInfo
debugBind, err := l.portAllocator.NextPort()
debugBind, err := plugin.AllocatePort()
if err != nil {
return fmt.Errorf("failed to start runner: %w", err)
}
debug = &localdebug.DebugInfo{
Language: info.language,
Port: debugBind,
Port: debugBind.Port,
}
l.debugPorts[info.module] = debug
ide.SyncIDEDebugIntegrations(ctx, l.debugPorts)
Expand All @@ -292,16 +288,17 @@ func (l *localScaling) startRunner(ctx context.Context, deploymentKey model.Depl
controllerEndpoint := l.controllerAddresses[len(l.runners)%len(l.controllerAddresses)]
logger := log.FromContext(ctx)

bind, err := l.portAllocator.Next()
bind, err := plugin.AllocatePort()
if err != nil {
return fmt.Errorf("failed to start runner: %w", err)
}

keySuffix := l.prevRunnerSuffix + 1
l.prevRunnerSuffix = keySuffix

bindURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", bind.Port))

Check failure on line 299 in backend/provisioner/scaling/localscaling/local_scaling.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to err (ineffassign)
config := runner.Config{
Bind: bind,
Bind: bindURL,
ControllerEndpoint: controllerEndpoint,
LeaseEndpoint: l.leaseAddress,
Key: model.NewLocalRunnerKey(keySuffix),
Expand All @@ -325,7 +322,7 @@ func (l *localScaling) startRunner(ctx context.Context, deploymentKey model.Depl
runnerCtx := log.ContextWithLogger(ctx, logger.Scope(simpleName).Module(info.module))

runnerCtx, cancel := context.WithCancel(runnerCtx)
info.runner = optional.Some(runnerInfo{cancelFunc: cancel, port: bind.Port(), host: bind.Hostname()})
info.runner = optional.Some(runnerInfo{cancelFunc: cancel, port: bind.Port, host: "127.0.0.1"})

go func() {
err := runner.Start(runnerCtx, config, l.storage)
Expand Down
1 change: 0 additions & 1 deletion frontend/cli/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ func (s *serveCommonConfig) run(

runnerScaling, err := localscaling.NewLocalScaling(
ctx,
bindAllocator,
controllerAddresses,
s.Lease.Bind,
projConfig.Path,
Expand Down

0 comments on commit fe74d68

Please sign in to comment.