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:
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user