Search in sources :

Example 31 with TransportException

use of org.elasticsearch.transport.TransportException 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)

Example 32 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch-indexing-proxy by codelibs.

the class IndexingProxyService method writeOnRemote.

private <Request extends ActionRequest, Response extends ActionResponse> void writeOnRemote(final String nodeName, final long version, final Request request, final ActionListener<Response> listener, final int tryCount) {
    final List<DiscoveryNode> nodeList = new ArrayList<>();
    clusterService.state().nodes().getNodes().valuesIt().forEachRemaining(node -> {
        if (writerNodes.isEmpty() || writerNodes.contains(node.getName())) {
            nodeList.add(node);
        }
    });
    int pos = -1;
    for (int i = 0; i < nodeList.size(); i++) {
        if (nodeList.get(i).getName().equals(nodeName)) {
            pos = i;
            break;
        }
    }
    if (pos == -1) {
        if (tryCount >= writerRetryCount) {
            listener.onFailure(new ElasticsearchException("Writer nodes are not found for writing."));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("No available write node.");
            }
            updateWriterNode(version, nodeList, (res, ex) -> write(request, listener, tryCount + 1));
        }
        return;
    }
    final int nodeIdx = pos;
    transportService.sendRequest(nodeList.get(nodeIdx), IndexingProxyPlugin.ACTION_IDXPROXY_WRITE, new WriteRequest<>(request), new TransportResponseHandler<WriteResponse>() {

        @Override
        public WriteResponse newInstance() {
            return new WriteResponse();
        }

        @Override
        public void handleResponse(final WriteResponse response) {
            if (response.isAcknowledged()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Stored request in " + nodeName);
                }
                listener.onResponse(null);
            } else {
                throw new ElasticsearchException("Failed to store request: " + RequestUtils.getClassType(request));
            }
        }

        @Override
        public void handleException(final TransportException e) {
            if (tryCount >= writerRetryCount) {
                listener.onFailure(e);
            } else {
                final DiscoveryNode nextNode = nodeList.get((nodeIdx + 1) % nodeList.size());
                if (nextNode.getName().equals(nodeName)) {
                    if (tryCount >= writerRetryCount) {
                        listener.onFailure(e);
                    } else {
                        randomWait();
                        write(request, listener, tryCount + 1);
                    }
                } else {
                    final Map<String, Object> source = new HashMap<>();
                    source.put(IndexingProxyPlugin.NODE_NAME, nextNode.getName());
                    source.put(IndexingProxyPlugin.TIMESTAMP, new Date());
                    client.prepareUpdate(IndexingProxyPlugin.INDEX_NAME, IndexingProxyPlugin.TYPE_NAME, FILE_ID).setVersion(version).setDoc(source).setRefreshPolicy(RefreshPolicy.WAIT_UNTIL).execute(wrap(res -> {
                        write(request, listener, tryCount + 1);
                    }, ex -> {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Failed to update file_id.", ex);
                        }
                        write(request, listener, tryCount + 1);
                    }));
                }
            }
        }

        @Override
        public String executor() {
            return ThreadPool.Names.GENERIC;
        }
    });
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) WriteResponse(org.codelibs.elasticsearch.idxproxy.action.WriteResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) Date(java.util.Date) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 33 with TransportException

use of org.elasticsearch.transport.TransportException in project elasticsearch-indexing-proxy by codelibs.

the class IndexingProxyService method renewOnRemote.

