@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "math"
23
24
"os"
24
25
"time"
25
26
@@ -111,11 +112,33 @@ func (s *KVCacheAwareScorer) Score(ctx *types.Context, pod types.Pod) (float64,
111
112
112
113
// update the cache
113
114
s .scoresCache = make (map [string ]float64 , len (scores ))
115
+ minScore := math .MaxFloat64
116
+ maxScore := 0.0
114
117
for _ , pod := range scores {
115
118
s .scoresCache [pod .Name ] = pod .Score
119
+
120
+ // maintain min and max to normalize later
121
+ if pod .Score < minScore {
122
+ minScore = pod .Score
123
+ }
124
+
125
+ if pod .Score > maxScore {
126
+ maxScore = pod .Score
127
+ }
116
128
}
129
+ // normalize scores
130
+ s .scoresCache = normalizeScores (s .scoresCache , minScore , maxScore )
131
+
117
132
s .scoresCacheLastUpdate = time .Now ()
118
133
loggerDebug .Info ("Updated scores cache" , "time" , s .scoresCacheLastUpdate , "pods" , s .scoresCache )
119
134
120
135
return s .scoresCache [pod .GetPod ().Address ], nil
121
136
}
137
+
138
+ func normalizeScores (scores map [string ]float64 , minScore , maxScore float64 ) map [string ]float64 {
139
+ normalizedScores := make (map [string ]float64 , len (scores ))
140
+ for podName , score := range scores {
141
+ normalizedScores [podName ] = (score - minScore ) / (maxScore - minScore )
142
+ }
143
+ return normalizedScores
144
+ }
0 commit comments