Skip to content

Commit

Permalink
Allow aggs to disable offloading sequential collection
Browse files Browse the repository at this point in the history
In elastic#98204 we are introducing unconditional offloading of collection to a
separate thread pool, even for requests of phases that don't enable
search concurrency. It turns out that some aggs don't support offloading
their collection to a separate thread, as their postCollect method is
executed on the search thread which trips a lucene assertion around
reusing data structures pulled from the search worker thread.

With this commit we allow aggs to specify when they don't support
offloading their sequential collection. Such aggs are a subset of the
ones that already declare that they don't support concurrency entirely.

Relates to elastic#96023
  • Loading branch information
javanna committed Aug 8, 2023
1 parent 837dcc2 commit 5025113
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,35 @@ public boolean isInSortOrderExecutionRequired() {
}

/**
* Return false if this aggregation or any of the child aggregations does not support concurrent search
* Return false if this aggregation or any of the child aggregations does not support concurrent search.
* As a result, such aggregation will always be executed sequentially despite concurrency is enabled for the query phase.
* Note: aggregations that don't support concurrency, may or may not support offloading their collection to the search worker threads,
* depending on what {@link #supportsOffloadingSequentialCollection()} returns.
*/
public boolean supportsConcurrentExecution() {
for (AggregationBuilder builder : factoriesBuilder.getAggregatorFactories()) {
if (builder.supportsConcurrentExecution() == false) {
if (builder.supportsOffloadingSequentialCollection() || builder.supportsConcurrentExecution() == false) {
return false;
}
}
return isInSortOrderExecutionRequired() == false;
}

/**
* Returns false if this aggregation or any of its child aggregations does not support offloading its sequential collection
* to a separate thread. As a result, such aggregation will always be executed sequentially, and fully in the search thread,
* without offloading its collection to the search worker threads.
* Note: aggregations that don't support offloading sequential collection, don't support concurrency by definition.
*/
public boolean supportsOffloadingSequentialCollection() {
for (AggregationBuilder builder : factoriesBuilder.getAggregatorFactories()) {
if (builder.supportsOffloadingSequentialCollection() == false) {
return false;
}
}
return true;
}

/**
* Called by aggregations whose parents must be sequentially ordered.
* @param type the type of the aggregation being validated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean supportsSampling() {
}

@Override
public boolean supportsConcurrentExecution() {
public boolean supportsOffloadingSequentialCollection() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public BucketCardinality bucketCardinality() {
}

@Override
public boolean supportsConcurrentExecution() {
public boolean supportsOffloadingSequentialCollection() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public boolean supportsSampling() {
}

@Override
public boolean supportsConcurrentExecution() {
public boolean supportsOffloadingSequentialCollection() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public class CardinalityAggregatorTests extends AggregatorTestCase {
/** Script to extract a collection of numeric values from the 'numbers' field **/
public static final String NUMERIC_VALUES_SCRIPT = "doc['numbers']";

public static final int HASHER_DEFAULT_SEED = 17;

@Override
protected ScriptService getMockScriptService() {
final Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,25 @@ public void testFromXContent() throws IOException {
public void testSupportsConcurrentExecution() {
AB builder = createTestAggregatorBuilder();
boolean supportsConcurrency = builder.supportsConcurrentExecution();
if (supportsConcurrency == false) {
assertFalse(builder.supportsOffloadingSequentialCollection());
}
AggregationBuilder bucketBuilder = new HistogramAggregationBuilder("test");
assertThat(bucketBuilder.supportsConcurrentExecution(), equalTo(true));
assertTrue(bucketBuilder.supportsConcurrentExecution());
bucketBuilder.subAggregation(builder);
assertThat(bucketBuilder.supportsConcurrentExecution(), equalTo(supportsConcurrency));
if (bucketBuilder.supportsConcurrentExecution() == false) {
assertFalse(bucketBuilder.supportsOffloadingSequentialCollection());
}
}

public void testSupportsOffloadingSequentialCollection() {
AB builder = createTestAggregatorBuilder();
boolean supportsOffloadingSequentialCollection = builder.supportsOffloadingSequentialCollection();
AggregationBuilder bucketBuilder = new HistogramAggregationBuilder("test");
assertTrue(bucketBuilder.supportsOffloadingSequentialCollection());
bucketBuilder.subAggregation(builder);
assertThat(bucketBuilder.supportsOffloadingSequentialCollection(), equalTo(supportsOffloadingSequentialCollection));
}

/**
Expand Down

0 comments on commit 5025113

Please sign in to comment.