private <Request extends ActionRequest, Response extends ActionResponse> void renewOnRemote(final String nodeName, final long version, final ActionListener<Response> listener, final int tryCount) {
    final List<DiscoveryNode> nodeList = new ArrayList<>();
    clusterService.state().nodes().getNodes().valuesIt().forEachRemaining(node -> {
        if (writerNodes.isEmpty() || writerNodes.contains(node.getName())) {
            nodeList.add(node);
        }
    });
    int pos = -1;
    for (int i = 0; i < nodeList.size(); i++) {
        if (nodeList.get(i).getName().equals(nodeName)) {
            pos = i;
            break;
        }
    }
    if (pos == -1) {
        if (tryCount >= writerRetryCount) {
            listener.onFailure(new ElasticsearchException("Writer nodes are not found for renew."));
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("No available write node.");
            }
            updateWriterNode(version, nodeList, (res, ex) -> renew(listener, tryCount + 1));
        }
        return;
    }
    final int nodeIdx = pos;
    transportService.sendRequest(nodeList.get(nodeIdx), IndexingProxyPlugin.ACTION_IDXPROXY_CREATE, new CreateRequest(), new TransportResponseHandler<CreateResponse>() {

        @Override
        public CreateResponse newInstance() {
            return new CreateResponse();
        }

        @Override
        public void handleResponse(final CreateResponse response) {
            if (response.isAcknowledged()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Update file_id in " + nodeName);
                }
                listener.onResponse(null);
            } else {
                throw new ElasticsearchException("Failed to update file_id in " + nodeName);
            }
        }

        @Override
        public void handleException(final TransportException e) {
            listener.onFailure(e);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.GENERIC;
        }
    });
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CreateRequest(org.codelibs.elasticsearch.idxproxy.action.CreateRequest) CreateResponse(org.codelibs.elasticsearch.idxproxy.action.CreateResponse) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException)

Example 34 with TransportException

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

the class PeerRecoveryTargetService method doRecovery.

