Skip to content

Commit 1f14d89

Browse files
committed
fixes
1 parent b6058b2 commit 1f14d89

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.io.stream.Writeable;
1616
import org.elasticsearch.common.logging.DeprecationLogger;
17+
import org.elasticsearch.core.SuppressForbidden;
1718
import org.elasticsearch.xcontent.ToXContentFragment;
1819
import org.elasticsearch.xcontent.XContentBuilder;
1920

@@ -213,10 +214,10 @@ public String toString() {
213214
}
214215

215216
/**
216-
* Strictly speaking, this is only <em>guaranteed</em> to return the exact right two decimals
217+
* Strictly speaking, this is only <em>guaranteed</em> to return the exact right string
217218
* when {@code numerator} is the rounded value of some multiple of 1% of {@code denominator}.
218-
* Other arbitrary values, where {@code numerator / denominator} exceeds the precision of a
219-
* {@code double}, may be off by a little.
219+
* Other arbitrary values could yield surprising results; for example, you might see
220+
* {@code 123.0} or even {@code 1221.00} in place of {@code 123}.
220221
*/
221222
static String format2Decimals(long numerator, long denominator) {
222223
// If numerator >> denominator, we risk exceeding the precision of a double, so we enlist
@@ -231,7 +232,12 @@ static String format2Decimals(long numerator, long denominator) {
231232
}
232233
}
233234

234-
private static final DecimalFormat MAX_TWO_DECIMALS = new DecimalFormat(".##");
235+
private static final DecimalFormat MAX_TWO_DECIMALS = maxTwoDecimalsFormat();
236+
237+
@SuppressForbidden(reason="We truly just want two decimals; Locale settings don't matter here")
238+
private static DecimalFormat maxTwoDecimalsFormat() {
239+
return new DecimalFormat(".##");
240+
}
235241

236242
public static ByteSizeValue parseBytesSizeValue(String sValue, String settingName) throws ElasticsearchParseException {
237243
return parseBytesSizeValue(sValue, null, settingName);
@@ -322,7 +328,7 @@ private static ByteSizeValue parse(
322328
BigDecimal decimalValue = new BigDecimal(s);
323329
if (decimalValue.scale() > 2) {
324330
throw new ElasticsearchParseException(
325-
"more than two decimal places in setting [{}]: [{}]",
331+
"more than two decimal places in setting [{}] with value [{}]",
326332
settingName,
327333
initialInput
328334
);

server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ public void testParseInvalidNumber() throws IOException {
315315
exception.getMessage()
316316
);
317317

318-
String unitSuffix = (randomBoolean() ? " " : "") + randomFrom(ByteSizeUnit.values()).getSuffix();
318+
ByteSizeUnit unit = randomFrom(ByteSizeUnit.values());
319+
String unitSuffix = (randomBoolean() ? " " : "") + unit.getSuffix();
319320
exception = expectThrows(
320321
ElasticsearchParseException.class,
321322
() -> ByteSizeValue.parseBytesSizeValue("notANumber" + unitSuffix, "test")
@@ -330,7 +331,12 @@ public void testParseInvalidNumber() throws IOException {
330331
);
331332

332333
exception = expectThrows(ElasticsearchParseException.class, () -> ByteSizeValue.parseBytesSizeValue("1.234" + unitSuffix, "test"));
333-
assertEquals("more than two decimal places in setting [test]: [1.234" + unitSuffix + "]", exception.getMessage());
334+
if (unit == BYTES) {
335+
// Decimals aren't allowed at all for bytes
336+
assertEquals("failed to parse setting [test] with value [1.234" + unitSuffix + "]", exception.getMessage());
337+
} else {
338+
assertEquals("more than two decimal places in setting [test] with value [1.234" + unitSuffix + "]", exception.getMessage());
339+
}
334340
}
335341

336342
public void testParseFractionalNumber() throws IOException {

0 commit comments

Comments
 (0)