generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9846932
commit 081fe10
Showing
7 changed files
with
271 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package bind | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/alecthomas/atomic" | ||
"github.com/alecthomas/errors" | ||
) | ||
|
||
type BindAllocator struct { | ||
baseURL *url.URL | ||
port atomic.Int32 | ||
} | ||
|
||
func NewBindAllocator(url *url.URL) (*BindAllocator, error) { | ||
_, portStr, err := net.SplitHostPort(url.Host) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
port, err := strconv.Atoi(portStr) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
return &BindAllocator{ | ||
baseURL: url, | ||
port: atomic.NewInt32(int32(port) - 1), //nolint:gosec | ||
}, nil | ||
} | ||
|
||
func (b *BindAllocator) Next() *url.URL { | ||
var l *net.TCPListener | ||
var err error | ||
for { | ||
b.port.Add(1) | ||
l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP(b.baseURL.Hostname()), Port: int(b.port.Load())}) | ||
|
||
if err != nil { | ||
continue | ||
} | ||
_ = l.Close() | ||
|
||
newURL := *b.baseURL | ||
newURL.Host = net.JoinHostPort(b.baseURL.Hostname(), fmt.Sprintf("%d", b.port.Load())) | ||
return &newURL | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package scaling | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/TBD54566975/ftl/backend/common/model" | ||
) | ||
|
||
var _ RunnerScaling = (*K8sScaling)(nil) | ||
|
||
type K8sScaling struct { | ||
} | ||
|
||
func NewK8sScaling() *K8sScaling { | ||
return &K8sScaling{} | ||
} | ||
|
||
func (k *K8sScaling) SetReplicas(ctx context.Context, replicas int, idleRunners []model.RunnerKey) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package scaling | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/TBD54566975/ftl/backend/common/bind" | ||
"github.com/TBD54566975/ftl/backend/common/log" | ||
"github.com/TBD54566975/ftl/backend/common/model" | ||
"github.com/TBD54566975/ftl/backend/runner" | ||
"github.com/alecthomas/errors" | ||
"github.com/alecthomas/kong" | ||
) | ||
|
||
var _ RunnerScaling = (*LocalScaling)(nil) | ||
|
||
type LocalScaling struct { | ||
lock sync.Mutex | ||
cacheDir string | ||
runners map[model.RunnerKey]context.CancelFunc | ||
|
||
portAllocator *bind.BindAllocator | ||
controllerAddresses []*url.URL | ||
} | ||
|
||
func NewLocalScaling(portAllocator *bind.BindAllocator, controllerAddresses []*url.URL) (*LocalScaling, error) { | ||
cacheDir, err := os.UserCacheDir() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return &LocalScaling{ | ||
lock: sync.Mutex{}, | ||
cacheDir: cacheDir, | ||
runners: map[model.RunnerKey]context.CancelFunc{}, | ||
portAllocator: portAllocator, | ||
controllerAddresses: controllerAddresses, | ||
}, nil | ||
} | ||
|
||
func (l *LocalScaling) SetReplicas(ctx context.Context, replicas int, idleRunners []model.RunnerKey) error { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
logger := log.FromContext(ctx) | ||
|
||
replicasToAdd := replicas - len(l.runners) | ||
|
||
if replicasToAdd <= 0 { | ||
replicasToRemove := -replicasToAdd | ||
|
||
for i := 0; i < replicasToRemove; i++ { | ||
if len(idleRunners) == 0 { | ||
return nil | ||
} | ||
runnerToRemove := idleRunners[len(idleRunners)-1] | ||
idleRunners = idleRunners[:len(idleRunners)-1] | ||
|
||
err := l.remove(ctx, runnerToRemove) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
logger.Infof("Adding %d replicas", replicasToAdd) | ||
for i := 0; i < replicasToAdd; i++ { | ||
i := i | ||
|
||
controllerEndpoint := l.controllerAddresses[len(l.runners)%len(l.controllerAddresses)] | ||
config := runner.Config{ | ||
Bind: l.portAllocator.Next(), | ||
ControllerEndpoint: controllerEndpoint, | ||
} | ||
|
||
name := fmt.Sprintf("runner%d", i) | ||
if err := kong.ApplyDefaults(&config, kong.Vars{ | ||
"deploymentdir": filepath.Join(l.cacheDir, "ftl-runner", name, "deployments"), | ||
"language": "go,kotlin", | ||
}); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
// Create a readable ULID for the runner. | ||
var ulid [16]byte | ||
binary.BigEndian.PutUint32(ulid[10:], uint32(len(l.runners)+1)) | ||
ulidStr := fmt.Sprintf("%025X", ulid) | ||
err := config.Key.Scan(ulidStr) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
runnerCtx := log.ContextWithLogger(ctx, logger.Scope(name)) | ||
|
||
runnerCtx, cancel := context.WithCancel(runnerCtx) | ||
l.runners[config.Key] = cancel | ||
|
||
go func() { | ||
logger.Infof("Starting runner: %s", config.Key) | ||
err := runner.Start(runnerCtx, config) | ||
if err != nil { | ||
logger.Errorf(err, "Error starting runner: %s", err) | ||
} | ||
}() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (l *LocalScaling) remove(ctx context.Context, runner model.RunnerKey) error { | ||
log := log.FromContext(ctx) | ||
log.Infof("Removing runner: %s", runner) | ||
|
||
cancel, ok := l.runners[runner] | ||
if !ok { | ||
return errors.Errorf("runner %s not found", runner) | ||
} | ||
|
||
cancel() | ||
delete(l.runners, runner) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package scaling | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/TBD54566975/ftl/backend/common/model" | ||
) | ||
|
||
type RunnerScaling interface { | ||
SetReplicas(ctx context.Context, replicas int, idleRunners []model.RunnerKey) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.