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

Fix wrong status code for SearchPhaseExecutionException #19851

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ private static Throwable deduplicateCause(Throwable cause, ShardSearchFailure[]
@Override
public RestStatus status() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are there any tests for this method?
It looks like the code only expects a SearchPhaseExecutionException only when there are shard failure(s). The bug was that an exception occurred on the coordinating node performing reduce, which resulted in this code assuming all shards failed (503 status instead of the expected root cause status).

if (shardFailures.length == 0) {
// if no successful shards, it means no active shards, so just return SERVICE_UNAVAILABLE
return RestStatus.SERVICE_UNAVAILABLE;
// no successful shard responses or no shard failures
Throwable cause = super.getCause();
if (cause == null) {
// if no successful shards, it means no active shards, so just return SERVICE_UNAVAILABLE
return RestStatus.SERVICE_UNAVAILABLE;
} else {
// no shard failures: exception on node performing reduce
return ExceptionsHelper.status(cause);
}
}
RestStatus status = shardFailures[0].status();
if (shardFailures.length > 1) {
Expand Down Expand Up @@ -150,7 +157,7 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO
@Override
protected void causeToXContent(XContentBuilder builder, Params params) throws IOException {
if (super.getCause() != null) {
// if the cause is null we inject a guessed root cause that will then be rendered twice so wi disable it manually
// if the cause is null we inject a guessed root cause that will then be rendered twice so we disable it manually
super.causeToXContent(builder, params);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static InternalOrder resolveOrder(String key, boolean asc) {
if ("_count".equals(key)) {
return (InternalOrder) (asc ? InternalOrder.COUNT_ASC : InternalOrder.COUNT_DESC);
}
// TODO check for valid sub-aggregation names and fields here instead of reduce phase.
return new InternalOrder.Aggregation(key, asc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ static InternalOrder resolveOrder(String key, boolean asc) {
if ("_count".equals(key)) {
return (InternalOrder) (asc ? InternalOrder.COUNT_ASC : InternalOrder.COUNT_DESC);
}
// TODO check for valid sub-aggregation names and fields here instead of reduce phase.
return new InternalOrder.Aggregation(key, asc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
} else {
// sorted by sub-aggregation, need to fall back to a costly n*log(n) sort
CollectionUtil.introSort(reducedBuckets, order.comparator());
if (reducedBuckets.size() == 1) {
// hack: force check of sub-aggregation names and fields if there is only 1 bucket (sort code bypassed)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently if the (date) histogram is ordered by sub-aggregation(s), the order path is validated during the reduce phase. This check occurs implicitly in the comparator when the histogram buckets are sorted. However if there are 0 or 1 buckets, there is nothing to sort so this code was bypassed. I added a hack here to catch the case with 1 bucket. To catch the case with 0 buckets, the validation code needs to be refactored to run during the query parsing phase (if possible). This would require parsing all sub-aggregations first and then validating the order path.

// TODO check for valid sub-aggregation names and fields during parsing instead of reduce phase
Bucket b = reducedBuckets.get(0);
order.comparator().compare(b, b);
}
}

return new InternalDateHistogram(getName(), reducedBuckets, order, minDocCount, offset, emptyBucketInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
} else {
// sorted by sub-aggregation, need to fall back to a costly n*log(n) sort
CollectionUtil.introSort(reducedBuckets, order.comparator());
if (reducedBuckets.size() == 1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lots of duplicated code between Histogram and DateHistogram :(

// hack: force check of sub-aggregation names and fields if there is only 1 bucket (sort code bypassed)
// TODO check for valid sub-aggregation names and fields during parsing instead of reduce phase
Bucket b = reducedBuckets.get(0);
order.comparator().compare(b, b);
}
}

return new InternalHistogram(getName(), reducedBuckets, order, minDocCount, emptyBucketInfo, format, keyed, pipelineAggregators(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.elasticsearch.search.aggregations.bucket;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.joda.DateMathParser;
Expand Down Expand Up @@ -66,6 +67,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNull.notNullValue;

Expand Down Expand Up @@ -471,6 +473,23 @@ public void testSingleValuedFieldOrderedBySubAggregationDesc() throws Exception
}
}

public void testSingleValuedFieldOrderedByMissingSubAggregation() throws Exception {
try {
client().prepareSearch("idx")
.addAggregation(dateHistogram("histo")
.field("date")
.dateHistogramInterval(DateHistogramInterval.MONTH)
.order(Histogram.Order.aggregation("stats_missing", "sum", false))
.subAggregation(stats("stats").field("value")))
.get();
fail();
} catch (ElasticsearchException ex) {
Throwable rootCause = ex.getRootCause();
assertThat(rootCause, instanceOf(IllegalArgumentException.class));
assertThat(rootCause.getMessage(), containsString("Invalid order path"));
}
}

public void testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(dateHistogram("histo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.search.aggregations.bucket;

import com.carrotsearch.hppc.LongHashSet;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
Expand Down Expand Up @@ -56,6 +57,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.core.IsNull.notNullValue;
Expand Down Expand Up @@ -470,6 +472,24 @@ public void testSingleValuedFieldOrderedBySubAggregationDesc() throws Exception
}
}

public void testSingleValuedFieldOrderedByMissingSubAggregation() throws Exception {
try {
client().prepareSearch("idx")
.addAggregation(
histogram("histo")
.field(SINGLE_VALUED_FIELD_NAME)
.interval(interval)
.order(Histogram.Order.aggregation("stats_missing.sum", false))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME)))
.get();
fail();
} catch (ElasticsearchException ex) {
Throwable rootCause = ex.getRootCause();
assertThat(rootCause, instanceOf(IllegalArgumentException.class));
assertThat(rootCause.getMessage(), containsString("Invalid order path"));
}
}

public void testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.addAggregation(
Expand Down