Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
340136c
add primary patch version to transport version
jdconrad Jul 17, 2025
46cf098
add loading for new transport version model
jdconrad Jul 17, 2025
a4ec0d6
update transport versions holder
jdconrad Jul 17, 2025
dc9cfe9
fix bugs
jdconrad Jul 17, 2025
a20dfd7
Merge branch 'main' into transport0
jdconrad Jul 18, 2025
b772a3a
update name
jdconrad Jul 21, 2025
3182209
Merge branch 'main' into transport0
jdconrad Jul 21, 2025
8aa55ca
fix equals/hashcode/toString
jdconrad Jul 21, 2025
f13e723
spotless
jdconrad Jul 21, 2025
ead340b
Merge branch 'main' into transport0
jdconrad Jul 21, 2025
be9d70b
migrate version with multiple patches
jdconrad Jul 21, 2025
01511ff
spotless
jdconrad Jul 21, 2025
cba38b1
fix names
jdconrad Jul 21, 2025
f9bebe8
Merge branch 'main' into transport0
jdconrad Jul 21, 2025
b02cb1a
name fixes
jdconrad Jul 22, 2025
a19caac
Merge branch 'main' into transport0
jdconrad Jul 22, 2025
c56ec3a
move examples to tests
jdconrad Jul 22, 2025
75fbbe4
fix new instances
jdconrad Jul 22, 2025
c42bfc2
add versions to test files
jdconrad Jul 22, 2025
1b272b6
Merge branch 'main' into transport0
jdconrad Jul 22, 2025
da5491c
update tests
jdconrad Jul 22, 2025
c80f8fb
Merge branch 'main' into transport0
jdconrad Jul 22, 2025
19b1923
add java docs
jdconrad Jul 23, 2025
ac02157
move latest
jdconrad Jul 23, 2025
71d2cc4
Merge branch 'main' into transport0
jdconrad Jul 23, 2025
6f4dd0d
move latest
jdconrad Jul 23, 2025
38d5710
more pr responses
jdconrad Jul 23, 2025
eb0a94f
fix ij refactoring
jdconrad Jul 23, 2025
518ed4b
more fixed files
jdconrad Jul 23, 2025
14e2539
add latest placeholder files for backport versions
jdconrad Jul 23, 2025
ec39480
Merge branch 'main' into transport0
jdconrad Jul 23, 2025
18d2206
switch to loading versions with txt files instead of json
jdconrad Jul 23, 2025
aa7a4bc
revert lazy change
jdconrad Jul 23, 2025
5da94f9
Merge branch 'main' into transport0
jdconrad Jul 23, 2025
e097c7a
Merge branch 'main' into transport0
jdconrad Jul 24, 2025
8c8ecfc
update to use csv files and remove names from files when possible
jdconrad Jul 24, 2025
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
207 changes: 192 additions & 15 deletions server/src/main/java/org/elasticsearch/TransportVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.internal.VersionExtension;
import org.elasticsearch.plugins.ExtensionLoader;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Represents the version of the wire protocol used to communicate between a pair of ES nodes.
Expand Down Expand Up @@ -57,7 +69,68 @@
* different version value. If you need to know whether the cluster as a whole speaks a new enough {@link TransportVersion} to understand a
* newly-added feature, use {@link org.elasticsearch.cluster.ClusterState#getMinTransportVersion}.
*/
public record TransportVersion(int id) implements VersionId<TransportVersion> {
public class TransportVersion implements VersionId<TransportVersion> {
Copy link
Member

Choose a reason for hiding this comment

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

Is there something about these changes that precludes using a record?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had gone down making the members private without getters in an initial version, but I don't think that makes sense anymore. I switched this back to a record. One thing I wasn't completely sure about is the implications of having name included as part of equals/hashcode. I would hope nothing is using these for direct comparisons for any type of serialization branching.

Copy link
Member

Choose a reason for hiding this comment

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

You can customize equals and hashCode for the record to only consider the id field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but is it correct to only use id for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this to only use id to maintain exactly what was already there.


private final String name;
private final int id;
private final TransportVersion nextPatchVersion;

public TransportVersion(int id) {
this(null, id, null);
}

public TransportVersion(String name, int id, TransportVersion patchVersion) {
this.name = name;
this.id = id;
this.nextPatchVersion = patchVersion;
}

public String name() {
return name;
}

public int id() {
return id;
}

public TransportVersion nextPatchVersion() {
return nextPatchVersion;
}

private static final ParseField NAME = new ParseField("name");
private static final ParseField IDS = new ParseField("ids");

private static final ConstructingObjectParser<TransportVersion, Integer> PARSER = new ConstructingObjectParser<>(
TransportVersion.class.getCanonicalName(),
false,
(args, latestTransportId) -> {
String name = (String) args[0];
@SuppressWarnings("unchecked")
List<Integer> ids = (List<Integer>) args[1];
ids.sort(Integer::compareTo);
TransportVersion transportVersion = null;
for (int idIndex = 0; idIndex < ids.size(); ++idIndex) {
if (ids.get(idIndex) > latestTransportId) {
break;
}
transportVersion = new TransportVersion(name, ids.get(idIndex), transportVersion);
}
return transportVersion;
}
);

static {
PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME);
PARSER.declareIntArray(ConstructingObjectParser.constructorArg(), IDS);
}

public static TransportVersion fromXContent(InputStream inputStream, Integer latest) {
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, inputStream)) {
return PARSER.apply(parser, latest);
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
}

public static TransportVersion readVersion(StreamInput in) throws IOException {
return fromId(in.readVInt());
Expand All @@ -70,12 +143,20 @@ public static TransportVersion readVersion(StreamInput in) throws IOException {
* The new instance is not registered in {@code TransportVersion.getAllVersions}.
*/
public static TransportVersion fromId(int id) {
TransportVersion known = VersionsHolder.ALL_VERSIONS_MAP.get(id);
TransportVersion known = VersionsHolder.ALL_VERSIONS_BY_ID.get(id);
if (known != null) {
return known;
}
// this is a version we don't otherwise know about - just create a placeholder
return new TransportVersion(id);
return new TransportVersion(null, id, null);
}

public static TransportVersion fromName(String name) {
Copy link
Member

Choose a reason for hiding this comment

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

Javadocs please, referencing how these are are in resource files and looked up

Copy link
Contributor

Choose a reason for hiding this comment

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

Javadoc should probably also explain usage/generation, or perhaps leave a TODO untill that part is finalized.

Thinking out loud: fromName is fine, but I'm wondering if we can somehow indicate that this method is special, and unique from fromId? E.g. can we indicate how this method isn't just used to get a TV from a name, but how it also generates/declares the TV too. Some ideas: withName, declare, usingName, access, use, retrieve. TBH I don't know if any of those are better. Hmm, I suppose I'm fine if we stick with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added JavaDocs for the methods that would be used by consumers. I consider loading to be generally internal.

TransportVersion known = VersionsHolder.ALL_VERSIONS_BY_NAME.get(name);
if (known == null) {
throw new IllegalStateException("unknown transport version [" + name + "]");
}
return known;
}

public static void writeVersion(TransportVersion version, StreamOutput out) throws IOException {
Expand Down Expand Up @@ -123,7 +204,7 @@ public static List<TransportVersion> getAllVersions() {
* in the wild (they're sent over the wire by numeric ID) but we don't know how to communicate using such versions.
*/
public boolean isKnown() {
return VersionsHolder.ALL_VERSIONS_MAP.containsKey(id);
return VersionsHolder.ALL_VERSIONS_BY_ID.containsKey(id);
}

/**
Expand All @@ -135,7 +216,7 @@ public TransportVersion bestKnownVersion() {
return this;
}
TransportVersion bestSoFar = TransportVersions.ZERO;
for (final var knownVersion : VersionsHolder.ALL_VERSIONS_MAP.values()) {
for (final var knownVersion : VersionsHolder.ALL_VERSIONS_BY_ID.values()) {
if (knownVersion.after(bestSoFar) && knownVersion.before(this)) {
bestSoFar = knownVersion;
}
Expand Down Expand Up @@ -171,38 +252,134 @@ public boolean isPatchFrom(TransportVersion version) {
return onOrAfter(version) && id < version.id + 100 - (version.id % 100);
}

public boolean supports(TransportVersion version) {
if (onOrAfter(version)) {
return true;
}
TransportVersion nextPatchVersion = version.nextPatchVersion;
Copy link
Member

Choose a reason for hiding this comment

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

This linked structure makes the logic a little harder to follow. I'm still not sure it's better than an array of patch ids. But if we are to use it, can we at least document it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I can see how this is simpler on one hand, and also creates less objects in memory. However, I'm uncertain about perf implications, if that even matters? This is effectively the same as boxing/wrapping, as our comparison now requires loading objects from the heap, IIUC. I'm not sure that matters, however, so I'll leave it as your call Jack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would prefer to leave it this way for now as I think having duplicate ids floating around even if an internal representation may be confusing. Right now it should have similar performance to checking each patch version since those ids are "unboxed" as well.

while (nextPatchVersion != null) {
if (isPatchFrom(version)) {
return true;
}
nextPatchVersion = nextPatchVersion.nextPatchVersion;
}
return false;
}

/**
* Returns a string representing the Elasticsearch release version of this transport version,
* if applicable for this deployment, otherwise the raw version number.
*/
public String toReleaseVersion() {
return TransportVersions.VERSION_LOOKUP.apply(id);
return VersionsHolder.VERSION_LOOKUP_BY_RELEASE.apply(id);
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
TransportVersion that = (TransportVersion) o;
return id == that.id;
}

@Override
public int hashCode() {
return id;
}

@Override
public String toString() {
return Integer.toString(id);
return "" + id;
Copy link
Member

Choose a reason for hiding this comment

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

Why use string building instead of the explicit toString that was here before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the new members to toString, but later decided to not make changes here. I reverted this to how it was originally.

}

private static class VersionsHolder {

private static final List<TransportVersion> ALL_VERSIONS;
private static final Map<Integer, TransportVersion> ALL_VERSIONS_MAP;
private static final Map<Integer, TransportVersion> ALL_VERSIONS_BY_ID;
private static final Map<String, TransportVersion> ALL_VERSIONS_BY_NAME;
private static final IntFunction<String> VERSION_LOOKUP_BY_RELEASE;
private static final TransportVersion CURRENT;

static {
// collect all the transport versions from server and es modules/plugins (defined in server)
List<TransportVersion> allVersions = new ArrayList<>(TransportVersions.DEFINED_VERSIONS);
Map<String, TransportVersion> allVersionsByName = loadTransportVersionsByName();
addTransportVersions(allVersionsByName.values(), allVersions).sort(TransportVersion::compareTo);

// set version lookup by release before adding serverless versions
VERSION_LOOKUP_BY_RELEASE = ReleaseVersions.generateVersionsLookup(
TransportVersions.class,
allVersions.get(allVersions.size() - 1).id()
);

// collect all the transport versions from serverless
Collection<TransportVersion> extendedVersions = ExtensionLoader.loadSingleton(ServiceLoader.load(VersionExtension.class))
.map(VersionExtension::getTransportVersions)
.orElse(Collections.emptyList());
addTransportVersions(extendedVersions, allVersions).sort(TransportVersion::compareTo);
for (TransportVersion version : extendedVersions) {
if (version.name() != null) {
allVersionsByName.put(version.name(), version);
}
}

// set the transport version lookups
ALL_VERSIONS = Collections.unmodifiableList(allVersions);
ALL_VERSIONS_BY_ID = ALL_VERSIONS.stream().collect(Collectors.toUnmodifiableMap(TransportVersion::id, Function.identity()));
ALL_VERSIONS_BY_NAME = Collections.unmodifiableMap(allVersionsByName);
CURRENT = ALL_VERSIONS.getLast();
}

private static Map<String, TransportVersion> loadTransportVersionsByName() {
Map<String, TransportVersion> transportVersions = new HashMap<>();

String latestLocation = "/transport/latest/" + Version.CURRENT.major + "." + Version.CURRENT.minor + "-LATEST.json";
Copy link
Member

Choose a reason for hiding this comment

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

fwiw since we already have the "latest" prefix, we don't also need the "-LATEST" suffix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

int latestId;
try (InputStream inputStream = TransportVersion.class.getResourceAsStream(latestLocation)) {
TransportVersion latest = fromXContent(inputStream, Integer.MAX_VALUE);
if (latest == null) {
throw new IllegalStateException(
"invalid latest transport version for release version [" + Version.CURRENT.major + "." + Version.CURRENT.minor + "]"
Copy link
Member

Choose a reason for hiding this comment

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

nit: this isn't a release version, it's a minor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

);
}
latestId = latest.id();
} catch (IOException ioe) {
throw new UncheckedIOException("latest transport version file not found at [" + latestLocation + "]", ioe);
}

if (extendedVersions.isEmpty()) {
ALL_VERSIONS = TransportVersions.DEFINED_VERSIONS;
} else {
ALL_VERSIONS = Stream.concat(TransportVersions.DEFINED_VERSIONS.stream(), extendedVersions.stream()).sorted().toList();
String manifestLocation = "/transport/generated/generated-transport-versions-files-manifest.txt";
Copy link
Member

Choose a reason for hiding this comment

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

nit: similar to the above, the prefix already has generated, so we don't also need the file to have "generated"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this to just manifest.txt, and I'm guessing we will have one in both constant and generated folders, but not 100% sure yet.

List<String> versionFileNames;
try (InputStream transportVersionsManifest = TransportVersion.class.getResourceAsStream(manifestLocation)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(transportVersionsManifest, StandardCharsets.UTF_8));
versionFileNames = reader.lines().filter(line -> line.isBlank() == false).toList();
} catch (IOException ioe) {
throw new UncheckedIOException("transport version manifest file not found at [" + manifestLocation + "]", ioe);
}

ALL_VERSIONS_MAP = ALL_VERSIONS.stream().collect(Collectors.toUnmodifiableMap(TransportVersion::id, Function.identity()));
for (String name : versionFileNames) {
String versionLocation = "/transport/generated/" + name;
try (InputStream inputStream = TransportVersion.class.getResourceAsStream(versionLocation)) {
TransportVersion transportVersion = TransportVersion.fromXContent(inputStream, latestId);
if (transportVersion != null) {
transportVersions.put(transportVersion.name(), transportVersion);
}
} catch (IOException ioe) {
throw new UncheckedIOException("transport version set file not found at [ " + versionLocation + "]", ioe);
}
}

CURRENT = ALL_VERSIONS.getLast();
return transportVersions;
}

private static List<TransportVersion> addTransportVersions(Collection<TransportVersion> addFrom, List<TransportVersion> addTo) {
for (TransportVersion transportVersion : addFrom) {
addTo.add(transportVersion);
TransportVersion patchVersion = transportVersion.nextPatchVersion();
while (patchVersion != null) {
addTo.add(patchVersion);
patchVersion = patchVersion.nextPatchVersion();
}
}
return addTo;
}
}
}
18 changes: 0 additions & 18 deletions server/src/main/java/org/elasticsearch/TransportVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.IntFunction;

/**
* <p>Transport version is used to coordinate compatible wire protocol communication between nodes, at a fine-grained level. This replaces
Expand Down Expand Up @@ -210,7 +209,6 @@ static TransportVersion def(int id) {
public static final TransportVersion ML_INFERENCE_COHERE_API_VERSION_8_19 = def(8_841_0_60);
public static final TransportVersion ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED_8_19 = def(8_841_0_61);
public static final TransportVersion ESQL_PROFILE_INCLUDE_PLAN_8_19 = def(8_841_0_62);
public static final TransportVersion ESQL_SPLIT_ON_BIG_VALUES_8_19 = def(8_841_0_63);
public static final TransportVersion ESQL_FIXED_INDEX_LIKE_8_19 = def(8_841_0_64);
public static final TransportVersion V_9_0_0 = def(9_000_0_09);
public static final TransportVersion INITIAL_ELASTICSEARCH_9_0_1 = def(9_000_0_10);
Expand Down Expand Up @@ -328,26 +326,22 @@ static TransportVersion def(int id) {
public static final TransportVersion ML_INFERENCE_COHERE_API_VERSION = def(9_110_0_00);
public static final TransportVersion ESQL_PROFILE_INCLUDE_PLAN = def(9_111_0_00);
public static final TransportVersion MAPPINGS_IN_DATA_STREAMS = def(9_112_0_00);
public static final TransportVersion ESQL_SPLIT_ON_BIG_VALUES_9_1 = def(9_112_0_01);
public static final TransportVersion ESQL_FIXED_INDEX_LIKE_9_1 = def(9_112_0_02);
public static final TransportVersion ESQL_SAMPLE_OPERATOR_STATUS_9_1 = def(9_112_0_03);
// Below is the first version in 9.2 and NOT in 9.1.
public static final TransportVersion PROJECT_STATE_REGISTRY_RECORDS_DELETIONS = def(9_113_0_00);
public static final TransportVersion ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE = def(9_114_0_00);
public static final TransportVersion ML_INFERENCE_IBM_WATSONX_COMPLETION_ADDED = def(9_115_0_00);
public static final TransportVersion ESQL_SPLIT_ON_BIG_VALUES = def(9_116_0_00);
public static final TransportVersion ESQL_LOCAL_RELATION_WITH_NEW_BLOCKS = def(9_117_0_00);
public static final TransportVersion ML_INFERENCE_CUSTOM_SERVICE_EMBEDDING_TYPE = def(9_118_0_00);
public static final TransportVersion ESQL_FIXED_INDEX_LIKE = def(9_119_0_00);
public static final TransportVersion LOOKUP_JOIN_CCS = def(9_120_0_00);
public static final TransportVersion NODE_USAGE_STATS_FOR_THREAD_POOLS_IN_CLUSTER_INFO = def(9_121_0_00);
public static final TransportVersion ESQL_CATEGORIZE_OPTIONS = def(9_122_0_00);
public static final TransportVersion ML_INFERENCE_AZURE_AI_STUDIO_RERANK_ADDED = def(9_123_0_00);
public static final TransportVersion PROJECT_STATE_REGISTRY_ENTRY = def(9_124_0_00);
public static final TransportVersion ML_INFERENCE_LLAMA_ADDED = def(9_125_0_00);
public static final TransportVersion SHARD_WRITE_LOAD_IN_CLUSTER_INFO = def(9_126_0_00);
public static final TransportVersion ESQL_SAMPLE_OPERATOR_STATUS = def(9_127_0_00);
public static final TransportVersion ESQL_TOPN_TIMINGS = def(9_128_0_00);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down Expand Up @@ -421,16 +415,6 @@ static TransportVersion def(int id) {
*/
static final List<TransportVersion> DEFINED_VERSIONS = collectAllVersionIdsDefinedInClass(TransportVersions.class);

// the highest transport version constant defined
static final TransportVersion LATEST_DEFINED;
static {
LATEST_DEFINED = DEFINED_VERSIONS.getLast();

// see comment on IDS field
// now we're registered all the transport versions, we can clear the map
IDS = null;
}

public static List<TransportVersion> collectAllVersionIdsDefinedInClass(Class<?> cls) {
Map<Integer, String> versionIdFields = new HashMap<>();
List<TransportVersion> definedTransportVersions = new ArrayList<>();
Expand Down Expand Up @@ -472,8 +456,6 @@ public static List<TransportVersion> collectAllVersionIdsDefinedInClass(Class<?>
return List.copyOf(definedTransportVersions);
}

static final IntFunction<String> VERSION_LOOKUP = ReleaseVersions.generateVersionsLookup(TransportVersions.class, LATEST_DEFINED.id());

// no instance
private TransportVersions() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@
*/
public abstract class StreamInput extends InputStream {

private TransportVersion version = TransportVersion.current();
private TransportVersion version;

/**
* The transport version the data is serialized as.
*/
public TransportVersion getTransportVersion() {
if (version == null) {
version = TransportVersion.current();
}
return this.version;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "esql-split-on-big-values",
"ids": [
9116000,
9112001,
8841063
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "esql-topn-timings",
"ids": [
9128000
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
esql-split-on-big-values.json
Copy link
Member

Choose a reason for hiding this comment

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

This manifest file shouldn't be checked in, right? Otherwise this isn't backportable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. I removed this.

ml-inference-azure-ai-studio-rerank-added.json
esql-topn-timings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ml-inference-azure-ai-studio-rerank-added",
"ids": [
9123000
]
}
Loading