-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix: Make scheduledFuture thread-safe in HttpNativeExecutionTaskResultFetcher #26649
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| import com.facebook.presto.spi.PrestoException; | ||
| import com.facebook.presto.spi.page.SerializedPage; | ||
|
|
||
| import javax.annotation.concurrent.GuardedBy; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
@@ -67,10 +69,9 @@ public class HttpNativeExecutionTaskResultFetcher | |
| private final Object taskHasResult; | ||
| private final AtomicReference<Throwable> lastException = new AtomicReference<>(); | ||
|
|
||
| @GuardedBy("this") | ||
| private ScheduledFuture<?> scheduledFuture; | ||
|
|
||
| private volatile boolean completed; | ||
|
|
||
| private long token; | ||
|
|
||
| public HttpNativeExecutionTaskResultFetcher( | ||
|
|
@@ -86,15 +87,15 @@ public HttpNativeExecutionTaskResultFetcher( | |
| this.taskHasResult = requireNonNull(taskHasResult, "taskHasResult is null"); | ||
| } | ||
|
|
||
| public void start() | ||
| public synchronized void start() | ||
| { | ||
| scheduledFuture = scheduler.scheduleAtFixedRate(this::doGetResults, | ||
| 0, | ||
| (long) FETCH_INTERVAL.getValue(), | ||
| FETCH_INTERVAL.getUnit()); | ||
| } | ||
|
|
||
| public void stop(boolean success) | ||
| public synchronized void stop(boolean success) | ||
| { | ||
| if (scheduledFuture != null) { | ||
| scheduledFuture.cancel(false); | ||
|
|
@@ -129,7 +130,7 @@ public boolean hasPage() | |
| return !pageBuffer.isEmpty(); | ||
| } | ||
|
|
||
| private void throwIfFailed() | ||
| private synchronized void throwIfFailed() | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (scheduledFuture != null && scheduledFuture.isCancelled() && lastException.get() != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| Throwable failure = lastException.get(); | ||
|
|
@@ -140,11 +141,6 @@ private void throwIfFailed() | |
|
|
||
| private void doGetResults() | ||
| { | ||
| if (completed && scheduledFuture != null) { | ||
| scheduledFuture.cancel(false); | ||
| return; | ||
| } | ||
|
|
||
| if (bufferMemoryBytes.longValue() >= MAX_BUFFER_SIZE.toBytes()) { | ||
| return; | ||
| } | ||
|
|
@@ -159,7 +155,7 @@ private void doGetResults() | |
| } | ||
| } | ||
|
|
||
| private void onSuccess(PageBufferClient.PagesResponse pagesResponse) | ||
| private synchronized void onSuccess(PageBufferClient.PagesResponse pagesResponse) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xin-zhang2 thanks for this change. I have a bit of concern about the For example, if another thread holds the lock on
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though concerning, this shall be alright. Outside of this file the sites are only waiting on this taskHasResult object. Only this file has the responsibility on notifying. This means all synchronized(taskHasResult) outside of this file will and should have taskHasResult.wait() in the block, which releases the lock. As long as they don't call this onSuccess or onFailure methods between synchronized(taskHasResult) and taskHasResult.wait() we should be good. And we don't have such cases in the codebase.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. Yes, on one hand you are correct, the And in fact, after checking the code in detail, I believe that a deadlock may occur between methods |
||
| { | ||
| List<SerializedPage> pages = pagesResponse.getPages(); | ||
| long bytes = 0; | ||
|
|
@@ -185,7 +181,6 @@ private void onSuccess(PageBufferClient.PagesResponse pagesResponse) | |
| } | ||
| token = nextToken; | ||
| if (pagesResponse.isClientComplete()) { | ||
| completed = true; | ||
| workerClient.abortResultsAsync(taskId); | ||
| if (scheduledFuture != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this shall not be null at this point, right? could we force a non-null check here instead? |
||
| scheduledFuture.cancel(false); | ||
|
|
||
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.
ditto. should we do a check instead? we should enforce this class to be started before calling stop()