add participant job type (#3443)
* add participant job type * cleanup * deps
This commit is contained in:
+55
-16
@@ -21,24 +21,32 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/workerpool"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
serverutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/psrpc"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
const (
|
||||
EnabledCacheTTL = 1 * time.Minute
|
||||
RoomAgentTopic = "room"
|
||||
PublisherAgentTopic = "publisher"
|
||||
ParticipantAgentTopic = "participant"
|
||||
DefaultHandlerNamespace = ""
|
||||
|
||||
CheckEnabledTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var jobTypeTopics = map[livekit.JobType]string{
|
||||
livekit.JobType_JT_ROOM: RoomAgentTopic,
|
||||
livekit.JobType_JT_PUBLISHER: PublisherAgentTopic,
|
||||
livekit.JobType_JT_PARTICIPANT: ParticipantAgentTopic,
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
// LaunchJob starts a room or participant job on an agent.
|
||||
// it will launch a job once for each worker in each namespace
|
||||
@@ -64,10 +72,12 @@ type agentClient struct {
|
||||
|
||||
// cache response to avoid constantly checking with controllers
|
||||
// cache is invalidated with AgentRegistered updates
|
||||
roomNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||
publisherNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||
roomAgentNames *serverutils.IncrementalDispatcher[string]
|
||||
publisherAgentNames *serverutils.IncrementalDispatcher[string]
|
||||
roomNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||
publisherNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||
participantNamespaces *serverutils.IncrementalDispatcher[string] // deprecated
|
||||
roomAgentNames *serverutils.IncrementalDispatcher[string]
|
||||
publisherAgentNames *serverutils.IncrementalDispatcher[string]
|
||||
participantAgentNames *serverutils.IncrementalDispatcher[string]
|
||||
|
||||
enabledExpiresAt time.Time
|
||||
|
||||
@@ -102,8 +112,10 @@ func NewAgentClient(bus psrpc.MessageBus) (Client, error) {
|
||||
c.mu.Lock()
|
||||
c.roomNamespaces = nil
|
||||
c.publisherNamespaces = nil
|
||||
c.participantNamespaces = nil
|
||||
c.roomAgentNames = nil
|
||||
c.publisherAgentNames = nil
|
||||
c.participantAgentNames = nil
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -114,11 +126,6 @@ func NewAgentClient(bus psrpc.MessageBus) (Client, error) {
|
||||
}
|
||||
|
||||
func (c *agentClient) LaunchJob(ctx context.Context, desc *JobRequest) *serverutils.IncrementalDispatcher[*livekit.Job] {
|
||||
jobTypeTopic := RoomAgentTopic
|
||||
if desc.JobType == livekit.JobType_JT_PUBLISHER {
|
||||
jobTypeTopic = PublisherAgentTopic
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
ret := serverutils.NewIncrementalDispatcher[*livekit.Job]()
|
||||
defer func() {
|
||||
@@ -128,6 +135,11 @@ func (c *agentClient) LaunchJob(ctx context.Context, desc *JobRequest) *serverut
|
||||
})
|
||||
}()
|
||||
|
||||
jobTypeTopic, ok := jobTypeTopics[desc.JobType]
|
||||
if !ok {
|
||||
return ret
|
||||
}
|
||||
|
||||
dispatcher := c.getDispatcher(desc.AgentName, desc.JobType)
|
||||
|
||||
if dispatcher == nil {
|
||||
@@ -186,21 +198,30 @@ func (c *agentClient) getDispatcher(agName string, jobType livekit.JobType) *ser
|
||||
c.mu.Lock()
|
||||
|
||||
if time.Since(c.enabledExpiresAt) > EnabledCacheTTL || c.roomNamespaces == nil ||
|
||||
c.publisherNamespaces == nil || c.roomAgentNames == nil || c.publisherAgentNames == nil {
|
||||
c.publisherNamespaces == nil || c.participantNamespaces == nil || c.roomAgentNames == nil || c.publisherAgentNames == nil || c.participantAgentNames == nil {
|
||||
c.enabledExpiresAt = time.Now()
|
||||
c.roomNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
||||
c.publisherNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
||||
c.participantNamespaces = serverutils.NewIncrementalDispatcher[string]()
|
||||
c.roomAgentNames = serverutils.NewIncrementalDispatcher[string]()
|
||||
c.publisherAgentNames = serverutils.NewIncrementalDispatcher[string]()
|
||||
c.participantAgentNames = serverutils.NewIncrementalDispatcher[string]()
|
||||
|
||||
go c.checkEnabled(c.roomNamespaces, c.publisherNamespaces, c.roomAgentNames, c.publisherAgentNames)
|
||||
go c.checkEnabled(c.roomNamespaces, c.publisherNamespaces, c.participantNamespaces, c.roomAgentNames, c.publisherAgentNames, c.participantAgentNames)
|
||||
}
|
||||
|
||||
target := c.roomNamespaces
|
||||
agentNames := c.roomAgentNames
|
||||
if jobType == livekit.JobType_JT_PUBLISHER {
|
||||
var target *serverutils.IncrementalDispatcher[string]
|
||||
var agentNames *serverutils.IncrementalDispatcher[string]
|
||||
switch jobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
target = c.roomNamespaces
|
||||
agentNames = c.roomAgentNames
|
||||
case livekit.JobType_JT_PUBLISHER:
|
||||
target = c.publisherNamespaces
|
||||
agentNames = c.publisherAgentNames
|
||||
case livekit.JobType_JT_PARTICIPANT:
|
||||
target = c.participantNamespaces
|
||||
agentNames = c.participantAgentNames
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
@@ -229,11 +250,13 @@ func (c *agentClient) getDispatcher(agName string, jobType livekit.JobType) *ser
|
||||
return <-done
|
||||
}
|
||||
|
||||
func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces, roomAgentNames, publisherAgentNames *serverutils.IncrementalDispatcher[string]) {
|
||||
func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces, participantNamespaces, roomAgentNames, publisherAgentNames, participantAgentNames *serverutils.IncrementalDispatcher[string]) {
|
||||
defer roomNamespaces.Done()
|
||||
defer publisherNamespaces.Done()
|
||||
defer participantNamespaces.Done()
|
||||
defer roomAgentNames.Done()
|
||||
defer publisherAgentNames.Done()
|
||||
defer participantAgentNames.Done()
|
||||
|
||||
resChan, err := c.client.CheckEnabled(context.Background(), &rpc.CheckEnabledRequest{}, psrpc.WithRequestTimeout(CheckEnabledTimeout))
|
||||
if err != nil {
|
||||
@@ -243,8 +266,10 @@ func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces, roomAgen
|
||||
|
||||
roomNSMap := make(map[string]bool)
|
||||
publisherNSMap := make(map[string]bool)
|
||||
participantNSMap := make(map[string]bool)
|
||||
roomAgMap := make(map[string]bool)
|
||||
publisherAgMap := make(map[string]bool)
|
||||
participantAgMap := make(map[string]bool)
|
||||
|
||||
for r := range resChan {
|
||||
if r.Result.GetRoomEnabled() {
|
||||
@@ -275,6 +300,20 @@ func (c *agentClient) checkEnabled(roomNamespaces, publisherNamespaces, roomAgen
|
||||
}
|
||||
}
|
||||
}
|
||||
if r.Result.GetParticipantEnabled() {
|
||||
for _, ns := range r.Result.GetNamespaces() {
|
||||
if _, ok := participantNSMap[ns]; !ok {
|
||||
participantNamespaces.Add(ns)
|
||||
participantNSMap[ns] = true
|
||||
}
|
||||
}
|
||||
for _, ag := range r.Result.GetAgentNames() {
|
||||
if _, ok := participantAgMap[ag]; !ok {
|
||||
participantAgentNames.Add(ag)
|
||||
participantAgMap[ag] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
-4
@@ -520,6 +520,8 @@ func (r *Room) Join(participant types.LocalParticipant, requestSource routing.Me
|
||||
}
|
||||
})
|
||||
|
||||
r.launchTargetAgents(maps.Values(r.agentDispatches), participant, livekit.JobType_JT_PARTICIPANT)
|
||||
|
||||
r.Logger.Debugw("new participant joined",
|
||||
"pID", participant.ID(),
|
||||
"participant", participant.Identity(),
|
||||
@@ -985,7 +987,10 @@ func (r *Room) AddAgentDispatch(dispatch *livekit.AgentDispatch) (*livekit.Agent
|
||||
r.lock.RLock()
|
||||
// launchPublisherAgents starts a goroutine to send requests, so is safe to call locked
|
||||
for _, p := range r.participants {
|
||||
r.launchPublisherAgents([]*agentDispatch{ad}, p)
|
||||
if p.IsPublisher() {
|
||||
r.launchTargetAgents([]*agentDispatch{ad}, p, livekit.JobType_JT_PUBLISHER)
|
||||
}
|
||||
r.launchTargetAgents([]*agentDispatch{ad}, p, livekit.JobType_JT_PARTICIPANT)
|
||||
}
|
||||
r.lock.RUnlock()
|
||||
|
||||
@@ -1209,7 +1214,7 @@ func (r *Room) onTrackPublished(participant types.LocalParticipant, track types.
|
||||
|
||||
if !hasPublished {
|
||||
r.lock.RLock()
|
||||
r.launchPublisherAgents(maps.Values(r.agentDispatches), participant)
|
||||
r.launchTargetAgents(maps.Values(r.agentDispatches), participant, livekit.JobType_JT_PUBLISHER)
|
||||
r.lock.RUnlock()
|
||||
if r.internal != nil && r.internal.ParticipantEgress != nil {
|
||||
go func() {
|
||||
@@ -1645,7 +1650,7 @@ func (r *Room) launchRoomAgents(ads []*agentDispatch) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) launchPublisherAgents(ads []*agentDispatch, p types.Participant) {
|
||||
func (r *Room) launchTargetAgents(ads []*agentDispatch, p types.Participant, jobType livekit.JobType) {
|
||||
if p == nil || p.IsDependent() || r.agentClient == nil {
|
||||
return
|
||||
}
|
||||
@@ -1655,7 +1660,7 @@ func (r *Room) launchPublisherAgents(ads []*agentDispatch, p types.Participant)
|
||||
|
||||
go func() {
|
||||
inc := r.agentClient.LaunchJob(context.Background(), &agent.JobRequest{
|
||||
JobType: livekit.JobType_JT_PUBLISHER,
|
||||
JobType: jobType,
|
||||
Room: r.ToProto(),
|
||||
Participant: p.ToProto(),
|
||||
Metadata: ad.Metadata,
|
||||
|
||||
+41
-17
@@ -17,6 +17,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -136,14 +137,16 @@ type AgentHandler struct {
|
||||
jobToWorker map[livekit.JobID]*agent.Worker
|
||||
keyProvider auth.KeyProvider
|
||||
|
||||
namespaceWorkers map[workerKey][]*agent.Worker
|
||||
roomKeyCount int
|
||||
publisherKeyCount int
|
||||
namespaces []string // namespaces deprecated
|
||||
agentNames []string
|
||||
namespaceWorkers map[workerKey][]*agent.Worker
|
||||
roomKeyCount int
|
||||
publisherKeyCount int
|
||||
participantKeyCount int
|
||||
namespaces []string // namespaces deprecated
|
||||
agentNames []string
|
||||
|
||||
roomTopic string
|
||||
publisherTopic string
|
||||
roomTopic string
|
||||
publisherTopic string
|
||||
participantTopic string
|
||||
}
|
||||
|
||||
type workerKey struct {
|
||||
@@ -179,6 +182,7 @@ func NewAgentService(conf *config.Config,
|
||||
serverInfo,
|
||||
agent.RoomAgentTopic,
|
||||
agent.PublisherAgentTopic,
|
||||
agent.ParticipantAgentTopic,
|
||||
)
|
||||
return s, nil
|
||||
}
|
||||
@@ -197,6 +201,7 @@ func NewAgentHandler(
|
||||
serverInfo *livekit.ServerInfo,
|
||||
roomTopic string,
|
||||
publisherTopic string,
|
||||
participantTopic string,
|
||||
) *AgentHandler {
|
||||
return &AgentHandler{
|
||||
agentServer: agentServer,
|
||||
@@ -208,6 +213,7 @@ func NewAgentHandler(
|
||||
keyProvider: keyProvider,
|
||||
roomTopic: roomTopic,
|
||||
publisherTopic: publisherTopic,
|
||||
participantTopic: participantTopic,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,10 +250,18 @@ func (h *AgentHandler) registerWorker(w *agent.Worker) {
|
||||
|
||||
if created {
|
||||
nameTopic := agent.GetAgentTopic(w.AgentName, w.Namespace)
|
||||
typeTopic := h.roomTopic
|
||||
if w.JobType == livekit.JobType_JT_PUBLISHER {
|
||||
var typeTopic string
|
||||
switch w.JobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
typeTopic = h.roomTopic
|
||||
case livekit.JobType_JT_PUBLISHER:
|
||||
typeTopic = h.publisherTopic
|
||||
case livekit.JobType_JT_PARTICIPANT:
|
||||
typeTopic = h.participantTopic
|
||||
}
|
||||
|
||||
fmt.Println(">>> register worker", typeTopic)
|
||||
|
||||
err := h.agentServer.RegisterJobRequestTopic(nameTopic, typeTopic)
|
||||
if err != nil {
|
||||
h.mu.Unlock()
|
||||
@@ -257,10 +271,13 @@ func (h *AgentHandler) registerWorker(w *agent.Worker) {
|
||||
return
|
||||
}
|
||||
|
||||
if w.JobType == livekit.JobType_JT_ROOM {
|
||||
switch w.JobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
h.roomKeyCount++
|
||||
} else {
|
||||
case livekit.JobType_JT_PUBLISHER:
|
||||
h.publisherKeyCount++
|
||||
case livekit.JobType_JT_PARTICIPANT:
|
||||
h.participantKeyCount++
|
||||
}
|
||||
|
||||
h.namespaces = append(h.namespaces, w.Namespace)
|
||||
@@ -316,12 +333,17 @@ func (h *AgentHandler) deregisterWorker(w *agent.Worker) {
|
||||
delete(h.namespaceWorkers, key)
|
||||
|
||||
topic := agent.GetAgentTopic(w.AgentName, w.Namespace)
|
||||
if w.JobType == livekit.JobType_JT_ROOM {
|
||||
|
||||
switch w.JobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
h.roomKeyCount--
|
||||
h.agentServer.DeregisterJobRequestTopic(topic, h.roomTopic)
|
||||
} else {
|
||||
case livekit.JobType_JT_PUBLISHER:
|
||||
h.publisherKeyCount--
|
||||
h.agentServer.DeregisterJobRequestTopic(topic, h.publisherTopic)
|
||||
case livekit.JobType_JT_PARTICIPANT:
|
||||
h.participantKeyCount--
|
||||
h.agentServer.DeregisterJobRequestTopic(topic, h.participantTopic)
|
||||
}
|
||||
|
||||
// agentNames and namespaces contains repeated entries for each agentNames/namespaces combinations
|
||||
@@ -352,6 +374,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
|
||||
"jobID", job.Id,
|
||||
"namespace", job.Namespace,
|
||||
"agentName", job.AgentName,
|
||||
"jobType", job.Type.String(),
|
||||
)
|
||||
if job.Room != nil {
|
||||
logger = logger.WithValues("room", job.Room.Name, "roomID", job.Room.Sid)
|
||||
@@ -441,10 +464,11 @@ func (h *AgentHandler) CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRe
|
||||
// This doesn't return the full agentName -> namespace mapping, which can cause some unnecessary RPC.
|
||||
// namespaces are however deprecated.
|
||||
return &rpc.CheckEnabledResponse{
|
||||
Namespaces: slices.Compact(slices.Clone(h.namespaces)),
|
||||
AgentNames: slices.Compact(slices.Clone(h.agentNames)),
|
||||
RoomEnabled: h.roomKeyCount != 0,
|
||||
PublisherEnabled: h.publisherKeyCount != 0,
|
||||
Namespaces: slices.Compact(slices.Clone(h.namespaces)),
|
||||
AgentNames: slices.Compact(slices.Clone(h.agentNames)),
|
||||
RoomEnabled: h.roomKeyCount != 0,
|
||||
PublisherEnabled: h.publisherKeyCount != 0,
|
||||
ParticipantEnabled: h.participantKeyCount != 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user