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

Fix OrderByOperator.finish to wait for finishMemoryRevoke #16431

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions core/trino-main/src/main/java/io/trino/operator/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ default ListenableFuture<Void> isBlocked()
* Since memory revoking signal is delivered asynchronously to the Operator, implementation
* must gracefully handle the case when there no longer is any revocable memory allocated.
* <p>
* After this method is called on Operator the Driver is disallowed to call any
* processing methods on it (isBlocked/needsInput/addInput/getOutput) until
* {@link #finishMemoryRevoke()} is called.
* After this method is called on Operator the Driver is disallowed to call most of
* processing methods on it
* ({@link #isBlocked()}/{@link #needsInput()}/{@link #addInput(Page)}/{@link #getOutput()})
* until {@link #finishMemoryRevoke()} is called. {@link #finish()} is the only processing
* method that can be called during that time and {@link #close()} remains callable
* at any time.
*/
default ListenableFuture<Void> startMemoryRevoke()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private enum State

private Optional<Spiller> spiller = Optional.empty();
private ListenableFuture<Void> spillInProgress = immediateVoidFuture();
private Runnable finishMemoryRevoke = () -> {};
private Optional<Runnable> finishMemoryRevoke = Optional.empty();

private Iterator<Optional<Page>> sortedPages;

Expand Down Expand Up @@ -205,6 +205,9 @@ public void finish()
return;
}
checkSuccess(spillInProgress, "spilling failed");
if (finishMemoryRevoke.isPresent()) {
findepi marked this conversation as resolved.
Show resolved Hide resolved
return;
findepi marked this conversation as resolved.
Show resolved Hide resolved
}

if (state == State.NEEDS_INPUT) {
state = State.HAS_OUTPUT;
Expand All @@ -220,7 +223,8 @@ public void finish()
// spill since revocable memory could not be converted to user memory immediately
// TODO: this should be asynchronous
getFutureValue(spillToDisk());
finishMemoryRevoke.run();
findepi marked this conversation as resolved.
Show resolved Hide resolved
finishMemoryRevoke.orElseThrow().run();
finishMemoryRevoke = Optional.empty();
}
}

Expand Down Expand Up @@ -302,7 +306,7 @@ private ListenableFuture<Void> spillToDisk()

if (revocableMemoryContext.getBytes() == 0) {
verify(pageIndex.getPositionCount() == 0 || state == State.HAS_OUTPUT);
finishMemoryRevoke = () -> {};
finishMemoryRevoke = Optional.of(() -> {});
return immediateVoidFuture();
}

Expand All @@ -317,19 +321,19 @@ private ListenableFuture<Void> spillToDisk()

pageIndex.sort(sortChannels, sortOrder);
spillInProgress = spiller.get().spill(pageIndex.getSortedPages());
finishMemoryRevoke = () -> {
finishMemoryRevoke = Optional.of(() -> {
pageIndex.clear();
updateMemoryUsage();
};
});

return spillInProgress;
}

@Override
public void finishMemoryRevoke()
{
finishMemoryRevoke.run();
finishMemoryRevoke = () -> {};
finishMemoryRevoke.orElseThrow().run();
finishMemoryRevoke = Optional.empty();
}

private List<WorkProcessor<Page>> getSpilledPages()
Expand Down