Skip to content

Commit e60987b

Browse files
author
Christoph Büscher
authored
Change trappy float comparison (#31889)
Comparing primitive floats with '==' can be trappy because e.g. special handling of NaN values. It can be avoided by using Float.compare().
1 parent a5d5234 commit e60987b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/BalancedShardsAllocator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ private boolean tryRelocateShard(ModelNode minNode, ModelNode maxNode, String id
10091009
// simulate moving shard from maxNode to minNode
10101010
final float delta = weight.weightShardAdded(this, minNode, idx) - weight.weightShardRemoved(this, maxNode, idx);
10111011
if (delta < minCost ||
1012-
(candidate != null && delta == minCost && candidate.id() > shard.id())) {
1012+
(candidate != null && Float.compare(delta, minCost) == 0 && candidate.id() > shard.id())) {
10131013
/* this last line is a tie-breaker to make the shard allocation alg deterministic
10141014
* otherwise we rely on the iteration order of the index.getAllShards() which is a set.*/
10151015
minCost = delta;

0 commit comments

Comments
 (0)