Use a worker to report signal/data stats. (#2260)

* Use a worker to report signal/data stats.

Was checking if reporting is needed on every update.
The check is wasted work if volume of signal/data messages is high
as reporting happens only once in 10 seconds.

Changing to a worker based on a timer. And also aligning with
telemetry reporting interval which defaults to 30 seconds.

* Remove unused constant
This commit is contained in:
Raja Subramanian
2023-11-22 11:47:15 +05:30
committed by GitHub
parent f18d6356e8
commit 56dd399684
4 changed files with 28 additions and 32 deletions
+1 -1
View File
@@ -786,7 +786,7 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
p.TransportManager.Close() p.TransportManager.Close()
}() }()
p.dataChannelStats.Report() p.dataChannelStats.Stop()
return nil return nil
} }
+1 -1
View File
@@ -269,7 +269,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
close(done) close(done)
if signalStats != nil { if signalStats != nil {
signalStats.Report() signalStats.Stop()
} }
}() }()
+20 -24
View File
@@ -20,12 +20,11 @@ import (
"go.uber.org/atomic" "go.uber.org/atomic"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils"
) )
const statsReportInterval = 10 * time.Second
type BytesTrackType string type BytesTrackType string
const ( const (
@@ -38,8 +37,8 @@ type BytesTrackStats struct {
trackID livekit.TrackID trackID livekit.TrackID
pID livekit.ParticipantID pID livekit.ParticipantID
send, recv atomic.Uint64 send, recv atomic.Uint64
lastStatsReport atomic.Value // *time.Time
telemetry TelemetryService telemetry TelemetryService
isStopped atomic.Bool
} }
func NewBytesTrackStats(trackID livekit.TrackID, pID livekit.ParticipantID, telemetry TelemetryService) *BytesTrackStats { func NewBytesTrackStats(trackID livekit.TrackID, pID livekit.ParticipantID, telemetry TelemetryService) *BytesTrackStats {
@@ -48,8 +47,7 @@ func NewBytesTrackStats(trackID livekit.TrackID, pID livekit.ParticipantID, tele
pID: pID, pID: pID,
telemetry: telemetry, telemetry: telemetry,
} }
now := time.Now() go s.reporter()
s.lastStatsReport.Store(&now)
return s return s
} }
@@ -59,29 +57,13 @@ func (s *BytesTrackStats) AddBytes(bytes uint64, isSend bool) {
} else { } else {
s.recv.Add(bytes) s.recv.Add(bytes)
} }
s.report(false)
} }
func (s *BytesTrackStats) Report() { func (s *BytesTrackStats) Stop() {
s.report(true) s.isStopped.Store(true)
}
func (s *BytesTrackStats) report(force bool) {
now := time.Now()
if !force {
lr := s.lastStatsReport.Load().(*time.Time)
if time.Since(*lr) < statsReportInterval {
return
}
if !s.lastStatsReport.CompareAndSwap(lr, &now) {
return
}
} else {
s.lastStatsReport.Store(&now)
} }
func (s *BytesTrackStats) report() {
if recv := s.recv.Swap(0); recv > 0 { if recv := s.recv.Swap(0); recv > 0 {
s.telemetry.TrackStats(StatsKeyForData(livekit.StreamType_UPSTREAM, s.pID, s.trackID), &livekit.AnalyticsStat{ s.telemetry.TrackStats(StatsKeyForData(livekit.StreamType_UPSTREAM, s.pID, s.trackID), &livekit.AnalyticsStat{
Streams: []*livekit.AnalyticsStream{ Streams: []*livekit.AnalyticsStream{
@@ -99,6 +81,20 @@ func (s *BytesTrackStats) report(force bool) {
} }
} }
func (s *BytesTrackStats) reporter() {
ticker := time.NewTicker(config.TelemetryStatsUpdateInterval)
defer ticker.Stop()
for !s.isStopped.Load() {
<-ticker.C
s.report()
}
s.report()
}
// -----------------------------------------------------------------------
func BytesTrackIDForParticipantID(typ BytesTrackType, participantID livekit.ParticipantID) livekit.TrackID { func BytesTrackIDForParticipantID(typ BytesTrackType, participantID livekit.ParticipantID) livekit.TrackID {
return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID)) return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID))
} }
+2 -2
View File
@@ -126,8 +126,6 @@ func (s *StatsWorker) ClosedAt() time.Time {
return s.closedAt return s.closedAt
} }
// -------------------------------------------------------------------------
func (s *StatsWorker) collectStats( func (s *StatsWorker) collectStats(
ts *timestamppb.Timestamp, ts *timestamppb.Timestamp,
streamType livekit.StreamType, streamType livekit.StreamType,
@@ -151,6 +149,8 @@ func (s *StatsWorker) collectStats(
return stats return stats
} }
// -------------------------------------------------------------------------
// create a single stream and single video layer post aggregation // create a single stream and single video layer post aggregation
func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat { func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
if len(stats) == 0 { if len(stats) == 0 {