Skip to content

Commit db90b71

Browse files
Introduce RemoteComputeException that forwards the cause's status code
1 parent bc166cc commit db90b71

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/CrossClusterCancellationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,6 @@ public void testCancelSkipUnavailable() throws Exception {
284284
}
285285

286286
Exception error = expectThrows(Exception.class, requestFuture::actionGet);
287-
assertThat(error, instanceOf(TaskCancelledException.class));
287+
assertThat(error.getCause(), instanceOf(TaskCancelledException.class));
288288
}
289289
}

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/CrossClusterQueryIT.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.transport.TransportService;
3030
import org.elasticsearch.xcontent.XContentBuilder;
3131
import org.elasticsearch.xcontent.json.JsonXContent;
32+
import org.elasticsearch.xpack.esql.RemoteComputeException;
3233
import org.elasticsearch.xpack.esql.VerificationException;
3334
import org.elasticsearch.xpack.esql.plugin.QueryPragmas;
3435

@@ -813,10 +814,13 @@ public void testRemoteFailureSkipUnavailableTrue() throws IOException {
813814
Map<String, Object> testClusterInfo = setupFailClusters();
814815
String localIndex = (String) testClusterInfo.get("local.index");
815816
String remote1Index = (String) testClusterInfo.get("remote.index");
816-
int localNumShards = (Integer) testClusterInfo.get("local.num_shards");
817817
String q = Strings.format("FROM %s,cluster-a:%s*", localIndex, remote1Index);
818-
IllegalStateException e = expectThrows(IllegalStateException.class, () -> runQuery(q, false));
819-
assertThat(e.getMessage(), containsString("Accessing failing field"));
818+
819+
RemoteComputeException rce = expectThrows(RemoteComputeException.class, () -> runQuery(q, false));
820+
Throwable t = rce.getCause();
821+
822+
assertThat(t, instanceOf(IllegalStateException.class));
823+
assertThat(t.getMessage(), containsString("Accessing failing field"));
820824
}
821825

822826
private static void assertClusterMetadataInResponse(EsqlQueryResponse resp, boolean responseExpectMeta) {

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/CrossClusterQueryWithPartialResultsIT.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.xcontent.XContentBuilder;
2525
import org.elasticsearch.xcontent.json.JsonXContent;
2626
import org.elasticsearch.xpack.esql.EsqlTestUtils;
27+
import org.elasticsearch.xpack.esql.RemoteComputeException;
2728
import org.elasticsearch.xpack.esql.plugin.ComputeService;
2829

2930
import java.io.IOException;
@@ -40,6 +41,7 @@
4041
import static org.hamcrest.Matchers.equalTo;
4142
import static org.hamcrest.Matchers.greaterThan;
4243
import static org.hamcrest.Matchers.in;
44+
import static org.hamcrest.Matchers.instanceOf;
4345
import static org.hamcrest.Matchers.is;
4446
import static org.hamcrest.Matchers.lessThanOrEqualTo;
4547

@@ -73,8 +75,11 @@ public void testPartialResults() throws Exception {
7375
request.includeCCSMetadata(randomBoolean());
7476
{
7577
request.allowPartialResults(false);
76-
IllegalStateException error = expectThrows(IllegalStateException.class, () -> runQuery(request).close());
77-
assertThat(error.getMessage(), containsString("Accessing failing field"));
78+
RemoteComputeException rce = expectThrows(RemoteComputeException.class, () -> runQuery(request).close());
79+
Throwable t = rce.getCause();
80+
81+
assertThat(t, instanceOf(IllegalStateException.class));
82+
assertThat(t.getMessage(), containsString("Accessing failing field"));
7883
}
7984
request.allowPartialResults(true);
8085
try (var resp = runQuery(request)) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.xpack.esql;
11+
12+
import org.elasticsearch.ElasticsearchException;
13+
import org.elasticsearch.ExceptionsHelper;
14+
import org.elasticsearch.rest.RestStatus;
15+
16+
import java.util.Objects;
17+
18+
/**
19+
* Represents an error that occurred when starting compute on a remote node.
20+
* It allows capturing some context such as the cluster alias that encountered the error.
21+
*/
22+
public class RemoteComputeException extends ElasticsearchException {
23+
24+
/**
25+
* @param clusterAlias Name of the cluster.
26+
* @param cause Error that was encountered.
27+
*/
28+
public RemoteComputeException(String clusterAlias, Throwable cause) {
29+
super("Remote [" + clusterAlias + "] encountered an error", cause);
30+
Objects.requireNonNull(cause);
31+
}
32+
33+
@Override
34+
public RestStatus status() {
35+
// This is similar to what we do in SearchPhaseExecutionException.
36+
return ExceptionsHelper.status(getCause());
37+
}
38+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plugin/ComputeService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
package org.elasticsearch.xpack.esql.plugin;
99

10-
import org.elasticsearch.ElasticsearchException;
1110
import org.elasticsearch.action.ActionListener;
1211
import org.elasticsearch.action.OriginalIndices;
1312
import org.elasticsearch.action.search.SearchRequest;
@@ -41,6 +40,7 @@
4140
import org.elasticsearch.transport.RemoteClusterAware;
4241
import org.elasticsearch.transport.TransportRequest;
4342
import org.elasticsearch.transport.TransportService;
43+
import org.elasticsearch.xpack.esql.RemoteComputeException;
4444
import org.elasticsearch.xpack.esql.action.EsqlExecutionInfo;
4545
import org.elasticsearch.xpack.esql.action.EsqlQueryAction;
4646
import org.elasticsearch.xpack.esql.core.expression.Attribute;
@@ -301,9 +301,7 @@ public void execute(
301301
cancelQueryOnFailure,
302302
execInfo,
303303
computeListener.acquireCompute()
304-
.delegateResponse(
305-
(l, ex) -> l.onFailure(new ElasticsearchException(cluster.clusterAlias() + " encountered an error", ex))
306-
)
304+
.delegateResponse((l, ex) -> l.onFailure(new RemoteComputeException(cluster.clusterAlias(), ex)))
307305
);
308306
}
309307
}

0 commit comments

Comments
 (0)