@@ -271,33 +271,34 @@ func (fbb *FactorBasedBalance) BackendsToBalance(backends []policy.BackendCtx) (
271271 continue
272272 }
273273 leftBitNum := fbb .totalBitNum
274+ logFields = logFields [:0 ]
274275 for _ , factor := range fbb .factors {
275276 bitNum := factor .ScoreBitNum ()
276277 score1 := scoredBackends [i ].scoreBits << (maxBitNum - leftBitNum ) >> (maxBitNum - bitNum )
277278 score2 := scoredBackends [0 ].scoreBits << (maxBitNum - leftBitNum ) >> (maxBitNum - bitNum )
278- if score1 > score2 {
279+ if score1 >= score2 {
279280 // The factors with higher priorities are ordered, so this factor shouldn't violate them.
280281 // E.g. if the CPU usage of A is higher than B, don't migrate from B to A even if A is preferred in location.
281- var fields []zap.Field
282282 var advice BalanceAdvice
283+ var fields []zap.Field
283284 advice , balanceCount , fields = factor .BalanceCount (scoredBackends [i ], scoredBackends [0 ])
284285 if advice == AdviceNegtive {
285286 // If the factor will be unbalanced after migration, skip the rest factors.
286287 // E.g. if the CPU usage of A will be much higher than B after migration,
287288 // don't migrate from B to A even if A is preferred in location.
288289 break
289- } else if advice == AdvicePositive {
290- if balanceCount > 0.0001 {
291- from , to = scoredBackends [ i ]. BackendCtx , scoredBackends [ 0 ]. BackendCtx
292- reason = factor . Name ()
293- logFields = append ( fields , zap . String ( "factor" , reason ),
294- zap .String ("from_total_score " , strconv . FormatUint ( scoredBackends [ i ]. scoreBits , 16 ) ),
295- zap .String ("to_total_score " , strconv .FormatUint (scoredBackends [0 ].scoreBits , 16 )),
296- zap .Uint64 ( "from_factor_score " , score1 ),
297- zap .Uint64 ("to_factor_score " , score2 ),
298- zap .Float64 ( "balance_count " , balanceCount ))
299- return
300- }
290+ }
291+ logFields = append ( logFields , fields ... )
292+ if score1 > score2 && advice == AdvicePositive && balanceCount > 0.0001 {
293+ from , to = scoredBackends [ i ]. BackendCtx , scoredBackends [ 0 ]. BackendCtx
294+ reason = factor . Name ()
295+ logFields = append ( logFields , zap .String ("factor " , reason ),
296+ zap .String ("from_total_score " , strconv .FormatUint (scoredBackends [i ].scoreBits , 16 )),
297+ zap .String ( "to_total_score " , strconv . FormatUint ( scoredBackends [ 0 ]. scoreBits , 16 ) ),
298+ zap .Uint64 ("from_factor_score " , score1 ),
299+ zap .Uint64 ( "to_factor_score " , score2 ),
300+ zap . Float64 ( "balance_count" , balanceCount ))
301+ return
301302 }
302303 } else if score1 < score2 {
303304 // Stop it once a factor is in the opposite order, otherwise a subsequent factor may violate this one.
0 commit comments