add agent environment (#4498)
* add agent environment * lint * psrpc error * deps
This commit is contained in:
+12
-5
@@ -63,6 +63,7 @@ type JobRequest struct {
|
||||
Participant *livekit.ParticipantInfo
|
||||
Metadata string
|
||||
AgentName string
|
||||
Environment string
|
||||
}
|
||||
|
||||
type agentClient struct {
|
||||
@@ -154,7 +155,7 @@ func (c *agentClient) LaunchJob(ctx context.Context, desc *JobRequest) *serverut
|
||||
}
|
||||
|
||||
dispatcher.ForEach(func(curNs string) {
|
||||
topic := GetAgentTopic(desc.AgentName, curNs)
|
||||
topic := GetAgentTopic(desc.AgentName, curNs, desc.Environment)
|
||||
|
||||
wg.Add(1)
|
||||
c.workers.Submit(func() {
|
||||
@@ -170,6 +171,7 @@ func (c *agentClient) LaunchJob(ctx context.Context, desc *JobRequest) *serverut
|
||||
AgentName: desc.AgentName,
|
||||
Metadata: desc.Metadata,
|
||||
EnableRecording: c.config.EnableUserDataRecording,
|
||||
Environment: desc.Environment,
|
||||
}
|
||||
resp, err := c.client.JobRequest(context.Background(), topic, jobTypeTopic, job)
|
||||
if err != nil {
|
||||
@@ -326,14 +328,19 @@ func (c *agentClient) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAgentTopic(agentName, namespace string) string {
|
||||
func GetAgentTopic(agentName, namespace, environment string) string {
|
||||
var topic string
|
||||
if agentName == "" {
|
||||
// Backward compatibility
|
||||
return namespace
|
||||
topic = namespace
|
||||
} else if namespace == "" {
|
||||
// Forward compatibility once the namespace field is removed from the worker SDK
|
||||
return agentName
|
||||
topic = agentName
|
||||
} else {
|
||||
return fmt.Sprintf("%s_%s", agentName, namespace)
|
||||
topic = fmt.Sprintf("%s_%s", agentName, namespace)
|
||||
}
|
||||
if environment != "" {
|
||||
topic += "_" + environment
|
||||
}
|
||||
return topic
|
||||
}
|
||||
|
||||
+8
-2
@@ -23,7 +23,7 @@ import (
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
pagent "github.com/livekit/protocol/agent"
|
||||
protoagent "github.com/livekit/protocol/agent"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
@@ -150,6 +150,7 @@ type WorkerRegistration struct {
|
||||
JobType livekit.JobType
|
||||
Permissions *livekit.ParticipantPermission
|
||||
ClientIP string
|
||||
Environment string
|
||||
}
|
||||
|
||||
func MakeWorkerRegistration() WorkerRegistration {
|
||||
@@ -196,6 +197,10 @@ func (h *WorkerRegisterer) HandleRegister(req *livekit.RegisterWorkerRequest) er
|
||||
return ErrUnknownJobType
|
||||
}
|
||||
|
||||
if err := protoagent.ValidateEnvironment(req.GetEnvironment()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
permissions := req.AllowedPermissions
|
||||
if permissions == nil {
|
||||
permissions = &livekit.ParticipantPermission{
|
||||
@@ -211,6 +216,7 @@ func (h *WorkerRegisterer) HandleRegister(req *livekit.RegisterWorkerRequest) er
|
||||
h.registration.Namespace = req.GetNamespace()
|
||||
h.registration.JobType = req.GetType()
|
||||
h.registration.Permissions = permissions
|
||||
h.registration.Environment = req.GetEnvironment()
|
||||
h.registered = true
|
||||
|
||||
_, err := h.conn.WriteServerMessage(&livekit.ServerMessage{
|
||||
@@ -384,7 +390,7 @@ func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job) (*livekit.JobS
|
||||
}
|
||||
attributes[AgentNameAttributeKey] = w.AgentName
|
||||
|
||||
token, err := pagent.BuildAgentToken(
|
||||
token, err := protoagent.BuildAgentToken(
|
||||
w.apiKey,
|
||||
w.apiSecret,
|
||||
job.Room.Name,
|
||||
|
||||
+12
-5
@@ -27,6 +27,7 @@ import (
|
||||
"go.uber.org/atomic"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
protoagent "github.com/livekit/protocol/agent"
|
||||
"github.com/livekit/protocol/codecs/mime"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
@@ -1762,11 +1763,12 @@ func (r *Room) launchRoomAgents(ads []*agentDispatch) {
|
||||
|
||||
go func() {
|
||||
inc := r.agentClient.LaunchJob(context.Background(), &agent.JobRequest{
|
||||
JobType: livekit.JobType_JT_ROOM,
|
||||
Room: r.ToProto(),
|
||||
Metadata: ad.Metadata,
|
||||
AgentName: ad.AgentName,
|
||||
DispatchId: ad.Id,
|
||||
JobType: livekit.JobType_JT_ROOM,
|
||||
Room: r.ToProto(),
|
||||
Metadata: ad.Metadata,
|
||||
AgentName: ad.AgentName,
|
||||
DispatchId: ad.Id,
|
||||
Environment: ad.Environment,
|
||||
})
|
||||
r.handleNewJobs(ad.AgentDispatch, inc)
|
||||
done()
|
||||
@@ -1790,6 +1792,7 @@ func (r *Room) launchTargetAgents(ads []*agentDispatch, p types.Participant, job
|
||||
Metadata: ad.Metadata,
|
||||
AgentName: ad.AgentName,
|
||||
DispatchId: ad.Id,
|
||||
Environment: ad.Environment,
|
||||
})
|
||||
r.handleNewJobs(ad.AgentDispatch, inc)
|
||||
done()
|
||||
@@ -1846,12 +1849,16 @@ func (r *Room) createAgentDispatch(dispatch *livekit.AgentDispatch) (*agentDispa
|
||||
}
|
||||
|
||||
func (r *Room) createAgentDispatchFromRoomDispatch(rad *livekit.RoomAgentDispatch) (*agentDispatch, error) {
|
||||
if err := protoagent.ValidateEnvironment(rad.GetEnvironment()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.createAgentDispatch(&livekit.AgentDispatch{
|
||||
Id: guid.New(guid.AgentDispatchPrefix),
|
||||
AgentName: rad.GetAgentName(),
|
||||
Metadata: rad.GetMetadata(),
|
||||
Room: r.protoRoom.Name,
|
||||
RestartPolicy: rad.GetRestartPolicy(),
|
||||
Environment: rad.GetEnvironment(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -19,11 +19,13 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/agent"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/protocol/utils/guid"
|
||||
"github.com/livekit/psrpc"
|
||||
)
|
||||
|
||||
type AgentDispatchService struct {
|
||||
@@ -54,6 +56,10 @@ func (ag *AgentDispatchService) CreateDispatch(ctx context.Context, req *livekit
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
if err := agent.ValidateEnvironment(req.GetEnvironment()); err != nil {
|
||||
return nil, psrpc.NewError(psrpc.InvalidArgument, err)
|
||||
}
|
||||
|
||||
if ag.roomAllocator.AutoCreateEnabled(ctx) {
|
||||
err := ag.roomAllocator.SelectRoomNode(ctx, livekit.RoomName(req.Room), "")
|
||||
if err != nil {
|
||||
@@ -72,6 +78,7 @@ func (ag *AgentDispatchService) CreateDispatch(ctx context.Context, req *livekit
|
||||
Room: req.Room,
|
||||
Metadata: req.Metadata,
|
||||
RestartPolicy: req.RestartPolicy,
|
||||
Environment: req.Environment,
|
||||
}
|
||||
return ag.agentDispatchClient.CreateDispatch(ctx, ag.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), dispatch)
|
||||
}
|
||||
|
||||
@@ -158,9 +158,10 @@ type AgentHandler struct {
|
||||
}
|
||||
|
||||
type workerKey struct {
|
||||
agentName string
|
||||
namespace string
|
||||
jobType livekit.JobType
|
||||
agentName string
|
||||
namespace string
|
||||
jobType livekit.JobType
|
||||
environment string
|
||||
}
|
||||
|
||||
func NewAgentService(
|
||||
@@ -255,13 +256,13 @@ func (h *AgentHandler) registerWorker(w *agent.Worker) {
|
||||
|
||||
h.workers[w.ID] = w
|
||||
|
||||
key := workerKey{w.AgentName, w.Namespace, w.JobType}
|
||||
key := workerKey{w.AgentName, w.Namespace, w.JobType, w.Environment}
|
||||
|
||||
workers := h.namespaceWorkers[key]
|
||||
created := len(workers) == 0
|
||||
|
||||
if created {
|
||||
nameTopic := agent.GetAgentTopic(w.AgentName, w.Namespace)
|
||||
nameTopic := agent.GetAgentTopic(w.AgentName, w.Namespace, w.Environment)
|
||||
var typeTopic string
|
||||
switch w.JobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
@@ -320,7 +321,7 @@ func (h *AgentHandler) deregisterWorker(w *agent.Worker) {
|
||||
|
||||
delete(h.workers, w.ID)
|
||||
|
||||
key := workerKey{w.AgentName, w.Namespace, w.JobType}
|
||||
key := workerKey{w.AgentName, w.Namespace, w.JobType, w.Environment}
|
||||
|
||||
workers, ok := h.namespaceWorkers[key]
|
||||
if !ok {
|
||||
@@ -342,7 +343,7 @@ func (h *AgentHandler) deregisterWorker(w *agent.Worker) {
|
||||
)
|
||||
delete(h.namespaceWorkers, key)
|
||||
|
||||
topic := agent.GetAgentTopic(w.AgentName, w.Namespace)
|
||||
topic := agent.GetAgentTopic(w.AgentName, w.Namespace, w.Environment)
|
||||
|
||||
switch w.JobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
@@ -393,7 +394,7 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
|
||||
logger = logger.WithValues("participant", job.Participant.Identity)
|
||||
}
|
||||
|
||||
key := workerKey{job.AgentName, job.Namespace, job.Type}
|
||||
key := workerKey{job.AgentName, job.Namespace, job.Type, job.Environment}
|
||||
attempted := make(map[*agent.Worker]struct{})
|
||||
for {
|
||||
selected, err := h.selectWorkerWeightedByLoad(key, attempted)
|
||||
@@ -438,7 +439,7 @@ func (h *AgentHandler) JobRequestAffinity(ctx context.Context, job *livekit.Job)
|
||||
|
||||
var affinity float32
|
||||
for _, w := range h.workers {
|
||||
if w.AgentName != job.AgentName || w.Namespace != job.Namespace || w.JobType != job.Type {
|
||||
if w.AgentName != job.AgentName || w.Namespace != job.Namespace || w.JobType != job.Type || w.Environment != job.Environment {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -90,23 +90,23 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
|
||||
}
|
||||
rtcEgressLauncher := NewEgressLauncher(egressClient, ioInfoService, objectStore)
|
||||
topicFormatter := rpc.NewTopicFormatter()
|
||||
roomClient, err := rpc.NewTypedRoomClient(clientParams)
|
||||
v, err := rpc.NewTypedRoomClient(clientParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
participantClient, err := rpc.NewTypedParticipantClient(clientParams)
|
||||
v2, err := rpc.NewTypedParticipantClient(clientParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roomService, err := NewRoomService(limitConfig, apiConfig, router, roomAllocator, objectStore, rtcEgressLauncher, topicFormatter, roomClient, participantClient)
|
||||
roomService, err := NewRoomService(limitConfig, apiConfig, router, roomAllocator, objectStore, rtcEgressLauncher, topicFormatter, v, v2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
agentDispatchInternalClient, err := rpc.NewTypedAgentDispatchInternalClient(clientParams)
|
||||
v3, err := rpc.NewTypedAgentDispatchInternalClient(clientParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
agentDispatchService := NewAgentDispatchService(agentDispatchInternalClient, topicFormatter, roomAllocator, router)
|
||||
agentDispatchService := NewAgentDispatchService(v3, topicFormatter, roomAllocator, router)
|
||||
egressService := NewEgressService(egressClient, rtcEgressLauncher, ioInfoService, roomService)
|
||||
ingressConfig := getIngressConfig(conf)
|
||||
ingressClient, err := rpc.NewIngressClient(clientParams)
|
||||
@@ -121,11 +121,11 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
|
||||
}
|
||||
sipService := NewSIPService(sipConfig, nodeID, messageBus, sipClient, sipStore, roomService, telemetryService)
|
||||
rtcService := NewRTCService(conf, roomAllocator, router, telemetryService)
|
||||
whipParticipantClient, err := rpc.NewTypedWHIPParticipantClient(clientParams)
|
||||
v4, err := rpc.NewTypedWHIPParticipantClient(clientParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceWHIPService, err := NewWHIPService(conf, router, roomAllocator, clientParams, topicFormatter, whipParticipantClient)
|
||||
serviceWHIPService, err := NewWHIPService(conf, router, roomAllocator, clientParams, topicFormatter, v4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user