Search in sources :

Example 1 with ConnectionProfile

use of org.opensearch.transport.ConnectionProfile in project OpenSearch by opensearch-project.

the class SimpleNioTransportTests method build.

@Override
protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    NetworkService networkService = new NetworkService(Collections.emptyList());
    return new NioTransport(settings, version, threadPool, networkService, new MockPageCacheRecycler(settings), namedWriteableRegistry, new NoneCircuitBreakerService(), new NioGroupFactory(settings, logger)) {

        @Override
        public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, ActionListener<Version> listener) {
            if (doHandshake) {
                super.executeHandshake(node, channel, profile, listener);
            } else {
                listener.onResponse(version.minimumCompatibilityVersion());
            }
        }
    };
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ActionListener(org.opensearch.action.ActionListener) TcpChannel(org.opensearch.transport.TcpChannel) NetworkService(org.opensearch.common.network.NetworkService) ConnectionProfile(org.opensearch.transport.ConnectionProfile) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 2 with ConnectionProfile

use of org.opensearch.transport.ConnectionProfile in project OpenSearch by opensearch-project.

the class SimpleMockNioTransportTests method build.

@Override
protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    NetworkService networkService = new NetworkService(Collections.emptyList());
    return new MockNioTransport(settings, version, threadPool, networkService, new MockPageCacheRecycler(settings), namedWriteableRegistry, new NoneCircuitBreakerService()) {

        @Override
        public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, ActionListener<Version> listener) {
            if (doHandshake) {
                super.executeHandshake(node, channel, profile, listener);
            } else {
                listener.onResponse(version.minimumCompatibilityVersion());
            }
        }
    };
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ActionListener(org.opensearch.action.ActionListener) TcpChannel(org.opensearch.transport.TcpChannel) NetworkService(org.opensearch.common.network.NetworkService) ConnectionProfile(org.opensearch.transport.ConnectionProfile) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 3 with ConnectionProfile

use of org.opensearch.transport.ConnectionProfile in project OpenSearch by opensearch-project.

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());
            final RunOnce runnable = new RunOnce(new AbstractRunnable() {

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

                @Override
                protected void doRun() throws IOException {
                    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.opensearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) RunOnce(org.opensearch.common.util.concurrent.RunOnce) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequest(org.opensearch.transport.TransportRequest) ConnectionProfile(org.opensearch.transport.ConnectionProfile) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectTransportException(org.opensearch.transport.ConnectTransportException) IOException(java.io.IOException) RequestHandlerRegistry(org.opensearch.transport.RequestHandlerRegistry) ConnectTransportException(org.opensearch.transport.ConnectTransportException) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) Transport(org.opensearch.transport.Transport) MockNioTransport(org.opensearch.transport.nio.MockNioTransport)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 ConnectionProfile (org.opensearch.transport.ConnectionProfile)3 ActionListener (org.opensearch.action.ActionListener)2 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)2 NetworkService (org.opensearch.common.network.NetworkService)2 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)2 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)2 TcpChannel (org.opensearch.transport.TcpChannel)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1 TimeValue (org.opensearch.common.unit.TimeValue)1 AbstractRunnable (org.opensearch.common.util.concurrent.AbstractRunnable)1 RunOnce (org.opensearch.common.util.concurrent.RunOnce)1 ConnectTransportException (org.opensearch.transport.ConnectTransportException)1 RequestHandlerRegistry (org.opensearch.transport.RequestHandlerRegistry)1 Transport (org.opensearch.transport.Transport)1 TransportRequest (org.opensearch.transport.TransportRequest)1 TransportRequestOptions (org.opensearch.transport.TransportRequestOptions)1