generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
local_scaling.go
132 lines (105 loc) · 3.15 KB
/
local_scaling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package localscaling
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"time"
"github.com/alecthomas/kong"
"github.com/TBD54566975/ftl/backend/controller/scaling"
"github.com/TBD54566975/ftl/backend/runner"
"github.com/TBD54566975/ftl/internal/bind"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
)
var _ scaling.RunnerScaling = (*LocalScaling)(nil)
type LocalScaling struct {
lock sync.Mutex
cacheDir string
runners map[string]context.CancelFunc
portAllocator *bind.BindAllocator
controllerAddresses []*url.URL
prevRunnerSuffix int
}
func NewLocalScaling(portAllocator *bind.BindAllocator, controllerAddresses []*url.URL) (*LocalScaling, error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return nil, err
}
return &LocalScaling{
lock: sync.Mutex{},
cacheDir: cacheDir,
runners: map[string]context.CancelFunc{},
portAllocator: portAllocator,
controllerAddresses: controllerAddresses,
prevRunnerSuffix: -1,
}, 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 range replicasToRemove {
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 err
}
}
return nil
}
logger.Debugf("Adding %d replicas", replicasToAdd)
for range replicasToAdd {
controllerEndpoint := l.controllerAddresses[len(l.runners)%len(l.controllerAddresses)]
bind := l.portAllocator.Next()
keySuffix := l.prevRunnerSuffix + 1
l.prevRunnerSuffix = keySuffix
config := runner.Config{
Bind: bind,
ControllerEndpoint: controllerEndpoint,
TemplateDir: templateDir(ctx),
Key: model.NewLocalRunnerKey(keySuffix),
}
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",
}); err != nil {
return err
}
config.HeartbeatPeriod = time.Second
config.HeartbeatJitter = time.Millisecond * 100
runnerCtx := log.ContextWithLogger(ctx, logger.Scope(simpleName))
runnerCtx, cancel := context.WithCancel(runnerCtx)
l.runners[config.Key.String()] = cancel
go func() {
logger.Debugf("Starting runner: %s", config.Key)
err := runner.Start(runnerCtx, config)
if err != nil && !errors.Is(err, context.Canceled) {
logger.Errorf(err, "Runner failed: %s", err)
}
}()
}
return nil
}
func (l *LocalScaling) remove(ctx context.Context, runner model.RunnerKey) error {
log := log.FromContext(ctx)
log.Debugf("Removing runner: %s", runner)
cancel, ok := l.runners[runner.String()]
if !ok {
return fmt.Errorf("runner %s not found", runner)
}
cancel()
delete(l.runners, runner.String())
return nil
}