Search in sources :

Example 1 with ShardRouting

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

the class ClusterAllocationExplainActionTests method testFindPrimaryShardToExplain.

public void testFindPrimaryShardToExplain() {
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), randomFrom(ShardRoutingState.values()));
    ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, true, null);
    ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
    assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 2 with ShardRouting

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

the class ClusterAllocationExplainActionTests method testFindAnyUnassignedShardToExplain.

public void testFindAnyUnassignedShardToExplain() {
    // find unassigned primary
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.UNASSIGNED);
    ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest();
    ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
    assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
    // find unassigned replica
    clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
    request = new ClusterAllocationExplainRequest();
    shard = findShardToExplain(request, routingAllocation(clusterState));
    assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0), shard);
    // no unassigned shard to explain
    final ClusterState allStartedClusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED);
    final ClusterAllocationExplainRequest anyUnassignedShardsRequest = new ClusterAllocationExplainRequest();
    expectThrows(IllegalStateException.class, () -> findShardToExplain(anyUnassignedShardsRequest, routingAllocation(allStartedClusterState)));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 3 with ShardRouting

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

the class ClusterAllocationExplainActionTests method testFindAnyReplicaToExplain.

public void testFindAnyReplicaToExplain() {
    // prefer unassigned replicas to started replicas
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
    ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, false, null);
    ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
    assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::unassigned).findFirst().get(), shard);
    // prefer started replicas to initializing/relocating replicas
    clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, randomFrom(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING), ShardRoutingState.STARTED);
    request = new ClusterAllocationExplainRequest("idx", 0, false, null);
    shard = findShardToExplain(request, routingAllocation(clusterState));
    assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(), shard);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 4 with ShardRouting

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

the class ClusterAllocationExplainActionTests method testFindShardAssignedToNode.

public void testFindShardAssignedToNode() {
    // find shard with given node
    final boolean primary = randomBoolean();
    ShardRoutingState[] replicaStates = new ShardRoutingState[0];
    if (primary == false) {
        replicaStates = new ShardRoutingState[] { ShardRoutingState.STARTED };
    }
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, replicaStates);
    ShardRouting shardToExplain = primary ? clusterState.getRoutingTable().index("idx").shard(0).primaryShard() : clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0);
    ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, primary, shardToExplain.currentNodeId());
    RoutingAllocation allocation = routingAllocation(clusterState);
    ShardRouting foundShard = findShardToExplain(request, allocation);
    assertEquals(shardToExplain, foundShard);
    // shard is not assigned to given node
    String explainNode = null;
    for (RoutingNode routingNode : clusterState.getRoutingNodes()) {
        if (routingNode.nodeId().equals(shardToExplain.currentNodeId()) == false) {
            explainNode = routingNode.nodeId();
            break;
        }
    }
    final ClusterAllocationExplainRequest failingRequest = new ClusterAllocationExplainRequest("idx", 0, primary, explainNode);
    expectThrows(IllegalStateException.class, () -> findShardToExplain(failingRequest, allocation));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Example 5 with ShardRouting

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

the class TransportReplicationActionTests method testReplicaProxy.

public void testReplicaProxy() throws InterruptedException, ExecutionException {
    ReplicationOperation.Replicas proxy = action.newReplicasProxy();
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2));
    logger.info("using state: {}", state);
    setState(clusterService, state);
    // check that at unknown node fails
    PlainActionFuture<ReplicaResponse> listener = new PlainActionFuture<>();
    proxy.performOn(TestShardRouting.newShardRouting(shardId, "NOT THERE", false, randomFrom(ShardRoutingState.values())), new Request(), listener);
    assertTrue(listener.isDone());
    assertListenerThrows("non existent node should throw a NoNodeAvailableException", listener, NoNodeAvailableException.class);
    final IndexShardRoutingTable shardRoutings = state.routingTable().shardRoutingTable(shardId);
    final ShardRouting replica = randomFrom(shardRoutings.replicaShards().stream().filter(ShardRouting::assignedToNode).collect(Collectors.toList()));
    listener = new PlainActionFuture<>();
    proxy.performOn(replica, new Request(), listener);
    assertFalse(listener.isDone());
    CapturingTransport.CapturedRequest[] captures = transport.getCapturedRequestsAndClear();
    assertThat(captures, arrayWithSize(1));
    if (randomBoolean()) {
        final TransportReplicationAction.ReplicaResponse response = new TransportReplicationAction.ReplicaResponse(randomAsciiOfLength(10), randomLong());
        transport.handleResponse(captures[0].requestId, response);
        assertTrue(listener.isDone());
        assertThat(listener.get(), equalTo(response));
    } else if (randomBoolean()) {
        transport.handleRemoteError(captures[0].requestId, new ElasticsearchException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, ElasticsearchException.class);
    } else {
        transport.handleError(captures[0].requestId, new TransportException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, TransportException.class);
    }
    AtomicReference<Object> failure = new AtomicReference<>();
    AtomicReference<Object> ignoredFailure = new AtomicReference<>();
    AtomicBoolean success = new AtomicBoolean();
    proxy.failShardIfNeeded(replica, randomIntBetween(1, 10), "test", new ElasticsearchException("simulated"), () -> success.set(true), failure::set, ignoredFailure::set);
    CapturingTransport.CapturedRequest[] shardFailedRequests = transport.getCapturedRequestsAndClear();
    // A replication action doesn't not fail the request
    assertEquals(0, shardFailedRequests.length);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ReplicaResponse(org.elasticsearch.action.support.replication.ReplicationOperation.ReplicaResponse) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)372 ClusterState (org.elasticsearch.cluster.ClusterState)162 ShardId (org.elasticsearch.index.shard.ShardId)104 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)92 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)79 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)75 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)75 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)74 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)71 Index (org.elasticsearch.index.Index)50 HashSet (java.util.HashSet)49 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)49 ArrayList (java.util.ArrayList)48 IndexShard (org.elasticsearch.index.shard.IndexShard)48 Settings (org.elasticsearch.common.settings.Settings)46 MetaData (org.elasticsearch.cluster.metadata.MetaData)45 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)44 IOException (java.io.IOException)43 HashMap (java.util.HashMap)41 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)41