Dampen stream allocator (#551)
* WIP commit * WIP commit * WIP commit * format * NACK window * Remove layer when it is expected to stop * Remove debug
This commit is contained in:
@@ -264,11 +264,16 @@ func (r *RTPStats) GetTotalPackets() uint32 {
|
|||||||
return r.getNumPacketsSeen() + r.packetsDuplicate + r.packetsPadding
|
return r.getNumPacketsSeen() + r.packetsDuplicate + r.packetsPadding
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RTPStats) GetTotalPacketsSansDuplicate() uint32 {
|
func (r *RTPStats) GetTotalPacketsPrimary() uint32 {
|
||||||
r.lock.RLock()
|
r.lock.RLock()
|
||||||
defer r.lock.RUnlock()
|
defer r.lock.RUnlock()
|
||||||
|
|
||||||
return r.getNumPacketsSeen() + r.packetsPadding
|
packetsSeen := r.getNumPacketsSeen()
|
||||||
|
if r.packetsPadding > packetsSeen {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return packetsSeen - r.packetsPadding
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RTPStats) GetTotalBytes() uint64 {
|
func (r *RTPStats) GetTotalBytes() uint64 {
|
||||||
|
|||||||
@@ -1056,10 +1056,6 @@ func (d *DownTrack) retransmitPackets(nacks []uint16) {
|
|||||||
nackMisses := uint32(0)
|
nackMisses := uint32(0)
|
||||||
for _, meta := range d.sequencer.getPacketsMeta(filtered) {
|
for _, meta := range d.sequencer.getPacketsMeta(filtered) {
|
||||||
if meta.layer == int8(InvalidLayerSpatial) {
|
if meta.layer == int8(InvalidLayerSpatial) {
|
||||||
if meta.nacked > 1 {
|
|
||||||
numRepeatedNACKs++
|
|
||||||
}
|
|
||||||
|
|
||||||
// padding packet, no RTX for those
|
// padding packet, no RTX for those
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -1279,7 +1275,7 @@ func (d *DownTrack) getQualityParams() *buffer.ConnectionQualityParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DownTrack) GetNackStats() (totalPackets uint32, totalRepeatedNACKs uint32) {
|
func (d *DownTrack) GetNackStats() (totalPackets uint32, totalRepeatedNACKs uint32) {
|
||||||
totalPackets = d.rtpStats.GetTotalPacketsSansDuplicate()
|
totalPackets = d.rtpStats.GetTotalPacketsPrimary()
|
||||||
|
|
||||||
d.statsLock.RLock()
|
d.statsLock.RLock()
|
||||||
totalRepeatedNACKs = d.totalRepeatedNACKs
|
totalRepeatedNACKs = d.totalRepeatedNACKs
|
||||||
|
|||||||
+190
-101
@@ -23,7 +23,13 @@ const (
|
|||||||
NumRequiredEstimatesNonProbe = 8
|
NumRequiredEstimatesNonProbe = 8
|
||||||
NumRequiredEstimatesProbe = 3
|
NumRequiredEstimatesProbe = 3
|
||||||
|
|
||||||
NackRatioThresholdNonProbe = 0.06
|
DownwardTrendThresholdNonProbe = -0.5
|
||||||
|
DownwardTrendThresholdProbe = 0.0
|
||||||
|
|
||||||
|
NackWindowDurationProbe = 0 * time.Second
|
||||||
|
NackWindowDurationNonProbe = 2 * time.Second
|
||||||
|
|
||||||
|
NackRatioThresholdNonProbe = 0.08
|
||||||
NackRatioThresholdProbe = 0.04
|
NackRatioThresholdProbe = 0.04
|
||||||
|
|
||||||
NackRatioAttenuator = 0.4 // how much to attenuate NACK ratio while calculating loss adjusted estimate
|
NackRatioAttenuator = 0.4 // how much to attenuate NACK ratio while calculating loss adjusted estimate
|
||||||
@@ -145,7 +151,6 @@ type StreamAllocator struct {
|
|||||||
abortedProbeClusterId ProbeClusterId
|
abortedProbeClusterId ProbeClusterId
|
||||||
probeTrendObserved bool
|
probeTrendObserved bool
|
||||||
probeEndTime time.Time
|
probeEndTime time.Time
|
||||||
probeChannelObserver *ChannelObserver
|
|
||||||
|
|
||||||
prober *Prober
|
prober *Prober
|
||||||
|
|
||||||
@@ -167,7 +172,6 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
|||||||
prober: NewProber(ProberParams{
|
prober: NewProber(ProberParams{
|
||||||
Logger: params.Logger,
|
Logger: params.Logger,
|
||||||
}),
|
}),
|
||||||
channelObserver: NewChannelObserver("non-probe", params.Logger, NumRequiredEstimatesNonProbe, NackRatioThresholdNonProbe),
|
|
||||||
videoTracks: make(map[livekit.TrackID]*Track),
|
videoTracks: make(map[livekit.TrackID]*Track),
|
||||||
eventCh: make(chan Event, 20),
|
eventCh: make(chan Event, 20),
|
||||||
}
|
}
|
||||||
@@ -248,7 +252,7 @@ func (s *StreamAllocator) SetTrackPriority(downTrack *DownTrack, priority uint8)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) resetState() {
|
func (s *StreamAllocator) resetState() {
|
||||||
s.channelObserver.Reset()
|
s.channelObserver = s.newChannelObserverNonProbe()
|
||||||
s.resetProbe()
|
s.resetProbe()
|
||||||
|
|
||||||
s.state = StateStable
|
s.state = StateStable
|
||||||
@@ -603,7 +607,7 @@ func (s *StreamAllocator) handleSignalProbeClusterDone(event *Event) {
|
|||||||
|
|
||||||
// ensure probe queue is flushed
|
// ensure probe queue is flushed
|
||||||
// LK-TODO: ProbeSettleWait should actually be a certain number of RTTs.
|
// LK-TODO: ProbeSettleWait should actually be a certain number of RTTs.
|
||||||
lowestEstimate := int64(math.Min(float64(s.committedChannelCapacity), float64(s.probeChannelObserver.GetLowestEstimate())))
|
lowestEstimate := int64(math.Min(float64(s.committedChannelCapacity), float64(s.channelObserver.GetLowestEstimate())))
|
||||||
expectedDuration := float64(info.BytesSent*8*1000) / float64(lowestEstimate)
|
expectedDuration := float64(info.BytesSent*8*1000) / float64(lowestEstimate)
|
||||||
queueTime := expectedDuration - float64(info.Duration.Milliseconds())
|
queueTime := expectedDuration - float64(info.Duration.Milliseconds())
|
||||||
if queueTime < 0.0 {
|
if queueTime < 0.0 {
|
||||||
@@ -648,18 +652,23 @@ func (s *StreamAllocator) handleNewEstimate(receivedEstimate int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) handleNewEstimateInProbe() {
|
func (s *StreamAllocator) handleNewEstimateInProbe() {
|
||||||
s.probeChannelObserver.AddEstimate(s.lastReceivedEstimate)
|
// always update NACKs, even if aborted
|
||||||
|
|
||||||
packetDelta, repeatedNackDelta := s.getNackDelta()
|
packetDelta, repeatedNackDelta := s.getNackDelta()
|
||||||
s.probeChannelObserver.AddNack(packetDelta, repeatedNackDelta)
|
|
||||||
|
|
||||||
trend := s.probeChannelObserver.GetTrend()
|
if s.abortedProbeClusterId != ProbeClusterIdInvalid {
|
||||||
|
// waiting for aborted probe to finalize
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.channelObserver.AddEstimate(s.lastReceivedEstimate)
|
||||||
|
s.channelObserver.AddNack(packetDelta, repeatedNackDelta)
|
||||||
|
|
||||||
|
trend, _ := s.channelObserver.GetTrend()
|
||||||
if trend != ChannelTrendNeutral {
|
if trend != ChannelTrendNeutral {
|
||||||
s.probeTrendObserved = true
|
s.probeTrendObserved = true
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case s.abortedProbeClusterId != ProbeClusterIdInvalid:
|
|
||||||
return
|
|
||||||
case !s.probeTrendObserved && time.Since(s.lastProbeStartTime) > ProbeTrendWait:
|
case !s.probeTrendObserved && time.Since(s.lastProbeStartTime) > ProbeTrendWait:
|
||||||
//
|
//
|
||||||
// More of a safety net.
|
// More of a safety net.
|
||||||
@@ -672,13 +681,13 @@ func (s *StreamAllocator) handleNewEstimateInProbe() {
|
|||||||
// stop immediately if the probe is congesting channel more
|
// stop immediately if the probe is congesting channel more
|
||||||
s.params.Logger.Debugw("probe: aborting, channel is congesting", "cluster", s.probeClusterId)
|
s.params.Logger.Debugw("probe: aborting, channel is congesting", "cluster", s.probeClusterId)
|
||||||
s.abortProbe()
|
s.abortProbe()
|
||||||
case s.probeChannelObserver.GetHighestEstimate() > s.probeGoalBps:
|
case s.channelObserver.GetHighestEstimate() > s.probeGoalBps:
|
||||||
// reached goal, stop probing
|
// reached goal, stop probing
|
||||||
s.params.Logger.Debugw(
|
s.params.Logger.Debugw(
|
||||||
"probe: stopping, goal reached",
|
"probe: stopping, goal reached",
|
||||||
"cluster", s.probeClusterId,
|
"cluster", s.probeClusterId,
|
||||||
"goal", s.probeGoalBps,
|
"goal", s.probeGoalBps,
|
||||||
"highest", s.probeChannelObserver.GetHighestEstimate(),
|
"highest", s.channelObserver.GetHighestEstimate(),
|
||||||
)
|
)
|
||||||
s.stopProbe()
|
s.stopProbe()
|
||||||
}
|
}
|
||||||
@@ -690,28 +699,38 @@ func (s *StreamAllocator) handleNewEstimateInNonProbe() {
|
|||||||
packetDelta, repeatedNackDelta := s.getNackDelta()
|
packetDelta, repeatedNackDelta := s.getNackDelta()
|
||||||
s.channelObserver.AddNack(packetDelta, repeatedNackDelta)
|
s.channelObserver.AddNack(packetDelta, repeatedNackDelta)
|
||||||
|
|
||||||
trend := s.channelObserver.GetTrend()
|
trend, reason := s.channelObserver.GetTrend()
|
||||||
if trend != ChannelTrendCongesting {
|
if trend != ChannelTrendCongesting {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nackRatio := s.channelObserver.GetNackRatio()
|
var estimateToCommit int64
|
||||||
lossAdjustedEstimate := s.lastReceivedEstimate
|
var nackRatio float64
|
||||||
|
expectedBandwidthUsage := s.getExpectedBandwidthUsage()
|
||||||
|
switch reason {
|
||||||
|
case ChannelCongestionReasonLoss:
|
||||||
|
estimateToCommit = expectedBandwidthUsage
|
||||||
|
nackRatio = s.channelObserver.GetNackRatio()
|
||||||
if nackRatio > NackRatioThresholdNonProbe {
|
if nackRatio > NackRatioThresholdNonProbe {
|
||||||
lossAdjustedEstimate = int64(float64(lossAdjustedEstimate) * (1.0 - NackRatioAttenuator*nackRatio))
|
estimateToCommit = int64(float64(estimateToCommit) * (1.0 - NackRatioAttenuator*nackRatio))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
estimateToCommit = s.lastReceivedEstimate
|
||||||
}
|
}
|
||||||
|
|
||||||
s.params.Logger.Infow(
|
s.params.Logger.Infow(
|
||||||
"channel congestion detected, updating channel capacity",
|
"channel congestion detected, updating channel capacity",
|
||||||
|
"reason", reason,
|
||||||
"old(bps)", s.committedChannelCapacity,
|
"old(bps)", s.committedChannelCapacity,
|
||||||
"new(bps)", lossAdjustedEstimate,
|
"new(bps)", estimateToCommit,
|
||||||
"lastReceived(bps)", s.lastReceivedEstimate,
|
"lastReceived(bps)", s.lastReceivedEstimate,
|
||||||
|
"expectedUsage(bps)", expectedBandwidthUsage,
|
||||||
"nackRatio", nackRatio,
|
"nackRatio", nackRatio,
|
||||||
)
|
)
|
||||||
s.committedChannelCapacity = lossAdjustedEstimate
|
s.committedChannelCapacity = estimateToCommit
|
||||||
|
|
||||||
// reset to get new set of samples for next trend
|
// reset to get new set of samples for next trend
|
||||||
s.channelObserver.Reset()
|
s.channelObserver = s.newChannelObserverNonProbe()
|
||||||
|
|
||||||
// reset probe to ensure it does not start too soon after a downward trend
|
// reset probe to ensure it does not start too soon after a downward trend
|
||||||
s.resetProbe()
|
s.resetProbe()
|
||||||
@@ -812,7 +831,7 @@ func (s *StreamAllocator) allocateTrack(track *Track) {
|
|||||||
|
|
||||||
func (s *StreamAllocator) finalizeProbe() {
|
func (s *StreamAllocator) finalizeProbe() {
|
||||||
aborted := s.probeClusterId == s.abortedProbeClusterId
|
aborted := s.probeClusterId == s.abortedProbeClusterId
|
||||||
highestEstimateInProbe := s.probeChannelObserver.GetHighestEstimate()
|
highestEstimateInProbe := s.channelObserver.GetHighestEstimate()
|
||||||
|
|
||||||
s.clearProbe()
|
s.clearProbe()
|
||||||
|
|
||||||
@@ -827,7 +846,7 @@ func (s *StreamAllocator) finalizeProbe() {
|
|||||||
// NOTE: With TWCC, it is possible to reset bandwidth estimation to clean state as
|
// NOTE: With TWCC, it is possible to reset bandwidth estimation to clean state as
|
||||||
// the send side is in full control of bandwidth estimation.
|
// the send side is in full control of bandwidth estimation.
|
||||||
//
|
//
|
||||||
s.channelObserver.Reset()
|
s.channelObserver = s.newChannelObserverNonProbe()
|
||||||
|
|
||||||
if aborted {
|
if aborted {
|
||||||
// failed probe, backoff
|
// failed probe, backoff
|
||||||
@@ -1013,6 +1032,30 @@ func (s *StreamAllocator) getNackDelta() (uint32, uint32) {
|
|||||||
return aggPacketDelta, aggRepeatedNackDelta
|
return aggPacketDelta, aggRepeatedNackDelta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StreamAllocator) newChannelObserverProbe() *ChannelObserver {
|
||||||
|
return NewChannelObserver(ChannelObserverParams{
|
||||||
|
Name: "probe",
|
||||||
|
Logger: s.params.Logger,
|
||||||
|
EstimateRequiredSamples: NumRequiredEstimatesProbe,
|
||||||
|
EstimateDownwardTrendThreshold: DownwardTrendThresholdProbe,
|
||||||
|
EstimateCollapseValues: false,
|
||||||
|
NackWindowDuration: NackWindowDurationProbe,
|
||||||
|
NackRatioThreshold: NackRatioThresholdProbe,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StreamAllocator) newChannelObserverNonProbe() *ChannelObserver {
|
||||||
|
return NewChannelObserver(ChannelObserverParams{
|
||||||
|
Name: "non-probe",
|
||||||
|
Logger: s.params.Logger,
|
||||||
|
EstimateRequiredSamples: NumRequiredEstimatesNonProbe,
|
||||||
|
EstimateDownwardTrendThreshold: DownwardTrendThresholdNonProbe,
|
||||||
|
EstimateCollapseValues: true,
|
||||||
|
NackWindowDuration: NackWindowDurationNonProbe,
|
||||||
|
NackRatioThreshold: NackRatioThresholdNonProbe,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
||||||
s.lastProbeStartTime = time.Now()
|
s.lastProbeStartTime = time.Now()
|
||||||
|
|
||||||
@@ -1025,8 +1068,8 @@ func (s *StreamAllocator) initProbe(probeRateBps int64) {
|
|||||||
|
|
||||||
s.probeEndTime = time.Time{}
|
s.probeEndTime = time.Time{}
|
||||||
|
|
||||||
s.probeChannelObserver = NewChannelObserver("probe", s.params.Logger, NumRequiredEstimatesProbe, NackRatioThresholdProbe)
|
s.channelObserver = s.newChannelObserverProbe()
|
||||||
s.probeChannelObserver.SeedEstimate(s.lastReceivedEstimate)
|
s.channelObserver.SeedEstimate(s.lastReceivedEstimate)
|
||||||
|
|
||||||
desiredRateBps := int(probeRateBps) + int(math.Max(float64(s.committedChannelCapacity), float64(expectedBandwidthUsage)))
|
desiredRateBps := int(probeRateBps) + int(math.Max(float64(s.committedChannelCapacity), float64(expectedBandwidthUsage)))
|
||||||
s.probeClusterId = s.prober.AddCluster(
|
s.probeClusterId = s.prober.AddCluster(
|
||||||
@@ -1057,12 +1100,6 @@ func (s *StreamAllocator) resetProbe() {
|
|||||||
func (s *StreamAllocator) clearProbe() {
|
func (s *StreamAllocator) clearProbe() {
|
||||||
s.probeClusterId = ProbeClusterIdInvalid
|
s.probeClusterId = ProbeClusterIdInvalid
|
||||||
s.abortedProbeClusterId = ProbeClusterIdInvalid
|
s.abortedProbeClusterId = ProbeClusterIdInvalid
|
||||||
|
|
||||||
s.probeTrendObserved = false
|
|
||||||
|
|
||||||
s.probeEndTime = time.Time{}
|
|
||||||
|
|
||||||
s.probeChannelObserver = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamAllocator) backoffProbeInterval() {
|
func (s *StreamAllocator) backoffProbeInterval() {
|
||||||
@@ -1468,38 +1505,61 @@ func (c ChannelTrend) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChannelCongestionReason int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ChannelCongestionReasonNone ChannelCongestionReason = iota
|
||||||
|
ChannelCongestionReasonEstimate
|
||||||
|
ChannelCongestionReasonLoss
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c ChannelCongestionReason) String() string {
|
||||||
|
switch c {
|
||||||
|
case ChannelCongestionReasonNone:
|
||||||
|
return "NONE"
|
||||||
|
case ChannelCongestionReasonEstimate:
|
||||||
|
return "ESTIMATE"
|
||||||
|
case ChannelCongestionReasonLoss:
|
||||||
|
return "LOSS"
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d", int(c))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChannelObserverParams struct {
|
||||||
|
Name string
|
||||||
|
Logger logger.Logger
|
||||||
|
EstimateRequiredSamples int
|
||||||
|
EstimateDownwardTrendThreshold float64
|
||||||
|
EstimateCollapseValues bool
|
||||||
|
NackWindowDuration time.Duration
|
||||||
|
NackRatioThreshold float64
|
||||||
|
}
|
||||||
|
|
||||||
type ChannelObserver struct {
|
type ChannelObserver struct {
|
||||||
name string
|
params ChannelObserverParams
|
||||||
logger logger.Logger
|
|
||||||
|
|
||||||
estimateTrend *TrendDetector
|
estimateTrend *TrendDetector
|
||||||
|
|
||||||
nackRatioThreshold float64
|
nackWindowStartTime time.Time
|
||||||
packets uint32
|
packets uint32
|
||||||
repeatedNacks uint32
|
repeatedNacks uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChannelObserver(
|
func NewChannelObserver(params ChannelObserverParams) *ChannelObserver {
|
||||||
name string,
|
|
||||||
logger logger.Logger,
|
|
||||||
estimateRequiredSamples int,
|
|
||||||
nackRatioThreshold float64,
|
|
||||||
) *ChannelObserver {
|
|
||||||
return &ChannelObserver{
|
return &ChannelObserver{
|
||||||
name: name,
|
params: params,
|
||||||
logger: logger,
|
estimateTrend: NewTrendDetector(TrendDetectorParams{
|
||||||
estimateTrend: NewTrendDetector(name+"-estimate", logger, estimateRequiredSamples),
|
Name: params.Name + "-estimate",
|
||||||
nackRatioThreshold: nackRatioThreshold,
|
Logger: params.Logger,
|
||||||
|
RequiredSamples: params.EstimateRequiredSamples,
|
||||||
|
DownwardTrendThreshold: params.EstimateDownwardTrendThreshold,
|
||||||
|
CollapseValues: params.EstimateCollapseValues,
|
||||||
|
}),
|
||||||
|
nackWindowStartTime: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChannelObserver) Reset() {
|
|
||||||
c.estimateTrend.Reset()
|
|
||||||
|
|
||||||
c.packets = 0
|
|
||||||
c.repeatedNacks = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ChannelObserver) SeedEstimate(estimate int64) {
|
func (c *ChannelObserver) SeedEstimate(estimate int64) {
|
||||||
c.estimateTrend.Seed(estimate)
|
c.estimateTrend.Seed(estimate)
|
||||||
}
|
}
|
||||||
@@ -1514,6 +1574,12 @@ func (c *ChannelObserver) AddEstimate(estimate int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChannelObserver) AddNack(packets uint32, repeatedNacks uint32) {
|
func (c *ChannelObserver) AddNack(packets uint32, repeatedNacks uint32) {
|
||||||
|
if c.params.NackWindowDuration != 0 && time.Since(c.nackWindowStartTime) > c.params.NackWindowDuration {
|
||||||
|
c.nackWindowStartTime = time.Now()
|
||||||
|
c.packets = 0
|
||||||
|
c.repeatedNacks = 0
|
||||||
|
}
|
||||||
|
|
||||||
c.packets += packets
|
c.packets += packets
|
||||||
c.repeatedNacks += repeatedNacks
|
c.repeatedNacks += repeatedNacks
|
||||||
}
|
}
|
||||||
@@ -1538,27 +1604,30 @@ func (c *ChannelObserver) GetNackRatio() float64 {
|
|||||||
return ratio
|
return ratio
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChannelObserver) GetTrend() ChannelTrend {
|
func (c *ChannelObserver) GetTrend() (ChannelTrend, ChannelCongestionReason) {
|
||||||
estimateDirection := c.estimateTrend.GetDirection()
|
estimateDirection := c.estimateTrend.GetDirection()
|
||||||
nackRatio := c.GetNackRatio()
|
nackRatio := c.GetNackRatio()
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case estimateDirection == TrendDirectionDownward:
|
case estimateDirection == TrendDirectionDownward:
|
||||||
c.logger.Debugw(
|
c.params.Logger.Debugw(
|
||||||
"channel observer: estimate is trending downward",
|
"channel observer: estimate is trending downward",
|
||||||
"lowest", c.estimateTrend.GetLowest(),
|
"name", c.params.Name,
|
||||||
"highest", c.estimateTrend.GetHighest(),
|
"estimate", c.estimateTrend.ToString(),
|
||||||
"estimates", c.estimateTrend.GetValues(),
|
|
||||||
)
|
)
|
||||||
return ChannelTrendCongesting
|
return ChannelTrendCongesting, ChannelCongestionReasonEstimate
|
||||||
case nackRatio > c.nackRatioThreshold:
|
case nackRatio > c.params.NackRatioThreshold:
|
||||||
c.logger.Debugw("channel observer: high rate of repeated NACKs", "ratio", nackRatio)
|
c.params.Logger.Debugw(
|
||||||
return ChannelTrendCongesting
|
"channel observer: high rate of repeated NACKs",
|
||||||
|
"name", c.params.Name,
|
||||||
|
"ratio", nackRatio,
|
||||||
|
)
|
||||||
|
return ChannelTrendCongesting, ChannelCongestionReasonLoss
|
||||||
case estimateDirection == TrendDirectionUpward:
|
case estimateDirection == TrendDirectionUpward:
|
||||||
return ChannelTrendClearing
|
return ChannelTrendClearing, ChannelCongestionReasonNone
|
||||||
}
|
}
|
||||||
|
|
||||||
return ChannelTrendNeutral
|
return ChannelTrendNeutral, ChannelCongestionReasonNone
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------
|
// ------------------------------------------------
|
||||||
@@ -1584,33 +1653,34 @@ func (t TrendDirection) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type TrendDetector struct {
|
type TrendDetectorParams struct {
|
||||||
name string
|
Name string
|
||||||
logger logger.Logger
|
Logger logger.Logger
|
||||||
requiredSamples int
|
RequiredSamples int
|
||||||
|
DownwardTrendThreshold float64
|
||||||
|
CollapseValues bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrendDetector struct {
|
||||||
|
params TrendDetectorParams
|
||||||
|
|
||||||
|
startTime time.Time
|
||||||
|
numSamples int
|
||||||
values []int64
|
values []int64
|
||||||
lowestvalue int64
|
lowestValue int64
|
||||||
highestvalue int64
|
highestValue int64
|
||||||
|
|
||||||
direction TrendDirection
|
direction TrendDirection
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTrendDetector(name string, logger logger.Logger, requiredSamples int) *TrendDetector {
|
func NewTrendDetector(params TrendDetectorParams) *TrendDetector {
|
||||||
return &TrendDetector{
|
return &TrendDetector{
|
||||||
name: name,
|
params: params,
|
||||||
logger: logger,
|
startTime: time.Now(),
|
||||||
requiredSamples: requiredSamples,
|
|
||||||
direction: TrendDirectionNeutral,
|
direction: TrendDirectionNeutral,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) Reset() {
|
|
||||||
t.values = nil
|
|
||||||
t.lowestvalue = int64(0)
|
|
||||||
t.highestvalue = int64(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TrendDetector) Seed(value int64) {
|
func (t *TrendDetector) Seed(value int64) {
|
||||||
if len(t.values) != 0 {
|
if len(t.values) != 0 {
|
||||||
return
|
return
|
||||||
@@ -1620,14 +1690,20 @@ func (t *TrendDetector) Seed(value int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) AddValue(value int64) {
|
func (t *TrendDetector) AddValue(value int64) {
|
||||||
if t.lowestvalue == 0 || value < t.lowestvalue {
|
t.numSamples++
|
||||||
t.lowestvalue = value
|
if t.lowestValue == 0 || value < t.lowestValue {
|
||||||
|
t.lowestValue = value
|
||||||
}
|
}
|
||||||
if value > t.highestvalue {
|
if value > t.highestValue {
|
||||||
t.highestvalue = value
|
t.highestValue = value
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(t.values) == t.requiredSamples {
|
// ignore duplicate values
|
||||||
|
if t.params.CollapseValues && len(t.values) != 0 && t.values[len(t.values)-1] == value {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(t.values) == t.params.RequiredSamples {
|
||||||
t.values = t.values[1:]
|
t.values = t.values[1:]
|
||||||
}
|
}
|
||||||
t.values = append(t.values, value)
|
t.values = append(t.values, value)
|
||||||
@@ -1636,11 +1712,11 @@ func (t *TrendDetector) AddValue(value int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) GetLowest() int64 {
|
func (t *TrendDetector) GetLowest() int64 {
|
||||||
return t.lowestvalue
|
return t.lowestValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) GetHighest() int64 {
|
func (t *TrendDetector) GetHighest() int64 {
|
||||||
return t.highestvalue
|
return t.highestValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) GetValues() []int64 {
|
func (t *TrendDetector) GetValues() []int64 {
|
||||||
@@ -1651,39 +1727,52 @@ func (t *TrendDetector) GetDirection() TrendDirection {
|
|||||||
return t.direction
|
return t.direction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TrendDetector) ToString() string {
|
||||||
|
now := time.Now()
|
||||||
|
elapsed := now.Sub(t.startTime).Seconds()
|
||||||
|
str := fmt.Sprintf("n: %s", t.params.Name)
|
||||||
|
str += fmt.Sprintf(", t: %+v|%+v|%.2fs", t.startTime.Format(time.UnixDate), now.Format(time.UnixDate), elapsed)
|
||||||
|
str += fmt.Sprintf(", v: %d|%d|%d|%+v|%.2f", t.numSamples, t.lowestValue, t.highestValue, t.values, kendallsTau(t.values))
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TrendDetector) updateDirection() {
|
func (t *TrendDetector) updateDirection() {
|
||||||
if len(t.values) < t.requiredSamples {
|
if len(t.values) < t.params.RequiredSamples {
|
||||||
t.direction = TrendDirectionNeutral
|
t.direction = TrendDirectionNeutral
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// using Kendall's Tau to find trend
|
// using Kendall's Tau to find trend
|
||||||
|
kt := kendallsTau(t.values)
|
||||||
|
|
||||||
|
t.direction = TrendDirectionNeutral
|
||||||
|
switch {
|
||||||
|
case kt > 0:
|
||||||
|
t.direction = TrendDirectionUpward
|
||||||
|
case kt < t.params.DownwardTrendThreshold:
|
||||||
|
t.direction = TrendDirectionDownward
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------
|
||||||
|
|
||||||
|
func kendallsTau(values []int64) float64 {
|
||||||
concordantPairs := 0
|
concordantPairs := 0
|
||||||
discordantPairs := 0
|
discordantPairs := 0
|
||||||
|
|
||||||
for i := 0; i < len(t.values)-1; i++ {
|
for i := 0; i < len(values)-1; i++ {
|
||||||
for j := i + 1; j < len(t.values); j++ {
|
for j := i + 1; j < len(values); j++ {
|
||||||
if t.values[i] < t.values[j] {
|
if values[i] < values[j] {
|
||||||
concordantPairs++
|
concordantPairs++
|
||||||
} else if t.values[i] > t.values[j] {
|
} else if values[i] > values[j] {
|
||||||
discordantPairs++
|
discordantPairs++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (concordantPairs + discordantPairs) == 0 {
|
if (concordantPairs + discordantPairs) == 0 {
|
||||||
t.direction = TrendDirectionNeutral
|
return 0.0
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.direction = TrendDirectionNeutral
|
return (float64(concordantPairs) - float64(discordantPairs)) / (float64(concordantPairs) + float64(discordantPairs))
|
||||||
kt := (float64(concordantPairs) - float64(discordantPairs)) / (float64(concordantPairs) + float64(discordantPairs))
|
|
||||||
switch {
|
|
||||||
case kt > 0:
|
|
||||||
t.direction = TrendDirectionUpward
|
|
||||||
case kt < 0:
|
|
||||||
t.direction = TrendDirectionDownward
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------
|
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func (s *StreamTrackerManager) AddTracker(layer int32) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !exempt {
|
if !exempt || layer > s.maxExpectedLayer {
|
||||||
s.removeAvailableLayer(layer)
|
s.removeAvailableLayer(layer)
|
||||||
} else {
|
} else {
|
||||||
s.logger.Debugw("not removing exempt layer", "layer", layer)
|
s.logger.Debugw("not removing exempt layer", "layer", layer)
|
||||||
|
|||||||
Reference in New Issue
Block a user