Skip to content

Commit da7af58

Browse files
authored
Merge pull request kubernetes#75510 from hex108/starttime
Pick pods for preemption based on StartTime of pods when priorities a…
2 parents 8198520 + 042b83b commit da7af58

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

pkg/scheduler/core/extender_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (f *FakeExtender) selectVictimsOnNodeByExtender(
197197
// and get cached node info by given node name.
198198
nodeInfoCopy := f.cachedNodeNameToInfo[node.GetName()].Clone()
199199

200-
potentialVictims := util.SortableList{CompFunc: util.HigherPriorityPod}
200+
potentialVictims := util.SortableList{CompFunc: util.MoreImportantPod}
201201

202202
removePod := func(rp *v1.Pod) {
203203
nodeInfoCopy.RemovePod(rp)

pkg/scheduler/core/generic_scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ func selectVictimsOnNode(
10461046
if nodeInfo == nil {
10471047
return nil, 0, false
10481048
}
1049-
potentialVictims := util.SortableList{CompFunc: util.HigherPriorityPod}
1049+
potentialVictims := util.SortableList{CompFunc: util.MoreImportantPod}
10501050
nodeInfoCopy := nodeInfo.Clone()
10511051

10521052
removePod := func(rp *v1.Pod) {

pkg/scheduler/core/generic_scheduler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,19 @@ func TestSelectNodesForPreemption(t *testing.T) {
946946
{ObjectMeta: metav1.ObjectMeta{Name: "e", UID: types.UID("e")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}}},
947947
expected: map[string]map[string]bool{"machine1": {"b": true, "c": true}},
948948
},
949+
{
950+
name: "mixed priority pods are preempted, pick later StartTime one when priorities are equal",
951+
predicates: map[string]algorithmpredicates.FitPredicate{"matches": algorithmpredicates.PodFitsResources},
952+
nodes: []string{"machine1", "machine2"},
953+
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "machine1", UID: types.UID("machine1")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority}},
954+
pods: []*v1.Pod{
955+
{ObjectMeta: metav1.ObjectMeta{Name: "a", UID: types.UID("a")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190107}},
956+
{ObjectMeta: metav1.ObjectMeta{Name: "b", UID: types.UID("b")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &lowPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190106}},
957+
{ObjectMeta: metav1.ObjectMeta{Name: "c", UID: types.UID("c")}, Spec: v1.PodSpec{Containers: mediumContainers, Priority: &midPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190105}},
958+
{ObjectMeta: metav1.ObjectMeta{Name: "d", UID: types.UID("d")}, Spec: v1.PodSpec{Containers: smallContainers, Priority: &highPriority, NodeName: "machine1"}, Status: v1.PodStatus{StartTime: &startTime20190104}},
959+
{ObjectMeta: metav1.ObjectMeta{Name: "e", UID: types.UID("e")}, Spec: v1.PodSpec{Containers: largeContainers, Priority: &highPriority, NodeName: "machine2"}, Status: v1.PodStatus{StartTime: &startTime20190103}}},
960+
expected: map[string]map[string]bool{"machine1": {"a": true, "c": true}},
961+
},
949962
{
950963
name: "pod with anti-affinity is preempted",
951964
predicates: map[string]algorithmpredicates.FitPredicate{"matches": algorithmpredicates.PodFitsResources},

pkg/scheduler/util/utils.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,15 @@ func (l *SortableList) Sort() {
134134
sort.Sort(l)
135135
}
136136

137-
// HigherPriorityPod return true when priority of the first pod is higher than
138-
// the second one. It takes arguments of the type "interface{}" to be used with
139-
// SortableList, but expects those arguments to be *v1.Pod.
140-
func HigherPriorityPod(pod1, pod2 interface{}) bool {
141-
return GetPodPriority(pod1.(*v1.Pod)) > GetPodPriority(pod2.(*v1.Pod))
137+
// MoreImportantPod return true when priority of the first pod is higher than
138+
// the second one. If two pods' priorities are equal, compare their StartTime.
139+
// It takes arguments of the type "interface{}" to be used with SortableList,
140+
// but expects those arguments to be *v1.Pod.
141+
func MoreImportantPod(pod1, pod2 interface{}) bool {
142+
p1 := GetPodPriority(pod1.(*v1.Pod))
143+
p2 := GetPodPriority(pod2.(*v1.Pod))
144+
if p1 != p2 {
145+
return p1 > p2
146+
}
147+
return GetPodStartTime(pod1.(*v1.Pod)).Before(GetPodStartTime(pod2.(*v1.Pod)))
142148
}

0 commit comments

Comments
 (0)