Search in sources :

Example 11 with TransportRequest

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

the class IndicesRequestIT method assertSameIndices.

private static void assertSameIndices(IndicesRequest originalRequest, boolean optional, String... actions) {
    for (String action : actions) {
        List<TransportRequest> requests = consumeTransportRequests(action);
        if (!optional) {
            assertThat("no internal requests intercepted for action [" + action + "]", requests.size(), greaterThan(0));
        }
        for (TransportRequest internalRequest : requests) {
            IndicesRequest indicesRequest = convertRequest(internalRequest);
            assertThat(internalRequest.getClass().getName(), indicesRequest.indices(), equalTo(originalRequest.indices()));
            assertThat(indicesRequest.indicesOptions(), equalTo(originalRequest.indicesOptions()));
        }
    }
}
Also used : TransportRequest(org.elasticsearch.transport.TransportRequest)

Example 12 with TransportRequest

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

the class IndicesRequestIT method assertIndicesSubset.

private static void assertIndicesSubset(List<String> indices, String... actions) {
    //indices returned by each bulk shard request need to be a subset of the original indices
    for (String action : actions) {
        List<TransportRequest> requests = consumeTransportRequests(action);
        assertThat("no internal requests intercepted for action [" + action + "]", requests.size(), greaterThan(0));
        for (TransportRequest internalRequest : requests) {
            IndicesRequest indicesRequest = convertRequest(internalRequest);
            for (String index : indicesRequest.indices()) {
                assertThat(indices, hasItem(index));
            }
        }
    }
}
Also used : TransportRequest(org.elasticsearch.transport.TransportRequest)

Example 13 with TransportRequest

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

the class MockTransportService method addUnresponsiveRule.

/**
     * Adds a rule that will cause ignores each send request, simulating an unresponsive node
     * and failing to connect once the rule was added.
     *
     * @param duration the amount of time to delay sending and connecting.
     */
