generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
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 bde28d4
Showing
6 changed files
with
203 additions
and
65 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
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,24 @@ | ||
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) error { | ||
return nil | ||
} | ||
|
||
func (*K8sScaling) Remove(ctx context.Context, runner 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,115 @@ | ||
package scaling | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"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 { | ||
cacheDir string | ||
runners []model.RunnerKey | ||
|
||
nextBind *url.URL | ||
controllerAddresses []*url.URL | ||
} | ||
|
||
func NewLocalScaling(nextBind *url.URL, controllerAddresses []*url.URL) (*LocalScaling, error) { | ||
cacheDir, err := os.UserCacheDir() | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return &LocalScaling{ | ||
cacheDir: cacheDir, | ||
nextBind: nextBind, | ||
controllerAddresses: controllerAddresses, | ||
}, nil | ||
} | ||
|
||
func (l *LocalScaling) SetReplicas(ctx context.Context, replicas int) error { | ||
logger := log.FromContext(ctx) | ||
logger.Infof("SetReplicas: %d", replicas) | ||
|
||
replicasToAdd := replicas - len(l.runners) | ||
|
||
logger.Infof("Requested replicas %d", replicas) | ||
if replicasToAdd <= 0 { | ||
return nil | ||
} | ||
|
||
logger.Infof("Adding %d replicas", replicasToAdd) | ||
for i := 0; i < replicasToAdd; i++ { | ||
i := i | ||
|
||
var err error | ||
l.nextBind, err = IncrementPort(l.nextBind) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
controllerEndpoint := l.controllerAddresses[i%len(l.controllerAddresses)] | ||
config := runner.Config{ | ||
Bind: l.nextBind, | ||
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)) | ||
l.runners = append(l.runners, config.Key) | ||
|
||
go func() { | ||
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("Remove: %s", runner) | ||
return nil | ||
} | ||
|
||
func IncrementPort(baseURL *url.URL) (*url.URL, error) { | ||
newURL := *baseURL | ||
|
||
newPort, err := strconv.Atoi(newURL.Port()) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
newURL.Host = fmt.Sprintf("%s:%d", baseURL.Hostname(), newPort+1) | ||
return &newURL, 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,12 @@ | ||
package scaling | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/TBD54566975/ftl/backend/common/model" | ||
) | ||
|
||
type RunnerScaling interface { | ||
SetReplicas(ctx context.Context, replicas int) error | ||
Remove(ctx context.Context, runner 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
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