Search in sources :

Example 76 with ShardRouting

use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.

the class ShardFailedClusterStateTaskExecutorTests method createExistingShards.

private List<ShardStateAction.FailedShardEntry> createExistingShards(ClusterState currentState, String reason) {
    List<ShardRouting> shards = new ArrayList<>();
    GroupShardsIterator<ShardIterator> shardGroups = currentState.routingTable().allAssignedShardsGrouped(new String[] { INDEX }, true);
    for (ShardIterator shardIt : shardGroups) {
        for (ShardRouting shard : shardIt) {
            shards.add(shard);
        }
    }
    List<ShardRouting> failures = randomSubsetOf(randomIntBetween(1, 1 + shards.size() / 4), shards.toArray(new ShardRouting[0]));
    String indexUUID = metadata.index(INDEX).getIndexUUID();
    int numberOfTasks = randomIntBetween(failures.size(), 2 * failures.size());
    List<ShardRouting> shardsToFail = new ArrayList<>(numberOfTasks);
    for (int i = 0; i < numberOfTasks; i++) {
        shardsToFail.add(randomFrom(failures));
    }
    return toTasks(currentState, shardsToFail, indexUUID, reason);
}
Also used : ArrayList(java.util.ArrayList) ShardIterator(org.opensearch.cluster.routing.ShardIterator) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 77 with ShardRouting

use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testStartedShards.

public void testStartedShards() throws Exception {
    final String indexName = "test";
    final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING, ShardRoutingState.INITIALIZING);
    final IndexMetadata indexMetadata = clusterState.metadata().index(indexName);
    final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0);
    final long primaryTerm = indexMetadata.primaryTerm(shardId.id());
    final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard();
    final String primaryAllocationId = primaryShard.allocationId().getId();
    final List<StartedShardEntry> tasks = new ArrayList<>();
    tasks.add(new StartedShardEntry(shardId, primaryAllocationId, primaryTerm, "test"));
    if (randomBoolean()) {
        final ShardRouting replicaShard = clusterState.routingTable().shardRoutingTable(shardId).replicaShards().iterator().next();
        final String replicaAllocationId = replicaShard.allocationId().getId();
        tasks.add(new StartedShardEntry(shardId, replicaAllocationId, primaryTerm, "test"));
    }
    final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, tasks);
    assertNotSame(clusterState, result.resultingState);
    assertThat(result.executionResults.size(), equalTo(tasks.size()));
    tasks.forEach(task -> {
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
        final IndexShardRoutingTable shardRoutingTable = result.resultingState.routingTable().shardRoutingTable(task.shardId);
        assertThat(shardRoutingTable.getByAllocationId(task.allocationId).state(), is(ShardRoutingState.STARTED));
    });
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) ArrayList(java.util.ArrayList) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 78 with ShardRouting

use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.

the class ShardStateActionTests method testNoClusterManager.

public void testNoClusterManager() throws InterruptedException {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    DiscoveryNodes.Builder noClusterManagerBuilder = DiscoveryNodes.builder(clusterService.state().nodes());
    noClusterManagerBuilder.masterNodeId(null);
    setState(clusterService, ClusterState.builder(clusterService.state()).nodes(noClusterManagerBuilder));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger retries = new AtomicInteger();
    AtomicBoolean success = new AtomicBoolean();
    setUpClusterManagerRetryVerification(1, retries, latch, requestId -> {
    });
    ShardRouting failedShard = getRandomShardRouting(index);
    shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener<Void>() {

        @Override
        public void onResponse(Void aVoid) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            success.set(false);
            latch.countDown();
            assert false;
        }
    });
    latch.await();
    assertThat(retries.get(), equalTo(1));
    assertTrue(success.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ShardRouting(org.opensearch.cluster.routing.ShardRouting) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NotMasterException(org.opensearch.cluster.NotMasterException) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException)

Example 79 with ShardRouting

use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.

the class ShardStateActionTests method testClusterManagerChannelException.

