Search in sources :

Example 21 with TransportRequestOptions

use of org.elasticsearch.transport.TransportRequestOptions 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 22 with TransportRequestOptions

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

the class RemoteRecoveryTargetHandler method cleanFiles.

@Override
public void cleanFiles(int totalTranslogOps, long globalCheckpoint, Store.MetadataSnapshot sourceMetadata, ActionListener<Void> listener) {
    final String action = PeerRecoveryTargetService.Actions.CLEAN_FILES;
    final RecoveryCleanFilesRequest request = new RecoveryCleanFilesRequest(recoveryId, shardId, sourceMetadata, totalTranslogOps, globalCheckpoint);
    final TransportRequestOptions options = TransportRequestOptions.builder().withTimeout(recoverySettings.internalActionTimeout()).build();
    final Writeable.Reader<TransportResponse.Empty> reader = in -> TransportResponse.Empty.INSTANCE;
    final ActionListener<TransportResponse.Empty> responseListener = ActionListener.map(listener, r -> null);
    executeRetryableAction(action, request, options, responseListener, reader);
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) ShardId(org.elasticsearch.index.shard.ShardId) TransportRequest(org.elasticsearch.transport.TransportRequest) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) RetentionLeases(org.elasticsearch.index.seqno.RetentionLeases) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) Store(org.elasticsearch.index.store.Store) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) TransportFuture(org.elasticsearch.transport.TransportFuture) TimeValue(io.crate.common.unit.TimeValue) Translog(org.elasticsearch.index.translog.Translog) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) RetryableAction(org.elasticsearch.action.support.RetryableAction) ReplicationTracker(org.elasticsearch.index.seqno.ReplicationTracker) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) SQLExceptions(io.crate.exceptions.SQLExceptions) Writeable(org.elasticsearch.common.io.stream.Writeable) LogManager(org.apache.logging.log4j.LogManager) RateLimiter(org.apache.lucene.store.RateLimiter) ActionListener(org.elasticsearch.action.ActionListener) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Writeable(org.elasticsearch.common.io.stream.Writeable)

Example 23 with TransportRequestOptions

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

the class RemoteRecoveryTargetHandler method prepareForTranslogOperations.

@Override
public void prepareForTranslogOperations(int totalTranslogOps, ActionListener<Void> listener) {
    final String action = PeerRecoveryTargetService.Actions.PREPARE_TRANSLOG;
    final RecoveryPrepareForTranslogOperationsRequest request = new RecoveryPrepareForTranslogOperationsRequest(recoveryId, shardId, totalTranslogOps);
    final TransportRequestOptions options = TransportRequestOptions.builder().withTimeout(recoverySettings.internalActionTimeout()).build();
    final Writeable.Reader<TransportResponse.Empty> reader = in -> TransportResponse.Empty.INSTANCE;
    final ActionListener<TransportResponse.Empty> responseListener = ActionListener.map(listener, r -> null);
    executeRetryableAction(action, request, options, responseListener, reader);
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) ShardId(org.elasticsearch.index.shard.ShardId) TransportRequest(org.elasticsearch.transport.TransportRequest) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) RetentionLeases(org.elasticsearch.index.seqno.RetentionLeases) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) Store(org.elasticsearch.index.store.Store) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) TransportFuture(org.elasticsearch.transport.TransportFuture) TimeValue(io.crate.common.unit.TimeValue) Translog(org.elasticsearch.index.translog.Translog) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler) RetryableAction(org.elasticsearch.action.support.RetryableAction) ReplicationTracker(org.elasticsearch.index.seqno.ReplicationTracker) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) SQLExceptions(io.crate.exceptions.SQLExceptions) Writeable(org.elasticsearch.common.io.stream.Writeable) LogManager(org.apache.logging.log4j.LogManager) RateLimiter(org.apache.lucene.store.RateLimiter) ActionListener(org.elasticsearch.action.ActionListener) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Writeable(org.elasticsearch.common.io.stream.Writeable)

Example 24 with TransportRequestOptions

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

the class TransportNodeStatsAction method execute.

public void execute(final String nodeName, final NodeStatsRequest request, final ActionListener<NodeStatsResponse> listener, final TimeValue timeout) {
    TransportRequestOptions options = TransportRequestOptions.builder().withTimeout(timeout).build();
    transports.sendRequest(ACTION_NAME, nodeName, request, listener, new ActionListenerResponseHandler<>(listener, NodeStatsResponse::new), options);
}
Also used : TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions)

Example 25 with TransportRequestOptions

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

the class IndexRecoveryIT method testDisconnectsDuringRecovery.

/**
 * Tests scenario where recovery target successfully sends recovery request to source but then the channel gets closed while
 * the source is working on the recovery process.
 */