public void addUnresponsiveRule(TransportAddress transportAddress, final TimeValue duration) {
    final long startTime = System.currentTimeMillis();
    addDelegate(transportAddress, new ClearableTransport(original) {

        private final Queue<Runnable> requestsToSendWhenCleared = new LinkedBlockingDeque<Runnable>();

        private boolean cleared = false;

        TimeValue getDelay() {
            return new TimeValue(duration.millis() - (System.currentTimeMillis() - startTime));
        }

        @Override
        public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Connection, ConnectionProfile, IOException> connectionValidator) throws ConnectTransportException {
            if (original.nodeConnected(node)) {
                // connecting to an already connected node is a no-op
                return;
            }
            TimeValue delay = getDelay();
            if (delay.millis() <= 0) {
                original.connectToNode(node, connectionProfile, connectionValidator);
                return;
            }
            // TODO: Replace with proper setting
            TimeValue connectingTimeout = NetworkService.TcpSettings.TCP_CONNECT_TIMEOUT.getDefault(Settings.EMPTY);
            try {
                if (delay.millis() < connectingTimeout.millis()) {
                    Thread.sleep(delay.millis());
                    original.connectToNode(node, connectionProfile, connectionValidator);
                } else {
                    Thread.sleep(connectingTimeout.millis());
                    throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
                }
            } catch (InterruptedException e) {
                throw new ConnectTransportException(node, "UNRESPONSIVE: simulated");
            }
        }

        @Override
        protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            // delayed sending - even if larger then the request timeout to simulated a potential late response from target node
            TimeValue delay = getDelay();
            if (delay.millis() <= 0) {
                connection.sendRequest(requestId, action, request, options);
                return;
            }
            // poor mans request cloning...
            RequestHandlerRegistry reg = MockTransportService.this.getRequestHandler(action);
            BytesStreamOutput bStream = new BytesStreamOutput();
            request.writeTo(bStream);
            final TransportRequest clonedRequest = reg.newRequest();
            clonedRequest.readFrom(bStream.bytes().streamInput());
            Runnable runnable = new AbstractRunnable() {

                AtomicBoolean requestSent = new AtomicBoolean();

                @Override
                public void onFailure(Exception e) {
                    logger.debug("failed to send delayed request", e);
                }

                @Override
                protected void doRun() throws IOException {
                    if (requestSent.compareAndSet(false, true)) {
                        connection.sendRequest(requestId, action, clonedRequest, options);
                    }
                }
            };
            // store the request to send it once the rule is cleared.
            synchronized (this) {
                if (cleared) {
                    runnable.run();
                } else {
                    requestsToSendWhenCleared.add(runnable);
                    threadPool.schedule(delay, ThreadPool.Names.GENERIC, runnable);
                }
            }
        }

        @Override
        public void clearRule() {
            synchronized (this) {
                assert cleared == false;
                cleared = true;
                requestsToSendWhenCleared.forEach(Runnable::run);
            }
        }
    });
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) TransportRequest(org.elasticsearch.transport.TransportRequest) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RequestHandlerRegistry(org.elasticsearch.transport.RequestHandlerRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 14 with TransportRequest

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

the class ClusterInfoServiceIT method testClusterInfoServiceInformationClearOnError.

public void testClusterInfoServiceInformationClearOnError() throws InterruptedException, ExecutionException {
    internalCluster().startNodes(2, // manually control publishing
    Settings.builder().put(InternalClusterInfoService.INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.getKey(), "60m").build());
    prepareCreate("test").setSettings(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).get();
    ensureGreen("test");
    InternalTestCluster internalTestCluster = internalCluster();
    InternalClusterInfoService infoService = (InternalClusterInfoService) internalTestCluster.getInstance(ClusterInfoService.class, internalTestCluster.getMasterName());
    // get one healthy sample
    ClusterInfo info = infoService.refresh();
    assertNotNull("failed to collect info", info);
    assertThat("some usages are populated", info.getNodeLeastAvailableDiskUsages().size(), Matchers.equalTo(2));
    assertThat("some shard sizes are populated", info.shardSizes.size(), greaterThan(0));
    MockTransportService mockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, internalTestCluster.getMasterName());
    final AtomicBoolean timeout = new AtomicBoolean(false);
    final Set<String> blockedActions = newHashSet(NodesStatsAction.NAME, NodesStatsAction.NAME + "[n]", IndicesStatsAction.NAME, IndicesStatsAction.NAME + "[n]");
    // drop all outgoing stats requests to force a timeout.
    for (DiscoveryNode node : internalTestCluster.clusterService().state().getNodes()) {
        mockTransportService.addDelegate(internalTestCluster.getInstance(TransportService.class, node.getName()), new MockTransportService.DelegateTransport(mockTransportService.original()) {

            @Override
            protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
                if (blockedActions.contains(action)) {
                    if (timeout.get()) {
                        logger.info("dropping [{}] to [{}]", action, node);
                        return;
                    }
                }
                super.sendRequest(connection, requestId, action, request, options);
            }
        });
    }
    // timeouts shouldn't clear the info
    timeout.set(true);
    info = infoService.refresh();
    assertNotNull("info should not be null", info);
    // node info will time out both on the request level on the count down latch. this means
    // it is likely to update the node disk usage based on the one response that came be from local
    // node.
    assertThat(info.getNodeLeastAvailableDiskUsages().size(), greaterThanOrEqualTo(1));
    assertThat(info.getNodeMostAvailableDiskUsages().size(), greaterThanOrEqualTo(1));
    // indices is guaranteed to time out on the latch, not updating anything.
    assertThat(info.shardSizes.size(), greaterThan(1));
    // now we cause an exception
    timeout.set(false);
    ActionFilters actionFilters = internalTestCluster.getInstance(ActionFilters.class, internalTestCluster.getMasterName());
    BlockingActionFilter blockingActionFilter = null;
    for (ActionFilter filter : actionFilters.filters()) {
        if (filter instanceof BlockingActionFilter) {
            blockingActionFilter = (BlockingActionFilter) filter;
            break;
        }
    }
    assertNotNull("failed to find BlockingActionFilter", blockingActionFilter);
    blockingActionFilter.blockActions(blockedActions.toArray(Strings.EMPTY_ARRAY));
    info = infoService.refresh();
    assertNotNull("info should not be null", info);
    assertThat(info.getNodeLeastAvailableDiskUsages().size(), equalTo(0));
    assertThat(info.getNodeMostAvailableDiskUsages().size(), equalTo(0));
    assertThat(info.shardSizes.size(), equalTo(0));
    // check we recover
    blockingActionFilter.blockActions();
    info = infoService.refresh();
    assertNotNull("info should not be null", info);
    assertThat(info.getNodeLeastAvailableDiskUsages().size(), equalTo(2));
    assertThat(info.getNodeMostAvailableDiskUsages().size(), equalTo(2));
    assertThat(info.shardSizes.size(), greaterThan(0));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) IOException(java.io.IOException) ActionFilters(org.elasticsearch.action.support.ActionFilters) ActionFilter(org.elasticsearch.action.support.ActionFilter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions)

