-
Notifications
You must be signed in to change notification settings - Fork 27
/
docker.go
373 lines (317 loc) · 10.4 KB
/
docker.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package worker
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
const containerModelDir = "/models"
const containerPort = "8000/tcp"
const pollingInterval = 500 * time.Millisecond
const containerTimeout = 2 * time.Minute
const externalContainerTimeout = 2 * time.Minute
const optFlagsContainerTimeout = 5 * time.Minute
const containerRemoveTimeout = 30 * time.Second
const containerCreatorLabel = "creator"
const containerCreator = "ai-worker"
// This only works right now on a single GPU because if there is another container
// using the GPU we stop it so we don't have to worry about having enough ports
var containerHostPorts = map[string]string{
"text-to-image": "8000",
"image-to-image": "8100",
"image-to-video": "8200",
"upscale": "8300",
"audio-to-text": "8400",
"llm": "8500",
"segment-anything-2": "8600",
"image-to-text": "8700",
"text-to-speech": "8800",
}
// Mapping for per pipeline container images.
var pipelineToImage = map[string]string{
"segment-anything-2": "livepeer/ai-runner:segment-anything-2",
"text-to-speech": "livepeer/ai-runner:text-to-speech",
}
type DockerManager struct {
defaultImage string
gpus []string
modelDir string
dockerClient *client.Client
// gpu ID => container name
gpuContainers map[string]string
// container name => container
containers map[string]*RunnerContainer
mu *sync.Mutex
}
func NewDockerManager(defaultImage string, gpus []string, modelDir string) (*DockerManager, error) {
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), containerTimeout)
if err := removeExistingContainers(ctx, dockerClient); err != nil {
cancel()
return nil, err
}
cancel()
return &DockerManager{
defaultImage: defaultImage,
gpus: gpus,
modelDir: modelDir,
dockerClient: dockerClient,
gpuContainers: make(map[string]string),
containers: make(map[string]*RunnerContainer),
mu: &sync.Mutex{},
}, nil
}
func (m *DockerManager) Warm(ctx context.Context, pipeline string, modelID string, optimizationFlags OptimizationFlags) error {
m.mu.Lock()
defer m.mu.Unlock()
_, err := m.createContainer(ctx, pipeline, modelID, true, optimizationFlags)
return err
}
func (m *DockerManager) Stop(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
var stopContainerWg sync.WaitGroup
for name, rc := range m.containers {
stopContainerWg.Add(1)
go func(containerID string) {
defer stopContainerWg.Done()
if err := dockerRemoveContainer(m.dockerClient, containerID); err != nil {
slog.Error("Error removing managed container", slog.String("name", name), slog.String("id", containerID))
}
}(rc.ID)
delete(m.gpuContainers, rc.GPU)
delete(m.containers, name)
}
stopContainerWg.Wait()
return nil
}
func (m *DockerManager) Borrow(ctx context.Context, pipeline, modelID string) (*RunnerContainer, error) {
m.mu.Lock()
defer m.mu.Unlock()
for _, runner := range m.containers {
if runner.Pipeline == pipeline && runner.ModelID == modelID {
delete(m.containers, runner.Name)
return runner, nil
}
}
// The container does not exist so try to create it
var err error
// TODO: Optimization flags for dynamically loaded (borrowed) containers are not currently supported due to startup delays.
rc, err := m.createContainer(ctx, pipeline, modelID, false, map[string]EnvValue{})
if err != nil {
return nil, err
}
// Remove container so it is unavailable until Return() is called
delete(m.containers, rc.Name)
return rc, nil
}
func (m *DockerManager) Return(rc *RunnerContainer) {
m.mu.Lock()
defer m.mu.Unlock()
m.containers[rc.Name] = rc
}
// HasCapacity checks if an unused managed container exists or if a GPU is available for a new container.
func (m *DockerManager) HasCapacity(ctx context.Context, pipeline, modelID string) bool {
m.mu.Lock()
defer m.mu.Unlock()
// Check if unused managed container exists for the requested model.
for _, rc := range m.containers {
if rc.Pipeline == pipeline && rc.ModelID == modelID {
return true
}
}
// Check for available GPU to allocate for a new container for the requested model.
_, err := m.allocGPU(ctx)
return err == nil
}
func (m *DockerManager) createContainer(ctx context.Context, pipeline string, modelID string, keepWarm bool, optimizationFlags OptimizationFlags) (*RunnerContainer, error) {
gpu, err := m.allocGPU(ctx)
if err != nil {
return nil, err
}
// NOTE: We currently allow only one container per GPU for each pipeline.
containerHostPort := containerHostPorts[pipeline][:3] + gpu
containerName := dockerContainerName(pipeline, modelID, containerHostPort)
containerImage := m.defaultImage
if pipelineSpecificImage, ok := pipelineToImage[pipeline]; ok {
containerImage = pipelineSpecificImage
}
slog.Info("Starting managed container", slog.String("gpu", gpu), slog.String("name", containerName), slog.String("modelID", modelID), slog.String("containerImage", containerImage))
// Add optimization flags as environment variables.
envVars := []string{
"PIPELINE=" + pipeline,
"MODEL_ID=" + modelID,
}
for key, value := range optimizationFlags {
envVars = append(envVars, key+"="+value.String())
}
containerConfig := &container.Config{
Image: containerImage,
Env: envVars,
Volumes: map[string]struct{}{
containerModelDir: {},
},
ExposedPorts: nat.PortSet{
containerPort: struct{}{},
},
Labels: map[string]string{
containerCreatorLabel: containerCreator,
},
}
gpuOpts := opts.GpuOpts{}
gpuOpts.Set("device=" + gpu)
hostConfig := &container.HostConfig{
Resources: container.Resources{
DeviceRequests: gpuOpts.Value(),
},
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: m.modelDir,
Target: containerModelDir,
},
},
PortBindings: nat.PortMap{
containerPort: []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: containerHostPort,
},
},
},
}
resp, err := m.dockerClient.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, containerName)
if err != nil {
return nil, err
}
cctx, cancel := context.WithTimeout(ctx, containerTimeout)
if err := m.dockerClient.ContainerStart(cctx, resp.ID, container.StartOptions{}); err != nil {
cancel()
dockerRemoveContainer(m.dockerClient, resp.ID)
return nil, err
}
cancel()
cctx, cancel = context.WithTimeout(ctx, containerTimeout)
if err := dockerWaitUntilRunning(cctx, m.dockerClient, resp.ID, pollingInterval); err != nil {
cancel()
dockerRemoveContainer(m.dockerClient, resp.ID)
return nil, err
}
cancel()
// Extend runner container timeout when optimization flags are used, as these
// pipelines may require more startup time.
runnerContainerTimeout := containerTimeout
if len(optimizationFlags) > 0 {
runnerContainerTimeout = optFlagsContainerTimeout
}
cfg := RunnerContainerConfig{
Type: Managed,
Pipeline: pipeline,
ModelID: modelID,
Endpoint: RunnerEndpoint{
URL: "http://localhost:" + containerHostPort,
},
ID: resp.ID,
GPU: gpu,
KeepWarm: keepWarm,
containerTimeout: runnerContainerTimeout,
}
rc, err := NewRunnerContainer(ctx, cfg, containerName)
if err != nil {
dockerRemoveContainer(m.dockerClient, resp.ID)
return nil, err
}
m.containers[containerName] = rc
m.gpuContainers[gpu] = containerName
return rc, nil
}
func (m *DockerManager) allocGPU(ctx context.Context) (string, error) {
// Is there a GPU available?
for _, gpu := range m.gpus {
_, ok := m.gpuContainers[gpu]
if !ok {
return gpu, nil
}
}
// Is there a GPU with an idle container?
for _, gpu := range m.gpus {
containerName := m.gpuContainers[gpu]
// If the container exists in this map then it is idle and if it not marked as keep warm we remove it
rc, ok := m.containers[containerName]
if ok && !rc.KeepWarm {
slog.Info("Removing managed container", slog.String("gpu", gpu), slog.String("name", containerName), slog.String("modelID", rc.ModelID))
delete(m.gpuContainers, gpu)
delete(m.containers, containerName)
if err := dockerRemoveContainer(m.dockerClient, rc.ID); err != nil {
return "", err
}
return gpu, nil
}
}
return "", errors.New("insufficient capacity")
}
func removeExistingContainers(ctx context.Context, client *client.Client) error {
filters := filters.NewArgs(filters.Arg("label", containerCreatorLabel+"="+containerCreator))
containers, err := client.ContainerList(ctx, container.ListOptions{All: true, Filters: filters})
if err != nil {
return err
}
for _, c := range containers {
slog.Info("Removing existing managed container", slog.String("name", c.Names[0]))
if err := dockerRemoveContainer(client, c.ID); err != nil {
return err
}
}
return nil
}
// dockerContainerName generates a unique container name based on the pipeline, model ID, and an optional suffix.
func dockerContainerName(pipeline string, modelID string, suffix ...string) string {
sanitizedModelID := strings.NewReplacer("/", "-", "_", "-").Replace(modelID)
if len(suffix) > 0 {
return fmt.Sprintf("%s_%s_%s", pipeline, sanitizedModelID, suffix[0])
}
return fmt.Sprintf("%s_%s", pipeline, sanitizedModelID)
}
func dockerRemoveContainer(client *client.Client, containerID string) error {
ctx, cancel := context.WithTimeout(context.Background(), containerRemoveTimeout)
if err := client.ContainerStop(ctx, containerID, container.StopOptions{}); err != nil {
cancel()
return err
}
cancel()
ctx, cancel = context.WithTimeout(context.Background(), containerRemoveTimeout)
defer cancel()
return client.ContainerRemove(ctx, containerID, container.RemoveOptions{})
}
func dockerWaitUntilRunning(ctx context.Context, client *client.Client, containerID string, pollingInterval time.Duration) error {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
tickerLoop:
for range ticker.C {
select {
case <-ctx.Done():
return errors.New("timed out waiting for managed container")
default:
json, err := client.ContainerInspect(ctx, containerID)
if err != nil {
return err
}
if json.State.Running {
break tickerLoop
}
}
}
return nil
}