Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of fractional byte size value settings #37172

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ public Builder put(final String key, final TimeValue timeValue) {
* @param byteSizeValue The setting value
* @return The builder
*/
public Builder put(String key, ByteSizeValue byteSizeValue) {
return put(key, byteSizeValue.toString());
public Builder put(final String key, final ByteSizeValue byteSizeValue) {
return put(key, byteSizeValue.getStringRep());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -726,4 +728,20 @@ public void testFractionalTimeValue() {
assertThat(actual, equalTo(expected));
}

public void testFractionalByteSizeValue() {
final Setting<ByteSizeValue> setting =
Setting.byteSizeSetting("key", ByteSizeValue.parseBytesSizeValue(randomIntBetween(1, 16) + "k", "key"));
final ByteSizeValue expected = new ByteSizeValue(randomNonNegativeLong(), ByteSizeUnit.BYTES);
final Settings settings = Settings.builder().put("key", expected).build();
/*
* Previously we would internally convert the byte size value to a string using a method that tries to be smart about the units
* (e.g., 1024 bytes would be converted to 1kb). However, this had a problem in that, for example, 1536 bytes would be converted to
* 1.5k. Then, 1.5k could not be converted back to a ByteSizeValue because ByteSizeValues do not support fractional components.
* Effectively this test is then asserting that we no longer make this mistake when doing the internal string conversion. Instead,
* we convert to a string using a method that does not lose the original unit.
*/
final ByteSizeValue actual = setting.get(settings);
assertThat(actual, equalTo(expected));
}

}