agent service: new protocol & namespaces (#2545)
* initial worker impl * fix test * fix build * TestAgentNamespaces * log err * nit cmt * TestAgentMultiNode * Update pkg/agent/worker.go Co-authored-by: David Zhao <dz@livekit.io> * retry on worker selection & fix review comments * Update roommanager.go * license * use testutils.WIthTimeout * abstract namespace/enabled logic into agent.Client, incrementally dispatch * typos and dates * lock * timeout is now optional * pass in topics instead of fixed * handler handles connections * onIdle, numConnections * fix WithGrants * update protocol * check agent client * broadcast after unlock * fix data race * remove ReadChan, fix dispatcher --------- Co-authored-by: David Zhao <dz@livekit.io> Co-authored-by: David Colburn <xero73@gmail.com>
This commit is contained in:
co-authored by
David Zhao
David Colburn
parent
63b1fba082
commit
dc67f505a5
+30
-21
@@ -25,7 +25,6 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type agentClient struct {
|
||||
@@ -38,11 +37,13 @@ type agentClient struct {
|
||||
participantAvailability atomic.Int32
|
||||
participantJobs atomic.Int32
|
||||
|
||||
requestedJobs chan *livekit.Job
|
||||
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func newAgentClient(token string) (*agentClient, error) {
|
||||
host := fmt.Sprintf("ws://localhost:%d", defaultServerPort)
|
||||
func newAgentClient(token string, port uint32) (*agentClient, error) {
|
||||
host := fmt.Sprintf("ws://localhost:%d", port)
|
||||
u, err := url.Parse(host + "/agent")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -57,25 +58,24 @@ func newAgentClient(token string) (*agentClient, error) {
|
||||
}
|
||||
|
||||
return &agentClient{
|
||||
conn: conn,
|
||||
done: make(chan struct{}),
|
||||
conn: conn,
|
||||
requestedJobs: make(chan *livekit.Job, 100),
|
||||
done: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *agentClient) Run(jobType livekit.JobType) (err error) {
|
||||
func (c *agentClient) Run(jobType livekit.JobType, namespace string) (err error) {
|
||||
go c.read()
|
||||
|
||||
workerID := utils.NewGuid("W_")
|
||||
|
||||
switch jobType {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
err = c.write(&livekit.WorkerMessage{
|
||||
Message: &livekit.WorkerMessage_Register{
|
||||
Register: &livekit.RegisterWorkerRequest{
|
||||
Type: livekit.JobType_JT_ROOM,
|
||||
WorkerId: workerID,
|
||||
Version: "version",
|
||||
Name: "name",
|
||||
Type: livekit.JobType_JT_ROOM,
|
||||
Version: "version",
|
||||
Namespace: &namespace,
|
||||
Name: "name",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -84,10 +84,10 @@ func (c *agentClient) Run(jobType livekit.JobType) (err error) {
|
||||
err = c.write(&livekit.WorkerMessage{
|
||||
Message: &livekit.WorkerMessage_Register{
|
||||
Register: &livekit.RegisterWorkerRequest{
|
||||
Type: livekit.JobType_JT_PUBLISHER,
|
||||
WorkerId: workerID,
|
||||
Version: "version",
|
||||
Name: "name",
|
||||
Type: livekit.JobType_JT_PUBLISHER,
|
||||
Version: "version",
|
||||
Namespace: &namespace,
|
||||
Name: "name",
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -139,6 +139,8 @@ func (c *agentClient) handleAvailability(req *livekit.AvailabilityRequest) {
|
||||
c.participantAvailability.Inc()
|
||||
}
|
||||
|
||||
c.requestedJobs <- req.Job
|
||||
|
||||
c.write(&livekit.WorkerMessage{
|
||||
Message: &livekit.WorkerMessage_Availability{
|
||||
Availability: &livekit.AvailabilityResponse{
|
||||
@@ -157,16 +159,23 @@ func (c *agentClient) write(msg *livekit.WorkerMessage) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
b, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-c.done:
|
||||
return nil
|
||||
default:
|
||||
b, err := proto.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.conn.WriteMessage(websocket.BinaryMessage, b)
|
||||
return c.conn.WriteMessage(websocket.BinaryMessage, b)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *agentClient) close() {
|
||||
c.mu.Lock()
|
||||
close(c.done)
|
||||
_ = c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
_ = c.conn.Close()
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
+133
-21
@@ -15,72 +15,184 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/testutils"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
var (
|
||||
RegisterTimeout = 2 * time.Second
|
||||
AssignJobTimeout = 3 * time.Second
|
||||
)
|
||||
|
||||
func TestAgents(t *testing.T) {
|
||||
_, finish := setupSingleNodeTest("TestAgents")
|
||||
defer finish()
|
||||
|
||||
ac1, err := newAgentClient(agentToken())
|
||||
|
||||
ac1, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
ac2, err := newAgentClient(agentToken())
|
||||
ac2, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
ac3, err := newAgentClient(agentToken())
|
||||
ac3, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
ac4, err := newAgentClient(agentToken())
|
||||
ac4, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
defer ac1.close()
|
||||
defer ac2.close()
|
||||
defer ac3.close()
|
||||
defer ac4.close()
|
||||
ac1.Run(livekit.JobType_JT_ROOM)
|
||||
ac2.Run(livekit.JobType_JT_ROOM)
|
||||
ac3.Run(livekit.JobType_JT_PUBLISHER)
|
||||
ac4.Run(livekit.JobType_JT_PUBLISHER)
|
||||
ac1.Run(livekit.JobType_JT_ROOM, "namespace")
|
||||
ac2.Run(livekit.JobType_JT_ROOM, "namespace")
|
||||
ac3.Run(livekit.JobType_JT_PUBLISHER, "namespace")
|
||||
ac4.Run(livekit.JobType_JT_PUBLISHER, "namespace")
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.registered.Load() != 1 || ac2.registered.Load() != 1 || ac3.registered.Load() != 1 || ac4.registered.Load() != 1 {
|
||||
return "worker not registered"
|
||||
}
|
||||
|
||||
require.Equal(t, int32(1), ac1.registered.Load())
|
||||
require.Equal(t, int32(1), ac2.registered.Load())
|
||||
require.Equal(t, int32(1), ac3.registered.Load())
|
||||
require.Equal(t, int32(1), ac4.registered.Load())
|
||||
return ""
|
||||
}, RegisterTimeout)
|
||||
|
||||
c1 := createRTCClient("c1", defaultServerPort, nil)
|
||||
c2 := createRTCClient("c2", defaultServerPort, nil)
|
||||
waitUntilConnected(t, c1, c2)
|
||||
|
||||
// publish 2 tracks
|
||||
t1, err := c1.AddStaticTrack("audio/opus", "audio", "webcam")
|
||||
t1, err := c1.AddStaticTrack("audio/opus", "audio", "micro")
|
||||
require.NoError(t, err)
|
||||
defer t1.Stop()
|
||||
t2, err := c1.AddStaticTrack("video/vp8", "video", "webcam")
|
||||
require.NoError(t, err)
|
||||
defer t2.Stop()
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.roomJobs.Load()+ac2.roomJobs.Load() != 1 {
|
||||
return "room job not assigned"
|
||||
}
|
||||
|
||||
require.Equal(t, int32(1), ac1.roomJobs.Load()+ac2.roomJobs.Load())
|
||||
require.Equal(t, int32(1), ac3.participantJobs.Load()+ac4.participantJobs.Load())
|
||||
if ac3.participantJobs.Load()+ac4.participantJobs.Load() != 1 {
|
||||
return fmt.Sprintf("participant jobs not assigned, ac3: %d, ac4: %d", ac3.participantJobs.Load(), ac4.participantJobs.Load())
|
||||
}
|
||||
|
||||
return ""
|
||||
}, 6 * time.Second)
|
||||
|
||||
// publish 2 tracks
|
||||
t3, err := c2.AddStaticTrack("audio/opus", "audio", "webcam")
|
||||
t3, err := c2.AddStaticTrack("audio/opus", "audio", "micro")
|
||||
require.NoError(t, err)
|
||||
defer t3.Stop()
|
||||
t4, err := c2.AddStaticTrack("video/vp8", "video", "webcam")
|
||||
require.NoError(t, err)
|
||||
defer t4.Stop()
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.roomJobs.Load()+ac2.roomJobs.Load() != 1 {
|
||||
return "room job must be assigned 1 time"
|
||||
}
|
||||
|
||||
require.Equal(t, int32(1), ac1.roomJobs.Load()+ac2.roomJobs.Load())
|
||||
require.Equal(t, int32(2), ac3.participantJobs.Load()+ac4.participantJobs.Load())
|
||||
if ac3.participantJobs.Load()+ac4.participantJobs.Load() != 2 {
|
||||
return "2 publisher jobs must assigned"
|
||||
}
|
||||
|
||||
return ""
|
||||
}, AssignJobTimeout)
|
||||
}
|
||||
|
||||
func TestAgentNamespaces(t *testing.T) {
|
||||
_, finish := setupSingleNodeTest("TestAgentNamespaces")
|
||||
defer finish()
|
||||
|
||||
ac1, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
ac2, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
defer ac1.close()
|
||||
defer ac2.close()
|
||||
ac1.Run(livekit.JobType_JT_ROOM, "namespace")
|
||||
ac2.Run(livekit.JobType_JT_ROOM, "namespace2")
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.registered.Load() != 1 || ac2.registered.Load() != 1 {
|
||||
return "worker not registered"
|
||||
}
|
||||
return ""
|
||||
}, RegisterTimeout)
|
||||
|
||||
c1 := createRTCClient("c1", defaultServerPort, nil)
|
||||
waitUntilConnected(t, c1)
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.roomJobs.Load() != 1 || ac2.roomJobs.Load() != 1 {
|
||||
return "room job not assigned"
|
||||
}
|
||||
|
||||
job1 := <-ac1.requestedJobs
|
||||
job2 := <-ac2.requestedJobs
|
||||
|
||||
if job1.Namespace != "namespace" {
|
||||
return "namespace is not 'namespace'"
|
||||
}
|
||||
|
||||
if job2.Namespace != "namespace2" {
|
||||
return "namespace is not 'namespace2'"
|
||||
}
|
||||
|
||||
if job1.Id == job2.Id {
|
||||
return "job ids are the same"
|
||||
}
|
||||
|
||||
return ""
|
||||
}, AssignJobTimeout)
|
||||
|
||||
}
|
||||
|
||||
func TestAgentMultiNode(t *testing.T) {
|
||||
_, _, finish := setupMultiNodeTest("TestAgentMultiNode")
|
||||
defer finish()
|
||||
|
||||
ac1, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
require.NoError(t, err)
|
||||
ac2, err := newAgentClient(agentToken(), defaultServerPort)
|
||||
defer ac1.close()
|
||||
defer ac2.close()
|
||||
ac1.Run(livekit.JobType_JT_ROOM, "namespace")
|
||||
ac2.Run(livekit.JobType_JT_PUBLISHER, "namespace")
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.registered.Load() != 1 || ac2.registered.Load() != 1 {
|
||||
return "worker not registered"
|
||||
}
|
||||
return ""
|
||||
}, RegisterTimeout)
|
||||
|
||||
c1 := createRTCClient("c1", secondServerPort, nil) // Create a room on the second node
|
||||
waitUntilConnected(t, c1)
|
||||
|
||||
t1, err := c1.AddStaticTrack("audio/opus", "audio", "micro")
|
||||
require.NoError(t, err)
|
||||
defer t1.Stop()
|
||||
|
||||
time.Sleep(time.Second * 10)
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if ac1.roomJobs.Load() != 1 {
|
||||
return "room job not assigned"
|
||||
}
|
||||
|
||||
if ac2.participantJobs.Load() != 1 {
|
||||
return "participant job not assigned"
|
||||
}
|
||||
|
||||
return ""
|
||||
}, AssignJobTimeout)
|
||||
}
|
||||
|
||||
func agentToken() string {
|
||||
|
||||
Reference in New Issue
Block a user