Search in sources :

Example 1 with TransportChannel

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

the class ZenDiscoveryUnitTests method testValidateOnUnsupportedIndexVersionCreated.

public void testValidateOnUnsupportedIndexVersionCreated() throws Exception {
    final int iters = randomIntBetween(3, 10);
    for (int i = 0; i < iters; i++) {
        ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.DEFAULT);
        final DiscoveryNode otherNode = new DiscoveryNode("other_node", buildNewFakeTransportAddress(), emptyMap(), EnumSet.allOf(DiscoveryNode.Role.class), Version.CURRENT);
        MembershipAction.ValidateJoinRequestRequestHandler request = new MembershipAction.ValidateJoinRequestRequestHandler();
        final boolean incompatible = randomBoolean();
        IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(Settings.builder().put(SETTING_VERSION_CREATED, incompatible ? VersionUtils.getPreviousVersion(Version.CURRENT.minimumIndexCompatibilityVersion()) : VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumIndexCompatibilityVersion(), Version.CURRENT)).put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put(SETTING_CREATION_DATE, System.currentTimeMillis())).state(IndexMetaData.State.OPEN).build();
        IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex());
        RoutingTable.Builder routing = new RoutingTable.Builder();
        routing.addAsNew(indexMetaData);
        final ShardId shardId = new ShardId("test", "_na_", 0);
        IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
        final DiscoveryNode primaryNode = otherNode;
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting("test", 0, primaryNode.getId(), null, true, ShardRoutingState.INITIALIZING, new UnassignedInfo(UnassignedInfo.Reason.INDEX_REOPENED, "getting there")));
        indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build());
        IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build();
        IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData);
        stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()).routingTable(RoutingTable.builder().add(indexRoutingTable).build());
        if (incompatible) {
            IllegalStateException ex = expectThrows(IllegalStateException.class, () -> request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), null));
            assertEquals("index [test] version not supported: " + VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()) + " minimum compatible index version is: " + Version.CURRENT.minimumCompatibilityVersion(), ex.getMessage());
        } else {
            AtomicBoolean sendResponse = new AtomicBoolean(false);
            request.messageReceived(new MembershipAction.ValidateJoinRequest(stateBuilder.build()), new TransportChannel() {

                @Override
                public String action() {
                    return null;
                }

                @Override
                public String getProfileName() {
                    return null;
                }

                @Override
                public long getRequestId() {
                    return 0;
                }

                @Override
                public String getChannelType() {
                    return null;
                }

                @Override
                public void sendResponse(TransportResponse response) throws IOException {
                    sendResponse.set(true);
                }

                @Override
                public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
                }

                @Override
                public void sendResponse(Exception exception) throws IOException {
                }
            });
            assertTrue(sendResponse.get());
        }
    }
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportResponse(org.elasticsearch.transport.TransportResponse) ShardId(org.elasticsearch.index.shard.ShardId) TransportChannel(org.elasticsearch.transport.TransportChannel) ClusterState(org.elasticsearch.cluster.ClusterState) ZenDiscovery.shouldIgnoreOrRejectNewClusterState(org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState) IOException(java.io.IOException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Role(org.elasticsearch.cluster.node.DiscoveryNode.Role) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) TransportResponseOptions(org.elasticsearch.transport.TransportResponseOptions)

Example 2 with TransportChannel

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

the class Netty4ScheduledPingTests method testScheduledPing.

public void testScheduledPing() throws Exception {
    ThreadPool threadPool = new TestThreadPool(getClass().getName());
    Settings settings = Settings.builder().put(TcpTransport.PING_SCHEDULE.getKey(), "5ms").put(TransportSettings.PORT.getKey(), 0).put("cluster.name", "test").build();
    CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService();
    NamedWriteableRegistry registry = new NamedWriteableRegistry(Collections.emptyList());
    final Netty4Transport nettyA = new Netty4Transport(settings, threadPool, new NetworkService(settings, Collections.emptyList()), BigArrays.NON_RECYCLING_INSTANCE, registry, circuitBreakerService);
    MockTransportService serviceA = new MockTransportService(settings, nettyA, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, null);
    serviceA.start();
    serviceA.acceptIncomingRequests();
    final Netty4Transport nettyB = new Netty4Transport(settings, threadPool, new NetworkService(settings, Collections.emptyList()), BigArrays.NON_RECYCLING_INSTANCE, registry, circuitBreakerService);
    MockTransportService serviceB = new MockTransportService(settings, nettyB, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, null);
    serviceB.start();
    serviceB.acceptIncomingRequests();
    DiscoveryNode nodeA = serviceA.getLocalDiscoNode();
    DiscoveryNode nodeB = serviceB.getLocalDiscoNode();
    serviceA.connectToNode(nodeB);
    serviceB.connectToNode(nodeA);
    assertBusy(new Runnable() {

        @Override
        public void run() {
            assertThat(nettyA.getPing().getSuccessfulPings(), greaterThan(100L));
            assertThat(nettyB.getPing().getSuccessfulPings(), greaterThan(100L));
        }
    });
    assertThat(nettyA.getPing().getFailedPings(), equalTo(0L));
    assertThat(nettyB.getPing().getFailedPings(), equalTo(0L));
    serviceA.registerRequestHandler("sayHello", TransportRequest.Empty::new, ThreadPool.Names.GENERIC, new TransportRequestHandler<TransportRequest.Empty>() {

        @Override
        public void messageReceived(TransportRequest.Empty request, TransportChannel channel) {
            try {
                channel.sendResponse(TransportResponse.Empty.INSTANCE, TransportResponseOptions.EMPTY);
            } catch (IOException e) {
                logger.error("Unexpected failure", e);
                fail(e.getMessage());
            }
        }
    });
    int rounds = scaledRandomIntBetween(100, 5000);
    for (int i = 0; i < rounds; i++) {
        serviceB.submitRequest(nodeA, "sayHello", TransportRequest.Empty.INSTANCE, TransportRequestOptions.builder().withCompress(randomBoolean()).build(), new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public TransportResponse.Empty newInstance() {
                return TransportResponse.Empty.INSTANCE;
            }

            @Override
            public String executor() {
                return ThreadPool.Names.GENERIC;
            }

            @Override
            public void handleResponse(TransportResponse.Empty response) {
            }

            @Override
            public void handleException(TransportException exp) {
                logger.error("Unexpected failure", exp);
                fail("got exception instead of a response: " + exp.getMessage());
            }
        }).txGet();
    }
    assertBusy(() -> {
        assertThat(nettyA.getPing().getSuccessfulPings(), greaterThan(200L));
        assertThat(nettyB.getPing().getSuccessfulPings(), greaterThan(200L));
    });
    assertThat(nettyA.getPing().getFailedPings(), equalTo(0L));
    assertThat(nettyB.getPing().getFailedPings(), equalTo(0L));
    Releasables.close(serviceA, serviceB);
    terminate(threadPool);
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) IOException(java.io.IOException) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportException(org.elasticsearch.transport.TransportException) NetworkService(org.elasticsearch.common.network.NetworkService) TransportChannel(org.elasticsearch.transport.TransportChannel) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) TransportSettings(org.elasticsearch.transport.TransportSettings) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 3 with TransportChannel

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

