Search in sources :

Example 46 with ShardRouting

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

the class ActiveShardCountTests method startLessThanWaitOnShards.

private ClusterState startLessThanWaitOnShards(final ClusterState clusterState, final String indexName, final int numShardsToStart) {
    RoutingTable routingTable = clusterState.routingTable();
    IndexRoutingTable indexRoutingTable = routingTable.index(indexName);
    IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        assert shardRoutingTable.getSize() > 2;
        int numToStart = numShardsToStart;
        // want less than half, and primary is already started
        for (ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary()) {
                assertTrue(shardRouting.active());
            } else {
                if (numToStart > 0) {
                    shardRouting = shardRouting.initialize(randomAsciiOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted();
                    numToStart--;
                }
            }
            newIndexRoutingTable.addShard(shardRouting);
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    return ClusterState.builder(clusterState).routingTable(routingTable).build();
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 47 with ShardRouting

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

the class ActiveShardCountTests method startAllShards.

private ClusterState startAllShards(final ClusterState clusterState, final String indexName) {
    RoutingTable routingTable = clusterState.routingTable();
    IndexRoutingTable indexRoutingTable = routingTable.index(indexName);
    IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary()) {
                assertTrue(shardRouting.active());
            } else {
                if (shardRouting.active() == false) {
                    shardRouting = shardRouting.initialize(randomAsciiOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted();
                }
            }
            newIndexRoutingTable.addShard(shardRouting);
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    return ClusterState.builder(clusterState).routingTable(routingTable).build();
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 48 with ShardRouting

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

the class ShardFailedClusterStateTaskExecutorTests method createNonExistentShards.

private List<ShardStateAction.ShardEntry> createNonExistentShards(ClusterState currentState, String reason) {
    // add shards from a non-existent index
    String nonExistentIndexUUID = "non-existent";
    Index index = new Index("non-existent", nonExistentIndexUUID);
    List<String> nodeIds = new ArrayList<>();
    for (ObjectCursor<String> nodeId : currentState.nodes().getNodes().keys()) {
        nodeIds.add(nodeId.toString());
    }
    List<ShardRouting> nonExistentShards = new ArrayList<>();
    nonExistentShards.add(nonExistentShardRouting(index, nodeIds, true));
    for (int i = 0; i < numberOfReplicas; i++) {
        nonExistentShards.add(nonExistentShardRouting(index, nodeIds, false));
    }
    List<ShardStateAction.ShardEntry> existingShards = createExistingShards(currentState, reason);
    List<ShardStateAction.ShardEntry> shardsWithMismatchedAllocationIds = new ArrayList<>();
    for (ShardStateAction.ShardEntry existingShard : existingShards) {
        shardsWithMismatchedAllocationIds.add(new ShardStateAction.ShardEntry(existingShard.shardId, UUIDs.randomBase64UUID(), 0L, existingShard.message, existingShard.failure));
    }
    List<ShardStateAction.ShardEntry> tasks = new ArrayList<>();
    nonExistentShards.forEach(shard -> tasks.add(new ShardStateAction.ShardEntry(shard.shardId(), shard.allocationId().getId(), 0L, reason, new CorruptIndexException("simulated", nonExistentIndexUUID))));
    tasks.addAll(shardsWithMismatchedAllocationIds);
    return tasks;
}
Also used : ArrayList(java.util.ArrayList) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting)

Example 49 with ShardRouting

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

the class ShardStateActionTests method testNoLongerPrimaryShardException.

public void testNoLongerPrimaryShardException() throws InterruptedException {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    ShardRouting failedShard = getRandomShardRouting(index);
    AtomicReference<Throwable> failure = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    long primaryTerm = clusterService.state().metaData().index(index).primaryTerm(failedShard.id());
    assertThat(primaryTerm, greaterThanOrEqualTo(1L));
    shardStateAction.remoteShardFailed(failedShard.shardId(), failedShard.allocationId().getId(), primaryTerm + 1, "test", getSimulatedFailure(), new ShardStateAction.Listener() {

        @Override
        public void onSuccess() {
            failure.set(null);
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            failure.set(e);
            latch.countDown();
        }
    });
    ShardStateAction.NoLongerPrimaryShardException catastrophicError = new ShardStateAction.NoLongerPrimaryShardException(failedShard.shardId(), "dummy failure");
    CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    transport.handleRemoteError(capturedRequests[0].requestId, catastrophicError);
    latch.await();
    assertNotNull(failure.get());
    assertThat(failure.get(), instanceOf(ShardStateAction.NoLongerPrimaryShardException.class));
    assertThat(failure.get().getMessage(), equalTo(catastrophicError.getMessage()));
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) NotMasterException(org.elasticsearch.cluster.NotMasterException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) TransportException(org.elasticsearch.transport.TransportException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 50 with ShardRouting

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

the class ShardStateActionTests method testUnhandledFailure.

public void testUnhandledFailure() {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    AtomicBoolean failure = new AtomicBoolean();
    ShardRouting failedShard = getRandomShardRouting(index);
    shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ShardStateAction.Listener() {

        @Override
        public void onSuccess() {
            failure.set(false);
            assert false;
        }

        @Override
        public void onFailure(Exception e) {
            failure.set(true);
        }
    });
    final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(1));
    assertFalse(failure.get());
    transport.handleRemoteError(capturedRequests[0].requestId, new TransportException("simulated"));
    assertTrue(failure.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)252 ClusterState (org.elasticsearch.cluster.ClusterState)119 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)71 ShardId (org.elasticsearch.index.shard.ShardId)60 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)59 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)56 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)52 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)51 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)46 MetaData (org.elasticsearch.cluster.metadata.MetaData)45 Index (org.elasticsearch.index.Index)36 Settings (org.elasticsearch.common.settings.Settings)32 ArrayList (java.util.ArrayList)29 HashSet (java.util.HashSet)29 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)29 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)28 IOException (java.io.IOException)27 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)26 HashMap (java.util.HashMap)25 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)25