Search in sources :

Example 26 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class RoutingIteratorTests method testEmptyIterator.

public void testEmptyIterator() {
    ShardShuffler shuffler = new RotationShardShuffler(0);
    ShardIterator shardIterator = new PlainShardIterator(new ShardId("test1", "_na_", 0), shuffler.shuffle(Collections.<ShardRouting>emptyList()));
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    shardIterator = new PlainShardIterator(new ShardId("test1", "_na_", 0), shuffler.shuffle(Collections.<ShardRouting>emptyList()));
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    shardIterator = new PlainShardIterator(new ShardId("test1", "_na_", 0), shuffler.shuffle(Collections.<ShardRouting>emptyList()));
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    shardIterator = new PlainShardIterator(new ShardId("test1", "_na_", 0), shuffler.shuffle(Collections.<ShardRouting>emptyList()));
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) RotationShardShuffler(org.elasticsearch.cluster.routing.RotationShardShuffler) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) RotationShardShuffler(org.elasticsearch.cluster.routing.RotationShardShuffler) ShardShuffler(org.elasticsearch.cluster.routing.ShardShuffler) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 27 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class RoutingIteratorTests method testAttributePreferenceRouting.

public void testAttributePreferenceRouting() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.awareness.attributes", "rack_id,zone").build());
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).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();
    Map<String, String> node1Attributes = new HashMap<>();
    node1Attributes.put("rack_id", "rack_1");
    node1Attributes.put("zone", "zone1");
    Map<String, String> node2Attributes = new HashMap<>();
    node2Attributes.put("rack_id", "rack_2");
    node2Attributes.put("zone", "zone2");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1", unmodifiableMap(node1Attributes))).add(newNode("node2", unmodifiableMap(node2Attributes))).localNodeId("node1")).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    // after all are started, check routing iteration
    ShardIterator shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(new String[] { "rack_id" }, clusterState.nodes());
    ShardRouting shardRouting = shardIterator.nextOrNull();
    assertThat(shardRouting, notNullValue());
    assertThat(shardRouting.currentNodeId(), equalTo("node1"));
    shardRouting = shardIterator.nextOrNull();
    assertThat(shardRouting, notNullValue());
    assertThat(shardRouting.currentNodeId(), equalTo("node2"));
    shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(new String[] { "rack_id" }, clusterState.nodes());
    shardRouting = shardIterator.nextOrNull();
    assertThat(shardRouting, notNullValue());
    assertThat(shardRouting.currentNodeId(), equalTo("node1"));
    shardRouting = shardIterator.nextOrNull();
    assertThat(shardRouting, notNullValue());
    assertThat(shardRouting.currentNodeId(), equalTo("node2"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) HashMap(java.util.HashMap) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 28 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class RoutingIteratorTests method testReplicaShardPreferenceIters.

public void testReplicaShardPreferenceIters() throws Exception {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build());
    OperationRouting operationRouting = new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(2)).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")).add(newNode("node3")).localNodeId("node1")).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    // When replicas haven't initialized, it comes back with the primary first, then initializing replicas
    GroupShardsIterator shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_replica_first");
    // two potential shards
    assertThat(shardIterators.size(), equalTo(2));
    ShardIterator iter = shardIterators.iterator().next();
    // three potential candidates for the shard
    assertThat(iter.size(), equalTo(3));
    ShardRouting routing = iter.nextOrNull();
    assertNotNull(routing);
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    // replicas haven't initialized yet, so primary is first
    assertTrue(routing.primary());
    assertTrue(routing.started());
    routing = iter.nextOrNull();
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    assertTrue(routing.initializing());
    routing = iter.nextOrNull();
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    assertTrue(routing.initializing());
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_replica");
    // two potential shards
    assertThat(shardIterators.size(), equalTo(2));
    iter = shardIterators.iterator().next();
    // two potential replicas for the shard
    assertThat(iter.size(), equalTo(2));
    routing = iter.nextOrNull();
    assertNotNull(routing);
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    routing = iter.nextOrNull();
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    shardIterators = operationRouting.searchShards(clusterState, new String[] { "test" }, null, "_replica_first");
    // two potential shards
    assertThat(shardIterators.size(), equalTo(2));
    iter = shardIterators.iterator().next();
    // three potential candidates for the shard
    assertThat(iter.size(), equalTo(3));
    routing = iter.nextOrNull();
    assertNotNull(routing);
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    routing = iter.nextOrNull();
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertFalse(routing.primary());
    // finally the primary
    routing = iter.nextOrNull();
    assertThat(routing.shardId().id(), anyOf(equalTo(0), equalTo(1)));
    assertTrue(routing.primary());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) OperationRouting(org.elasticsearch.cluster.routing.OperationRouting) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 29 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class RoutingIteratorTests method testIterator1.

