Skip to content

Commit

Permalink
Moved isRemoteUnavailableException to ExceptionsHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
quux00 committed Sep 12, 2024
1 parent 38b4788 commit 0195ac2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
26 changes: 25 additions & 1 deletion server/src/main/java/org/elasticsearch/ExceptionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.Index;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.ConnectTransportException;
import org.elasticsearch.transport.NoSeedNodeLeftException;
import org.elasticsearch.transport.NoSuchRemoteClusterException;
import org.elasticsearch.xcontent.XContentParseException;

import java.io.IOException;
Expand Down Expand Up @@ -471,7 +474,7 @@ public static ShardOperationFailedException[] groupBy(ShardOperationFailedExcept
}

/**
* Utility method useful for determine whether to log an Exception or perhaps
* Utility method useful for determining whether to log an Exception or perhaps
* avoid logging a stacktrace if the caller/logger is not interested in these
* types of node/shard issues.
*
Expand All @@ -489,6 +492,27 @@ public static boolean isNodeOrShardUnavailableTypeException(Throwable t) {
|| t instanceof org.elasticsearch.cluster.block.ClusterBlockException);
}

/**
* Checks the exception against a known list of exceptions that indicate a remote cluster
* cannot be connected to.
*
* @param e Exception to inspect
* @return true if the Exception is known to indicate that a remote cluster
* is unavailable (cannot be connected to by the transport layer)
*/
public static boolean isRemoteUnavailableException(Exception e) {
Throwable unwrap = unwrap(e, ConnectTransportException.class, NoSuchRemoteClusterException.class, NoSeedNodeLeftException.class);
if (unwrap != null) {
return true;
}
Throwable ill = unwrap(e, IllegalStateException.class, IllegalArgumentException.class);
if (ill != null && (ill.getMessage().contains("Unable to open any connections") || ill.getMessage().contains("unknown host"))) {
return true;
}
// doesn't look like any of the known remote exceptions
return false;
}

private static class GroupBy {
final String reason;
final String index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.query.SearchTimeoutException;
import org.elasticsearch.tasks.TaskCancelledException;
import org.elasticsearch.transport.ConnectTransportException;
import org.elasticsearch.transport.NoSeedNodeLeftException;
import org.elasticsearch.transport.NoSuchRemoteClusterException;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -117,7 +114,7 @@ public static Result getFailureType(Exception e) {
if (unwrapped instanceof Exception) {
e = (Exception) unwrapped;
}
if (isRemoteUnavailable(e)) {
if (ExceptionsHelper.isRemoteUnavailableException(e)) {
return Result.REMOTES_UNAVAILABLE;
}
if (ExceptionsHelper.unwrap(e, ResourceNotFoundException.class) != null) {
Expand Down Expand Up @@ -148,27 +145,6 @@ public static Result getFailureType(Exception e) {
return Result.UNKNOWN;
}

/**
* Is this failure exception because remote was unavailable?
* See also: TransportResolveClusterAction#notConnectedError
*/
static boolean isRemoteUnavailable(Exception e) {
if (ExceptionsHelper.unwrap(
e,
ConnectTransportException.class,
NoSuchRemoteClusterException.class,
NoSeedNodeLeftException.class
) != null) {
return true;
}
Throwable ill = ExceptionsHelper.unwrap(e, IllegalStateException.class, IllegalArgumentException.class);
if (ill != null && (ill.getMessage().contains("Unable to open any connections") || ill.getMessage().contains("unknown host"))) {
return true;
}
// Ok doesn't look like any of the known remote exceptions
return false;
}

/**
* Is this failure coming from a remote cluster?
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.ConnectTransportException;
import org.elasticsearch.transport.NoSeedNodeLeftException;
import org.elasticsearch.transport.NoSuchRemoteClusterException;
import org.elasticsearch.transport.RemoteClusterAware;
import org.elasticsearch.transport.RemoteClusterService;
import org.elasticsearch.transport.TransportService;
Expand Down Expand Up @@ -171,7 +168,7 @@ public void onFailure(Exception failure) {
releaseResourcesOnCancel(clusterInfoMap);
return;
}
if (notConnectedError(failure)) {
if (ExceptionsHelper.isRemoteUnavailableException((failure))) {
clusterInfoMap.put(clusterAlias, new ResolveClusterInfo(false, skipUnavailable));
} else if (ExceptionsHelper.unwrap(
failure,
Expand Down Expand Up @@ -245,27 +242,6 @@ public void onFailure(Exception e) {
}
}

/**
* Checks the exception against a known list of exceptions that indicate a remote cluster
* cannot be connected to.
*/
private boolean notConnectedError(Exception e) {
if (e instanceof ConnectTransportException || e instanceof NoSuchRemoteClusterException) {
return true;
}
if (e instanceof IllegalStateException && e.getMessage().contains("Unable to open any connections")) {
return true;
}
Throwable ill = ExceptionsHelper.unwrap(e, IllegalArgumentException.class);
if (ill != null && ill.getMessage().contains("unknown host")) {
return true;
}
if (ExceptionsHelper.unwrap(e, NoSeedNodeLeftException.class) != null) {
return true;
}
return false;
}

/**
* Checks whether the local cluster has any matching indices (non-closed), aliases or data streams for
* the index expression captured in localIndices.
Expand Down

0 comments on commit 0195ac2

Please sign in to comment.