@Test
public void testDisconnectsDuringRecovery() throws Exception {
    boolean primaryRelocation = randomBoolean();
    final String indexName = IndexParts.toIndexName(sqlExecutor.getCurrentSchema(), "test", null);
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), TimeValue.timeValueMillis(randomIntBetween(0, 100))).build();
    TimeValue disconnectAfterDelay = TimeValue.timeValueMillis(randomIntBetween(0, 100));
    // start a master node
    String masterNodeName = internalCluster().startMasterOnlyNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    execute("CREATE TABLE test (id int) CLUSTERED INTO 1 SHARDS " + "WITH (" + " number_of_replicas=0," + " \"routing.allocation.include.color\" = 'blue'" + ")");
    int numDocs = scaledRandomIntBetween(25, 250);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { i };
    }
    execute("INSERT INTO test (id) VALUES (?)", args);
    ensureGreen();
    refresh();
    var searchResponse = execute("SELECT COUNT(*) FROM test");
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
    MockTransportService masterTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, masterNodeName);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    redMockTransportService.addSendBehavior(blueMockTransportService, new StubbableTransport.SendRequestBehavior() {

        private final AtomicInteger count = new AtomicInteger();

        @Override
        public void sendRequest(Transport.Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
            logger.info("--> sending request {} on {}", action, connection.getNode());
            if (PeerRecoverySourceService.Actions.START_RECOVERY.equals(action) && count.incrementAndGet() == 1) {
                // ensures that it's considered as valid recovery attempt by source
                try {
                    assertBusy(() -> assertThat("Expected there to be some initializing shards", client(blueNodeName).admin().cluster().prepareState().setLocal(true).get().getState().getRoutingTable().index(indexName).shard(0).getAllInitializingShards(), not(empty())));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                connection.sendRequest(requestId, action, request, options);
                try {
                    Thread.sleep(disconnectAfterDelay.millis());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                throw new ConnectTransportException(connection.getNode(), "DISCONNECT: simulation disconnect after successfully sending " + action + " request");
            } else {
                connection.sendRequest(requestId, action, request, options);
            }
        }
    });
    final AtomicBoolean finalized = new AtomicBoolean();
    blueMockTransportService.addSendBehavior(redMockTransportService, (connection, requestId, action, request, options) -> {
        logger.info("--> sending request {} on {}", action, connection.getNode());
        if (action.equals(PeerRecoveryTargetService.Actions.FINALIZE)) {
            finalized.set(true);
        }
        connection.sendRequest(requestId, action, request, options);
    });
    for (MockTransportService mockTransportService : Arrays.asList(redMockTransportService, blueMockTransportService)) {
        mockTransportService.addSendBehavior(masterTransportService, (connection, requestId, action, request, options) -> {
            logger.info("--> sending request {} on {}", action, connection.getNode());
            if ((primaryRelocation && finalized.get()) == false) {
                assertNotEquals(action, ShardStateAction.SHARD_FAILED_ACTION_NAME);
            }
            connection.sendRequest(requestId, action, request, options);
        });
    }
    if (primaryRelocation) {
        logger.info("--> starting primary relocation recovery from blue to red");
        execute("ALTER TABLE test SET (" + " \"routing.allocation.include.color\" = 'red'" + ")");
        // also waits for relocation / recovery to complete
        ensureGreen();
        // if a primary relocation fails after the source shard has been marked as relocated, both source and target are failed. If the
        // source shard is moved back to started because the target fails first, it's possible that there is a cluster state where the
        // shard is marked as started again (and ensureGreen returns), but while applying the cluster state the primary is failed and
        // will be reallocated. The cluster will thus become green, then red, then green again. Triggering a refresh here before
        // searching helps, as in contrast to search actions, refresh waits for the closed shard to be reallocated.
        refresh();
    } else {
        logger.info("--> starting replica recovery from blue to red");
        execute("ALTER TABLE test SET (" + " number_of_replicas=1," + " \"routing.allocation.include.color\" = 'red,blue'" + ")");
        ensureGreen();
    }
    for (int i = 0; i < 10; i++) {
        searchResponse = execute("SELECT COUNT(*) FROM test");
        assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
    }
}
Also used : TransportRequest(org.elasticsearch.transport.TransportRequest) MockTransportService(org.elasticsearch.test.transport.MockTransportService) IOException(java.io.IOException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Transport(org.elasticsearch.transport.Transport) StubbableTransport(org.elasticsearch.test.transport.StubbableTransport) StubbableTransport(org.elasticsearch.test.transport.StubbableTransport) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) TimeValue(io.crate.common.unit.TimeValue) Test(org.junit.Test)

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