|
| 1 | +/* |
| 2 | + * Licensed to ElasticSearch and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. ElasticSearch licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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 | + |
| 20 | +package org.elasticsearch.test.unit.cluster.routing.allocation; |
| 21 | + |
| 22 | +import org.apache.lucene.util.LuceneTestCase; |
| 23 | +import org.elasticsearch.cluster.ClusterState; |
| 24 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 25 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 26 | +import org.elasticsearch.cluster.routing.allocation.AllocationService; |
| 27 | +import org.elasticsearch.common.logging.ESLogger; |
| 28 | +import org.elasticsearch.common.logging.Loggers; |
| 29 | +import org.elasticsearch.test.integration.ElasticsearchTestCase; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.elasticsearch.cluster.ClusterState.newClusterStateBuilder; |
| 33 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.newIndexMetaDataBuilder; |
| 34 | +import static org.elasticsearch.cluster.metadata.MetaData.newMetaDataBuilder; |
| 35 | +import static org.elasticsearch.cluster.node.DiscoveryNodes.newNodesBuilder; |
| 36 | +import static org.elasticsearch.cluster.routing.RoutingBuilders.routingTable; |
| 37 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; |
| 38 | +import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; |
| 39 | +import static org.elasticsearch.test.unit.cluster.routing.allocation.RoutingAllocationTests.newNode; |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | + |
| 42 | +/** |
| 43 | + */ |
| 44 | +public class PreferPrimaryAllocationTests extends ElasticsearchTestCase { |
| 45 | + |
| 46 | + private final ESLogger logger = Loggers.getLogger(PreferPrimaryAllocationTests.class); |
| 47 | + |
| 48 | + @LuceneTestCase.AwaitsFix(bugUrl = "seems like unassigned is cleared so throttling for primaries is not properly working") |
| 49 | + @Test |
| 50 | + public void testPreferPrimaryAllocationOverReplicas() { |
| 51 | + logger.info("create an allocation with 1 initial recoveries"); |
| 52 | + AllocationService strategy = new AllocationService(settingsBuilder() |
| 53 | + .put("cluster.routing.allocation.node_concurrent_recoveries", 1) |
| 54 | + .put("cluster.routing.allocation.node_initial_primaries_recoveries", 1) |
| 55 | + .build()); |
| 56 | + |
| 57 | + logger.info("create several indices with no replicas, and wait till all are allocated"); |
| 58 | + |
| 59 | + MetaData metaData = newMetaDataBuilder() |
| 60 | + .put(newIndexMetaDataBuilder("test1").numberOfShards(10).numberOfReplicas(0)) |
| 61 | + .put(newIndexMetaDataBuilder("test2").numberOfShards(10).numberOfReplicas(0)) |
| 62 | + .build(); |
| 63 | + |
| 64 | + RoutingTable routingTable = routingTable() |
| 65 | + .addAsNew(metaData.index("test1")) |
| 66 | + .addAsNew(metaData.index("test2")) |
| 67 | + .build(); |
| 68 | + |
| 69 | + ClusterState clusterState = newClusterStateBuilder().metaData(metaData).routingTable(routingTable).build(); |
| 70 | + |
| 71 | + logger.info("adding two nodes and performing rerouting till all are allocated"); |
| 72 | + clusterState = newClusterStateBuilder().state(clusterState).nodes(newNodesBuilder().put(newNode("node1")).put(newNode("node2"))).build(); |
| 73 | + routingTable = strategy.reroute(clusterState).routingTable(); |
| 74 | + clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build(); |
| 75 | + |
| 76 | + while (!clusterState.routingNodes().shardsWithState(INITIALIZING).isEmpty()) { |
| 77 | + routingTable = strategy.applyStartedShards(clusterState, clusterState.routingNodes().shardsWithState(INITIALIZING)).routingTable(); |
| 78 | + clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build(); |
| 79 | + } |
| 80 | + |
| 81 | + logger.info("increasing the number of replicas to 1, and perform a reroute (to get the replicas allocation going)"); |
| 82 | + routingTable = RoutingTable.builder().routingTable(routingTable).updateNumberOfReplicas(1).build(); |
| 83 | + metaData = MetaData.newMetaDataBuilder().metaData(clusterState.metaData()).updateNumberOfReplicas(1).build(); |
| 84 | + clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).metaData(metaData).build(); |
| 85 | + |
| 86 | + routingTable = strategy.reroute(clusterState).routingTable(); |
| 87 | + clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build(); |
| 88 | + |
| 89 | + logger.info("2 replicas should be initializing now for the existing indices (we throttle to 1)"); |
| 90 | + assertThat(clusterState.routingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); |
| 91 | + |
| 92 | + logger.info("create a new index"); |
| 93 | + metaData = newMetaDataBuilder().metaData(clusterState.metaData()) |
| 94 | + .put(newIndexMetaDataBuilder("new_index").numberOfShards(4).numberOfReplicas(0)) |
| 95 | + .build(); |
| 96 | + |
| 97 | + routingTable = routingTable().routingTable(clusterState.routingTable()) |
| 98 | + .addAsNew(metaData.index("new_index")) |
| 99 | + .build(); |
| 100 | + |
| 101 | + clusterState = newClusterStateBuilder().state(clusterState).metaData(metaData).routingTable(routingTable).build(); |
| 102 | + |
| 103 | + logger.info("reroute, verify that primaries for the new index primary shards are allocated"); |
| 104 | + routingTable = strategy.reroute(clusterState).routingTable(); |
| 105 | + clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).build(); |
| 106 | + |
| 107 | + assertThat(clusterState.routingTable().index("new_index").shardsWithState(INITIALIZING).size(), equalTo(2)); |
| 108 | + } |
| 109 | +} |
0 commit comments