From 848c2a8eebedadecdd02dbe1ea06f2c16f5e12b8 Mon Sep 17 00:00:00 2001 From: "wangjianyu.wjy" Date: Thu, 14 Mar 2024 11:17:36 +0800 Subject: [PATCH] scheduler: pick vf by random Signed-off-by: wangjianyu.wjy --- pkg/scheduler/plugins/deviceshare/device_allocator.go | 8 +++++++- .../plugins/deviceshare/device_allocator_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/scheduler/plugins/deviceshare/device_allocator.go b/pkg/scheduler/plugins/deviceshare/device_allocator.go index 262e57e21..f70d6d993 100644 --- a/pkg/scheduler/plugins/deviceshare/device_allocator.go +++ b/pkg/scheduler/plugins/deviceshare/device_allocator.go @@ -18,6 +18,7 @@ package deviceshare import ( "fmt" + "math/rand" "sort" corev1 "k8s.io/api/core/v1" @@ -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{} @@ -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 } diff --git a/pkg/scheduler/plugins/deviceshare/device_allocator_test.go b/pkg/scheduler/plugins/deviceshare/device_allocator_test.go index 629fea82a..ea0198ef9 100644 --- a/pkg/scheduler/plugins/deviceshare/device_allocator_test.go +++ b/pkg/scheduler/plugins/deviceshare/device_allocator_test.go @@ -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() @@ -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()