Skip to content

Commit

Permalink
feat(worker): add HasCapability method (#73)
Browse files Browse the repository at this point in the history
This commit adds a HasCapability method to the worker that can be used to
check if the worker has enough capacity to take a request.
  • Loading branch information
rickstaa committed May 3, 2024
1 parent 15bbf4f commit 5fd6e76
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions worker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ func (m *DockerManager) Return(rc *RunnerContainer) {
m.containers[dockerContainerName(rc.Pipeline, rc.ModelID)] = 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 {
containerName := dockerContainerName(pipeline, modelID)

m.mu.Lock()
defer m.mu.Unlock()

// Check if unused managed container exists for the requested model.
_, ok := m.containers[containerName]
if ok {
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) {
containerName := dockerContainerName(pipeline, modelID)

Expand Down
16 changes: 16 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ func (w *Worker) Stop(ctx context.Context) error {
return nil
}

// HasCapacity returns true if the worker has capacity for the given pipeline and model ID.
func (w *Worker) HasCapacity(pipeline, modelID string) bool {
managedCapacity := w.manager.HasCapacity(context.Background(), pipeline, modelID)
if managedCapacity {
return true
}

// Check if we have capacity for external containers.
name := dockerContainerName(pipeline, modelID)
w.mu.Lock()
defer w.mu.Unlock()
_, ok := w.externalContainers[name]

return ok
}

func (w *Worker) borrowContainer(ctx context.Context, pipeline, modelID string) (*RunnerContainer, error) {
w.mu.Lock()

Expand Down

0 comments on commit 5fd6e76

Please sign in to comment.