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().
This commit is contained in:
Christoph Büscher 2018-07-10 09:19:11 +02:00 committed by GitHub
parent a5d5234eff
commit e60987b95f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 1 deletions

View File

@ -1009,7 +1009,7 @@ public class BalancedShardsAllocator extends AbstractComponent implements Shards
// simulate moving shard from maxNode to minNode // simulate moving shard from maxNode to minNode
final float delta = weight.weightShardAdded(this, minNode, idx) - weight.weightShardRemoved(this, maxNode, idx); final float delta = weight.weightShardAdded(this, minNode, idx) - weight.weightShardRemoved(this, maxNode, idx);
if (delta < minCost || if (delta < minCost ||
(candidate != null && delta == minCost && candidate.id() > shard.id())) { (candidate != null && Float.compare(delta, minCost) == 0 && candidate.id() > shard.id())) {
/* this last line is a tie-breaker to make the shard allocation alg deterministic /* this last line is a tie-breaker to make the shard allocation alg deterministic
* otherwise we rely on the iteration order of the index.getAllShards() which is a set.*/ * otherwise we rely on the iteration order of the index.getAllShards() which is a set.*/
minCost = delta; minCost = delta;