This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
fix: Watchdog does not does not wait for executor to shutdown on awaitTermination #1884
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8414d5
fix: [Approach 3]Watchdog does not shut down executor on client closi…
blakeli0 b7b9be3
fix: Use CountDownLatch to make sure the last execution of run() meth…
blakeli0 5f39308
fix: Only check countdownLatch in isTerminated()
blakeli0 51fd5d3
fix: Use Phaser to make sure the last execution of run() method is co…
blakeli0 e8cfa03
fix: Reformat
blakeli0 b18a1b2
fix: Add tests
blakeli0 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -36,9 +36,11 @@ | |
import java.util.Map.Entry; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Phaser; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nonnull; | ||
|
@@ -66,7 +68,7 @@ public final class Watchdog implements Runnable, BackgroundResource { | |
// Dummy value to convert the ConcurrentHashMap into a Set | ||
private static Object PRESENT = new Object(); | ||
private final ConcurrentHashMap<WatchdogStream, Object> openStreams = new ConcurrentHashMap<>(); | ||
|
||
private final Phaser phaser; | ||
private final ApiClock clock; | ||
private final Duration scheduleInterval; | ||
private final ScheduledExecutorService executor; | ||
|
@@ -84,6 +86,8 @@ private Watchdog(ApiClock clock, Duration scheduleInterval, ScheduledExecutorSer | |
this.clock = Preconditions.checkNotNull(clock, "clock can't be null"); | ||
this.scheduleInterval = scheduleInterval; | ||
this.executor = executor; | ||
// Register the main thread | ||
this.phaser = new Phaser(1); | ||
} | ||
|
||
private void start() { | ||
|
@@ -113,10 +117,15 @@ public <ResponseT> ResponseObserver<ResponseT> watch( | |
|
||
@Override | ||
public void run() { | ||
// Register the current thread | ||
phaser.register(); | ||
try { | ||
runUnsafe(); | ||
} catch (Throwable t) { | ||
LOG.log(Level.SEVERE, "Caught throwable in periodic Watchdog run. Continuing.", t); | ||
} finally { | ||
// Unregister the current thread | ||
phaser.arriveAndDeregister(); | ||
} | ||
} | ||
|
||
|
@@ -134,26 +143,37 @@ private void runUnsafe() { | |
@Override | ||
public void shutdown() { | ||
future.cancel(false); | ||
// Unregister the main thread | ||
phaser.arriveAndDeregister(); | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return executor.isShutdown(); | ||
return future.isCancelled(); | ||
} | ||
|
||
@Override | ||
public boolean isTerminated() { | ||
return executor.isTerminated(); | ||
return phaser.isTerminated(); | ||
} | ||
|
||
@Override | ||
public void shutdownNow() { | ||
future.cancel(true); | ||
// Unregister the main thread | ||
phaser.arriveAndDeregister(); | ||
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. Might be good to avoid double calls: if (future.cancel(true)) { } |
||
} | ||
|
||
@Override | ||
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { | ||
return executor.awaitTermination(duration, unit); | ||
try { | ||
// Default phase is 0, this method wait until all parties arrive and unregister, then | ||
// terminate the Phaser | ||
phaser.awaitAdvanceInterruptibly(0, duration, unit); | ||
return true; | ||
} catch (TimeoutException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -173,6 +193,7 @@ enum State { | |
} | ||
|
||
class WatchdogStream<ResponseT> extends StateCheckingResponseObserver<ResponseT> { | ||
|
||
private final Object lock = new Object(); | ||
|
||
private final Duration waitTimeout; | ||
|
This file contains 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
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.
Might be good to avoid double calls:
if (future.cancel(true)) {
phaser.arriveAndDeregister();
}