store cpu load in node stats (#524)

* store cpu load in node stats

* num cpus uint32

* cpu load selector test

* dep update
This commit is contained in:
Mathew Kamkar
2022-03-16 14:51:22 -07:00
committed by GitHub
parent 33f9726b79
commit cac6d22a72
11 changed files with 146 additions and 27 deletions
+2
View File
@@ -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{},
}
+42
View File
@@ -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
}
+37
View File
@@ -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")
}
}
}
+4
View File
@@ -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,
+4 -1
View File
@@ -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,
},
}
+13 -5
View File
@@ -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
}
+12 -10
View File
@@ -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
}
+19 -1
View File
@@ -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
}