-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Release aggregations earlier during reduce #124520
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
original-brownbear
merged 3 commits into
elastic:main
from
original-brownbear:release-aggs-earlier
Mar 13, 2025
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ | |
| */ | ||
| package org.elasticsearch.search.aggregations; | ||
|
|
||
| import org.elasticsearch.common.io.stream.DelayableWriteable; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
|
|
@@ -23,7 +22,6 @@ | |
| import org.elasticsearch.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.AbstractList; | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
@@ -180,44 +178,22 @@ public SortValue sortValue(AggregationPath.PathElement head, Iterator<Aggregatio | |
| } | ||
|
|
||
| /** | ||
| * Equivalent to {@link #topLevelReduce(List, AggregationReduceContext)} but it takes a list of | ||
| * {@link DelayableWriteable}. The object will be expanded once via {@link DelayableWriteable#expand()} | ||
| * but it is the responsibility of the caller to release those releasables. | ||
| * Equivalent to {@link #topLevelReduce(List, AggregationReduceContext)} but it takes an iterator and a count. | ||
| */ | ||
| public static InternalAggregations topLevelReduceDelayable( | ||
| List<DelayableWriteable<InternalAggregations>> delayableAggregations, | ||
| AggregationReduceContext context | ||
| ) { | ||
| final List<InternalAggregations> aggregations = new AbstractList<>() { | ||
| @Override | ||
| public InternalAggregations get(int index) { | ||
| return delayableAggregations.get(index).expand(); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return delayableAggregations.size(); | ||
| } | ||
| }; | ||
| return topLevelReduce(aggregations, context); | ||
| public static InternalAggregations topLevelReduce(Iterator<InternalAggregations> aggs, int count, AggregationReduceContext context) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is single-use for now but I left it here since it could be used where the existing version of it that consumes a list is used today as well to save some more indirection and maybe heap. |
||
| if (count == 0) { | ||
| return null; | ||
| } | ||
| return maybeExecuteFinalReduce(context, count == 1 ? reduce(aggs.next(), context) : reduce(aggs, count, context)); | ||
| } | ||
|
|
||
| /** | ||
| * Begin the reduction process. This should be the entry point for the "first" reduction, e.g. called by | ||
| * SearchPhaseController or anywhere else that wants to initiate a reduction. It _should not_ be called | ||
| * as an intermediate reduction step (e.g. in the middle of an aggregation tree). | ||
| * | ||
| * This method first reduces the aggregations, and if it is the final reduce, then reduce the pipeline | ||
| * aggregations (both embedded parent/sibling as well as top-level sibling pipelines) | ||
| */ | ||
| public static InternalAggregations topLevelReduce(List<InternalAggregations> aggregationsList, AggregationReduceContext context) { | ||
| InternalAggregations reduced = reduce(aggregationsList, context); | ||
| private static InternalAggregations maybeExecuteFinalReduce(AggregationReduceContext context, InternalAggregations reduced) { | ||
| if (reduced == null) { | ||
| return null; | ||
| } | ||
| if (context.isFinalReduce()) { | ||
| List<InternalAggregation> reducedInternalAggs = reduced.getInternalAggregations(); | ||
| reducedInternalAggs = reducedInternalAggs.stream() | ||
| List<InternalAggregation> reducedInternalAggs = reduced.getInternalAggregations() | ||
| .stream() | ||
| .map(agg -> agg.reducePipelines(agg, context, context.pipelineTreeRoot().subTree(agg.getName()))) | ||
| .collect(Collectors.toCollection(ArrayList::new)); | ||
|
|
||
|
|
@@ -231,6 +207,18 @@ public static InternalAggregations topLevelReduce(List<InternalAggregations> agg | |
| return reduced; | ||
| } | ||
|
|
||
| /** | ||
| * Begin the reduction process. This should be the entry point for the "first" reduction, e.g. called by | ||
| * SearchPhaseController or anywhere else that wants to initiate a reduction. It _should not_ be called | ||
| * as an intermediate reduction step (e.g. in the middle of an aggregation tree). | ||
| * | ||
| * This method first reduces the aggregations, and if it is the final reduce, then reduce the pipeline | ||
| * aggregations (both embedded parent/sibling as well as top-level sibling pipelines) | ||
| */ | ||
| public static InternalAggregations topLevelReduce(List<InternalAggregations> aggregationsList, AggregationReduceContext context) { | ||
| return maybeExecuteFinalReduce(context, reduce(aggregationsList, context)); | ||
| } | ||
|
|
||
| /** | ||
| * Reduces the given list of aggregations as well as the top-level pipeline aggregators extracted from the first | ||
| * {@link InternalAggregations} object found in the list. | ||
|
|
@@ -254,6 +242,16 @@ public static InternalAggregations reduce(List<InternalAggregations> aggregation | |
| } | ||
| } | ||
|
|
||
| private static InternalAggregations reduce(Iterator<InternalAggregations> aggsIterator, int count, AggregationReduceContext context) { | ||
| // general case | ||
| var first = aggsIterator.next(); | ||
| try (AggregatorsReducer reducer = new AggregatorsReducer(first, context, count)) { | ||
| reducer.accept(first); | ||
| aggsIterator.forEachRemaining(reducer::accept); | ||
| return reducer.get(); | ||
| } | ||
| } | ||
|
|
||
| public static InternalAggregations reduce(InternalAggregations aggregations, AggregationReduceContext context) { | ||
| final List<InternalAggregation> internalAggregations = aggregations.asList(); | ||
| int size = internalAggregations.size(); | ||
|
|
||
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.