Skip to content

Commit ebcba60

Browse files
committed
Move the mem accounting buffer size in aggregationcontext
1 parent 86da7fd commit ebcba60

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/search/aggregations/AggConstructionContentionBenchmark.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ public Set<String> sourcePath(String fullName) {
366366
return Set.of(fullName);
367367
}
368368

369+
@Override
370+
public long memoryAccountingBufferSize() {
371+
return 1024 * 1024;
372+
}
373+
369374
@Override
370375
public void close() {
371376
List<Releasable> releaseMe = new ArrayList<>(this.releaseMe);

server/src/main/java/org/elasticsearch/search/SearchService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,8 @@ private void parseSource(DefaultSearchContext context, SearchSourceBuilder sourc
14411441
context::isCancelled,
14421442
context::buildFilteredQuery,
14431443
enableRewriteAggsToFilterByFilter,
1444-
source.aggregations().isInSortOrderExecutionRequired()
1444+
source.aggregations().isInSortOrderExecutionRequired(),
1445+
memoryAccountingBufferSize
14451446
);
14461447
context.addQuerySearchResultReleasable(aggContext);
14471448
try {

server/src/main/java/org/elasticsearch/search/aggregations/metrics/TopHitsAggregator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.elasticsearch.index.query.SearchExecutionContext;
3636
import org.elasticsearch.search.SearchHit;
3737
import org.elasticsearch.search.SearchHits;
38-
import org.elasticsearch.search.SearchService;
3938
import org.elasticsearch.search.aggregations.AggregationExecutionContext;
4039
import org.elasticsearch.search.aggregations.Aggregator;
4140
import org.elasticsearch.search.aggregations.InternalAggregation;
@@ -57,8 +56,6 @@
5756

5857
class TopHitsAggregator extends MetricsAggregator {
5958

60-
private final long memAccountingBufferSize;
61-
6259
private static class Collectors {
6360
public final TopDocsCollector<?> topDocsCollector;
6461
public final MaxScoreCollector maxScoreCollector;
@@ -90,7 +87,6 @@ private static class Collectors {
9087
this.subSearchContext = subSearchContext;
9188
this.topDocsCollectors = new LongObjectPagedHashMap<>(1, bigArrays);
9289
this.fetchProfiles = context.profiling() ? new ArrayList<>() : null;
93-
this.memAccountingBufferSize = context.getClusterSettings().get(SearchService.MEMORY_ACCOUNTING_BUFFER_SIZE).getBytes();
9490
}
9591

9692
@Override
@@ -201,7 +197,12 @@ public InternalAggregation buildAggregation(long owningBucketOrdinal) throws IOE
201197
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
202198
docIdsToLoad[i] = topDocs.scoreDocs[i].doc;
203199
}
204-
FetchSearchResult fetchResult = runFetchPhase(subSearchContext, docIdsToLoad, context.breaker(), memAccountingBufferSize);
200+
FetchSearchResult fetchResult = runFetchPhase(
201+
subSearchContext,
202+
docIdsToLoad,
203+
context.breaker(),
204+
context.memoryAccountingBufferSize()
205+
);
205206
if (fetchProfiles != null) {
206207
fetchProfiles.add(fetchResult.profileResult());
207208
}

server/src/main/java/org/elasticsearch/search/aggregations/support/AggregationContext.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ public final AggregationUsageService getUsageService() {
308308

309309
public abstract Set<String> sourcePath(String fullName);
310310

311+
/**
312+
* Return the amount of memory to buffer locally before accounting for it in the breaker.
313+
*/
314+
public abstract long memoryAccountingBufferSize();
315+
311316
/**
312317
* Does this index have a {@code _doc_count} field in any segment?
313318
*/
@@ -355,6 +360,7 @@ public static class ProductionAggregationContext extends AggregationContext {
355360
private final AnalysisRegistry analysisRegistry;
356361

357362
private final List<Aggregator> releaseMe = new ArrayList<>();
363+
private final long memoryAccountingBufferSize;
358364

359365
public ProductionAggregationContext(
360366
AnalysisRegistry analysisRegistry,
@@ -372,7 +378,8 @@ public ProductionAggregationContext(
372378
Supplier<Boolean> isCancelled,
373379
Function<Query, Query> filterQuery,
374380
boolean enableRewriteToFilterByFilter,
375-
boolean inSortOrderExecutionRequired
381+
boolean inSortOrderExecutionRequired,
382+
long memoryAccountingBufferSize
376383
) {
377384
this.analysisRegistry = analysisRegistry;
378385
this.context = context;
@@ -407,6 +414,7 @@ public ProductionAggregationContext(
407414
this.filterQuery = filterQuery;
408415
this.enableRewriteToFilterByFilter = enableRewriteToFilterByFilter;
409416
this.inSortOrderExecutionRequired = inSortOrderExecutionRequired;
417+
this.memoryAccountingBufferSize = memoryAccountingBufferSize;
410418
}
411419

412420
@Override
@@ -612,6 +620,11 @@ public Set<String> sourcePath(String fullName) {
612620
return context.sourcePath(fullName);
613621
}
614622

623+
@Override
624+
public long memoryAccountingBufferSize() {
625+
return memoryAccountingBufferSize;
626+
}
627+
615628
@Override
616629
public void close() {
617630
/*

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,11 @@ public Set<String> sourcePath(String fullName) {
675675
return Set.of(fullName);
676676
}
677677

678+
@Override
679+
public long memoryAccountingBufferSize() {
680+
return 1024 * 1024;
681+
}
682+
678683
@Override
679684
public void close() {
680685
throw new UnsupportedOperationException();

test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
import org.elasticsearch.common.network.NetworkAddress;
6969
import org.elasticsearch.common.settings.ClusterSettings;
7070
import org.elasticsearch.common.settings.Settings;
71+
import org.elasticsearch.common.unit.ByteSizeUnit;
72+
import org.elasticsearch.common.unit.ByteSizeValue;
7173
import org.elasticsearch.common.util.BigArrays;
7274
import org.elasticsearch.common.util.MockBigArrays;
7375
import org.elasticsearch.common.util.MockPageCacheRecycler;
@@ -444,7 +446,8 @@ public Iterable<MappedFieldType> dimensionFields() {
444446
isCancelled,
445447
q -> q,
446448
true,
447-
isInSortOrderExecutionRequired
449+
isInSortOrderExecutionRequired,
450+
ByteSizeValue.of(1, ByteSizeUnit.MB).getBytes()
448451
);
449452
return context;
450453
}

0 commit comments

Comments
 (0)