Cleaning up unused stream allocator experiments. (#3237)

Not sure if we will ever use it. Can bring it back if needed.
This commit is contained in:
Raja Subramanian
2024-12-08 13:00:58 +05:30
committed by GitHub
parent 7c5a558a48
commit c172ba13e6
10 changed files with 54 additions and 647 deletions
-172
View File
@@ -1,172 +0,0 @@
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package streamallocator
import (
"fmt"
"sync"
"time"
"github.com/livekit/protocol/utils/timeseries"
)
// ------------------------------------------------
const (
rateMonitorWindow = 10 * time.Second
queueMonitorWindow = 2 * time.Second
)
// ------------------------------------------------
type RateMonitor struct {
mu sync.Mutex
bitrateEstimate *timeseries.TimeSeries[int64]
managedBytesSent *timeseries.TimeSeries[uint32]
managedBytesRetransmitted *timeseries.TimeSeries[uint32]
unmanagedBytesSent *timeseries.TimeSeries[uint32]
unmanagedBytesRetransmitted *timeseries.TimeSeries[uint32]
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO: remove after experimental
history []string
}
func NewRateMonitor() *RateMonitor {
return &RateMonitor{
bitrateEstimate: timeseries.NewTimeSeries[int64](timeseries.TimeSeriesParams{
UpdateOp: timeseries.TimeSeriesUpdateOpLatest,
Window: rateMonitorWindow,
}),
managedBytesSent: timeseries.NewTimeSeries[uint32](timeseries.TimeSeriesParams{
UpdateOp: timeseries.TimeSeriesUpdateOpAdd,
Window: rateMonitorWindow,
}),
managedBytesRetransmitted: timeseries.NewTimeSeries[uint32](timeseries.TimeSeriesParams{
UpdateOp: timeseries.TimeSeriesUpdateOpAdd,
Window: rateMonitorWindow,
}),
unmanagedBytesSent: timeseries.NewTimeSeries[uint32](timeseries.TimeSeriesParams{
UpdateOp: timeseries.TimeSeriesUpdateOpAdd,
Window: rateMonitorWindow,
}),
unmanagedBytesRetransmitted: timeseries.NewTimeSeries[uint32](timeseries.TimeSeriesParams{
UpdateOp: timeseries.TimeSeriesUpdateOpAdd,
Window: rateMonitorWindow,
}),
}
}
func (r *RateMonitor) Update(estimate int64, managedBytesSent uint32, managedBytesRetransmitted uint32, unmanagedBytesSent uint32, unmanagedBytesRetransmitted uint32) {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
r.bitrateEstimate.AddSampleAt(estimate, now)
r.managedBytesSent.AddSampleAt(managedBytesSent, now)
r.managedBytesRetransmitted.AddSampleAt(managedBytesRetransmitted, now)
r.unmanagedBytesSent.AddSampleAt(unmanagedBytesSent, now)
r.unmanagedBytesRetransmitted.AddSampleAt(unmanagedBytesRetransmitted, now)
r.updateHistory()
}
// STREAM-ALLOCATOR-TODO:
// This should be updated periodically to flush any pending.
// Reason is that the estimate could be higher than the actual rate by a significant amount.
// So, updating periodically to flush out samples that will not contribute to queueing would be good.
func (r *RateMonitor) GetQueuingGuess() float64 {
_, _, _, _, _, queuingDelay := r.getRates(queueMonitorWindow)
return queuingDelay
}
func (r *RateMonitor) getRates(monitorDuration time.Duration) (totalBitrateEstimate, totalManagedSent, totalManagedRetransmitted, totalUnmanagedSent, totalUnmanagedRetransmitted, queuingDelay float64) {
r.mu.Lock()
defer r.mu.Unlock()
threshold := time.Now().Add(-monitorDuration)
if !r.bitrateEstimate.HasSamplesAfter(threshold) ||
!(r.managedBytesSent.HasSamplesAfter(threshold) ||
r.managedBytesRetransmitted.HasSamplesAfter(threshold) ||
r.unmanagedBytesSent.HasSamplesAfter(threshold) ||
r.unmanagedBytesRetransmitted.HasSamplesAfter(threshold)) {
return
}
totalBitrateEstimate = getTimeWeightedSum(r.bitrateEstimate.ReverseIterateSamplesAfter(threshold))
totalManagedSent = getRate(r.managedBytesSent.ReverseIterateSamplesAfter(threshold)) * 8
totalManagedRetransmitted = getRate(r.managedBytesRetransmitted.ReverseIterateSamplesAfter(threshold)) * 8
totalUnmanagedSent = getRate(r.unmanagedBytesSent.ReverseIterateSamplesAfter(threshold)) * 8
totalUnmanagedRetransmitted = getRate(r.unmanagedBytesRetransmitted.ReverseIterateSamplesAfter(threshold)) * 8
totalBits := totalManagedSent + totalManagedRetransmitted + totalUnmanagedSent + totalUnmanagedRetransmitted
if totalBits > totalBitrateEstimate {
latestBitrateEstimate := r.bitrateEstimate.Back().Value
excessBits := totalBits - totalBitrateEstimate
queuingDelay = excessBits / float64(latestBitrateEstimate)
}
return
}
func (r *RateMonitor) updateHistory() {
if len(r.history) >= 10 {
r.history = r.history[1:]
}
e, m, mr, um, umr, qd := r.getRates(time.Second)
if e == 0.0 {
return
}
r.mu.Lock()
r.history = append(
r.history,
fmt.Sprintf("t: %+v, e: %.2f, m: %.2f/%.2f, um: %.2f/%.2f, qd: %.2f", time.Now().UnixMilli(), e, m, mr, um, umr, qd),
)
r.mu.Unlock()
}
func (r *RateMonitor) GetHistory() []string {
return r.history
}
// ------------------------------------------------
func getTimeWeightedSum[T int64 | uint32](it timeseries.ReverseIterator[T]) float64 {
sum := 0.0
next := time.Now()
for it.Next() {
diff := next.Sub(it.Value().At).Seconds()
sum += diff * float64(it.Value().Value)
next = it.Value().At
}
return sum
}
func getRate[T int64 | uint32](it timeseries.ReverseIterator[T]) float64 {
var sum float64
var first, last time.Time
for it.Next() {
if last.IsZero() {
last = it.Value().At
}
first = it.Value().At
sum += float64(it.Value().Value)
}
if duration := last.Sub(first); duration > 0 {
return sum / duration.Seconds()
}
return 0
}
+3 -125
View File
@@ -90,8 +90,6 @@ const (
streamAllocatorSignalResume
streamAllocatorSignalSetAllowPause
streamAllocatorSignalSetChannelCapacity
// STREAM-ALLOCATOR-DATA streamAllocatorSignalNACK
// STREAM-ALLOCATOR-DATA streamAllocatorSignalRTCPReceiverReport
streamAllocatorSignalCongestionStateChange
)
@@ -119,12 +117,6 @@ func (s streamAllocatorSignal) String() string {
return "SET_ALLOW_PAUSE"
case streamAllocatorSignalSetChannelCapacity:
return "SET_CHANNEL_CAPACITY"
/* STREAM-ALLOCATOR-DATA
case streamAllocatorSignalNACK:
return "NACK"
case streamAllocatorSignalRTCPReceiverReport:
return "RTCP_RECEIVER_REPORT"
*/
case streamAllocatorSignalCongestionStateChange:
return "CONGESTION_STATE_CHANGE"
default:
@@ -199,8 +191,6 @@ type StreamAllocator struct {
prober *ccutils.Prober
// STREAM-ALLOCATOR-DATA rateMonitor *RateMonitor
videoTracksMu sync.RWMutex
videoTracks map[livekit.TrackID]*Track
isAllocateAllPending bool
@@ -219,10 +209,9 @@ type StreamAllocator struct {
func NewStreamAllocator(params StreamAllocatorParams, enabled bool, allowPause bool) *StreamAllocator {
s := &StreamAllocator{
params: params,
enabled: enabled,
allowPause: allowPause,
// STREAM-ALLOCATOR-DATA rateMonitor: NewRateMonitor(),
params: params,
enabled: enabled,
allowPause: allowPause,
videoTracks: make(map[livekit.TrackID]*Track),
state: streamAllocatorStateStable,
activeProbeClusterId: ccutils.ProbeClusterIdInvalid,
@@ -508,27 +497,6 @@ func (s *StreamAllocator) OnResume(downTrack *sfu.DownTrack) {
})
}
/* STREAM-ALLOCATOR-DATA
// called by a video DownTrack when it processes NACKs
func (s *StreamAllocator) OnNACK(downTrack *sfu.DownTrack, nackInfos []sfu.NackInfo) {
s.postEvent(Event{
Signal: streamAllocatorSignalNACK,
TrackID: livekit.TrackID(downTrack.ID()),
Data: nackInfos,
})
}
// called by a video DownTrack when it receives an RTCP Receiver Report
// STREAM-ALLOCATOR-TODO: this should probably be done for audio tracks also
func (s *StreamAllocator) OnRTCPReceiverReport(downTrack *sfu.DownTrack, rr rtcp.ReceptionReport) {
s.postEvent(Event{
Signal: streamAllocatorSignalRTCPReceiverReport,
TrackID: livekit.TrackID(downTrack.ID()),
Data: rr,
})
}
*/
// called when probe cluster changes
func (s *StreamAllocator) OnProbeClusterSwitch(pci ccutils.ProbeClusterInfo) {
s.postEvent(Event{
@@ -639,12 +607,6 @@ func (s *StreamAllocator) postEvent(event Event) {
event.handleSignalSetAllowPause(event)
case streamAllocatorSignalSetChannelCapacity:
event.handleSignalSetChannelCapacity(event)
/* STREAM-ALLOCATOR-DATA
case streamAllocatorSignalNACK:
event.s.handleSignalNACK(event)
case streamAllocatorSignalRTCPReceiverReport:
event.s.handleSignalRTCPReceiverReport(event)
*/
case streamAllocatorSignalCongestionStateChange:
s.handleSignalCongestionStateChange(event)
}
@@ -729,11 +691,6 @@ func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
}
}
}
/* STREAM-ALLOCATOR-DATA
s.monitorRate(s.committedChannelCapacity)
s.updateTracksHistory()
*/
}
func (s *StreamAllocator) handleSignalProbeClusterSwitch(event Event) {
@@ -805,32 +762,6 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
}
}
/* STREAM-ALLOCATOR-DATA
func (s *StreamAllocator) handleSignalNACK(event Event) {
nackInfos := event.Data.([]sfu.NackInfo)
s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock()
if track != nil {
track.UpdateNack(nackInfos)
}
}
func (s *StreamAllocator) handleSignalRTCPReceiverReport(event Event) {
rr := event.Data.(rtcp.ReceptionReport)
s.videoTracksMu.Lock()
track := s.videoTracks[event.TrackID]
s.videoTracksMu.Unlock()
if track != nil {
track.ProcessRTCPReceiverReport(rr)
}
}
*/
func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
cscd := event.Data.(congestionStateChangeData)
if cscd.congestionState != bwe.CongestionStateNone {
@@ -873,14 +804,6 @@ func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
"new(bps)", cscd.estimatedAvailableChannelCapacity,
"expectedUsage(bps)", s.getExpectedBandwidthUsage(),
)
/* STREAM-ALLOCATOR-DATA
s.params.Logger.Debugw(
fmt.Sprintf("stream allocator: channel congestion detected, %s channel capacity: experimental", action),
"rateHistory", s.rateMonitor.GetHistory(),
"expectedQueuing", s.rateMonitor.GetQueuingGuess(),
"trackHistory", s.getTracksHistory(),
)
*/
s.committedChannelCapacity = cscd.estimatedAvailableChannelCapacity
s.allocateAllTracks()
@@ -1427,51 +1350,6 @@ func (s *StreamAllocator) getMaxDistanceSortedDeficient() MaxDistanceSorter {
return maxDistanceSorter
}
/* STREAM-ALLOCATOR-DATA
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO
// Monitor sent rate vs estimate to figure out queuing on congestion.
// Idea here is to pause all managed tracks on congestion detection immediately till queue drains.
// That will allow channel to clear up without more traffic added and a re-allocation can start afresh.
// Some bits to work out
// - how good is queuing estimate?
// - should we pause unmanaged tracks also? But, they will restart at highest layer and request a key frame.
// - what should be the channel capacity to use when resume re-allocation happens?
func (s *StreamAllocator) monitorRate(estimate int64) {
managedBytesSent := uint32(0)
managedBytesRetransmitted := uint32(0)
unmanagedBytesSent := uint32(0)
unmanagedBytesRetransmitted := uint32(0)
for _, track := range s.getTracks() {
b, r := track.GetAndResetBytesSent()
if track.IsManaged() {
managedBytesSent += b
managedBytesRetransmitted += r
} else {
unmanagedBytesSent += b
unmanagedBytesRetransmitted += r
}
}
s.rateMonitor.Update(estimate, managedBytesSent, managedBytesRetransmitted, unmanagedBytesSent, unmanagedBytesRetransmitted)
}
func (s *StreamAllocator) updateTracksHistory() {
for _, track := range s.getTracks() {
track.UpdateHistory()
}
}
func (s *StreamAllocator) getTracksHistory() map[livekit.TrackID]string {
tracks := s.getTracks()
history := make(map[livekit.TrackID]string, len(tracks))
for _, track := range tracks {
history[track.ID()] = track.GetHistory()
}
return history
}
*/
// ------------------------------------------------
func updateStreamStateChange(track *Track, allocation sfu.VideoAllocation, update *StreamStateUpdate) {
-166
View File
@@ -35,21 +35,6 @@ type Track struct {
totalPackets uint32
totalRepeatedNacks uint32
/* STREAM-ALLOCATOR-DATA
nackInfos map[uint16]sfu.NackInfo
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO: remove after experimental
nackHistory []string
receiverReportInitialized bool
totalLostAtLastRead uint32
totalLost uint32
highestSequenceNumberAtLastRead uint32
highestSequenceNumber uint32
maxRTT uint32
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO: remove after experimental
receiverReportHistory []string
*/
isDirty bool
streamState StreamState
@@ -68,11 +53,6 @@ func NewTrack(
isSimulcast: isSimulcast,
publisherID: publisherID,
logger: logger,
/* STREAM-ALLOCATOR-DATA
nackInfos: make(map[uint16]sfu.NackInfo),
nackHistory: make([]string, 0, 10),
receiverReportHistory: make([]string, 0, 10),
*/
streamState: StreamStateInactive,
}
t.SetPriority(0)
@@ -218,152 +198,6 @@ func (t *Track) GetNackDelta() (uint32, uint32) {
return packetDelta, nackDelta
}
/* STREAM-ALLOCATOR-DATA
func (t *Track) UpdateNack(nackInfos []sfu.NackInfo) {
for _, ni := range nackInfos {
t.nackInfos[ni.SequenceNumber] = ni
}
}
func (t *Track) GetAndResetNackStats() (lowest uint16, highest uint16, numNacked int, numNacks int, numRuns int) {
if len(t.nackInfos) == 0 {
return
}
sns := make([]uint16, 0, len(t.nackInfos))
for _, ni := range t.nackInfos {
if lowest == 0 || ni.SequenceNumber-lowest > (1<<15) {
lowest = ni.SequenceNumber
}
if highest == 0 || highest-ni.SequenceNumber > (1<<15) {
highest = ni.SequenceNumber
}
numNacks += int(ni.Attempts)
sns = append(sns, ni.SequenceNumber)
}
numNacked = len(t.nackInfos)
// find number of runs, i. e. bursts of contiguous sequence numbers NACKed, does not include isolated NACKs
sort.Slice(sns, func(i, j int) bool {
return (sns[i] - sns[j]) > (1 << 15)
})
rsn := sns[0]
rsi := 0
for i := 1; i < len(sns); i++ {
if sns[i] == rsn+1 {
continue
}
if (i - rsi - 1) > 0 {
numRuns++
}
rsn = sns[i]
rsi = i
}
t.nackInfos = make(map[uint16]sfu.NackInfo)
return
}
func (t *Track) ProcessRTCPReceiverReport(rr rtcp.ReceptionReport) {
if !t.receiverReportInitialized {
t.receiverReportInitialized = true
t.totalLostAtLastRead = rr.TotalLost
t.highestSequenceNumberAtLastRead = rr.LastSequenceNumber
}
t.totalLost = rr.TotalLost
t.highestSequenceNumber = rr.LastSequenceNumber
if rtt, err := mediatransportutil.GetRttMsFromReceiverReportOnly(&rr); err != nil {
if rtt > t.maxRTT {
t.maxRTT = rtt
}
}
t.updateReceiverReportHistory()
}
func (t *Track) GetRTCPReceiverReportDelta() (uint32, uint32, uint32) {
deltaPackets := t.highestSequenceNumber - t.highestSequenceNumberAtLastRead
t.highestSequenceNumberAtLastRead = t.highestSequenceNumber
deltaLost := t.totalLost - t.totalLostAtLastRead
t.totalLostAtLastRead = t.totalLost
maxRTT := t.maxRTT
t.maxRTT = 0
return deltaLost, deltaPackets, maxRTT
}
func (t *Track) GetAndResetBytesSent() (uint32, uint32) {
return t.downTrack.GetAndResetBytesSent()
}
func (t *Track) UpdateHistory() {
t.updateNackHistory()
}
func (t *Track) GetHistory() string {
return fmt.Sprintf("t: %+v, n: %+v, rr: %+v", time.Now(), t.nackHistory, t.receiverReportHistory)
}
// STREAM-ALLOCATOR-EXPERIMENTAL-TODO:
// Idea is to check if this provides a good signal to detect congestion.
// This measures a few things
// 1. Spread: sequence number difference between highest and lowest NACK
// - shows how widespread the losses are
// 2. Number of runs of length more than 1: Counts number of burst losses.
// - could be a sign of congestion when losses are bursty
// 3. NACK density: how many sequence numbers in the spread were NACKed.
// - a high density could be a sign of congestion
// 4. NACK intensity: how many times those sequence numbers were NACKed.
// - high intensity could be a sign of congestion
//
// While these all could be good signals, some challenges in making use of these
// - aggregating across tracks
// - proper thresholing, i. e. something based on averages should not trip
// because of small numbers, e. g. a single NACK run of 2 sequence numbers
// is technically a burst, but is it a signal of congestion?
func (t *Track) updateNackHistory() {
if len(t.nackHistory) >= 10 {
t.nackHistory = t.nackHistory[1:]
}
l, h, nnd, nns, nr := t.GetAndResetNackStats()
spread := h - l + 1
density := float64(0.0)
if nnd != 0 {
density = float64(nnd) / float64(spread)
} else {
spread = 0
}
intensity := float64(0.0)
if nnd != 0 {
intensity = float64(nns) / float64(nnd)
}
t.nackHistory = append(
t.nackHistory,
fmt.Sprintf("t: %+v, l: %d, h: %d, sp: %d, nnd: %d, dens: %.2f, nns: %d, int: %.2f, nr: %d", time.Now().UnixMilli(), l, h, spread, nnd, density, nns, intensity, nr),
)
}
func (t *Track) updateReceiverReportHistory() {
if len(t.receiverReportHistory) >= 10 {
t.receiverReportHistory = t.receiverReportHistory[1:]
}
dl, dp, maxRTT := t.GetRTCPReceiverReportDelta()
t.receiverReportHistory = append(
t.receiverReportHistory,
fmt.Sprintf("t: %+v, l: %d, p: %d, rtt: %d", time.Now().Format(time.UnixDate), dl, dp, maxRTT),
)
}
*/
// ------------------------------------------------
type TrackSorter []*Track