private void doRecovery(final long recoveryId) {
    final StartRecoveryRequest request;
    final RecoveryState.Timer timer;
    CancellableThreads cancellableThreads;
    try (RecoveryRef recoveryRef = onGoingRecoveries.getRecovery(recoveryId)) {
        if (recoveryRef == null) {
            LOGGER.trace("not running recovery with id [{}] - can not find it (probably finished)", recoveryId);
            return;
        }
        final RecoveryTarget recoveryTarget = recoveryRef.target();
        timer = recoveryTarget.state().getTimer();
        cancellableThreads = recoveryTarget.cancellableThreads();
        try {
            assert recoveryTarget.sourceNode() != null : "can not do a recovery without a source node";
            LOGGER.trace("{} preparing shard for peer recovery", recoveryTarget.shardId());
            recoveryTarget.indexShard().prepareForIndexRecovery();
            final long startingSeqNo = recoveryTarget.indexShard().recoverLocallyUpToGlobalCheckpoint();
            assert startingSeqNo == UNASSIGNED_SEQ_NO || recoveryTarget.state().getStage() == RecoveryState.Stage.TRANSLOG : "unexpected recovery stage [" + recoveryTarget.state().getStage() + "] starting seqno [ " + startingSeqNo + "]";
            request = getStartRecoveryRequest(LOGGER, clusterService.localNode(), recoveryTarget, startingSeqNo);
        } catch (final Exception e) {
            // this will be logged as warning later on...
            LOGGER.trace("unexpected error while preparing shard for peer recovery, failing recovery", e);
            onGoingRecoveries.failRecovery(recoveryId, new RecoveryFailedException(recoveryTarget.state(), "failed to prepare shard for recovery", e), true);
            return;
        }
    }
    Consumer<Exception> handleException = e -> {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(() -> new ParameterizedMessage("[{}][{}] Got exception on recovery", request.shardId().getIndex().getName(), request.shardId().id()), e);
        }
        Throwable cause = SQLExceptions.unwrap(e);
        if (cause instanceof CancellableThreads.ExecutionCancelledException) {
            // this can also come from the source wrapped in a RemoteTransportException
            onGoingRecoveries.failRecovery(recoveryId, new RecoveryFailedException(request, "source has canceled the recovery", cause), false);
            return;
        }
        if (cause instanceof RecoveryEngineException) {
            // unwrap an exception that was thrown as part of the recovery
            cause = cause.getCause();
        }
        // do it twice, in case we have double transport exception
        cause = SQLExceptions.unwrap(cause);
        if (cause instanceof RecoveryEngineException) {
            // unwrap an exception that was thrown as part of the recovery
            cause = cause.getCause();
        }
        if (cause instanceof IllegalIndexShardStateException || cause instanceof IndexNotFoundException || cause instanceof ShardNotFoundException) {
            // if the target is not ready yet, retry
            retryRecovery(recoveryId, "remote shard not ready", recoverySettings.retryDelayStateSync(), recoverySettings.activityTimeout());
            return;
        }
        if (cause instanceof DelayRecoveryException) {
            retryRecovery(recoveryId, cause, recoverySettings.retryDelayStateSync(), recoverySettings.activityTimeout());
            return;
        }
        if (cause instanceof ConnectTransportException) {
            LOGGER.debug("delaying recovery of {} for [{}] due to networking error [{}]", request.shardId(), recoverySettings.retryDelayNetwork(), cause.getMessage());
            retryRecovery(recoveryId, cause.getMessage(), recoverySettings.retryDelayNetwork(), recoverySettings.activityTimeout());
            return;
        }
        if (cause instanceof AlreadyClosedException) {
            onGoingRecoveries.failRecovery(recoveryId, new RecoveryFailedException(request, "source shard is closed", cause), false);
            return;
        }
        onGoingRecoveries.failRecovery(recoveryId, new RecoveryFailedException(request, e), true);
    };
    try {
        LOGGER.trace("{} starting recovery from {}", request.shardId(), request.sourceNode());
        cancellableThreads.executeIO(() -> transportService.sendRequest(request.sourceNode(), PeerRecoverySourceService.Actions.START_RECOVERY, request, new TransportResponseHandler<RecoveryResponse>() {

            @Override
            public void handleResponse(RecoveryResponse recoveryResponse) {
                final TimeValue recoveryTime = new TimeValue(timer.time());
                // do this through ongoing recoveries to remove it from the collection
                onGoingRecoveries.markRecoveryAsDone(recoveryId);
                if (LOGGER.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append('[').append(request.shardId().getIndex().getName()).append(']').append('[').append(request.shardId().id()).append("] ");
                    sb.append("recovery completed from ").append(request.sourceNode()).append(", took[").append(recoveryTime).append("]\n");
                    sb.append("   phase1: recovered_files [").append(recoveryResponse.phase1FileNames.size()).append("]").append(" with total_size of [").append(new ByteSizeValue(recoveryResponse.phase1TotalSize)).append("]").append(", took [").append(timeValueMillis(recoveryResponse.phase1Time)).append("], throttling_wait [").append(timeValueMillis(recoveryResponse.phase1ThrottlingWaitTime)).append(']').append("\n");
                    sb.append("         : reusing_files   [").append(recoveryResponse.phase1ExistingFileNames.size()).append("] with total_size of [").append(new ByteSizeValue(recoveryResponse.phase1ExistingTotalSize)).append("]\n");
                    sb.append("   phase2: start took [").append(timeValueMillis(recoveryResponse.startTime)).append("]\n");
                    sb.append("         : recovered [").append(recoveryResponse.phase2Operations).append("]").append(" transaction log operations").append(", took [").append(timeValueMillis(recoveryResponse.phase2Time)).append("]").append("\n");
                    LOGGER.trace("{}", sb);
                } else {
                    LOGGER.debug("{} recovery done from [{}], took [{}]", request.shardId(), request.sourceNode(), recoveryTime);
                }
            }

            @Override
            public void handleException(TransportException e) {
                handleException.accept(e);
            }

            @Override
            public String executor() {
                // we do some heavy work like refreshes in the response so fork off to the generic threadpool
                return ThreadPool.Names.GENERIC;
            }

            @Override
            public RecoveryResponse read(StreamInput in) throws IOException {
                return new RecoveryResponse(in);
            }
        }));
    } catch (CancellableThreads.ExecutionCancelledException e) {
        LOGGER.trace("recovery cancelled", e);
    } catch (Exception e) {
        handleException.accept(e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) ShardId(org.elasticsearch.index.shard.ShardId) TransportChannel(org.elasticsearch.transport.TransportChannel) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) ClusterService(org.elasticsearch.cluster.service.ClusterService) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Settings(org.elasticsearch.common.settings.Settings) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Store(org.elasticsearch.index.store.Store) ChannelActionListener(org.elasticsearch.action.support.ChannelActionListener) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) Nullable(javax.annotation.Nullable) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) RecoveryRef(org.elasticsearch.indices.recovery.RecoveriesCollection.RecoveryRef) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) IndexShard(org.elasticsearch.index.shard.IndexShard) UNASSIGNED_SEQ_NO(org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO) IOException(java.io.IOException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) Logger(org.apache.logging.log4j.Logger) TimeValue.timeValueMillis(io.crate.common.unit.TimeValue.timeValueMillis) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TimeValue(io.crate.common.unit.TimeValue) Translog(org.elasticsearch.index.translog.Translog) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) SQLExceptions(io.crate.exceptions.SQLExceptions) LogManager(org.apache.logging.log4j.LogManager) TransportException(org.elasticsearch.transport.TransportException) RateLimiter(org.apache.lucene.store.RateLimiter) ActionListener(org.elasticsearch.action.ActionListener) MapperException(org.elasticsearch.index.mapper.MapperException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) RecoveryRef(org.elasticsearch.indices.recovery.RecoveriesCollection.RecoveryRef) TimeValue(io.crate.common.unit.TimeValue) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) TranslogCorruptedException(org.elasticsearch.index.translog.TranslogCorruptedException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) TransportException(org.elasticsearch.transport.TransportException) MapperException(org.elasticsearch.index.mapper.MapperException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) StreamInput(org.elasticsearch.common.io.stream.StreamInput) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 35 with TransportException

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

