Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce TwoPhaseCollector to leverage ContextIndexSearcher in AggregatorTestCase #98835

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.ScoreMode;
import org.elasticsearch.search.internal.TwoPhaseCollector;

import java.io.IOException;

Expand Down Expand Up @@ -79,7 +80,7 @@ public final Collector asCollector() {
return new BucketCollectorWrapper(this);
}

public record BucketCollectorWrapper(BucketCollector bucketCollector) implements Collector {
public record BucketCollectorWrapper(BucketCollector bucketCollector) implements TwoPhaseCollector {

@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
Expand All @@ -90,5 +91,10 @@ public LeafCollector getLeafCollector(LeafReaderContext context) throws IOExcept
public ScoreMode scoreMode() {
return bucketCollector.scoreMode();
}

@Override
public void doPostCollection() throws IOException {
bucketCollector.postCollection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@
import org.elasticsearch.lucene.util.CombinedBitSet;
import org.elasticsearch.search.dfs.AggregatedDfs;
import org.elasticsearch.search.profile.Timer;
import org.elasticsearch.search.profile.query.InternalProfileCollector;
import org.elasticsearch.search.profile.query.ProfileWeight;
import org.elasticsearch.search.profile.query.QueryProfileBreakdown;
import org.elasticsearch.search.profile.query.QueryProfiler;
import org.elasticsearch.search.profile.query.QueryTimingType;
import org.elasticsearch.search.query.QueryPhaseCollector;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -489,10 +487,8 @@ public void search(List<LeafReaderContext> leaves, Weight weight, Collector coll
}

private void doAggregationPostCollection(Collector collector) throws IOException {
if (collector instanceof QueryPhaseCollector queryPhaseCollector) {
queryPhaseCollector.doPostCollection();
} else if (collector instanceof InternalProfileCollector profilerCollector) {
profilerCollector.doPostCollection();
if (collector instanceof TwoPhaseCollector twoPhaseCollector) {
twoPhaseCollector.doPostCollection();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.internal;

import org.apache.lucene.search.Collector;

import java.io.IOException;

/** A {@link Collector} extension that allows to run a post-collection phase. This phase
* is run on the same thread as the collection phase. */
public interface TwoPhaseCollector extends Collector {

/**
* run post-collection phase
*/
void doPostCollection() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import org.apache.lucene.sandbox.search.ProfilerCollector;
import org.apache.lucene.search.Collector;
import org.elasticsearch.search.aggregations.BucketCollector;
import org.elasticsearch.search.query.QueryPhaseCollector;
import org.elasticsearch.search.internal.TwoPhaseCollector;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -27,7 +26,7 @@
* <p>
* InternalProfiler facilitates the linking of the Collector graph
*/
public class InternalProfileCollector extends ProfilerCollector {
public class InternalProfileCollector extends ProfilerCollector implements TwoPhaseCollector {

private final InternalProfileCollector[] children;
private final Collector wrappedCollector;
Expand Down Expand Up @@ -78,13 +77,10 @@ public CollectorResult getCollectorTree() {
return new CollectorResult(getName(), getReason(), getTime(), childResults);
}

@Override
public void doPostCollection() throws IOException {
if (wrappedCollector instanceof InternalProfileCollector profileCollector) {
profileCollector.doPostCollection();
} else if (wrappedCollector instanceof QueryPhaseCollector queryPhaseCollector) {
queryPhaseCollector.doPostCollection();
} else if (wrappedCollector instanceof BucketCollector.BucketCollectorWrapper aggsCollector) {
aggsCollector.bucketCollector().postCollection();
if (wrappedCollector instanceof TwoPhaseCollector twoPhaseCollector) {
twoPhaseCollector.doPostCollection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.Bits;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.search.aggregations.BucketCollector;
import org.elasticsearch.search.profile.query.InternalProfileCollector;
import org.elasticsearch.search.internal.TwoPhaseCollector;

import java.io.IOException;
import java.util.Objects;
Expand All @@ -40,7 +39,7 @@
* When top docs as well as aggs are collected (because both collectors were provided), skipping low scoring hits via
* {@link Scorable#setMinCompetitiveScore(float)} is not supported for either of the collectors.
*/
public final class QueryPhaseCollector implements Collector {
public final class QueryPhaseCollector implements TwoPhaseCollector {
private final Collector aggsCollector;
private final Collector topDocsCollector;
private final TerminateAfterChecker terminateAfterChecker;
Expand Down Expand Up @@ -374,11 +373,10 @@ boolean incrementHitCountAndCheckThreshold() {
}
};

@Override
public void doPostCollection() throws IOException {
if (aggsCollector instanceof BucketCollector.BucketCollectorWrapper bucketCollectorWrapper) {
bucketCollectorWrapper.bucketCollector().postCollection();
} else if (aggsCollector instanceof InternalProfileCollector profileCollector) {
profileCollector.doPostCollection();
if (aggsCollector instanceof TwoPhaseCollector twoPhaseCollector) {
twoPhaseCollector.doPostCollection();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should aggsCollector always be a TwoPhaseCollector? Since BucketCollectorWrapper and InternalProfileCollector implement that interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but trying to exploit that was a headache because some tests are abusing the Collector interface so I am leaving this for a follow up

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.index.AssertingDirectoryReader;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.search.AssertingIndexSearcher;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.BytesRef;
Expand Down Expand Up @@ -569,6 +568,7 @@ private <A extends InternalAggregation, C extends Aggregator> A searchAndReduce(
root.preCollection();
aggregators.add(root);
new TimeSeriesIndexSearcher(searcher, List.of()).search(rewritten, MultiBucketCollector.wrap(true, List.of(root)));
root.postCollection();
} else {
CollectorManager<Collector, Void> collectorManager = new CollectorManager<>() {
@Override
Expand All @@ -591,7 +591,6 @@ public Void reduce(Collection<Collector> collectors) {
}
}
for (C agg : aggregators) {
agg.postCollection();
internalAggs.add(agg.buildTopLevel());
}
} finally {
Expand Down Expand Up @@ -776,7 +775,6 @@ protected <R extends InternalAggregation> void debugTestCase(
Aggregator aggregator = createAggregator(builder, context);
aggregator.preCollection();
searcher.search(context.query(), aggregator.asCollector());
aggregator.postCollection();
InternalAggregation r = aggregator.buildTopLevel();
r = r.reduce(
List.of(r),
Expand Down Expand Up @@ -909,21 +907,16 @@ protected static DirectoryReader wrapInMockESDirectoryReader(DirectoryReader dir
* sets the IndexSearcher to run on concurrent mode.
*/
protected IndexSearcher newIndexSearcher(DirectoryReader indexReader) throws IOException {
if (randomBoolean()) {
// this executes basic query checks and asserts that weights are normalized only once etc.
return new AssertingIndexSearcher(random(), indexReader);
} else {
return new ContextIndexSearcher(
indexReader,
IndexSearcher.getDefaultSimilarity(),
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
randomBoolean(),
this.threadPoolExecutor,
this.threadPoolExecutor.getMaximumPoolSize(),
1 // forces multiple slices
);
}
return new ContextIndexSearcher(
indexReader,
IndexSearcher.getDefaultSimilarity(),
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
randomBoolean(),
this.threadPoolExecutor,
this.threadPoolExecutor.getMaximumPoolSize(),
1 // forces multiple slices
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public boolean supportsSampling() {
return true;
}

@Override
public boolean supportsParallelCollection() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this agg didn't support parallel collection also before this change?

Copy link
Contributor Author

@iverase iverase Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the postcollection method is accessing doc values created in the coordinating thread.

return false;
}

@Override
protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metadata) {
return new FrequentItemSetsAggregationBuilder(name, fields, minimumSupport, minimumSetSize, size, filter, executionHint);
Expand Down