-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce merge map memory overhead in the Variable Width Histogram Aggr…
…egation (#59366) When a document which is distant from existing buckets gets collected, the `variable_width_histogram` will create a new bucket and then insert it into the ordered list of buckets. Currently, a new merge map array is created to move this bucket. This is very expensive as there might be thousands of buckets. This PR creates `mergeBuckets(UnaryOperator<Long> mergeMap)` methods in `BucketsAggregator` and `MergingBucketsDefferingCollector`, and updates the `variable_width_histogram` to use them. This eliminates the need to create an entire merge map array for each new bucket and reduces the memory overhead of the algorithm.
- Loading branch information
1 parent
db0adfd
commit 8b7c556
Showing
5 changed files
with
361 additions
and
20 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
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
131 changes: 131 additions & 0 deletions
131
...er/src/test/java/org/elasticsearch/search/aggregations/bucket/BucketsAggregatorTests.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,131 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.search.aggregations.bucket; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.SortedNumericDocValuesField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.RandomIndexWriter; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.store.Directory; | ||
import org.elasticsearch.common.breaker.CircuitBreaker; | ||
import org.elasticsearch.index.mapper.NumberFieldMapper; | ||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
import org.elasticsearch.search.aggregations.AggregatorFactories; | ||
import org.elasticsearch.search.aggregations.AggregatorTestCase; | ||
import org.elasticsearch.search.aggregations.InternalAggregation; | ||
import org.elasticsearch.search.aggregations.LeafBucketCollector; | ||
import org.elasticsearch.search.aggregations.MultiBucketConsumerService; | ||
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.search.aggregations.MultiBucketConsumerService.DEFAULT_MAX_BUCKETS; | ||
|
||
public class BucketsAggregatorTests extends AggregatorTestCase{ | ||
|
||
public BucketsAggregator buildMergeAggregator() throws IOException{ | ||
try(Directory directory = newDirectory()) { | ||
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { | ||
Document document = new Document(); | ||
document.add(new SortedNumericDocValuesField("numeric", 0)); | ||
indexWriter.addDocument(document); | ||
} | ||
|
||
try (IndexReader indexReader = DirectoryReader.open(directory)) { | ||
IndexSearcher indexSearcher = new IndexSearcher(indexReader); | ||
|
||
SearchContext searchContext = createSearchContext( | ||
indexSearcher, | ||
createIndexSettings(), | ||
null, | ||
new MultiBucketConsumerService.MultiBucketConsumer( | ||
DEFAULT_MAX_BUCKETS, | ||
new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST) | ||
), | ||
new NumberFieldMapper.NumberFieldType("test", NumberFieldMapper.NumberType.INTEGER) | ||
); | ||
|
||
return new BucketsAggregator("test", AggregatorFactories.EMPTY, searchContext, null, null, null) { | ||
@Override | ||
protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { | ||
return new InternalAggregation[0]; | ||
} | ||
|
||
@Override | ||
public InternalAggregation buildEmptyAggregation() { | ||
return null; | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public void testBucketMergeNoDelete() throws IOException{ | ||
BucketsAggregator mergeAggregator = buildMergeAggregator(); | ||
|
||
mergeAggregator.grow(10); | ||
for(int i = 0; i < 10; i++){ | ||
mergeAggregator.incrementBucketDocCount(i, i); | ||
} | ||
|
||
mergeAggregator.mergeBuckets(10, bucket -> bucket % 5); | ||
|
||
for(int i=0; i<5; i++) { | ||
// The i'th bucket should now have all docs whose index % 5 = i | ||
// This is buckets i and i + 5 | ||
// i + (i+5) = 2*i + 5 | ||
assertEquals(mergeAggregator.getDocCounts().get(i), (2 * i) + 5); | ||
} | ||
for(int i=5; i<10; i++){ | ||
assertEquals(mergeAggregator.getDocCounts().get(i), 0); | ||
} | ||
} | ||
|
||
public void testBucketMergeAndDelete() throws IOException{ | ||
BucketsAggregator mergeAggregator = buildMergeAggregator(); | ||
|
||
mergeAggregator.grow(10); | ||
int sum = 0; | ||
for(int i = 0; i < 20; i++){ | ||
mergeAggregator.incrementBucketDocCount(i, i); | ||
if(5 <= i && i < 15) { | ||
sum += i; | ||
} | ||
} | ||
|
||
// Put the buckets in indices 5 ... 14 into bucket 5, and delete the rest of the buckets | ||
mergeAggregator.mergeBuckets(10, bucket -> (5 <= bucket && bucket < 15) ? 5 : -1); | ||
|
||
assertEquals(mergeAggregator.getDocCounts().size(), 10); // Confirm that the 10 other buckets were deleted | ||
for(int i=0; i<10; i++){ | ||
assertEquals(mergeAggregator.getDocCounts().get(i), i == 5 ? sum : 0); | ||
} | ||
} | ||
} |
Oops, something went wrong.