store active stats workers in list (#2690)

* store active stats workers in list

* test

* single node cleanup

* cleanup

* cleanup

* cleanup
This commit is contained in:
Paul Wells
2024-04-26 17:24:10 -07:00
committed by GitHub
parent 15ffb53df9
commit ac1b0e38ca
3 changed files with 81 additions and 72 deletions
+1 -2
View File
@@ -163,10 +163,9 @@ func (t *telemetryService) ParticipantLeft(ctx context.Context,
isConnected := false isConnected := false
if worker, ok := t.getWorker(livekit.ParticipantID(participant.Sid)); ok { if worker, ok := t.getWorker(livekit.ParticipantID(participant.Sid)); ok {
isConnected = worker.IsConnected() isConnected = worker.IsConnected()
if worker.ClosedAt().IsZero() { if worker.Close() {
prometheus.SubParticipant() prometheus.SubParticipant()
} }
worker.Close()
} }
if isConnected && shouldSendEvent { if isConnected && shouldSendEvent {
+14 -12
View File
@@ -29,6 +29,8 @@ import (
// StatsWorker handles participant stats // StatsWorker handles participant stats
type StatsWorker struct { type StatsWorker struct {
next *StatsWorker
ctx context.Context ctx context.Context
t TelemetryService t TelemetryService
roomID livekit.RoomID roomID livekit.RoomID
@@ -91,8 +93,8 @@ func (s *StatsWorker) IsConnected() bool {
return s.isConnected return s.isConnected
} }
func (s *StatsWorker) Flush() { func (s *StatsWorker) Flush(now time.Time) bool {
ts := timestamppb.Now() ts := timestamppb.New(now)
s.lock.Lock() s.lock.Lock()
stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack)) stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack))
@@ -102,6 +104,8 @@ func (s *StatsWorker) Flush() {
outgoingPerTrack := s.outgoingPerTrack outgoingPerTrack := s.outgoingPerTrack
s.outgoingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat) s.outgoingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat)
closed := !s.closedAt.IsZero() && now.Sub(s.closedAt) > workerCleanupWait
s.lock.Unlock() s.lock.Unlock()
stats = s.collectStats(ts, livekit.StreamType_UPSTREAM, incomingPerTrack, stats) stats = s.collectStats(ts, livekit.StreamType_UPSTREAM, incomingPerTrack, stats)
@@ -109,21 +113,19 @@ func (s *StatsWorker) Flush() {
if len(stats) > 0 { if len(stats) > 0 {
s.t.SendStats(s.ctx, stats) s.t.SendStats(s.ctx, stats)
} }
return closed
} }
func (s *StatsWorker) Close() { func (s *StatsWorker) Close() bool {
s.Flush()
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock()
ok := s.closedAt.IsZero()
if ok {
s.closedAt = time.Now() s.closedAt = time.Now()
s.lock.Unlock()
} }
return ok
func (s *StatsWorker) ClosedAt() time.Time {
s.lock.RLock()
defer s.lock.RUnlock()
return s.closedAt
} }
func (s *StatsWorker) collectStats( func (s *StatsWorker) collectStats(
+63 -55
View File
@@ -19,8 +19,6 @@ import (
"sync" "sync"
"time" "time"
"golang.org/x/exp/maps"
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
@@ -95,9 +93,11 @@ type telemetryService struct {
notifier webhook.QueuedNotifier notifier webhook.QueuedNotifier
jobsQueue *utils.OpsQueue jobsQueue *utils.OpsQueue
lock sync.RWMutex workersMu sync.RWMutex
workers map[livekit.ParticipantID]*StatsWorker workers map[livekit.ParticipantID]*StatsWorker
workersShadow []*StatsWorker workerList *StatsWorker
flushMu sync.Mutex
} }
func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsService) TelemetryService { func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsService) TelemetryService {
@@ -121,29 +121,62 @@ func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsSer
} }
func (t *telemetryService) FlushStats() { func (t *telemetryService) FlushStats() {
t.lock.RLock() t.flushMu.Lock()
workersShadow := t.workersShadow defer t.flushMu.Unlock()
t.lock.RUnlock()
for _, worker := range workersShadow { t.workersMu.RLock()
worker.Flush() worker := t.workerList
t.workersMu.RUnlock()
now := time.Now()
var prev, reapHead, reapTail *StatsWorker
for worker != nil {
if closed := worker.Flush(now); closed {
if prev == nil {
// this worker was at the head of the list
t.workersMu.Lock()
p := &t.workerList
for *p != worker {
// new workers have been added. scan until we find the one
// immediately before this
prev = *p
p = &prev.next
}
*p = worker.next
t.workersMu.Unlock()
} else {
prev.next = worker.next
}
if reapHead == nil {
reapHead = worker
reapTail = worker
} else {
reapTail.next = worker
reapTail = worker
}
} else {
prev = worker
}
worker = worker.next
}
if reapHead != nil {
t.workersMu.Lock()
for {
delete(t.workers, reapHead.participantID)
if reapHead == reapTail {
break
}
reapHead = reapHead.next
}
t.workersMu.Unlock()
} }
} }
func (t *telemetryService) run() { func (t *telemetryService) run() {
ticker := time.NewTicker(config.TelemetryStatsUpdateInterval) for range time.Tick(config.TelemetryStatsUpdateInterval) {
defer ticker.Stop()
cleanupTicker := time.NewTicker(time.Minute)
defer cleanupTicker.Stop()
for {
select {
case <-ticker.C:
t.FlushStats() t.FlushStats()
case <-cleanupTicker.C:
t.cleanupWorkers()
}
} }
} }
@@ -152,21 +185,22 @@ func (t *telemetryService) enqueue(op func()) {
} }
func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) { func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) {
t.lock.RLock() t.workersMu.RLock()
defer t.lock.RUnlock() defer t.workersMu.RUnlock()
worker, ok = t.workers[participantID] worker, ok = t.workers[participantID]
return return
} }
func (t *telemetryService) getOrCreateWorker(ctx context.Context, func (t *telemetryService) getOrCreateWorker(
ctx context.Context,
roomID livekit.RoomID, roomID livekit.RoomID,
roomName livekit.RoomName, roomName livekit.RoomName,
participantID livekit.ParticipantID, participantID livekit.ParticipantID,
participantIdentity livekit.ParticipantIdentity, participantIdentity livekit.ParticipantIdentity,
) (*StatsWorker, bool) { ) (*StatsWorker, bool) {
t.lock.Lock() t.workersMu.Lock()
defer t.lock.Unlock() defer t.workersMu.Unlock()
if worker, ok := t.workers[participantID]; ok { if worker, ok := t.workers[participantID]; ok {
return worker, true return worker, true
@@ -182,39 +216,13 @@ func (t *telemetryService) getOrCreateWorker(ctx context.Context,
) )
t.workers[participantID] = worker t.workers[participantID] = worker
t.workersShadow = maps.Values(t.workers)
worker.next = t.workerList
t.workerList = worker
return worker, false return worker, false
} }
func (t *telemetryService) cleanupWorkers() {
t.lock.RLock()
workersShadow := t.workersShadow
t.lock.RUnlock()
toReap := make([]livekit.ParticipantID, 0, len(workersShadow))
for _, worker := range workersShadow {
closedAt := worker.ClosedAt()
if !closedAt.IsZero() && time.Since(closedAt) > workerCleanupWait {
worker.Flush()
toReap = append(toReap, worker.ParticipantID())
}
}
if len(toReap) == 0 {
return
}
t.lock.Lock()
logger.Debugw("reaping analytics worker for participants", "pID", toReap)
for _, pID := range toReap {
delete(t.workers, pID)
}
t.workersShadow = maps.Values(t.workers)
t.lock.Unlock()
}
func (t *telemetryService) LocalRoomState(ctx context.Context, info *livekit.AnalyticsNodeRooms) { func (t *telemetryService) LocalRoomState(ctx context.Context, info *livekit.AnalyticsNodeRooms) {
t.enqueue(func() { t.enqueue(func() {
t.SendNodeRoomStates(ctx, info) t.SendNodeRoomStates(ctx, info)