Skip to content
Merged
Changes from 2 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 @@ -21,8 +21,6 @@
import org.apache.lucene.search.TopScoreDocCollectorManager;
import org.apache.lucene.util.RamUsageEstimator;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.DocBlock;
import org.elasticsearch.compute.data.DocVector;
import org.elasticsearch.compute.data.DoubleBlock;
Expand All @@ -42,6 +40,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -91,8 +90,7 @@ public Factory(
public SourceOperator get(DriverContext driverContext) {
return new LuceneTopNSourceOperator(
contexts,
driverContext.breaker(),
driverContext.blockFactory(),
driverContext,
maxPageSize,
sorts,
estimatedPerRowSortSize,
Expand Down Expand Up @@ -126,7 +124,7 @@ public String describe() {
// We use the same value as the INITIAL_INTERVAL from CancellableBulkScorer
private static final int NUM_DOCS_INTERVAL = 1 << 12;

private final CircuitBreaker breaker;
private final DriverContext driverContext;
private final List<SortBuilder<?>> sorts;
private final long estimatedPerRowSortSize;
private final int limit;
Expand All @@ -151,22 +149,21 @@ public String describe() {

public LuceneTopNSourceOperator(
List<? extends ShardContext> contexts,
CircuitBreaker breaker,
BlockFactory blockFactory,
DriverContext driverContext,
int maxPageSize,
List<SortBuilder<?>> sorts,
long estimatedPerRowSortSize,
int limit,
LuceneSliceQueue sliceQueue,
boolean needsScore
) {
super(contexts, blockFactory, maxPageSize, sliceQueue);
this.breaker = breaker;
super(contexts, driverContext.blockFactory(), maxPageSize, sliceQueue);
this.driverContext = driverContext;
this.sorts = sorts;
this.estimatedPerRowSortSize = estimatedPerRowSortSize;
this.limit = limit;
this.needsScore = needsScore;
breaker.addEstimateBytesAndMaybeBreak(reserveSize(), "esql lucene topn");
driverContext.breaker().addEstimateBytesAndMaybeBreak(reserveSize(), "esql lucene topn");
}

@Override
Expand Down Expand Up @@ -201,34 +198,49 @@ public Page getCheckedOutput() throws IOException {

private Page collect() throws IOException {
assert doneCollecting == false;
long start = System.nanoTime();

var scorer = getCurrentOrLoadNextScorer();
if (scorer == null) {
doneCollecting = true;
startEmitting();
return emit();
}
try {

while (scorer != null) {
if (scorer.tags().isEmpty() == false) {
throw new UnsupportedOperationException("tags not supported by " + getClass());
}
if (perShardCollector == null || perShardCollector.shardContext.index() != scorer.shardContext().index()) {
// TODO: share the bottom between shardCollectors
perShardCollector = newPerShardCollector(scorer.shardContext(), sorts, needsScore, limit);

try {
if (perShardCollector == null || perShardCollector.shardContext.index() != scorer.shardContext().index()) {
// TODO: share the bottom between shardCollectors
perShardCollector = newPerShardCollector(scorer.shardContext(), sorts, needsScore, limit);
}
var leafCollector = perShardCollector.getLeafCollector(scorer.leafReaderContext());
scorer.scoreNextRange(leafCollector, scorer.leafReaderContext().reader().getLiveDocs(), NUM_DOCS_INTERVAL);
} catch (CollectionTerminatedException cte) {
// Lucene terminated early the collection (doing topN for an index that's sorted and the topN uses the same sorting)
scorer.markAsDone();
}
var leafCollector = perShardCollector.getLeafCollector(scorer.leafReaderContext());
scorer.scoreNextRange(leafCollector, scorer.leafReaderContext().reader().getLiveDocs(), NUM_DOCS_INTERVAL);
} catch (CollectionTerminatedException cte) {
// Lucene terminated early the collection (doing topN for an index that's sorted and the topN uses the same sorting)
scorer.markAsDone();
}
if (scorer.isDone()) {
var nextScorer = getCurrentOrLoadNextScorer();
if (nextScorer == null || nextScorer.shardContext().index() != scorer.shardContext().index()) {
startEmitting();
return emit();

// check if the query has been cancelled.
driverContext.checkForEarlyTermination();

if (scorer.isDone()) {
var nextScorer = getCurrentOrLoadNextScorer();
if (nextScorer != null && nextScorer.shardContext().index() != scorer.shardContext().index()) {
startEmitting();
return emit();
}
scorer = nextScorer;
}

// If we stayed longer than 1 second to execute getOutput, we should return back to the driver, so we can update its status.
// Even if this should almost never happen, we want to update the driver status even when a query runs "forever".
if (TimeUnit.SECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS) >= 1) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd yank this into a constant - maybe even one in nanos. Just a little more expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep - makes sense - guess I was in too much of a hurry - addressed.

Copy link
Member

Choose a reason for hiding this comment

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

Not too much a of hurry, just a little thing.

return null;
}
}
return null;

doneCollecting = true;
startEmitting();
return emit();
}

private boolean isEmitting() {
Expand Down Expand Up @@ -348,7 +360,7 @@ protected void describe(StringBuilder sb) {

@Override
protected void additionalClose() {
Releasables.close(() -> breaker.addWithoutBreaking(-reserveSize()));
Releasables.close(() -> driverContext.breaker().addWithoutBreaking(-reserveSize()));
}

private long reserveSize() {
Expand Down