Search in sources :

Example 6 with TransportException

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

the class ZenDiscovery method handleAnotherMaster.

private ClusterStateTaskExecutor.ClusterTasksResult handleAnotherMaster(ClusterState localClusterState, final DiscoveryNode otherMaster, long otherClusterStateVersion, String reason) {
    assert localClusterState.nodes().isLocalNodeElectedMaster() : "handleAnotherMaster called but current node is not a master";
    assert Thread.currentThread().getName().contains(ClusterService.UPDATE_THREAD_NAME) : "not called from the cluster state update thread";
    if (otherClusterStateVersion > localClusterState.version()) {
        return rejoin(localClusterState, "zen-disco-discovered another master with a new cluster_state [" + otherMaster + "][" + reason + "]");
    } else {
        logger.warn("discovered [{}] which is also master but with an older cluster_state, telling [{}] to rejoin the cluster ([{}])", otherMaster, otherMaster, reason);
        // spawn to a background thread to not do blocking operations on the cluster state thread
        threadPool.generic().execute(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to send rejoin request to [{}]", otherMaster), e);
            }

            @Override
            protected void doRun() throws Exception {
                // make sure we're connected to this node (connect to node does nothing if we're already connected)
                // since the network connections are asymmetric, it may be that we received a state but have disconnected from the node
                // in the past (after a master failure, for example)
                transportService.connectToNode(otherMaster);
                transportService.sendRequest(otherMaster, DISCOVERY_REJOIN_ACTION_NAME, new RejoinClusterRequest(localNode().getId()), new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

                    @Override
                    public void handleException(TransportException exp) {
                        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to send rejoin request to [{}]", otherMaster), exp);
                    }
                });
            }
        });
        return LocalClusterUpdateTask.unchanged();
    }
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with TransportException

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

the class PublishClusterStateAction method sendClusterStateToNode.

private void sendClusterStateToNode(final ClusterState clusterState, BytesReference bytes, final DiscoveryNode node, final TimeValue publishTimeout, final SendingController sendingController, final boolean sendDiffs, final Map<Version, BytesReference> serializedStates) {
    try {
        // -> no need to put a timeout on the options here, because we want the response to eventually be received
        //  and not log an error if it arrives after the timeout
        // -> no need to compress, we already compressed the bytes
        TransportRequestOptions options = TransportRequestOptions.builder().withType(TransportRequestOptions.Type.STATE).withCompress(false).build();
        transportService.sendRequest(node, SEND_ACTION_NAME, new BytesTransportRequest(bytes, node.getVersion()), options, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                if (sendingController.getPublishingTimedOut()) {
                    logger.debug("node {} responded for cluster state [{}] (took longer than [{}])", node, clusterState.version(), publishTimeout);
                }
                sendingController.onNodeSendAck(node);
            }

            @Override
            public void handleException(TransportException exp) {
                if (sendDiffs && exp.unwrapCause() instanceof IncompatibleClusterStateVersionException) {
                    logger.debug("resending full cluster state to node {} reason {}", node, exp.getDetailedMessage());
                    sendFullClusterState(clusterState, serializedStates, node, publishTimeout, sendingController);
                } else {
                    logger.debug((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("failed to send cluster state to {}", node), exp);
                    sendingController.onNodeSendFailed(node, exp);
                }
            }
        });
    } catch (Exception e) {
        logger.warn((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("error sending cluster state to {}", node), e);
        sendingController.onNodeSendFailed(node, e);
    }
}
Also used : TransportResponse(org.elasticsearch.transport.TransportResponse) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException)

Example 8 with TransportException

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

the class LocalAllocateDangledIndices method allocateDangled.

