|
21 | 21 | import org.elasticsearch.Version; |
22 | 22 | import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest; |
23 | 23 | import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 24 | +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; |
24 | 25 | import org.elasticsearch.action.support.ActiveShardCount; |
25 | 26 | import org.elasticsearch.action.support.replication.ClusterStateCreationUtils; |
26 | 27 | import org.elasticsearch.cluster.ClusterState; |
|
32 | 33 | import org.elasticsearch.common.settings.Settings; |
33 | 34 | import org.elasticsearch.indices.cluster.ClusterStateChanges; |
34 | 35 | import org.elasticsearch.test.ESTestCase; |
| 36 | +import org.elasticsearch.test.VersionUtils; |
35 | 37 | import org.elasticsearch.threadpool.TestThreadPool; |
36 | 38 | import org.elasticsearch.threadpool.ThreadPool; |
37 | 39 |
|
|
46 | 48 |
|
47 | 49 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS; |
48 | 50 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
| 51 | +import static org.hamcrest.Matchers.equalTo; |
49 | 52 | import static org.hamcrest.Matchers.everyItem; |
50 | 53 | import static org.hamcrest.Matchers.isIn; |
51 | 54 |
|
@@ -104,12 +107,15 @@ public void testInvalidValues() { |
104 | 107 |
|
105 | 108 | private static final AtomicInteger nodeIdGenerator = new AtomicInteger(); |
106 | 109 |
|
107 | | - protected DiscoveryNode createNode(DiscoveryNodeRole... mustHaveRoles) { |
| 110 | + protected DiscoveryNode createNode(Version version, DiscoveryNodeRole... mustHaveRoles) { |
108 | 111 | Set<DiscoveryNodeRole> roles = new HashSet<>(randomSubsetOf(DiscoveryNodeRole.BUILT_IN_ROLES)); |
109 | 112 | Collections.addAll(roles, mustHaveRoles); |
110 | 113 | final String id = String.format(Locale.ROOT, "node_%03d", nodeIdGenerator.incrementAndGet()); |
111 | | - return new DiscoveryNode(id, id, buildNewFakeTransportAddress(), Collections.emptyMap(), roles, |
112 | | - Version.CURRENT); |
| 114 | + return new DiscoveryNode(id, id, buildNewFakeTransportAddress(), Collections.emptyMap(), roles, version); |
| 115 | + } |
| 116 | + |
| 117 | + protected DiscoveryNode createNode(DiscoveryNodeRole... mustHaveRoles) { |
| 118 | + return createNode(Version.CURRENT, mustHaveRoles); |
113 | 119 | } |
114 | 120 |
|
115 | 121 | /** |
@@ -200,4 +206,56 @@ public void testAutoExpandWhenNodeLeavesAndPossiblyRejoins() throws InterruptedE |
200 | 206 | terminate(threadPool); |
201 | 207 | } |
202 | 208 | } |
| 209 | + |
| 210 | + public void testOnlyAutoExpandAllocationFilteringAfterAllNodesUpgraded() { |
| 211 | + final ThreadPool threadPool = new TestThreadPool(getClass().getName()); |
| 212 | + final ClusterStateChanges cluster = new ClusterStateChanges(xContentRegistry(), threadPool); |
| 213 | + |
| 214 | + try { |
| 215 | + List<DiscoveryNode> allNodes = new ArrayList<>(); |
| 216 | + DiscoveryNode oldNode = createNode(VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_1), |
| 217 | + DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE); // local node is the master |
| 218 | + allNodes.add(oldNode); |
| 219 | + ClusterState state = ClusterStateCreationUtils.state(oldNode, oldNode, allNodes.toArray(new DiscoveryNode[0])); |
| 220 | + |
| 221 | + CreateIndexRequest request = new CreateIndexRequest("index", |
| 222 | + Settings.builder() |
| 223 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 224 | + .put(SETTING_AUTO_EXPAND_REPLICAS, "0-all").build()) |
| 225 | + .waitForActiveShards(ActiveShardCount.NONE); |
| 226 | + state = cluster.createIndex(state, request); |
| 227 | + assertTrue(state.metaData().hasIndex("index")); |
| 228 | + while (state.routingTable().index("index").shard(0).allShardsStarted() == false) { |
| 229 | + logger.info(state); |
| 230 | + state = cluster.applyStartedShards(state, |
| 231 | + state.routingTable().index("index").shard(0).shardsWithState(ShardRoutingState.INITIALIZING)); |
| 232 | + state = cluster.reroute(state, new ClusterRerouteRequest()); |
| 233 | + } |
| 234 | + |
| 235 | + DiscoveryNode newNode = createNode(Version.V_7_6_0, |
| 236 | + DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE); // local node is the master |
| 237 | + |
| 238 | + state = cluster.addNodes(state, Collections.singletonList(newNode)); |
| 239 | + |
| 240 | + // use allocation filtering |
| 241 | + state = cluster.updateSettings(state, new UpdateSettingsRequest("index").settings(Settings.builder() |
| 242 | + .put(IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_PREFIX + "._name", oldNode.getName()).build())); |
| 243 | + |
| 244 | + while (state.routingTable().index("index").shard(0).allShardsStarted() == false) { |
| 245 | + logger.info(state); |
| 246 | + state = cluster.applyStartedShards(state, |
| 247 | + state.routingTable().index("index").shard(0).shardsWithState(ShardRoutingState.INITIALIZING)); |
| 248 | + state = cluster.reroute(state, new ClusterRerouteRequest()); |
| 249 | + } |
| 250 | + |
| 251 | + // check that presence of old node means that auto-expansion does not take allocation filtering into account |
| 252 | + assertThat(state.routingTable().index("index").shard(0).size(), equalTo(2)); |
| 253 | + |
| 254 | + // remove old node and check that auto-expansion takes allocation filtering into account |
| 255 | + state = cluster.removeNodes(state, Collections.singletonList(oldNode)); |
| 256 | + assertThat(state.routingTable().index("index").shard(0).size(), equalTo(1)); |
| 257 | + } finally { |
| 258 | + terminate(threadPool); |
| 259 | + } |
| 260 | + } |
203 | 261 | } |
0 commit comments