use of org.elasticsearch.cluster.routing.GroupShardsIterator in project elasticsearch by elastic.
the class RoutingIteratorTests method testShardsAndPreferNodeRouting.
public void testShardsAndPreferNodeRouting() {
AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build());
MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)).build();
RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).localNodeId("node1")).build();
clusterState = strategy.reroute(clusterState, "reroute");
clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
OperationRouting operationRouting = new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
GroupShardsIterator shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:0");
assertThat(shardIterators.size(), equalTo(1));
assertThat(shardIterators.iterator().next().shardId().id(), equalTo(0));
shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:1");
assertThat(shardIterators.size(), equalTo(1));
assertThat(shardIterators.iterator().next().shardId().id(), equalTo(1));
//check node preference, first without preference to see they switch
shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:0|");
assertThat(shardIterators.size(), equalTo(1));
assertThat(shardIterators.iterator().next().shardId().id(), equalTo(0));
String firstRoundNodeId = shardIterators.iterator().next().nextOrNull().currentNodeId();
shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:0");
assertThat(shardIterators.size(), equalTo(1));
assertThat(shardIterators.iterator().next().shardId().id(), equalTo(0));
assertThat(shardIterators.iterator().next().nextOrNull().currentNodeId(), not(equalTo(firstRoundNodeId)));
shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:0|_prefer_nodes:node1");
assertThat(shardIterators.size(), equalTo(1));
assertThat(shardIterators.iterator().next().shardId().id(), equalTo(0));
assertThat(shardIterators.iterator().next().nextOrNull().currentNodeId(), equalTo("node1"));
shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_shards:0|_prefer_nodes:node1,node2");
assertThat(shardIterators.size(), equalTo(1));
Iterator<ShardIterator> iterator = shardIterators.iterator();
final ShardIterator it = iterator.next();
assertThat(it.shardId().id(), equalTo(0));
final String firstNodeId = it.nextOrNull().currentNodeId();
assertThat(firstNodeId, anyOf(equalTo("node1"), equalTo("node2")));
if ("node1".equals(firstNodeId)) {
assertThat(it.nextOrNull().currentNodeId(), equalTo("node2"));
} else {
assertThat(it.nextOrNull().currentNodeId(), equalTo("node1"));
}
}
Aggregations