Use Deque in ops queue. (#2418)

* Use Seque in ops queue.

Standardizing some uses
- Change OpsQueue to use Deque so that it can grow/shrink as necessary and
  need not worry about channel getting full and dropping events.
- Change StreamAllocator and TelemetryService to use OpsQueue so that
  they also need not worry about channel size and overflows.

* Address feedback

* delete obvious comment

* clean up
This commit is contained in:
Raja Subramanian
2024-01-28 13:48:30 +05:30
committed by GitHub
parent c2549081c8
commit b71d373f4a
4 changed files with 69 additions and 85 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ func NewDynacastManager(params DynacastManagerParams) *DynacastManager {
maxSubscribedQuality: make(map[string]livekit.VideoQuality), maxSubscribedQuality: make(map[string]livekit.VideoQuality),
committedMaxSubscribedQuality: make(map[string]livekit.VideoQuality), committedMaxSubscribedQuality: make(map[string]livekit.VideoQuality),
maxSubscribedQualityDebounce: debounce.New(params.DynacastPauseDelay), maxSubscribedQualityDebounce: debounce.New(params.DynacastPauseDelay),
qualityNotifyOpQueue: utils.NewOpsQueue(params.Logger, "quality-notify", 100), qualityNotifyOpQueue: utils.NewOpsQueue("quality-notify", 0, true),
} }
d.qualityNotifyOpQueue.Start() d.qualityNotifyOpQueue.Start()
return d return d
+9 -31
View File
@@ -31,6 +31,7 @@ import (
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/sfu" "github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/buffer" "github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/utils"
) )
const ( const (
@@ -165,8 +166,7 @@ type StreamAllocator struct {
state streamAllocatorState state streamAllocatorState
eventChMu sync.RWMutex eventsQueue *utils.OpsQueue
eventCh chan Event
isStopped atomic.Bool isStopped atomic.Bool
} }
@@ -180,7 +180,7 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
}), }),
rateMonitor: NewRateMonitor(), rateMonitor: NewRateMonitor(),
videoTracks: make(map[livekit.TrackID]*Track), videoTracks: make(map[livekit.TrackID]*Track),
eventCh: make(chan Event, 1000), eventsQueue: utils.NewOpsQueue("stream-allocator", 64, true),
} }
s.probeController = NewProbeController(ProbeControllerParams{ s.probeController = NewProbeController(ProbeControllerParams{
@@ -197,19 +197,18 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
} }
func (s *StreamAllocator) Start() { func (s *StreamAllocator) Start() {
go s.processEvents() s.eventsQueue.Start()
go s.ping() go s.ping()
} }
func (s *StreamAllocator) Stop() { func (s *StreamAllocator) Stop() {
s.eventChMu.Lock()
if s.isStopped.Swap(true) { if s.isStopped.Swap(true) {
s.eventChMu.Unlock()
return return
} }
close(s.eventCh) // wait for eventsQueue to be done
s.eventChMu.Unlock() <-s.eventsQueue.Stop()
s.probeController.StopProbe()
} }
func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate) error) { func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate) error) {
@@ -546,30 +545,9 @@ func (s *StreamAllocator) maybePostEventAllocateTrack(downTrack *sfu.DownTrack)
} }
func (s *StreamAllocator) postEvent(event Event) { func (s *StreamAllocator) postEvent(event Event) {
s.eventChMu.RLock() s.eventsQueue.Enqueue(func() {
if s.isStopped.Load() {
s.eventChMu.RUnlock()
return
}
select {
case s.eventCh <- event:
default:
s.params.Logger.Warnw("stream allocator: event queue full", nil, "event", event.String())
}
s.eventChMu.RUnlock()
}
func (s *StreamAllocator) processEvents() {
for event := range s.eventCh {
if s.isStopped.Load() {
break
}
s.handleEvent(&event) s.handleEvent(&event)
} })
s.probeController.StopProbe()
} }
func (s *StreamAllocator) ping() { func (s *StreamAllocator) ping() {
+10 -15
View File
@@ -20,6 +20,7 @@ import (
"time" "time"
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/webhook" "github.com/livekit/protocol/webhook"
@@ -82,15 +83,15 @@ type TelemetryService interface {
} }
const ( const (
workerCleanupWait = 3 * time.Minute workerCleanupWait = 3 * time.Minute
jobQueueBufferSize = 10000 jobsQueueMinSize = 2048
) )
type telemetryService struct { type telemetryService struct {
AnalyticsService AnalyticsService
notifier webhook.QueuedNotifier notifier webhook.QueuedNotifier
jobsChan chan func() jobsQueue *utils.OpsQueue
lock sync.RWMutex lock sync.RWMutex
workers map[livekit.ParticipantID]*StatsWorker workers map[livekit.ParticipantID]*StatsWorker
@@ -100,11 +101,12 @@ func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsSer
t := &telemetryService{ t := &telemetryService{
AnalyticsService: analytics, AnalyticsService: analytics,
notifier: notifier, notifier: notifier,
jobsChan: make(chan func(), jobQueueBufferSize), jobsQueue: utils.NewOpsQueue("telemetry", jobsQueueMinSize, true),
workers: make(map[livekit.ParticipantID]*StatsWorker), workers: make(map[livekit.ParticipantID]*StatsWorker),
} }
t.jobsQueue.Start()
go t.run() go t.run()
return t return t
@@ -132,19 +134,12 @@ func (t *telemetryService) run() {
t.FlushStats() t.FlushStats()
case <-cleanupTicker.C: case <-cleanupTicker.C:
t.cleanupWorkers() t.cleanupWorkers()
case op := <-t.jobsChan:
op()
} }
} }
} }
func (t *telemetryService) enqueue(op func()) { func (t *telemetryService) enqueue(op func()) {
select { t.jobsQueue.Enqueue(op)
case t.jobsChan <- op:
// success
default:
logger.Warnw("telemetry queue full", nil)
}
} }
func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) { func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) {
+49 -38
View File
@@ -15,33 +15,34 @@
package utils package utils
import ( import (
"math/bits"
"sync" "sync"
"github.com/livekit/protocol/logger" "github.com/gammazero/deque"
"github.com/livekit/protocol/utils"
) )
type OpsQueue struct { type OpsQueue struct {
logger logger.Logger name string
name string flushOnStop bool
size int
lock sync.RWMutex lock sync.Mutex
ops chan func() ops deque.Deque[func()]
wake chan struct{}
isStarted bool isStarted bool
doneChan chan struct{}
isStopped bool isStopped bool
} }
func NewOpsQueue(logger logger.Logger, name string, size int) *OpsQueue { func NewOpsQueue(name string, minSize uint, flushOnStop bool) *OpsQueue {
return &OpsQueue{ oq := &OpsQueue{
logger: logger, name: name,
name: name, flushOnStop: flushOnStop,
size: size, wake: make(chan struct{}, 1),
ops: make(chan func(), size), doneChan: make(chan struct{}),
} }
} oq.ops.SetMinCapacity(uint(utils.Min(bits.Len64(uint64(minSize-1)), 16)))
return oq
func (oq *OpsQueue) SetLogger(logger logger.Logger) {
oq.logger = logger
} }
func (oq *OpsQueue) Start() { func (oq *OpsQueue) Start() {
@@ -57,42 +58,52 @@ func (oq *OpsQueue) Start() {
go oq.process() go oq.process()
} }
func (oq *OpsQueue) Stop() { func (oq *OpsQueue) Stop() <-chan struct{} {
oq.lock.Lock() oq.lock.Lock()
if oq.isStopped { if oq.isStopped {
oq.lock.Unlock() oq.lock.Unlock()
return return oq.doneChan
} }
oq.isStopped = true oq.isStopped = true
close(oq.ops) close(oq.wake)
oq.lock.Unlock() oq.lock.Unlock()
} return oq.doneChan
func (oq *OpsQueue) IsStarted() bool {
oq.lock.RLock()
defer oq.lock.RUnlock()
return oq.isStarted
} }
func (oq *OpsQueue) Enqueue(op func()) { func (oq *OpsQueue) Enqueue(op func()) {
oq.lock.RLock() oq.lock.Lock()
if oq.isStopped { defer oq.lock.Unlock()
oq.lock.RUnlock()
return
}
select { oq.ops.PushBack(op)
case oq.ops <- op: if oq.ops.Len() == 1 && !oq.isStopped {
default: select {
oq.logger.Errorw("ops queue full", nil, "name", oq.name, "size", oq.size) case oq.wake <- struct{}{}:
default:
}
} }
oq.lock.RUnlock()
} }
func (oq *OpsQueue) process() { func (oq *OpsQueue) process() {
for op := range oq.ops { defer close(oq.doneChan)
op()
for {
<-oq.wake
for {
oq.lock.Lock()
if oq.isStopped && (!oq.flushOnStop || oq.ops.Len() == 0) {
oq.lock.Unlock()
return
}
if oq.ops.Len() == 0 {
oq.lock.Unlock()
break
}
op := oq.ops.PopFront()
oq.lock.Unlock()
op()
}
} }
} }