Search in sources :

Example 1 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch by elastic.

the class VerifyNodeRepositoryAction method verify.

public void verify(String repository, String verificationToken, final ActionListener<VerifyResponse> listener) {
    final DiscoveryNodes discoNodes = clusterService.state().nodes();
    final DiscoveryNode localNode = discoNodes.getLocalNode();
    final ObjectContainer<DiscoveryNode> masterAndDataNodes = discoNodes.getMasterAndDataNodes().values();
    final List<DiscoveryNode> nodes = new ArrayList<>();
    for (ObjectCursor<DiscoveryNode> cursor : masterAndDataNodes) {
        DiscoveryNode node = cursor.value;
        nodes.add(node);
    }
    final CopyOnWriteArrayList<VerificationFailure> errors = new CopyOnWriteArrayList<>();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    for (final DiscoveryNode node : nodes) {
        if (node.equals(localNode)) {
            try {
                doVerify(repository, verificationToken, localNode);
            } catch (Exception e) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to verify repository", repository), e);
                errors.add(new VerificationFailure(node.getId(), e));
            }
            if (counter.decrementAndGet() == 0) {
                finishVerification(listener, nodes, errors);
            }
        } else {
            transportService.sendRequest(node, ACTION_NAME, new VerifyNodeRepositoryRequest(repository, verificationToken), new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

                @Override
                public void handleResponse(TransportResponse.Empty response) {
                    if (counter.decrementAndGet() == 0) {
                        finishVerification(listener, nodes, errors);
                    }
                }

                @Override
                public void handleException(TransportException exp) {
                    errors.add(new VerificationFailure(node.getId(), exp));
                    if (counter.decrementAndGet() == 0) {
                        finishVerification(listener, nodes, errors);
                    }
                }
            });
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportException(org.elasticsearch.transport.TransportException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch by elastic.

the class TransportReplicationActionTests method testReplicaProxy.

public void testReplicaProxy() throws InterruptedException, ExecutionException {
    ReplicationOperation.Replicas proxy = action.newReplicasProxy();
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2));
    logger.info("using state: {}", state);
    setState(clusterService, state);
    // check that at unknown node fails
    PlainActionFuture<ReplicaResponse> listener = new PlainActionFuture<>();
    proxy.performOn(TestShardRouting.newShardRouting(shardId, "NOT THERE", false, randomFrom(ShardRoutingState.values())), new Request(), listener);
    assertTrue(listener.isDone());
    assertListenerThrows("non existent node should throw a NoNodeAvailableException", listener, NoNodeAvailableException.class);
    final IndexShardRoutingTable shardRoutings = state.routingTable().shardRoutingTable(shardId);
    final ShardRouting replica = randomFrom(shardRoutings.replicaShards().stream().filter(ShardRouting::assignedToNode).collect(Collectors.toList()));
    listener = new PlainActionFuture<>();
    proxy.performOn(replica, new Request(), listener);
    assertFalse(listener.isDone());
    CapturingTransport.CapturedRequest[] captures = transport.getCapturedRequestsAndClear();
    assertThat(captures, arrayWithSize(1));
    if (randomBoolean()) {
        final TransportReplicationAction.ReplicaResponse response = new TransportReplicationAction.ReplicaResponse(randomAsciiOfLength(10), randomLong());
        transport.handleResponse(captures[0].requestId, response);
        assertTrue(listener.isDone());
        assertThat(listener.get(), equalTo(response));
    } else if (randomBoolean()) {
        transport.handleRemoteError(captures[0].requestId, new ElasticsearchException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, ElasticsearchException.class);
    } else {
        transport.handleError(captures[0].requestId, new TransportException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, TransportException.class);
    }
    AtomicReference<Object> failure = new AtomicReference<>();
    AtomicReference<Object> ignoredFailure = new AtomicReference<>();
    AtomicBoolean success = new AtomicBoolean();
    proxy.failShardIfNeeded(replica, randomIntBetween(1, 10), "test", new ElasticsearchException("simulated"), () -> success.set(true), failure::set, ignoredFailure::set);
    CapturingTransport.CapturedRequest[] shardFailedRequests = transport.getCapturedRequestsAndClear();
    // A replication action doesn't not fail the request
    assertEquals(0, shardFailedRequests.length);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ReplicaResponse(org.elasticsearch.action.support.replication.ReplicationOperation.ReplicaResponse) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 3 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch by elastic.

the class TransportWriteActionTests method testReplicaProxy.

public void testReplicaProxy() throws InterruptedException, ExecutionException {
    CapturingTransport transport = new CapturingTransport();
    TransportService transportService = new TransportService(clusterService.getSettings(), transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    ShardStateAction shardStateAction = new ShardStateAction(Settings.EMPTY, clusterService, transportService, null, null, threadPool);
    TestAction action = action = new TestAction(Settings.EMPTY, "testAction", transportService, clusterService, shardStateAction, threadPool);
    ReplicationOperation.Replicas proxy = action.newReplicasProxy();
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = ClusterStateCreationUtils.stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2));
    logger.info("using state: {}", state);
    ClusterServiceUtils.setState(clusterService, state);
    // check that at unknown node fails
    PlainActionFuture<ReplicaResponse> listener = new PlainActionFuture<>();
    proxy.performOn(TestShardRouting.newShardRouting(shardId, "NOT THERE", false, randomFrom(ShardRoutingState.values())), new TestRequest(), listener);
    assertTrue(listener.isDone());
    assertListenerThrows("non existent node should throw a NoNodeAvailableException", listener, NoNodeAvailableException.class);
    final IndexShardRoutingTable shardRoutings = state.routingTable().shardRoutingTable(shardId);
    final ShardRouting replica = randomFrom(shardRoutings.replicaShards().stream().filter(ShardRouting::assignedToNode).collect(Collectors.toList()));
    listener = new PlainActionFuture<>();
    proxy.performOn(replica, new TestRequest(), listener);
    assertFalse(listener.isDone());
    CapturingTransport.CapturedRequest[] captures = transport.getCapturedRequestsAndClear();
    assertThat(captures, arrayWithSize(1));
    if (randomBoolean()) {
        final TransportReplicationAction.ReplicaResponse response = new TransportReplicationAction.ReplicaResponse(randomAsciiOfLength(10), randomLong());
        transport.handleResponse(captures[0].requestId, response);
        assertTrue(listener.isDone());
        assertThat(listener.get(), equalTo(response));
    } else if (randomBoolean()) {
        transport.handleRemoteError(captures[0].requestId, new ElasticsearchException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, ElasticsearchException.class);
    } else {
        transport.handleError(captures[0].requestId, new TransportException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, TransportException.class);
    }
    AtomicReference<Object> failure = new AtomicReference<>();
    AtomicReference<Object> ignoredFailure = new AtomicReference<>();
    AtomicBoolean success = new AtomicBoolean();
    proxy.failShardIfNeeded(replica, randomIntBetween(1, 10), "test", new ElasticsearchException("simulated"), () -> success.set(true), failure::set, ignoredFailure::set);
    CapturingTransport.CapturedRequest[] shardFailedRequests = transport.getCapturedRequestsAndClear();
    // A write replication action proxy should fail the shard
    assertEquals(1, shardFailedRequests.length);
    CapturingTransport.CapturedRequest shardFailedRequest = shardFailedRequests[0];
    ShardStateAction.ShardEntry shardEntry = (ShardStateAction.ShardEntry) shardFailedRequest.request;
    // the shard the request was sent to and the shard to be failed should be the same
    assertEquals(shardEntry.getShardId(), replica.shardId());
    assertEquals(shardEntry.getAllocationId(), replica.allocationId().getId());
    if (randomBoolean()) {
        // simulate success
        transport.handleResponse(shardFailedRequest.requestId, TransportResponse.Empty.INSTANCE);
        assertTrue(success.get());
        assertNull(failure.get());
        assertNull(ignoredFailure.get());
    } else if (randomBoolean()) {
        // simulate the primary has been demoted
        transport.handleRemoteError(shardFailedRequest.requestId, new ShardStateAction.NoLongerPrimaryShardException(replica.shardId(), "shard-failed-test"));
        assertFalse(success.get());
        assertNotNull(failure.get());
        assertNull(ignoredFailure.get());
    } else {
        // simulated an "ignored" exception
        transport.handleRemoteError(shardFailedRequest.requestId, new NodeClosedException(state.nodes().getLocalNode()));
        assertFalse(success.get());
        assertNull(failure.get());
        assertNotNull(ignoredFailure.get());
    }
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) Matchers.anyString(org.mockito.Matchers.anyString) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardId(org.elasticsearch.index.shard.ShardId) NodeClosedException(org.elasticsearch.node.NodeClosedException) ReplicationOperation(org.elasticsearch.action.support.replication.ReplicationOperation) ClusterState(org.elasticsearch.cluster.ClusterState) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportException(org.elasticsearch.transport.TransportException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransportService(org.elasticsearch.transport.TransportService) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ReplicaResponse(org.elasticsearch.action.support.replication.ReplicationOperation.ReplicaResponse) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 4 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch by elastic.

the class TransportInstanceSingleOperationActionTests method testFailureWithoutRetry.

public void testFailureWithoutRetry() throws Exception {
    Request request = new Request().index("test");
    request.shardId = new ShardId("test", "_na_", 0);
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    setState(clusterService, ClusterStateCreationUtils.state("test", randomBoolean(), ShardRoutingState.STARTED));
    action.new AsyncSingleAction(request, listener).start();
    assertThat(transport.capturedRequests().length, equalTo(1));
    long requestId = transport.capturedRequests()[0].requestId;
    transport.clear();
    // this should not trigger retry or anything and the listener should report exception immediately
    transport.handleRemoteError(requestId, new TransportException("a generic transport exception", new Exception("generic test exception")));
    try {
        // result should return immediately
        assertTrue(listener.isDone());
        listener.get();
        fail("this should fail with a transport exception");
    } catch (ExecutionException t) {
        if (ExceptionsHelper.unwrap(t, TransportException.class) == null) {
            logger.info("expected TransportException  but got ", t);
            fail("expected and TransportException");
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IndicesRequest(org.elasticsearch.action.IndicesRequest) ExecutionException(java.util.concurrent.ExecutionException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException) TimeoutException(java.util.concurrent.TimeoutException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) TransportException(org.elasticsearch.transport.TransportException)

Example 5 with TransportException

use of org.elasticsearch.transport.TransportException 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

TransportException (org.elasticsearch.transport.TransportException)46 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 IOException (java.io.IOException)28 StreamInput (org.elasticsearch.common.io.stream.StreamInput)17 ElasticsearchException (org.elasticsearch.ElasticsearchException)16 ClusterState (org.elasticsearch.cluster.ClusterState)16 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)15 TransportResponse (org.elasticsearch.transport.TransportResponse)14 TransportResponseHandler (org.elasticsearch.transport.TransportResponseHandler)12 TransportService (org.elasticsearch.transport.TransportService)12 Settings (org.elasticsearch.common.settings.Settings)10 ClusterService (org.elasticsearch.cluster.service.ClusterService)9 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)9 EmptyTransportResponseHandler (org.elasticsearch.transport.EmptyTransportResponseHandler)9 Version (org.elasticsearch.Version)8 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)8 TimeValue (io.crate.common.unit.TimeValue)7 ArrayList (java.util.ArrayList)7 Consumer (java.util.function.Consumer)7 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)7