From fc94295ea0ef5b84798ddb85d07e88cbbc6068b7 Mon Sep 17 00:00:00 2001 From: buptcozy <70878006+buptcozy@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:42:08 +0800 Subject: [PATCH] scheduler: modify coscheduling Less function with considering childScheduleCycle (#1702) Signed-off-by: xingbao.zy Co-authored-by: xingbao.zy --- .../plugins/coscheduling/core/core.go | 10 +++++ .../plugins/coscheduling/core/core_test.go | 3 ++ .../plugins/coscheduling/coscheduling.go | 6 +++ .../plugins/coscheduling/coscheduling_test.go | 38 ++++++++++++++++--- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/pkg/scheduler/plugins/coscheduling/core/core.go b/pkg/scheduler/plugins/coscheduling/core/core.go index 7241daf42..832393e41 100644 --- a/pkg/scheduler/plugins/coscheduling/core/core.go +++ b/pkg/scheduler/plugins/coscheduling/core/core.go @@ -72,6 +72,7 @@ type Manager interface { GetGangSummary(gangId string) (*GangSummary, bool) GetGangSummaries() map[string]*GangSummary IsGangMinSatisfied(*corev1.Pod) bool + GetChildScheduleCycle(*corev1.Pod) int } // PodGroupManager defines the scheduling operation called @@ -534,3 +535,12 @@ func (pgMgr *PodGroupManager) GetGangSummaries() map[string]*GangSummary { return result } + +func (pgMgr *PodGroupManager) GetChildScheduleCycle(pod *corev1.Pod) int { + gang := pgMgr.GetGangByPod(pod) + if gang == nil { + return 0 + } + + return gang.getChildScheduleCycle(pod) +} diff --git a/pkg/scheduler/plugins/coscheduling/core/core_test.go b/pkg/scheduler/plugins/coscheduling/core/core_test.go index 79b44f238..69a789fb2 100644 --- a/pkg/scheduler/plugins/coscheduling/core/core_test.go +++ b/pkg/scheduler/plugins/coscheduling/core/core_test.go @@ -306,6 +306,9 @@ func TestPlugin_PreFilter(t *testing.T) { assert.Equal(t, tt.expectedScheduleCycle, gang.getScheduleCycle()) assert.Equal(t, tt.expectedScheduleCycleValid, gang.isScheduleCycleValid()) assert.Equal(t, tt.expectedChildCycleMap, gang.ChildrenScheduleRoundMap) + + assert.Equal(t, tt.expectedChildCycleMap[util.GetId(tt.pod.Namespace, tt.pod.Name)], + mgr.GetChildScheduleCycle(tt.pod)) } }) } diff --git a/pkg/scheduler/plugins/coscheduling/coscheduling.go b/pkg/scheduler/plugins/coscheduling/coscheduling.go index 93ba871b0..a05a4c93a 100644 --- a/pkg/scheduler/plugins/coscheduling/coscheduling.go +++ b/pkg/scheduler/plugins/coscheduling/coscheduling.go @@ -145,6 +145,12 @@ func (cs *Coscheduling) Less(podInfo1, podInfo2 *framework.QueuedPodInfo) bool { return !isgang1satisfied } + childScheduleCycle1 := cs.pgMgr.GetChildScheduleCycle(podInfo1.Pod) + childScheduleCycle2 := cs.pgMgr.GetChildScheduleCycle(podInfo2.Pod) + if childScheduleCycle1 != childScheduleCycle2 { + return childScheduleCycle1 < childScheduleCycle2 + } + creationTime1 := cs.pgMgr.GetCreatTime(podInfo1) creationTime2 := cs.pgMgr.GetCreatTime(podInfo2) if creationTime1.Equal(creationTime2) { diff --git a/pkg/scheduler/plugins/coscheduling/coscheduling_test.go b/pkg/scheduler/plugins/coscheduling/coscheduling_test.go index 835e5e01e..8299e1aae 100644 --- a/pkg/scheduler/plugins/coscheduling/coscheduling_test.go +++ b/pkg/scheduler/plugins/coscheduling/coscheduling_test.go @@ -22,6 +22,8 @@ import ( "testing" "time" + "github.com/koordinator-sh/koordinator/pkg/scheduler/plugins/coscheduling/core" + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -319,11 +321,13 @@ func TestLess(t *testing.T) { } time.Sleep(100 * time.Millisecond) for _, tt := range []struct { - name string - p1 *framework.QueuedPodInfo - p2 *framework.QueuedPodInfo - annotations map[string]string - expected bool + name string + p1 *framework.QueuedPodInfo + p2 *framework.QueuedPodInfo + childScheduleCycle1 int + childScheduleCycle2 int + annotations map[string]string + expected bool }{ { name: "p1.priority less than p2.priority,but p1's subPriority is greater than p2's", @@ -457,6 +461,20 @@ func TestLess(t *testing.T) { }, expected: true, // p1 should be ahead of p2 in the queue }, + { + name: "equal priority and creation time, both belongs to gangB, childScheduleCycle not equal", + p1: &framework.QueuedPodInfo{ + PodInfo: framework.NewPodInfo(st.MakePod().Namespace(gangB_ns).Name("pod1").Priority(highPriority).Label(v1alpha1.PodGroupLabel, "gangB").Obj()), + InitialAttemptTimestamp: lateTime, + }, + p2: &framework.QueuedPodInfo{ + PodInfo: framework.NewPodInfo(st.MakePod().Namespace(gangB_ns).Name("pod2").Priority(highPriority).Label(v1alpha1.PodGroupLabel, "gangB").Obj()), + InitialAttemptTimestamp: earltTime, + }, + childScheduleCycle1: 2, + childScheduleCycle2: 1, + expected: false, // p1 should be ahead of p2 in the queue + }, { name: "equal priority and creation time, p1 belongs to gangA that has been satisfied", p1: &framework.QueuedPodInfo{ @@ -475,6 +493,16 @@ func TestLess(t *testing.T) { if len(tt.annotations) != 0 { tt.p1.Pod.Annotations = tt.annotations } + + gang1 := gp.pgMgr.(*core.PodGroupManager).GetGangByPod(tt.p1.Pod) + gang2 := gp.pgMgr.(*core.PodGroupManager).GetGangByPod(tt.p2.Pod) + if gang1 != nil { + gang1.ChildrenScheduleRoundMap[util.GetId(tt.p1.Pod.Namespace, tt.p1.Pod.Name)] = tt.childScheduleCycle1 + } + if gang2 != nil { + gang2.ChildrenScheduleRoundMap[util.GetId(tt.p2.Pod.Namespace, tt.p2.Pod.Name)] = tt.childScheduleCycle2 + } + if got := gp.Less(tt.p1, tt.p2); got != tt.expected { t.Errorf("expected %v, got %v", tt.expected, got) }