public void testClusterManagerChannelException() throws InterruptedException {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger retries = new AtomicInteger();
    AtomicBoolean success = new AtomicBoolean();
    AtomicReference<Throwable> throwable = new AtomicReference<>();
    LongConsumer retryLoop = requestId -> {
        if (randomBoolean()) {
            transport.handleRemoteError(requestId, randomFrom(new NotMasterException("simulated"), new FailedToCommitClusterStateException("simulated")));
        } else {
            if (randomBoolean()) {
                transport.handleLocalError(requestId, new NodeNotConnectedException(null, "simulated"));
            } else {
                transport.handleError(requestId, new NodeDisconnectedException(null, ShardStateAction.SHARD_FAILED_ACTION_NAME));
            }
        }
    };
    final int numberOfRetries = randomIntBetween(1, 256);
    setUpClusterManagerRetryVerification(numberOfRetries, retries, latch, retryLoop);
    ShardRouting failedShard = getRandomShardRouting(index);
    shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener<Void>() {

        @Override
        public void onResponse(Void aVoid) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            success.set(false);
            throwable.set(e);
            latch.countDown();
            assert false;
        }
    });
    final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(1));
    assertFalse(success.get());
    assertThat(retries.get(), equalTo(0));
    retryLoop.accept(capturedRequests[0].requestId);
    latch.await();
    assertNull(throwable.get());
    assertThat(retries.get(), equalTo(numberOfRetries));
    assertTrue(success.get());
}
Also used : FailedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.FailedShardEntry) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) ClusterStateCreationUtils(org.opensearch.action.support.replication.ClusterStateCreationUtils) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Matchers.nullValue(org.hamcrest.Matchers.nullValue) NotMasterException(org.opensearch.cluster.NotMasterException) ActionListener(org.opensearch.action.ActionListener) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) AfterClass(org.junit.AfterClass) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponse(org.opensearch.transport.TransportResponse) UUID(java.util.UUID) TransportService(org.opensearch.transport.TransportService) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) CountDownLatch(java.util.concurrent.CountDownLatch) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) Matchers.is(org.hamcrest.Matchers.is) TransportException(org.opensearch.transport.TransportException) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BytesReference(org.opensearch.common.bytes.BytesReference) BeforeClass(org.junit.BeforeClass) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Writeable(org.opensearch.common.io.stream.Writeable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) RerouteService(org.opensearch.cluster.routing.RerouteService) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) SetOnce(org.apache.lucene.util.SetOnce) TransportRequest(org.opensearch.transport.TransportRequest) IOException(java.io.IOException) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) LongConsumer(java.util.function.LongConsumer) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) Phaser(java.util.concurrent.Phaser) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NotMasterException(org.opensearch.cluster.NotMasterException) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) NodeDisconnectedException(org.opensearch.transport.NodeDisconnectedException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(java.util.function.LongConsumer) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotMasterException(org.opensearch.cluster.NotMasterException) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 80 with ShardRouting

use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.

the class ShardStateActionTests method getRandomShardRouting.

private ShardRouting getRandomShardRouting(String index) {
    IndexRoutingTable indexRoutingTable = clusterService.state().routingTable().index(index);
    ShardsIterator shardsIterator = indexRoutingTable.randomAllActiveShardsIt();
    ShardRouting shardRouting = shardsIterator.nextOrNull();
    assert shardRouting != null;
    return shardRouting;
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardsIterator(org.opensearch.cluster.routing.ShardsIterator)

Aggregations

ShardRouting (org.opensearch.cluster.routing.ShardRouting)366 ClusterState (org.opensearch.cluster.ClusterState)173 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)137 ShardId (org.opensearch.index.shard.ShardId)112 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)102 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)93 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)87 RoutingTable (org.opensearch.cluster.routing.RoutingTable)85 Settings (org.opensearch.common.settings.Settings)85 Metadata (org.opensearch.cluster.metadata.Metadata)71 HashSet (java.util.HashSet)60 IOException (java.io.IOException)59 RoutingNode (org.opensearch.cluster.routing.RoutingNode)59 ArrayList (java.util.ArrayList)58 List (java.util.List)52 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)51 Index (org.opensearch.index.Index)50 IndexShard (org.opensearch.index.shard.IndexShard)50 UnassignedInfo (org.opensearch.cluster.routing.UnassignedInfo)49 CountDownLatch (java.util.concurrent.CountDownLatch)46