public void testIterator1() {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(2)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test1")).build();
    ShardIterator shardIterator = routingTable.index("test1").shard(0).shardsIt(0);
    assertThat(shardIterator.size(), equalTo(3));
    ShardRouting shardRouting1 = shardIterator.nextOrNull();
    assertThat(shardRouting1, notNullValue());
    assertThat(shardIterator.remaining(), equalTo(2));
    ShardRouting shardRouting2 = shardIterator.nextOrNull();
    assertThat(shardRouting2, notNullValue());
    assertThat(shardIterator.remaining(), equalTo(1));
    assertThat(shardRouting2, not(sameInstance(shardRouting1)));
    ShardRouting shardRouting3 = shardIterator.nextOrNull();
    assertThat(shardRouting3, notNullValue());
    assertThat(shardRouting3, not(sameInstance(shardRouting1)));
    assertThat(shardRouting3, not(sameInstance(shardRouting2)));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
    assertThat(shardIterator.nextOrNull(), nullValue());
    assertThat(shardIterator.remaining(), equalTo(0));
}
Also used : RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 30 with ShardIterator

use of org.elasticsearch.cluster.routing.ShardIterator in project elasticsearch by elastic.

the class TruncateTranslogIT method getTranslogDirs.

private Set<Path> getTranslogDirs(String indexName) throws IOException {
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    GroupShardsIterator shardIterators = state.getRoutingTable().activePrimaryShardsGrouped(new String[] { indexName }, false);
    final Index idx = state.metaData().index(indexName).getIndex();
    List<ShardIterator> iterators = iterableAsArrayList(shardIterators);
    ShardIterator shardIterator = RandomPicks.randomFrom(random(), iterators);
    ShardRouting shardRouting = shardIterator.nextOrNull();
    assertNotNull(shardRouting);
    assertTrue(shardRouting.primary());
    assertTrue(shardRouting.assignedToNode());
    String nodeId = shardRouting.currentNodeId();
    NodesStatsResponse nodeStatses = client().admin().cluster().prepareNodesStats(nodeId).setFs(true).get();
    // treeset makes sure iteration order is deterministic
    Set<Path> translogDirs = new TreeSet<>();
    for (FsInfo.Path fsPath : nodeStatses.getNodes().get(0).getFs()) {
        String path = fsPath.getPath();
        final String relativeDataLocationPath = "indices/" + idx.getUUID() + "/" + Integer.toString(shardRouting.getId()) + "/translog";
        Path translogPath = PathUtils.get(path).resolve(relativeDataLocationPath);
        if (Files.isDirectory(translogPath)) {
            translogDirs.add(translogPath);
        }
    }
    return translogDirs;
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) Index(org.elasticsearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) FsInfo(org.elasticsearch.monitor.fs.FsInfo) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) TreeSet(java.util.TreeSet) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)30 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)22 GroupShardsIterator (org.elasticsearch.cluster.routing.GroupShardsIterator)16 ClusterState (org.elasticsearch.cluster.ClusterState)14 PlainShardIterator (org.elasticsearch.cluster.routing.PlainShardIterator)11 HashMap (java.util.HashMap)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 Index (org.elasticsearch.index.Index)7 ArrayList (java.util.ArrayList)6 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)6 MetaData (org.elasticsearch.cluster.metadata.MetaData)6 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)6 ShardId (org.elasticsearch.index.shard.ShardId)6 AliasFilter (org.elasticsearch.search.internal.AliasFilter)6 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 ActionListener (org.elasticsearch.action.ActionListener)4 Transport (org.elasticsearch.transport.Transport)4 Path (java.nio.file.Path)3