Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/src/main/java/org/elasticsearch/TransportVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ static TransportVersion def(int id) {
public static final TransportVersion REMOVE_DESIRED_NODE_VERSION_90 = def(9_000_0_03);
public static final TransportVersion ESQL_DRIVER_TASK_DESCRIPTION_90 = def(9_000_0_04);
public static final TransportVersion REMOVE_ALL_APPLICABLE_SELECTOR_9_0 = def(9_000_0_05);
public static final TransportVersion BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_90 = def(9_000_0_06);
public static final TransportVersion COHERE_BIT_EMBEDDING_TYPE_SUPPORT_ADDED = def(9_001_0_00);
public static final TransportVersion REMOVE_SNAPSHOT_FAILURES = def(9_002_0_00);
public static final TransportVersion TRANSPORT_STATS_HANDLING_TIME_REQUIRED = def(9_003_0_00);
Expand All @@ -201,6 +202,8 @@ static TransportVersion def(int id) {
public static final TransportVersion REMOVE_REPOSITORY_CONFLICT_MESSAGE = def(9_012_0_00);
public static final TransportVersion RERANKER_FAILURES_ALLOWED = def(9_013_0_00);
public static final TransportVersion VOYAGE_AI_INTEGRATION_ADDED = def(9_014_0_00);
public static final TransportVersion BYTE_SIZE_VALUE_ALWAYS_USES_BYTES = def(9_015_0_00);

/*
* STOP! READ THIS FIRST! No, really,
* ____ _____ ___ ____ _ ____ _____ _ ____ _____ _ _ ___ ____ _____ ___ ____ ____ _____ _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.Locale;
import java.util.Objects;

import static org.elasticsearch.TransportVersions.BYTE_SIZE_VALUE_ALWAYS_USES_BYTES;
import static org.elasticsearch.TransportVersions.BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1;
import static org.elasticsearch.TransportVersions.BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_90;
import static org.elasticsearch.TransportVersions.REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1;
import static org.elasticsearch.common.unit.ByteSizeUnit.BYTES;
import static org.elasticsearch.common.unit.ByteSizeUnit.GB;
Expand Down Expand Up @@ -113,8 +115,7 @@ static ByteSizeValue newByteSizeValue(long sizeInBytes, ByteSizeUnit desiredUnit
public static ByteSizeValue readFrom(StreamInput in) throws IOException {
long size = in.readZLong();
ByteSizeUnit unit = ByteSizeUnit.readFrom(in);
TransportVersion tv = in.getTransportVersion();
if (tv.onOrAfter(BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1) && tv.before(REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1)) {
if (alwaysUseBytes(in.getTransportVersion())) {
return newByteSizeValue(size, unit);
} else {
return of(size, unit);
Expand All @@ -123,15 +124,20 @@ public static ByteSizeValue readFrom(StreamInput in) throws IOException {

@Override
public void writeTo(StreamOutput out) throws IOException {
TransportVersion tv = out.getTransportVersion();
if (tv.onOrAfter(BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1) && tv.before(REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1)) {
if (alwaysUseBytes(out.getTransportVersion())) {
out.writeZLong(sizeInBytes);
} else {
out.writeZLong(Math.divideExact(sizeInBytes, desiredUnit.toBytes(1)));
}
desiredUnit.writeTo(out);
}

private static boolean alwaysUseBytes(TransportVersion tv) {
return tv.onOrAfter(BYTE_SIZE_VALUE_ALWAYS_USES_BYTES)
|| tv.isPatchFrom(BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_90)
|| tv.between(BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1, REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1);
}

ByteSizeValue(long sizeInBytes, ByteSizeUnit desiredUnit) {
this.sizeInBytes = sizeInBytes;
this.desiredUnit = desiredUnit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
Expand All @@ -22,6 +21,10 @@
import java.util.List;
import java.util.function.Function;

import static org.elasticsearch.TransportVersions.BYTE_SIZE_VALUE_ALWAYS_USES_BYTES;
import static org.elasticsearch.TransportVersions.BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_90;
import static org.elasticsearch.TransportVersions.INITIAL_ELASTICSEARCH_9_0;
import static org.elasticsearch.TransportVersions.V_8_16_0;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -520,44 +523,43 @@ protected void assertEqualInstances(ByteSizeValue expectedInstance, ByteSizeValu

public void testBWCTransportFormat() throws IOException {
var tenMegs = ByteSizeValue.ofMb(10);
try (BytesStreamOutput expected = new BytesStreamOutput(); BytesStreamOutput actual = new BytesStreamOutput()) {
expected.writeZLong(10);
ByteSizeUnit.MB.writeTo(expected);
actual.setTransportVersion(TransportVersions.V_8_16_0);
tenMegs.writeTo(actual);
assertArrayEquals(
"Size denominated in the desired unit for backward compatibility",
expected.bytes().array(),
actual.bytes().array()
);
for (var tv : List.of(V_8_16_0, INITIAL_ELASTICSEARCH_9_0)) {
try (BytesStreamOutput expected = new BytesStreamOutput(); BytesStreamOutput actual = new BytesStreamOutput()) {
expected.writeZLong(10);
ByteSizeUnit.MB.writeTo(expected);
actual.setTransportVersion(tv);
tenMegs.writeTo(actual);
assertArrayEquals(
"Size denominated in the desired unit for backward compatibility",
expected.bytes().array(),
actual.bytes().array()
);
}
}
}

/**
* @see TransportVersions#REVERT_BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_1
*/
@AwaitsFix(bugUrl = "https://elasticco.atlassian.net/browse/ES-10585")
public void testTwoDigitTransportRoundTrips() throws IOException {
TransportVersion tv = TransportVersion.current();
for (var desiredUnit : ByteSizeUnit.values()) {
if (desiredUnit == ByteSizeUnit.BYTES) {
continue;
}
checkTransportRoundTrip(ByteSizeValue.parseBytesSizeValue("23" + desiredUnit.getSuffix(), "test"), tv);
for (int tenths = 1; tenths <= 9; tenths++) {
checkTransportRoundTrip(ByteSizeValue.parseBytesSizeValue("23." + tenths + desiredUnit.getSuffix(), "test"), tv);
for (int hundredths = 1; hundredths <= 9; hundredths++) {
checkTransportRoundTrip(
ByteSizeValue.parseBytesSizeValue("23." + tenths + hundredths + desiredUnit.getSuffix(), "test"),
tv
);
for (var tv : List.of(TransportVersion.current(), BYTE_SIZE_VALUE_ALWAYS_USES_BYTES, BYTE_SIZE_VALUE_ALWAYS_USES_BYTES_90)) {
for (var desiredUnit : ByteSizeUnit.values()) {
if (desiredUnit == ByteSizeUnit.BYTES) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we skip bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't have a fraction of a byte, but you can have a fraction of any other unit.

continue;
}
checkTransportRoundTrip(ByteSizeValue.parseBytesSizeValue("23" + desiredUnit.getSuffix(), "test"), tv);
for (int tenths = 1; tenths <= 9; tenths++) {
checkTransportRoundTrip(ByteSizeValue.parseBytesSizeValue("23." + tenths + desiredUnit.getSuffix(), "test"), tv);
for (int hundredths = 1; hundredths <= 9; hundredths++) {
checkTransportRoundTrip(
ByteSizeValue.parseBytesSizeValue("23." + tenths + hundredths + desiredUnit.getSuffix(), "test"),
tv
);
}
}
}
}
}

public void testIntegerTransportRoundTrips() throws IOException {
for (var tv : List.of(TransportVersion.current(), TransportVersions.V_8_16_0)) {
for (var tv : List.of(TransportVersion.current(), V_8_16_0)) {
checkTransportRoundTrip(ByteSizeValue.ONE, tv);
checkTransportRoundTrip(ByteSizeValue.ZERO, tv);
checkTransportRoundTrip(ByteSizeValue.MINUS_ONE, tv);
Expand Down