Skip to content

Commit

Permalink
Use extension method to collect all versions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-ivanov-es committed Nov 28, 2024
1 parent bf4fc20 commit f9a87e2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
20 changes: 18 additions & 2 deletions server/src/main/java/org/elasticsearch/TransportVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

import org.elasticsearch.core.Assertions;
import org.elasticsearch.core.UpdateForV9;
import org.elasticsearch.internal.VersionExtension;
import org.elasticsearch.plugins.ExtensionLoader;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
Expand Down Expand Up @@ -279,7 +282,7 @@ static TransportVersion def(int id) {
*/
public static final TransportVersion MINIMUM_CCS_VERSION = V_8_15_0;

static final NavigableMap<Integer, TransportVersion> VERSION_IDS = getAllVersionIds(TransportVersions.class);
static final NavigableMap<Integer, TransportVersion> VERSION_IDS = getAllVersionIds();

// the highest transport version constant defined in this file, used as a fallback for TransportVersion.current()
static final TransportVersion LATEST_DEFINED;
Expand All @@ -291,7 +294,20 @@ static TransportVersion def(int id) {
IDS = null;
}

public static NavigableMap<Integer, TransportVersion> getAllVersionIds(Class<?> cls) {
public static NavigableMap<Integer, TransportVersion> getAllVersionIds() {
NavigableMap<Integer, TransportVersion> transportVersions = collectAllVersionIdsDefinedInClass(TransportVersions.class);
Collection<TransportVersion> extendedVersions = ExtensionLoader.loadSingleton(ServiceLoader.load(VersionExtension.class))
.map(VersionExtension::getTransportVersions)
.orElse(Collections.emptyList());

for (TransportVersion extendedVersion : extendedVersions) {
transportVersions.put(extendedVersion.id(), extendedVersion);
}

return Collections.unmodifiableNavigableMap(transportVersions);
}

public static NavigableMap<Integer, TransportVersion> collectAllVersionIdsDefinedInClass(Class<?> cls) {
Map<Integer, String> versionIdFields = new HashMap<>();
NavigableMap<Integer, TransportVersion> builder = new TreeMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.elasticsearch.TransportVersion;
import org.elasticsearch.index.IndexVersion;

import java.util.Collection;
import java.util.Collections;

/**
* Allows plugging in current version elements.
*/
Expand All @@ -24,6 +27,13 @@ public interface VersionExtension {
*/
TransportVersion getCurrentTransportVersion(TransportVersion fallback);

/**
* Returns collection of {@link TransportVersion} defined by extension
*/
default Collection<TransportVersion> getTransportVersions() {
return Collections.emptyList();
}

/**
* Returns the {@link IndexVersion} that Elasticsearch should use.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static class DuplicatedIdFakeVersion {

public void testStaticTransportVersionChecks() {
assertThat(
TransportVersions.getAllVersionIds(CorrectFakeVersion.class),
TransportVersions.collectAllVersionIdsDefinedInClass(CorrectFakeVersion.class),
equalTo(
Map.of(
199,
Expand All @@ -83,7 +83,10 @@ public void testStaticTransportVersionChecks() {
)
)
);
AssertionError e = expectThrows(AssertionError.class, () -> TransportVersions.getAllVersionIds(DuplicatedIdFakeVersion.class));
AssertionError e = expectThrows(
AssertionError.class,
() -> TransportVersions.collectAllVersionIdsDefinedInClass(DuplicatedIdFakeVersion.class)
);
assertThat(e.getMessage(), containsString("have the same version number"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ public static TransportVersion randomVersionBetween(
}
int maxVersionIndex = ALL_VERSIONS.size() - 1;
if (maxVersion != null) {
// current version might be serverless one and therefore might be after the last version in ALL_VERSIONS
if (maxVersion.after(ALL_VERSIONS.getLast()) && maxVersion.onOrBefore(TransportVersion.current())) {
maxVersionIndex = ALL_VERSIONS.size() - 1;
} else {
maxVersionIndex = Collections.binarySearch(ALL_VERSIONS, maxVersion);
}
maxVersionIndex = Collections.binarySearch(ALL_VERSIONS, maxVersion);
}
if (minVersionIndex < 0) {
throw new IllegalArgumentException("minVersion [" + minVersion + "] does not exist.");
Expand Down

0 comments on commit f9a87e2

Please sign in to comment.