mirror of
https://github.com/livekit/livekit.git
synced 2026-04-03 19:15:47 +00:00
* Add sort by option to node selector Allow the final decision of a selected node to be based on different sorting options rather than just random. Add a `sysload`, `cpuload`, `rooms`, `clients`, `tracks`, and `bytespersec` sorting options to select the appropriate room when multiple are suitable for the chosen selector. Add tests for sysload sort_by option Closes: #598 * NodeSelector: rename "random" selector to "any" Since the selector introduces limits but the final selection is now based on the sort by value, the random selector name does not make sense. Rename this selector to "any" to accurately reflect that it will select any available no with no additional selection limits. Allow the old setting "random" to point to the new selector name, but print a deprecation warning message.
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package selector_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
|
|
|
"github.com/livekit/livekit-server/pkg/routing/selector"
|
|
)
|
|
|
|
func SortByTest(t *testing.T, sortBy string) {
|
|
sel := selector.SystemLoadSelector{SortBy: sortBy}
|
|
nodes := []*livekit.Node{nodeLoadLow, nodeLoadMedium, nodeLoadHigh}
|
|
|
|
for i := 0; i < 5; i++ {
|
|
node, err := sel.SelectNode(nodes)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if node != nodeLoadLow {
|
|
t.Error("selected the wrong node for SortBy:", sortBy)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSortByErrors(t *testing.T) {
|
|
sel := selector.SystemLoadSelector{}
|
|
nodes := []*livekit.Node{nodeLoadLow, nodeLoadMedium, nodeLoadHigh}
|
|
|
|
// Test unset sort by option error
|
|
_, err := sel.SelectNode(nodes)
|
|
if err != selector.ErrSortByNotSet {
|
|
t.Error("shouldn't allow empty sortBy")
|
|
}
|
|
|
|
// Test unknown sort by option error
|
|
sel.SortBy = "testFail"
|
|
_, err = sel.SelectNode(nodes)
|
|
if err != selector.ErrSortByUnknown {
|
|
t.Error("shouldn't allow unknown sortBy")
|
|
}
|
|
}
|
|
|
|
func TestSortBy(t *testing.T) {
|
|
sortByTests := []string{"sysload", "cpuload", "rooms", "clients", "tracks", "bytespersec"}
|
|
|
|
for _, sortBy := range sortByTests {
|
|
SortByTest(t, sortBy)
|
|
}
|
|
}
|