diff --git a/pkg/telemetry/events.go b/pkg/telemetry/events.go index 1a5d2285..e16f5fad 100644 --- a/pkg/telemetry/events.go +++ b/pkg/telemetry/events.go @@ -163,10 +163,9 @@ func (t *telemetryService) ParticipantLeft(ctx context.Context, isConnected := false if worker, ok := t.getWorker(livekit.ParticipantID(participant.Sid)); ok { isConnected = worker.IsConnected() - if worker.ClosedAt().IsZero() { + if worker.Close() { prometheus.SubParticipant() } - worker.Close() } if isConnected && shouldSendEvent { diff --git a/pkg/telemetry/statsworker.go b/pkg/telemetry/statsworker.go index 18bde9e5..735821b6 100644 --- a/pkg/telemetry/statsworker.go +++ b/pkg/telemetry/statsworker.go @@ -29,6 +29,8 @@ import ( // StatsWorker handles participant stats type StatsWorker struct { + next *StatsWorker + ctx context.Context t TelemetryService roomID livekit.RoomID @@ -91,8 +93,8 @@ func (s *StatsWorker) IsConnected() bool { return s.isConnected } -func (s *StatsWorker) Flush() { - ts := timestamppb.Now() +func (s *StatsWorker) Flush(now time.Time) bool { + ts := timestamppb.New(now) s.lock.Lock() stats := make([]*livekit.AnalyticsStat, 0, len(s.incomingPerTrack)+len(s.outgoingPerTrack)) @@ -102,6 +104,8 @@ func (s *StatsWorker) Flush() { outgoingPerTrack := s.outgoingPerTrack s.outgoingPerTrack = make(map[livekit.TrackID][]*livekit.AnalyticsStat) + + closed := !s.closedAt.IsZero() && now.Sub(s.closedAt) > workerCleanupWait s.lock.Unlock() stats = s.collectStats(ts, livekit.StreamType_UPSTREAM, incomingPerTrack, stats) @@ -109,21 +113,19 @@ func (s *StatsWorker) Flush() { if len(stats) > 0 { s.t.SendStats(s.ctx, stats) } + + return closed } -func (s *StatsWorker) Close() { - s.Flush() - +func (s *StatsWorker) Close() bool { s.lock.Lock() - s.closedAt = time.Now() - s.lock.Unlock() -} + defer s.lock.Unlock() -func (s *StatsWorker) ClosedAt() time.Time { - s.lock.RLock() - defer s.lock.RUnlock() - - return s.closedAt + ok := s.closedAt.IsZero() + if ok { + s.closedAt = time.Now() + } + return ok } func (s *StatsWorker) collectStats( diff --git a/pkg/telemetry/telemetryservice.go b/pkg/telemetry/telemetryservice.go index 9695476a..e20e2a54 100644 --- a/pkg/telemetry/telemetryservice.go +++ b/pkg/telemetry/telemetryservice.go @@ -19,8 +19,6 @@ import ( "sync" "time" - "golang.org/x/exp/maps" - "github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/protocol/livekit" @@ -95,9 +93,11 @@ type telemetryService struct { notifier webhook.QueuedNotifier jobsQueue *utils.OpsQueue - lock sync.RWMutex - workers map[livekit.ParticipantID]*StatsWorker - workersShadow []*StatsWorker + workersMu sync.RWMutex + workers map[livekit.ParticipantID]*StatsWorker + workerList *StatsWorker + + flushMu sync.Mutex } func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsService) TelemetryService { @@ -121,29 +121,62 @@ func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsSer } func (t *telemetryService) FlushStats() { - t.lock.RLock() - workersShadow := t.workersShadow - t.lock.RUnlock() + t.flushMu.Lock() + defer t.flushMu.Unlock() - for _, worker := range workersShadow { - worker.Flush() + t.workersMu.RLock() + 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() { - ticker := time.NewTicker(config.TelemetryStatsUpdateInterval) - defer ticker.Stop() - - cleanupTicker := time.NewTicker(time.Minute) - defer cleanupTicker.Stop() - - for { - select { - case <-ticker.C: - t.FlushStats() - case <-cleanupTicker.C: - t.cleanupWorkers() - } + for range time.Tick(config.TelemetryStatsUpdateInterval) { + t.FlushStats() } } @@ -152,21 +185,22 @@ func (t *telemetryService) enqueue(op func()) { } func (t *telemetryService) getWorker(participantID livekit.ParticipantID) (worker *StatsWorker, ok bool) { - t.lock.RLock() - defer t.lock.RUnlock() + t.workersMu.RLock() + defer t.workersMu.RUnlock() worker, ok = t.workers[participantID] return } -func (t *telemetryService) getOrCreateWorker(ctx context.Context, +func (t *telemetryService) getOrCreateWorker( + ctx context.Context, roomID livekit.RoomID, roomName livekit.RoomName, participantID livekit.ParticipantID, participantIdentity livekit.ParticipantIdentity, ) (*StatsWorker, bool) { - t.lock.Lock() - defer t.lock.Unlock() + t.workersMu.Lock() + defer t.workersMu.Unlock() if worker, ok := t.workers[participantID]; ok { return worker, true @@ -182,39 +216,13 @@ func (t *telemetryService) getOrCreateWorker(ctx context.Context, ) t.workers[participantID] = worker - t.workersShadow = maps.Values(t.workers) + + worker.next = t.workerList + t.workerList = worker 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) { t.enqueue(func() { t.SendNodeRoomStates(ctx, info)