forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Aggregator#buildTopLevel() to search worker thread. (elastic#98715)
This commit introduces an AggregatorCollector that contains a finish method which performs aggregation post-collection and builds the internal aggregation for this collector. This method is called on the worker thread at the end of the collection phase.
- Loading branch information
Showing
18 changed files
with
293 additions
and
224 deletions.
There are no files selected for viewing
This file contains 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 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
61 changes: 61 additions & 0 deletions
61
server/src/main/java/org/elasticsearch/search/aggregations/AggregatorCollector.java
This file contains 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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.LeafCollector; | ||
import org.apache.lucene.search.ScoreMode; | ||
import org.elasticsearch.search.internal.TwoPhaseCollector; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** Collector that controls the life cycle of an aggregation document collection. */ | ||
public class AggregatorCollector implements TwoPhaseCollector { | ||
final Aggregator[] aggregators; | ||
final BucketCollector bucketCollector; | ||
final List<InternalAggregation> internalAggregations; | ||
|
||
public AggregatorCollector(Aggregator[] aggregators, BucketCollector bucketCollector) { | ||
this.aggregators = aggregators; | ||
this.bucketCollector = bucketCollector; | ||
this.internalAggregations = new ArrayList<>(aggregators.length); | ||
} | ||
|
||
@Override | ||
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException { | ||
return bucketCollector.getLeafCollector(new AggregationExecutionContext(context, null, null, null)); | ||
} | ||
|
||
@Override | ||
public ScoreMode scoreMode() { | ||
return bucketCollector.scoreMode(); | ||
} | ||
|
||
@Override | ||
public void doPostCollection() throws IOException { | ||
bucketCollector.postCollection(); | ||
for (Aggregator aggregator : aggregators) { | ||
internalAggregations.add(aggregator.buildTopLevel()); | ||
// release the aggregator to claim the used bytes as we don't need it anymore | ||
aggregator.releaseAggregations(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String[] aggNames = new String[aggregators.length]; | ||
for (int i = 0; i < aggregators.length; i++) { | ||
aggNames[i] = aggregators[i].name(); | ||
} | ||
return Arrays.toString(aggNames); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
server/src/main/java/org/elasticsearch/search/aggregations/AggregatorCollectorManager.java
This file contains 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,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations; | ||
|
||
import org.apache.lucene.search.CollectorManager; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
/** Collector manager that produces {@link AggregatorCollector} and merges them during the reduce phase. */ | ||
public class AggregatorCollectorManager implements CollectorManager<AggregatorCollector, Void> { | ||
|
||
private final Supplier<AggregatorCollector> collectorSupplier; | ||
private final Consumer<InternalAggregations> internalAggregationsConsumer; | ||
private final Supplier<AggregationReduceContext> reduceContextSupplier; | ||
|
||
public AggregatorCollectorManager( | ||
Supplier<AggregatorCollector> collectorSupplier, | ||
Consumer<InternalAggregations> internalAggregationsConsumer, | ||
Supplier<AggregationReduceContext> reduceContextSupplier | ||
) { | ||
this.collectorSupplier = collectorSupplier; | ||
this.internalAggregationsConsumer = internalAggregationsConsumer; | ||
this.reduceContextSupplier = reduceContextSupplier; | ||
} | ||
|
||
@Override | ||
public AggregatorCollector newCollector() throws IOException { | ||
return collectorSupplier.get(); | ||
} | ||
|
||
@Override | ||
public Void reduce(Collection<AggregatorCollector> collectors) throws IOException { | ||
if (collectors.size() > 1) { | ||
// we execute this search using more than one slice. In order to keep memory requirements | ||
// low, we do a partial reduction here. | ||
final List<InternalAggregations> internalAggregations = new ArrayList<>(collectors.size()); | ||
collectors.forEach(c -> internalAggregations.add(InternalAggregations.from(c.internalAggregations))); | ||
internalAggregationsConsumer.accept(InternalAggregations.topLevelReduce(internalAggregations, reduceContextSupplier.get())); | ||
} else if (collectors.size() == 1) { | ||
internalAggregationsConsumer.accept(InternalAggregations.from(collectors.iterator().next().internalAggregations)); | ||
} | ||
return null; | ||
} | ||
} |
This file contains 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.