De-centralize some configs to where they are used. (#3162)

* De-centralize some configs to where they are used.

And make default variables.

Renaming a bit, but these are all internal config and have not been
added to documented config.

* Keep documented config as is.

* test

* typo
This commit is contained in:
Raja Subramanian
2024-11-08 12:47:30 +05:30
committed by GitHub
parent 55d084fd18
commit 86383b2271
20 changed files with 426 additions and 327 deletions
+26 -15
View File
@@ -17,7 +17,6 @@ package streamallocator
import (
"fmt"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/protocol/logger"
)
@@ -69,9 +68,28 @@ func (c ChannelCongestionReason) String() string {
// ------------------------------------------------
type ChannelObserverConfig struct {
Estimate TrendDetectorConfig `yaml:"estimate,omitempty"`
Nack NackTrackerConfig `yaml:"nack,omitempty"`
}
var (
DefaultChannelObserverConfigProbe = ChannelObserverConfig{
Estimate: DefaultTrendDetectorConfigProbe,
Nack: DefaultNackTrackerConfigProbe,
}
DefaultChannelObserverConfigNonProbe = ChannelObserverConfig{
Estimate: DefaultTrendDetectorConfigNonProbe,
Nack: DefaultNackTrackerConfigNonProbe,
}
)
// ------------------------------------------------
type ChannelObserverParams struct {
Name string
Config config.CongestionControlChannelObserverConfig
Config ChannelObserverConfig
}
type ChannelObserver struct {
@@ -87,21 +105,14 @@ func NewChannelObserver(params ChannelObserverParams, logger logger.Logger) *Cha
params: params,
logger: logger,
estimateTrend: NewTrendDetector(TrendDetectorParams{
Name: params.Name + "-estimate",
Logger: logger,
RequiredSamples: params.Config.EstimateRequiredSamples,
RequiredSamplesMin: params.Config.EstimateRequiredSamplesMin,
DownwardTrendThreshold: params.Config.EstimateDownwardTrendThreshold,
DownwardTrendMaxWait: params.Config.EstimateDownwardTrendMaxWait,
CollapseThreshold: params.Config.EstimateCollapseThreshold,
ValidityWindow: params.Config.EstimateValidityWindow,
Name: params.Name + "-estimate",
Logger: logger,
Config: params.Config.Estimate,
}),
nackTracker: NewNackTracker(NackTrackerParams{
Name: params.Name + "-nack",
Logger: logger,
WindowMinDuration: params.Config.NackWindowMinDuration,
WindowMaxDuration: params.Config.NackWindowMaxDuration,
RatioThreshold: params.Config.NackRatioThreshold,
Name: params.Name + "-nack",
Logger: logger,
Config: params.Config.Nack,
}),
}
}
+28 -8
View File
@@ -23,12 +23,32 @@ import (
// ------------------------------------------------
type NackTrackerConfig struct {
WindowMinDuration time.Duration `yaml:"window_min_duration,omitempty"`
WindowMaxDuration time.Duration `yaml:"window_max_duration,omitempty"`
RatioThreshold float64 `yaml:"ratio_threshold,omitempty"`
}
var (
DefaultNackTrackerConfigProbe = NackTrackerConfig{
WindowMinDuration: 500 * time.Millisecond,
WindowMaxDuration: 1 * time.Second,
RatioThreshold: 0.04,
}
DefaultNackTrackerConfigNonProbe = NackTrackerConfig{
WindowMinDuration: 2 * time.Second,
WindowMaxDuration: 3 * time.Second,
RatioThreshold: 0.08,
}
)
// ------------------------------------------------
type NackTrackerParams struct {
Name string
Logger logger.Logger
WindowMinDuration time.Duration
WindowMaxDuration time.Duration
RatioThreshold float64
Name string
Logger logger.Logger
Config NackTrackerConfig
}
type NackTracker struct {
@@ -52,7 +72,7 @@ func NewNackTracker(params NackTrackerParams) *NackTracker {
}
func (n *NackTracker) Add(packets uint32, repeatedNacks uint32) {
if n.params.WindowMaxDuration != 0 && !n.windowStartTime.IsZero() && time.Since(n.windowStartTime) > n.params.WindowMaxDuration {
if n.params.Config.WindowMaxDuration != 0 && !n.windowStartTime.IsZero() && time.Since(n.windowStartTime) > n.params.Config.WindowMaxDuration {
// STREAM-ALLOCATOR-DATA n.updateHistory()
n.windowStartTime = time.Time{}
@@ -89,8 +109,8 @@ func (n *NackTracker) GetRatio() float64 {
}
func (n *NackTracker) IsTriggered() bool {
if n.params.WindowMinDuration != 0 && !n.windowStartTime.IsZero() && time.Since(n.windowStartTime) > n.params.WindowMinDuration {
return n.GetRatio() > n.params.RatioThreshold
if n.params.Config.WindowMinDuration != 0 && !n.windowStartTime.IsZero() && time.Since(n.windowStartTime) > n.params.Config.WindowMinDuration {
return n.GetRatio() > n.params.Config.RatioThreshold
}
return false
+41 -2
View File
@@ -18,14 +18,53 @@ import (
"sync"
"time"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/protocol/logger"
)
// ---------------------------------------------------------------------------
type ProbeControllerConfig struct {
BaseInterval time.Duration `yaml:"base_interval,omitempty"`
BackoffFactor float64 `yaml:"backoff_factor,omitempty"`
MaxInterval time.Duration `yaml:"max_interval,omitempty"`
SettleWait time.Duration `yaml:"settle_wait,omitempty"`
SettleWaitMax time.Duration `yaml:"settle_wait_max,omitempty"`
TrendWait time.Duration `yaml:"trend_wait,omitempty"`
OveragePct int64 `yaml:"overage_pct,omitempty"`
MinBps int64 `yaml:"min_bps,omitempty"`
MinDuration time.Duration `yaml:"min_duration,omitempty"`
MaxDuration time.Duration `yaml:"max_duration,omitempty"`
DurationOverflowFactor float64 `yaml:"duration_overflow_factor,omitempty"`
DurationIncreaseFactor float64 `yaml:"duration_increase_factor,omitempty"`
}
var (
DefaultProbeControllerConfig = ProbeControllerConfig{
BaseInterval: 3 * time.Second,
BackoffFactor: 1.5,
MaxInterval: 2 * time.Minute,
SettleWait: 250 * time.Millisecond,
SettleWaitMax: 10 * time.Second,
TrendWait: 2 * time.Second,
OveragePct: 120,
MinBps: 200_000,
MinDuration: 200 * time.Millisecond,
MaxDuration: 20 * time.Second,
DurationOverflowFactor: 1.25,
DurationIncreaseFactor: 1.5,
}
)
// ---------------------------------------------------------------------------
type ProbeControllerParams struct {
Config config.CongestionControlProbeConfig
Config ProbeControllerConfig
Prober *Prober
Logger logger.Logger
}
+45 -11
View File
@@ -28,7 +28,6 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/utils"
@@ -137,8 +136,41 @@ func (e Event) String() string {
// ---------------------------------------------------------------------------
type (
ProbeMode string
)
const (
ProbeModePadding ProbeMode = "padding"
ProbeModeMedia ProbeMode = "media"
)
type StreamAllocatorConfig struct {
NackRatioAttenuator float64 `yaml:"nack_ratio_attenuator,omitempty"`
ExpectedUsageThreshold float64 `yaml:"expected_usage_threshold,omitempty"`
ProbeMode ProbeMode `yaml:"probe_mode,omitempty"`
MinChannelCapacity int64 `yaml:"min_channel_capacity,omitempty"`
ProbeController ProbeControllerConfig `yaml:"probe_controller,omitempty"`
ChannelObserverProbe ChannelObserverConfig `yaml:"channel_observer_probe,omitempty"`
ChannelObserverNonProbe ChannelObserverConfig `yaml:"channel_observer_non_probe,omitempty"`
DisableEstimationUnmanagedTracks bool `yaml:"disable_etimation_unmanaged_tracks,omitempty"`
}
var (
DefaultStreamAllocatorConfig = StreamAllocatorConfig{
NackRatioAttenuator: 0.4,
ExpectedUsageThreshold: 0.95,
ProbeMode: ProbeModePadding,
ProbeController: DefaultProbeControllerConfig,
ChannelObserverProbe: DefaultChannelObserverConfigProbe,
ChannelObserverNonProbe: DefaultChannelObserverConfigNonProbe,
}
)
// ---------------------------------------------------------------------------
type StreamAllocatorParams struct {
Config config.CongestionControlConfig
Config StreamAllocatorConfig
Logger logger.Logger
}
@@ -149,6 +181,7 @@ type StreamAllocator struct {
bwe cc.BandwidthEstimator
enabled bool
allowPause bool
lastReceivedEstimate int64
@@ -174,10 +207,11 @@ type StreamAllocator struct {
isStopped atomic.Bool
}
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
func NewStreamAllocator(params StreamAllocatorParams, enabled bool, allowPause bool) *StreamAllocator {
s := &StreamAllocator{
params: params,
allowPause: params.Config.AllowPause,
enabled: enabled,
allowPause: allowPause,
prober: NewProber(ProberParams{
Logger: params.Logger,
}),
@@ -191,7 +225,7 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
}
s.probeController = NewProbeController(ProbeControllerParams{
Config: s.params.Config.ProbeConfig,
Config: s.params.Config.ProbeController,
Prober: s.prober,
Logger: params.Logger,
})
@@ -869,7 +903,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
s.probeController.AbortProbe()
// if not deficient, free pass allocate track
if !s.params.Config.Enabled || s.state == streamAllocatorStateStable || !track.IsManaged() {
if !s.enabled || s.state == streamAllocatorStateStable || !track.IsManaged() {
update := NewStreamStateUpdate()
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal)
updateStreamStateChange(track, allocation, update)
@@ -1096,7 +1130,7 @@ boost_loop:
}
func (s *StreamAllocator) allocateAllTracks() {
if !s.params.Config.Enabled {
if !s.enabled {
// nothing else to do when disabled
return
}
@@ -1276,7 +1310,7 @@ func (s *StreamAllocator) newChannelObserverProbe() *ChannelObserver {
return NewChannelObserver(
ChannelObserverParams{
Name: "probe",
Config: s.params.Config.ChannelObserverProbeConfig,
Config: s.params.Config.ChannelObserverProbe,
},
s.params.Logger,
)
@@ -1286,7 +1320,7 @@ func (s *StreamAllocator) newChannelObserverNonProbe() *ChannelObserver {
return NewChannelObserver(
ChannelObserverParams{
Name: "non-probe",
Config: s.params.Config.ChannelObserverNonProbeConfig,
Config: s.params.Config.ChannelObserverNonProbe,
},
s.params.Logger,
)
@@ -1325,10 +1359,10 @@ func (s *StreamAllocator) maybeProbe() {
}
switch s.params.Config.ProbeMode {
case config.CongestionControlProbeModeMedia:
case ProbeModeMedia:
s.maybeProbeWithMedia()
s.adjustState()
case config.CongestionControlProbeModePadding:
case ProbeModePadding:
s.maybeProbeWithPadding()
}
}
+43 -17
View File
@@ -70,15 +70,41 @@ func trendDetectorSampleListToString(samples []trendDetectorSample) string {
// ------------------------------------------------
type TrendDetectorConfig struct {
RequiredSamples int `yaml:"required_samples,omitempty"`
RequiredSamplesMin int `yaml:"required_samples_min,omitempty"`
DownwardTrendThreshold float64 `yaml:"downward_trend_threshold,omitempty"`
DownwardTrendMaxWait time.Duration `yaml:"downward_trend_max_wait,omitempty"`
CollapseThreshold time.Duration `yaml:"collapse_threshold,omitempty"`
ValidityWindow time.Duration `yaml:"validity_window,omitempty"`
}
var (
DefaultTrendDetectorConfigProbe = TrendDetectorConfig{
RequiredSamples: 3,
RequiredSamplesMin: 3,
DownwardTrendThreshold: 0.0,
DownwardTrendMaxWait: 5 * time.Second,
CollapseThreshold: 0,
ValidityWindow: 10 * time.Second,
}
DefaultTrendDetectorConfigNonProbe = TrendDetectorConfig{
RequiredSamples: 12,
RequiredSamplesMin: 8,
DownwardTrendThreshold: -0.6,
DownwardTrendMaxWait: 5 * time.Second,
CollapseThreshold: 500 * time.Millisecond,
ValidityWindow: 10 * time.Second,
}
)
// ------------------------------------------------
type TrendDetectorParams struct {
Name string
Logger logger.Logger
RequiredSamples int
RequiredSamplesMin int
DownwardTrendThreshold float64
DownwardTrendMaxWait time.Duration
CollapseThreshold time.Duration
ValidityWindow time.Duration
Name string
Logger logger.Logger
Config TrendDetectorConfig
}
type TrendDetector struct {
@@ -134,7 +160,7 @@ func (t *TrendDetector) AddValue(value int64) {
if len(t.samples) != 0 {
lastSample = &t.samples[len(t.samples)-1]
}
if lastSample != nil && lastSample.value == value && t.params.CollapseThreshold > 0 && time.Since(lastSample.at) < t.params.CollapseThreshold {
if lastSample != nil && lastSample.value == value && t.params.Config.CollapseThreshold > 0 && time.Since(lastSample.at) < t.params.Config.CollapseThreshold {
return
}
@@ -156,7 +182,7 @@ func (t *TrendDetector) GetDirection() TrendDirection {
}
func (t *TrendDetector) HasEnoughSamples() bool {
return t.numSamples >= t.params.RequiredSamples
return t.numSamples >= t.params.Config.RequiredSamples
}
func (t *TrendDetector) ToString() string {
@@ -173,13 +199,13 @@ func (t *TrendDetector) prune() {
// prune based on a few rules
// 1. If there are more than required samples
if len(t.samples) > t.params.RequiredSamples {
t.samples = t.samples[len(t.samples)-t.params.RequiredSamples:]
if len(t.samples) > t.params.Config.RequiredSamples {
t.samples = t.samples[len(t.samples)-t.params.Config.RequiredSamples:]
}
// 2. drop samples that are too old
if len(t.samples) != 0 && t.params.ValidityWindow > 0 {
cutoffTime := time.Now().Add(-t.params.ValidityWindow)
if len(t.samples) != 0 && t.params.Config.ValidityWindow > 0 {
cutoffTime := time.Now().Add(-t.params.Config.ValidityWindow)
cutoffIndex := -1
for i := 0; i < len(t.samples); i++ {
if t.samples[i].at.After(cutoffTime) {
@@ -213,7 +239,7 @@ func (t *TrendDetector) prune() {
}
func (t *TrendDetector) updateDirection() {
if len(t.samples) < t.params.RequiredSamplesMin {
if len(t.samples) < t.params.Config.RequiredSamplesMin {
t.direction = TrendDirectionNeutral
return
}
@@ -223,9 +249,9 @@ func (t *TrendDetector) updateDirection() {
t.direction = TrendDirectionNeutral
switch {
case kt > 0 && len(t.samples) >= t.params.RequiredSamples:
case kt > 0 && len(t.samples) >= t.params.Config.RequiredSamples:
t.direction = TrendDirectionUpward
case kt < t.params.DownwardTrendThreshold && (len(t.samples) >= t.params.RequiredSamples || t.samples[len(t.samples)-1].at.Sub(t.samples[0].at) > t.params.DownwardTrendMaxWait):
case kt < t.params.Config.DownwardTrendThreshold && (len(t.samples) >= t.params.Config.RequiredSamples || t.samples[len(t.samples)-1].at.Sub(t.samples[0].at) > t.params.Config.DownwardTrendMaxWait):
t.direction = TrendDirectionDownward
}
}