clean up legacy rpc (#2384)

* clean up legacy rpc

* cleanup

* cleanup

* cleanup

* tidy

* cleanup

* cleanup
This commit is contained in:
Paul Wells
2024-01-14 01:49:26 -08:00
committed by GitHub
parent 1cb4b3e585
commit 3f2f850bdb
13 changed files with 45 additions and 937 deletions
+2 -27
View File
@@ -101,7 +101,7 @@ func NewLocalRoomManager(
return nil, err
}
r := &RoomManager{
return &RoomManager{
config: conf,
rtcConfig: rtcConf,
currentNode: currentNode,
@@ -126,12 +126,7 @@ func NewLocalRoomManager(
Region: conf.Region,
NodeId: currentNode.Id,
},
}
// hook up to router
router.OnNewParticipantRTC(r.StartSession)
router.OnRTCMessage(r.handleRTCMessage)
return r, nil
}, nil
}
func (r *RoomManager) GetRoom(_ context.Context, roomName livekit.RoomName) *rtc.Room {
@@ -638,26 +633,6 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.LocalPa
}
}
// handles RTC messages resulted from Room API calls
func (r *RoomManager) handleRTCMessage(ctx context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity, msg *livekit.RTCNodeMessage) {
switch rm := msg.Message.(type) {
case *livekit.RTCNodeMessage_RemoveParticipant:
r.RemoveParticipant(ctx, rm.RemoveParticipant)
case *livekit.RTCNodeMessage_MuteTrack:
r.MutePublishedTrack(ctx, rm.MuteTrack)
case *livekit.RTCNodeMessage_UpdateParticipant:
r.UpdateParticipant(ctx, rm.UpdateParticipant)
case *livekit.RTCNodeMessage_DeleteRoom:
r.DeleteRoom(ctx, rm.DeleteRoom)
case *livekit.RTCNodeMessage_UpdateSubscriptions:
r.UpdateSubscriptions(ctx, rm.UpdateSubscriptions)
case *livekit.RTCNodeMessage_SendData:
r.SendData(ctx, rm.SendData)
case *livekit.RTCNodeMessage_UpdateRoomMetadata:
r.UpdateRoomMetadata(ctx, rm.UpdateRoomMetadata)
}
}
type participantReq interface {
GetRoom() string
GetIdentity() string
+9 -192
View File
@@ -16,20 +16,16 @@ package service
import (
"context"
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/thoas/go-funk"
"github.com/twitchtv/twirp"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
)
@@ -170,39 +166,7 @@ func (s *RoomService) DeleteRoom(ctx context.Context, req *livekit.DeleteRoomReq
return nil, twirpAuthError(err)
}
if s.psrpcConf.Enabled {
return s.roomClient.DeleteRoom(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
if _, _, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false); err == ErrRoomNotFound {
return nil, twirp.NotFoundError("room not found")
}
err := s.router.WriteRoomRTC(ctx, livekit.RoomName(req.Room), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_DeleteRoom{
DeleteRoom: req,
},
})
if err != nil {
return nil, err
}
// we should not return until when the room is confirmed deleted
err = s.confirmExecution(func() error {
_, _, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false)
if err == nil {
return ErrOperationFailed
} else if err != ErrRoomNotFound {
return err
} else {
return nil
}
})
if err != nil {
return nil, err
}
return &livekit.DeleteRoomResponse{}, nil
return s.roomClient.DeleteRoom(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
func (s *RoomService) ListParticipants(ctx context.Context, req *livekit.ListParticipantsRequest) (*livekit.ListParticipantsResponse, error) {
@@ -247,34 +211,7 @@ func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomPa
return nil, twirp.NotFoundError("participant not found")
}
if s.psrpcConf.Enabled {
return s.participantClient.RemoveParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
err := s.writeParticipantMessage(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_RemoveParticipant{
RemoveParticipant: req,
},
})
if err != nil {
return nil, err
}
err = s.confirmExecution(func() error {
_, err := s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err == ErrParticipantNotFound {
return nil
} else if err != nil {
return err
} else {
return ErrOperationFailed
}
})
if err != nil {
return nil, err
}
return &livekit.RemoveParticipantResponse{}, nil
return s.participantClient.RemoveParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteRoomTrackRequest) (*livekit.MuteRoomTrackResponse, error) {
@@ -283,47 +220,7 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR
return nil, twirpAuthError(err)
}
if s.psrpcConf.Enabled {
return s.participantClient.MutePublishedTrack(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
err := s.writeParticipantMessage(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_MuteTrack{
MuteTrack: req,
},
})
if err != nil {
return nil, err
}
var track *livekit.TrackInfo
err = s.confirmExecution(func() error {
p, err := s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err != nil {
return err
}
// ensure track is muted
t := funk.Find(p.Tracks, func(t *livekit.TrackInfo) bool {
return t.Sid == req.TrackSid
})
var ok bool
track, ok = t.(*livekit.TrackInfo)
if !ok {
return ErrTrackNotFound
}
if track.Muted != req.Muted {
return ErrOperationFailed
}
return nil
})
if err != nil {
return nil, err
}
res := &livekit.MuteRoomTrackResponse{
Track: track,
}
return res, nil
return s.participantClient.MutePublishedTrack(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.UpdateParticipantRequest) (*livekit.ParticipantInfo, error) {
@@ -337,42 +234,7 @@ func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.Update
return nil, twirpAuthError(err)
}
if s.psrpcConf.Enabled {
return s.participantClient.UpdateParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
err := s.writeParticipantMessage(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_UpdateParticipant{
UpdateParticipant: req,
},
})
if err != nil {
return nil, err
}
var participant *livekit.ParticipantInfo
var detailedError error
err = s.confirmExecution(func() error {
participant, err = s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err != nil {
return err
}
if req.Metadata != "" && participant.Metadata != req.Metadata {
detailedError = fmt.Errorf("metadata does not match")
return ErrOperationFailed
}
if req.Permission != nil && !proto.Equal(req.Permission, participant.Permission) {
detailedError = fmt.Errorf("permissions do not match, expected: %v, actual: %v", req.Permission, participant.Permission)
return ErrOperationFailed
}
return nil
})
if err != nil {
logger.Warnw("could not confirm participant update", detailedError)
return nil, err
}
return participant, nil
return s.participantClient.UpdateParticipant(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.UpdateSubscriptionsRequest) (*livekit.UpdateSubscriptionsResponse, error) {
@@ -386,20 +248,7 @@ func (s *RoomService) UpdateSubscriptions(ctx context.Context, req *livekit.Upda
return nil, twirpAuthError(err)
}
if s.psrpcConf.Enabled {
return s.participantClient.UpdateSubscriptions(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
err := s.writeParticipantMessage(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_UpdateSubscriptions{
UpdateSubscriptions: req,
},
})
if err != nil {
return nil, err
}
return &livekit.UpdateSubscriptionsResponse{}, nil
return s.participantClient.UpdateSubscriptions(ctx, s.topicFormatter.ParticipantTopic(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity)), req)
}
func (s *RoomService) SendData(ctx context.Context, req *livekit.SendDataRequest) (*livekit.SendDataResponse, error) {
@@ -409,20 +258,7 @@ func (s *RoomService) SendData(ctx context.Context, req *livekit.SendDataRequest
return nil, twirpAuthError(err)
}
if s.psrpcConf.Enabled {
return s.roomClient.SendData(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
err := s.router.WriteRoomRTC(ctx, roomName, &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_SendData{
SendData: req,
},
})
if err != nil {
return nil, err
}
return &livekit.SendDataResponse{}, nil
return s.roomClient.SendData(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
}
func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.UpdateRoomMetadataRequest) (*livekit.Room, error) {
@@ -451,20 +287,9 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
return nil, err
}
if s.psrpcConf.Enabled {
_, err := s.roomClient.UpdateRoomMetadata(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
if err != nil {
return nil, err
}
} else {
err = s.router.WriteRoomRTC(ctx, livekit.RoomName(req.Room), &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_UpdateRoomMetadata{
UpdateRoomMetadata: req,
},
})
if err != nil {
return nil, err
}
_, err = s.roomClient.UpdateRoomMetadata(ctx, s.topicFormatter.RoomTopic(ctx, livekit.RoomName(req.Room)), req)
if err != nil {
return nil, err
}
err = s.confirmExecution(func() error {
@@ -494,14 +319,6 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
return room, nil
}
func (s *RoomService) writeParticipantMessage(ctx context.Context, room livekit.RoomName, identity livekit.ParticipantIdentity, msg *livekit.RTCNodeMessage) error {
if err := EnsureAdminPermission(ctx, room); err != nil {
return twirpAuthError(err)
}
return s.router.WriteParticipantRTC(ctx, room, identity, msg)
}
func (s *RoomService) confirmExecution(f func() error) error {
expired := time.After(s.apiConf.ExecutionTimeout)
var err error
-20
View File
@@ -33,26 +33,6 @@ import (
)
func TestDeleteRoom(t *testing.T) {
t.Run("delete non-existent", func(t *testing.T) {
svc := newTestRoomService(config.RoomConfig{})
grant := &auth.ClaimGrants{
Video: &auth.VideoGrant{
RoomCreate: true,
},
}
ctx := service.WithGrants(context.Background(), grant)
svc.store.LoadRoomReturns(nil, nil, service.ErrRoomNotFound)
_, err := svc.DeleteRoom(ctx, &livekit.DeleteRoomRequest{
Room: "testroom",
})
require.Error(t, err)
if terr, ok := err.(twirp.Error); ok {
require.Equal(t, twirp.NotFound, terr.Code())
} else {
require.Fail(t, "should be twirp error")
}
})
t.Run("missing permissions", func(t *testing.T) {
svc := newTestRoomService(config.RoomConfig{})
grant := &auth.ClaimGrants{
+10 -20
View File
@@ -102,27 +102,17 @@ func (s *defaultSessionHandler) HandleSession(
) error {
prometheus.IncrementParticipantRtcInit(1)
if rr, ok := s.router.(*routing.RedisRouter); ok {
rtcNode, err := s.router.GetNodeForRoom(ctx, roomName)
if err != nil {
return err
}
rtcNode, err := s.router.GetNodeForRoom(ctx, roomName)
if err != nil {
return err
}
if rtcNode.Id != s.currentNode.Id {
err = routing.ErrIncorrectRTCNode
logger.Errorw("called participant on incorrect node", err,
"rtcNode", rtcNode,
)
return err
}
pKey := routing.ParticipantKeyLegacy(roomName, pi.Identity)
pKeyB62 := routing.ParticipantKey(roomName, pi.Identity)
// RTC session should start on this node
if err := rr.SetParticipantRTCNode(pKey, pKeyB62, s.currentNode.Id); err != nil {
return err
}
if rtcNode.Id != s.currentNode.Id {
err = routing.ErrIncorrectRTCNode
logger.Errorw("called participant on incorrect node", err,
"rtcNode", rtcNode,
)
return err
}
return s.roomManager.StartSession(ctx, roomName, pi, requestSource, responseSink)
-1
View File
@@ -40,7 +40,6 @@ func init() {
func TestSignal(t *testing.T) {
cfg := config.SignalRelayConfig{
Enabled: false,
RetryTimeout: 30 * time.Second,
MinRetryInterval: 500 * time.Millisecond,
MaxRetryInterval: 5 * time.Second,
+2 -2
View File
@@ -50,7 +50,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
if err != nil {
return nil, err
}
router := routing.CreateRouter(conf, universalClient, currentNode, signalClient)
router := routing.CreateRouter(universalClient, currentNode, signalClient)
objectStore := createStore(universalClient)
roomAllocator, err := NewRoomAllocator(conf, router, objectStore)
if err != nil {
@@ -149,7 +149,7 @@ func InitializeRouter(conf *config.Config, currentNode routing.LocalNode) (routi
if err != nil {
return nil, err
}
router := routing.CreateRouter(conf, universalClient, currentNode, signalClient)
router := routing.CreateRouter(universalClient, currentNode, signalClient)
return router, nil
}