From 822f8c3944da0fa34804798aed818c612d4a1998 Mon Sep 17 00:00:00 2001 From: "Brint E. Kriebel" Date: Sun, 10 Oct 2021 22:21:37 -0700 Subject: [PATCH] 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. --- cmd/server/commands.go | 4 ++-- cmd/server/main.go | 5 +++++ pkg/config/config.go | 3 +++ pkg/routing/node.go | 1 + pkg/routing/selector/regionaware.go | 1 + pkg/routing/selector/regionaware_test.go | 17 +++++++++++++++++ pkg/service/server.go | 3 +++ 7 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/server/commands.go b/cmd/server/commands.go index ff94d5c5..f5abf442 100644 --- a/cmd/server/commands.go +++ b/cmd/server/commands.go @@ -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, diff --git a/cmd/server/main.go b/cmd/server/main.go index b70d111e..67ecb442 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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", diff --git a/pkg/config/config.go b/pkg/config/config.go index b1f3288a..47bdd99a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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") } diff --git a/pkg/routing/node.go b/pkg/routing/node.go index b0251282..6e547982 100644 --- a/pkg/routing/node.go +++ b/pkg/routing/node.go @@ -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(), diff --git a/pkg/routing/selector/regionaware.go b/pkg/routing/selector/regionaware.go index cfdce0e4..664a2eb5 100644 --- a/pkg/routing/selector/regionaware.go +++ b/pkg/routing/selector/regionaware.go @@ -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 { diff --git a/pkg/routing/selector/regionaware_test.go b/pkg/routing/selector/regionaware_test.go index 3763640b..f5372dac 100644 --- a/pkg/routing/selector/regionaware_test.go +++ b/pkg/routing/selector/regionaware_test.go @@ -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{ diff --git a/pkg/service/server.go b/pkg/service/server.go index 50b91206..27233897 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -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) }