Skip to content

Commit

Permalink
scheduler: pick vf by random (koordinator-sh#1953)
Browse files Browse the repository at this point in the history
Signed-off-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com>
Co-authored-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com>
  • Loading branch information
2 people authored and ls-2018 committed Mar 25, 2024
1 parent e3838c1 commit 4568bb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/scheduler/plugins/deviceshare/device_allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package deviceshare

import (
"fmt"
"math/rand"
"sort"

corev1 "k8s.io/api/core/v1"
Expand All @@ -33,6 +34,10 @@ import (
"github.com/koordinator-sh/koordinator/pkg/util/bitmask"
)

var (
randIntnFn = rand.Intn
)

var deviceHandlers = map[schedulingv1alpha1.DeviceType]DeviceHandler{}
var deviceAllocators = map[schedulingv1alpha1.DeviceType]DeviceAllocator{}

Expand Down Expand Up @@ -476,10 +481,11 @@ func allocateVF(vfAllocation *VFAllocation, deviceInfos map[int]*schedulingv1alp
if len(remainingVFs) == 0 {
return nil
}
// Here we sort the remaining vf just for test deterministic. In fact, we pick the vf by random to alleviating some unexpected vf duplicate allocation problem due to uncoordinated scheduling and node-side vf allocation components
sort.Slice(remainingVFs, func(i, j int) bool {
return remainingVFs[i].BusID < remainingVFs[j].BusID
})
vf := &remainingVFs[0]
vf := &remainingVFs[randIntnFn(len(remainingVFs))]
return vf
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/scheduler/plugins/deviceshare/device_allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ func TestAutopilotAllocator(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
randIntnFn = func(n int) int {
return 0
}
deviceCR := tt.deviceCR.DeepCopy()
deviceCR.ResourceVersion = "1"
koordFakeClient := koordfake.NewSimpleClientset()
Expand Down Expand Up @@ -1703,6 +1706,9 @@ func TestAutopilotAllocatorWithExclusivePolicyAndRequiredScope(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
randIntnFn = func(n int) int {
return 0
}
deviceCR := tt.deviceCR.DeepCopy()
deviceCR.ResourceVersion = "1"
koordFakeClient := koordfake.NewSimpleClientset()
Expand Down

0 comments on commit 4568bb8

Please sign in to comment.