public void allocateDangled(Collection<IndexMetaData> indices, final Listener listener) {
    ClusterState clusterState = clusterService.state();
    DiscoveryNode masterNode = clusterState.nodes().getMasterNode();
    if (masterNode == null) {
        listener.onFailure(new MasterNotDiscoveredException("no master to send allocate dangled request"));
        return;
    }
    AllocateDangledRequest request = new AllocateDangledRequest(clusterService.localNode(), indices.toArray(new IndexMetaData[indices.size()]));
    transportService.sendRequest(masterNode, ACTION_NAME, request, new TransportResponseHandler<AllocateDangledResponse>() {

        @Override
        public AllocateDangledResponse newInstance() {
            return new AllocateDangledResponse();
        }

        @Override
        public void handleResponse(AllocateDangledResponse response) {
            listener.onResponse(response);
        }

        @Override
        public void handleException(TransportException exp) {
            listener.onFailure(exp);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    });
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) TransportException(org.elasticsearch.transport.TransportException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 9 with TransportException

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

the class TransportClientNodesServiceTests method testListenerFailures.

public void testListenerFailures() throws InterruptedException {
    int iters = iterations(10, 100);
    for (int i = 0; i < iters; i++) {
        try (TestIteration iteration = new TestIteration()) {
            // stop transport from responding early
            iteration.transport.endConnectMode();
            final CountDownLatch latch = new CountDownLatch(1);
            final AtomicInteger finalFailures = new AtomicInteger();
            final AtomicReference<Throwable> finalFailure = new AtomicReference<>();
            final AtomicReference<TestResponse> response = new AtomicReference<>();
            ActionListener<TestResponse> actionListener = new ActionListener<TestResponse>() {

                @Override
                public void onResponse(TestResponse testResponse) {
                    response.set(testResponse);
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception e) {
                    finalFailures.incrementAndGet();
                    finalFailure.set(e);
                    latch.countDown();
                }
            };
            final AtomicInteger preSendFailures = new AtomicInteger();
            iteration.transportClientNodesService.execute((node, retryListener) -> {
                if (rarely()) {
                    preSendFailures.incrementAndGet();
                    //throw whatever exception that is not a subclass of ConnectTransportException
                    throw new IllegalArgumentException();
                }
                iteration.transportService.sendRequest(node, "action", new TestRequest(), TransportRequestOptions.EMPTY, new TransportResponseHandler<TestResponse>() {

                    @Override
                    public TestResponse newInstance() {
                        return new TestResponse();
                    }

                    @Override
                    public void handleResponse(TestResponse response1) {
                        retryListener.onResponse(response1);
                    }

                    @Override
                    public void handleException(TransportException exp) {
                        retryListener.onFailure(exp);
                    }

                    @Override
                    public String executor() {
                        return randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC;
                    }
                });
            }, actionListener);
            assertThat(latch.await(1, TimeUnit.SECONDS), equalTo(true));
            //there can be only either one failure that causes the request to fail straightaway or success
            assertThat(preSendFailures.get() + iteration.transport.failures() + iteration.transport.successes(), lessThanOrEqualTo(1));
            if (iteration.transport.successes() == 1) {
                assertThat(finalFailures.get(), equalTo(0));
                assertThat(finalFailure.get(), nullValue());
                assertThat(response.get(), notNullValue());
            } else {
                assertThat(finalFailures.get(), equalTo(1));
                assertThat(finalFailure.get(), notNullValue());
                assertThat(response.get(), nullValue());
                if (preSendFailures.get() == 0 && iteration.transport.failures() == 0) {
                    assertThat(finalFailure.get(), instanceOf(NoNodeAvailableException.class));
                }
            }
            assertThat(iteration.transport.triedNodes().size(), lessThanOrEqualTo(iteration.listNodesCount));
            assertThat(iteration.transport.triedNodes().size(), equalTo(iteration.transport.connectTransportExceptions() + iteration.transport.failures() + iteration.transport.successes()));
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransportException(org.elasticsearch.transport.TransportException) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with TransportException

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

the class ZenDiscoveryIT method testNodeRejectsClusterStateWithWrongMasterNode.

public void testNodeRejectsClusterStateWithWrongMasterNode() throws Exception {
    List<String> nodeNames = internalCluster().startNodes(2);
    List<String> nonMasterNodes = new ArrayList<>(nodeNames);
    nonMasterNodes.remove(internalCluster().getMasterName());
    String noneMasterNode = nonMasterNodes.get(0);
    ClusterState state = internalCluster().getInstance(ClusterService.class).state();
    DiscoveryNode node = null;
    for (DiscoveryNode discoveryNode : state.nodes()) {
        if (discoveryNode.getName().equals(noneMasterNode)) {
            node = discoveryNode;
        }
    }
    assert node != null;
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(state.nodes()).add(new DiscoveryNode("abc", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT)).masterNodeId("abc");
    ClusterState.Builder builder = ClusterState.builder(state);
    builder.nodes(nodes);
    BytesReference bytes = PublishClusterStateAction.serializeFullClusterState(builder.build(), node.getVersion());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Exception> reference = new AtomicReference<>();
    internalCluster().getInstance(TransportService.class, noneMasterNode).sendRequest(node, PublishClusterStateAction.SEND_ACTION_NAME, new BytesTransportRequest(bytes, Version.CURRENT), new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

        @Override
        public void handleResponse(TransportResponse.Empty response) {
            super.handleResponse(response);
            latch.countDown();
        }

        @Override
        public void handleException(TransportException exp) {
            super.handleException(exp);
            reference.set(exp);
            latch.countDown();
        }
    });
    latch.await();
    assertThat(reference.get(), notNullValue());
    assertThat(ExceptionsHelper.detailedMessage(reference.get()), containsString("cluster state from a different master than the current one, rejecting"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportException(org.elasticsearch.transport.TransportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) TransportException(org.elasticsearch.transport.TransportException) ClusterService(org.elasticsearch.cluster.service.ClusterService) TransportService(org.elasticsearch.transport.TransportService) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

TransportException (org.elasticsearch.transport.TransportException)21 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)11 IOException (java.io.IOException)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)8 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)7 EmptyTransportResponseHandler (org.elasticsearch.transport.EmptyTransportResponseHandler)7 TransportResponse (org.elasticsearch.transport.TransportResponse)7 ClusterState (org.elasticsearch.cluster.ClusterState)6 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Supplier (org.apache.logging.log4j.util.Supplier)5 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)4 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)3 CountDown (org.elasticsearch.common.util.concurrent.CountDown)3 ShardId (org.elasticsearch.index.shard.ShardId)3 TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)3 TransportResponseHandler (org.elasticsearch.transport.TransportResponseHandler)3