Skip to content

Commit 4388449

Browse files
authored
Ignore cancellation error when search is cancelled (#64240)
Since #63520, we will cancel a search that hits shard failures and does not accept partial results. However, that change can return the wrong HTTP code for bad requests (from 4xx to 5xx) due to the cancellation. Relates #63520 Closes #64012 Closes #63702
1 parent 7384e68 commit 4388449

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

server/src/main/java/org/elasticsearch/action/search/AbstractSearchAsyncAction.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.elasticsearch.search.internal.InternalSearchResponse;
4545
import org.elasticsearch.search.internal.SearchContext;
4646
import org.elasticsearch.search.internal.ShardSearchRequest;
47+
import org.elasticsearch.tasks.TaskCancelledException;
4748
import org.elasticsearch.transport.Transport;
4849

4950
import java.util.ArrayDeque;
@@ -437,8 +438,9 @@ protected void onShardGroupFailure(int shardIndex, SearchShardTarget shardTarget
437438
*/
438439
@Override
439440
public final void onShardFailure(final int shardIndex, @Nullable SearchShardTarget shardTarget, Exception e) {
440-
// we don't aggregate shard failures on non active shards (but do keep the header counts right)
441-
if (TransportActions.isShardNotAvailableException(e) == false) {
441+
// we don't aggregate shard failures on non active shards and failures due to the internal cancellation,
442+
// but do keep the header counts right
443+
if (TransportActions.isShardNotAvailableException(e) == false && (requestCancelled.get() && isTaskCancelledException(e)) == false) {
442444
AtomicArray<ShardSearchFailure> shardFailures = this.shardFailures.get();
443445
// lazily create shard failures, so we can early build the empty shard failure list in most cases (no failures)
444446
if (shardFailures == null) { // this is double checked locking but it's fine since SetOnce uses a volatile read internally
@@ -469,6 +471,10 @@ public final void onShardFailure(final int shardIndex, @Nullable SearchShardTarg
469471
results.consumeShardFailure(shardIndex);
470472
}
471473

474+
private static boolean isTaskCancelledException(Exception e) {
475+
return ExceptionsHelper.unwrapCausesAndSuppressed(e, ex -> ex instanceof TaskCancelledException).isPresent();
476+
}
477+
472478
/**
473479
* Executed once for every successful shard level request.
474480
* @param result the result returned form the shard

x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchActionIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ public void testRemoveAsyncIndex() throws Exception {
406406
ensureTaskRemoval(newResp.getId());
407407
}
408408

409-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/63702")
410409
public void testSearchPhaseFailureNoCause() throws Exception {
411410
SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest(indexName);
412411
request.setKeepOnCompletion(true);

0 commit comments

Comments
 (0)