-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add task cancellation check in aggregation code paths #18426
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
Merged
jainankitk
merged 11 commits into
opensearch-project:main
from
kaushalmahi12:task_cancellation_check_2
Jun 11, 2025
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2178fc8
introduce cancellation check in aggs
kaushalmahi12 ef5b297
add more checks
kaushalmahi12 e3f8307
add additional checks
kaushalmahi12 5aebcce
add additional checks
kaushalmahi12 51f8f32
add dump stack
kaushalmahi12 a319ad3
remove dump stack check
kaushalmahi12 064fbde
add UT
kaushalmahi12 d80b8fe
add CHANGELOG
kaushalmahi12 a5e1364
fix failing tests and add NPE check in SearchContext
kaushalmahi12 cfff31e
start making changes
kaushalmahi12 f072cdc
address comments
kaushalmahi12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
...ernalClusterTest/java/org/opensearch/search/aggregations/AggregatorCancellationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.search.aggregations; | ||
|
|
||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.SortedNumericDocValuesField; | ||
| import org.apache.lucene.document.SortedSetDocValuesField; | ||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.IndexReader; | ||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.search.IndexSearcher; | ||
| import org.apache.lucene.search.MatchAllDocsQuery; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.tests.index.RandomIndexWriter; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.opensearch.core.common.io.stream.StreamInput; | ||
| import org.opensearch.core.common.io.stream.StreamOutput; | ||
| import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; | ||
| import org.opensearch.core.xcontent.ObjectParser; | ||
| import org.opensearch.core.xcontent.XContentBuilder; | ||
| import org.opensearch.index.query.QueryShardContext; | ||
| import org.opensearch.plugins.SearchPlugin; | ||
| import org.opensearch.search.DocValueFormat; | ||
| import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder; | ||
| import org.opensearch.search.aggregations.metrics.InternalSum; | ||
| import org.opensearch.search.internal.SearchContext; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class AggregatorCancellationTests extends AggregatorTestCase { | ||
|
|
||
| @Override | ||
| protected List<SearchPlugin> getSearchPlugins() { | ||
| return List.of(new SearchPlugin() { | ||
| @Override | ||
| public List<AggregationSpec> getAggregations() { | ||
| return List.of( | ||
| new AggregationSpec( | ||
| "custom_cancellable", | ||
| CustomCancellableAggregationBuilder::new, | ||
| CustomCancellableAggregationBuilder.PARSER | ||
| ) | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void testNestedAggregationCancellation() throws IOException { | ||
| try (Directory directory = newDirectory()) { | ||
| RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); | ||
|
|
||
| // Create documents | ||
| for (int i = 0; i < 100; i++) { | ||
| Document doc = new Document(); | ||
| doc.add(new SortedSetDocValuesField("category", new BytesRef("cat" + (i % 10)))); | ||
| doc.add(new SortedSetDocValuesField("subcategory", new BytesRef("subcat" + (i % 50)))); | ||
| doc.add(new SortedSetDocValuesField("brand", new BytesRef("brand" + (i % 20)))); | ||
| doc.add(new SortedNumericDocValuesField("value", i)); | ||
| indexWriter.addDocument(doc); | ||
| } | ||
| indexWriter.close(); | ||
|
|
||
| try (IndexReader reader = DirectoryReader.open(directory)) { | ||
| IndexSearcher searcher = newIndexSearcher(reader); | ||
|
|
||
| // Create nested aggregations with our custom cancellable agg | ||
| CustomCancellableAggregationBuilder aggBuilder = new CustomCancellableAggregationBuilder("test_agg").subAggregation( | ||
| new TermsAggregationBuilder("categories").field("category") | ||
| .size(10) | ||
| .subAggregation( | ||
| new TermsAggregationBuilder("subcategories").field("subcategory") | ||
| .size(50000) | ||
| .subAggregation(new TermsAggregationBuilder("brands").field("brand").size(20000)) | ||
| ) | ||
| ); | ||
|
|
||
| expectThrows( | ||
| OpenSearchRejectedExecutionException.class, | ||
| () -> searchAndReduce( | ||
| searcher, | ||
| new MatchAllDocsQuery(), | ||
| aggBuilder, | ||
| keywordField("category"), | ||
| keywordField("subcategory"), | ||
| keywordField("brand") | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class CustomCancellableAggregationBuilder extends AbstractAggregationBuilder<CustomCancellableAggregationBuilder> { | ||
|
|
||
| public static final String NAME = "custom_cancellable"; | ||
|
|
||
| public static final ObjectParser<CustomCancellableAggregationBuilder, String> PARSER = ObjectParser.fromBuilder( | ||
| NAME, | ||
| CustomCancellableAggregationBuilder::new | ||
| ); | ||
|
|
||
| CustomCancellableAggregationBuilder(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| public CustomCancellableAggregationBuilder(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| protected AggregatorFactory doBuild( | ||
| QueryShardContext queryShardContext, | ||
| AggregatorFactory parent, | ||
| AggregatorFactories.Builder subfactoriesBuilder | ||
| ) throws IOException { | ||
| return new AggregatorFactory(name, queryShardContext, parent, subfactoriesBuilder, metadata) { | ||
| @Override | ||
| protected Aggregator createInternal( | ||
| SearchContext searchContext, | ||
| Aggregator parent, | ||
| CardinalityUpperBound cardinality, | ||
| Map<String, Object> metadata | ||
| ) throws IOException { | ||
| return new CustomCancellableAggregator( | ||
| name, | ||
| searchContext, | ||
| parent, | ||
| subfactoriesBuilder.build(queryShardContext, this), | ||
| null | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException { | ||
| return builder; | ||
| } | ||
|
|
||
| @Override | ||
| public CustomCancellableAggregationBuilder shallowCopy(AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metadata) { | ||
| return new CustomCancellableAggregationBuilder(getName()).setMetadata(metadata).subAggregations(factoriesBuilder); | ||
| } | ||
|
|
||
| public String getType() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public BucketCardinality bucketCardinality() { | ||
| return BucketCardinality.NONE; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doWriteTo(StreamOutput out) throws IOException { | ||
| // Nothing to write | ||
| } | ||
| } | ||
|
|
||
| private static class CustomCancellableAggregator extends AggregatorBase { | ||
|
|
||
| CustomCancellableAggregator( | ||
| String name, | ||
| SearchContext context, | ||
| Aggregator parent, | ||
| AggregatorFactories factories, | ||
| Map<String, Object> metadata | ||
| ) throws IOException { | ||
| super(name, factories, context, parent, CardinalityUpperBound.NONE, metadata); | ||
| } | ||
|
|
||
| @Override | ||
| protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException { | ||
| checkCancelled(); | ||
| return LeafBucketCollector.NO_OP_COLLECTOR; | ||
| } | ||
|
|
||
| protected void checkCancelled() { | ||
| throw new OpenSearchRejectedExecutionException("The request has been cancelled"); | ||
| } | ||
|
|
||
| @Override | ||
| public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { | ||
| InternalAggregation internalAggregation = new InternalSum(name(), 0.0, DocValueFormat.RAW, metadata()); | ||
| return new InternalAggregation[] { internalAggregation }; | ||
| } | ||
|
|
||
| @Override | ||
| public InternalAggregation buildEmptyAggregation() { | ||
| return new InternalSum(name(), 0.0, DocValueFormat.RAW, metadata()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.