the class Netty4TcpChannel method sendMessage.

@Override
public void sendMessage(BytesReference reference, ActionListener<Void> listener) {
    ChannelPromise writePromise = channel.newPromise();
    writePromise.addListener(f -> {
        if (f.isSuccess()) {
            listener.onResponse(null);
        } else {
            final Throwable cause = f.cause();
            ExceptionsHelper.maybeDieOnAnotherThread(cause);
            assert cause instanceof Exception;
            listener.onFailure((Exception) cause);
        }
    });
    channel.writeAndFlush(Netty4Utils.toByteBuf(reference), writePromise);
    if (channel.eventLoop().isShutdown()) {
        listener.onFailure(new TransportException("Cannot send message, event loop is shutting down."));
    }
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) TransportException(org.elasticsearch.transport.TransportException) TransportException(org.elasticsearch.transport.TransportException)

Aggregations

TransportException (org.elasticsearch.transport.TransportException)46 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 IOException (java.io.IOException)28 StreamInput (org.elasticsearch.common.io.stream.StreamInput)17 ElasticsearchException (org.elasticsearch.ElasticsearchException)16 ClusterState (org.elasticsearch.cluster.ClusterState)16 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)15 TransportResponse (org.elasticsearch.transport.TransportResponse)14 TransportResponseHandler (org.elasticsearch.transport.TransportResponseHandler)12 TransportService (org.elasticsearch.transport.TransportService)12 Settings (org.elasticsearch.common.settings.Settings)10 ClusterService (org.elasticsearch.cluster.service.ClusterService)9 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)9 EmptyTransportResponseHandler (org.elasticsearch.transport.EmptyTransportResponseHandler)9 Version (org.elasticsearch.Version)8 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)8 TimeValue (io.crate.common.unit.TimeValue)7 ArrayList (java.util.ArrayList)7 Consumer (java.util.function.Consumer)7 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)7