Skip to content

Commit

Permalink
Migrate the remaining uses of Version to TransportVersion (#93384)
Browse files Browse the repository at this point in the history
Remove get/setVersion methods
  • Loading branch information
thecoop committed Feb 13, 2023
1 parent 7b98f79 commit 4c46cca
Show file tree
Hide file tree
Showing 107 changed files with 592 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package org.elasticsearch.geometry;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.geometry.utils.GeographyValidator;
import org.elasticsearch.geometry.utils.WellKnownText;
import org.elasticsearch.test.AbstractWireTestCase;
Expand All @@ -32,7 +32,7 @@ protected final T createTestInstance() {

@SuppressWarnings("unchecked")
@Override
protected T copyInstance(T instance, Version version) throws IOException {
protected T copyInstance(T instance, TransportVersion version) throws IOException {
String text = WellKnownText.toWKT(instance);
try {
return (T) WellKnownText.fromWKT(GeographyValidator.instance(true), true, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.aggregations.bucket.histogram;

import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.aggregations.bucket.AggregationMultiBucketAggregationTestCase;
import org.elasticsearch.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder.RoundingInfo;
import org.elasticsearch.aggregations.bucket.histogram.InternalAutoDateHistogram.BucketInfo;
Expand All @@ -26,7 +26,7 @@
import org.elasticsearch.search.aggregations.InternalAggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.xcontent.ContextParser;

import java.io.IOException;
Expand Down Expand Up @@ -475,10 +475,10 @@ public void testSerializationPre830() throws IOException {
createTestMetadata(),
InternalAggregations.EMPTY
);
Version version = VersionUtils.randomVersionBetween(
TransportVersion version = TransportVersionUtils.randomVersionBetween(
random(),
Version.CURRENT.minimumCompatibilityVersion(),
VersionUtils.getPreviousVersion(Version.V_8_3_0)
TransportVersion.CURRENT.minimumCompatibilityVersion(),
TransportVersionUtils.getPreviousVersion(TransportVersion.V_8_3_0)
);
InternalAutoDateHistogram deserialized = copyInstance(instance, version);
assertEquals(1, deserialized.getBucketInnerInterval());
Expand All @@ -504,7 +504,7 @@ public void testReadFromPre830() throws IOException {
+ "AAyAAAAZAF5BHllYXIAAARib29sAQAAAAAAAAAKZAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
);
try (StreamInput in = new NamedWriteableAwareStreamInput(new BytesArray(bytes).streamInput(), getNamedWriteableRegistry())) {
in.setVersion(Version.V_8_2_0);
in.setTransportVersion(TransportVersion.V_8_2_0);
InternalAutoDateHistogram deserialized = new InternalAutoDateHistogram(in);
assertEquals("name", deserialized.getName());
assertEquals(1, deserialized.getBucketInnerInterval());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.script.mustache;

import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -200,6 +200,9 @@ public void testCCSCheckCompatibility() throws Exception {
Exception ex = response.getFailure();
assertThat(ex.getMessage(), containsString("[class org.elasticsearch.action.search.SearchRequest] is not compatible with version"));
assertThat(ex.getMessage(), containsString("'search.check_ccs_compatibility' setting is enabled."));
assertEquals("This query isn't serializable to nodes before " + Version.CURRENT, ex.getCause().getMessage());
assertEquals(
"This query isn't serializable with transport versions before " + TransportVersion.CURRENT,
ex.getCause().getMessage()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package org.elasticsearch.script.mustache;

import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.search.SearchRequest;
Expand Down Expand Up @@ -376,6 +376,9 @@ public void testCCSCheckCompatibility() throws Exception {
containsString("[class org.elasticsearch.action.search.SearchRequest] is not compatible with version")
);
assertThat(ex.getCause().getMessage(), containsString("'search.check_ccs_compatibility' setting is enabled."));
assertEquals("This query isn't serializable to nodes before " + Version.CURRENT, ex.getCause().getCause().getMessage());
assertEquals(
"This query isn't serializable with transport versions before " + TransportVersion.CURRENT,
ex.getCause().getCause().getMessage()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.search.similarities.PerFieldSimilarityWrapper;
import org.apache.lucene.search.similarities.Similarity;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent;
Expand All @@ -45,7 +46,7 @@
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
Expand Down Expand Up @@ -182,7 +183,7 @@ protected void doAssertLuceneQuery(HasChildQueryBuilder queryBuilder, Query quer
* Test (de)serialization on all previous released versions
*/
public void testSerializationBWC() throws IOException {
for (Version version : VersionUtils.allReleasedVersions()) {
for (TransportVersion version : TransportVersionUtils.allReleasedVersions()) {
HasChildQueryBuilder testQuery = createTestQueryBuilder();
assertSerialization(testQuery, version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.index.mapper.MapperService;
Expand All @@ -31,7 +31,7 @@
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
Expand Down Expand Up @@ -152,7 +152,7 @@ protected void doAssertLuceneQuery(HasParentQueryBuilder queryBuilder, Query que
* Test (de)serialization on all previous released versions
*/
public void testSerializationBWC() throws IOException {
for (Version version : VersionUtils.allReleasedVersions()) {
for (TransportVersion version : TransportVersionUtils.allReleasedVersions()) {
HasParentQueryBuilder testQuery = createTestQueryBuilder();
assertSerialization(testQuery, version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldTy
registry
)
) {
input.setVersion(indexVersion);
input.setTransportVersion(indexVersion.transportVersion);
// Query builder's content is stored via BinaryFieldMapper, which has a custom encoding
// to encode multiple binary values into a single binary doc values field.
// This is the reason we need to first need to read the number of values and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ static void createQueryBuilderField(
) throws IOException {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
try (OutputStreamStreamOutput out = new OutputStreamStreamOutput(stream)) {
out.setVersion(indexVersion);
out.setTransportVersion(indexVersion.transportVersion);
out.writeNamedWriteable(queryBuilder);
qbField.indexValue(context, stream.toByteArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.reindex;

import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
Expand Down Expand Up @@ -185,15 +185,15 @@ public void testRethrottleRequest() throws IOException {
}

private StreamInput toInputByteStream(Writeable example) throws IOException {
return toInputByteStream(Version.CURRENT, example);
return toInputByteStream(TransportVersion.CURRENT, example);
}

private StreamInput toInputByteStream(Version version, Writeable example) throws IOException {
private StreamInput toInputByteStream(TransportVersion version, Writeable example) throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(version);
out.setTransportVersion(version);
example.writeTo(out);
StreamInput in = out.bytes().streamInput();
in.setVersion(version);
in.setTransportVersion(version);
return new NamedWriteableAwareStreamInput(in, writableRegistry());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void testQueryBuilderBWC() throws Exception {
byte[] qbSource = Base64.getDecoder().decode(queryBuilderStr);
try (InputStream in = new ByteArrayInputStream(qbSource, 0, qbSource.length)) {
try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), registry)) {
input.setVersion(getOldClusterVersion());
input.setTransportVersion(getOldClusterVersion().transportVersion);
QueryBuilder queryBuilder = input.readNamedWriteable(QueryBuilder.class);
assert in.read() == -1;
assertEquals(expectedQueryBuilder, queryBuilder);
Expand Down
11 changes: 4 additions & 7 deletions server/src/main/java/org/elasticsearch/TransportVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ static NavigableMap<Integer, TransportVersion> getAllVersionIds(Class<?> cls) {
VERSION_IDS = getAllVersionIds(TransportVersion.class);
}

static Collection<TransportVersion> getAllVersions() {
return VERSION_IDS.values();
}

public static TransportVersion readVersion(StreamInput in) throws IOException {
return fromId(in.readVInt());
}
Expand Down Expand Up @@ -226,13 +230,6 @@ public static TransportVersion max(TransportVersion version1, TransportVersion v
return version1.id > version2.id ? version1 : version2;
}

/**
* returns a sorted collection of declared transport version constants
*/
public static Collection<TransportVersion> getAllVersions() {
return VERSION_IDS.values();
}

public final int id;
private final String uniqueId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public void sendSearchResponse(InternalSearchResponse internalSearchResponse, At
final String scrollId = request.scroll() != null ? TransportSearchHelper.buildScrollId(queryResults) : null;
final String searchContextId;
if (buildPointInTimeFromSearchResults()) {
searchContextId = SearchContextId.encode(queryResults.asList(), aliasFilter, minNodeVersion);
searchContextId = SearchContextId.encode(queryResults.asList(), aliasFilter, minNodeVersion.transportVersion);
} else {
if (request.source() != null && request.source().pointInTimeBuilder() != null) {
searchContextId = request.source().pointInTimeBuilder().getEncodedId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

package org.elasticsearch.action.search;

import org.elasticsearch.Version;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
Expand Down Expand Up @@ -58,7 +58,11 @@ public boolean contains(ShardSearchContextId contextId) {
return contextIds.contains(contextId);
}

public static String encode(List<SearchPhaseResult> searchPhaseResults, Map<String, AliasFilter> aliasFilter, Version version) {
public static String encode(
List<SearchPhaseResult> searchPhaseResults,
Map<String, AliasFilter> aliasFilter,
TransportVersion version
) {
final Map<ShardId, SearchContextIdForNode> shards = new HashMap<>();
for (SearchPhaseResult searchPhaseResult : searchPhaseResults) {
final SearchShardTarget target = searchPhaseResult.getSearchShardTarget();
Expand All @@ -68,8 +72,8 @@ public static String encode(List<SearchPhaseResult> searchPhaseResults, Map<Stri
);
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(version);
Version.writeVersion(version, out);
out.setTransportVersion(version);
TransportVersion.writeVersion(version, out);
out.writeMap(shards, (o, k) -> k.writeTo(o), (o, v) -> v.writeTo(o));
out.writeMap(aliasFilter, StreamOutput::writeString, (o, v) -> v.writeTo(o));
return Base64.getUrlEncoder().encodeToString(BytesReference.toBytes(out.bytes()));
Expand All @@ -86,8 +90,8 @@ public static SearchContextId decode(NamedWriteableRegistry namedWriteableRegist
throw new IllegalArgumentException("invalid id: [" + id + "]", e);
}
try (StreamInput in = new NamedWriteableAwareStreamInput(new ByteBufferStreamInput(byteBuffer), namedWriteableRegistry)) {
final Version version = Version.readVersion(in);
in.setVersion(version);
final TransportVersion version = TransportVersion.readVersion(in);
in.setTransportVersion(version);
final Map<ShardId, SearchContextIdForNode> shards = in.readMap(ShardId::new, SearchContextIdForNode::new);
final Map<String, AliasFilter> aliasFilters = in.readMap(StreamInput::readString, AliasFilter::readFrom);
if (in.available() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static ParsedScrollId parseScrollId(String scrollId) {
*/
public static void checkCCSVersionCompatibility(Writeable writeableRequest) {
try {
writeableRequest.writeTo(new VersionCheckingStreamOutput(CCS_CHECK_VERSION));
writeableRequest.writeTo(new VersionCheckingStreamOutput(CCS_CHECK_VERSION.transportVersion));
} catch (Exception e) {
// if we cannot serialize, raise this as an error to indicate to the caller that CCS has problems with this request
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionListenerResponseHandler;
Expand Down Expand Up @@ -95,7 +96,7 @@ public class JoinValidationService {
private final Supplier<ClusterState> clusterStateSupplier;
private final AtomicInteger queueSize = new AtomicInteger();
private final Queue<AbstractRunnable> queue = new ConcurrentLinkedQueue<>();
private final Map<Version, ReleasableBytesReference> statesByVersion = new HashMap<>();
private final Map<TransportVersion, ReleasableBytesReference> statesByVersion = new HashMap<>();
private final RefCounted executeRefs;

public JoinValidationService(
Expand Down Expand Up @@ -301,7 +302,7 @@ protected void doRun() throws Exception {
assert discoveryNode.getVersion().onOrAfter(Version.V_8_3_0) : discoveryNode.getVersion();
// NB these things never run concurrently to each other, or to the cache cleaner (see IMPLEMENTATION NOTES above) so it is safe
// to do these (non-atomic) things to the (unsynchronized) statesByVersion map.
final var cachedBytes = statesByVersion.get(discoveryNode.getVersion());
final var cachedBytes = statesByVersion.get(discoveryNode.getVersion().transportVersion);
final var bytes = Objects.requireNonNullElseGet(cachedBytes, () -> serializeClusterState(discoveryNode));
assert bytes.hasReferences() : "already closed";
bytes.incRef();
Expand Down Expand Up @@ -343,20 +344,20 @@ private ReleasableBytesReference serializeClusterState(DiscoveryNode discoveryNo
var success = false;
try {
final var clusterState = clusterStateSupplier.get();
final var version = discoveryNode.getVersion();
final var version = discoveryNode.getVersion().transportVersion;
try (
var stream = new OutputStreamStreamOutput(
CompressorFactory.COMPRESSOR.threadLocalOutputStream(Streams.flushOnCloseStream(bytesStream))
)
) {
stream.setVersion(version);
stream.setTransportVersion(version);
clusterState.writeTo(stream);
} catch (IOException e) {
throw new ElasticsearchException("failed to serialize cluster state for publishing to node {}", e, discoveryNode);
}
final var newBytes = new ReleasableBytesReference(bytesStream.bytes(), bytesStream);
logger.trace(
"serialized join validation cluster state version [{}] for node version [{}] with size [{}]",
"serialized join validation cluster state version [{}] for transport version [{}] with size [{}]",
clusterState.version(),
version,
newBytes.length()
Expand Down

0 comments on commit 4c46cca

Please sign in to comment.