From 70b84871fe835a8c8b6e3ba91e4193a635e803cd Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Tue, 10 Sep 2024 18:12:09 -0700 Subject: [PATCH] compute agent service affinity from available capacity (#2995) --- pkg/service/agentservice.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/pkg/service/agentservice.go b/pkg/service/agentservice.go index c7c0846c0..4762731b7 100644 --- a/pkg/service/agentservice.go +++ b/pkg/service/agentservice.go @@ -42,6 +42,8 @@ import ( "github.com/livekit/psrpc" ) +const agentWorkerLoadTarget = 0.65 + type AgentSocketUpgrader struct { websocket.Upgrader } @@ -378,20 +380,13 @@ func (h *AgentHandler) JobRequestAffinity(ctx context.Context, job *livekit.Job) defer h.mu.Unlock() var affinity float32 - var maxLoad float32 for _, w := range h.workers { if w.AgentName() != job.AgentName || w.Namespace() != job.Namespace || w.JobType() != job.Type { continue } if w.Status() == livekit.WorkerStatus_WS_AVAILABLE { - load := w.Load() - if len(w.RunningJobs()) > 0 && load > maxLoad { - maxLoad = load - affinity = 0.5 + load/2 - } else if affinity == 0 { - affinity = 0.5 - } + affinity += max(0, agentWorkerLoadTarget-w.Load()) } }