Skip to content

Commit

Permalink
Determine latest index version dynamically (#99599)
Browse files Browse the repository at this point in the history
This commit reduces some of the boilerplate needed for defining new
IndexVersion constants. Previously defining a new constant meant both
adding the constant and updating the definition of CURRENT. With this
change the CURRENT is automatically figured out. It utilizes the already
existing sorted map that we have for finding duplicate index version
definitions.
  • Loading branch information
rjernst authored Sep 18, 2023
1 parent ee39083 commit 91e4662
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions server/src/main/java/org/elasticsearch/index/IndexVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,17 @@ private static IndexVersion registerIndexVersion(int id, Version luceneVersion,
*/

private static class CurrentHolder {
private static final IndexVersion CURRENT = findCurrent(V_8_500_001);
private static final IndexVersion CURRENT = findCurrent();

// finds the pluggable current version, or uses the given fallback
private static IndexVersion findCurrent(IndexVersion fallback) {
private static IndexVersion findCurrent() {
var versionExtension = ExtensionLoader.loadSingleton(ServiceLoader.load(VersionExtension.class), () -> null);
if (versionExtension == null) {
return fallback;
return LATEST_DEFINED;
}
var version = versionExtension.getCurrentIndexVersion();

assert version.onOrAfter(fallback);
assert version.onOrAfter(LATEST_DEFINED);
assert version.luceneVersion.equals(Version.LATEST)
: "IndexVersion must be upgraded to ["
+ Version.LATEST
Expand All @@ -175,7 +175,12 @@ private static IndexVersion findCurrent(IndexVersion fallback) {

public static final IndexVersion MINIMUM_COMPATIBLE = V_7_0_0;

private static final NavigableMap<Integer, IndexVersion> VERSION_IDS = getAllVersionIds(IndexVersion.class);

static final IndexVersion LATEST_DEFINED;
static {
LATEST_DEFINED = VERSION_IDS.lastEntry().getValue();

// see comment on IDS field
// now we're registered the index versions, we can clear the map
IDS = null;
Expand Down Expand Up @@ -220,12 +225,6 @@ static NavigableMap<Integer, IndexVersion> getAllVersionIds(Class<?> cls) {
return Collections.unmodifiableNavigableMap(builder);
}

private static final NavigableMap<Integer, IndexVersion> VERSION_IDS;

static {
VERSION_IDS = getAllVersionIds(IndexVersion.class);
}

static Collection<IndexVersion> getAllVersions() {
return VERSION_IDS.values();
}
Expand Down

0 comments on commit 91e4662

Please sign in to comment.