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 {
}
};
}
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;
}
});
}
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;
}
});
}
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);
}
}
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."));
}
}
Aggregations