telemetry: support roomID change for a participant (#4816)

* telemetry: support roomID change for a participant

A room can get a new id while participants are connected. Key stats
workers as map[roomID]map[participantID] so moving a room is a single
map splice, and add reKeyRoom/RoomIDChanged to do the move.

Stats collected before the change are sealed off with the room they
were collected in so they stay attributed to it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* telemetry: close superseded worker on re-key collision

Only one worker can be keyed at (room, participant). If a re-key lands
on a room that already has a worker for the same participant, keep the
one already filed there and close the superseded one so it drains and
is reaped instead of lingering in the flush list.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* telemetry: hand references to the successor on force close

A ReferenceGuard records that it activated some worker, not which one,
so a superseded worker cannot just drop its references - the survivor
would be left with references it never sees released and would never
close. Hand them over instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Raja Subramanian
2026-08-29 12:58:22 +05:30
committed by GitHub
co-authored by Claude Opus 5
parent 54496c765d
commit 5528c464a0
6 changed files with 441 additions and 30 deletions
+86 -14
View File
@@ -19,6 +19,7 @@ import (
"sync"
"time"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit"
@@ -45,6 +46,9 @@ type TelemetryService interface {
ParticipantResumed(ctx context.Context, room *livekit.Room, participant *livekit.ParticipantInfo, nodeID livekit.NodeID, reason livekit.ReconnectReason)
// ParticipantLeft - the participant leaves the room, only sent if ParticipantActive has been called before
ParticipantLeft(ctx context.Context, room *livekit.Room, participant *livekit.ParticipantInfo, shouldSendEvent bool, guard *ReferenceGuard)
// RoomIDChanged - the room kept its session, but got a different id (a provisional room id
// replaced by the resolved one), re-keys the stats workers of every participant in the room
RoomIDChanged(ctx context.Context, prevRoomID livekit.RoomID, room *livekit.Room)
// TrackPublishRequested - a publication attempt has been received
TrackPublishRequested(ctx context.Context, room *livekit.Room, participantID livekit.ParticipantID, identity livekit.ParticipantIdentity, track *livekit.TrackInfo, shouldSendEvent bool)
// TrackPublished - a publication attempt has been successful
@@ -115,6 +119,8 @@ func (n NullTelemetryService) ParticipantResumed(ctx context.Context, room *live
}
func (n NullTelemetryService) ParticipantLeft(ctx context.Context, room *livekit.Room, participant *livekit.ParticipantInfo, shouldSendEvent bool, guard *ReferenceGuard) {
}
func (n NullTelemetryService) RoomIDChanged(ctx context.Context, prevRoomID livekit.RoomID, room *livekit.Room) {
}
func (n NullTelemetryService) TrackPublishRequested(ctx context.Context, room *livekit.Room, participantID livekit.ParticipantID, identity livekit.ParticipantIdentity, track *livekit.TrackInfo, shouldSendEvent bool) {
}
func (n NullTelemetryService) TrackPublished(ctx context.Context, room *livekit.Room, participantID livekit.ParticipantID, identity livekit.ParticipantIdentity, track *livekit.TrackInfo, shouldSendEvent bool) {
@@ -166,11 +172,6 @@ const (
telemetryStatsUpdateInterval = time.Second * 30
)
type statsWorkerKey struct {
roomID livekit.RoomID
participantID livekit.ParticipantID
}
type telemetryService struct {
AnalyticsService
@@ -178,7 +179,7 @@ type telemetryService struct {
jobsQueue *utils.OpsQueue
workersMu sync.RWMutex
workers map[statsWorkerKey]*StatsWorker
workers map[livekit.RoomID]map[livekit.ParticipantID]*StatsWorker
workerList *StatsWorker
flushMu sync.Mutex
@@ -194,7 +195,7 @@ func NewTelemetryService(notifier webhook.QueuedNotifier, analytics AnalyticsSer
FlushOnStop: true,
Logger: logger.GetLogger(),
}),
workers: make(map[statsWorkerKey]*StatsWorker),
workers: make(map[livekit.RoomID]map[livekit.ParticipantID]*StatsWorker),
}
t.jobsQueue.Start()
@@ -243,9 +244,12 @@ func (t *telemetryService) FlushStats() {
if reap != nil {
t.workersMu.Lock()
for reap != nil {
key := statsWorkerKey{reap.roomID, reap.participantID}
if reap == t.workers[key] {
delete(t.workers, key)
roomID := reap.RoomID()
if roomWorkers := t.workers[roomID]; reap == roomWorkers[reap.participantID] {
delete(roomWorkers, reap.participantID)
if len(roomWorkers) == 0 {
delete(t.workers, roomID)
}
}
reap = reap.next
}
@@ -267,7 +271,7 @@ func (t *telemetryService) getWorker(roomID livekit.RoomID, participantID liveki
t.workersMu.RLock()
defer t.workersMu.RUnlock()
worker, ok = t.workers[statsWorkerKey{roomID, participantID}]
worker, ok = t.workers[roomID][participantID]
return
}
@@ -282,8 +286,8 @@ func (t *telemetryService) getOrCreateWorker(
t.workersMu.Lock()
defer t.workersMu.Unlock()
key := statsWorkerKey{roomID, participantID}
worker, ok := t.workers[key]
roomWorkers := t.workers[roomID]
worker, ok := roomWorkers[participantID]
if ok && !worker.Closed(guard) {
return worker, true
}
@@ -306,7 +310,11 @@ func (t *telemetryService) getOrCreateWorker(
worker.SetConnected()
}
t.workers[key] = worker
if roomWorkers == nil {
roomWorkers = make(map[livekit.ParticipantID]*StatsWorker)
t.workers[roomID] = roomWorkers
}
roomWorkers[participantID] = worker
worker.next = t.workerList
t.workerList = worker
@@ -314,6 +322,70 @@ func (t *telemetryService) getOrCreateWorker(
return worker, false
}
// reKeyRoom files every one of a room's stats workers under `roomID` instead of
// `prevRoomID`.
//
// A room can be restarted while participants are connected and reporting stats, which
// gives it a new id. As every worker of the room moves at once, the move is a single map
// splice - the workers themselves are untouched and keep their place in the flush list.
// Each worker then seals off what it collected under `prevRoomID` so those stats stay
// attributed to the session that ended (see StatsWorker.SetRoom).
func (t *telemetryService) reKeyRoom(prevRoomID livekit.RoomID, roomID livekit.RoomID, roomName livekit.RoomName) {
if prevRoomID == roomID {
return
}
t.workersMu.Lock()
defer t.workersMu.Unlock()
roomWorkers := t.workers[prevRoomID]
if len(roomWorkers) == 0 {
delete(t.workers, prevRoomID)
return
}
delete(t.workers, prevRoomID)
existing := t.workers[roomID]
if existing == nil {
t.workers[roomID] = roomWorkers
} else {
// should not happen as a room id is only ever replaced by a freshly minted one
logger.Warnw(
"telemetry re-keying room into an existing entry", nil,
"prevRoomID", prevRoomID,
"room", roomName,
"roomID", roomID,
"numWorkers", len(roomWorkers),
"numExistingWorkers", len(existing),
)
}
for participantID, worker := range roomWorkers {
if existing != nil {
if survivor, ok := existing[participantID]; ok {
// only one worker can be keyed at (room, participant) and the one already
// filed there wins, close the superseded one so that it drains and is
// reaped instead of lingering in the flush list unreachable
if worker.ForceClose(survivor) {
prometheus.SubParticipant()
}
continue
}
existing[participantID] = worker
}
worker.SetRoom(roomID, roomName)
}
logger.Infow(
"telemetry re-keyed room",
"prevRoomID", prevRoomID,
"room", roomName,
"roomID", roomID,
"numWorkers", len(roomWorkers),
)
}
func (t *telemetryService) LocalRoomState(ctx context.Context, info *livekit.AnalyticsNodeRooms) {
t.enqueue(func() {
t.SendNodeRoomStates(ctx, info)