@@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "math"
2424 "math/rand"
25+ "slices"
2526 "sort"
2627 "time"
2728
@@ -104,52 +105,35 @@ func (p *WeightedRandomPicker) TypedName() plugins.TypedName {
104105// Pick selects the pod(s) randomly from the list of candidates, where the probability of the pod to get picked is derived
105106// from its weighted score.
106107func (p * WeightedRandomPicker ) Pick (ctx context.Context , cycleState * types.CycleState , scoredPods []* types.ScoredPod ) * types.ProfileRunResult {
107- log .FromContext (ctx ).V (logutil .DEBUG ).Info (fmt .Sprintf ("Selecting maximum '%d' pods from %d candidates using weighted random sampling: %+v" ,
108- p .maxNumOfEndpoints , len (scoredPods ), scoredPods ))
109-
110- // Check if all weights are zero or negative
111- allZeroWeights := true
112- for _ , scoredPod := range scoredPods {
113- if scoredPod .Score > 0 {
114- allZeroWeights = false
115- break
116- }
117- }
118-
119- // Delegate to RandomPicker for uniform selection when all weights are zero
120- if allZeroWeights {
121- log .FromContext (ctx ).V (logutil .DEBUG ).Info ("All weights are zero, delegating to RandomPicker for uniform selection" )
108+ // Check if there is at least one pod with Score > 0, if not let random picker run
109+ if slices .IndexFunc (scoredPods , func (scoredPod * types.ScoredPod ) bool { return scoredPod .Score > 0 }) == - 1 {
110+ log .FromContext (ctx ).V (logutil .DEBUG ).Info ("All scores are zero, delegating to RandomPicker for uniform selection" )
122111 return p .randomPicker .Pick (ctx , cycleState , scoredPods )
123112 }
124113
114+ log .FromContext (ctx ).V (logutil .DEBUG ).Info (fmt .Sprintf ("Selecting maximum '%d' pods from %d candidates using weighted random sampling: %+v" ,
115+ p .maxNumOfEndpoints , len (scoredPods ), scoredPods ))
116+
125117 randomGenerator := rand .New (rand .NewSource (time .Now ().UnixNano ()))
126118
127119 // A-Res algorithm: keyᵢ = Uᵢ^(1/wᵢ)
128- weightedPods := make ([]weightedScoredPod , 0 , len (scoredPods ))
129-
130- for _ , scoredPod := range scoredPods {
131- weight := float64 (scoredPod .Score )
132-
133- // Handle zero or negative weights
134- if weight <= 0 {
135- // Assign very small key for zero-weight pods (effectively excludes them)
136- weightedPods = append (weightedPods , weightedScoredPod {
137- ScoredPod : scoredPod ,
138- key : 0 ,
139- })
120+ weightedPods := make ([]weightedScoredPod , len (scoredPods ))
121+
122+ for i , scoredPod := range scoredPods {
123+ // Handle zero score
124+ if scoredPod .Score <= 0 {
125+ // Assign key=0 for zero-score pods (effectively excludes them from selection)
126+ weightedPods [i ] = weightedScoredPod {ScoredPod : scoredPod , key : 0 }
140127 continue
141128 }
142129
143- // Generate random number U in (0,1)
130+ // If we're here the scoredPod.Score > 0. Generate a random number U in (0,1)
144131 u := randomGenerator .Float64 ()
145132 if u == 0 {
146133 u = 1e-10 // Avoid log(0)
147134 }
148135
149- weightedPods = append (weightedPods , weightedScoredPod {
150- ScoredPod : scoredPod ,
151- key : math .Pow (u , 1.0 / weight ), // key = U^(1/weight)
152- })
136+ weightedPods [i ] = weightedScoredPod {ScoredPod : scoredPod , key : math .Pow (u , 1.0 / scoredPod .Score )} // key = U^(1/weight)
153137 }
154138
155139 // Sort by key in descending order (largest keys first)
0 commit comments