11Versioning Elasticsearch
22========================
33
4- Elasticsearch is a complicated product, and is run in many different scenarios.
4+ Elasticsearch is a complicated product and is run in many different scenarios.
55A single version number is not sufficient to cover the whole of the product,
66instead we need different concepts to provide versioning capabilities
77for different aspects of Elasticsearch, depending on their scope, updatability,
@@ -13,16 +13,16 @@ This is the version number used for published releases of Elasticsearch,
1313and the Elastic stack. This takes the form _ major.minor.patch_ ,
1414with a corresponding version id.
1515
16- Uses of this version number should be avoided, as it does not apply to
17- some scenarios, and use of release version will break Elasticsearch nodes.
16+ Use of this version number should be avoided, as it does not apply to
17+ some scenarios, and use of a release version will break Elasticsearch nodes.
1818
1919The release version is accessible in code through ` Build.current().version() ` ,
2020but it ** should not** be assumed that this is a semantic version number,
2121it could be any arbitrary string.
2222
2323## Transport protocol
2424
25- The transport protocol is used to send binary data between Elasticsearch nodes;
25+ The transport protocol is used to send binary data between Elasticsearch nodes; the
2626` TransportVersion ` is the version number used for this protocol.
2727This version number is negotiated between each pair of nodes in the cluster
2828on first connection, and is set as the lower of the highest transport version
@@ -31,78 +31,113 @@ This version is then accessible through the `getTransportVersion` method
3131on ` StreamInput ` and ` StreamOutput ` , so serialization code can read/write
3232objects in a form that will be understood by the other node.
3333
34- Every change to the transport protocol is represented by a new transport version,
35- higher than all previous transport versions, which then becomes the highest version
36- recognized by that build of Elasticsearch. The version ids are stored
37- as constants in the ` TransportVersions ` class .
34+ Internally, every change to the transport protocol is represented by a new transport version, higher than all previous
35+ transport versions, which then becomes the highest version recognized by that build of Elasticsearch. The version ids
36+ are stored
37+ in state files in the ` transport/ ` resources directory .
3838Each id has a standard pattern ` M_NNN_S_PP ` , where:
39+
3940* ` M ` is the major version
4041* ` NNN ` is an incrementing id
4142* ` S ` is used in subsidiary repos amending the default transport protocol
4243* ` PP ` is used for patches and backports
4344
44- When you make a change to the serialization form of any object,
45- you need to create a new sequential constant in ` TransportVersions ` ,
46- introduced in the same PR that adds the change, that increments
47- the ` NNN ` component from the previous highest version,
48- with other components set to zero.
49- For example, if the previous version number is ` 8_413_0_01 ` ,
50- the next version number should be ` 8_414_0_00 ` .
51-
52- Once you have defined your constant, you then need to use it
53- in serialization code. If the transport version is at or above the new id,
54- the modified protocol should be used:
55-
56- str = in.readString();
57- bool = in.readBoolean();
58- if (in.getTransportVersion().onOrAfter(TransportVersions.NEW_CONSTANT)) {
59- num = in.readVInt();
45+ * For every change to the transport protocol (serialization of any object), we need to create a new transport version
46+ _ in the same PR that adds the change_ .
47+
48+ [ // ] : # ( TODO description of state files, patch version, and latest upper bounds )
49+
50+ _ Elastic developers_ - please see corresponding documentation for Serverless
51+ on creating transport versions for Serverless changes.
52+
53+ ### Creating transport versions
54+
55+ We use gradle tooling to manage the creation of transport versions state files. To create a new transport version, first
56+ declare a reference anywhere in the java code. The ` fromName ` arg (called the "name") must be a string literal, composed
57+ only of characters that are alphanumeric or underscores:
58+
59+ private static final TransportVersion MY_NEW_TV = TransportVersion.fromName("my_new_tv");
60+
61+ Next, you need to create the transport version state stored in the resource files, including the creation of a new
62+ transport version id. This is done by running the ` :generateTransportVersion ` gradle task. The generation task can
63+ automatically detect the name by checking if a state file is missing for a given constant. e.g.:
64+
65+ ./gradlew :generateTransportVersion
66+
67+ Once you have declared your constant and generated its state, you can then use it to gate serialization code. e.g.:
68+
69+ if (in.getTransportVersion().supports(MY_NEW_TV)) {
70+ // new serialization code
6071 }
6172
62- If a transport version change needs to be reverted, a ** new** version constant
63- should be added representing the revert, and the version id checks
64- adjusted appropriately to only use the modified protocol between the version id
65- the change was added, and the new version id used for the revert (exclusive).
66- The ` between ` method can be used for this.
73+ Note that the generation task is idempotent. This means that if you have already generated a transport version file, and
74+ you run the task again, it will not create a duplicate transport version id. If you remove backports (described below)
75+ from a subsequent run, the backport will be removed from the state files. It is important to note that only one
76+ transport version may be generated for a given PR, though it can be referenced in multiple places in the code by using
77+ the same name.
78+
79+ ### Reverting changes
80+
81+ #### On main
6782
6883Once a transport change with a new version has been merged into main or a release branch,
6984it ** must not** be modified - this is so the meaning of that specific
7085transport version does not change.
7186
72- _ Elastic developers_ - please see corresponding documentation for Serverless
73- on creating transport versions for Serverless changes.
87+ If a transport version change on main needs to be reverted, a ** new** version constant
88+ should be added representing the revert. The version id checks should be
89+ adjusted to only use the modified protocol between the version id
90+ the change was added, and the new version id used for the revert (exclusive).
91+
92+ TransportVersion inTV = in.getTransportVersion();
93+ if (inTV.supports(MY_ORIGINAL_TV) && inTV.supports(MY_REVERT_TV) == false) {
94+ // old serialization code
95+ } else if (inTV.supports(MY_REVERT_TV)) {
96+ // new serialization code
97+ }
98+
99+ #### On a PR
100+
101+ A transport version change in a PR that has not yet been merged can be easily reverted by simply removing the code that
102+ references the transport version (` TransportVersion.fromName("my_new_tv"); ` ), then rerunning the generation task with no
103+ args. Because this command is idempotent, it will detect that the transport version constant is no longer referenced,
104+ and remove the corresponding state file. E.g.:
105+
106+ ./gradlew :generateTransportVersion
107+
108+ ### Backporting transport versions
109+
110+ To prevent race conditions, all transport version ids are first created on the ` main ` branch, after which they can be
111+ backported to prior release branches.
74112
75- ### Collapsing transport versions
76-
77- As each change adds a new constant, the list of constants in ` TransportVersions `
78- will keep growing. However, once there has been an official release of Elasticsearch,
79- that includes that change, that specific transport version is no longer needed,
80- apart from constants that happen to be used for release builds.
81- As part of managing transport versions, consecutive transport versions can be
82- periodically collapsed together into those that are only used for release builds.
83- This task is normally performed by Core/Infra on a semi-regular basis,
84- usually after each new minor release, to collapse the transport versions
85- for the previous minor release. An example of such an operation can be found
86- [ here] ( https://github.com/elastic/elasticsearch/pull/104937 ) .
87-
88- #### Tips
89-
90- - We collapse versions only on the ` main ` branch.
91- - Use [ TransportVersions.csv] ( ../../server/src/main/resources/org/elasticsearch/TransportVersions.csv ) as your guide.
92- - For each release version listed in that file with a corresponding ` INITIAL_ELASTICSEARCH_ ` entry corresponding to that version,
93- - change the prefix to ` V_ `
94- - replace all intervening entries since the previous patch ` V_ ` entry with the new ` V_ ` entry
95- - look at all uses of the new ` V_ ` entry and look for dead code that can be deleted
96-
97- For example, if there's a version ` 1.2.3 ` in the CSV file,
98- and ` TransportVersions.java ` has an entry called ` INITIAL_ELASTICSEARCH_1_2_3 ` ,
99- then:
100- - rename it to ` V_1_2_3 ` ,
101- - replace any other intervening symbols between ` V_1_2_2 ` and ` V_1_2_3 ` with ` V_1_2_3 ` itself, and
102- - look through all the uses of the new ` V_1_2_3 ` symbol; if any contain dead code, simplify it.
103-
104- When in doubt, you can always leave the code as-is.
105- This is an optional cleanup step that is never required for correctness.
113+ Backport state files can either be generated on the command line by gradle, or by adding a gihub version label to your
114+ PR.
115+
116+ If you have already generated a transport version file, gradle will not be able to automatically detect which transport
117+ version you want to create backport ids for, so you will need to specify the name as an arg. E.g.:
118+
119+ ./gradlew :generateTransportVersion --name=my_new_tv --backport-branches=9.1,9.0
120+
121+ If you later decide that you'd like to remove a backport, you can rerun the above command without the branch you want to
122+ remove. e.g. to remove the 9.0 backport:
123+
124+ ./gradlew :generateTransportVersion --name=my_new_tv --backport-branches=9.1
125+
126+ You can also remove the label on github, and this will trigger CI to remove the backport.
127+
128+ It is also safe to create backports for pre-existing transport versions on main, just be sure to include the code
129+ changes in the same PR to the prior release branch (these must be part of a single atomic commit). E.g.:
130+
131+ ./gradlew :generateTransportVersion --name=my_preexisting_tv --backport-branches=9.1,9.0
132+
133+ #### Merge conflicts
134+
135+ By design, a merge conflict will occur when two different PRs try to create the same transport version id (e.g. because
136+ they were both based off the same prior id in main). This will produce a merge conflict in a state file in the
137+ ` transport/upper_bounds/ ` resource directory. To resolve the conflict, rerun the full generation command, and it will
138+ regenerate the state files, including the new transport version id. E.g.:
139+
140+ ./gradlew :generateTransportVersion --name=my_new_tv
106141
107142### Minimum compatibility versions
108143
@@ -133,54 +168,6 @@ is updated automatically as part of performing a release.
133168In releases that do not have a release version number, that method becomes
134169a no-op.
135170
136- ### Managing patches and backports
137-
138- Backporting transport version changes to previous releases
139- should only be done if absolutely necessary, as it is very easy to get wrong
140- and break the release in a way that is very hard to recover from.
141-
142- If we consider the version number as an incrementing line, what we are doing is
143- grafting a change that takes effect at a certain point in the line,
144- to additionally take effect in a fixed window earlier in the line.
145-
146- To take an example, using indicative version numbers, when the latest
147- transport version is 52, we decide we need to backport a change done in
148- transport version 50 to transport version 45. We use the ` P ` version id component
149- to create version 45.1 with the backported change.
150- This change will apply for version ids 45.1 to 45.9 (should they exist in the future).
151-
152- The serialization code in the backport needs to use the backported protocol
153- for all version numbers 45.1 to 45.9. The ` TransportVersion.isPatchFrom ` method
154- can be used to easily determine if this is the case: ` streamVersion.isPatchFrom(45.1) ` .
155- However, the ` onOrAfter ` also does what is needed on patch branches.
156-
157- The serialization code in version 53 then needs to additionally check
158- version numbers 45.1-45.9 to use the backported protocol, also using the ` isPatchFrom ` method.
159-
160- As an example, [ this transport change] ( https://github.com/elastic/elasticsearch/pull/107862 )
161- was backported from 8.15 to [ 8.14.0] ( https://github.com/elastic/elasticsearch/pull/108251 )
162- and [ 8.13.4] ( https://github.com/elastic/elasticsearch/pull/108250 ) at the same time
163- (8.14 was a build candidate at the time).
164-
165- The 8.13 PR has:
166-
167- if (transportVersion.onOrAfter(8.13_backport_id))
168-
169- The 8.14 PR has:
170-
171- if (transportVersion.isPatchFrom(8.13_backport_id)
172- || transportVersion.onOrAfter(8.14_backport_id))
173-
174- The 8.15 PR has:
175-
176- if (transportVersion.isPatchFrom(8.13_backport_id)
177- || transportVersion.isPatchFrom(8.14_backport_id)
178- || transportVersion.onOrAfter(8.15_transport_id))
179-
180- In particular, if you are backporting a change to a patch release,
181- you also need to make sure that any subsequent released version on any branch
182- also has that change, and knows about the patch backport ids and what they mean.
183-
184171## Index version
185172
186173Index version is a single incrementing version number for the index data format,
0 commit comments