From cac6d22a72e4add7536867a1489a695a91b0c1ba Mon Sep 17 00:00:00 2001 From: Mathew Kamkar <578302+matkam@users.noreply.github.com> Date: Wed, 16 Mar 2022 14:51:22 -0700 Subject: [PATCH] store cpu load in node stats (#524) * store cpu load in node stats * num cpus uint32 * cpu load selector test * dep update --- cmd/server/commands.go | 10 +++--- go.mod | 4 +-- go.sum | 9 ++--- pkg/config/config.go | 2 ++ pkg/routing/selector/cpuload.go | 42 +++++++++++++++++++++++ pkg/routing/selector/cpuload_test.go | 37 ++++++++++++++++++++ pkg/routing/selector/interfaces.go | 4 +++ pkg/routing/selector/sysload_test.go | 5 ++- pkg/telemetry/prometheus/node.go | 18 +++++++--- pkg/telemetry/prometheus/node_linux.go | 22 ++++++------ pkg/telemetry/prometheus/node_nonlinux.go | 20 ++++++++++- 11 files changed, 146 insertions(+), 27 deletions(-) create mode 100644 pkg/routing/selector/cpuload.go create mode 100644 pkg/routing/selector/cpuload_test.go diff --git a/cmd/server/commands.go b/cmd/server/commands.go index 2750f605..a8570788 100644 --- a/cmd/server/commands.go +++ b/cmd/server/commands.go @@ -6,12 +6,13 @@ import ( "strconv" "time" - "github.com/livekit/protocol/auth" - "github.com/livekit/protocol/utils" "github.com/olekukonko/tablewriter" "github.com/urfave/cli/v2" "gopkg.in/yaml.v3" + "github.com/livekit/protocol/auth" + "github.com/livekit/protocol/utils" + "github.com/livekit/livekit-server/pkg/routing" "github.com/livekit/livekit-server/pkg/service" ) @@ -154,7 +155,7 @@ func listNodes(c *cli.Context) error { table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{ "ID", "IP Address", "Region", - "CPUs", "Load", + "CPUs", "CPU Usage", "Load", "Clients", "Rooms", "Tracks In/Out", "Bytes In/Out", "Packets In/Out", "Nack", "Bps In/Out", "Pps In/Out", "Nack/Sec", "Started At", "Updated At", @@ -164,6 +165,7 @@ func listNodes(c *cli.Context) error { // System stats cpus := strconv.Itoa(int(stats.NumCpus)) + cpuUsage := fmt.Sprintf("%.2f %%", stats.CpuLoad*100) loadAvg := fmt.Sprintf("%.2f, %.2f, %.2f", stats.LoadAvgLast1Min, stats.LoadAvgLast5Min, stats.LoadAvgLast15Min) // Room stats @@ -184,7 +186,7 @@ func listNodes(c *cli.Context) error { table.Append([]string{ node.Id, node.Ip, node.Region, - cpus, loadAvg, + cpus, cpuUsage, loadAvg, clients, rooms, tracks, bytes, packets, nack, bps, packetsPerSec, nackPerSec, startedAt, updatedAt, diff --git a/go.mod b/go.mod index b43bd76c..45692605 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.17 require ( github.com/bep/debounce v1.2.0 - github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 github.com/elliotchance/orderedmap v1.4.0 github.com/gammazero/deque v0.1.0 github.com/gammazero/workerpool v1.1.2 @@ -14,7 +13,8 @@ require ( github.com/google/wire v0.5.0 github.com/gorilla/websocket v1.4.2 github.com/hashicorp/golang-lru v0.5.4 - github.com/livekit/protocol v0.11.14-0.20220316120321-6f2a49e44914 + github.com/livekit/protocol v0.11.14-0.20220316214650-51f5188bc422 + github.com/mackerelio/go-osstat v0.2.1 github.com/magefile/mage v1.11.0 github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 396968cb..acb92a0e 100644 --- a/go.sum +++ b/go.sum @@ -15,8 +15,6 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo= github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= -github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK84APFuMvxqsk3tEIaKH/z4Rpu3g= -github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -134,8 +132,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lithammer/shortuuid/v3 v3.0.6 h1:pr15YQyvhiSX/qPxncFtqk+v4xLEpOZObbsY/mKrcvA= github.com/lithammer/shortuuid/v3 v3.0.6/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts= -github.com/livekit/protocol v0.11.14-0.20220316120321-6f2a49e44914 h1:zD7PFc1Qjrr+bzAMKwmA/sjBpaSDWh0mboKRB4XJLBA= -github.com/livekit/protocol v0.11.14-0.20220316120321-6f2a49e44914/go.mod h1:3pHsWUtQmWaH8mG0cXrQWpbf3Vo+kj0U+In77CEXu90= +github.com/livekit/protocol v0.11.14-0.20220316214650-51f5188bc422 h1:PQ+YgSJxL/UmXi46ThYSVbAl7BWyTAYEy9sCmnyokPg= +github.com/livekit/protocol v0.11.14-0.20220316214650-51f5188bc422/go.mod h1:3pHsWUtQmWaH8mG0cXrQWpbf3Vo+kj0U+In77CEXu90= +github.com/mackerelio/go-osstat v0.2.1 h1:5AeAcBEutEErAOlDz6WCkEvm6AKYgHTUQrfwm5RbeQc= +github.com/mackerelio/go-osstat v0.2.1/go.mod h1:UzRL8dMCCTqG5WdRtsxbuljMpZt9PCAGXqxPst5QtaY= github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= @@ -362,6 +362,7 @@ golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/pkg/config/config.go b/pkg/config/config.go index 52ee1473..456a1ebe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -158,6 +158,7 @@ type WebHookConfig struct { type NodeSelectorConfig struct { Kind string `yaml:"kind"` + CPULoadLimit float32 `yaml:"cpu_load_limit"` SysloadLimit float32 `yaml:"sysload_limit"` Regions []RegionConfig `yaml:"regions"` } @@ -226,6 +227,7 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) { NodeSelector: NodeSelectorConfig{ Kind: "random", SysloadLimit: 0.9, + CPULoadLimit: 0.9, }, Keys: map[string]string{}, } diff --git a/pkg/routing/selector/cpuload.go b/pkg/routing/selector/cpuload.go new file mode 100644 index 00000000..e485bbc4 --- /dev/null +++ b/pkg/routing/selector/cpuload.go @@ -0,0 +1,42 @@ +package selector + +import ( + "github.com/thoas/go-funk" + + "github.com/livekit/protocol/livekit" +) + +// CPULoadSelector eliminates nodes that have CPU usage higher than CPULoadLimit +// then selects a node randomly from nodes that are not overloaded +type CPULoadSelector struct { + CPULoadLimit float32 +} + +func (s *CPULoadSelector) filterNodes(nodes []*livekit.Node) ([]*livekit.Node, error) { + nodes = GetAvailableNodes(nodes) + if len(nodes) == 0 { + return nil, ErrNoAvailableNodes + } + + nodesLowLoad := make([]*livekit.Node, 0) + for _, node := range nodes { + stats := node.Stats + if stats.CpuLoad < s.CPULoadLimit { + nodesLowLoad = append(nodesLowLoad, node) + } + } + if len(nodesLowLoad) > 0 { + nodes = nodesLowLoad + } + return nodes, nil +} + +func (s *CPULoadSelector) SelectNode(nodes []*livekit.Node) (*livekit.Node, error) { + nodes, err := s.filterNodes(nodes) + if err != nil { + return nil, err + } + + idx := funk.RandomInt(0, len(nodes)) + return nodes[idx], nil +} diff --git a/pkg/routing/selector/cpuload_test.go b/pkg/routing/selector/cpuload_test.go new file mode 100644 index 00000000..186472dd --- /dev/null +++ b/pkg/routing/selector/cpuload_test.go @@ -0,0 +1,37 @@ +package selector_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/livekit/protocol/livekit" + + "github.com/livekit/livekit-server/pkg/routing/selector" +) + +func TestCPULoadSelector_SelectNode(t *testing.T) { + sel := selector.CPULoadSelector{CPULoadLimit: 0.8} + + var nodes []*livekit.Node + _, err := sel.SelectNode(nodes) + require.Error(t, err, "should error no available nodes") + + // Select a node with high load when no nodes with low load are available + nodes = []*livekit.Node{nodeLoadHigh} + if _, err := sel.SelectNode(nodes); err != nil { + t.Error(err) + } + + // Select a node with low load when available + nodes = []*livekit.Node{nodeLoadLow, 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") + } + } +} diff --git a/pkg/routing/selector/interfaces.go b/pkg/routing/selector/interfaces.go index 6b97d51b..b678e44d 100644 --- a/pkg/routing/selector/interfaces.go +++ b/pkg/routing/selector/interfaces.go @@ -21,6 +21,10 @@ func CreateNodeSelector(conf *config.Config) (NodeSelector, error) { kind = "random" } switch kind { + case "cpuload": + return &CPULoadSelector{ + CPULoadLimit: conf.NodeSelector.CPULoadLimit, + }, nil case "sysload": return &SystemLoadSelector{ SysloadLimit: conf.NodeSelector.SysloadLimit, diff --git a/pkg/routing/selector/sysload_test.go b/pkg/routing/selector/sysload_test.go index 9ff2e7bd..3b61ec63 100644 --- a/pkg/routing/selector/sysload_test.go +++ b/pkg/routing/selector/sysload_test.go @@ -4,9 +4,10 @@ import ( "testing" "time" - "github.com/livekit/protocol/livekit" "github.com/stretchr/testify/require" + "github.com/livekit/protocol/livekit" + "github.com/livekit/livekit-server/pkg/routing/selector" ) @@ -16,6 +17,7 @@ var ( Stats: &livekit.NodeStats{ UpdatedAt: time.Now().Unix(), NumCpus: 1, + CpuLoad: 0.1, LoadAvgLast1Min: 0.0, }, } @@ -25,6 +27,7 @@ var ( Stats: &livekit.NodeStats{ UpdatedAt: time.Now().Unix(), NumCpus: 1, + CpuLoad: 0.99, LoadAvgLast1Min: 2.0, }, } diff --git a/pkg/telemetry/prometheus/node.go b/pkg/telemetry/prometheus/node.go index 07cb6b0d..4aa6676e 100644 --- a/pkg/telemetry/prometheus/node.go +++ b/pkg/telemetry/prometheus/node.go @@ -3,9 +3,11 @@ package prometheus import ( "time" + "github.com/mackerelio/go-osstat/loadavg" + "github.com/prometheus/client_golang/prometheus" + "github.com/livekit/protocol/livekit" "github.com/livekit/protocol/utils" - "github.com/prometheus/client_golang/prometheus" ) const livekitNamespace string = "livekit" @@ -45,7 +47,12 @@ func init() { } func GetUpdatedNodeStats(prev *livekit.NodeStats) (*livekit.NodeStats, error) { - numCPUs, avg1Min, avg5Min, avg15Min, err := getSystemStats() + loadAvg, err := loadavg.Get() + if err != nil { + return nil, err + } + + cpuLoad, numCPUs, err := getCPUStats() if err != nil { return nil, err } @@ -77,9 +84,10 @@ func GetUpdatedNodeStats(prev *livekit.NodeStats) (*livekit.NodeStats, error) { PacketsOutPerSec: perSec(prev.PacketsOut, packetsOutNow, elapsed), NackPerSec: perSec(prev.NackTotal, nackTotalNow, elapsed), NumCpus: numCPUs, - LoadAvgLast1Min: avg1Min, - LoadAvgLast5Min: avg5Min, - LoadAvgLast15Min: avg15Min, + CpuLoad: cpuLoad, + LoadAvgLast1Min: float32(loadAvg.Loadavg1), + LoadAvgLast5Min: float32(loadAvg.Loadavg5), + LoadAvgLast15Min: float32(loadAvg.Loadavg15), }, nil } diff --git a/pkg/telemetry/prometheus/node_linux.go b/pkg/telemetry/prometheus/node_linux.go index 084d90d5..c44441eb 100644 --- a/pkg/telemetry/prometheus/node_linux.go +++ b/pkg/telemetry/prometheus/node_linux.go @@ -4,23 +4,25 @@ package prometheus import ( - linuxproc "github.com/c9s/goprocinfo/linux" + "github.com/mackerelio/go-osstat/cpu" ) -func getSystemStats() (numCPUs uint32, avg1Min, avg5Min, avg15Min float32, err error) { - cpuInfo, err := linuxproc.ReadCPUInfo("/proc/cpuinfo") +var lastCPUTotal, lastCPUIdle uint64 + +func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) { + cpuInfo, err := cpu.Get() if err != nil { return } - loadAvg, err := linuxproc.ReadLoadAvg("/proc/loadavg") - if err != nil { - return + if lastCPUTotal > 0 && lastCPUTotal < cpuInfo.Total { + cpuLoad = 1 - float32(cpuInfo.Idle-lastCPUIdle)/float32(cpuInfo.Total-lastCPUTotal) } - numCPUs = uint32(cpuInfo.NumCPU()) - avg1Min = float32(loadAvg.Last1Min) - avg5Min = float32(loadAvg.Last5Min) - avg15Min = float32(loadAvg.Last15Min) + lastCPUTotal = cpuInfo.Total + lastCPUIdle = cpuInfo.Idle // + cpu.Iowait + + numCPUs = uint32(cpuInfo.CPUCount) + return } diff --git a/pkg/telemetry/prometheus/node_nonlinux.go b/pkg/telemetry/prometheus/node_nonlinux.go index 3d9249a1..49c628c5 100644 --- a/pkg/telemetry/prometheus/node_nonlinux.go +++ b/pkg/telemetry/prometheus/node_nonlinux.go @@ -3,6 +3,24 @@ package prometheus -func getSystemStats() (numCPUs uint32, avg1Min, avg5Min, avg15Min float32, err error) { +import ( + "github.com/mackerelio/go-osstat/cpu" +) + +var lastCPUTotal, lastCPUIdle uint64 + +func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) { + cpuInfo, err := cpu.Get() + if err != nil { + return + } + + if lastCPUTotal > 0 && lastCPUTotal < cpuInfo.Total { + cpuLoad = 1 - float32(cpuInfo.Idle-lastCPUIdle)/float32(cpuInfo.Total-lastCPUTotal) + } + + lastCPUTotal = cpuInfo.Total + lastCPUIdle = cpuInfo.Idle + return }