-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Avoid over collecting in Limit or Lucene Operator #123296
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| pr: 123296 | ||
| summary: Avoid over collecting in Limit or Lucene Operator | ||
| area: ES|QL | ||
| type: bug | ||
| issues: [] |
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/Limiter.java
This file contains hidden or 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,72 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.compute.operator; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| /** | ||
| * A shared limiter used by multiple drivers to collect hits in parallel without exceeding the output limit. | ||
| * For example, if the query `FROM test-1,test-2 | LIMIT 100` is run with two drivers, and one driver (e.g., querying `test-1`) | ||
| * has collected 60 hits, then the other driver querying `test-2` should collect at most 40 hits. | ||
| */ | ||
| public class Limiter { | ||
| private final int limit; | ||
| private final AtomicInteger collected = new AtomicInteger(); | ||
|
|
||
| public static Limiter NO_LIMIT = new Limiter(Integer.MAX_VALUE) { | ||
| @Override | ||
| public int tryAccumulateHits(int numHits) { | ||
| return numHits; | ||
| } | ||
|
|
||
| @Override | ||
| public int remaining() { | ||
| return Integer.MAX_VALUE; | ||
| } | ||
| }; | ||
|
|
||
| public Limiter(int limit) { | ||
| this.limit = limit; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the remaining number of hits that can be collected. | ||
| */ | ||
| public int remaining() { | ||
| final int remaining = limit - collected.get(); | ||
| assert remaining >= 0 : remaining; | ||
| return remaining; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the limit of this limiter. | ||
| */ | ||
| public int limit() { | ||
| return limit; | ||
| } | ||
|
|
||
| /** | ||
| * Tries to accumulate hits and returns the number of hits that has been accepted. | ||
| * | ||
| * @param numHits the number of hits to try to accumulate | ||
| * @return the accepted number of hits. If the returned number is less than the numHits, | ||
| * it means the limit has been reached and the difference can be discarded. | ||
| */ | ||
| public int tryAccumulateHits(int numHits) { | ||
| while (true) { | ||
| int curVal = collected.get(); | ||
| if (curVal >= limit) { | ||
| return 0; | ||
| } | ||
| final int toAccept = Math.min(limit - curVal, numHits); | ||
| if (collected.compareAndSet(curVal, curVal + toAccept)) { | ||
| return toAccept; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea!
I wonder if we should make it explicit that this works as long as
test-1andtest-2are on the same node?