feat: SubscriptionManager to consolidate subscription handling (#1317)

Added a new manager to handle all subscription needs. Implemented using reconciler pattern. The goals are:

improve subscription resilience by separating desired state and current state
reduce complexity of synchronous processing
better detect failures with the ability to trigger full reconnect
This commit is contained in:
David Zhao
2023-01-24 23:06:16 -08:00
committed by GitHub
parent c696626fe8
commit cd6b8b80b9
31 changed files with 2625 additions and 2208 deletions
+32 -4
View File
@@ -246,6 +246,8 @@ func (t *telemetryService) TrackSubscribeRequested(
publisher *livekit.ParticipantInfo,
) {
t.enqueue(func() {
prometheus.RecordTrackSubscribeAttempt()
room := t.getRoomDetails(participantID)
ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBE_REQUESTED, room, participantID, track)
ev.Publisher = publisher
@@ -260,7 +262,7 @@ func (t *telemetryService) TrackSubscribed(
publisher *livekit.ParticipantInfo,
) {
t.enqueue(func() {
prometheus.AddSubscribedTrack(track.Type.String())
prometheus.RecordTrackSubscribeSuccess(track.Type.String())
room := t.getRoomDetails(participantID)
ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBED, room, participantID, track)
@@ -269,12 +271,38 @@ func (t *telemetryService) TrackSubscribed(
})
}
func (t *telemetryService) TrackUnsubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo) {
func (t *telemetryService) TrackSubscribeFailed(
ctx context.Context,
participantID livekit.ParticipantID,
trackID livekit.TrackID,
err error,
isUserError bool,
) {
t.enqueue(func() {
prometheus.SubSubscribedTrack(track.Type.String())
prometheus.RecordTrackSubscribeFailure(err, isUserError)
room := t.getRoomDetails(participantID)
t.SendEvent(ctx, newTrackEvent(livekit.AnalyticsEventType_TRACK_UNSUBSCRIBED, room, participantID, track))
ev := newTrackEvent(livekit.AnalyticsEventType_TRACK_SUBSCRIBE_FAILED, room, participantID, &livekit.TrackInfo{
Sid: string(trackID),
})
ev.Error = err.Error()
t.SendEvent(ctx, ev)
})
}
func (t *telemetryService) TrackUnsubscribed(
ctx context.Context,
participantID livekit.ParticipantID,
track *livekit.TrackInfo,
shouldSendEvent bool,
) {
t.enqueue(func() {
prometheus.RecordTrackUnsubscribed(track.Type.String())
if shouldSendEvent {
room := t.getRoomDetails(participantID)
t.SendEvent(ctx, newTrackEvent(livekit.AnalyticsEventType_TRACK_UNSUBSCRIBED, room, participantID, track))
}
})
}
+5 -5
View File
@@ -156,10 +156,10 @@ func GetUpdatedNodeStats(prev *livekit.NodeStats, prevAverage *livekit.NodeStats
stats := &livekit.NodeStats{
StartedAt: prev.StartedAt,
UpdatedAt: updatedAt,
NumRooms: roomTotal.Load(),
NumClients: participantTotal.Load(),
NumTracksIn: trackPublishedTotal.Load(),
NumTracksOut: trackSubscribedTotal.Load(),
NumRooms: roomCurrent.Load(),
NumClients: participantCurrent.Load(),
NumTracksIn: trackPublishedCurrent.Load(),
NumTracksOut: trackSubscribedCurrent.Load(),
NumTrackPublishAttempts: trackPublishAttemptsNow,
NumTrackPublishSuccess: trackPublishSuccessNow,
NumTrackSubscribeAttempts: trackSubscribeAttemptsNow,
@@ -222,7 +222,7 @@ func GetUpdatedNodeStats(prev *livekit.NodeStats, prevAverage *livekit.NodeStats
if packetTotal == 0 {
stats.SysPacketsDroppedPctPerSec = 0
} else {
stats.SysPacketsDroppedPctPerSec = float32(stats.SysPacketsDroppedPerSec) / float32(packetTotal)
stats.SysPacketsDroppedPctPerSec = stats.SysPacketsDroppedPerSec / packetTotal
}
promSysDroppedPacketPctGauge.Set(float64(stats.SysPacketsDroppedPctPerSec))
}
+60 -48
View File
@@ -10,26 +10,29 @@ import (
)
var (
roomTotal atomic.Int32
participantTotal atomic.Int32
trackPublishedTotal atomic.Int32
trackSubscribedTotal atomic.Int32
roomCurrent atomic.Int32
participantCurrent atomic.Int32
trackPublishedCurrent atomic.Int32
trackSubscribedCurrent atomic.Int32
trackPublishAttempts atomic.Int32
trackPublishSuccess atomic.Int32
trackSubscribeAttempts atomic.Int32
trackSubscribeSuccess atomic.Int32
// count the number of failures that are due to user error (permissions, track doesn't exist), so we could compute
// success rate by subtracting this from total attempts
trackSubscribeUserError atomic.Int32
promRoomTotal prometheus.Gauge
promRoomDuration prometheus.Histogram
promParticipantTotal prometheus.Gauge
promTrackPublishedTotal *prometheus.GaugeVec
promTrackSubscribedTotal *prometheus.GaugeVec
promTrackPublishCounter *prometheus.CounterVec
promTrackSubscribeCounter *prometheus.CounterVec
promRoomCurrent prometheus.Gauge
promRoomDuration prometheus.Histogram
promParticipantCurrent prometheus.Gauge
promTrackPublishedCurrent *prometheus.GaugeVec
promTrackSubscribedCurrent *prometheus.GaugeVec
promTrackPublishCounter *prometheus.CounterVec
promTrackSubscribeCounter *prometheus.CounterVec
)
func initRoomStats(nodeID string, nodeType livekit.NodeType) {
promRoomTotal = prometheus.NewGauge(prometheus.GaugeOpts{
promRoomCurrent = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "room",
Name: "total",
@@ -44,19 +47,19 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType) {
5, 10, 60, 5 * 60, 10 * 60, 30 * 60, 60 * 60, 2 * 60 * 60, 5 * 60 * 60, 10 * 60 * 60,
},
})
promParticipantTotal = prometheus.NewGauge(prometheus.GaugeOpts{
promParticipantCurrent = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "participant",
Name: "total",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()},
})
promTrackPublishedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
promTrackPublishedCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "track",
Name: "published_total",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()},
}, []string{"kind"})
promTrackSubscribedTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
promTrackSubscribedCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: livekitNamespace,
Subsystem: "track",
Name: "subscribed_total",
@@ -73,48 +76,48 @@ func initRoomStats(nodeID string, nodeType livekit.NodeType) {
Subsystem: "track",
Name: "subscribe_counter",
ConstLabels: prometheus.Labels{"node_id": nodeID, "node_type": nodeType.String()},
}, []string{"kind", "state"})
}, []string{"state", "error"})
prometheus.MustRegister(promRoomTotal)
prometheus.MustRegister(promRoomCurrent)
prometheus.MustRegister(promRoomDuration)
prometheus.MustRegister(promParticipantTotal)
prometheus.MustRegister(promTrackPublishedTotal)
prometheus.MustRegister(promTrackSubscribedTotal)
prometheus.MustRegister(promParticipantCurrent)
prometheus.MustRegister(promTrackPublishedCurrent)
prometheus.MustRegister(promTrackSubscribedCurrent)
prometheus.MustRegister(promTrackPublishCounter)
prometheus.MustRegister(promTrackSubscribeCounter)
}
func RoomStarted() {
promRoomTotal.Add(1)
roomTotal.Inc()
promRoomCurrent.Add(1)
roomCurrent.Inc()
}
func RoomEnded(startedAt time.Time) {
if !startedAt.IsZero() {
promRoomDuration.Observe(float64(time.Since(startedAt)) / float64(time.Second))
}
promRoomTotal.Sub(1)
roomTotal.Dec()
promRoomCurrent.Sub(1)
roomCurrent.Dec()
}
func AddParticipant() {
promParticipantTotal.Add(1)
participantTotal.Inc()
promParticipantCurrent.Add(1)
participantCurrent.Inc()
}
func SubParticipant() {
promParticipantTotal.Sub(1)
participantTotal.Dec()
promParticipantCurrent.Sub(1)
participantCurrent.Dec()
}
func AddPublishedTrack(kind string) {
promTrackPublishedTotal.WithLabelValues(kind).Add(1)
trackPublishedTotal.Inc()
promTrackPublishedCurrent.WithLabelValues(kind).Add(1)
trackPublishedCurrent.Inc()
}
func SubPublishedTrack(kind string) {
promTrackPublishedTotal.WithLabelValues(kind).Sub(1)
trackPublishedTotal.Dec()
promTrackPublishedCurrent.WithLabelValues(kind).Sub(1)
trackPublishedCurrent.Dec()
}
func AddPublishAttempt(kind string) {
@@ -127,22 +130,31 @@ func AddPublishSuccess(kind string) {
promTrackPublishCounter.WithLabelValues(kind, "success").Inc()
}
func AddSubscribedTrack(kind string) {
promTrackSubscribedTotal.WithLabelValues(kind).Add(1)
trackSubscribedTotal.Inc()
}
func RecordTrackSubscribeSuccess(kind string) {
// modify both current and total counters
promTrackSubscribedCurrent.WithLabelValues(kind).Add(1)
trackSubscribedCurrent.Inc()
func SubSubscribedTrack(kind string) {
promTrackSubscribedTotal.WithLabelValues(kind).Sub(1)
trackSubscribedTotal.Dec()
}
func AddSubscribeAttempt(kind string) {
trackSubscribeAttempts.Inc()
promTrackSubscribeCounter.WithLabelValues(kind, "attempt").Inc()
}
func AddSubscribeSuccess(kind string) {
promTrackSubscribeCounter.WithLabelValues("success", "").Inc()
trackSubscribeSuccess.Inc()
promTrackSubscribeCounter.WithLabelValues(kind, "success").Inc()
}
func RecordTrackUnsubscribed(kind string) {
// unsubscribed modifies current counter, but we leave the total values alone since they
// are used to compute rate
promTrackSubscribedCurrent.WithLabelValues(kind).Sub(1)
trackSubscribedCurrent.Dec()
}
func RecordTrackSubscribeAttempt() {
trackSubscribeAttempts.Inc()
promTrackSubscribeCounter.WithLabelValues("attempt", "").Inc()
}
func RecordTrackSubscribeFailure(err error, isUserError bool) {
promTrackSubscribeCounter.WithLabelValues("failure", err.Error()).Inc()
if isUserError {
trackSubscribeUserError.Inc()
}
}
@@ -135,6 +135,15 @@ type FakeTelemetryService struct {
arg1 telemetry.StatsKey
arg2 *livekit.AnalyticsStat
}
TrackSubscribeFailedStub func(context.Context, livekit.ParticipantID, livekit.TrackID, error, bool)
trackSubscribeFailedMutex sync.RWMutex
trackSubscribeFailedArgsForCall []struct {
arg1 context.Context
arg2 livekit.ParticipantID
arg3 livekit.TrackID
arg4 error
arg5 bool
}
TrackSubscribeRequestedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, *livekit.ParticipantInfo)
trackSubscribeRequestedMutex sync.RWMutex
trackSubscribeRequestedArgsForCall []struct {
@@ -167,12 +176,13 @@ type FakeTelemetryService struct {
arg4 *livekit.TrackInfo
arg5 bool
}
TrackUnsubscribedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo)
TrackUnsubscribedStub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, bool)
trackUnsubscribedMutex sync.RWMutex
trackUnsubscribedArgsForCall []struct {
arg1 context.Context
arg2 livekit.ParticipantID
arg3 *livekit.TrackInfo
arg4 bool
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
@@ -787,6 +797,42 @@ func (fake *FakeTelemetryService) TrackStatsArgsForCall(i int) (telemetry.StatsK
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeTelemetryService) TrackSubscribeFailed(arg1 context.Context, arg2 livekit.ParticipantID, arg3 livekit.TrackID, arg4 error, arg5 bool) {
fake.trackSubscribeFailedMutex.Lock()
fake.trackSubscribeFailedArgsForCall = append(fake.trackSubscribeFailedArgsForCall, struct {
arg1 context.Context
arg2 livekit.ParticipantID
arg3 livekit.TrackID
arg4 error
arg5 bool
}{arg1, arg2, arg3, arg4, arg5})
stub := fake.TrackSubscribeFailedStub
fake.recordInvocation("TrackSubscribeFailed", []interface{}{arg1, arg2, arg3, arg4, arg5})
fake.trackSubscribeFailedMutex.Unlock()
if stub != nil {
fake.TrackSubscribeFailedStub(arg1, arg2, arg3, arg4, arg5)
}
}
func (fake *FakeTelemetryService) TrackSubscribeFailedCallCount() int {
fake.trackSubscribeFailedMutex.RLock()
defer fake.trackSubscribeFailedMutex.RUnlock()
return len(fake.trackSubscribeFailedArgsForCall)
}
func (fake *FakeTelemetryService) TrackSubscribeFailedCalls(stub func(context.Context, livekit.ParticipantID, livekit.TrackID, error, bool)) {
fake.trackSubscribeFailedMutex.Lock()
defer fake.trackSubscribeFailedMutex.Unlock()
fake.TrackSubscribeFailedStub = stub
}
func (fake *FakeTelemetryService) TrackSubscribeFailedArgsForCall(i int) (context.Context, livekit.ParticipantID, livekit.TrackID, error, bool) {
fake.trackSubscribeFailedMutex.RLock()
defer fake.trackSubscribeFailedMutex.RUnlock()
argsForCall := fake.trackSubscribeFailedArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5
}
func (fake *FakeTelemetryService) TrackSubscribeRequested(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo, arg4 *livekit.ParticipantInfo) {
fake.trackSubscribeRequestedMutex.Lock()
fake.trackSubscribeRequestedArgsForCall = append(fake.trackSubscribeRequestedArgsForCall, struct {
@@ -927,18 +973,19 @@ func (fake *FakeTelemetryService) TrackUnpublishedArgsForCall(i int) (context.Co
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5
}
func (fake *FakeTelemetryService) TrackUnsubscribed(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo) {
func (fake *FakeTelemetryService) TrackUnsubscribed(arg1 context.Context, arg2 livekit.ParticipantID, arg3 *livekit.TrackInfo, arg4 bool) {
fake.trackUnsubscribedMutex.Lock()
fake.trackUnsubscribedArgsForCall = append(fake.trackUnsubscribedArgsForCall, struct {
arg1 context.Context
arg2 livekit.ParticipantID
arg3 *livekit.TrackInfo
}{arg1, arg2, arg3})
arg4 bool
}{arg1, arg2, arg3, arg4})
stub := fake.TrackUnsubscribedStub
fake.recordInvocation("TrackUnsubscribed", []interface{}{arg1, arg2, arg3})
fake.recordInvocation("TrackUnsubscribed", []interface{}{arg1, arg2, arg3, arg4})
fake.trackUnsubscribedMutex.Unlock()
if stub != nil {
fake.TrackUnsubscribedStub(arg1, arg2, arg3)
fake.TrackUnsubscribedStub(arg1, arg2, arg3, arg4)
}
}
@@ -948,17 +995,17 @@ func (fake *FakeTelemetryService) TrackUnsubscribedCallCount() int {
return len(fake.trackUnsubscribedArgsForCall)
}
func (fake *FakeTelemetryService) TrackUnsubscribedCalls(stub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo)) {
func (fake *FakeTelemetryService) TrackUnsubscribedCalls(stub func(context.Context, livekit.ParticipantID, *livekit.TrackInfo, bool)) {
fake.trackUnsubscribedMutex.Lock()
defer fake.trackUnsubscribedMutex.Unlock()
fake.TrackUnsubscribedStub = stub
}
func (fake *FakeTelemetryService) TrackUnsubscribedArgsForCall(i int) (context.Context, livekit.ParticipantID, *livekit.TrackInfo) {
func (fake *FakeTelemetryService) TrackUnsubscribedArgsForCall(i int) (context.Context, livekit.ParticipantID, *livekit.TrackInfo, bool) {
fake.trackUnsubscribedMutex.RLock()
defer fake.trackUnsubscribedMutex.RUnlock()
argsForCall := fake.trackUnsubscribedArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
}
func (fake *FakeTelemetryService) Invocations() map[string][][]interface{} {
@@ -1000,6 +1047,8 @@ func (fake *FakeTelemetryService) Invocations() map[string][][]interface{} {
defer fake.trackPublishedUpdateMutex.RUnlock()
fake.trackStatsMutex.RLock()
defer fake.trackStatsMutex.RUnlock()
fake.trackSubscribeFailedMutex.RLock()
defer fake.trackSubscribeFailedMutex.RUnlock()
fake.trackSubscribeRequestedMutex.RLock()
defer fake.trackSubscribeRequestedMutex.RUnlock()
fake.trackSubscribedMutex.RLock()
+3 -1
View File
@@ -40,7 +40,9 @@ type TelemetryService interface {
// TrackSubscribed - a participant subscribed to a track successfully
TrackSubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, publisher *livekit.ParticipantInfo)
// TrackUnsubscribed - a participant unsubscribed from a track successfully
TrackUnsubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo)
TrackUnsubscribed(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo, shouldSendEvent bool)
// TrackSubscribeFailed - failure to subscribe to a track
TrackSubscribeFailed(ctx context.Context, participantID livekit.ParticipantID, trackID livekit.TrackID, err error, isUserError bool)
// TrackMuted - the publisher has muted the Track
TrackMuted(ctx context.Context, participantID livekit.ParticipantID, track *livekit.TrackInfo)
// TrackUnmuted - the publisher has muted the Track