Search in sources :

Example 1 with ConcreteShardRequest

use of org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest in project crate by crate.

the class TransportVerifyShardBeforeCloseActionTests method testUnavailableShardsMarkedAsStale.

@Test
public void testUnavailableShardsMarkedAsStale() throws Exception {
    String index = "test";
    ShardId shardId = new ShardId(index, "_na_", 0);
    int nbReplicas = randomIntBetween(1, 10);
    ShardRoutingState[] replicaStates = new ShardRoutingState[nbReplicas];
    Arrays.fill(replicaStates, ShardRoutingState.STARTED);
    final ClusterState clusterState = state(index, true, ShardRoutingState.STARTED, replicaStates);
    setState(clusterService, clusterState);
    IndexShardRoutingTable shardRoutingTable = clusterState.routingTable().index(index).shard(shardId.id());
    IndexMetadata indexMetaData = clusterState.getMetadata().index(index);
    ShardRouting primaryRouting = shardRoutingTable.primaryShard();
    long primaryTerm = indexMetaData.primaryTerm(0);
    Set<String> inSyncAllocationIds = indexMetaData.inSyncAllocationIds(0);
    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();
    ReplicationGroup replicationGroup = new ReplicationGroup(shardRoutingTable, inSyncAllocationIds, trackedShards);
    assertThat(replicationGroup.getUnavailableInSyncShards().size(), greaterThan(0));
    PlainActionFuture<PrimaryResult> listener = new PlainActionFuture<>();
    TransportVerifyShardBeforeCloseAction.ShardRequest request = new TransportVerifyShardBeforeCloseAction.ShardRequest(shardId, false, clusterBlock);
    ReplicationOperation.Replicas<TransportVerifyShardBeforeCloseAction.ShardRequest> proxy = action.newReplicasProxy();
    ReplicationOperation<TransportVerifyShardBeforeCloseAction.ShardRequest, TransportVerifyShardBeforeCloseAction.ShardRequest, PrimaryResult> operation = new ReplicationOperation<>(request, createPrimary(primaryRouting, replicationGroup), listener, proxy, logger, "test", primaryTerm);
    operation.execute();
    CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(nbReplicas));
    for (CapturingTransport.CapturedRequest capturedRequest : capturedRequests) {
        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);
        }
    }
    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 : ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Mockito.doThrow(org.mockito.Mockito.doThrow) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) EnumSet(java.util.EnumSet) AfterClass(org.junit.AfterClass) ConcreteShardRequest(org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportReplicationAction(org.elasticsearch.action.support.replication.TransportReplicationAction) Engine(org.elasticsearch.index.engine.Engine) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) RestStatus(org.elasticsearch.rest.RestStatus) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Mockito.mock(org.mockito.Mockito.mock) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ClusterStateCreationUtils.state(org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) BeforeClass(org.junit.BeforeClass) ClusterService(org.elasticsearch.cluster.service.ClusterService) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) ArgumentCaptor(org.mockito.ArgumentCaptor) TransportResponse(org.elasticsearch.transport.TransportResponse) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) INDEX_CLOSED_BLOCK_ID(io.crate.execution.ddl.tables.TransportCloseTable.INDEX_CLOSED_BLOCK_ID) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) Before(org.junit.Before) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) SetOnce(org.apache.lucene.util.SetOnce) IndexShard(org.elasticsearch.index.shard.IndexShard) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) ReplicationOperation(org.elasticsearch.action.support.replication.ReplicationOperation) ReplicationGroup(org.elasticsearch.index.shard.ReplicationGroup) ClusterServiceUtils.setState(org.elasticsearch.test.ClusterServiceUtils.setState) ActionListener(org.elasticsearch.action.ActionListener) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ReplicationGroup(org.elasticsearch.index.shard.ReplicationGroup) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) ShardId(org.elasticsearch.index.shard.ShardId) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ReplicationOperation(org.elasticsearch.action.support.replication.ReplicationOperation) ClusterState(org.elasticsearch.cluster.ClusterState) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConcreteShardRequest(org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Test(org.junit.Test)

Aggregations

INDEX_CLOSED_BLOCK_ID (io.crate.execution.ddl.tables.TransportCloseTable.INDEX_CLOSED_BLOCK_ID)1 Arrays (java.util.Arrays)1 EnumSet (java.util.EnumSet)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 ActionListener (org.elasticsearch.action.ActionListener)1 FlushRequest (org.elasticsearch.action.admin.indices.flush.FlushRequest)1 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)1 ClusterStateCreationUtils.state (org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state)1 ReplicationOperation (org.elasticsearch.action.support.replication.ReplicationOperation)1 ReplicationResponse (org.elasticsearch.action.support.replication.ReplicationResponse)1 TransportReplicationAction (org.elasticsearch.action.support.replication.TransportReplicationAction)1 ConcreteShardRequest (org.elasticsearch.action.support.replication.TransportReplicationAction.ConcreteShardRequest)1 ClusterName (org.elasticsearch.cluster.ClusterName)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ShardStateAction (org.elasticsearch.cluster.action.shard.ShardStateAction)1 ClusterBlock (org.elasticsearch.cluster.block.ClusterBlock)1