Search in sources :

Example 1 with ReplicationOperation

use of org.opensearch.action.support.replication.ReplicationOperation in project OpenSearch by opensearch-project.

the class TransportVerifyShardBeforeCloseActionTests method testUnavailableShardsMarkedAsStale.

public void testUnavailableShardsMarkedAsStale() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    final int nbReplicas = randomIntBetween(1, 10);
    final ShardRoutingState[] replicaStates = new ShardRoutingState[nbReplicas];
    for (int i = 0; i < replicaStates.length; i++) {
        replicaStates[i] = ShardRoutingState.STARTED;
    }
    final ClusterState clusterState = state(index, true, ShardRoutingState.STARTED, replicaStates);
    setState(clusterService, clusterState);
    IndexShardRoutingTable shardRoutingTable = clusterState.routingTable().index(index).shard(shardId.id());
    final IndexMetadata indexMetadata = clusterState.getMetadata().index(index);
    final ShardRouting primaryRouting = shardRoutingTable.primaryShard();
    final long primaryTerm = indexMetadata.primaryTerm(0);
    final Set<String> inSyncAllocationIds = indexMetadata.inSyncAllocationIds(0);
    final Set<String> trackedShards = shardRoutingTable.getAllAllocationIds();
    List<ShardRouting> unavailableShards = randomSubsetOf(randomIntBetween(1, nbReplicas), shardRoutingTable.replicaShards());
    IndexShardRoutingTable.Builder shardRoutingTableBuilder = new IndexShardRoutingTable.Builder(shardRoutingTable);
    unavailableShards.forEach(shardRoutingTableBuilder::removeShard);
    shardRoutingTable = shardRoutingTableBuilder.build();
    final ReplicationGroup replicationGroup = new ReplicationGroup(shardRoutingTable, inSyncAllocationIds, trackedShards, 0);
    assertThat(replicationGroup.getUnavailableInSyncShards().size(), greaterThan(0));
    final PlainActionFuture<PrimaryResult> listener = new PlainActionFuture<>();
    TaskId taskId = new TaskId(clusterService.localNode().getId(), 0L);
    TransportVerifyShardBeforeCloseAction.ShardRequest request = new TransportVerifyShardBeforeCloseAction.ShardRequest(shardId, clusterBlock, false, taskId);
    ReplicationOperation.Replicas<TransportVerifyShardBeforeCloseAction.ShardRequest> proxy = action.newReplicasProxy();
    ReplicationOperation<TransportVerifyShardBeforeCloseAction.ShardRequest, TransportVerifyShardBeforeCloseAction.ShardRequest, PrimaryResult> operation = new ReplicationOperation<>(request, createPrimary(primaryRouting, replicationGroup), listener, proxy, logger, threadPool, "test", primaryTerm, TimeValue.timeValueMillis(20), TimeValue.timeValueSeconds(60));
    operation.execute();
    final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(nbReplicas));
    for (CapturingTransport.CapturedRequest capturedRequest : capturedRequests) {
        final String actionName = capturedRequest.action;
        if (actionName.startsWith(ShardStateAction.SHARD_FAILED_ACTION_NAME)) {
            assertThat(capturedRequest.request, instanceOf(ShardStateAction.FailedShardEntry.class));
            String allocationId = ((ShardStateAction.FailedShardEntry) capturedRequest.request).getAllocationId();
            assertTrue(unavailableShards.stream().anyMatch(shardRouting -> shardRouting.allocationId().getId().equals(allocationId)));
            transport.handleResponse(capturedRequest.requestId, TransportResponse.Empty.INSTANCE);
        } else if (actionName.startsWith(TransportVerifyShardBeforeCloseAction.NAME)) {
            assertThat(capturedRequest.request, instanceOf(ConcreteShardRequest.class));
            String allocationId = ((ConcreteShardRequest) capturedRequest.request).getTargetAllocationID();
            assertFalse(unavailableShards.stream().anyMatch(shardRouting -> shardRouting.allocationId().getId().equals(allocationId)));
            assertTrue(inSyncAllocationIds.stream().anyMatch(inSyncAllocationId -> inSyncAllocationId.equals(allocationId)));
            transport.handleResponse(capturedRequest.requestId, new TransportReplicationAction.ReplicaResponse(0L, 0L));
        } else {
            fail("Test does not support action " + capturedRequest.action);
        }
    }
    final ReplicationResponse.ShardInfo shardInfo = listener.get().getShardInfo();
    assertThat(shardInfo.getFailed(), equalTo(0));
    assertThat(shardInfo.getFailures(), arrayWithSize(0));
    assertThat(shardInfo.getSuccessful(), equalTo(1 + nbReplicas - unavailableShards.size()));
}
Also used : TestThreadPool(org.opensearch.threadpool.TestThreadPool) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) Mockito.doThrow(org.mockito.Mockito.doThrow) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) After(org.junit.After) ActionListener(org.opensearch.action.ActionListener) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IndicesService(org.opensearch.indices.IndicesService) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) TransportResponse(org.opensearch.transport.TransportResponse) Mockito.doNothing(org.mockito.Mockito.doNothing) ClusterStateCreationUtils.state(org.opensearch.action.support.replication.ClusterStateCreationUtils.state) TransportService(org.opensearch.transport.TransportService) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) ConcreteShardRequest(org.opensearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest) ClusterState(org.opensearch.cluster.ClusterState) IndexShard(org.opensearch.index.shard.IndexShard) ArgumentCaptor(org.mockito.ArgumentCaptor) ReplicationOperation(org.opensearch.action.support.replication.ReplicationOperation) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) Before(org.junit.Before) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) SetOnce(org.apache.lucene.util.SetOnce) PendingReplicationActions(org.opensearch.action.support.replication.PendingReplicationActions) TaskId(org.opensearch.tasks.TaskId) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) TransportReplicationAction(org.opensearch.action.support.replication.TransportReplicationAction) Mockito.verify(org.mockito.Mockito.verify) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) TaskId(org.opensearch.tasks.TaskId) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) ShardId(org.opensearch.index.shard.ShardId) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ReplicationOperation(org.opensearch.action.support.replication.ReplicationOperation) ClusterState(org.opensearch.cluster.ClusterState) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ConcreteShardRequest(org.opensearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 SetOnce (org.apache.lucene.util.SetOnce)1 Matchers.arrayWithSize (org.hamcrest.Matchers.arrayWithSize)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.is (org.hamcrest.Matchers.is)1 After (org.junit.After)1 AfterClass (org.junit.AfterClass)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Mockito.any (org.mockito.Mockito.any)1 Mockito.doNothing (org.mockito.Mockito.doNothing)1 Mockito.doThrow (org.mockito.Mockito.doThrow)1 Mockito.mock (org.mockito.Mockito.mock)1