Skip to content

Commit

Permalink
koordlet: fix bug that cpu eviction does not take effect (koordinator…
Browse files Browse the repository at this point in the history
…-sh#1932)

Signed-off-by: Wang Xiaoqiang <wangxiaoqiang@qiyi.com>
  • Loading branch information
wangxiaoq authored and ls-2018 committed Mar 25, 2024
1 parent 1cdbd72 commit 2d7715e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/koordlet/util/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,23 @@ func GetBECgroupCurCPUSet() ([]int32, error) {
if err != nil {
return nil, err
}
if len(containerPaths) != 0 {
targetCgroupDir = containerPaths[0]
}
containerPaths = append(containerPaths, targetCgroupDir)

cpus, err := resourceexecutor.NewCgroupReader().ReadCPUSet(targetCgroupDir)
if err != nil {
return nil, err
// find the minimal length of container's cpuset to avoid the interference of sandbox's cpuset
// since the cpuset's value of sandbox could always be set to all cpu ids of the machine by kubelet
var targetCpus []int32
for _, path := range containerPaths {
cpuStr, err := resourceexecutor.NewCgroupReader().ReadCPUSet(path)
if err != nil {
return nil, err
}
curCpus := cpuset.ParseCPUSet(cpuStr)
if targetCpus == nil || len(curCpus) < len(targetCpus) {
targetCpus = curCpus
}
}
return cpuset.ParseCPUSet(cpus), nil

return targetCpus, nil
}

// GetBECPUSetPathsByMaxDepth gets all the be cpuset groups' paths recursively from upper to lower
Expand Down
47 changes: 47 additions & 0 deletions pkg/koordlet/util/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,50 @@ func TestGetCgroupPathsByTargetDepth(t *testing.T) {
})
}
}

func TestGetBECgroupCurCPUSet(t *testing.T) {
type fields struct {
prepareFn func(helper *system.FileTestUtil)
}
tests := []struct {
name string
fields fields
want []int32
wantErr bool
}{
{
name: "get the cpuset value of be containers fail",
wantErr: true,
},
{
name: "get the cpuset value of be containers successcully",
fields: fields{
prepareFn: func(helper *system.FileTestUtil) {
helper.SetCgroupsV2(false)
cpuset, _ := system.GetCgroupResource(system.CPUSetCPUSName)
qosCgroupDir := GetPodQoSRelativePath(corev1.PodQOSBestEffort)
helper.WriteCgroupFileContents(qosCgroupDir, cpuset, "0-63")
containerCgroupDir := filepath.Join(qosCgroupDir, "/kubepods-test-besteffort-pod-0.slice/cri-containerd-xxx.scope")
sandboxCgroupDir := filepath.Join(qosCgroupDir, "/kubepods-test-besteffort-pod-0.slice/cri-containerd-yyy.scope")
helper.WriteCgroupFileContents(containerCgroupDir, cpuset, "0-63")
helper.WriteCgroupFileContents(sandboxCgroupDir, cpuset, "0-1")
},
},
wantErr: false,
want: []int32{0, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
if tt.fields.prepareFn != nil {
tt.fields.prepareFn(helper)
}

got, gotErr := GetBECgroupCurCPUSet()
assert.Equal(t, tt.wantErr, gotErr != nil, gotErr)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 2d7715e

Please sign in to comment.