config rework to handle distributed setup, fixed redis bugs

This commit is contained in:
David Zhao
2021-01-17 17:06:50 -08:00
parent 30e99e8c6b
commit 458241d1f1
21 changed files with 382 additions and 104 deletions
+4 -1
View File
@@ -25,12 +25,15 @@ type ParticipantCallback func(roomId, participantId, participantName string, req
//counterfeiter:generate . Router
type Router interface {
GetNodeIdForRoom(roomName string) (string, error)
GetNodeForRoom(roomName string) (string, error)
SetNodeForRoom(roomName string, nodeId string) error
RegisterNode() error
UnregisterNode() error
GetNode(nodeId string) (*livekit.Node, error)
ListNodes() ([]*livekit.Node, error)
SetParticipantRTCNode(participantId, nodeId string) error
// functions for websocket handler
GetRequestSink(participantId string) (MessageSink, error)
GetResponseSource(participantId string) (MessageSource, error)
+11 -1
View File
@@ -25,10 +25,14 @@ func NewLocalRouter(currentNode LocalNode) *LocalRouter {
}
}
func (r *LocalRouter) GetNodeIdForRoom(roomName string) (string, error) {
func (r *LocalRouter) GetNodeForRoom(roomName string) (string, error) {
return r.currentNode.Id, nil
}
func (r *LocalRouter) SetNodeForRoom(roomName string, nodeId string) error {
return nil
}
func (r *LocalRouter) RegisterNode() error {
return nil
}
@@ -44,6 +48,12 @@ func (r *LocalRouter) GetNode(nodeId string) (*livekit.Node, error) {
return nil, ErrNotFound
}
func (r *LocalRouter) ListNodes() ([]*livekit.Node, error) {
return []*livekit.Node{
r.currentNode,
}, nil
}
func (r *LocalRouter) StartParticipant(roomName, participantId, participantName string) error {
// treat it as a new participant connecting
if r.onNewParticipant == nil {
+8 -1
View File
@@ -68,10 +68,17 @@ type RedisSink struct {
rc *redis.Client
nodeId string
participantId string
channel string
onClose func()
}
func NewRedisSink(rc *redis.Client, nodeId, participantId string) *RedisSink {
return &RedisSink{
rc: rc,
nodeId: nodeId,
participantId: participantId,
}
}
func (s *RedisSink) WriteMessage(msg proto.Message) error {
return publishRouterMessage(s.rc, s.nodeId, s.participantId, msg)
}
+56 -13
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/logger"
@@ -34,6 +35,7 @@ func NewRedisRouter(currentNode LocalNode, rc *redis.Client, useLocal bool) *Red
LocalRouter: *NewLocalRouter(currentNode),
useLocal: useLocal,
rc: rc,
redisSinks: make(map[string]*RedisSink),
}
rr.ctx, rr.cancel = context.WithCancel(context.Background())
rr.cr = utils.NewCachedRedis(rr.ctx, rr.rc)
@@ -46,7 +48,10 @@ func (r *RedisRouter) RegisterNode() error {
return err
}
r.cr.ExpireHash(NodesKey, r.currentNode.Id)
return r.rc.HSet(r.ctx, NodesKey, data).Err()
if err := r.rc.HSet(r.ctx, NodesKey, r.currentNode.Id, data).Err(); err != nil {
return errors.Wrap(err, "could not register node")
}
return nil
}
func (r *RedisRouter) UnregisterNode() error {
@@ -54,8 +59,17 @@ func (r *RedisRouter) UnregisterNode() error {
return nil
}
func (r *RedisRouter) GetNodeIdForRoom(roomName string) (string, error) {
return r.cr.CachedHGet(NodeRoomKey, roomName)
func (r *RedisRouter) GetNodeForRoom(roomName string) (string, error) {
val, err := r.cr.CachedHGet(NodeRoomKey, roomName)
if err != nil {
err = errors.Wrap(err, "could not get node for room")
}
return val, err
}
func (r *RedisRouter) SetNodeForRoom(roomName string, nodeId string) error {
// TODO: how do we clear this periodically to remove old rooms?
return r.rc.HSet(r.ctx, NodeRoomKey, roomName, nodeId).Err()
}
func (r *RedisRouter) GetNode(nodeId string) (*livekit.Node, error) {
@@ -70,9 +84,29 @@ func (r *RedisRouter) GetNode(nodeId string) (*livekit.Node, error) {
return &n, nil
}
func (r *RedisRouter) ListNodes() ([]*livekit.Node, error) {
items, err := r.rc.HVals(r.ctx, NodesKey).Result()
if err != nil {
return nil, errors.Wrap(err, "could not list nodes")
}
nodes := make([]*livekit.Node, 0, len(items))
for _, item := range items {
n := livekit.Node{}
if err := proto.Unmarshal([]byte(item), &n); err != nil {
return nil, err
}
nodes = append(nodes, &n)
}
return nodes, nil
}
func (r *RedisRouter) SetParticipantRTCNode(participantId, nodeId string) error {
r.cr.Expire(participantRTCKey(participantId))
return r.rc.Set(r.ctx, participantRTCKey(participantId), nodeId, 0).Err()
err := r.rc.Set(r.ctx, participantRTCKey(participantId), nodeId, 0).Err()
if err != nil {
err = errors.Wrap(err, "could not set rtc node")
}
return err
}
// for a local router, sink and source are pointing to the same spot
@@ -107,9 +141,10 @@ func (r *RedisRouter) GetResponseSource(participantId string) (MessageSource, er
return source, nil
}
// StartParticipant always called on the signal node
func (r *RedisRouter) StartParticipant(roomName, participantId, participantName string) error {
// find the node where the room is hosted at
rtcNode, err := r.GetNodeIdForRoom(roomName)
rtcNode, err := r.GetNodeForRoom(roomName)
if err != nil {
return err
}
@@ -123,6 +158,11 @@ func (r *RedisRouter) StartParticipant(roomName, participantId, participantName
return r.LocalRouter.StartParticipant(roomName, participantId, participantId)
}
err = r.setParticipantSignalNode(participantId, r.currentNode.Id)
if err != nil {
return err
}
// find signal node to send responses back
signalNode, err := r.getParticipantSignalNode(participantId)
if err != nil {
@@ -152,6 +192,14 @@ func (r *RedisRouter) Stop() {
r.cancel()
}
func (r *RedisRouter) setParticipantSignalNode(participantId, nodeId string) error {
r.cr.Expire(participantSignalKey(participantId))
if err := r.rc.Set(r.ctx, participantSignalKey(participantId), nodeId, 0).Err(); err != nil {
return errors.Wrap(err, "could not set signal node")
}
return nil
}
func (r *RedisRouter) getOrCreateRedisSink(nodeId string, participantId string) *RedisSink {
r.lock.RLock()
sink := r.redisSinks[participantId]
@@ -161,12 +209,7 @@ func (r *RedisRouter) getOrCreateRedisSink(nodeId string, participantId string)
return sink
}
sink = &RedisSink{
rc: r.rc,
nodeId: nodeId,
participantId: participantId,
channel: nodeChannel(nodeId),
}
sink = NewRedisSink(r.rc, nodeId, participantId)
sink.OnClose(func() {
r.lock.Lock()
delete(r.redisSinks, participantId)
@@ -175,7 +218,7 @@ func (r *RedisRouter) getOrCreateRedisSink(nodeId string, participantId string)
r.lock.Lock()
r.redisSinks[participantId] = sink
r.lock.Unlock()
return nil
return sink
}
func (r *RedisRouter) getParticipantRTCNode(participantId string) (string, error) {
@@ -235,7 +278,7 @@ func (r *RedisRouter) subscribeWorker() {
case *livekit.RouterMessage_EndSession:
signalNode, err := r.getParticipantRTCNode(pId)
if err == nil {
if err != nil {
logger.Errorw("could not get participant RTC node",
"error", err)
continue
+185 -39
View File
@@ -22,16 +22,16 @@ type FakeRouter struct {
result1 *livekit.Node
result2 error
}
GetNodeIdForRoomStub func(string) (string, error)
getNodeIdForRoomMutex sync.RWMutex
getNodeIdForRoomArgsForCall []struct {
GetNodeForRoomStub func(string) (string, error)
getNodeForRoomMutex sync.RWMutex
getNodeForRoomArgsForCall []struct {
arg1 string
}
getNodeIdForRoomReturns struct {
getNodeForRoomReturns struct {
result1 string
result2 error
}
getNodeIdForRoomReturnsOnCall map[int]struct {
getNodeForRoomReturnsOnCall map[int]struct {
result1 string
result2 error
}
@@ -61,6 +61,18 @@ type FakeRouter struct {
result1 routing.MessageSource
result2 error
}
ListNodesStub func() ([]*livekit.Node, error)
listNodesMutex sync.RWMutex
listNodesArgsForCall []struct {
}
listNodesReturns struct {
result1 []*livekit.Node
result2 error
}
listNodesReturnsOnCall map[int]struct {
result1 []*livekit.Node
result2 error
}
OnNewParticipantStub func(routing.ParticipantCallback)
onNewParticipantMutex sync.RWMutex
onNewParticipantArgsForCall []struct {
@@ -76,6 +88,18 @@ type FakeRouter struct {
registerNodeReturnsOnCall map[int]struct {
result1 error
}
SetNodeForRoomStub func(string, string) error
setNodeForRoomMutex sync.RWMutex
setNodeForRoomArgsForCall []struct {
arg1 string
arg2 string
}
setNodeForRoomReturns struct {
result1 error
}
setNodeForRoomReturnsOnCall map[int]struct {
result1 error
}
SetParticipantRTCNodeStub func(string, string) error
setParticipantRTCNodeMutex sync.RWMutex
setParticipantRTCNodeArgsForCall []struct {
@@ -193,16 +217,16 @@ func (fake *FakeRouter) GetNodeReturnsOnCall(i int, result1 *livekit.Node, resul
}{result1, result2}
}
func (fake *FakeRouter) GetNodeIdForRoom(arg1 string) (string, error) {
fake.getNodeIdForRoomMutex.Lock()
ret, specificReturn := fake.getNodeIdForRoomReturnsOnCall[len(fake.getNodeIdForRoomArgsForCall)]
fake.getNodeIdForRoomArgsForCall = append(fake.getNodeIdForRoomArgsForCall, struct {
func (fake *FakeRouter) GetNodeForRoom(arg1 string) (string, error) {
fake.getNodeForRoomMutex.Lock()
ret, specificReturn := fake.getNodeForRoomReturnsOnCall[len(fake.getNodeForRoomArgsForCall)]
fake.getNodeForRoomArgsForCall = append(fake.getNodeForRoomArgsForCall, struct {
arg1 string
}{arg1})
stub := fake.GetNodeIdForRoomStub
fakeReturns := fake.getNodeIdForRoomReturns
fake.recordInvocation("GetNodeIdForRoom", []interface{}{arg1})
fake.getNodeIdForRoomMutex.Unlock()
stub := fake.GetNodeForRoomStub
fakeReturns := fake.getNodeForRoomReturns
fake.recordInvocation("GetNodeForRoom", []interface{}{arg1})
fake.getNodeForRoomMutex.Unlock()
if stub != nil {
return stub(arg1)
}
@@ -212,46 +236,46 @@ func (fake *FakeRouter) GetNodeIdForRoom(arg1 string) (string, error) {
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeRouter) GetNodeIdForRoomCallCount() int {
fake.getNodeIdForRoomMutex.RLock()
defer fake.getNodeIdForRoomMutex.RUnlock()
return len(fake.getNodeIdForRoomArgsForCall)
func (fake *FakeRouter) GetNodeForRoomCallCount() int {
fake.getNodeForRoomMutex.RLock()
defer fake.getNodeForRoomMutex.RUnlock()
return len(fake.getNodeForRoomArgsForCall)
}
func (fake *FakeRouter) GetNodeIdForRoomCalls(stub func(string) (string, error)) {
fake.getNodeIdForRoomMutex.Lock()
defer fake.getNodeIdForRoomMutex.Unlock()
fake.GetNodeIdForRoomStub = stub
func (fake *FakeRouter) GetNodeForRoomCalls(stub func(string) (string, error)) {
fake.getNodeForRoomMutex.Lock()
defer fake.getNodeForRoomMutex.Unlock()
fake.GetNodeForRoomStub = stub
}
func (fake *FakeRouter) GetNodeIdForRoomArgsForCall(i int) string {
fake.getNodeIdForRoomMutex.RLock()
defer fake.getNodeIdForRoomMutex.RUnlock()
argsForCall := fake.getNodeIdForRoomArgsForCall[i]
func (fake *FakeRouter) GetNodeForRoomArgsForCall(i int) string {
fake.getNodeForRoomMutex.RLock()
defer fake.getNodeForRoomMutex.RUnlock()
argsForCall := fake.getNodeForRoomArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeRouter) GetNodeIdForRoomReturns(result1 string, result2 error) {
fake.getNodeIdForRoomMutex.Lock()
defer fake.getNodeIdForRoomMutex.Unlock()
fake.GetNodeIdForRoomStub = nil
fake.getNodeIdForRoomReturns = struct {
func (fake *FakeRouter) GetNodeForRoomReturns(result1 string, result2 error) {
fake.getNodeForRoomMutex.Lock()
defer fake.getNodeForRoomMutex.Unlock()
fake.GetNodeForRoomStub = nil
fake.getNodeForRoomReturns = struct {
result1 string
result2 error
}{result1, result2}
}
func (fake *FakeRouter) GetNodeIdForRoomReturnsOnCall(i int, result1 string, result2 error) {
fake.getNodeIdForRoomMutex.Lock()
defer fake.getNodeIdForRoomMutex.Unlock()
fake.GetNodeIdForRoomStub = nil
if fake.getNodeIdForRoomReturnsOnCall == nil {
fake.getNodeIdForRoomReturnsOnCall = make(map[int]struct {
func (fake *FakeRouter) GetNodeForRoomReturnsOnCall(i int, result1 string, result2 error) {
fake.getNodeForRoomMutex.Lock()
defer fake.getNodeForRoomMutex.Unlock()
fake.GetNodeForRoomStub = nil
if fake.getNodeForRoomReturnsOnCall == nil {
fake.getNodeForRoomReturnsOnCall = make(map[int]struct {
result1 string
result2 error
})
}
fake.getNodeIdForRoomReturnsOnCall[i] = struct {
fake.getNodeForRoomReturnsOnCall[i] = struct {
result1 string
result2 error
}{result1, result2}
@@ -385,6 +409,62 @@ func (fake *FakeRouter) GetResponseSourceReturnsOnCall(i int, result1 routing.Me
}{result1, result2}
}
func (fake *FakeRouter) ListNodes() ([]*livekit.Node, error) {
fake.listNodesMutex.Lock()
ret, specificReturn := fake.listNodesReturnsOnCall[len(fake.listNodesArgsForCall)]
fake.listNodesArgsForCall = append(fake.listNodesArgsForCall, struct {
}{})
stub := fake.ListNodesStub
fakeReturns := fake.listNodesReturns
fake.recordInvocation("ListNodes", []interface{}{})
fake.listNodesMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeRouter) ListNodesCallCount() int {
fake.listNodesMutex.RLock()
defer fake.listNodesMutex.RUnlock()
return len(fake.listNodesArgsForCall)
}
func (fake *FakeRouter) ListNodesCalls(stub func() ([]*livekit.Node, error)) {
fake.listNodesMutex.Lock()
defer fake.listNodesMutex.Unlock()
fake.ListNodesStub = stub
}
func (fake *FakeRouter) ListNodesReturns(result1 []*livekit.Node, result2 error) {
fake.listNodesMutex.Lock()
defer fake.listNodesMutex.Unlock()
fake.ListNodesStub = nil
fake.listNodesReturns = struct {
result1 []*livekit.Node
result2 error
}{result1, result2}
}
func (fake *FakeRouter) ListNodesReturnsOnCall(i int, result1 []*livekit.Node, result2 error) {
fake.listNodesMutex.Lock()
defer fake.listNodesMutex.Unlock()
fake.ListNodesStub = nil
if fake.listNodesReturnsOnCall == nil {
fake.listNodesReturnsOnCall = make(map[int]struct {
result1 []*livekit.Node
result2 error
})
}
fake.listNodesReturnsOnCall[i] = struct {
result1 []*livekit.Node
result2 error
}{result1, result2}
}
func (fake *FakeRouter) OnNewParticipant(arg1 routing.ParticipantCallback) {
fake.onNewParticipantMutex.Lock()
fake.onNewParticipantArgsForCall = append(fake.onNewParticipantArgsForCall, struct {
@@ -470,6 +550,68 @@ func (fake *FakeRouter) RegisterNodeReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRouter) SetNodeForRoom(arg1 string, arg2 string) error {
fake.setNodeForRoomMutex.Lock()
ret, specificReturn := fake.setNodeForRoomReturnsOnCall[len(fake.setNodeForRoomArgsForCall)]
fake.setNodeForRoomArgsForCall = append(fake.setNodeForRoomArgsForCall, struct {
arg1 string
arg2 string
}{arg1, arg2})
stub := fake.SetNodeForRoomStub
fakeReturns := fake.setNodeForRoomReturns
fake.recordInvocation("SetNodeForRoom", []interface{}{arg1, arg2})
fake.setNodeForRoomMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeRouter) SetNodeForRoomCallCount() int {
fake.setNodeForRoomMutex.RLock()
defer fake.setNodeForRoomMutex.RUnlock()
return len(fake.setNodeForRoomArgsForCall)
}
func (fake *FakeRouter) SetNodeForRoomCalls(stub func(string, string) error) {
fake.setNodeForRoomMutex.Lock()
defer fake.setNodeForRoomMutex.Unlock()
fake.SetNodeForRoomStub = stub
}
func (fake *FakeRouter) SetNodeForRoomArgsForCall(i int) (string, string) {
fake.setNodeForRoomMutex.RLock()
defer fake.setNodeForRoomMutex.RUnlock()
argsForCall := fake.setNodeForRoomArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeRouter) SetNodeForRoomReturns(result1 error) {
fake.setNodeForRoomMutex.Lock()
defer fake.setNodeForRoomMutex.Unlock()
fake.SetNodeForRoomStub = nil
fake.setNodeForRoomReturns = struct {
result1 error
}{result1}
}
func (fake *FakeRouter) SetNodeForRoomReturnsOnCall(i int, result1 error) {
fake.setNodeForRoomMutex.Lock()
defer fake.setNodeForRoomMutex.Unlock()
fake.SetNodeForRoomStub = nil
if fake.setNodeForRoomReturnsOnCall == nil {
fake.setNodeForRoomReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.setNodeForRoomReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeRouter) SetParticipantRTCNode(arg1 string, arg2 string) error {
fake.setParticipantRTCNodeMutex.Lock()
ret, specificReturn := fake.setParticipantRTCNodeReturnsOnCall[len(fake.setParticipantRTCNodeArgsForCall)]
@@ -730,16 +872,20 @@ func (fake *FakeRouter) Invocations() map[string][][]interface{} {
defer fake.invocationsMutex.RUnlock()
fake.getNodeMutex.RLock()
defer fake.getNodeMutex.RUnlock()
fake.getNodeIdForRoomMutex.RLock()
defer fake.getNodeIdForRoomMutex.RUnlock()
fake.getNodeForRoomMutex.RLock()
defer fake.getNodeForRoomMutex.RUnlock()
fake.getRequestSinkMutex.RLock()
defer fake.getRequestSinkMutex.RUnlock()
fake.getResponseSourceMutex.RLock()
defer fake.getResponseSourceMutex.RUnlock()
fake.listNodesMutex.RLock()
defer fake.listNodesMutex.RUnlock()
fake.onNewParticipantMutex.RLock()
defer fake.onNewParticipantMutex.RUnlock()
fake.registerNodeMutex.RLock()
defer fake.registerNodeMutex.RUnlock()
fake.setNodeForRoomMutex.RLock()
defer fake.setNodeForRoomMutex.RUnlock()
fake.setParticipantRTCNodeMutex.RLock()
defer fake.setParticipantRTCNodeMutex.RUnlock()
fake.startMutex.RLock()