Region Aware node selection fixes and enhancements (#141)

* cli: Allow setting the current node region with flag or env variable

Also add region to "starting LiveKit server" log.

* routing: Add region to node registration

Register the node's region on the selected router so it can be used for
region aware node selection.

Also add the region to the list-nodes output.

* regionaware: Set minDist to zero for the current node

If you don't set the minDist when leaving the loop early for a node that
matches the current region, the minDist value with still be at max. This
causes the the wrong node to be selected if the current node is the
first one the loop passes through.

Add a test that validates this change. The new test fails if this new
change is not in place.
This commit is contained in:
Brint E. Kriebel
2021-10-10 22:21:37 -07:00
committed by GitHub
parent 8ff18e0326
commit 822f8c3944
7 changed files with 32 additions and 2 deletions
+2 -2
View File
@@ -152,7 +152,7 @@ func listNodes(c *cli.Context) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"ID", "IP Address",
"ID", "IP Address", "Region",
"CPUs", "Load",
"Clients", "Rooms", "Tracks In/Out",
"Bytes In/Out", "Packets In/Out", "Nack", "Bps In/Out", "Pps In/Out", "Nack/Sec",
@@ -180,7 +180,7 @@ func listNodes(c *cli.Context) error {
updatedAt := time.Unix(node.Stats.UpdatedAt, 0).String()
table.Append([]string{
node.Id, node.Ip,
node.Id, node.Ip, node.Region,
cpus, loadAvg,
clients, rooms, tracks,
bytes, packets, nack, bps, packetsPerSec, nackPerSec,
+5
View File
@@ -49,6 +49,11 @@ func main() {
Usage: "api keys (key: secret\\n)",
EnvVars: []string{"LIVEKIT_KEYS"},
},
&cli.StringFlag{
Name: "region",
Usage: "region of the current node. Used by regionaware node selector",
EnvVars: []string{"LIVEKIT_REGION"},
},
&cli.StringFlag{
Name: "node-ip",
Usage: "IP address of the current node, used to advertise to clients. Automatically determined by default",
+3
View File
@@ -216,6 +216,9 @@ func (conf *Config) updateFromCLI(c *cli.Context) error {
return errors.New("Could not parse keys, it needs to be exactly, \"key: secret\", including the space")
}
}
if c.IsSet("region") {
conf.Region = c.String("region")
}
if c.IsSet("redis-host") {
conf.Redis.Address = c.String("redis-host")
}
+1
View File
@@ -28,6 +28,7 @@ func NewLocalNode(conf *config.Config) (LocalNode, error) {
Id: fmt.Sprintf("%s%s", utils.NodePrefix, HashedID(hostname)[:8]),
Ip: conf.RTC.NodeIP,
NumCpus: uint32(runtime.NumCPU()),
Region: conf.Region,
State: livekit.NodeState_SERVING,
Stats: &livekit.NodeStats{
StartedAt: time.Now().Unix(),
+1
View File
@@ -69,6 +69,7 @@ func (s *RegionAwareSelector) SelectNode(nodes []*livekit.Node, room *livekit.Ro
for _, node := range nodes {
if node.Region == nearestRegion {
nearestNodes = append(nearestNodes, node)
minDist = 0
continue
}
if dist, ok := s.regionDistances[node.Region]; ok {
+17
View File
@@ -64,6 +64,23 @@ func TestRegionAwareRouting(t *testing.T) {
require.Equal(t, expectedNode, node)
})
t.Run("picks available nodes in same region when current node is first in the list", func(t *testing.T) {
expectedNode := newTestNodeInRegion(regionEast, true)
nodes := []*livekit.Node{
expectedNode,
newTestNodeInRegion(regionSeattle, true),
newTestNodeInRegion(regionWest, true),
newTestNodeInRegion(regionEast, false),
}
s, err := selector.NewRegionAwareSelector(regionEast, rc)
require.NoError(t, err)
s.SysloadLimit = loadLimit
node, err := s.SelectNode(nodes, nil)
require.NoError(t, err)
require.Equal(t, expectedNode, node)
})
t.Run("picks closest node in a diff region", func(t *testing.T) {
expectedNode := newTestNodeInRegion(regionWest, true)
nodes := []*livekit.Node{
+3
View File
@@ -157,6 +157,9 @@ func (s *LivekitServer) Start() error {
"nodeIP", s.currentNode.Ip,
"version", version.Version,
}
if s.config.Region != "" {
values = append(values, "region", s.config.Region)
}
if s.config.RTC.TCPPort != 0 {
values = append(values, "rtc.portTCP", s.config.RTC.TCPPort)
}