@@ -15,6 +15,7 @@
|
||||
package streamallocator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -22,22 +23,51 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/pacer"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils/mono"
|
||||
)
|
||||
|
||||
const (
|
||||
cDefaultRTT = float64(0.070) // 70 ms
|
||||
cRTTSmoothingFactor = float64(0.5)
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type ProbeControllerState int
|
||||
|
||||
const (
|
||||
ProbeControllerStateNone ProbeControllerState = iota
|
||||
ProbeControllerStateProbing
|
||||
ProbeControllerStateHangover
|
||||
)
|
||||
|
||||
func (p ProbeControllerState) String() string {
|
||||
switch p {
|
||||
case ProbeControllerStateNone:
|
||||
return "NONE"
|
||||
case ProbeControllerStateProbing:
|
||||
return "PROBING"
|
||||
case ProbeControllerStateHangover:
|
||||
return "HANGOVER"
|
||||
default:
|
||||
return fmt.Sprintf("%d", int(p))
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
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"`
|
||||
SettleWaitNumRTT uint32 `yaml:"settle_wait_num_rtt,omitempty"`
|
||||
SettleWaitMin time.Duration `yaml:"settle_wait_min,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"`
|
||||
|
||||
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"`
|
||||
DurationIncreaseFactor float64 `yaml:"duration_increase_factor,omitempty"`
|
||||
@@ -49,13 +79,13 @@ var (
|
||||
BackoffFactor: 1.5,
|
||||
MaxInterval: 2 * time.Minute,
|
||||
|
||||
SettleWait: 250 * time.Millisecond,
|
||||
SettleWaitMax: 10 * time.Second,
|
||||
SettleWaitNumRTT: 10,
|
||||
SettleWaitMin: 500 * time.Millisecond,
|
||||
SettleWaitMax: 10 * time.Second,
|
||||
|
||||
TrendWait: 2 * time.Second,
|
||||
OveragePct: 120,
|
||||
MinBps: 200_000,
|
||||
|
||||
OveragePct: 120,
|
||||
MinBps: 200_000,
|
||||
MinDuration: 200 * time.Millisecond,
|
||||
MaxDuration: 20 * time.Second,
|
||||
DurationIncreaseFactor: 1.5,
|
||||
@@ -75,23 +105,23 @@ type ProbeControllerParams struct {
|
||||
type ProbeController struct {
|
||||
params ProbeControllerParams
|
||||
|
||||
lock sync.RWMutex
|
||||
probeInterval time.Duration
|
||||
lastProbeStartTime time.Time
|
||||
probeGoalBps int64
|
||||
probeClusterId ccutils.ProbeClusterId
|
||||
doneProbeClusterInfo ccutils.ProbeClusterInfo
|
||||
abortedProbeClusterId ccutils.ProbeClusterId
|
||||
goalReachedProbeClusterId ccutils.ProbeClusterId
|
||||
probeTrendObserved bool
|
||||
probeEndTime time.Time
|
||||
probeDuration time.Duration
|
||||
lock sync.RWMutex
|
||||
|
||||
state ProbeControllerState
|
||||
stateSwitchedAt time.Time
|
||||
|
||||
pci ccutils.ProbeClusterInfo
|
||||
rtt float64
|
||||
|
||||
probeInterval time.Duration
|
||||
probeDuration time.Duration
|
||||
nextProbeEarliestAt time.Time
|
||||
}
|
||||
|
||||
func NewProbeController(params ProbeControllerParams) *ProbeController {
|
||||
p := &ProbeController{
|
||||
params: params,
|
||||
probeDuration: params.Config.MinDuration,
|
||||
params: params,
|
||||
rtt: cDefaultRTT,
|
||||
}
|
||||
|
||||
p.Reset()
|
||||
@@ -102,260 +132,157 @@ func (p *ProbeController) Reset() {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.lastProbeStartTime = time.Now()
|
||||
|
||||
p.resetProbeIntervalLocked()
|
||||
p.resetProbeDurationLocked()
|
||||
|
||||
p.StopProbe()
|
||||
p.clearProbeLocked()
|
||||
}
|
||||
|
||||
func (p *ProbeController) ProbeClusterDone(probeClusterId ccutils.ProbeClusterId) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
if p.probeClusterId != probeClusterId {
|
||||
p.params.Logger.Debugw("not expected probe cluster", "probeClusterId", p.probeClusterId, "resetProbeClusterId", probeClusterId)
|
||||
} else {
|
||||
p.doneProbeClusterInfo = p.params.Pacer.EndProbeCluster(probeClusterId)
|
||||
p.params.Prober.ClusterDone(p.doneProbeClusterInfo)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProbeController) MaybeFinalizeProbe(
|
||||
isComplete bool,
|
||||
trend bwe.ChannelTrend,
|
||||
lowestEstimate int64,
|
||||
) (isHandled bool, isNotFailing bool, isGoalReached bool) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
if !p.isInProbeLocked() {
|
||||
return false, false, false
|
||||
}
|
||||
|
||||
if p.goalReachedProbeClusterId != ccutils.ProbeClusterIdInvalid {
|
||||
// finalise goal reached probe cluster
|
||||
p.finalizeProbeLocked(bwe.ChannelTrendNeutral)
|
||||
return true, true, true
|
||||
}
|
||||
|
||||
if (isComplete || p.abortedProbeClusterId != ccutils.ProbeClusterIdInvalid) &&
|
||||
p.probeEndTime.IsZero() &&
|
||||
p.doneProbeClusterInfo.ProbeClusterId != ccutils.ProbeClusterIdInvalid && p.doneProbeClusterInfo.ProbeClusterId == p.probeClusterId {
|
||||
// ensure any queueing due to probing is flushed
|
||||
// STREAM-ALLOCATOR-TODO: ProbeControllerConfig.SettleWait should actually be a certain number of RTTs.
|
||||
expectedDuration := float64(0.0)
|
||||
if lowestEstimate != 0 {
|
||||
expectedDuration = float64(p.doneProbeClusterInfo.Bytes()*8*1000) / float64(lowestEstimate)
|
||||
}
|
||||
queueTime := expectedDuration - float64(p.doneProbeClusterInfo.Duration().Milliseconds())
|
||||
if queueTime < 0.0 {
|
||||
queueTime = 0.0
|
||||
}
|
||||
queueWait := (time.Duration(queueTime) * time.Millisecond) + p.params.Config.SettleWait
|
||||
if queueWait > p.params.Config.SettleWaitMax {
|
||||
queueWait = p.params.Config.SettleWaitMax
|
||||
}
|
||||
p.probeEndTime = p.lastProbeStartTime.Add(queueWait + p.doneProbeClusterInfo.Duration())
|
||||
p.params.Logger.Debugw(
|
||||
"setting probe end time",
|
||||
"probeClusterId", p.probeClusterId,
|
||||
"expectedDuration", expectedDuration,
|
||||
"queueTime", queueTime,
|
||||
"queueWait", queueWait,
|
||||
"probeEndTime", p.probeEndTime,
|
||||
)
|
||||
}
|
||||
|
||||
if !p.probeEndTime.IsZero() && time.Now().After(p.probeEndTime) {
|
||||
// finalize aborted or non-failing but non-goal-reached probe cluster
|
||||
return true, p.finalizeProbeLocked(trend), false
|
||||
}
|
||||
|
||||
return false, false, false
|
||||
}
|
||||
|
||||
func (p *ProbeController) DoesProbeNeedFinalize() bool {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
return p.abortedProbeClusterId != ccutils.ProbeClusterIdInvalid || p.goalReachedProbeClusterId != ccutils.ProbeClusterIdInvalid
|
||||
}
|
||||
|
||||
func (p *ProbeController) finalizeProbeLocked(trend bwe.ChannelTrend) (isNotFailing bool) {
|
||||
aborted := p.probeClusterId == p.abortedProbeClusterId
|
||||
|
||||
p.clearProbeLocked()
|
||||
|
||||
if aborted || trend == bwe.ChannelTrendCongesting {
|
||||
// failed probe, backoff
|
||||
p.backoffProbeIntervalLocked()
|
||||
p.resetProbeDurationLocked()
|
||||
return false
|
||||
}
|
||||
|
||||
// reset probe interval and increase probe duration on a upward trending probe
|
||||
p.resetProbeIntervalLocked()
|
||||
if trend == bwe.ChannelTrendClearing {
|
||||
p.increaseProbeDurationLocked()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *ProbeController) InitProbe(probeGoalDeltaBps int64, expectedBandwidthUsage int64) (ccutils.ProbeClusterId, int64) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.lastProbeStartTime = time.Now()
|
||||
|
||||
// overshoot a bit to account for noise (in measurement/estimate etc)
|
||||
desiredIncreaseBps := (probeGoalDeltaBps * p.params.Config.OveragePct) / 100
|
||||
if desiredIncreaseBps < p.params.Config.MinBps {
|
||||
desiredIncreaseBps = p.params.Config.MinBps
|
||||
}
|
||||
p.probeGoalBps = expectedBandwidthUsage + desiredIncreaseBps
|
||||
|
||||
p.doneProbeClusterInfo = ccutils.ProbeClusterInfoInvalid
|
||||
p.abortedProbeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
p.goalReachedProbeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
|
||||
p.probeTrendObserved = false
|
||||
|
||||
p.probeEndTime = time.Time{}
|
||||
|
||||
p.probeClusterId = p.params.Prober.AddCluster(
|
||||
ccutils.ProbeClusterModeUniform,
|
||||
int(p.probeGoalBps),
|
||||
int(expectedBandwidthUsage),
|
||||
p.probeDuration,
|
||||
)
|
||||
|
||||
p.pollProbe(p.probeClusterId, expectedBandwidthUsage)
|
||||
|
||||
return p.probeClusterId, p.probeGoalBps
|
||||
}
|
||||
|
||||
func (p *ProbeController) pollProbe(probeClusterId ccutils.ProbeClusterId, expectedBandwidthUsage int64) {
|
||||
p.params.BWE.ProbingStart(expectedBandwidthUsage)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
p.lock.Lock()
|
||||
if p.probeClusterId != probeClusterId {
|
||||
p.lock.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
done := false
|
||||
|
||||
_, trend, _, highestEstimate := p.params.BWE.GetProbeStatus()
|
||||
if !p.probeTrendObserved && trend != bwe.ChannelTrendNeutral {
|
||||
p.probeTrendObserved = true
|
||||
}
|
||||
|
||||
switch {
|
||||
case trend == bwe.ChannelTrendCongesting:
|
||||
// stop immediately if the probe is congesting channel more
|
||||
p.params.Logger.Infow(
|
||||
"stream allocator: probe: aborting, channel is congesting",
|
||||
"cluster", probeClusterId,
|
||||
)
|
||||
p.abortProbeLocked()
|
||||
done = true
|
||||
break
|
||||
|
||||
case highestEstimate > p.probeGoalBps:
|
||||
// reached goal, stop probing
|
||||
p.params.Logger.Infow(
|
||||
"stream allocator: probe: stopping, goal reached",
|
||||
"cluster", probeClusterId,
|
||||
"goal", p.probeGoalBps,
|
||||
"highestEstimate", highestEstimate,
|
||||
)
|
||||
p.goalReachedProbeClusterId = p.probeClusterId
|
||||
p.StopProbe()
|
||||
done = true
|
||||
break
|
||||
|
||||
case !p.probeTrendObserved && time.Since(p.lastProbeStartTime) > p.params.Config.TrendWait:
|
||||
//
|
||||
// More of a safety net.
|
||||
// In rare cases, the estimate gets stuck. Prevent from probe running amok
|
||||
// STREAM-ALLOCATOR-TODO: Need more testing here to ensure that probe does not cause a lot of damage
|
||||
//
|
||||
p.params.Logger.Infow("stream allocator: probe: aborting, no trend", "cluster", probeClusterId)
|
||||
p.abortProbeLocked()
|
||||
done = true
|
||||
break
|
||||
}
|
||||
p.lock.Unlock()
|
||||
|
||||
if done {
|
||||
return
|
||||
}
|
||||
|
||||
// BWE-TODO: do not hard code sleep time
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (p *ProbeController) clearProbeLocked() {
|
||||
p.probeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
p.doneProbeClusterInfo = ccutils.ProbeClusterInfoInvalid
|
||||
p.abortedProbeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
p.goalReachedProbeClusterId = ccutils.ProbeClusterIdInvalid
|
||||
}
|
||||
|
||||
func (p *ProbeController) backoffProbeIntervalLocked() {
|
||||
p.probeInterval = time.Duration(p.probeInterval.Seconds()*p.params.Config.BackoffFactor) * time.Second
|
||||
if p.probeInterval > p.params.Config.MaxInterval {
|
||||
p.probeInterval = p.params.Config.MaxInterval
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProbeController) resetProbeIntervalLocked() {
|
||||
p.state = ProbeControllerStateNone
|
||||
p.stateSwitchedAt = mono.Now()
|
||||
p.pci = ccutils.ProbeClusterInfoInvalid
|
||||
p.probeInterval = p.params.Config.BaseInterval
|
||||
}
|
||||
|
||||
func (p *ProbeController) resetProbeDurationLocked() {
|
||||
p.probeDuration = p.params.Config.MinDuration
|
||||
}
|
||||
|
||||
func (p *ProbeController) increaseProbeDurationLocked() {
|
||||
p.probeDuration = time.Duration(float64(p.probeDuration.Milliseconds())*p.params.Config.DurationIncreaseFactor) * time.Millisecond
|
||||
if p.probeDuration > p.params.Config.MaxDuration {
|
||||
p.probeDuration = p.params.Config.MaxDuration
|
||||
func (p *ProbeController) UpdateRTT(rtt float64) {
|
||||
if rtt == 0 {
|
||||
p.rtt = cDefaultRTT
|
||||
} else {
|
||||
if p.rtt == 0 {
|
||||
p.rtt = rtt
|
||||
} else {
|
||||
p.rtt = cRTTSmoothingFactor*rtt + (1.0-cRTTSmoothingFactor)*p.rtt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProbeController) StopProbe() {
|
||||
p.params.Prober.Reset(p.params.Pacer.EndProbeCluster(p.probeClusterId))
|
||||
}
|
||||
|
||||
func (p *ProbeController) AbortProbe() {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.abortProbeLocked()
|
||||
}
|
||||
|
||||
func (p *ProbeController) abortProbeLocked() {
|
||||
p.abortedProbeClusterId = p.probeClusterId
|
||||
p.StopProbe()
|
||||
}
|
||||
|
||||
func (p *ProbeController) isInProbeLocked() bool {
|
||||
return p.probeClusterId != ccutils.ProbeClusterIdInvalid
|
||||
}
|
||||
|
||||
func (p *ProbeController) CanProbe() bool {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
return time.Since(p.lastProbeStartTime) >= p.probeInterval && p.probeClusterId == ccutils.ProbeClusterIdInvalid
|
||||
return p.state == ProbeControllerStateNone && mono.Now().After(p.nextProbeEarliestAt)
|
||||
}
|
||||
|
||||
func (p *ProbeController) MaybeInitiateProbe(availableBandwidthBps int64, probeGoalDeltaBps int64, expectedBandwidthUsage int64) (ccutils.ProbeClusterGoal, bool) {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
if p.state != ProbeControllerStateNone {
|
||||
// already probing or in probe hangover, don't start a new one
|
||||
return ccutils.ProbeClusterGoal{}, false
|
||||
}
|
||||
|
||||
if mono.Now().Before(p.nextProbeEarliestAt) {
|
||||
return ccutils.ProbeClusterGoal{}, false
|
||||
}
|
||||
|
||||
// overshoot a bit to account for noise (in measurement/estimate etc)
|
||||
desiredIncreaseBps := (probeGoalDeltaBps * p.params.Config.OveragePct) / 100
|
||||
if desiredIncreaseBps < p.params.Config.MinBps {
|
||||
desiredIncreaseBps = p.params.Config.MinBps
|
||||
}
|
||||
return ccutils.ProbeClusterGoal{
|
||||
AvailableBandwidthBps: int(availableBandwidthBps),
|
||||
ExpectedUsageBps: int(expectedBandwidthUsage),
|
||||
DesiredBps: int(expectedBandwidthUsage + desiredIncreaseBps),
|
||||
Duration: p.probeDuration,
|
||||
}, true
|
||||
}
|
||||
|
||||
func (p *ProbeController) ProbeClusterStarting(pci ccutils.ProbeClusterInfo) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
if p.state != ProbeControllerStateNone {
|
||||
p.params.Logger.Warnw("unexpected probe controller state", nil, "state", p.state)
|
||||
}
|
||||
|
||||
p.setState(ProbeControllerStateProbing)
|
||||
p.pci = pci
|
||||
}
|
||||
|
||||
func (p *ProbeController) ProbeClusterDone(pci ccutils.ProbeClusterInfo) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
if p.state != ProbeControllerStateProbing {
|
||||
p.params.Logger.Warnw("unexpected probe controller state", nil, "state", p.state)
|
||||
}
|
||||
|
||||
if p.pci.Id != pci.Id {
|
||||
p.params.Logger.Warnw("not expected probe cluster", nil, "expectedId", p.pci.Id, "doneId", pci.Id)
|
||||
}
|
||||
|
||||
p.pci.Result = pci.Result
|
||||
p.params.Prober.ClusterDone(pci)
|
||||
|
||||
p.setState(ProbeControllerStateHangover)
|
||||
}
|
||||
|
||||
func (p *ProbeController) MaybeFinalizeProbe() (ccutils.ProbeClusterInfo, bool) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
if p.state != ProbeControllerStateHangover {
|
||||
return ccutils.ProbeClusterInfoInvalid, false
|
||||
}
|
||||
|
||||
settleWait := time.Duration(float64(p.params.Config.SettleWaitNumRTT) * p.rtt * float64(time.Second))
|
||||
if settleWait < p.params.Config.SettleWaitMin {
|
||||
settleWait = p.params.Config.SettleWaitMin
|
||||
}
|
||||
if settleWait > p.params.Config.SettleWaitMax {
|
||||
settleWait = p.params.Config.SettleWaitMax
|
||||
}
|
||||
if time.Since(p.stateSwitchedAt) < settleWait {
|
||||
return ccutils.ProbeClusterInfoInvalid, false
|
||||
}
|
||||
|
||||
p.setState(ProbeControllerStateNone)
|
||||
return p.pci, true
|
||||
}
|
||||
|
||||
func (p *ProbeController) ProbeCongestionSignal(isCongestionClearing bool) {
|
||||
if !isCongestionClearing {
|
||||
// wait longer till next probe
|
||||
p.probeInterval = time.Duration(p.probeInterval.Seconds()*p.params.Config.BackoffFactor) * time.Second
|
||||
if p.probeInterval > p.params.Config.MaxInterval {
|
||||
p.probeInterval = p.params.Config.MaxInterval
|
||||
}
|
||||
|
||||
// revert back to starting with shortest probe
|
||||
p.probeDuration = p.params.Config.MinDuration
|
||||
} else {
|
||||
// probe can be started again after minimal interval as previous congestion signal indicated congestion clearing
|
||||
p.probeInterval = p.params.Config.BaseInterval
|
||||
|
||||
// can do longer probe after a good probe
|
||||
p.probeDuration = time.Duration(float64(p.probeDuration.Milliseconds())*p.params.Config.DurationIncreaseFactor) * time.Millisecond
|
||||
if p.probeDuration > p.params.Config.MaxDuration {
|
||||
p.probeDuration = p.params.Config.MaxDuration
|
||||
}
|
||||
}
|
||||
|
||||
if p.pci.CreatedAt.IsZero() {
|
||||
p.nextProbeEarliestAt = mono.Now().Add(p.probeInterval)
|
||||
} else {
|
||||
p.nextProbeEarliestAt = p.pci.CreatedAt.Add(p.probeInterval)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ProbeController) GetActiveProbeClusterId() ccutils.ProbeClusterId {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
if p.state == ProbeControllerStateNone {
|
||||
return ccutils.ProbeClusterIdInvalid
|
||||
}
|
||||
|
||||
return p.pci.Id
|
||||
}
|
||||
|
||||
func (p *ProbeController) setState(state ProbeControllerState) {
|
||||
if state == p.state {
|
||||
return
|
||||
}
|
||||
|
||||
p.state = state
|
||||
p.stateSwitchedAt = mono.Now()
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
@@ -50,6 +50,8 @@ const (
|
||||
FlagAllowOvershootInProbe = true
|
||||
FlagAllowOvershootInCatchup = false
|
||||
FlagAllowOvershootInBoost = true
|
||||
|
||||
cRTTPullInterval = 30 * time.Second
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -82,14 +84,15 @@ const (
|
||||
streamAllocatorSignalAdjustState
|
||||
streamAllocatorSignalEstimate
|
||||
streamAllocatorSignalPeriodicPing
|
||||
streamAllocatorSignalProbeClusterSwitch
|
||||
streamAllocatorSignalSendProbe
|
||||
streamAllocatorSignalPacerProbeObserverClusterComplete
|
||||
streamAllocatorSignalResume
|
||||
streamAllocatorSignalSetAllowPause
|
||||
streamAllocatorSignalSetChannelCapacity
|
||||
// STREAM-ALLOCATOR-DATA streamAllocatorSignalNACK
|
||||
// STREAM-ALLOCATOR-DATA streamAllocatorSignalRTCPReceiverReport
|
||||
streamAllocatorSignalCongestionStateChange
|
||||
streamAllocatorSignalPacerProbeObserverClusterComplete
|
||||
)
|
||||
|
||||
func (s streamAllocatorSignal) String() string {
|
||||
@@ -104,8 +107,12 @@ func (s streamAllocatorSignal) String() string {
|
||||
return "ESTIMATE"
|
||||
case streamAllocatorSignalPeriodicPing:
|
||||
return "PERIODIC_PING"
|
||||
case streamAllocatorSignalProbeClusterSwitch:
|
||||
return "PROBE_CLUSTER_SWITCH"
|
||||
case streamAllocatorSignalSendProbe:
|
||||
return "SEND_PROBE"
|
||||
case streamAllocatorSignalPacerProbeObserverClusterComplete:
|
||||
return "PACER_PROBE_OBSERVER_CLUSTER_COMPLETE"
|
||||
case streamAllocatorSignalResume:
|
||||
return "RESUME"
|
||||
case streamAllocatorSignalSetAllowPause:
|
||||
@@ -120,8 +127,6 @@ func (s streamAllocatorSignal) String() string {
|
||||
*/
|
||||
case streamAllocatorSignalCongestionStateChange:
|
||||
return "CONGESTION_STATE_CHANGE"
|
||||
case streamAllocatorSignalPacerProbeObserverClusterComplete:
|
||||
return "PACER_PROBE_OBSERVER_CLUSTER_COMPLETE"
|
||||
default:
|
||||
return fmt.Sprintf("%d", int(s))
|
||||
}
|
||||
@@ -168,10 +173,11 @@ var (
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type StreamAllocatorParams struct {
|
||||
Config StreamAllocatorConfig
|
||||
BWE bwe.BWE
|
||||
Pacer pacer.Pacer
|
||||
Logger logger.Logger
|
||||
Config StreamAllocatorConfig
|
||||
BWE bwe.BWE
|
||||
Pacer pacer.Pacer
|
||||
RTTGetter func() (float64, bool)
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type StreamAllocator struct {
|
||||
@@ -188,8 +194,7 @@ type StreamAllocator struct {
|
||||
overriddenChannelCapacity int64
|
||||
|
||||
probeController *ProbeController
|
||||
|
||||
prober *ccutils.Prober
|
||||
prober *ccutils.Prober
|
||||
|
||||
// STREAM-ALLOCATOR-DATA rateMonitor *RateMonitor
|
||||
|
||||
@@ -204,6 +209,8 @@ type StreamAllocator struct {
|
||||
|
||||
eventsQueue *utils.TypedOpsQueue[Event]
|
||||
|
||||
lastRTTTime time.Time
|
||||
|
||||
isStopped atomic.Bool
|
||||
}
|
||||
|
||||
@@ -213,7 +220,9 @@ func NewStreamAllocator(params StreamAllocatorParams, enabled bool, allowPause b
|
||||
enabled: enabled,
|
||||
allowPause: allowPause,
|
||||
// STREAM-ALLOCATOR-DATA rateMonitor: NewRateMonitor(),
|
||||
videoTracks: make(map[livekit.TrackID]*Track),
|
||||
videoTracks: make(map[livekit.TrackID]*Track),
|
||||
state: streamAllocatorStateStable,
|
||||
congestionState: bwe.CongestionStateNone,
|
||||
eventsQueue: utils.NewTypedOpsQueue[Event](utils.OpsQueueParams{
|
||||
Name: "stream-allocator",
|
||||
MinSize: 64,
|
||||
@@ -237,8 +246,6 @@ func NewStreamAllocator(params StreamAllocatorParams, enabled bool, allowPause b
|
||||
s.params.BWE.SetBWEListener(s)
|
||||
s.params.Pacer.SetPacerProbeObserverListener(s)
|
||||
|
||||
s.resetState()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -254,7 +261,8 @@ func (s *StreamAllocator) Stop() {
|
||||
|
||||
// wait for eventsQueue to be done
|
||||
<-s.eventsQueue.Stop()
|
||||
s.probeController.StopProbe()
|
||||
|
||||
s.maybeStopProbe()
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate) error) {
|
||||
@@ -341,13 +349,6 @@ func (s *StreamAllocator) SetChannelCapacity(channelCapacity int64) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) resetState() {
|
||||
s.params.BWE.Reset()
|
||||
s.probeController.Reset()
|
||||
|
||||
s.state = streamAllocatorStateStable
|
||||
}
|
||||
|
||||
// called when a new REMB is received (receive side bandwidth estimation)
|
||||
func (s *StreamAllocator) OnREMB(downTrack *sfu.DownTrack, remb *rtcp.ReceiverEstimatedMaximumBitrate) {
|
||||
//
|
||||
@@ -532,6 +533,14 @@ func (s *StreamAllocator) OnRTCPReceiverReport(downTrack *sfu.DownTrack, rr rtcp
|
||||
}
|
||||
*/
|
||||
|
||||
// called when probe cluster changes
|
||||
func (s *StreamAllocator) OnProbeClusterSwitch(pci ccutils.ProbeClusterInfo) {
|
||||
s.postEvent(Event{
|
||||
Signal: streamAllocatorSignalProbeClusterSwitch,
|
||||
Data: pci,
|
||||
})
|
||||
}
|
||||
|
||||
// called when prober wants to send packet(s)
|
||||
func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
|
||||
s.postEvent(Event{
|
||||
@@ -540,15 +549,6 @@ func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
|
||||
})
|
||||
}
|
||||
|
||||
// called when probe cluster changes
|
||||
func (s *StreamAllocator) OnProbeClusterSwitch(probeClusterId ccutils.ProbeClusterId, desiredBytes int) {
|
||||
s.params.Pacer.StartProbeCluster(probeClusterId, desiredBytes)
|
||||
|
||||
for _, t := range s.getTracks() {
|
||||
t.DownTrack().SetProbeClusterId(probeClusterId)
|
||||
}
|
||||
}
|
||||
|
||||
// called when pacer probe observer observes a cluster completion
|
||||
func (s *StreamAllocator) OnPacerProbeObserverClusterComplete(probeClusterId ccutils.ProbeClusterId) {
|
||||
s.postEvent(Event{
|
||||
@@ -631,8 +631,12 @@ func (s *StreamAllocator) postEvent(event Event) {
|
||||
event.handleSignalEstimate(event)
|
||||
case streamAllocatorSignalPeriodicPing:
|
||||
event.handleSignalPeriodicPing(event)
|
||||
case streamAllocatorSignalProbeClusterSwitch:
|
||||
event.handleSignalProbeClusterSwitch(event)
|
||||
case streamAllocatorSignalSendProbe:
|
||||
event.handleSignalSendProbe(event)
|
||||
case streamAllocatorSignalPacerProbeObserverClusterComplete:
|
||||
event.handleSignalPacerProbeObserverClusterComplete(event)
|
||||
case streamAllocatorSignalResume:
|
||||
event.handleSignalResume(event)
|
||||
case streamAllocatorSignalSetAllowPause:
|
||||
@@ -647,8 +651,6 @@ func (s *StreamAllocator) postEvent(event Event) {
|
||||
*/
|
||||
case streamAllocatorSignalCongestionStateChange:
|
||||
s.handleSignalCongestionStateChange(event)
|
||||
case streamAllocatorSignalPacerProbeObserverClusterComplete:
|
||||
event.handleSignalPacerProbeObserverClusterComplete(event)
|
||||
}
|
||||
}, event)
|
||||
}
|
||||
@@ -688,7 +690,6 @@ func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
||||
|
||||
s.params.BWE.HandleREMB(
|
||||
receivedEstimate,
|
||||
s.probeController.DoesProbeNeedFinalize(), // waiting for goal reached OR aborted probe to finalize
|
||||
s.getExpectedBandwidthUsage(),
|
||||
packetDelta,
|
||||
repeatedNackDelta,
|
||||
@@ -696,15 +697,18 @@ func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
|
||||
// finalize probe if necessary
|
||||
isValidSignal, trend, lowestEstimate, highestEstimate := s.params.BWE.GetProbeStatus()
|
||||
isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe(
|
||||
isValidSignal,
|
||||
trend,
|
||||
lowestEstimate,
|
||||
)
|
||||
if isHandled {
|
||||
s.onProbeDone(isNotFailing, isGoalReached, highestEstimate)
|
||||
// finalize any probe that may have finished/aborted
|
||||
if pci, ok := s.probeController.MaybeFinalizeProbe(); ok {
|
||||
isCongestionClearing, channelCapacity := s.params.BWE.ProbeClusterDone(pci)
|
||||
if isCongestionClearing {
|
||||
if channelCapacity > s.committedChannelCapacity {
|
||||
s.committedChannelCapacity = channelCapacity
|
||||
}
|
||||
|
||||
s.maybeBoostDeficientTracks()
|
||||
}
|
||||
|
||||
s.probeController.ProbeCongestionSignal(isCongestionClearing)
|
||||
}
|
||||
|
||||
// probe if necessary and timing is right
|
||||
@@ -712,12 +716,34 @@ func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
|
||||
s.maybeProbe()
|
||||
}
|
||||
|
||||
if time.Since(s.lastRTTTime) > cRTTPullInterval {
|
||||
s.lastRTTTime = time.Now()
|
||||
|
||||
if s.params.RTTGetter != nil {
|
||||
if rtt, ok := s.params.RTTGetter(); ok {
|
||||
s.probeController.UpdateRTT(rtt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* STREAM-ALLOCATOR-DATA
|
||||
s.monitorRate(s.committedChannelCapacity)
|
||||
s.updateTracksHistory()
|
||||
*/
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalProbeClusterSwitch(event Event) {
|
||||
pci := event.Data.(ccutils.ProbeClusterInfo)
|
||||
s.probeController.ProbeClusterStarting(pci)
|
||||
s.params.BWE.ProbeClusterStarting(pci)
|
||||
|
||||
s.params.Pacer.StartProbeCluster(pci)
|
||||
|
||||
for _, t := range s.getTracks() {
|
||||
t.DownTrack().SetProbeClusterId(pci.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
||||
bytesToSend := event.Data.(int)
|
||||
if bytesToSend <= 0 {
|
||||
@@ -735,6 +761,13 @@ func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalPacerProbeObserverClusterComplete(event Event) {
|
||||
probeClusterId, _ := event.Data.(ccutils.ProbeClusterId)
|
||||
pci := s.params.Pacer.EndProbeCluster(probeClusterId)
|
||||
s.probeController.ProbeClusterDone(pci)
|
||||
s.params.BWE.ProbeClusterDone(pci)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalResume(event Event) {
|
||||
s.videoTracksMu.Lock()
|
||||
track := s.videoTracks[event.TrackID]
|
||||
@@ -791,7 +824,8 @@ func (s *StreamAllocator) handleSignalRTCPReceiverReport(event Event) {
|
||||
func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
|
||||
cscd := event.Data.(congestionStateChangeData)
|
||||
if cscd.congestionState != bwe.CongestionStateNone {
|
||||
s.probeController.AbortProbe()
|
||||
// end/abort any running probe if channel is not clear
|
||||
s.maybeStopProbe()
|
||||
}
|
||||
|
||||
if cscd.congestionState == bwe.CongestionStateEarlyWarning ||
|
||||
@@ -805,7 +839,7 @@ func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
|
||||
if s.isHolding && cscd.congestionState == bwe.CongestionStateNone && s.state == streamAllocatorStateStable {
|
||||
update := NewStreamStateUpdate()
|
||||
for _, track := range s.getTracks() {
|
||||
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, s.isHolding)
|
||||
allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, false)
|
||||
updateStreamStateChange(track, allocation, update)
|
||||
}
|
||||
s.maybeSendUpdate(update)
|
||||
@@ -815,41 +849,37 @@ func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
|
||||
}
|
||||
|
||||
if cscd.congestionState == bwe.CongestionStateCongested {
|
||||
s.params.Logger.Infow(
|
||||
"stream allocator: channel congestion detected, updating channel capacity",
|
||||
"old(bps)", s.committedChannelCapacity,
|
||||
"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
|
||||
if s.probeController.GetActiveProbeClusterId() == ccutils.ProbeClusterIdInvalid {
|
||||
s.params.Logger.Infow(
|
||||
"stream allocator: channel congestion detected, not updating channel capacity in active probe",
|
||||
"old(bps)", s.committedChannelCapacity,
|
||||
"new(bps)", cscd.estimatedAvailableChannelCapacity,
|
||||
"expectedUsage(bps)", s.getExpectedBandwidthUsage(),
|
||||
)
|
||||
} else {
|
||||
s.params.Logger.Infow(
|
||||
"stream allocator: channel congestion detected, updating channel capacity",
|
||||
"old(bps)", s.committedChannelCapacity,
|
||||
"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
|
||||
|
||||
// reset probe to ensure it does not start too soon after a downward trend
|
||||
// BWE-TODO: maybe probe controller setting should be algorithm specific
|
||||
// BWE-TODO: for e. g., the reset could be waiting shorter in SSBWE case
|
||||
// BWE-TODO: a couple of things to consider
|
||||
// BWE-TODO: 1. Make ProbeController be owned by BWE modules?
|
||||
// BWE-TODO: 2. Add an interface method to BWE to check if probe controller should be reset?
|
||||
s.probeController.Reset()
|
||||
|
||||
s.allocateAllTracks()
|
||||
s.allocateAllTracks()
|
||||
}
|
||||
}
|
||||
|
||||
s.congestionState = cscd.congestionState
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalPacerProbeObserverClusterComplete(event Event) {
|
||||
probeClusterId, _ := event.Data.(ccutils.ProbeClusterId)
|
||||
s.probeController.ProbeClusterDone(probeClusterId)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) setState(state streamAllocatorState) {
|
||||
if s.state == state {
|
||||
return
|
||||
@@ -858,16 +888,14 @@ func (s *StreamAllocator) setState(state streamAllocatorState) {
|
||||
s.params.Logger.Infow("stream allocator: state change", "from", s.state, "to", state)
|
||||
s.state = state
|
||||
|
||||
// reset probe to enforce a delay after state change before probing
|
||||
s.probeController.Reset()
|
||||
// restart everything when when state is stable
|
||||
if state == streamAllocatorStateStable {
|
||||
s.maybeStopProbe()
|
||||
|
||||
// a fresh start after state transition to get clean data
|
||||
// BWE-TODO: ssbwe maybe should not reset like this as it might have useful state across
|
||||
// BWE-TODO: state changes in this module, actually even remotebwe should also manage it
|
||||
// BWE-TODO: internally, Reset should probably only be used if all managed tracks go away
|
||||
// BWE-TODO: and we can get a clean start, mimicking existing behaviour till this can be
|
||||
// BWE-TODO: evaluated more.
|
||||
s.params.BWE.Reset()
|
||||
s.probeController.Reset()
|
||||
|
||||
s.params.BWE.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) adjustState() {
|
||||
@@ -882,8 +910,8 @@ func (s *StreamAllocator) adjustState() {
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) allocateTrack(track *Track) {
|
||||
// abort any probe that may be running when a track specific change needs allocation
|
||||
s.probeController.AbortProbe()
|
||||
// end/abort any probe that may be running when a track specific change needs allocation
|
||||
s.maybeStopProbe()
|
||||
|
||||
// if not deficient, free pass allocate track
|
||||
if !s.enabled || s.state == streamAllocatorStateStable || !track.IsManaged() {
|
||||
@@ -1036,18 +1064,13 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
|
||||
s.adjustState()
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) onProbeDone(isNotFailing bool, isGoalReached bool, highestEstimate int64) {
|
||||
s.params.BWE.ProbingEnd(isNotFailing, isGoalReached)
|
||||
|
||||
if !isNotFailing {
|
||||
return
|
||||
func (s *StreamAllocator) maybeStopProbe() {
|
||||
activeProbeClusterId := s.probeController.GetActiveProbeClusterId()
|
||||
if activeProbeClusterId != ccutils.ProbeClusterIdInvalid {
|
||||
pci := s.params.Pacer.EndProbeCluster(activeProbeClusterId)
|
||||
s.prober.Reset(pci)
|
||||
s.probeController.ProbeClusterDone(pci)
|
||||
}
|
||||
|
||||
if highestEstimate > s.committedChannelCapacity {
|
||||
s.committedChannelCapacity = highestEstimate
|
||||
}
|
||||
|
||||
s.maybeBoostDeficientTracks()
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) maybeBoostDeficientTracks() {
|
||||
@@ -1268,19 +1291,6 @@ func (s *StreamAllocator) getNackDelta() (uint32, uint32) {
|
||||
return aggPacketDelta, aggRepeatedNackDelta
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) initProbe(probeGoalDeltaBps int64) {
|
||||
expectedBandwidthUsage := s.getExpectedBandwidthUsage()
|
||||
probeClusterId, probeGoalBps := s.probeController.InitProbe(probeGoalDeltaBps, expectedBandwidthUsage)
|
||||
s.params.Logger.Debugw(
|
||||
"stream allocator: starting probe",
|
||||
"probeClusterId", probeClusterId,
|
||||
"current usage", expectedBandwidthUsage,
|
||||
"committed", s.committedChannelCapacity,
|
||||
"probeGoalDeltaBps", probeGoalDeltaBps,
|
||||
"goalBps", probeGoalBps,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) maybeProbe() {
|
||||
if s.overriddenChannelCapacity > 0 {
|
||||
// do not probe if channel capacity is overridden
|
||||
@@ -1325,7 +1335,14 @@ func (s *StreamAllocator) maybeProbeWithPadding() {
|
||||
continue
|
||||
}
|
||||
|
||||
s.initProbe(transition.BandwidthDelta)
|
||||
pcg, ok := s.probeController.MaybeInitiateProbe(s.committedChannelCapacity, transition.BandwidthDelta, s.getExpectedBandwidthUsage())
|
||||
if ok {
|
||||
pci := s.prober.AddCluster(ccutils.ProbeClusterModeUniform, pcg)
|
||||
s.params.Logger.Debugw(
|
||||
"stream allocator: starting probe",
|
||||
"probeClusterInfo", pci,
|
||||
)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user