Skip to content

Commit b8730e3

Browse files
authored
perf(scheduler): avoid pod-affinity pre-score allocations (#1983)
Signed-off-by: Erez Freiberger <enoodle@gmail.com>
1 parent d46194d commit b8730e3

5 files changed

Lines changed: 74 additions & 11 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: |-
3+
Removed unnecessary pod-affinity pre-score allocation churn

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ BENCH_OUTPUT ?= benchmark-results.txt
154154
# because some reclaim benchmarks require -benchtime=1x and only a curated subset
155155
# should run in CI.
156156
BENCH_SPECIAL_PACKAGES := ./pkg/scheduler/actions/reclaim
157-
BENCH_SPECIAL_REGEX := '^BenchmarkReclaim(WithMissingPVCJobs|UnschedulableDistributedJob_(10|50|100)Node)$$'
157+
BENCH_SPECIAL_REGEX := '^BenchmarkReclaim(WithMissingPVCJobs|UnschedulableDistributedJob_((10|50|100)Node|AntiAffinity100Node))$$'
158158

159159
.PHONY: benchstat
160160
benchstat: $(BENCHSTAT)

pkg/scheduler/actions/reclaim/reclaim_benchmark_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/pod_status"
1919
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/podgroup_info/subgroup_info"
2020
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/topology_info"
21+
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/conf"
22+
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/conf_util"
2123
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/constants"
2224
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/test_utils"
2325
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/test_utils/jobs_fake"
@@ -239,6 +241,10 @@ func buildUnschedulableDistributedReclaimBenchmarkTopology(
239241
}
240242

241243
jobs = append(jobs, distributedJob)
244+
var schedulerConfig *conf.SchedulerConfiguration
245+
if params.FailureMode == unschedulableDistributedReclaimAntiAffinity {
246+
schedulerConfig = schedulerConfigWithPodAffinity()
247+
}
242248

243249
return test_utils.TestTopologyBasic{
244250
Name: "unschedulable distributed reclaim benchmark",
@@ -259,10 +265,21 @@ func buildUnschedulableDistributedReclaimBenchmarkTopology(
259265
},
260266
Mocks: &test_utils.TestMock{
261267
CacheRequirements: &test_utils.CacheMocking{},
268+
SchedulerConf: schedulerConfig,
262269
},
263270
}
264271
}
265272

273+
func schedulerConfigWithPodAffinity() *conf.SchedulerConfiguration {
274+
config, err := conf_util.GetDefaultSchedulerConf()
275+
if err != nil {
276+
panic(err)
277+
}
278+
config.ScenarioSearchBudgets = nil
279+
config.Tiers[0].Plugins = append(config.Tiers[0].Plugins, conf.PluginOption{Name: "podaffinity"})
280+
return config
281+
}
282+
266283
func unschedulableDistributedRackCount(params unschedulableDistributedReclaimBenchmarkParams) int {
267284
switch params.FailureMode {
268285
case unschedulableDistributedReclaimAntiAffinity:

pkg/scheduler/plugins/podaffinity/podaffinity.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
package podaffinity
55

66
import (
7-
ksf "k8s.io/kube-scheduler/framework"
8-
k8sframework "k8s.io/kubernetes/pkg/scheduler/framework"
9-
107
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api"
118
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/common_info"
129
"github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/api/node_info"
@@ -50,13 +47,8 @@ func (pp *podAffinityPlugin) OnSessionOpen(ssn *framework.Session) {
5047
}
5148

5249
func (pp *podAffinityPlugin) nodePreOrderFn(k8sPlugins *k8s_internal.SessionScoreFns) api.NodePreOrderFn {
53-
return func(task *pod_info.PodInfo, fittingNodes []*node_info.NodeInfo) error {
54-
var nodes []ksf.NodeInfo
55-
for range fittingNodes {
56-
nodes = append(nodes, &k8sframework.NodeInfo{})
57-
}
58-
59-
status := k8sPlugins.PrePodAffinity(task.Pod, nodes)
50+
return func(task *pod_info.PodInfo, _ []*node_info.NodeInfo) error {
51+
status := k8sPlugins.PrePodAffinity(task.Pod, nil)
6052
if status.IsSkip() {
6153
pp.skipOrderFn.add(task.UID)
6254
}

pkg/scheduler/plugins/podaffinity/podaffinity_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ var _ = Describe("Preferred Pod Affinity", func() {
5959
},
6060
},
6161
}
62+
exampleAntiAffinity := &v1.Affinity{
63+
PodAntiAffinity: &v1.PodAntiAffinity{
64+
PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
65+
{
66+
Weight: 10,
67+
PodAffinityTerm: v1.PodAffinityTerm{
68+
LabelSelector: &metav1.LabelSelector{
69+
MatchLabels: exampleLabels,
70+
},
71+
TopologyKey: nodeLabelName,
72+
},
73+
},
74+
},
75+
},
76+
}
6277

6378
for testName, testData := range map[string]testInput{
6479
"No Pod Affinity": {
@@ -156,6 +171,42 @@ var _ = Describe("Preferred Pod Affinity", func() {
156171
}, resource_info.NewResourceVectorMap()),
157172
expectedScore: 2 * 10 * scores.K8sPlugins,
158173
},
174+
"Matching Pod Anti-Affinity": {
175+
nodes: []*v1.Node{
176+
{
177+
ObjectMeta: metav1.ObjectMeta{
178+
Name: "node-1",
179+
Labels: map[string]string{
180+
nodeLabelName: "node-1",
181+
},
182+
},
183+
},
184+
},
185+
pods: []*v1.Pod{
186+
{
187+
ObjectMeta: metav1.ObjectMeta{
188+
Name: "existing-pod-1",
189+
Namespace: "test",
190+
Labels: exampleLabels,
191+
},
192+
Spec: v1.PodSpec{
193+
NodeName: "node-1",
194+
Affinity: exampleAntiAffinity.DeepCopy(),
195+
},
196+
},
197+
},
198+
task: pod_info.NewTaskInfo(&v1.Pod{
199+
ObjectMeta: metav1.ObjectMeta{
200+
Name: "test-pod",
201+
Namespace: "test",
202+
Labels: exampleLabels,
203+
},
204+
Spec: v1.PodSpec{
205+
Affinity: exampleAntiAffinity.DeepCopy(),
206+
},
207+
}, resource_info.NewResourceVectorMap()),
208+
expectedScore: -2 * 10 * scores.K8sPlugins,
209+
},
159210
} {
160211
testName := testName
161212
testData := testData

0 commit comments

Comments
 (0)