-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Streaming aggregation planning to determine the appropriate flush mode #19488
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
rishabhmaurya
merged 9 commits into
opensearch-project:main
from
rishabhmaurya:rishma-stream-planning
Oct 3, 2025
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d7b831e
Planning for flush mode for streaming aggs
rishabhmaurya 65243ba
Address PR comments
rishabhmaurya e7b86fb
Fix for nested aggs and more unit tests
rishabhmaurya 5b5806a
Integ test to validate stream agg used using profile output
rishabhmaurya 6088fbc
Make StreamNumericTermsAggregator streamable
rishabhmaurya 705c2a7
Integ test for StreamNumericTermsAggregator
rishabhmaurya 4c608dd
Improve coverage and PR comments
rishabhmaurya 79fa652
Minor refactor and address PR comments
rishabhmaurya bbb5979
Merge branch 'main' into rishma-stream-planning
rishabhmaurya 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
242 changes: 219 additions & 23 deletions
242
...c/src/internalClusterTest/java/org/opensearch/streaming/aggregation/SubAggregationIT.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
94 changes: 94 additions & 0 deletions
94
server/src/main/java/org/opensearch/search/aggregations/AggregatorTreeEvaluator.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,94 @@ | ||
| /* | ||
| * 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.search.Collector; | ||
| import org.opensearch.common.CheckedFunction; | ||
| import org.opensearch.common.annotation.ExperimentalApi; | ||
| import org.opensearch.search.internal.SearchContext; | ||
| import org.opensearch.search.streaming.FlushMode; | ||
| import org.opensearch.search.streaming.FlushModeResolver; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Performs cost-benefit analysis on aggregator trees to optimize streaming decisions. | ||
| * | ||
| * <p>Evaluates whether streaming aggregations will be beneficial by analyzing the entire | ||
| * collector tree using {@link FlushModeResolver}. When streaming is determined to be | ||
| * inefficient, recreates the aggregator tree with traditional (non-streaming) aggregators. | ||
| * Decisions are cached to ensure consistency across concurrent segment processing. | ||
| * | ||
| * @opensearch.experimental | ||
| */ | ||
| @ExperimentalApi | ||
| public final class AggregatorTreeEvaluator { | ||
|
|
||
| private AggregatorTreeEvaluator() {} | ||
|
|
||
| /** | ||
| * Analyzes collector tree and recreates it with optimal aggregator types. | ||
| * | ||
| * <p>Determines the appropriate {@link FlushMode} for the collector tree and recreates | ||
| * aggregators if streaming is not beneficial. Should be called after initial aggregator | ||
| * creation but before query execution. | ||
| * | ||
| * @param collector the root collector to analyze | ||
| * @param searchContext search context for caching and configuration | ||
| * @param aggProvider factory function to recreate aggregators when needed | ||
| * @return optimized collector (original if streaming, recreated if traditional) | ||
| * @throws IOException if aggregator recreation fails | ||
| */ | ||
| public static Collector evaluateAndRecreateIfNeeded( | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Collector collector, | ||
| SearchContext searchContext, | ||
| CheckedFunction<SearchContext, List<Aggregator>, IOException> aggProvider | ||
| ) throws IOException { | ||
| if (!searchContext.isStreamSearch()) { | ||
| return collector; | ||
| } | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| FlushMode flushMode = getFlushMode(collector, searchContext); | ||
|
|
||
| if (flushMode == FlushMode.PER_SEGMENT) { | ||
| return collector; | ||
| } else { | ||
| return MultiBucketCollector.wrap(aggProvider.apply(searchContext)); | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves flush mode using cached decision or on-demand evaluation. | ||
| * | ||
| * @param collector the collector to evaluate | ||
| * @param searchContext search context for decision caching | ||
| * @return the resolved flush mode for this query | ||
| */ | ||
| private static FlushMode getFlushMode(Collector collector, SearchContext searchContext) { | ||
| FlushMode cached = searchContext.getFlushMode(); | ||
| if (cached != null) { | ||
| return cached; | ||
| } | ||
|
|
||
| long maxBucketCount = searchContext.getStreamingMaxEstimatedBucketCount(); | ||
| double minCardinalityRatio = searchContext.getStreamingMinCardinalityRatio(); | ||
| long minBucketCount = searchContext.getStreamingMinEstimatedBucketCount(); | ||
| FlushMode mode = FlushModeResolver.resolve(collector, FlushMode.PER_SHARD, maxBucketCount, minCardinalityRatio, minBucketCount); | ||
|
|
||
| if (!searchContext.setFlushModeIfAbsent(mode)) { | ||
| // this could happen in case of race condition, we go ahead with what's been set already | ||
| FlushMode existingMode = searchContext.getFlushMode(); | ||
rishabhmaurya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return existingMode != null ? existingMode : mode; | ||
| } | ||
|
|
||
| return mode; | ||
| } | ||
|
|
||
| } | ||
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.