From 395f403132497aa66b0d0c46f470ec5c448310b7 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Sat, 17 Jun 2023 12:35:29 +0530 Subject: [PATCH] Small stream allocator tweaks. (#1800) 1. Probe end time needs to include the probe cluster running time also. 2. Apply collapse window only within the sliding window. This is to prevent cases of some old data declaring congestion. For example, an estimate could have fallen 15 seconds ago and there might have been a bunch of estimates at that fallen value. And the whole sliding window could have that value at some point. But, a further drop may trigger congestion detection. But, that might be acting too fast, i. e. on one instance of value fall. Change it so that we detect if there is a fall within the sliding window and apply collapse based on that. --- pkg/sfu/streamallocator/probe_controller.go | 2 +- pkg/sfu/streamallocator/trenddetector.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/sfu/streamallocator/probe_controller.go b/pkg/sfu/streamallocator/probe_controller.go index 6b476b6d..28fda6ac 100644 --- a/pkg/sfu/streamallocator/probe_controller.go +++ b/pkg/sfu/streamallocator/probe_controller.go @@ -86,7 +86,7 @@ func (p *ProbeController) ProbeClusterDone(info ProbeClusterInfo, lowestEstimate if queueWait > ProbeSettleWaitMax { queueWait = ProbeSettleWaitMax } - p.probeEndTime = p.lastProbeStartTime.Add(queueWait) + p.probeEndTime = p.lastProbeStartTime.Add(queueWait + info.Duration) p.params.Logger.Infow( "setting probe end time", "probeClusterId", p.probeClusterId, diff --git a/pkg/sfu/streamallocator/trenddetector.go b/pkg/sfu/streamallocator/trenddetector.go index 2c5bf485..9f96b629 100644 --- a/pkg/sfu/streamallocator/trenddetector.go +++ b/pkg/sfu/streamallocator/trenddetector.go @@ -49,7 +49,6 @@ type TrendDetector struct { lowestValue int64 highestValue int64 - hasFallen bool lastSampleAt time.Time direction TrendDirection @@ -70,7 +69,6 @@ func (t *TrendDetector) Seed(value int64) { t.values = append(t.values, value) t.lastSampleAt = time.Now() - t.hasFallen = false } func (t *TrendDetector) AddValue(value int64) { @@ -91,22 +89,26 @@ func (t *TrendDetector) AddValue(value int64) { // the reaction is not too fast, i. e. reacting to falling values too quick could mean a lot of re-allocation // resulting in layer switches, key frames and more congestion. // - // But, on the flip side, estimate could fall once or twice withing a sliding window and stay there. - // In those cases, using a collapse window to record value even if it is duplicate. By doing that, + // But, on the flip side, estimate could fall once or twice within a sliding window and stay there. + // In those cases, using a collapse window to record a value even if it is duplicate. By doing that, // a trend could be detected eventually. If will be delayed, but that is fine with slow changing estimates. lastValue := int64(0) if len(t.values) != 0 { lastValue = t.values[len(t.values)-1] } if lastValue == value && t.params.CollapseThreshold > 0 { - if !t.hasFallen || (!t.lastSampleAt.IsZero() && time.Since(t.lastSampleAt) < t.params.CollapseThreshold) { + hasFallen := false + for idx := 1; idx < len(t.values); idx++ { + if t.values[idx] < t.values[idx-1] { + hasFallen = true + break + } + } + if !hasFallen || (!t.lastSampleAt.IsZero() && time.Since(t.lastSampleAt) < t.params.CollapseThreshold) { return } } - if lastValue > value { - t.hasFallen = true - } t.lastSampleAt = time.Now() if len(t.values) == t.params.RequiredSamples {