Move probe observer to pacer (#3214)

* Probe ID pass

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* clean up

* typo

* populate desired bytes

* correct num probes calculation

* debug log

* remove unused constant

* log channel observer

* debug

* clear isInProbe flag on end

* clear probe flag on reset

* re-arrange
This commit is contained in:
Raja Subramanian
2024-11-29 09:19:48 +05:30
committed by GitHub
parent 3372e6e285
commit 427ed23478
10 changed files with 442 additions and 405 deletions
+17 -28
View File
@@ -20,6 +20,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
"github.com/livekit/livekit-server/pkg/sfu/pacer"
"github.com/livekit/protocol/logger"
)
@@ -39,7 +40,6 @@ type ProbeControllerConfig struct {
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"`
}
@@ -58,7 +58,6 @@ var (
MinBps: 200_000,
MinDuration: 200 * time.Millisecond,
MaxDuration: 20 * time.Second,
DurationOverflowFactor: 1.25,
DurationIncreaseFactor: 1.5,
}
)
@@ -68,6 +67,8 @@ var (
type ProbeControllerParams struct {
Config ProbeControllerConfig
Prober *ccutils.Prober
BWE bwe.BWE
Pacer pacer.Pacer
Logger logger.Logger
}
@@ -75,7 +76,6 @@ type ProbeController struct {
params ProbeControllerParams
lock sync.RWMutex
bwe bwe.BWE
probeInterval time.Duration
lastProbeStartTime time.Time
probeGoalBps int64
@@ -98,13 +98,6 @@ func NewProbeController(params ProbeControllerParams) *ProbeController {
return p
}
func (p *ProbeController) SetBWE(bwe bwe.BWE) {
p.lock.Lock()
defer p.lock.Unlock()
p.bwe = bwe
}
func (p *ProbeController) Reset() {
p.lock.Lock()
defer p.lock.Unlock()
@@ -118,14 +111,15 @@ func (p *ProbeController) Reset() {
p.clearProbeLocked()
}
func (p *ProbeController) ProbeClusterDone(info ccutils.ProbeClusterInfo) {
func (p *ProbeController) ProbeClusterDone(probeClusterId ccutils.ProbeClusterId) {
p.lock.Lock()
defer p.lock.Unlock()
if p.probeClusterId != info.Id {
p.params.Logger.Debugw("not expected probe cluster", "probeClusterId", p.probeClusterId, "resetProbeClusterId", info.Id)
if p.probeClusterId != probeClusterId {
p.params.Logger.Debugw("not expected probe cluster", "probeClusterId", p.probeClusterId, "resetProbeClusterId", probeClusterId)
} else {
p.doneProbeClusterInfo = info
p.doneProbeClusterInfo = p.params.Pacer.EndProbeCluster(probeClusterId)
p.params.Prober.ClusterDone(p.doneProbeClusterInfo)
}
}
@@ -149,14 +143,14 @@ func (p *ProbeController) MaybeFinalizeProbe(
if (isComplete || p.abortedProbeClusterId != ccutils.ProbeClusterIdInvalid) &&
p.probeEndTime.IsZero() &&
p.doneProbeClusterInfo.Id != ccutils.ProbeClusterIdInvalid && p.doneProbeClusterInfo.Id == p.probeClusterId {
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.BytesSent*8*1000) / float64(lowestEstimate)
expectedDuration = float64(p.doneProbeClusterInfo.Bytes()*8*1000) / float64(lowestEstimate)
}
queueTime := expectedDuration - float64(p.doneProbeClusterInfo.Duration.Milliseconds())
queueTime := expectedDuration - float64(p.doneProbeClusterInfo.Duration().Milliseconds())
if queueTime < 0.0 {
queueTime = 0.0
}
@@ -164,7 +158,7 @@ func (p *ProbeController) MaybeFinalizeProbe(
if queueWait > p.params.Config.SettleWaitMax {
queueWait = p.params.Config.SettleWaitMax
}
p.probeEndTime = p.lastProbeStartTime.Add(queueWait + p.doneProbeClusterInfo.Duration)
p.probeEndTime = p.lastProbeStartTime.Add(queueWait + p.doneProbeClusterInfo.Duration())
p.params.Logger.Debugw(
"setting probe end time",
"probeClusterId", p.probeClusterId,
@@ -223,7 +217,7 @@ func (p *ProbeController) InitProbe(probeGoalDeltaBps int64, expectedBandwidthUs
}
p.probeGoalBps = expectedBandwidthUsage + desiredIncreaseBps
p.doneProbeClusterInfo = ccutils.ProbeClusterInfo{Id: ccutils.ProbeClusterIdInvalid}
p.doneProbeClusterInfo = ccutils.ProbeClusterInfoInvalid
p.abortedProbeClusterId = ccutils.ProbeClusterIdInvalid
p.goalReachedProbeClusterId = ccutils.ProbeClusterIdInvalid
@@ -236,7 +230,6 @@ func (p *ProbeController) InitProbe(probeGoalDeltaBps int64, expectedBandwidthUs
int(p.probeGoalBps),
int(expectedBandwidthUsage),
p.probeDuration,
time.Duration(float64(p.probeDuration.Milliseconds())*p.params.Config.DurationOverflowFactor)*time.Millisecond,
)
p.pollProbe(p.probeClusterId, expectedBandwidthUsage)
@@ -245,11 +238,7 @@ func (p *ProbeController) InitProbe(probeGoalDeltaBps int64, expectedBandwidthUs
}
func (p *ProbeController) pollProbe(probeClusterId ccutils.ProbeClusterId, expectedBandwidthUsage int64) {
if p.bwe == nil {
return
}
p.bwe.ProbingStart(expectedBandwidthUsage)
p.params.BWE.ProbingStart(expectedBandwidthUsage)
go func() {
for {
@@ -261,7 +250,7 @@ func (p *ProbeController) pollProbe(probeClusterId ccutils.ProbeClusterId, expec
done := false
_, trend, _, highestEstimate := p.bwe.GetProbeStatus()
_, trend, _, highestEstimate := p.params.BWE.GetProbeStatus()
if !p.probeTrendObserved && trend != bwe.ChannelTrendNeutral {
p.probeTrendObserved = true
}
@@ -315,7 +304,7 @@ func (p *ProbeController) pollProbe(probeClusterId ccutils.ProbeClusterId, expec
func (p *ProbeController) clearProbeLocked() {
p.probeClusterId = ccutils.ProbeClusterIdInvalid
p.doneProbeClusterInfo = ccutils.ProbeClusterInfo{Id: ccutils.ProbeClusterIdInvalid}
p.doneProbeClusterInfo = ccutils.ProbeClusterInfoInvalid
p.abortedProbeClusterId = ccutils.ProbeClusterIdInvalid
p.goalReachedProbeClusterId = ccutils.ProbeClusterIdInvalid
}
@@ -343,7 +332,7 @@ func (p *ProbeController) increaseProbeDurationLocked() {
}
func (p *ProbeController) StopProbe() {
p.params.Prober.Reset()
p.params.Prober.Reset(p.params.Pacer.EndProbeCluster(p.probeClusterId))
}
func (p *ProbeController) AbortProbe() {
+57 -88
View File
@@ -32,6 +32,7 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/sfu/bwe"
"github.com/livekit/livekit-server/pkg/sfu/ccutils"
"github.com/livekit/livekit-server/pkg/sfu/pacer"
"github.com/livekit/livekit-server/pkg/utils"
)
@@ -82,13 +83,13 @@ const (
streamAllocatorSignalEstimate
streamAllocatorSignalPeriodicPing
streamAllocatorSignalSendProbe
streamAllocatorSignalProbeClusterDone
streamAllocatorSignalResume
streamAllocatorSignalSetAllowPause
streamAllocatorSignalSetChannelCapacity
// STREAM-ALLOCATOR-DATA streamAllocatorSignalNACK
// STREAM-ALLOCATOR-DATA streamAllocatorSignalRTCPReceiverReport
streamAllocatorSignalCongestionStateChange
streamAllocatorSignalPacerProbeObserverClusterComplete
)
func (s streamAllocatorSignal) String() string {
@@ -105,8 +106,6 @@ func (s streamAllocatorSignal) String() string {
return "PERIODIC_PING"
case streamAllocatorSignalSendProbe:
return "SEND_PROBE"
case streamAllocatorSignalProbeClusterDone:
return "PROBE_CLUSTER_DONE"
case streamAllocatorSignalResume:
return "RESUME"
case streamAllocatorSignalSetAllowPause:
@@ -121,6 +120,8 @@ 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,6 +169,8 @@ var (
type StreamAllocatorParams struct {
Config StreamAllocatorConfig
BWE bwe.BWE
Pacer pacer.Pacer
Logger logger.Logger
}
@@ -176,7 +179,6 @@ type StreamAllocator struct {
onStreamStateChange func(update *StreamStateUpdate) error
bwe bwe.BWE
sendSideBWEInterceptor cc.BandwidthEstimator
enabled bool
@@ -227,9 +229,14 @@ func NewStreamAllocator(params StreamAllocatorParams, enabled bool, allowPause b
s.probeController = NewProbeController(ProbeControllerParams{
Config: s.params.Config.ProbeController,
Prober: s.prober,
BWE: s.params.BWE,
Pacer: s.params.Pacer,
Logger: params.Logger,
})
s.params.BWE.SetBWEListener(s)
s.params.Pacer.SetPacerProbeObserverListener(s)
s.resetState()
return s
@@ -254,14 +261,6 @@ func (s *StreamAllocator) OnStreamStateChange(f func(update *StreamStateUpdate)
s.onStreamStateChange = f
}
func (s *StreamAllocator) SetBWE(bwe bwe.BWE) {
if bwe != nil {
bwe.SetBWEListener(s)
}
s.bwe = bwe
s.probeController.SetBWE(bwe)
}
func (s *StreamAllocator) SetSendSideBWEInterceptor(sendSideBWEInterceptor cc.BandwidthEstimator) {
if sendSideBWEInterceptor != nil {
sendSideBWEInterceptor.OnTargetBitrateChange(s.onTargetBitrateChange)
@@ -295,10 +294,7 @@ func (s *StreamAllocator) AddTrack(downTrack *sfu.DownTrack, params AddTrackPara
}
downTrack.SetStreamAllocatorListener(s)
if s.prober.IsRunning() {
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate
downTrack.SetStreamAllocatorReportInterval(50 * time.Millisecond)
}
downTrack.SetProbeClusterId(s.prober.GetActiveClusterId())
s.maybePostEventAllocateTrack(downTrack)
}
@@ -346,9 +342,7 @@ func (s *StreamAllocator) SetChannelCapacity(channelCapacity int64) {
}
func (s *StreamAllocator) resetState() {
if s.bwe != nil {
s.bwe.Reset()
}
s.params.BWE.Reset()
s.probeController.Reset()
s.state = streamAllocatorStateStable
@@ -440,9 +434,7 @@ func (s *StreamAllocator) OnTransportCCFeedback(downTrack *sfu.DownTrack, fb *rt
s.sendSideBWEInterceptor.WriteRTCP([]rtcp.Packet{fb}, nil)
}
if s.bwe != nil {
s.bwe.HandleTWCCFeedback(fb)
}
s.params.BWE.HandleTWCCFeedback(fb)
}
// called when target bitrate changes (send side bandwidth estimation)
@@ -463,10 +455,7 @@ type congestionStateChangeData struct {
func (s *StreamAllocator) OnCongestionStateChange(congestionState bwe.CongestionState, estimatedAvailableChannelCapacity int64) {
s.postEvent(Event{
Signal: streamAllocatorSignalCongestionStateChange,
Data: congestionStateChangeData{
congestionState: congestionState,
estimatedAvailableChannelCapacity: estimatedAvailableChannelCapacity,
},
Data: congestionStateChangeData{congestionState, estimatedAvailableChannelCapacity},
})
}
@@ -522,11 +511,6 @@ func (s *StreamAllocator) OnResume(downTrack *sfu.DownTrack) {
})
}
// called by a video DownTrack to report packet send
func (s *StreamAllocator) OnPacketsSent(downTrack *sfu.DownTrack, size int) {
s.prober.PacketsSent(size)
}
/* STREAM-ALLOCATOR-DATA
// called by a video DownTrack when it processes NACKs
func (s *StreamAllocator) OnNACK(downTrack *sfu.DownTrack, nackInfos []sfu.NackInfo) {
@@ -556,24 +540,21 @@ func (s *StreamAllocator) OnSendProbe(bytesToSend int) {
})
}
// called when prober finishes a probe cluster, could be called when prober is reset which stops an active cluster
func (s *StreamAllocator) OnProbeClusterDone(info ccutils.ProbeClusterInfo) {
s.postEvent(Event{
Signal: streamAllocatorSignalProbeClusterDone,
Data: info,
})
// 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 prober active state changes
func (s *StreamAllocator) OnActiveChanged(isActive bool) {
for _, t := range s.getTracks() {
if isActive {
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate
t.DownTrack().SetStreamAllocatorReportInterval(50 * time.Millisecond)
} else {
t.DownTrack().ClearStreamAllocatorReportInterval()
}
}
// called when pacer probe observer observes a cluster completion
func (s *StreamAllocator) OnPacerProbeObserverClusterComplete(probeClusterId ccutils.ProbeClusterId) {
s.postEvent(Event{
Signal: streamAllocatorSignalPacerProbeObserverClusterComplete,
Data: probeClusterId,
})
}
// called to check if track should participate in BWE
@@ -652,8 +633,6 @@ func (s *StreamAllocator) postEvent(event Event) {
event.handleSignalPeriodicPing(event)
case streamAllocatorSignalSendProbe:
event.handleSignalSendProbe(event)
case streamAllocatorSignalProbeClusterDone:
event.handleSignalProbeClusterDone(event)
case streamAllocatorSignalResume:
event.handleSignalResume(event)
case streamAllocatorSignalSetAllowPause:
@@ -668,6 +647,8 @@ func (s *StreamAllocator) postEvent(event Event) {
*/
case streamAllocatorSignalCongestionStateChange:
s.handleSignalCongestionStateChange(event)
case streamAllocatorSignalPacerProbeObserverClusterComplete:
event.handleSignalPacerProbeObserverClusterComplete(event)
}
}, event)
}
@@ -705,29 +686,25 @@ func (s *StreamAllocator) handleSignalEstimate(event Event) {
// always update NACKs
packetDelta, repeatedNackDelta := s.getNackDelta()
if s.bwe != nil {
s.bwe.HandleREMB(
receivedEstimate,
s.probeController.DoesProbeNeedFinalize(), // waiting for goal reached OR aborted probe to finalize
s.getExpectedBandwidthUsage(),
packetDelta,
repeatedNackDelta,
)
}
s.params.BWE.HandleREMB(
receivedEstimate,
s.probeController.DoesProbeNeedFinalize(), // waiting for goal reached OR aborted probe to finalize
s.getExpectedBandwidthUsage(),
packetDelta,
repeatedNackDelta,
)
}
func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
// finalize probe if necessary
if s.bwe != nil {
isValidSignal, trend, lowestEstimate, highestEstimate := s.bwe.GetProbeStatus()
isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe(
isValidSignal,
trend,
lowestEstimate,
)
if isHandled {
s.onProbeDone(isNotFailing, isGoalReached, highestEstimate)
}
isValidSignal, trend, lowestEstimate, highestEstimate := s.params.BWE.GetProbeStatus()
isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe(
isValidSignal,
trend,
lowestEstimate,
)
if isHandled {
s.onProbeDone(isNotFailing, isGoalReached, highestEstimate)
}
// probe if necessary and timing is right
@@ -756,15 +733,6 @@ func (s *StreamAllocator) handleSignalSendProbe(event Event) {
break
}
}
if bytesSent != 0 {
s.prober.ProbeSent(bytesSent)
}
}
func (s *StreamAllocator) handleSignalProbeClusterDone(event Event) {
info, _ := event.Data.(ccutils.ProbeClusterInfo)
s.probeController.ProbeClusterDone(info)
}
func (s *StreamAllocator) handleSignalResume(event Event) {
@@ -877,6 +845,11 @@ func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) {
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
@@ -889,14 +862,12 @@ func (s *StreamAllocator) setState(state streamAllocatorState) {
s.probeController.Reset()
// a fresh start after state transition to get clean data
if s.bwe != nil {
// 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.bwe.Reset()
}
// 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()
}
func (s *StreamAllocator) adjustState() {
@@ -1066,9 +1037,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
}
func (s *StreamAllocator) onProbeDone(isNotFailing bool, isGoalReached bool, highestEstimate int64) {
if s.bwe != nil {
s.bwe.ProbingEnd(isNotFailing, isGoalReached)
}
s.params.BWE.ProbingEnd(isNotFailing, isGoalReached)
if !isNotFailing {
return