the class DisruptableMockTransport method onConnectedDuringSend.

protected void onConnectedDuringSend(long requestId, String action, TransportRequest request, DisruptableMockTransport destinationTransport) {
    final RequestHandlerRegistry<TransportRequest> requestHandler = destinationTransport.getRequestHandlers().getHandler(action);
    final DiscoveryNode destination = destinationTransport.getLocalNode();
    final String requestDescription = getRequestDescription(requestId, action, destination);
    final TransportChannel transportChannel = new TransportChannel() {

        @Override
        public String getProfileName() {
            return "default";
        }

        @Override
        public String getChannelType() {
            return "disruptable-mock-transport-channel";
        }

        @Override
        public void sendResponse(final TransportResponse response) {
            execute(new Runnable() {

                @Override
                public void run() {
                    final ConnectionStatus connectionStatus = destinationTransport.getConnectionStatus(getLocalNode());
                    switch(connectionStatus) {
                        case CONNECTED:
                        case BLACK_HOLE_REQUESTS_ONLY:
                            handleResponse(requestId, response);
                            break;
                        case BLACK_HOLE:
                        case DISCONNECTED:
                            logger.trace("dropping response to {}: channel is {}", requestDescription, connectionStatus);
                            break;
                        default:
                            throw new AssertionError("unexpected status: " + connectionStatus);
                    }
                }

                @Override
                public String toString() {
                    return "response to " + requestDescription;
                }
            });
        }

        @Override
        public void sendResponse(Exception exception) {
            execute(new Runnable() {

                @Override
                public void run() {
                    final ConnectionStatus connectionStatus = destinationTransport.getConnectionStatus(getLocalNode());
                    switch(connectionStatus) {
                        case CONNECTED:
                        case BLACK_HOLE_REQUESTS_ONLY:
                            handleRemoteError(requestId, exception);
                            break;
                        case BLACK_HOLE:
                        case DISCONNECTED:
                            logger.trace("dropping exception response to {}: channel is {}", requestDescription, connectionStatus);
                            break;
                        default:
                            throw new AssertionError("unexpected status: " + connectionStatus);
                    }
                }

                @Override
                public String toString() {
                    return "error response to " + requestDescription;
                }
            });
        }
    };
    final TransportRequest copiedRequest;
    try {
        copiedRequest = copyWriteable(request, writeableRegistry(), requestHandler::newRequest);
    } catch (IOException e) {
        throw new AssertionError("exception de/serializing request", e);
    }
    try {
        requestHandler.processMessageReceived(copiedRequest, transportChannel);
    } catch (Exception e) {
        try {
            transportChannel.sendResponse(e);
        } catch (Exception ee) {
            logger.warn("failed to send failure", e);
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequest(org.elasticsearch.transport.TransportRequest) TransportChannel(org.elasticsearch.transport.TransportChannel) IOException(java.io.IOException) TransportResponse(org.elasticsearch.transport.TransportResponse) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

IOException (java.io.IOException)3 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)3 TransportChannel (org.elasticsearch.transport.TransportChannel)3 TransportResponse (org.elasticsearch.transport.TransportResponse)3 TransportException (org.elasticsearch.transport.TransportException)2 TransportRequest (org.elasticsearch.transport.TransportRequest)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 Role (org.elasticsearch.cluster.node.DiscoveryNode.Role)1 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)1 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)1 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 NetworkService (org.elasticsearch.common.network.NetworkService)1 Settings (org.elasticsearch.common.settings.Settings)1 ZenDiscovery.shouldIgnoreOrRejectNewClusterState (org.elasticsearch.discovery.zen.ZenDiscovery.shouldIgnoreOrRejectNewClusterState)1 ShardId (org.elasticsearch.index.shard.ShardId)1 CircuitBreakerService (org.elasticsearch.indices.breaker.CircuitBreakerService)1