Update agents service to updated protocol (#2837)
- Deprecate namespace field - Restore former semantic of starting a job for each registered namespace, for a given Agent Name - Add agentName field - Use "dispatcher" naming convention
This commit is contained in:
@@ -20,7 +20,7 @@ require (
|
|||||||
github.com/jxskiss/base62 v1.1.0
|
github.com/jxskiss/base62 v1.1.0
|
||||||
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
|
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1
|
||||||
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7
|
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7
|
||||||
github.com/livekit/protocol v1.19.2-0.20240705134535-94a2cfe2f1ee
|
github.com/livekit/protocol v1.19.2-0.20240706015329-8c1eef4468ee
|
||||||
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a
|
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a
|
||||||
github.com/mackerelio/go-osstat v0.2.5
|
github.com/mackerelio/go-osstat v0.2.5
|
||||||
github.com/magefile/mage v1.15.0
|
github.com/magefile/mage v1.15.0
|
||||||
|
|||||||
@@ -167,8 +167,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD
|
|||||||
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
|
github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ=
|
||||||
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7 h1:F1L8inJoynwIAYpZENNYS+1xHJMF5RFRorsnAlcxfSY=
|
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7 h1:F1L8inJoynwIAYpZENNYS+1xHJMF5RFRorsnAlcxfSY=
|
||||||
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7/go.mod h1:jwKUCmObuiEDH0iiuJHaGMXwRs3RjrB4G6qqgkr/5oE=
|
github.com/livekit/mediatransportutil v0.0.0-20240625074155-301bb4a816b7/go.mod h1:jwKUCmObuiEDH0iiuJHaGMXwRs3RjrB4G6qqgkr/5oE=
|
||||||
github.com/livekit/protocol v1.19.2-0.20240705134535-94a2cfe2f1ee h1:J1U5fqAB5wJ4+Dl/DAf43Eiw+syyLTKAJoGuUj3rjQI=
|
github.com/livekit/protocol v1.19.2-0.20240706015329-8c1eef4468ee h1:t+EHiCHcxOe/hH3KZNhai+0lscBBs6HoYLg09wXAhuE=
|
||||||
github.com/livekit/protocol v1.19.2-0.20240705134535-94a2cfe2f1ee/go.mod h1:bNjJi+8frdvC84xG0CJ/7VfVvqerLg2MzjOks0ucyC4=
|
github.com/livekit/protocol v1.19.2-0.20240706015329-8c1eef4468ee/go.mod h1:bNjJi+8frdvC84xG0CJ/7VfVvqerLg2MzjOks0ucyC4=
|
||||||
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a h1:EQAHmcYEGlc6V517cQ3Iy0+jHgP6+tM/B4l2vGuLpQo=
|
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a h1:EQAHmcYEGlc6V517cQ3Iy0+jHgP6+tM/B4l2vGuLpQo=
|
||||||
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a/go.mod h1:CQUBSPfYYAaevg1TNCc6/aYsa8DJH4jSRFdCeSZk5u0=
|
github.com/livekit/psrpc v0.5.3-0.20240616012458-ac39c8549a0a/go.mod h1:CQUBSPfYYAaevg1TNCc6/aYsa8DJH4jSRFdCeSZk5u0=
|
||||||
github.com/mackerelio/go-osstat v0.2.5 h1:+MqTbZUhoIt4m8qzkVoXUJg1EuifwlAJSk4Yl2GXh+o=
|
github.com/mackerelio/go-osstat v0.2.5 h1:+MqTbZUhoIt4m8qzkVoXUJg1EuifwlAJSk4Yl2GXh+o=
|
||||||
|
|||||||
+76
-32
@@ -16,6 +16,7 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ type JobRequest struct {
|
|||||||
// only set for participant jobs
|
// only set for participant jobs
|
||||||
Participant *livekit.ParticipantInfo
|
Participant *livekit.ParticipantInfo
|
||||||
Metadata string
|
Metadata string
|
||||||
Namespace string
|
AgentName string
|
||||||
}
|
}
|
||||||
|
|
||||||
type agentClient struct {
|
type agentClient struct {
|
||||||
@@ -61,9 +62,12 @@ type agentClient struct {
|
|||||||
|
|
||||||
// cache response to avoid constantly checking with controllers
|
// cache response to avoid constantly checking with controllers
|
||||||
// cache is invalidated with AgentRegistered updates
|
// cache is invalidated with AgentRegistered updates
|
||||||
roomNamespaces *serverutils.IncrementalDispatcher[string]
|
roomNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||||
publisherNamespaces *serverutils.IncrementalDispatcher[string]
|
publisherNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||||
enabledExpiresAt time.Time
|
roomAgentNames *serverutils.IncrementalDispatcher[string]
|
||||||
|
publisherAgentNames *serverutils.IncrementalDispatcher[string]
|
||||||
|
|
||||||
|
enabledExpiresAt time.Time
|
||||||
|
|
||||||
workers *workerpool.WorkerPool
|
workers *workerpool.WorkerPool
|
||||||
|
|
||||||
@@ -96,6 +100,8 @@ func NewAgentClient(bus psrpc.MessageBus) (Client, error) {
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.roomNamespaces = nil
|
c.roomNamespaces = nil
|
||||||
c.publisherNamespaces = nil
|
c.publisherNamespaces = nil
|
||||||
|
c.roomAgentNames = nil
|
||||||
|
c.publisherAgentNames = nil
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,68 +117,80 @@ func (c *agentClient) LaunchJob(ctx context.Context, desc *JobRequest) {
|
|||||||
jobTypeTopic = PublisherAgentTopic
|
jobTypeTopic = PublisherAgentTopic
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.isNamespaceActive(desc.Namespace, desc.JobType) {
|
dispatcher := c.getDispatcher(desc.AgentName, desc.JobType)
|
||||||
logger.Infow("not dispatching agent job since no worker is available", "namespace", desc.Namespace, "jobType", desc.JobType)
|
|
||||||
|
if dispatcher == nil {
|
||||||
|
logger.Infow("not dispatching agent job since no worker is available", "agentName", desc.AgentName, "jobType", desc.JobType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.workers.Submit(func() {
|
dispatcher.ForEach(func(curNs string) {
|
||||||
_, err := c.client.JobRequest(context.Background(), desc.Namespace, jobTypeTopic, &livekit.Job{
|
topic := GetAgentTopic(desc.AgentName, curNs)
|
||||||
Id: utils.NewGuid(utils.AgentJobPrefix),
|
c.workers.Submit(func() {
|
||||||
Type: desc.JobType,
|
// The cached agent parameters do not provide the exact combination of available job type/agent name/namespace, so some of the JobRequest RPC may not trigger any worker
|
||||||
Room: desc.Room,
|
_, err := c.client.JobRequest(context.Background(), topic, jobTypeTopic, &livekit.Job{
|
||||||
Participant: desc.Participant,
|
Id: utils.NewGuid(utils.AgentJobPrefix),
|
||||||
Namespace: desc.Namespace,
|
Type: desc.JobType,
|
||||||
Metadata: desc.Metadata,
|
Room: desc.Room,
|
||||||
|
Participant: desc.Participant,
|
||||||
|
Namespace: curNs,
|
||||||
|
AgentName: desc.AgentName,
|
||||||
|
Metadata: desc.Metadata,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Infow("failed to send job request", "error", err, "namespace", curNs, "jobType", desc.JobType)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
logger.Infow("failed to send job request", "error", err, "namespace", desc.Namespace, "jobType", desc.JobType)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *agentClient) isNamespaceActive(ns string, jobType livekit.JobType) bool {
|
func (c *agentClient) getDispatcher(agName string, jobType livekit.JobType) *serverutils.IncrementalDispatcher[string] {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
|
||||||
if time.Since(c.enabledExpiresAt) > EnabledCacheTTL || c.roomNamespaces == nil || c.publisherNamespaces == nil {
|
if time.Since(c.enabledExpiresAt) > EnabledCacheTTL || c.roomNamespaces == nil ||
|
||||||
|
c.publisherNamespaces == nil || c.roomAgentNames == nil || c.publisherAgentNames == nil {
|
||||||
c.enabledExpiresAt = time.Now()
|
c.enabledExpiresAt = time.Now()
|
||||||
c.roomNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
c.roomNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
||||||
c.publisherNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
c.publisherNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
||||||
go c.checkEnabled(c.roomNamespaces, c.publisherNamespaces)
|
c.roomAgentNames = serverutils.NewIncrementalDispatcher[string]()
|
||||||
|
c.publisherAgentNames = serverutils.NewIncrementalDispatcher[string]()
|
||||||
|
|
||||||
|
go c.checkEnabled(c.roomNamespaces, c.publisherNamespaces, c.roomAgentNames, c.publisherAgentNames)
|
||||||
}
|
}
|
||||||
|
|
||||||
target := c.roomNamespaces
|
target := c.roomNamespaces
|
||||||
|
agentNames := c.roomAgentNames
|
||||||
if jobType == livekit.JobType_JT_PUBLISHER {
|
if jobType == livekit.JobType_JT_PUBLISHER {
|
||||||
target = c.publisherNamespaces
|
target = c.publisherNamespaces
|
||||||
|
agentNames = c.publisherAgentNames
|
||||||
}
|
}
|
||||||
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
done := make(chan bool, 1)
|
done := make(chan *serverutils.IncrementalDispatcher[string], 1)
|
||||||
|
c.workers.Submit(func() {
|
||||||
go func() {
|
agentNames.ForEach(func(ag string) {
|
||||||
target.ForEach(func(curNs string) {
|
if ag == agName {
|
||||||
if curNs == ns {
|
|
||||||
select {
|
select {
|
||||||
case done <- true:
|
case done <- target:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
select {
|
select {
|
||||||
case done <- false:
|
case done <- nil:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
|
|
||||||
return <-done
|
return <-done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces *serverutils.IncrementalDispatcher[string]) {
|
func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces, roomAgentNames, publisherAgentNames *serverutils.IncrementalDispatcher[string]) {
|
||||||
defer roomNamespaces.Done()
|
defer roomNamespaces.Done()
|
||||||
defer publisherNamespaces.Done()
|
defer publisherNamespaces.Done()
|
||||||
|
defer roomAgentNames.Done()
|
||||||
|
defer publisherAgentNames.Done()
|
||||||
|
|
||||||
resChan, err := c.client.CheckEnabled(context.Background(), &rpc.CheckEnabledRequest{}, psrpc.WithRequestTimeout(CheckEnabledTimeout))
|
resChan, err := c.client.CheckEnabled(context.Background(), &rpc.CheckEnabledRequest{}, psrpc.WithRequestTimeout(CheckEnabledTimeout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorw("failed to check enabled", err)
|
logger.Errorw("failed to check enabled", err)
|
||||||
@@ -181,6 +199,8 @@ func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces *serverut
|
|||||||
|
|
||||||
roomNSMap := make(map[string]bool)
|
roomNSMap := make(map[string]bool)
|
||||||
publisherNSMap := make(map[string]bool)
|
publisherNSMap := make(map[string]bool)
|
||||||
|
roomAgMap := make(map[string]bool)
|
||||||
|
publisherAgMap := make(map[string]bool)
|
||||||
|
|
||||||
for r := range resChan {
|
for r := range resChan {
|
||||||
if r.Result.GetRoomEnabled() {
|
if r.Result.GetRoomEnabled() {
|
||||||
@@ -190,6 +210,12 @@ func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces *serverut
|
|||||||
roomNSMap[ns] = true
|
roomNSMap[ns] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, ag := range r.Result.GetAgentNames() {
|
||||||
|
if _, ok := roomAgMap[ag]; !ok {
|
||||||
|
roomAgentNames.Add(ag)
|
||||||
|
roomAgMap[ag] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if r.Result.GetPublisherEnabled() {
|
if r.Result.GetPublisherEnabled() {
|
||||||
for _, ns := range r.Result.GetNamespaces() {
|
for _, ns := range r.Result.GetNamespaces() {
|
||||||
@@ -198,6 +224,12 @@ func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces *serverut
|
|||||||
publisherNSMap[ns] = true
|
publisherNSMap[ns] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, ag := range r.Result.GetAgentNames() {
|
||||||
|
if _, ok := publisherAgMap[ag]; !ok {
|
||||||
|
publisherAgentNames.Add(ag)
|
||||||
|
publisherAgMap[ag] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,3 +239,15 @@ func (c *agentClient) Stop() error {
|
|||||||
<-c.subDone
|
<-c.subDone
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAgentTopic(agentName, namespace string) string {
|
||||||
|
if agentName == "" {
|
||||||
|
// Backward compatibility
|
||||||
|
return namespace
|
||||||
|
} else if namespace == "" {
|
||||||
|
// Forward compatibility once the namespace field is removed from the worker SDK
|
||||||
|
return agentName
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s_%s", agentName, namespace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -78,6 +78,7 @@ type Worker struct {
|
|||||||
id string
|
id string
|
||||||
jobType livekit.JobType
|
jobType livekit.JobType
|
||||||
version string
|
version string
|
||||||
|
agentName string
|
||||||
name string
|
name string
|
||||||
namespace string
|
namespace string
|
||||||
load float32
|
load float32
|
||||||
@@ -161,6 +162,12 @@ func (w *Worker) Namespace() string {
|
|||||||
return w.namespace
|
return w.namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Worker) AgentName() string {
|
||||||
|
w.mu.Lock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
|
return w.agentName
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Worker) Status() livekit.WorkerStatus {
|
func (w *Worker) Status() livekit.WorkerStatus {
|
||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
@@ -318,6 +325,7 @@ func (w *Worker) handleRegister(req *livekit.RegisterWorkerRequest) {
|
|||||||
|
|
||||||
w.version = req.Version
|
w.version = req.Version
|
||||||
w.name = req.Name
|
w.name = req.Name
|
||||||
|
w.agentName = req.GetAgentName()
|
||||||
w.namespace = req.GetNamespace()
|
w.namespace = req.GetNamespace()
|
||||||
w.jobType = req.GetType()
|
w.jobType = req.GetType()
|
||||||
|
|
||||||
@@ -408,6 +416,7 @@ func (w *Worker) handleSimulateJob(simulate *livekit.SimulateJobRequest) {
|
|||||||
Room: simulate.Room,
|
Room: simulate.Room,
|
||||||
Participant: simulate.Participant,
|
Participant: simulate.Participant,
|
||||||
Namespace: w.Namespace(),
|
Namespace: w.Namespace(),
|
||||||
|
AgentName: w.AgentName(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
+8
-28
@@ -1432,34 +1432,14 @@ func (r *Room) launchPublisherAgents(p types.Participant) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ag := range r.internal.Agents {
|
for _, ag := range r.internal.AgentDispatches {
|
||||||
if ag.Type != livekit.JobType_JT_PUBLISHER {
|
go r.agentClient.LaunchJob(context.Background(), &agent.JobRequest{
|
||||||
continue
|
JobType: livekit.JobType_JT_PUBLISHER,
|
||||||
}
|
Room: r.ToProto(),
|
||||||
|
Participant: p.ToProto(),
|
||||||
var startAgent bool
|
Metadata: ag.Metadata,
|
||||||
|
AgentName: ag.AgentName,
|
||||||
if len(ag.ParticipantIdentity) == 0 {
|
})
|
||||||
// If no participant given, start for all participants
|
|
||||||
startAgent = true
|
|
||||||
} else {
|
|
||||||
for _, pi := range ag.ParticipantIdentity {
|
|
||||||
if pi == string(p.Identity()) {
|
|
||||||
startAgent = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if startAgent {
|
|
||||||
go r.agentClient.LaunchJob(context.Background(), &agent.JobRequest{
|
|
||||||
JobType: livekit.JobType_JT_PUBLISHER,
|
|
||||||
Room: r.ToProto(),
|
|
||||||
Participant: p.ToProto(),
|
|
||||||
Metadata: ag.Metadata,
|
|
||||||
Namespace: ag.Namespace,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+29
-15
@@ -94,14 +94,15 @@ type AgentHandler struct {
|
|||||||
namespaceWorkers map[workerKey][]*agent.Worker
|
namespaceWorkers map[workerKey][]*agent.Worker
|
||||||
roomKeyCount int
|
roomKeyCount int
|
||||||
publisherKeyCount int
|
publisherKeyCount int
|
||||||
// TODO remove once deprecated CheckEnabled is removed
|
namespaces []string // namespaces deprecated
|
||||||
namespaces []string
|
agentNames []string
|
||||||
|
|
||||||
roomTopic string
|
roomTopic string
|
||||||
publisherTopic string
|
publisherTopic string
|
||||||
}
|
}
|
||||||
|
|
||||||
type workerKey struct {
|
type workerKey struct {
|
||||||
|
agentName string
|
||||||
namespace string
|
namespace string
|
||||||
jobType livekit.JobType
|
jobType livekit.JobType
|
||||||
}
|
}
|
||||||
@@ -203,17 +204,18 @@ func (h *AgentHandler) HandleConnection(ctx context.Context, conn agent.SignalCo
|
|||||||
func (h *AgentHandler) HandleWorkerRegister(w *agent.Worker) {
|
func (h *AgentHandler) HandleWorkerRegister(w *agent.Worker) {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
|
|
||||||
key := workerKey{w.Namespace(), w.JobType()}
|
key := workerKey{w.AgentName(), w.Namespace(), w.JobType()}
|
||||||
|
|
||||||
workers := h.namespaceWorkers[key]
|
workers := h.namespaceWorkers[key]
|
||||||
created := len(workers) == 0
|
created := len(workers) == 0
|
||||||
|
|
||||||
if created {
|
if created {
|
||||||
topic := h.roomTopic
|
nameTopic := agent.GetAgentTopic(w.AgentName(), w.Namespace())
|
||||||
|
typeTopic := h.roomTopic
|
||||||
if w.JobType() == livekit.JobType_JT_PUBLISHER {
|
if w.JobType() == livekit.JobType_JT_PUBLISHER {
|
||||||
topic = h.publisherTopic
|
typeTopic = h.publisherTopic
|
||||||
}
|
}
|
||||||
err := h.agentServer.RegisterJobRequestTopic(w.Namespace(), topic)
|
err := h.agentServer.RegisterJobRequestTopic(nameTopic, typeTopic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
|
|
||||||
@@ -230,16 +232,19 @@ func (h *AgentHandler) HandleWorkerRegister(w *agent.Worker) {
|
|||||||
|
|
||||||
h.namespaces = append(h.namespaces, w.Namespace())
|
h.namespaces = append(h.namespaces, w.Namespace())
|
||||||
sort.Strings(h.namespaces)
|
sort.Strings(h.namespaces)
|
||||||
|
h.agentNames = append(h.agentNames, w.AgentName())
|
||||||
|
sort.Strings(h.agentNames)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h.namespaceWorkers[key] = append(workers, w)
|
h.namespaceWorkers[key] = append(workers, w)
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
|
|
||||||
if created {
|
if created {
|
||||||
h.logger.Infow("initial worker registered", "namespace", w.Namespace(), "jobType", w.JobType())
|
h.logger.Infow("initial worker registered", "namespace", w.Namespace(), "jobType", w.JobType(), "agentName", w.AgentName())
|
||||||
err := h.agentServer.PublishWorkerRegistered(context.Background(), agent.DefaultHandlerNamespace, &emptypb.Empty{})
|
err := h.agentServer.PublishWorkerRegistered(context.Background(), agent.DefaultHandlerNamespace, &emptypb.Empty{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Logger().Errorw("failed to publish worker registered", err)
|
w.Logger().Errorw("failed to publish worker registered", err, "namespace", w.Namespace(), "jobType", w.JobType(), "agentName", w.AgentName())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +253,7 @@ func (h *AgentHandler) HandleWorkerDeregister(w *agent.Worker) {
|
|||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
key := workerKey{w.Namespace(), w.JobType()}
|
key := workerKey{w.AgentName(), w.Namespace(), w.JobType()}
|
||||||
|
|
||||||
workers, ok := h.namespaceWorkers[key]
|
workers, ok := h.namespaceWorkers[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -262,25 +267,30 @@ func (h *AgentHandler) HandleWorkerDeregister(w *agent.Worker) {
|
|||||||
if len(workers) > 1 {
|
if len(workers) > 1 {
|
||||||
h.namespaceWorkers[key] = slices.Delete(workers, index, index+1)
|
h.namespaceWorkers[key] = slices.Delete(workers, index, index+1)
|
||||||
} else {
|
} else {
|
||||||
h.logger.Debugw("last worker deregistered")
|
h.logger.Debugw("last worker deregistered", "namespace", w.Namespace(), "jobType", w.JobType(), "agentName", w.AgentName())
|
||||||
delete(h.namespaceWorkers, key)
|
delete(h.namespaceWorkers, key)
|
||||||
|
|
||||||
|
topic := agent.GetAgentTopic(w.AgentName(), w.Namespace())
|
||||||
if w.JobType() == livekit.JobType_JT_ROOM {
|
if w.JobType() == livekit.JobType_JT_ROOM {
|
||||||
h.roomKeyCount--
|
h.roomKeyCount--
|
||||||
h.agentServer.DeregisterJobRequestTopic(w.Namespace(), h.roomTopic)
|
h.agentServer.DeregisterJobRequestTopic(topic, h.roomTopic)
|
||||||
} else {
|
} else {
|
||||||
h.publisherKeyCount--
|
h.publisherKeyCount--
|
||||||
h.agentServer.DeregisterJobRequestTopic(w.Namespace(), h.publisherTopic)
|
h.agentServer.DeregisterJobRequestTopic(topic, h.publisherTopic)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// agentNames and namespaces contains repeated entries for each agentNames/namespaces combinations
|
||||||
if i := slices.Index(h.namespaces, w.Namespace()); i != -1 {
|
if i := slices.Index(h.namespaces, w.Namespace()); i != -1 {
|
||||||
h.namespaces = slices.Delete(h.namespaces, i, i+1)
|
h.namespaces = slices.Delete(h.namespaces, i, i+1)
|
||||||
}
|
}
|
||||||
|
if i := slices.Index(h.agentNames, w.AgentName()); i != -1 {
|
||||||
|
h.agentNames = slices.Delete(h.agentNames, i, i+1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*emptypb.Empty, error) {
|
func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*emptypb.Empty, error) {
|
||||||
key := workerKey{job.Namespace, job.Type}
|
key := workerKey{job.AgentName, job.Namespace, job.Type}
|
||||||
attempted := make(map[*agent.Worker]struct{})
|
attempted := make(map[*agent.Worker]struct{})
|
||||||
for {
|
for {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
@@ -312,6 +322,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*empty
|
|||||||
values := []interface{}{
|
values := []interface{}{
|
||||||
"jobID", job.Id,
|
"jobID", job.Id,
|
||||||
"namespace", job.Namespace,
|
"namespace", job.Namespace,
|
||||||
|
"agentName", job.AgentName,
|
||||||
"workerID", selected.ID(),
|
"workerID", selected.ID(),
|
||||||
}
|
}
|
||||||
if job.Room != nil {
|
if job.Room != nil {
|
||||||
@@ -320,7 +331,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*empty
|
|||||||
if job.Participant != nil {
|
if job.Participant != nil {
|
||||||
values = append(values, "participant", job.Participant.Identity)
|
values = append(values, "participant", job.Participant.Identity)
|
||||||
}
|
}
|
||||||
logger.Debugw("assigning job", values...)
|
h.logger.Debugw("assigning job", values...)
|
||||||
err := selected.AssignJob(ctx, job)
|
err := selected.AssignJob(ctx, job)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, agent.ErrWorkerNotAvailable) {
|
if errors.Is(err, agent.ErrWorkerNotAvailable) {
|
||||||
@@ -340,7 +351,7 @@ func (h *AgentHandler) JobRequestAffinity(ctx context.Context, job *livekit.Job)
|
|||||||
var affinity float32
|
var affinity float32
|
||||||
var maxLoad float32
|
var maxLoad float32
|
||||||
for _, w := range h.workers {
|
for _, w := range h.workers {
|
||||||
if w.Namespace() != job.Namespace || w.JobType() != job.Type {
|
if w.AgentName() != job.AgentName || w.Namespace() != job.Namespace || w.JobType() != job.Type {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,8 +373,11 @@ func (h *AgentHandler) CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRe
|
|||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
// This doesn't return the full agentName -> namespace mapping, which can cause some unnecessary RPC.
|
||||||
|
// namespaces are however deprecated.
|
||||||
return &rpc.CheckEnabledResponse{
|
return &rpc.CheckEnabledResponse{
|
||||||
Namespaces: slices.Compact(slices.Clone(h.namespaces)),
|
Namespaces: slices.Compact(slices.Clone(h.namespaces)),
|
||||||
|
AgentNames: slices.Compact(slices.Clone(h.agentNames)),
|
||||||
RoomEnabled: h.roomKeyCount != 0,
|
RoomEnabled: h.roomKeyCount != 0,
|
||||||
PublisherEnabled: h.publisherKeyCount != 0,
|
PublisherEnabled: h.publisherKeyCount != 0,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -107,23 +107,14 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if req.Agent == nil {
|
if req.Agent == nil {
|
||||||
// Backward compatibility: by default, start any agent in the empty namespace
|
// Backward compatibility: by default, start any agent in the empty JobName
|
||||||
req.Agent = &livekit.RoomAgent{
|
req.Agent = &livekit.RoomAgent{
|
||||||
Agents: []*livekit.CreateAgentJobDefinitionRequest{
|
Dispatches: []*livekit.RoomAgentDispatch{
|
||||||
&livekit.CreateAgentJobDefinitionRequest{
|
&livekit.RoomAgentDispatch{},
|
||||||
Type: livekit.JobType_JT_ROOM,
|
|
||||||
Room: req.Name,
|
|
||||||
Namespace: "default",
|
|
||||||
},
|
|
||||||
&livekit.CreateAgentJobDefinitionRequest{
|
|
||||||
Type: livekit.JobType_JT_PUBLISHER,
|
|
||||||
Room: req.Name,
|
|
||||||
Namespace: "default",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal.Agents = req.Agent.Agents
|
internal.AgentDispatches = req.Agent.Dispatches
|
||||||
if req.MinPlayoutDelay > 0 || req.MaxPlayoutDelay > 0 {
|
if req.MinPlayoutDelay > 0 || req.MaxPlayoutDelay > 0 {
|
||||||
internal.PlayoutDelay = &livekit.PlayoutDelay{
|
internal.PlayoutDelay = &livekit.PlayoutDelay{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
|
|||||||
@@ -105,8 +105,8 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
|||||||
if created {
|
if created {
|
||||||
_, internal, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Name), true)
|
_, internal, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Name), true)
|
||||||
|
|
||||||
if internal.Agents != nil {
|
if internal.AgentDispatches != nil {
|
||||||
err = s.launchAgents(ctx, rm, internal.Agents)
|
err = s.launchAgents(ctx, rm, internal.AgentDispatches)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -130,17 +130,13 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
|||||||
return rm, nil
|
return rm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RoomService) launchAgents(ctx context.Context, rm *livekit.Room, agents []*livekit.CreateAgentJobDefinitionRequest) error {
|
func (s *RoomService) launchAgents(ctx context.Context, rm *livekit.Room, agents []*livekit.RoomAgentDispatch) error {
|
||||||
for _, ag := range agents {
|
for _, ag := range agents {
|
||||||
if ag.Type != livekit.JobType_JT_ROOM {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
||||||
JobType: ag.Type,
|
JobType: livekit.JobType_JT_ROOM,
|
||||||
Room: rm,
|
Room: rm,
|
||||||
Metadata: ag.Metadata,
|
Metadata: ag.Metadata,
|
||||||
Namespace: ag.Namespace,
|
AgentName: ag.AgentName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,7 +340,7 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
|||||||
}
|
}
|
||||||
|
|
||||||
if created {
|
if created {
|
||||||
err = s.launchAgents(ctx, room, internal.Agents)
|
err = s.launchAgents(ctx, room, internal.AgentDispatches)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -527,16 +527,12 @@ func (s *RTCService) startConnection(
|
|||||||
return connectionResult{}, nil, err
|
return connectionResult{}, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ag := range internal.Agents {
|
for _, ag := range internal.AgentDispatches {
|
||||||
if ag.Type != livekit.JobType_JT_ROOM {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
||||||
JobType: ag.Type,
|
JobType: livekit.JobType_JT_ROOM,
|
||||||
Room: cr.Room,
|
Room: cr.Room,
|
||||||
Metadata: ag.Metadata,
|
Metadata: ag.Metadata,
|
||||||
Namespace: ag.Namespace,
|
AgentName: ag.AgentName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-6
@@ -121,12 +121,10 @@ func TestAgentNamespaces(t *testing.T) {
|
|||||||
_, err = roomClient.CreateRoom(contextWithToken(createRoomToken()), &livekit.CreateRoomRequest{
|
_, err = roomClient.CreateRoom(contextWithToken(createRoomToken()), &livekit.CreateRoomRequest{
|
||||||
Name: testRoom,
|
Name: testRoom,
|
||||||
Agent: &livekit.RoomAgent{
|
Agent: &livekit.RoomAgent{
|
||||||
Agents: []*livekit.CreateAgentJobDefinitionRequest{
|
Dispatches: []*livekit.RoomAgentDispatch{
|
||||||
&livekit.CreateAgentJobDefinitionRequest{
|
&livekit.RoomAgentDispatch{},
|
||||||
Namespace: "namespace1",
|
&livekit.RoomAgentDispatch{
|
||||||
},
|
AgentName: "ag",
|
||||||
&livekit.CreateAgentJobDefinitionRequest{
|
|
||||||
Namespace: "namespace2",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user