|
20 | 20 | package org.elasticsearch.discovery.zen; |
21 | 21 |
|
22 | 22 | import java.io.Closeable; |
| 23 | +import java.io.IOException; |
23 | 24 | import java.util.ArrayList; |
24 | 25 | import java.util.Arrays; |
25 | 26 | import java.util.Collections; |
| 27 | +import java.util.EnumSet; |
26 | 28 | import java.util.HashSet; |
27 | 29 | import java.util.List; |
28 | 30 | import java.util.Set; |
29 | 31 | import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.atomic.AtomicBoolean; |
30 | 33 | import java.util.stream.Collectors; |
31 | 34 |
|
32 | 35 | import org.apache.lucene.util.IOUtils; |
|
35 | 38 | import org.elasticsearch.cluster.ClusterChangedEvent; |
36 | 39 | import org.elasticsearch.cluster.ClusterName; |
37 | 40 | import org.elasticsearch.cluster.ClusterState; |
| 41 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 42 | +import org.elasticsearch.cluster.metadata.MetaData; |
38 | 43 | import org.elasticsearch.cluster.node.DiscoveryNode; |
39 | 44 | import org.elasticsearch.cluster.node.DiscoveryNode.Role; |
40 | 45 | import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 46 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 47 | +import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
| 48 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 49 | +import org.elasticsearch.cluster.routing.ShardRoutingState; |
| 50 | +import org.elasticsearch.cluster.routing.TestShardRouting; |
| 51 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
41 | 52 | import org.elasticsearch.cluster.service.ClusterService; |
42 | 53 | import org.elasticsearch.common.settings.ClusterSettings; |
43 | 54 | import org.elasticsearch.common.settings.Settings; |
44 | 55 | import org.elasticsearch.discovery.Discovery; |
45 | 56 | import org.elasticsearch.discovery.zen.PublishClusterStateActionTests.AssertingAckListener; |
| 57 | +import org.elasticsearch.index.shard.ShardId; |
46 | 58 | import org.elasticsearch.test.ESTestCase; |
| 59 | +import org.elasticsearch.test.VersionUtils; |
47 | 60 | import org.elasticsearch.test.transport.MockTransportService; |
48 | 61 | import org.elasticsearch.threadpool.TestThreadPool; |
49 | 62 | import org.elasticsearch.threadpool.ThreadPool; |
| 63 | +import org.elasticsearch.transport.TransportChannel; |
| 64 | +import org.elasticsearch.transport.TransportResponse; |
| 65 | +import org.elasticsearch.transport.TransportResponseOptions; |
50 | 66 | import org.elasticsearch.transport.TransportService; |
51 | 67 |
|
52 | 68 | import static java.util.Collections.emptyMap; |
53 | 69 | import static java.util.Collections.emptySet; |
| 70 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_CREATION_DATE; |
| 71 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; |
| 72 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
| 73 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED; |
| 74 | +import static org.elasticsearch.cluster.routing.RoutingTableTests.updateActiveAllocations; |
54 | 75 | import static org.elasticsearch.discovery.zen.ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING; |
55 | 76 | import static org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState; |
56 | 77 | import static org.elasticsearch.test.ClusterServiceUtils.createClusterService; |
@@ -283,4 +304,77 @@ private Set<DiscoveryNode> fdNodesForState(ClusterState clusterState, DiscoveryN |
283 | 304 | }); |
284 | 305 | return discoveryNodes; |
285 | 306 | } |
| 307 | + |
| 308 | + public void testValidateOnUnsupportedIndexVersionCreated() throws Exception { |
| 309 | + ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.DEFAULT); |
| 310 | + final DiscoveryNode otherNode = new DiscoveryNode("other_node", buildNewFakeTransportAddress(), emptyMap(), |
| 311 | + EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT); |
| 312 | + MembershipAction.ValidateJoinRequestRequestHandler request = new MembershipAction.ValidateJoinRequestRequestHandler(); |
| 313 | + final boolean closed = randomBoolean(); |
| 314 | + IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(Settings.builder() |
| 315 | + .put(SETTING_VERSION_CREATED, VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion())) |
| 316 | + .put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 317 | + .put(SETTING_CREATION_DATE, System.currentTimeMillis())) |
| 318 | + .state(closed ? IndexMetaData.State.CLOSE : IndexMetaData.State.OPEN) |
| 319 | + .build(); |
| 320 | + IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex()); |
| 321 | + RoutingTable.Builder routing = new RoutingTable.Builder(); |
| 322 | + routing.addAsNew(indexMetaData); |
| 323 | + final ShardId shardId = new ShardId("test", "_na_", 0); |
| 324 | + IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId); |
| 325 | + |
| 326 | + final DiscoveryNode primaryNode = otherNode; |
| 327 | + indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, primaryNode.getId(), null, true, |
| 328 | + ShardRoutingState.INITIALIZING, new UnassignedInfo(UnassignedInfo.Reason.INDEX_REOPENED, "getting there"))); |
| 329 | + indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build()); |
| 330 | + IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build(); |
| 331 | + IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData); |
| 332 | + stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()) |
| 333 | + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()); |
| 334 | + if (closed == false) { |
| 335 | + IllegalStateException ex = expectThrows(IllegalStateException.class, () -> |
| 336 | + request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), null)); |
| 337 | + assertEquals("index [test] version not supported: " |
| 338 | + + VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()), ex.getMessage()); |
| 339 | + } else { |
| 340 | + AtomicBoolean sendResponse = new AtomicBoolean(false); |
| 341 | + request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), new TransportChannel() { |
| 342 | + @Override |
| 343 | + public String action() { |
| 344 | + return null; |
| 345 | + } |
| 346 | + |
| 347 | + @Override |
| 348 | + public String getProfileName() { |
| 349 | + return null; |
| 350 | + } |
| 351 | + |
| 352 | + @Override |
| 353 | + public long getRequestId() { |
| 354 | + return 0; |
| 355 | + } |
| 356 | + |
| 357 | + @Override |
| 358 | + public String getChannelType() { |
| 359 | + return null; |
| 360 | + } |
| 361 | + |
| 362 | + @Override |
| 363 | + public void sendResponse(TransportResponse response) throws IOException { |
| 364 | + sendResponse.set(true); |
| 365 | + } |
| 366 | + |
| 367 | + @Override |
| 368 | + public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException { |
| 369 | + |
| 370 | + } |
| 371 | + |
| 372 | + @Override |
| 373 | + public void sendResponse(Exception exception) throws IOException { |
| 374 | + |
| 375 | + } |
| 376 | + }); |
| 377 | + assertTrue(sendResponse.get()); |
| 378 | + } |
| 379 | + } |
286 | 380 | } |
0 commit comments