Example 15 with TransportRequest

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

the class FailAndRetryMockTransport method getConnection.

@Override
public Connection getConnection(DiscoveryNode node) {
    return new Connection() {

        @Override
        public DiscoveryNode getNode() {
            return node;
        }

        @Override
        public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException, TransportException {
            //we make sure that nodes get added to the connected ones when calling addTransportAddress, by returning proper nodes info
            if (connectMode) {
                if (TransportLivenessAction.NAME.equals(action)) {
                    TransportResponseHandler transportResponseHandler = transportServiceAdapter.onResponseReceived(requestId);
                    transportResponseHandler.handleResponse(new LivenessResponse(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY), node));
                } else if (ClusterStateAction.NAME.equals(action)) {
                    TransportResponseHandler transportResponseHandler = transportServiceAdapter.onResponseReceived(requestId);
                    ClusterState clusterState = getMockClusterState(node);
                    transportResponseHandler.handleResponse(new ClusterStateResponse(clusterName, clusterState, 0L));
                } else {
                    throw new UnsupportedOperationException("Mock transport does not understand action " + action);
                }
                return;
            }
            //once nodes are connected we'll just return errors for each sendRequest call
            triedNodes.add(node);
            if (random.nextInt(100) > 10) {
                connectTransportExceptions.incrementAndGet();
                throw new ConnectTransportException(node, "node not available");
            } else {
                if (random.nextBoolean()) {
                    failures.incrementAndGet();
                    //throw whatever exception that is not a subclass of ConnectTransportException
                    throw new IllegalStateException();
                } else {
                    TransportResponseHandler transportResponseHandler = transportServiceAdapter.onResponseReceived(requestId);
                    if (random.nextBoolean()) {
                        successes.incrementAndGet();
                        transportResponseHandler.handleResponse(newResponse());
                    } else {
                        failures.incrementAndGet();
                        transportResponseHandler.handleException(new TransportException("transport exception"));
                    }
                }
            }
        }

        @Override
        public void close() throws IOException {
        }
    };
}
Also used : LivenessResponse(org.elasticsearch.action.admin.cluster.node.liveness.LivenessResponse) ClusterState(org.elasticsearch.cluster.ClusterState) TransportRequest(org.elasticsearch.transport.TransportRequest) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

TransportRequest (org.elasticsearch.transport.TransportRequest)17 TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)12 IOException (java.io.IOException)10 MockTransportService (org.elasticsearch.test.transport.MockTransportService)9 TransportService (org.elasticsearch.transport.TransportService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Settings (org.elasticsearch.common.settings.Settings)5 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)5 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)4 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 RecoveryFileChunkRequest (org.elasticsearch.indices.recovery.RecoveryFileChunkRequest)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)2