Skip to content

Commit a5e854b

Browse files
authored
Merge pull request #718 from k8s-infra-cherrypick-robot/cherry-pick-700-to-release-1.28
[release-1.28] Avoid unnecessary requeue operations in coscheduling
2 parents ac06ba6 + 873a591 commit a5e854b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

pkg/coscheduling/core/core.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,22 @@ const (
4848
PodGroupNotFound Status = "PodGroup not found"
4949
Success Status = "Success"
5050
Wait Status = "Wait"
51+
52+
permitStateKey = "PermitCoscheduling"
5153
)
5254

55+
type PermitState struct {
56+
Activate bool
57+
}
58+
59+
func (s *PermitState) Clone() framework.StateData {
60+
return &PermitState{Activate: s.Activate}
61+
}
62+
5363
// Manager defines the interfaces for PodGroup management.
5464
type Manager interface {
5565
PreFilter(context.Context, *corev1.Pod) error
56-
Permit(context.Context, *corev1.Pod) Status
66+
Permit(context.Context, *framework.CycleState, *corev1.Pod) Status
5767
GetPodGroup(context.Context, *corev1.Pod) (string, *v1alpha1.PodGroup)
5868
GetCreationTimestamp(*corev1.Pod, time.Time) time.Time
5969
DeletePermittedPodGroup(string)
@@ -108,6 +118,13 @@ func (pgMgr *PodGroupManager) ActivateSiblings(pod *corev1.Pod, state *framework
108118
return
109119
}
110120

121+
// Only proceed if it's explicitly requested to activate sibling pods.
122+
if c, err := state.Read(permitStateKey); err != nil {
123+
return
124+
} else if s, ok := c.(*PermitState); !ok || !s.Activate {
125+
return
126+
}
127+
111128
pods, err := pgMgr.podLister.Pods(pod.Namespace).List(
112129
labels.SelectorFromSet(labels.Set{v1alpha1.PodGroupLabel: pgName}),
113130
)
@@ -193,7 +210,7 @@ func (pgMgr *PodGroupManager) PreFilter(ctx context.Context, pod *corev1.Pod) er
193210
}
194211

195212
// Permit permits a pod to run, if the minMember match, it would send a signal to chan.
196-
func (pgMgr *PodGroupManager) Permit(ctx context.Context, pod *corev1.Pod) Status {
213+
func (pgMgr *PodGroupManager) Permit(ctx context.Context, state *framework.CycleState, pod *corev1.Pod) Status {
197214
pgFullName, pg := pgMgr.GetPodGroup(ctx, pod)
198215
if pgFullName == "" {
199216
return PodGroupNotSpecified
@@ -209,6 +226,19 @@ func (pgMgr *PodGroupManager) Permit(ctx context.Context, pod *corev1.Pod) Statu
209226
if int32(assigned)+1 >= pg.Spec.MinMember {
210227
return Success
211228
}
229+
230+
if assigned == 0 {
231+
// Given we've reached Permit(), it's mean all PreFilter checks (minMember & minResource)
232+
// already pass through, so if assigned == 0, it could be due to:
233+
// - minResource get satisfied
234+
// - new pods added
235+
// In either case, we should and only should use this 0-th pod to trigger activating
236+
// its siblings.
237+
// It'd be in-efficient if we trigger activating siblings unconditionally.
238+
// See https://github.com/kubernetes-sigs/scheduler-plugins/issues/682
239+
state.Write(permitStateKey, &PermitState{Activate: true})
240+
}
241+
212242
return Wait
213243
}
214244

pkg/coscheduling/core/core_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/client-go/informers"
2929
clientsetfake "k8s.io/client-go/kubernetes/fake"
3030
clicache "k8s.io/client-go/tools/cache"
31+
"k8s.io/kubernetes/pkg/scheduler/framework"
3132
st "k8s.io/kubernetes/pkg/scheduler/testing"
3233
"sigs.k8s.io/scheduler-plugins/apis/scheduling/v1alpha1"
3334
tu "sigs.k8s.io/scheduler-plugins/test/util"
@@ -278,7 +279,7 @@ func TestPermit(t *testing.T) {
278279
podInformer.Informer().GetStore().Add(p)
279280
}
280281

281-
if got := pgMgr.Permit(ctx, tt.pod); got != tt.want {
282+
if got := pgMgr.Permit(ctx, &framework.CycleState{}, tt.pod); got != tt.want {
282283
t.Errorf("Want %v, but got %v", tt.want, got)
283284
}
284285
})

pkg/coscheduling/coscheduling.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (cs *Coscheduling) PreFilterExtensions() framework.PreFilterExtensions {
204204
// Permit is the functions invoked by the framework at "Permit" extension point.
205205
func (cs *Coscheduling) Permit(ctx context.Context, state *framework.CycleState, pod *v1.Pod, nodeName string) (*framework.Status, time.Duration) {
206206
waitTime := *cs.scheduleTimeout
207-
s := cs.pgMgr.Permit(ctx, pod)
207+
s := cs.pgMgr.Permit(ctx, state, pod)
208208
var retStatus *framework.Status
209209
switch s {
210210
case core.PodGroupNotSpecified:

0 commit comments

Comments
 (0)