Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report failures on partial results #124823

Merged
merged 9 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/124823.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 124823
summary: Report failures on partial results
area: "ES|QL"
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.concurrent.atomic.AtomicLong;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

Expand Down Expand Up @@ -143,7 +144,27 @@ protected static void assertClusterMetadataInResponse(EsqlQueryResponse resp, bo
assertThat((int) inner.get("total"), equalTo(numClusters));
assertTrue(inner.containsKey("details"));
} else {
assertNull(clusters);
final Object partial = esqlResponseAsMap.get("is_partial");
if (partial != null && (Boolean) partial) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests that exercise this new assertion path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipped is partial too, so all results with skipped clusters would go this route.

// If we have partial response, we could have cluster metadata, it should contain details.
// Details should not be empty, and it should contain clusters with failures.
if (clusters != null) {
@SuppressWarnings("unchecked")
Map<String, Object> inner = (Map<String, Object>) clusters;
assertThat(inner, aMapWithSize(1));
assertTrue(inner.containsKey("details"));
@SuppressWarnings("unchecked")
Map<String, Object> details = (Map<String, Object>) inner.get("details");
assertThat(details.size(), greaterThanOrEqualTo(1));
details.forEach((k, v) -> {
@SuppressWarnings("unchecked")
Map<String, Object> cluster = (Map<String, Object>) v;
assertTrue(cluster.containsKey("failures"));
});
}
} else {
assertNull(clusters);
}
}
} catch (IOException e) {
fail("Could not convert ESQLQueryResponse to Map: " + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ public boolean isCrossClusterSearch() {
|| clusterInfo.size() == 1 && clusterInfo.containsKey(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY) == false;
}

/**
* Is there any metadata to report in the response?
* This is true on cross-cluster search with includeCCSMetadata=true or when there are partial failures.
*/
public boolean hasMetadataToReport() {
return isCrossClusterSearch() && includeCCSMetadata
|| (isPartial && clusterInfo.values().stream().anyMatch(c -> c.getFailures().isEmpty() == false));
}

public Cluster getCluster(String clusterAlias) {
return clusterInfo.get(clusterAlias);
}
Expand Down Expand Up @@ -257,9 +266,13 @@ public Cluster swapCluster(String clusterAlias, BiFunction<String, Cluster, Clus

@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
if (isCrossClusterSearch() == false || clusterInfo.isEmpty()) {
if (clusterInfo.isEmpty()) {
return Collections.emptyIterator();
}
if (includeCCSMetadata == false) {
// If includeCCSMetadata is false, the only reason we're here is partial failures, so just report them.
return onlyFailuresToXContent();
}
Map<Cluster.Status, Integer> clusterStatuses = new EnumMap<>(Cluster.Status.class);
for (Cluster info : clusterInfo.values()) {
clusterStatuses.merge(info.getStatus(), 1, Integer::sum);
Expand All @@ -280,6 +293,19 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
);
}

private Iterator<? extends ToXContent> onlyFailuresToXContent() {
Iterator<Cluster> failuresIterator = clusterInfo.values().stream().filter(c -> (c.getFailures().isEmpty() == false)).iterator();
if (failuresIterator.hasNext()) {
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
ChunkedToXContentHelper.object("details", failuresIterator),
ChunkedToXContentHelper.endObject()
);
} else {
return Collections.emptyIterator();
}
}

/**
* @param status the status you want to access
* @return a stream of clusters with that status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,9 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
Iterator<ToXContent> profileRender = profile != null
? ChunkedToXContentHelper.field("profile", profile, params)
: Collections.emptyIterator();
Iterator<ToXContent> executionInfoRender = executionInfo != null
&& executionInfo.isCrossClusterSearch()
&& executionInfo.includeCCSMetadata()
? ChunkedToXContentHelper.field("_clusters", executionInfo, params)
: Collections.emptyIterator();
Iterator<ToXContent> executionInfoRender = executionInfo != null && executionInfo.hasMetadataToReport()
? ChunkedToXContentHelper.field("_clusters", executionInfo, params)
: Collections.emptyIterator();
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
asyncPropertiesOrEmpty(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.RemoteClusterService;

import java.util.List;

public class EsqlExecutionInfoTests extends ESTestCase {

static final EsqlExecutionInfo.Cluster localCluster = new EsqlExecutionInfo.Cluster(
RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY,
"test"
);
static final EsqlExecutionInfo.Cluster remoteCluster = new EsqlExecutionInfo.Cluster("remote", "test");

public void testHasMetadataInclude() {
// includeCCSMetadata + non-local clusters will produce true
EsqlExecutionInfo info = new EsqlExecutionInfo(true);
assertFalse(info.hasMetadataToReport());
info.swapCluster(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY, (k, v) -> localCluster);
assertFalse(info.hasMetadataToReport());
info.swapCluster("remote", (k, v) -> remoteCluster);
assertTrue(info.hasMetadataToReport());
// Only remote is enough
info = new EsqlExecutionInfo(true);
info.swapCluster("remote", (k, v) -> remoteCluster);
assertTrue(info.hasMetadataToReport());
}

public void testHasMetadataIncludeFalse() {
// If includeCCSMetadata is false, then it should always return false
EsqlExecutionInfo info = new EsqlExecutionInfo(false);
assertFalse(info.hasMetadataToReport());
assertFalse(info.hasMetadataToReport());
info.swapCluster(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY, (k, v) -> localCluster);
assertFalse(info.hasMetadataToReport());
info.swapCluster("remote", (k, v) -> remoteCluster);
assertFalse(info.hasMetadataToReport());
}

public void testHasMetadataPartial() {
EsqlExecutionInfo info = new EsqlExecutionInfo(false);
String key = randomFrom(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY, "remote");
info.swapCluster(key, (k, v) -> new EsqlExecutionInfo.Cluster(k, "test", false, EsqlExecutionInfo.Cluster.Status.SUCCESSFUL));
assertFalse(info.isPartial());
assertFalse(info.hasMetadataToReport());
info.swapCluster(key, (k, v) -> new EsqlExecutionInfo.Cluster(k, "test", false, EsqlExecutionInfo.Cluster.Status.PARTIAL));
assertTrue(info.isPartial());
assertFalse(info.hasMetadataToReport());
info.swapCluster(key, (k, v) -> {
EsqlExecutionInfo.Cluster.Builder builder = new EsqlExecutionInfo.Cluster.Builder(v);
builder.setFailures(List.of(new ShardSearchFailure(new IllegalStateException("shard failure"))));
return builder.build();
});
assertTrue(info.hasMetadataToReport());
}

}