Skip to content

Commit

Permalink
fix(QOSSort): refine queue sorting logic by adding inqueue timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Jan 13, 2025
1 parent f633dd2 commit 3d85d00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/qos/queue_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ func (pl *Sort) Name() string {

// Less is the function used by the activeQ heap algorithm to sort pods.
// It sorts pods based on their priorities. When the priorities are equal, it uses
// the Pod QoS classes to break the tie.
// the Pod QoS classes to break the tie. If both the priority and QoS class are equal,
// it uses PodQueueInfo.timestamp.
func (*Sort) Less(pInfo1, pInfo2 *framework.QueuedPodInfo) bool {
p1 := corev1helpers.PodPriority(pInfo1.Pod)
p2 := corev1helpers.PodPriority(pInfo2.Pod)
return (p1 > p2) || (p1 == p2 && compQOS(pInfo1.Pod, pInfo2.Pod))

if p1 != p2 {
return p1 > p2
}
if compQOS(pInfo1.Pod, pInfo2.Pod) {
return true
}
return pInfo1.Timestamp.Before(pInfo2.Timestamp)
}

func compQOS(p1, p2 *v1.Pod) bool {
Expand Down
15 changes: 15 additions & 0 deletions pkg/qos/queue_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package qos

import (
"testing"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -31,6 +32,8 @@ func createPodInfo(pod *v1.Pod) *framework.PodInfo {
}

func TestSortLess(t *testing.T) {
t1 := time.Now()
t2 := t1.Add(time.Second)
tests := []struct {
name string
pInfo1 *framework.QueuedPodInfo
Expand Down Expand Up @@ -117,6 +120,18 @@ func TestSortLess(t *testing.T) {
},
want: true,
},
{
name: "both p1 and p2 are Guaranteed, but p1 is added to schedulingQ earlier than p2",
pInfo1: &framework.QueuedPodInfo{
PodInfo: createPodInfo(makePod("p1", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi"))),
Timestamp: t1,
},
pInfo2: &framework.QueuedPodInfo{
PodInfo: createPodInfo(makePod("p2", 0, getResList("100m", "100Mi"), getResList("100m", "100Mi"))),
Timestamp: t2,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 3d85d00

Please sign in to comment.