Skip to content

Commit ae09836

Browse files
jimcziafoucret
authored andcommitted
BWC Handling for ModelRegistryMetadata (elastic#125301)
ModelRegistryMetadata has now been backported to 8.19 via elastic#125150. This update ensures that we properly differentiate between nodes running 8.19.x (which supports the new custom metadata) and 9.0.x (which does not). To achieve this, this PR introduces a new `supportsVersion(TransportVersion)` method for `NamedWriteable` and `NamedDiff`, allowing subclasses to customize their backward compatibility behavior.
1 parent 8fd6bd1 commit ae09836

File tree

8 files changed

+63
-23
lines changed

8 files changed

+63
-23
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ static TransportVersion def(int id) {
150150
public static final TransportVersion ML_INFERENCE_DEEPSEEK_8_19 = def(8_841_0_09);
151151
public static final TransportVersion ESQL_SERIALIZE_BLOCK_TYPE_CODE_8_19 = def(8_841_0_10);
152152
public static final TransportVersion ESQL_FAILURE_FROM_REMOTE_8_19 = def(8_841_0_11);
153+
public static final TransportVersion ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL_8_19 = def(8_841_0_12);
154+
public static final TransportVersion INFERENCE_MODEL_REGISTRY_METADATA_8_19 = def(8_841_0_13);
153155
public static final TransportVersion INITIAL_ELASTICSEARCH_9_0 = def(9_000_0_00);
154156
public static final TransportVersion REMOVE_SNAPSHOT_FAILURES_90 = def(9_000_0_01);
155157
public static final TransportVersion TRANSPORT_STATS_HANDLING_TIME_REQUIRED_90 = def(9_000_0_02);

server/src/main/java/org/elasticsearch/cluster/NamedDiff.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@
1717
*/
1818
public interface NamedDiff<T extends Diffable<T>> extends Diff<T>, NamedWriteable {
1919
/**
20-
* The minimal version of the recipient this custom object can be sent to
20+
* The minimal version of the recipient this object can be sent to.
21+
* See {@link #supportsVersion(TransportVersion)} for the default serialization check.
2122
*/
2223
TransportVersion getMinimalSupportedVersion();
2324

25+
/**
26+
* Determines whether this instance should be serialized based on the provided transport version.
27+
*
28+
* The default implementation returns {@code true} if the given transport version is
29+
* equal to or newer than {@link #getMinimalSupportedVersion()}.
30+
* Subclasses may override this method to define custom serialization logic.
31+
*
32+
* @param version the transport version of the receiving node
33+
* @return {@code true} if the instance should be serialized, {@code false} otherwise
34+
*/
35+
default boolean supportsVersion(TransportVersion version) {
36+
return version.onOrAfter(getMinimalSupportedVersion());
37+
}
38+
2439
}

server/src/main/java/org/elasticsearch/cluster/NamedDiffableValueSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public T read(StreamInput in, String key) throws IOException {
3232

3333
@Override
3434
public boolean supportsVersion(Diff<T> value, TransportVersion version) {
35-
return version.onOrAfter(((NamedDiff<?>) value).getMinimalSupportedVersion());
35+
return ((NamedDiff<?>) value).supportsVersion(version);
3636
}
3737

3838
@Override
3939
public boolean supportsVersion(T value, TransportVersion version) {
40-
return version.onOrAfter(value.getMinimalSupportedVersion());
40+
return value.supportsVersion(version);
4141
}
4242

4343
@SuppressWarnings("unchecked")

server/src/main/java/org/elasticsearch/common/io/stream/VersionedNamedWriteable.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@ public interface VersionedNamedWriteable extends NamedWriteable {
2525
String getWriteableName();
2626

2727
/**
28-
* The minimal version of the recipient this object can be sent to
28+
* The minimal version of the recipient this object can be sent to.
29+
* See {@link #supportsVersion(TransportVersion)} for the default serialization check.
2930
*/
3031
TransportVersion getMinimalSupportedVersion();
3132

3233
/**
33-
* Tests whether or not the custom should be serialized. The criteria is the output stream must be at least the minimum supported
34-
* version of the custom. That is, we only serialize customs to clients than can understand the custom based on the version of the
35-
* client.
34+
* Determines whether this instance should be serialized based on the provided transport version.
3635
*
37-
* @param out the output stream
38-
* @param custom the custom to serialize
39-
* @param <T> the type of the custom
40-
* @return true if the custom should be serialized and false otherwise
36+
* The default implementation returns {@code true} if the given transport version is
37+
* equal to or newer than {@link #getMinimalSupportedVersion()}.
38+
* Subclasses may override this method to define custom serialization logic.
39+
*
40+
* @param version the transport version of the receiving node
41+
* @return {@code true} if the instance should be serialized, {@code false} otherwise
4142
*/
42-
static <T extends VersionedNamedWriteable> boolean shouldSerialize(final StreamOutput out, final T custom) {
43-
return out.getTransportVersion().onOrAfter(custom.getMinimalSupportedVersion());
43+
default boolean supportsVersion(TransportVersion version) {
44+
return version.onOrAfter(getMinimalSupportedVersion());
4445
}
4546

4647
/**
47-
* Writes all those values in the given map to {@code out} that pass the version check in {@link #shouldSerialize} as a list.
48+
* Writes all those values in the given map to {@code out} that pass the version check in
49+
* {@link VersionedNamedWriteable#supportsVersion} as a list.
4850
*
4951
* @param out stream to write to
5052
* @param customs map of customs
@@ -58,13 +60,13 @@ static void writeVersionedWriteables(StreamOutput out, Iterable<? extends Versio
5860
// filter out objects not supported by the stream version
5961
int numberOfCompatibleValues = 0;
6062
for (var value : writeables) {
61-
if (shouldSerialize(out, value)) {
63+
if (value.supportsVersion(out.getTransportVersion())) {
6264
numberOfCompatibleValues++;
6365
}
6466
}
6567
out.writeVInt(numberOfCompatibleValues);
6668
for (var value : writeables) {
67-
if (shouldSerialize(out, value)) {
69+
if (value.supportsVersion(out.getTransportVersion())) {
6870
out.writeNamedWriteable(value);
6971
}
7072
}

server/src/main/java/org/elasticsearch/inference/MinimalServiceSettings.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.IOException;
2727
import java.util.Objects;
2828

29+
import static org.elasticsearch.TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA;
30+
import static org.elasticsearch.TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA_8_19;
2931
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.ElementType;
3032
import static org.elasticsearch.inference.TaskType.CHAT_COMPLETION;
3133
import static org.elasticsearch.inference.TaskType.COMPLETION;
@@ -161,7 +163,12 @@ public String getWriteableName() {
161163

162164
@Override
163165
public TransportVersion getMinimalSupportedVersion() {
164-
return TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA;
166+
return TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA_8_19;
167+
}
168+
169+
@Override
170+
public boolean supportsVersion(TransportVersion version) {
171+
return version.isPatchFrom(INFERENCE_MODEL_REGISTRY_METADATA_8_19) || version.onOrAfter(INFERENCE_MODEL_REGISTRY_METADATA);
165172
}
166173

167174
@Override

server/src/main/java/org/elasticsearch/persistent/PersistentTasks.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.cluster.metadata.ProjectId;
1717
import org.elasticsearch.common.collect.Iterators;
1818
import org.elasticsearch.common.io.stream.StreamOutput;
19-
import org.elasticsearch.common.io.stream.VersionedNamedWriteable;
2019
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
2120
import org.elasticsearch.core.Nullable;
2221
import org.elasticsearch.core.Tuple;
@@ -102,7 +101,7 @@ default long getNumberOfTasksOnNode(String nodeId, String taskName) {
102101
default void doWriteTo(StreamOutput out) throws IOException {
103102
out.writeLong(getLastAllocationId());
104103
Map<String, PersistentTask<?>> filteredTasks = tasks().stream()
105-
.filter(t -> VersionedNamedWriteable.shouldSerialize(out, t.getParams()))
104+
.filter(t -> t.getParams().supportsVersion(out.getTransportVersion()))
106105
.collect(Collectors.toMap(PersistentTask::getId, Function.identity()));
107106
out.writeMap(filteredTasks, StreamOutput::writeWriteable);
108107
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/OperatorStatus.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.elasticsearch.common.Strings;
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.common.io.stream.StreamOutput;
13-
import org.elasticsearch.common.io.stream.VersionedNamedWriteable;
1413
import org.elasticsearch.common.io.stream.Writeable;
1514
import org.elasticsearch.core.Nullable;
1615
import org.elasticsearch.xcontent.ToXContentObject;
@@ -33,7 +32,7 @@ public static OperatorStatus readFrom(StreamInput in) throws IOException {
3332
@Override
3433
public void writeTo(StreamOutput out) throws IOException {
3534
out.writeString(operator);
36-
out.writeOptionalNamedWriteable(status != null && VersionedNamedWriteable.shouldSerialize(out, status) ? status : null);
35+
out.writeOptionalNamedWriteable(status != null && status.supportsVersion(out.getTransportVersion()) ? status : null);
3736
}
3837

3938
@Override

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/registry/ModelRegistryMetadata.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Objects;
3939
import java.util.Set;
4040

41+
import static org.elasticsearch.TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA;
42+
import static org.elasticsearch.TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA_8_19;
4143
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
4244
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
4345

@@ -234,7 +236,12 @@ public String getWriteableName() {
234236

235237
@Override
236238
public TransportVersion getMinimalSupportedVersion() {
237-
return TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA;
239+
return INFERENCE_MODEL_REGISTRY_METADATA_8_19;
240+
}
241+
242+
@Override
243+
public boolean supportsVersion(TransportVersion version) {
244+
return shouldSerialize(version);
238245
}
239246

240247
@Override
@@ -300,7 +307,12 @@ public String getWriteableName() {
300307

301308
@Override
302309
public TransportVersion getMinimalSupportedVersion() {
303-
return TransportVersions.INFERENCE_MODEL_REGISTRY_METADATA;
310+
return INFERENCE_MODEL_REGISTRY_METADATA_8_19;
311+
}
312+
313+
@Override
314+
public boolean supportsVersion(TransportVersion version) {
315+
return shouldSerialize(version);
304316
}
305317

306318
@Override
@@ -313,4 +325,8 @@ public Metadata.ProjectCustom apply(Metadata.ProjectCustom part) {
313325
}
314326
}
315327
}
328+
329+
static boolean shouldSerialize(TransportVersion version) {
330+
return version.isPatchFrom(INFERENCE_MODEL_REGISTRY_METADATA_8_19) || version.onOrAfter(INFERENCE_MODEL_REGISTRY_METADATA);
331+
}
316332
}

0 commit comments

Comments
 (0)