rework multi-node routing, cleaner design for reconnection

This commit is contained in:
David Zhao
2021-01-30 22:24:08 -08:00
parent 28e73bd109
commit 2cf2c448be
23 changed files with 702 additions and 661 deletions
+4 -27
View File
@@ -6,7 +6,6 @@ import (
"github.com/thoas/go-funk"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -16,17 +15,14 @@ type LocalRoomStore struct {
rooms map[string]*livekit.Room
// map of roomName => roomId
roomIds map[string]string
// map of roomName => { participantName: participantId }
participantIds map[string]map[string]string
lock sync.RWMutex
lock sync.RWMutex
}
func NewLocalRoomStore() *LocalRoomStore {
return &LocalRoomStore{
rooms: make(map[string]*livekit.Room),
roomIds: make(map[string]string),
participantIds: make(map[string]map[string]string),
lock: sync.RWMutex{},
rooms: make(map[string]*livekit.Room),
roomIds: make(map[string]string),
lock: sync.RWMutex{},
}
}
@@ -77,22 +73,3 @@ func (p *LocalRoomStore) DeleteRoom(idOrName string) error {
delete(p.roomIds, room.Name)
return nil
}
func (p *LocalRoomStore) GetParticipantId(room, name string) (string, error) {
p.lock.Lock()
defer p.lock.Unlock()
roomParticipantIds := p.participantIds[room]
if roomParticipantIds == nil {
p.participantIds[room] = make(map[string]string)
roomParticipantIds = p.participantIds[room]
}
pId := roomParticipantIds[name]
if pId == "" {
pId = utils.NewGuid(utils.ParticipantPrefix)
roomParticipantIds[name] = pId
}
return pId, nil
}
-31
View File
@@ -1,31 +0,0 @@
package service_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/service"
)
func TestLocalRoomStore_GetParticipantId(t *testing.T) {
s := service.NewLocalRoomStore()
id, _ := s.GetParticipantId("room1", "p1")
assert.NotEmpty(t, id)
t.Run("diff room, same name returns a new ID", func(t *testing.T) {
id2, _ := s.GetParticipantId("room2", "p1")
assert.NotEmpty(t, id2)
assert.NotEqual(t, id, id2)
})
t.Run("same room returns identical id", func(t *testing.T) {
id2, _ := s.GetParticipantId("room1", "p1")
assert.Equal(t, id, id2)
})
t.Run("same room with different name", func(t *testing.T) {
id2, _ := s.GetParticipantId("room1", "p2")
assert.NotEqual(t, id, id2)
})
}
+6 -26
View File
@@ -8,7 +8,6 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -18,12 +17,12 @@ const (
// hash of room_id => room name
RoomIdMap = "room_id_map"
// hash of participant_name => participant_id
// a key for each room, with expiration
RoomParticipantMapPrefix = "participant_map:room:"
participantMappingTTL = 24 * time.Hour
//
//// hash of participant_name => participant_id
//// a key for each room, with expiration
//RoomParticipantMapPrefix = "participant_map:room:"
//
//participantMappingTTL = 24 * time.Hour
)
type RedisRoomStore struct {
@@ -112,26 +111,7 @@ func (p *RedisRoomStore) DeleteRoom(idOrName string) error {
pp := p.rc.Pipeline()
pp.HDel(p.ctx, RoomIdMap, room.Sid)
pp.HDel(p.ctx, RoomsKey, room.Name)
pp.HDel(p.ctx, RoomParticipantMapPrefix+room.Name)
_, err = pp.Exec(p.ctx)
return err
}
func (p *RedisRoomStore) GetParticipantId(room, name string) (string, error) {
key := RoomParticipantMapPrefix + room
pId, err := p.rc.HGet(p.ctx, key, name).Result()
if err == redis.Nil {
// create
pId = utils.NewGuid(utils.ParticipantPrefix)
pp := p.rc.Pipeline()
pp.HSet(p.ctx, key, name, pId)
pp.Expire(p.ctx, key, participantMappingTTL)
_, err = pp.Exec(p.ctx)
} else if err != nil {
return "", err
}
return pId, err
}
+21 -3
View File
@@ -79,6 +79,7 @@ func (r *RoomManager) CreateRoom(req *livekit.CreateRoomRequest) (*livekit.Room,
// DeleteRoom completely deletes all room information, including active sessions, room store, and routing info
func (r *RoomManager) DeleteRoom(roomName string) error {
logger.Infow("deleting room state", "room", roomName)
r.lock.Lock()
delete(r.rooms, roomName)
r.lock.Unlock()
@@ -124,17 +125,34 @@ func (r *RoomManager) Cleanup() error {
}
// starts WebRTC session when a new participant is connected, takes place on RTC node
func (r *RoomManager) StartSession(roomName, participantId, participantName string, requestSource routing.MessageSource, responseSink routing.MessageSink) {
func (r *RoomManager) StartSession(roomName, identity string, requestSource routing.MessageSource, responseSink routing.MessageSink) {
room, err := r.getOrCreateRoom(roomName)
if err != nil {
logger.Errorw("could not create room", "error", err)
return
}
// Use existing peer connection if it's already connected, perhaps from a different signal connection
participant := room.GetParticipant(identity)
if participant != nil {
logger.Debugw("resuming RTC session",
"room", roomName,
"node", r.currentNode.Id,
"participant", identity,
)
// close previous sink, and link to new one
prevSink := participant.GetResponseSink()
if prevSink != nil {
prevSink.Close()
}
participant.SetResponseSink(responseSink)
return
}
logger.Debugw("starting RTC session",
"room", roomName,
"node", r.currentNode.Id,
"participant", participantName,
"participant", identity,
"num_participants", len(room.GetParticipants()),
)
@@ -144,7 +162,7 @@ func (r *RoomManager) StartSession(roomName, participantId, participantName stri
return
}
participant, err := rtc.NewParticipant(participantId, participantName, pc, responseSink, r.config.Receiver)
participant, err = rtc.NewParticipant(identity, pc, responseSink, r.config.Receiver)
if err != nil {
logger.Errorw("could not create participant", "error", err)
return
-2
View File
@@ -14,6 +14,4 @@ type RoomStore interface {
GetRoom(idOrName string) (*livekit.Room, error)
ListRooms() ([]*livekit.Room, error)
DeleteRoom(idOrName string) error
// returns the current participant id in room, or create new one
GetParticipantId(room, name string) (string, error)
}
+13 -22
View File
@@ -48,7 +48,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if claims == nil || claims.Video == nil {
handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
}
pName := claims.Identity
identity := claims.Identity
onlyName, err := EnsureJoinPermission(r.Context())
if err != nil {
@@ -67,35 +67,21 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
participantId, err := s.roomManager.roomStore.GetParticipantId(roomName, pName)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not get participant ID: "+err.Error())
return
}
err = s.router.StartParticipantSignal(roomName, participantId, pName)
// this needs to be started first *before* using router functions on this node
reqSink, resSource, err := s.router.StartParticipantSignal(roomName, identity)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
return
}
reqSink, err := s.router.GetRequestSink(participantId)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not get request sink"+err.Error())
return
}
logger.Debugw("started participant signal", "sink", reqSink, "source", resSource)
done := make(chan bool, 1)
defer func() {
logger.Infow("WS connection closed", "participant", pName)
logger.Infow("WS connection closed", "participant", identity)
reqSink.Close()
close(done)
}()
resSource, err := s.router.GetResponseSource(participantId)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not get response source"+err.Error())
return
}
// upgrade only once the basics are good to go
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
@@ -110,12 +96,17 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger.Infow("new client connected",
"room", rm.Sid,
"roomName", rm.Name,
"name", pName,
"resSource", fmt.Sprintf("%p", resSource),
"name", identity,
)
// handle responses
go func() {
defer func() {
// when the source is terminated, this means Participant.Close had been called and RTC connection is done
// we would terminate the signal connection as well
conn.Close()
}()
defer rtc.Recover()
for {
select {
case <-done:
@@ -31,20 +31,6 @@ type FakeRoomStore struct {
deleteRoomReturnsOnCall map[int]struct {
result1 error
}
GetParticipantIdStub func(string, string) (string, error)
getParticipantIdMutex sync.RWMutex
getParticipantIdArgsForCall []struct {
arg1 string
arg2 string
}
getParticipantIdReturns struct {
result1 string
result2 error
}
getParticipantIdReturnsOnCall map[int]struct {
result1 string
result2 error
}
GetRoomStub func(string) (*livekit.Room, error)
getRoomMutex sync.RWMutex
getRoomArgsForCall []struct {
@@ -196,71 +182,6 @@ func (fake *FakeRoomStore) DeleteRoomReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRoomStore) GetParticipantId(arg1 string, arg2 string) (string, error) {
fake.getParticipantIdMutex.Lock()
ret, specificReturn := fake.getParticipantIdReturnsOnCall[len(fake.getParticipantIdArgsForCall)]
fake.getParticipantIdArgsForCall = append(fake.getParticipantIdArgsForCall, struct {
arg1 string
arg2 string
}{arg1, arg2})
stub := fake.GetParticipantIdStub
fakeReturns := fake.getParticipantIdReturns
fake.recordInvocation("GetParticipantId", []interface{}{arg1, arg2})
fake.getParticipantIdMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1, ret.result2
}
return fakeReturns.result1, fakeReturns.result2
}
func (fake *FakeRoomStore) GetParticipantIdCallCount() int {
fake.getParticipantIdMutex.RLock()
defer fake.getParticipantIdMutex.RUnlock()
return len(fake.getParticipantIdArgsForCall)
}
func (fake *FakeRoomStore) GetParticipantIdCalls(stub func(string, string) (string, error)) {
fake.getParticipantIdMutex.Lock()
defer fake.getParticipantIdMutex.Unlock()
fake.GetParticipantIdStub = stub
}
func (fake *FakeRoomStore) GetParticipantIdArgsForCall(i int) (string, string) {
fake.getParticipantIdMutex.RLock()
defer fake.getParticipantIdMutex.RUnlock()
argsForCall := fake.getParticipantIdArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeRoomStore) GetParticipantIdReturns(result1 string, result2 error) {
fake.getParticipantIdMutex.Lock()
defer fake.getParticipantIdMutex.Unlock()
fake.GetParticipantIdStub = nil
fake.getParticipantIdReturns = struct {
result1 string
result2 error
}{result1, result2}
}
func (fake *FakeRoomStore) GetParticipantIdReturnsOnCall(i int, result1 string, result2 error) {
fake.getParticipantIdMutex.Lock()
defer fake.getParticipantIdMutex.Unlock()
fake.GetParticipantIdStub = nil
if fake.getParticipantIdReturnsOnCall == nil {
fake.getParticipantIdReturnsOnCall = make(map[int]struct {
result1 string
result2 error
})
}
fake.getParticipantIdReturnsOnCall[i] = struct {
result1 string
result2 error
}{result1, result2}
}
func (fake *FakeRoomStore) GetRoom(arg1 string) (*livekit.Room, error) {
fake.getRoomMutex.Lock()
ret, specificReturn := fake.getRoomReturnsOnCall[len(fake.getRoomArgsForCall)]
@@ -388,8 +309,6 @@ func (fake *FakeRoomStore) Invocations() map[string][][]interface{} {
defer fake.createRoomMutex.RUnlock()
fake.deleteRoomMutex.RLock()
defer fake.deleteRoomMutex.RUnlock()
fake.getParticipantIdMutex.RLock()
defer fake.getParticipantIdMutex.RUnlock()
fake.getRoomMutex.RLock()
defer fake.getRoomMutex.RUnlock()
fake.listRoomsMutex.RLock()