Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selection: Clear known sessions if none of them has good enough latency score #3086

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
leszko marked this conversation as resolved.
Show resolved Hide resolved
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)
leszko marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading