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:
@@ -786,7 +786,7 @@ func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseRea
|
||||
p.TransportManager.Close()
|
||||
}()
|
||||
|
||||
p.dataChannelStats.Report()
|
||||
p.dataChannelStats.Stop()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -269,7 +269,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
close(done)
|
||||
|
||||
if signalStats != nil {
|
||||
signalStats.Report()
|
||||
signalStats.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -20,12 +20,11 @@ import (
|
||||
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
const statsReportInterval = 10 * time.Second
|
||||
|
||||
type BytesTrackType string
|
||||
|
||||
const (
|
||||
@@ -35,11 +34,11 @@ const (
|
||||
|
||||
// stats for signal and data channel
|
||||
type BytesTrackStats struct {
|
||||
trackID livekit.TrackID
|
||||
pID livekit.ParticipantID
|
||||
send, recv atomic.Uint64
|
||||
lastStatsReport atomic.Value // *time.Time
|
||||
telemetry TelemetryService
|
||||
trackID livekit.TrackID
|
||||
pID livekit.ParticipantID
|
||||
send, recv atomic.Uint64
|
||||
telemetry TelemetryService
|
||||
isStopped atomic.Bool
|
||||
}
|
||||
|
||||
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,
|
||||
telemetry: telemetry,
|
||||
}
|
||||
now := time.Now()
|
||||
s.lastStatsReport.Store(&now)
|
||||
go s.reporter()
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -59,29 +57,13 @@ func (s *BytesTrackStats) AddBytes(bytes uint64, isSend bool) {
|
||||
} else {
|
||||
s.recv.Add(bytes)
|
||||
}
|
||||
|
||||
s.report(false)
|
||||
}
|
||||
|
||||
func (s *BytesTrackStats) Report() {
|
||||
s.report(true)
|
||||
func (s *BytesTrackStats) Stop() {
|
||||
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 {
|
||||
s.telemetry.TrackStats(StatsKeyForData(livekit.StreamType_UPSTREAM, s.pID, s.trackID), &livekit.AnalyticsStat{
|
||||
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 {
|
||||
return livekit.TrackID(fmt.Sprintf("%s_%s%s", utils.TrackPrefix, string(typ), participantID))
|
||||
}
|
||||
|
||||
@@ -126,8 +126,6 @@ func (s *StatsWorker) ClosedAt() time.Time {
|
||||
return s.closedAt
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
func (s *StatsWorker) collectStats(
|
||||
ts *timestamppb.Timestamp,
|
||||
streamType livekit.StreamType,
|
||||
@@ -151,6 +149,8 @@ func (s *StatsWorker) collectStats(
|
||||
return stats
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// create a single stream and single video layer post aggregation
|
||||
func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
|
||||
if len(stats) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user