Skip to content

Commit bdb1cab

Browse files
authored
Prevent nodes from joining if newer indices exist in the cluster (#23843)
Today we prevent nodes from joining when indices exists that are too old. Yet, the opposite can happen too since lucene / elasticsearch is not forward compatible when it gets to indices we won't let nodes join the cluster once there are indices in the clusterstate that are newer than the nodes version. This prevents forward compatibility issues which we never test against. Yet, this will not prevent rolling restarts or anything like this since indices are always created with the minimum node version in the cluster such that an index can only get the version of the higher nodes once all nodes are upgraded to this version.
1 parent 998eeb7 commit bdb1cab

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

core/src/main/java/org/elasticsearch/discovery/zen/MembershipAction.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,28 @@ static class ValidateJoinRequestRequestHandler implements TransportRequestHandle
178178

179179
@Override
180180
public void messageReceived(ValidateJoinRequest request, TransportChannel channel) throws Exception {
181-
ensureIndexCompatibility(Version.CURRENT.minimumIndexCompatibilityVersion(), request.state.getMetaData());
181+
ensureIndexCompatibility(Version.CURRENT, request.state.getMetaData());
182182
// for now, the mere fact that we can serialize the cluster state acts as validation....
183183
channel.sendResponse(TransportResponse.Empty.INSTANCE);
184184
}
185185
}
186186

187187
/**
188-
* Ensures that all indices are compatible with the supported index version.
188+
* Ensures that all indices are compatible with the given node version. This will ensure that all indices in the given metadata
189+
* will not be created with a newer version of elasticsearch as well as that all indices are newer or equal to the minimum index
190+
* compatibility version.
191+
* @see Version#minimumIndexCompatibilityVersion()
189192
* @throws IllegalStateException if any index is incompatible with the given version
190193
*/
191-
static void ensureIndexCompatibility(final Version supportedIndexVersion, MetaData metaData) {
194+
static void ensureIndexCompatibility(final Version nodeVersion, MetaData metaData) {
195+
Version supportedIndexVersion = nodeVersion.minimumIndexCompatibilityVersion();
192196
// we ensure that all indices in the cluster we join are compatible with us no matter if they are
193197
// closed or not we can't read mappings of these indices so we need to reject the join...
194198
for (IndexMetaData idxMetaData : metaData) {
199+
if (idxMetaData.getCreationVersion().after(nodeVersion)) {
200+
throw new IllegalStateException("index " + idxMetaData.getIndex() + " version not supported: "
201+
+ idxMetaData.getCreationVersion() + " the node version is: " + nodeVersion);
202+
}
195203
if (idxMetaData.getCreationVersion().before(supportedIndexVersion)) {
196204
throw new IllegalStateException("index " + idxMetaData.getIndex() + " version not supported: "
197205
+ idxMetaData.getCreationVersion() + " minimum compatible index version is: " + supportedIndexVersion);

core/src/main/java/org/elasticsearch/discovery/zen/NodeJoinController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public ClusterTasksResult<DiscoveryNode> execute(ClusterState currentState, List
453453
}
454454
// we do this validation quite late to prevent race conditions between nodes joining and importing dangling indices
455455
// we have to reject nodes that don't support all indices we have in this cluster
456-
MembershipAction.ensureIndexCompatibility(minNodeVersion.minimumIndexCompatibilityVersion(), currentState.getMetaData());
456+
MembershipAction.ensureIndexCompatibility(minNodeVersion, currentState.getMetaData());
457457
if (nodesChanged) {
458458
newState.nodes(nodesBuilder);
459459
return results.build(allocationService.reroute(newState.build(), "node_join"));

core/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ void handleJoinRequest(final DiscoveryNode node, final ClusterState state, final
839839
} else {
840840
// we do this in a couple of places including the cluster update thread. This one here is really just best effort
841841
// to ensure we fail as fast as possible.
842-
MembershipAction.ensureIndexCompatibility(node.getVersion().minimumIndexCompatibilityVersion(), state.getMetaData());
842+
MembershipAction.ensureIndexCompatibility(node.getVersion(), state.getMetaData());
843843
// try and connect to the node, if it fails, we can raise an exception back to the client...
844844
transportService.connectToNode(node);
845845

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.discovery.zen;
20+
21+
import org.elasticsearch.Version;
22+
import org.elasticsearch.cluster.metadata.IndexMetaData;
23+
import org.elasticsearch.cluster.metadata.MetaData;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.test.ESTestCase;
26+
import org.elasticsearch.test.VersionUtils;
27+
28+
public class MembershipActionTests extends ESTestCase {
29+
30+
public void testPreventJoinClusterWithNewerIndices() {
31+
Settings.builder().build();
32+
MetaData.Builder metaBuilder = MetaData.builder();
33+
IndexMetaData indexMetaData = IndexMetaData.builder("test")
34+
.settings(settings(Version.CURRENT))
35+
.numberOfShards(1)
36+
.numberOfReplicas(1).build();
37+
metaBuilder.put(indexMetaData, false);
38+
MetaData metaData = metaBuilder.build();
39+
MembershipAction.ensureIndexCompatibility(Version.CURRENT, metaData);
40+
41+
expectThrows(IllegalStateException.class, () ->
42+
MembershipAction.ensureIndexCompatibility(VersionUtils.getPreviousVersion(Version.CURRENT),
43+
metaData));
44+
}
45+
46+
public void testPreventJoinClusterWithUnsupportedIndices() {
47+
Settings.builder().build();
48+
MetaData.Builder metaBuilder = MetaData.builder();
49+
IndexMetaData indexMetaData = IndexMetaData.builder("test")
50+
.settings(settings(VersionUtils.getPreviousVersion(Version.CURRENT
51+
.minimumIndexCompatibilityVersion())))
52+
.numberOfShards(1)
53+
.numberOfReplicas(1).build();
54+
metaBuilder.put(indexMetaData, false);
55+
MetaData metaData = metaBuilder.build();
56+
expectThrows(IllegalStateException.class, () ->
57+
MembershipAction.ensureIndexCompatibility(Version.CURRENT,
58+
metaData));
59+
}
60+
61+
public void testSuccess() {
62+
Settings.builder().build();
63+
MetaData.Builder metaBuilder = MetaData.builder();
64+
IndexMetaData indexMetaData = IndexMetaData.builder("test")
65+
.settings(settings(VersionUtils.randomVersionBetween(random(),
66+
Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
67+
.numberOfShards(1)
68+
.numberOfReplicas(1).build();
69+
metaBuilder.put(indexMetaData, false);
70+
indexMetaData = IndexMetaData.builder("test1")
71+
.settings(settings(VersionUtils.randomVersionBetween(random(),
72+
Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)))
73+
.numberOfShards(1)
74+
.numberOfReplicas(1).build();
75+
metaBuilder.put(indexMetaData, false);
76+
MetaData metaData = metaBuilder.build();
77+
MembershipAction.ensureIndexCompatibility(Version.CURRENT,
78+
metaData);
79+
}
80+
}

0 commit comments

Comments
 (0)