Search in sources :

Example 1 with SendRequestTransportException

use of org.opensearch.transport.SendRequestTransportException in project OpenSearch by opensearch-project.

the class ReplicationOperationTests method testNoLongerPrimary.

public void testNoLongerPrimary() throws Exception {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState initialState = stateWithActivePrimary(index, true, 1 + randomInt(2), randomInt(2));
    IndexMetadata indexMetadata = initialState.getMetadata().index(index);
    final long primaryTerm = indexMetadata.primaryTerm(0);
    final IndexShardRoutingTable indexShardRoutingTable = initialState.getRoutingTable().shardRoutingTable(shardId);
    ShardRouting primaryShard = indexShardRoutingTable.primaryShard();
    if (primaryShard.relocating() && randomBoolean()) {
        // simulate execution of the replication phase on the relocation target node after relocation source was marked as relocated
        initialState = ClusterState.builder(initialState).nodes(DiscoveryNodes.builder(initialState.nodes()).localNodeId(primaryShard.relocatingNodeId())).build();
        primaryShard = primaryShard.getTargetRelocatingShard();
    }
    // add an in-sync allocation id that doesn't have a corresponding routing entry
    final Set<String> staleAllocationIds = Sets.newHashSet(randomAlphaOfLength(10));
    final Set<String> inSyncAllocationIds = Sets.union(indexMetadata.inSyncAllocationIds(0), staleAllocationIds);
    final Set<String> trackedShards = new HashSet<>();
    addTrackingInfo(indexShardRoutingTable, primaryShard, trackedShards, new HashSet<>());
    trackedShards.addAll(staleAllocationIds);
    final ReplicationGroup replicationGroup = new ReplicationGroup(indexShardRoutingTable, inSyncAllocationIds, trackedShards, 0);
    final Set<ShardRouting> expectedReplicas = getExpectedReplicas(shardId, initialState, trackedShards);
    final Map<ShardRouting, Exception> expectedFailures = new HashMap<>();
    if (expectedReplicas.isEmpty()) {
        return;
    }
    final ShardRouting failedReplica = randomFrom(new ArrayList<>(expectedReplicas));
    expectedFailures.put(failedReplica, new CorruptIndexException("simulated", (String) null));
    Request request = new Request(shardId);
    PlainActionFuture<TestPrimary.Result> listener = new PlainActionFuture<>();
    final boolean testPrimaryDemotedOnStaleShardCopies = randomBoolean();
    final Exception shardActionFailure;
    if (randomBoolean()) {
        shardActionFailure = new NodeClosedException(new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT));
    } else if (randomBoolean()) {
        DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT);
        shardActionFailure = new SendRequestTransportException(node, ShardStateAction.SHARD_FAILED_ACTION_NAME, new NodeClosedException(node));
    } else {
        shardActionFailure = new ShardStateAction.NoLongerPrimaryShardException(failedReplica.shardId(), "the king is dead");
    }
    final TestReplicaProxy replicasProxy = new TestReplicaProxy(expectedFailures) {

        @Override
        public void failShardIfNeeded(ShardRouting replica, long primaryTerm, String message, Exception exception, ActionListener<Void> shardActionListener) {
            if (testPrimaryDemotedOnStaleShardCopies) {
                super.failShardIfNeeded(replica, primaryTerm, message, exception, shardActionListener);
            } else {
                assertThat(replica, equalTo(failedReplica));
                shardActionListener.onFailure(shardActionFailure);
            }
        }

        @Override
        public void markShardCopyAsStaleIfNeeded(ShardId shardId, String allocationId, long primaryTerm, ActionListener<Void> shardActionListener) {
            if (testPrimaryDemotedOnStaleShardCopies) {
                shardActionListener.onFailure(shardActionFailure);
            } else {
                super.markShardCopyAsStaleIfNeeded(shardId, allocationId, primaryTerm, shardActionListener);
            }
        }
    };
    AtomicBoolean primaryFailed = new AtomicBoolean();
    final TestPrimary primary = new TestPrimary(primaryShard, () -> replicationGroup, threadPool) {

        @Override
        public void failShard(String message, Exception exception) {
            assertThat(exception, instanceOf(ShardStateAction.NoLongerPrimaryShardException.class));
            assertTrue(primaryFailed.compareAndSet(false, true));
        }
    };
    final TestReplicationOperation op = new TestReplicationOperation(request, primary, listener, replicasProxy, primaryTerm);
    op.execute();
    assertThat("request was not processed on primary", request.processedOnPrimary.get(), equalTo(true));
    assertTrue("listener is not marked as done", listener.isDone());
    if (shardActionFailure instanceof ShardStateAction.NoLongerPrimaryShardException) {
        assertTrue(primaryFailed.get());
    } else {
        assertFalse(primaryFailed.get());
    }
    assertListenerThrows("should throw exception to trigger retry", listener, ReplicationOperation.RetryOnPrimaryException.class);
}
Also used : IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) ShardId(org.opensearch.index.shard.ShardId) SendRequestTransportException(org.opensearch.transport.SendRequestTransportException) NodeClosedException(org.opensearch.node.NodeClosedException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeClosedException(org.opensearch.node.NodeClosedException) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) IndexShardNotStartedException(org.opensearch.index.shard.IndexShardNotStartedException) SendRequestTransportException(org.opensearch.transport.SendRequestTransportException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 2 with SendRequestTransportException

use of org.opensearch.transport.SendRequestTransportException in project OpenSearch by opensearch-project.

the class MockTransport method handleLocalError.

/**
 * simulate a local error for the given requestId, will be wrapped
 * by a {@link SendRequestTransportException}
 *
 * @param requestId the id corresponding to the captured send
 *                  request
 * @param t         the failure to wrap
 */
public void handleLocalError(final long requestId, final Throwable t) {
    Tuple<DiscoveryNode, String> request = requests.get(requestId);
    assert request != null;
    this.handleError(requestId, new SendRequestTransportException(request.v1(), request.v2(), t));
}
Also used : SendRequestTransportException(org.opensearch.transport.SendRequestTransportException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 SendRequestTransportException (org.opensearch.transport.SendRequestTransportException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 ActionListener (org.opensearch.action.ActionListener)1 UnavailableShardsException (org.opensearch.action.UnavailableShardsException)1 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)1 ClusterState (org.opensearch.cluster.ClusterState)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)1 ShardRouting (org.opensearch.cluster.routing.ShardRouting)1 CircuitBreakingException (org.opensearch.common.breaker.CircuitBreakingException)1 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)1 IndexShardNotStartedException (org.opensearch.index.shard.IndexShardNotStartedException)1 ReplicationGroup (org.opensearch.index.shard.ReplicationGroup)1 ShardId (org.opensearch.index.shard.ShardId)1