Search in sources :

Example 1 with BytesTransportRequest

use of org.elasticsearch.transport.BytesTransportRequest 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 2 with BytesTransportRequest

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

Example 3 with BytesTransportRequest

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

the class PublishClusterStateActionTests method testOutOfOrderCommitMessages.

public void testOutOfOrderCommitMessages() throws Throwable {
    MockNode node = createMockNode("node").setAsMaster();
    final CapturingTransportChannel channel = new CapturingTransportChannel();
    List<ClusterState> states = new ArrayList<>();
    final int numOfStates = scaledRandomIntBetween(3, 25);
    for (int i = 1; i <= numOfStates; i++) {
        states.add(ClusterState.builder(node.clusterState).version(i).stateUUID(ClusterState.UNKNOWN_UUID).build());
    }
    final ClusterState finalState = states.get(numOfStates - 1);
    logger.info("--> publishing states");
    for (ClusterState state : states) {
        node.action.handleIncomingClusterStateRequest(new BytesTransportRequest(PublishClusterStateAction.serializeFullClusterState(state, Version.CURRENT), Version.CURRENT), channel);
        assertThat(channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
        assertThat(channel.error.get(), nullValue());
        channel.clear();
    }
    logger.info("--> committing states");
    long largestVersionSeen = Long.MIN_VALUE;
    Randomness.shuffle(states);
    for (ClusterState state : states) {
        node.action.handleCommitRequest(new PublishClusterStateAction.CommitClusterStateRequest(state.stateUUID()), channel);
        if (largestVersionSeen < state.getVersion()) {
            assertThat(channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
            if (channel.error.get() != null) {
                throw channel.error.get();
            }
            largestVersionSeen = state.getVersion();
        } else {
            // older cluster states will be rejected
            assertNotNull(channel.error.get());
            assertThat(channel.error.get(), instanceOf(IllegalStateException.class));
        }
        channel.clear();
    }
    //now check the last state held
    assertSameState(node.clusterState, finalState);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TransportResponse(org.elasticsearch.transport.TransportResponse)

Example 4 with BytesTransportRequest

use of org.elasticsearch.transport.BytesTransportRequest in project crate by crate.

the class PublicationTransportHandler method sendClusterStateToNode.

private void sendClusterStateToNode(ClusterState clusterState, BytesReference bytes, DiscoveryNode node, ActionListener<PublishWithJoinResponse> responseActionListener, boolean sendDiffs, Map<Version, BytesReference> serializedStates) {
    try {
        final BytesTransportRequest request = new BytesTransportRequest(bytes, node.getVersion());
        final Consumer<TransportException> transportExceptionHandler = exp -> {
            if (sendDiffs && exp.unwrapCause() instanceof IncompatibleClusterStateVersionException) {
                LOGGER.debug("resending full cluster state to node {} reason {}", node, exp.getDetailedMessage());
                sendFullClusterState(clusterState, serializedStates, node, responseActionListener);
            } else {
                LOGGER.debug(() -> new ParameterizedMessage("failed to send cluster state to {}", node), exp);
                responseActionListener.onFailure(exp);
            }
        };
        final TransportResponseHandler<PublishWithJoinResponse> publishWithJoinResponseHandler = new TransportResponseHandler<PublishWithJoinResponse>() {

            @Override
            public PublishWithJoinResponse read(StreamInput in) throws IOException {
                return new PublishWithJoinResponse(in);
            }

            @Override
            public void handleResponse(PublishWithJoinResponse response) {
                responseActionListener.onResponse(response);
            }

            @Override
            public void handleException(TransportException exp) {
                transportExceptionHandler.accept(exp);
            }

            @Override
            public String executor() {
                return ThreadPool.Names.GENERIC;
            }
        };
        transportService.sendRequest(node, PUBLISH_STATE_ACTION_NAME, request, stateRequestOptions, publishWithJoinResponseHandler);
    } catch (Exception e) {
        LOGGER.warn(() -> new ParameterizedMessage("error sending cluster state to {}", node), e);
        responseActionListener.onFailure(e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) TransportChannel(org.elasticsearch.transport.TransportChannel) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Diff(org.elasticsearch.cluster.Diff) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Compressor(org.elasticsearch.common.compress.Compressor) IOUtils(io.crate.common.io.IOUtils) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) CompressorFactory(org.elasticsearch.common.compress.CompressorFactory) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) LogManager(org.apache.logging.log4j.LogManager) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) BytesTransportRequest(org.elasticsearch.transport.BytesTransportRequest) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

BytesTransportRequest (org.elasticsearch.transport.BytesTransportRequest)4 TransportResponse (org.elasticsearch.transport.TransportResponse)4 IOException (java.io.IOException)3 ClusterState (org.elasticsearch.cluster.ClusterState)3 TransportException (org.elasticsearch.transport.TransportException)3 ArrayList (java.util.ArrayList)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IncompatibleClusterStateVersionException (org.elasticsearch.cluster.IncompatibleClusterStateVersionException)2 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 EmptyTransportResponseHandler (org.elasticsearch.transport.EmptyTransportResponseHandler)2 TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)2 TransportService (org.elasticsearch.transport.TransportService)2 IOUtils (io.crate.common.io.IOUtils)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1