Search in sources :

Example 26 with TransportRequestOptions

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

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();
    Supplier<TimeValue> delaySupplier = () -> new TimeValue(duration.millis() - (System.currentTimeMillis() - startTime));
    transport().addConnectBehavior(transportAddress, new StubbableTransport.OpenConnectionBehavior() {

        private CountDownLatch stopLatch = new CountDownLatch(1);

        @Override
        public void openConnection(Transport transport, DiscoveryNode discoveryNode, ConnectionProfile profile, ActionListener<Transport.Connection> listener) {
            TimeValue delay = delaySupplier.get();
            if (delay.millis() <= 0) {
                original.openConnection(discoveryNode, profile, listener);
                return;
            }
            // TODO: Replace with proper setting
            TimeValue connectingTimeout = TransportSettings.CONNECT_TIMEOUT.getDefault(Settings.EMPTY);
            try {
                if (delay.millis() < connectingTimeout.millis()) {
                    stopLatch.await(delay.millis(), TimeUnit.MILLISECONDS);
                    original.openConnection(discoveryNode, profile, listener);
                } else {
                    stopLatch.await(connectingTimeout.millis(), TimeUnit.MILLISECONDS);
                    listener.onFailure(new ConnectTransportException(discoveryNode, "UNRESPONSIVE: simulated"));
                }
            } catch (InterruptedException e) {
                listener.onFailure(new ConnectTransportException(discoveryNode, "UNRESPONSIVE: simulated"));
            }
        }

        @Override
        public void clearCallback() {
            stopLatch.countDown();
        }
    });
    transport().addSendBehavior(transportAddress, new StubbableTransport.SendRequestBehavior() {

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

        private boolean cleared = false;

        @Override
        public void sendRequest(Transport.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 = delaySupplier.get();
            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(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(runnable, delay, ThreadPool.Names.GENERIC);
                }
            }
        }

        @Override
        public void clearCallback() {
            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) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) TimeValue(io.crate.common.unit.TimeValue) TransportRequest(org.elasticsearch.transport.TransportRequest) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IOException(java.io.IOException) RequestHandlerRegistry(org.elasticsearch.transport.RequestHandlerRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Transport(org.elasticsearch.transport.Transport) TcpTransport(org.elasticsearch.transport.TcpTransport) Netty4Transport(org.elasticsearch.transport.netty4.Netty4Transport)

Aggregations

TransportRequestOptions (org.elasticsearch.transport.TransportRequestOptions)26 IOException (java.io.IOException)21 TransportRequest (org.elasticsearch.transport.TransportRequest)19 TransportService (org.elasticsearch.transport.TransportService)15 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)11 MockTransportService (org.elasticsearch.test.transport.MockTransportService)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)8 TimeValue (io.crate.common.unit.TimeValue)6 ArrayList (java.util.ArrayList)6 Settings (org.elasticsearch.common.settings.Settings)6 EmptyTransportResponseHandler (org.elasticsearch.transport.EmptyTransportResponseHandler)6 TransportResponse (org.elasticsearch.transport.TransportResponse)6 List (java.util.List)5 Map (java.util.Map)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 Logger (org.apache.logging.log4j.Logger)5 RateLimiter (org.apache.lucene.store.RateLimiter)5