telemetry: drop the stats worker re-key path (#4918)

Reverts #4816. Since livekit/cloud#4747 a room re-key closes every
participant's stats worker under the old room id and opens a new one
under the new id (LeaveSession/JoinSession, #4917), so RoomIDChanged
and reKeyRoom have nothing left to move, and with them go the sealed
stats batches, SetRoom, ForceClose and the reference hand-over that
only the re-key collision case needed. The worker map goes back to a
flat (room, participant) key.

The guard fixes that landed after #4816 (#4817, #4856, #4860) are kept.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Raja Subramanian
2026-09-28 19:07:12 +05:30
committed by GitHub
co-authored by Claude Fable 5.1
parent dc138b2bf0
commit 9a6c187afd
6 changed files with 34 additions and 455 deletions
-127
View File
@@ -626,133 +626,6 @@ func Test_BothDownstreamAndUpstreamStatsAreSentTogether(t *testing.T) {
require.Equal(t, livekit.StreamType_DOWNSTREAM, stats[1].Kind)
}
func Test_RoomIDChangeReKeysStatsWorkers(t *testing.T) {
fixture := createFixture()
// prepare
room := &livekit.Room{Sid: "RoomSid", Name: "RoomName"}
partSID := livekit.ParticipantID("part1")
participantInfo := &livekit.ParticipantInfo{Sid: string(partSID)}
trackID := livekit.TrackID("trackID")
guard := &telemetry.ReferenceGuard{}
fixture.sut.ParticipantJoined(context.Background(), room, participantInfo, nil, nil, true, guard)
stat1 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 33}}}
fixture.sut.TrackStats(livekit.RoomID(room.Sid), livekit.RoomName(room.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat1)
// do - the room restarts and gets a new id
restartedRoom := &livekit.Room{Sid: "RestartedSid", Name: "RoomName"}
fixture.sut.RoomIDChanged(context.Background(), livekit.RoomID(room.Sid), restartedRoom)
// stats reported with the new id reach the same worker
stat2 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 44}}}
fixture.sut.TrackStats(livekit.RoomID(restartedRoom.Sid), livekit.RoomName(restartedRoom.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat2)
fixture.flush()
// one worker, one flush, but two stats - each attributed to the session it was collected in
require.Equal(t, 1, fixture.analytics.SendStatsCallCount())
_, stats := fixture.analytics.SendStatsArgsForCall(0)
require.Equal(t, 2, len(stats))
byRoom := map[string]*livekit.AnalyticsStat{}
for _, stat := range stats {
require.Equal(t, string(partSID), stat.ParticipantId)
byRoom[stat.RoomId] = stat
}
require.Len(t, byRoom, 2)
require.Equal(t, uint64(33), byRoom[room.Sid].Streams[0].PrimaryBytes)
require.Equal(t, uint64(44), byRoom[restartedRoom.Sid].Streams[0].PrimaryBytes)
// the worker moved rather than being duplicated, so closing it out drains everything
fixture.sut.ParticipantLeft(context.Background(), restartedRoom, participantInfo, true, guard)
fixture.flush()
require.Equal(t, 1, fixture.analytics.SendStatsCallCount())
}
// a forwarded participant is in more than one room at a time under the same participant
// id, so re-keying one of those rooms must leave the other alone
func Test_RoomIDChangeLeavesForwardedParticipantAlone(t *testing.T) {
fixture := createFixture()
// prepare - the same participant id in a source room and a forwarding destination room
sourceRoom := &livekit.Room{Sid: "SourceSid", Name: "SourceRoom"}
destRoom := &livekit.Room{Sid: "DestSid", Name: "DestRoom"}
partSID := livekit.ParticipantID("part1")
participantInfo := &livekit.ParticipantInfo{Sid: string(partSID)}
trackID := livekit.TrackID("trackID")
fixture.sut.ParticipantJoined(context.Background(), sourceRoom, participantInfo, nil, nil, true, &telemetry.ReferenceGuard{})
fixture.sut.ParticipantJoined(context.Background(), destRoom, participantInfo, nil, nil, true, &telemetry.ReferenceGuard{})
// do - only the destination room restarts
restartedDest := &livekit.Room{Sid: "RestartedDestSid", Name: "DestRoom"}
fixture.sut.RoomIDChanged(context.Background(), livekit.RoomID(destRoom.Sid), restartedDest)
stat1 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 33}}}
fixture.sut.TrackStats(livekit.RoomID(sourceRoom.Sid), livekit.RoomName(sourceRoom.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat1)
stat2 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 44}}}
fixture.sut.TrackStats(livekit.RoomID(restartedDest.Sid), livekit.RoomName(restartedDest.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat2)
fixture.flush()
// the source room's worker is untouched, the destination room's worker moved
byRoom := map[string]*livekit.AnalyticsStat{}
for i := 0; i < fixture.analytics.SendStatsCallCount(); i++ {
_, stats := fixture.analytics.SendStatsArgsForCall(i)
for _, stat := range stats {
byRoom[stat.RoomId] = stat
}
}
require.Len(t, byRoom, 2)
require.Equal(t, uint64(33), byRoom[sourceRoom.Sid].Streams[0].PrimaryBytes)
require.Equal(t, sourceRoom.Name, byRoom[sourceRoom.Sid].RoomName)
require.Equal(t, uint64(44), byRoom[restartedDest.Sid].Streams[0].PrimaryBytes)
require.Equal(t, restartedDest.Name, byRoom[restartedDest.Sid].RoomName)
}
// a re-key should never land on a room that already has workers, but if it does only
// one worker can be keyed at (room, participant) and the superseded one must not be
// left unreachable in the flush list
func Test_RoomIDChangeParticipantCollision(t *testing.T) {
fixture := createFixture()
// prepare - the same participant id in the room being re-keyed and in its destination
prevRoom := &livekit.Room{Sid: "PrevSid", Name: "PrevRoom"}
destRoom := &livekit.Room{Sid: "DestSid", Name: "DestRoom"}
partSID := livekit.ParticipantID("part1")
participantInfo := &livekit.ParticipantInfo{Sid: string(partSID)}
trackID := livekit.TrackID("trackID")
fixture.sut.ParticipantJoined(context.Background(), prevRoom, participantInfo, nil, nil, true, &telemetry.ReferenceGuard{})
fixture.sut.ParticipantJoined(context.Background(), destRoom, participantInfo, nil, nil, true, &telemetry.ReferenceGuard{})
stat1 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 33}}}
fixture.sut.TrackStats(livekit.RoomID(prevRoom.Sid), livekit.RoomName(prevRoom.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat1)
// do
fixture.sut.RoomIDChanged(context.Background(), livekit.RoomID(prevRoom.Sid), destRoom)
// the superseded worker drains what it collected under the room it was in
fixture.flush()
byRoom := map[string]*livekit.AnalyticsStat{}
for i := 0; i < fixture.analytics.SendStatsCallCount(); i++ {
_, stats := fixture.analytics.SendStatsArgsForCall(i)
for _, stat := range stats {
byRoom[stat.RoomId] = stat
}
}
require.Equal(t, uint64(33), byRoom[prevRoom.Sid].Streams[0].PrimaryBytes)
// the worker already keyed at the destination wins and keeps receiving stats
stat2 := &livekit.AnalyticsStat{Streams: []*livekit.AnalyticsStream{{PrimaryBytes: 44}}}
fixture.sut.TrackStats(livekit.RoomID(destRoom.Sid), livekit.RoomName(destRoom.Name), telemetry.StatsKeyForData("test", livekit.StreamType_DOWNSTREAM, partSID, trackID), stat2)
fixture.flush()
_, stats := fixture.analytics.SendStatsArgsForCall(fixture.analytics.SendStatsCallCount() - 1)
require.Equal(t, 1, len(stats))
require.Equal(t, destRoom.Sid, stats[0].RoomId)
require.Equal(t, uint64(44), stats[0].Streams[0].PrimaryBytes)
}
func (f *telemetryServiceFixture) flush() {
time.Sleep(time.Millisecond * 500)
f.sut.FlushStats()