|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch; |
| 11 | + |
| 12 | +import org.elasticsearch.common.VersionId; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.internal.VersionExtension; |
| 16 | +import org.elasticsearch.plugins.ExtensionLoader; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.ServiceLoader; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents the version of the wire protocol used to communicate between a pair of ES nodes. |
| 30 | + * <p> |
| 31 | + * Prior to 8.8.0, the release {@link Version} was used everywhere. This class separates the wire protocol version from the release version. |
| 32 | + * <p> |
| 33 | + * Each transport version constant has an id number, which for versions prior to 8.9.0 is the same as the release version for backwards |
| 34 | + * compatibility. In 8.9.0 this is changed to an incrementing number, disconnected from the release version. |
| 35 | + * <p> |
| 36 | + * Each version constant has a unique id string. This is not actually used in the binary protocol, but is there to ensure each protocol |
| 37 | + * version is only added to the source file once. This string needs to be unique (normally a UUID, but can be any other unique nonempty |
| 38 | + * string). If two concurrent PRs add the same transport version, the different unique ids cause a git conflict, ensuring that the second PR |
| 39 | + * to be merged must be updated with the next free version first. Without the unique id string, git will happily merge the two versions |
| 40 | + * together, resulting in the same transport version being used across multiple commits, causing problems when you try to upgrade between |
| 41 | + * those two merged commits. |
| 42 | + * |
| 43 | + * <h2>Version compatibility</h2> |
| 44 | + * The earliest compatible version is hardcoded in the {@link TransportVersions#MINIMUM_COMPATIBLE} field. Previously, this was dynamically |
| 45 | + * calculated from the major/minor versions of {@link Version}, but {@code TransportVersion} does not have separate major/minor version |
| 46 | + * numbers. So the minimum compatible version is hard-coded as the transport version used by the highest minor release of the previous |
| 47 | + * major version. {@link TransportVersions#MINIMUM_COMPATIBLE} should be updated appropriately whenever a major release happens. |
| 48 | + * <p> |
| 49 | + * The earliest CCS compatible version is hardcoded at {@link TransportVersions#MINIMUM_CCS_VERSION}, as the transport version used by the |
| 50 | + * previous minor release. This should be updated appropriately whenever a minor release happens. |
| 51 | + * |
| 52 | + * <h2>Scope of usefulness of {@link TransportVersion}</h2> |
| 53 | + * {@link TransportVersion} is a property of the transport connection between a pair of nodes, and should not be used as an indication of |
| 54 | + * the version of any single node. The {@link TransportVersion} of a connection is negotiated between the nodes via some logic that is not |
| 55 | + * totally trivial, and may change in future. Any other places that might make decisions based on this version effectively have to reproduce |
| 56 | + * this negotiation logic, which would be fragile. If you need to make decisions based on the version of a single node, do so using a |
| 57 | + * different version value. If you need to know whether the cluster as a whole speaks a new enough {@link TransportVersion} to understand a |
| 58 | + * newly-added feature, use {@link org.elasticsearch.cluster.ClusterState#getMinTransportVersion}. |
| 59 | + */ |
| 60 | +public record TransportVersion(int id) implements VersionId<TransportVersion> { |
| 61 | + |
| 62 | + public static TransportVersion readVersion(StreamInput in) throws IOException { |
| 63 | + return fromId(in.readVInt()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Finds a {@code TransportVersion} by its id. |
| 68 | + * If a transport version with the specified ID does not exist, |
| 69 | + * this method creates and returns a new instance of {@code TransportVersion} with the specified ID. |
| 70 | + * The new instance is not registered in {@code TransportVersion.getAllVersions}. |
| 71 | + */ |
| 72 | + public static TransportVersion fromId(int id) { |
| 73 | + TransportVersion known = VersionsHolder.ALL_VERSIONS_MAP.get(id); |
| 74 | + if (known != null) { |
| 75 | + return known; |
| 76 | + } |
| 77 | + // this is a version we don't otherwise know about - just create a placeholder |
| 78 | + return new TransportVersion(id); |
| 79 | + } |
| 80 | + |
| 81 | + public static void writeVersion(TransportVersion version, StreamOutput out) throws IOException { |
| 82 | + out.writeVInt(version.id); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the minimum version of {@code version1} and {@code version2} |
| 87 | + */ |
| 88 | + public static TransportVersion min(TransportVersion version1, TransportVersion version2) { |
| 89 | + return version1.id < version2.id ? version1 : version2; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the maximum version of {@code version1} and {@code version2} |
| 94 | + */ |
| 95 | + public static TransportVersion max(TransportVersion version1, TransportVersion version2) { |
| 96 | + return version1.id > version2.id ? version1 : version2; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns {@code true} if the specified version is compatible with this running version of Elasticsearch. |
| 101 | + */ |
| 102 | + public static boolean isCompatible(TransportVersion version) { |
| 103 | + return version.onOrAfter(TransportVersions.MINIMUM_COMPATIBLE); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Reference to the most recent transport version. |
| 108 | + * This should be the transport version with the highest id. |
| 109 | + */ |
| 110 | + public static TransportVersion current() { |
| 111 | + return VersionsHolder.CURRENT; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sorted list of all defined transport versions |
| 116 | + */ |
| 117 | + public static List<TransportVersion> getAllVersions() { |
| 118 | + return VersionsHolder.ALL_VERSIONS; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return whether this is a known {@link TransportVersion}, i.e. one declared in {@link TransportVersions}. Other versions may exist |
| 123 | + * in the wild (they're sent over the wire by numeric ID) but we don't know how to communicate using such versions. |
| 124 | + */ |
| 125 | + public boolean isKnown() { |
| 126 | + return VersionsHolder.ALL_VERSIONS_MAP.containsKey(id); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return the newest known {@link TransportVersion} which is no older than this instance. Returns {@link TransportVersions#ZERO} if |
| 131 | + * there are no such versions. |
| 132 | + */ |
| 133 | + public TransportVersion bestKnownVersion() { |
| 134 | + if (isKnown()) { |
| 135 | + return this; |
| 136 | + } |
| 137 | + TransportVersion bestSoFar = TransportVersions.ZERO; |
| 138 | + for (final var knownVersion : VersionsHolder.ALL_VERSIONS_MAP.values()) { |
| 139 | + if (knownVersion.after(bestSoFar) && knownVersion.before(this)) { |
| 140 | + bestSoFar = knownVersion; |
| 141 | + } |
| 142 | + } |
| 143 | + return bestSoFar; |
| 144 | + } |
| 145 | + |
| 146 | + public static TransportVersion fromString(String str) { |
| 147 | + return TransportVersion.fromId(Integer.parseInt(str)); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns {@code true} if this version is a patch version at or after {@code version}. |
| 152 | + * <p> |
| 153 | + * This should not be used normally. It is used for matching patch versions of the same base version, |
| 154 | + * using the standard version number format specified in {@link TransportVersions}. |
| 155 | + * When a patch version of an existing transport version is created, {@code transportVersion.isPatchFrom(patchVersion)} |
| 156 | + * will match any transport version at or above {@code patchVersion} that is also of the same base version. |
| 157 | + * <p> |
| 158 | + * For example, {@code version.isPatchFrom(8_800_0_04)} will return the following for the given {@code version}: |
| 159 | + * <ul> |
| 160 | + * <li>{@code 8_799_0_00.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 161 | + * <li>{@code 8_799_0_09.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 162 | + * <li>{@code 8_800_0_00.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 163 | + * <li>{@code 8_800_0_03.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 164 | + * <li>{@code 8_800_0_04.isPatchFrom(8_800_0_04)}: {@code true}</li> |
| 165 | + * <li>{@code 8_800_0_49.isPatchFrom(8_800_0_04)}: {@code true}</li> |
| 166 | + * <li>{@code 8_800_1_00.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 167 | + * <li>{@code 8_801_0_00.isPatchFrom(8_800_0_04)}: {@code false}</li> |
| 168 | + * </ul> |
| 169 | + */ |
| 170 | + public boolean isPatchFrom(TransportVersion version) { |
| 171 | + return onOrAfter(version) && id < version.id + 100 - (version.id % 100); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns a string representing the Elasticsearch release version of this transport version, |
| 176 | + * if applicable for this deployment, otherwise the raw version number. |
| 177 | + */ |
| 178 | + public String toReleaseVersion() { |
| 179 | + return TransportVersions.VERSION_LOOKUP.apply(id); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public String toString() { |
| 184 | + return Integer.toString(id); |
| 185 | + } |
| 186 | + |
| 187 | + private static class VersionsHolder { |
| 188 | + private static final List<TransportVersion> ALL_VERSIONS; |
| 189 | + private static final Map<Integer, TransportVersion> ALL_VERSIONS_MAP; |
| 190 | + private static final TransportVersion CURRENT; |
| 191 | + |
| 192 | + static { |
| 193 | + Collection<TransportVersion> extendedVersions = ExtensionLoader.loadSingleton(ServiceLoader.load(VersionExtension.class)) |
| 194 | + .map(VersionExtension::getTransportVersions) |
| 195 | + .orElse(Collections.emptyList()); |
| 196 | + |
| 197 | + if (extendedVersions.isEmpty()) { |
| 198 | + ALL_VERSIONS = TransportVersions.DEFINED_VERSIONS; |
| 199 | + } else { |
| 200 | + ALL_VERSIONS = Stream.concat(TransportVersions.DEFINED_VERSIONS.stream(), extendedVersions.stream()).sorted().toList(); |
| 201 | + } |
| 202 | + |
| 203 | + ALL_VERSIONS_MAP = ALL_VERSIONS.stream().collect(Collectors.toUnmodifiableMap(TransportVersion::id, Function.identity())); |
| 204 | + |
| 205 | + CURRENT = ALL_VERSIONS.getLast(); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments