Skip to content

Commit 077a11e

Browse files
authored
Lazily initialize current transport version (#96852)
The CURRENT transport version may be plugged in via SPI. Unfortunately since it is a static constant, this requires loading the SPI during clinit of TransportVersion, which precludes the SPI classes from actually referring to TransportVersion (or say, having their own TransportVersion constant) without causing an initalization deadlock. This commit delays initalization of CURRENT by moving it into a static inner class. Since the visibility is private, and the only use of the class is within the current() method, initialization of that class should be delayed until the first call to current().
1 parent 3b5ccef commit 077a11e

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* Each transport version should only be used in a single merged commit (apart from BwC versions copied from {@link Version}).
5454
* <p>
5555
* To add a new transport version, add a new constant at the bottom of the list that is one greater than the current highest version,
56-
* ensure it has a unique id, and update the {@link #CURRENT} constant to point to the new version.
56+
* ensure it has a unique id, and update the {@link CurrentHolder#CURRENT} constant to point to the new version.
5757
* <h2>Reverting a transport version</h2>
5858
* If you revert a commit with a transport version change, you <em>must</em> ensure there is a <em>new</em> transport version
5959
* representing the reverted change. <em>Do not</em> let the transport version go backwards, it must <em>always</em> be incremented.
@@ -136,7 +136,18 @@ private static TransportVersion registerTransportVersion(int id, String uniqueId
136136
public static final TransportVersion V_8_500_010 = registerTransportVersion(8_500_010, "9818C628-1EEC-439B-B943-468F61460675");
137137
public static final TransportVersion V_8_500_011 = registerTransportVersion(8_500_011, "2209F28D-B52E-4BC4-9889-E780F291C32E");
138138

139-
private static final TransportVersion CURRENT = findCurrent(V_8_500_011);
139+
private static class CurrentHolder {
140+
private static final TransportVersion CURRENT = findCurrent(V_8_500_011);
141+
142+
// finds the pluggable current version, or uses the given fallback
143+
private static TransportVersion findCurrent(TransportVersion fallback) {
144+
var versionExtension = VersionExtension.load();
145+
if (versionExtension == null) {
146+
return fallback;
147+
}
148+
return new TransportVersion(versionExtension.getCurrentTransportVersionId());
149+
}
150+
}
140151

141152
/**
142153
* Reference to the earliest compatible transport version to this version of the codebase.
@@ -244,7 +255,7 @@ public static boolean isCompatible(TransportVersion version) {
244255
* This should be the transport version with the highest id.
245256
*/
246257
public static TransportVersion current() {
247-
return CURRENT;
258+
return CurrentHolder.CURRENT;
248259
}
249260

250261
public boolean after(TransportVersion version) {
@@ -272,15 +283,6 @@ public static TransportVersion fromString(String str) {
272283
return TransportVersion.fromId(Integer.parseInt(str));
273284
}
274285

275-
// finds the pluggable current version, or uses the given fallback
276-
private static TransportVersion findCurrent(TransportVersion fallback) {
277-
var versionExtension = VersionExtension.load();
278-
if (versionExtension == null) {
279-
return fallback;
280-
}
281-
return new TransportVersion(versionExtension.getCurrentTransportVersionId());
282-
}
283-
284286
@Override
285287
public int compareTo(TransportVersion other) {
286288
return Integer.compare(this.id, other.id);

0 commit comments

Comments
 (0)