Skip to content

Commit af3cbe7

Browse files
authored
Limit precision when formatting percentage in DesiredBalanceReconciler (elastic#97811)
1 parent 19c69a1 commit af3cbe7

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
2222
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
2323
import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider;
24+
import org.elasticsearch.common.Strings;
2425
import org.elasticsearch.common.settings.ClusterSettings;
2526
import org.elasticsearch.common.settings.Setting;
2627
import org.elasticsearch.core.TimeValue;
@@ -461,11 +462,11 @@ private void maybeLogUndesiredAllocationsWarning(long allAllocations, long undes
461462
if (allAllocations > 0 && undesiredAllocations > undesiredAllocationsLogThreshold * allAllocations) {
462463
undesiredAllocationLogInterval.maybeExecute(
463464
() -> logger.warn(
464-
"[{}%] of assigned shards ({}/{}) are not on their desired nodes, which exceeds the warn threshold of [{}%]",
465-
100.0 * undesiredAllocations / allAllocations,
465+
"[{}] of assigned shards ({}/{}) are not on their desired nodes, which exceeds the warn threshold of [{}]",
466+
Strings.format1Decimals(100.0 * undesiredAllocations / allAllocations, "%"),
466467
undesiredAllocations,
467468
allAllocations,
468-
100.0 * undesiredAllocationsLogThreshold
469+
Strings.format1Decimals(100.0 * undesiredAllocationsLogThreshold, "%")
469470
)
470471
);
471472
}

server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconcilerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ public void testShouldLogOnTooManyUndesiredAllocations() {
11591159
"Should log first too many shards on undesired locations",
11601160
DesiredBalanceReconciler.class.getCanonicalName(),
11611161
Level.WARN,
1162-
"[100.0%] of assigned shards (1/1) are not on their desired nodes, which exceeds the warn threshold of [10.0%]"
1162+
"[100%] of assigned shards (1/1) are not on their desired nodes, which exceeds the warn threshold of [10%]"
11631163
)
11641164
);
11651165
assertThatLogger(

server/src/test/java/org/elasticsearch/common/StringsTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ public void testStripDisallowedChars() {
364364
assertEquals(validFileName, stripDisallowedChars(invalidFileName.toString()));
365365
}
366366

367+
public void testFormat1Decimals() {
368+
assertThat(Strings.format1Decimals(100.0 / 2, "%"), equalTo("50%"));
369+
assertThat(Strings.format1Decimals(100.0 / 3, "%"), equalTo("33.3%"));
370+
}
371+
367372
private static String lowercaseAsciiOnly(String s) {
368373
// explicitly lowercase just ascii characters
369374
StringBuilder sb = new StringBuilder(s);

0 commit comments

Comments
 (0)