Skip to content

Commit

Permalink
selection: Clear known sessions if none of them has good enough laten…
Browse files Browse the repository at this point in the history
…cy score (#3086)

Problem: We never cleaned the known sessions, even if all of them had too low latency score to use; example of what sometimes happens:
1) Broadcaster accumulated 8 known sessions, but all had too low latency score
2) Session refresh was not triggered because we had a lot of knows session in the pool
3) At the same time, we didn't have sessions in unknown session pool
4) Selection checked that it cannot use the known session and tried to select from unknown sessions (but the pool was empty or there were no sessions fulfilling the condition of max price or perf score)

Co-authored-by: Thom Shutt <thomshutt@users.noreply.github.com>
  • Loading branch information
leszko and thomshutt authored Sep 10, 2024
1 parent 45652c4 commit 834a9c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
11 changes: 7 additions & 4 deletions server/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ func (s *MinLSSelector) Select(ctx context.Context) *BroadcastSession {
return s.selectUnknownSession(ctx)
}

minSess := sess.(*BroadcastSession)
if minSess.LatencyScore > s.minLS && len(s.unknownSessions) > 0 {
return s.selectUnknownSession(ctx)
lowestLatencyScoreKnownSession := heap.Pop(s.knownSessions).(*BroadcastSession)
if lowestLatencyScoreKnownSession.LatencyScore <= s.minLS {
// known session has good enough latency score, use it
return lowestLatencyScoreKnownSession
}

return heap.Pop(s.knownSessions).(*BroadcastSession)
// known session does not have good enough latency score, clear the heap and use unknown session
s.knownSessions = &sessHeap{}
return s.selectUnknownSession(ctx)
}

// Size returns the number of sessions stored by the selector
Expand Down
47 changes: 19 additions & 28 deletions server/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,48 +183,39 @@ func TestMinLSSelector(t *testing.T) {
assert.Equal(sel.Size(), 2)
assert.Equal(len(sel.unknownSessions), 2)

// Set sess1.LatencyScore to not be good enough
sess1.LatencyScore = 1.1
// Set sess1.LatencyScore to good enough
sess1.LatencyScore = 0.9
sel.Complete(sess1)
assert.Equal(sel.Size(), 3)
assert.Equal(len(sel.unknownSessions), 2)
assert.Equal(sel.knownSessions.Len(), 1)

// Select from unknownSessions
sess2 := sel.Select(context.TODO())
// Select sess1 because it's a known session with good enough latency score
sess := sel.Select(context.TODO())
assert.Equal(sel.Size(), 2)
assert.Equal(len(sel.unknownSessions), 1)
assert.Equal(sel.knownSessions.Len(), 1)
assert.Equal(len(sel.unknownSessions), 2)
assert.Equal(sel.knownSessions.Len(), 0)

// Set sess2.LatencyScore to be good enough
sess2.LatencyScore = .9
sel.Complete(sess2)
// Set sess.LatencyScore to not be good enough
sess.LatencyScore = 1.1
sel.Complete(sess)
assert.Equal(sel.Size(), 3)
assert.Equal(len(sel.unknownSessions), 1)
assert.Equal(sel.knownSessions.Len(), 2)
assert.Equal(len(sel.unknownSessions), 2)
assert.Equal(sel.knownSessions.Len(), 1)

// Select from knownSessions
knownSess := sel.Select(context.TODO())
// Select from unknownSessions, because sess2 does not have a good enough latency score
sess = sel.Select(context.TODO())
sess.LatencyScore = 1.1
sel.Complete(sess)
assert.Equal(sel.Size(), 2)
assert.Equal(len(sel.unknownSessions), 1)
assert.Equal(sel.knownSessions.Len(), 1)
assert.Equal(knownSess, sess2)

// Set knownSess.LatencyScore to not be good enough
knownSess.LatencyScore = 1.1
sel.Complete(knownSess)
// Clear unknownSessions
sess := sel.Select(context.TODO())
sess.LatencyScore = 2.1
sel.Complete(sess)
assert.Equal(len(sel.unknownSessions), 0)
assert.Equal(sel.knownSessions.Len(), 3)

// Select from knownSessions
knownSess = sel.Select(context.TODO())
assert.Equal(sel.Size(), 2)
// Select the last unknown session
sess = sel.Select(context.TODO())
assert.Equal(sel.Size(), 0)
assert.Equal(len(sel.unknownSessions), 0)
assert.Equal(sel.knownSessions.Len(), 2)
assert.Equal(sel.knownSessions.Len(), 0)

sel.Clear()
assert.Zero(sel.Size())
Expand Down

0 comments on commit 834a9c6

Please sign in to comment.