Kaynağa Gözat

Reduce heap usage for AggregatorsReducer (#112874) (#113116)

This commit reduces heap usage by sizing properly the hashmap containing the aggregations by name.
Ignacio Vera 1 yıl önce
ebeveyn
işleme
77f20955f8

+ 5 - 0
docs/changelog/112874.yaml

@@ -0,0 +1,5 @@
+pr: 112874
+summary: Reduce heap usage for `AggregatorsReducer`
+area: Aggregations
+type: enhancement
+issues: []

+ 10 - 2
server/src/main/java/org/elasticsearch/search/aggregations/AggregatorsReducer.java

@@ -20,13 +20,21 @@ import java.util.Map;
  */
 public final class AggregatorsReducer implements Releasable {
 
-    private final Map<String, AggregatorReducer> aggByName = new HashMap<>();
+    private final Map<String, AggregatorReducer> aggByName;
     private final AggregationReduceContext context;
     private final int size;
 
-    public AggregatorsReducer(AggregationReduceContext context, int size) {
+    /**
+     * Solo constructor
+     *
+     * @param proto The prototype {@link InternalAggregations} we are aggregating.
+     * @param context The aggregation context
+     * @param size The number of {@link InternalAggregations} we are aggregating.
+     */
+    public AggregatorsReducer(InternalAggregations proto, AggregationReduceContext context, int size) {
         this.context = context;
         this.size = size;
+        aggByName = new HashMap<>(proto.asList().size());
     }
 
     /**

+ 1 - 1
server/src/main/java/org/elasticsearch/search/aggregations/InternalAggregations.java

@@ -268,7 +268,7 @@ public final class InternalAggregations implements Iterable<InternalAggregation>
             return from(reduced);
         }
         // general case
-        try (AggregatorsReducer reducer = new AggregatorsReducer(context, aggregationsList.size())) {
+        try (AggregatorsReducer reducer = new AggregatorsReducer(aggregationsList.get(0), context, aggregationsList.size())) {
             for (InternalAggregations aggregations : aggregationsList) {
                 reducer.accept(aggregations);
             }

+ 1 - 1
server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketReducer.java

@@ -26,7 +26,7 @@ public final class BucketReducer<B extends MultiBucketsAggregation.Bucket> imple
     private long count = 0;
 
     public BucketReducer(B proto, AggregationReduceContext context, int size) {
-        this.aggregatorsReducer = new AggregatorsReducer(context, size);
+        this.aggregatorsReducer = new AggregatorsReducer(proto.getAggregations(), context, size);
         this.proto = proto;
     }
 

+ 3 - 1
server/src/main/java/org/elasticsearch/search/aggregations/bucket/DelayedBucketReducer.java

@@ -68,7 +68,9 @@ public final class DelayedBucketReducer<B extends MultiBucketsAggregation.Bucket
      * returns the reduced {@link InternalAggregations}.
      */
     public InternalAggregations getAggregations() {
-        try (AggregatorsReducer aggregatorsReducer = new AggregatorsReducer(context, internalAggregations.size())) {
+        try (
+            AggregatorsReducer aggregatorsReducer = new AggregatorsReducer(proto.getAggregations(), context, internalAggregations.size())
+        ) {
             for (InternalAggregations agg : internalAggregations) {
                 aggregatorsReducer.accept(agg);
             }

+ 1 - 1
server/src/main/java/org/elasticsearch/search/aggregations/bucket/InternalSingleBucketAggregation.java

@@ -98,7 +98,7 @@ public abstract class InternalSingleBucketAggregation extends InternalAggregatio
     protected AggregatorReducer getLeaderReducer(AggregationReduceContext reduceContext, int size) {
         return new AggregatorReducer() {
             long docCount = 0L;
-            final AggregatorsReducer subAggregatorReducer = new AggregatorsReducer(reduceContext, size);
+            final AggregatorsReducer subAggregatorReducer = new AggregatorsReducer(getAggregations(), reduceContext, size);
 
             @Override
             public void accept(InternalAggregation aggregation) {

+ 1 - 1
server/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/random/InternalRandomSampler.java

@@ -92,7 +92,7 @@ public class InternalRandomSampler extends InternalSingleBucketAggregation imple
     protected AggregatorReducer getLeaderReducer(AggregationReduceContext reduceContext, int size) {
         return new AggregatorReducer() {
             long docCount = 0L;
-            final AggregatorsReducer subAggregatorReducer = new AggregatorsReducer(reduceContext, size);
+            final AggregatorsReducer subAggregatorReducer = new AggregatorsReducer(getAggregations(), reduceContext, size);
 
             @Override
             public void accept(InternalAggregation aggregation) {