Skip to content

Commit 9238338

Browse files
authored
Removing concurrency issues from Random Picker (#1314)
1 parent 5bb70f6 commit 9238338

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/epp/scheduling/framework/plugins/picker/max_score_picker.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,13 @@ func (p *MaxScorePicker) Pick(ctx context.Context, cycleState *types.CycleState,
8787
log.FromContext(ctx).V(logutil.DEBUG).Info(fmt.Sprintf("Selecting maximum '%d' pods from %d candidates sorted by max score: %+v", p.maxNumOfEndpoints,
8888
len(scoredPods), scoredPods))
8989

90+
// TODO: merge this with the logic in RandomPicker
91+
// Rand package is not safe for concurrent use, so we create a new instance.
92+
// Source: https://pkg.go.dev/math/rand#pkg-overview
93+
randomGenerator := rand.New(rand.NewSource(time.Now().UnixNano()))
94+
9095
// Shuffle in-place - needed for random tie break when scores are equal
91-
p.randomGenerator.Shuffle(len(scoredPods), func(i, j int) {
96+
randomGenerator.Shuffle(len(scoredPods), func(i, j int) {
9297
scoredPods[i], scoredPods[j] = scoredPods[j], scoredPods[i]
9398
})
9499

pkg/epp/scheduling/framework/plugins/picker/random_picker.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ func NewRandomPicker(maxNumOfEndpoints int) *RandomPicker {
5858
return &RandomPicker{
5959
typedName: plugins.TypedName{Type: RandomPickerType, Name: RandomPickerType},
6060
maxNumOfEndpoints: maxNumOfEndpoints,
61-
randomGenerator: rand.New(rand.NewSource(time.Now().UnixNano())),
6261
}
6362
}
6463

6564
// RandomPicker picks random pod(s) from the list of candidates.
6665
type RandomPicker struct {
6766
typedName plugins.TypedName
6867
maxNumOfEndpoints int
69-
randomGenerator *rand.Rand // randomGenerator for randomly pick endpoint on tie-break
7068
}
7169

7270
// WithName sets the name of the picker.
@@ -85,8 +83,13 @@ func (p *RandomPicker) Pick(ctx context.Context, _ *types.CycleState, scoredPods
8583
log.FromContext(ctx).V(logutil.DEBUG).Info(fmt.Sprintf("Selecting maximum '%d' pods from %d candidates randomly: %+v", p.maxNumOfEndpoints,
8684
len(scoredPods), scoredPods))
8785

86+
// TODO: merge this with the logic in MaxScorePicker
87+
// Rand package is not safe for concurrent use, so we create a new instance.
88+
// Source: https://pkg.go.dev/math/rand#pkg-overview
89+
randomGenerator := rand.New(rand.NewSource(time.Now().UnixNano()))
90+
8891
// Shuffle in-place
89-
p.randomGenerator.Shuffle(len(scoredPods), func(i, j int) {
92+
randomGenerator.Shuffle(len(scoredPods), func(i, j int) {
9093
scoredPods[i], scoredPods[j] = scoredPods[j], scoredPods[i]
9194
})
9295

0 commit comments

Comments
 (0)