-
Notifications
You must be signed in to change notification settings - Fork 14
feature(ActionTimeout): add garbage collection time tracking #418
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
3 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
204 changes: 117 additions & 87 deletions
204
sdk/src/main/server-api/sc/framework/plugins/ActionTimeout.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 |
|---|---|---|
| @@ -1,127 +1,157 @@ | ||
| package sc.framework.plugins; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.GarbageCollectorMXBean; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| // TODO We can probably utilise an inbuilt class instead. | ||
| /** Tracks timeouts in Milliseconds. */ | ||
| public class ActionTimeout { | ||
| static final Logger logger = LoggerFactory.getLogger(ActionTimeout.class); | ||
|
|
||
| private final long softTimeoutInMilliseconds; | ||
|
|
||
| private final long hardTimeoutInMilliseconds; | ||
| static final Logger logger = LoggerFactory.getLogger(ActionTimeout.class); | ||
|
|
||
| private final boolean canTimeout; | ||
| private final long softTimeoutInMilliseconds; | ||
|
|
||
| private Thread timeoutThread; | ||
| private final long hardTimeoutInMilliseconds; | ||
|
|
||
| private Status status = Status.NEW; | ||
| private final boolean canTimeout; | ||
|
|
||
| private long startTimestamp = 0; | ||
| private long stopTimestamp = 0; | ||
| private Thread timeoutThread; | ||
|
|
||
| private static final int DEFAULT_HARD_TIMEOUT = 10000; | ||
| private static final int DEFAULT_SOFT_TIMEOUT = 5000; | ||
| private Status status = Status.NEW; | ||
|
|
||
| private enum Status { | ||
| NEW, STARTED, STOPPED | ||
| } | ||
| private long startTimestamp = 0; | ||
| private long stopTimestamp = 0; | ||
| private long gcStartTime = 0; | ||
| private long gcStopTime = 0; | ||
|
|
||
| public ActionTimeout(boolean canTimeout) { | ||
| this(canTimeout, DEFAULT_HARD_TIMEOUT, DEFAULT_SOFT_TIMEOUT); | ||
| } | ||
| private static final int DEFAULT_HARD_TIMEOUT = 10000; | ||
| private static final int DEFAULT_SOFT_TIMEOUT = 2000; | ||
|
|
||
| public ActionTimeout(boolean canTimeout, int hardTimeoutInMilliseconds) { | ||
| this(canTimeout, hardTimeoutInMilliseconds, hardTimeoutInMilliseconds); | ||
| } | ||
| private enum Status { | ||
| NEW, STARTED, STOPPED | ||
| } | ||
|
|
||
| public ActionTimeout(boolean canTimeout, int hardTimeoutInMilliseconds, int softTimeoutInMilliseconds) { | ||
| if (hardTimeoutInMilliseconds < softTimeoutInMilliseconds) { | ||
| throw new IllegalArgumentException( | ||
| "HardTimeout must be greater or equal the SoftTimeout"); | ||
| public ActionTimeout(boolean canTimeout) { | ||
| this(canTimeout, DEFAULT_HARD_TIMEOUT, DEFAULT_SOFT_TIMEOUT); | ||
| } | ||
|
|
||
| this.canTimeout = canTimeout; | ||
| this.softTimeoutInMilliseconds = softTimeoutInMilliseconds; | ||
| this.hardTimeoutInMilliseconds = hardTimeoutInMilliseconds; | ||
| } | ||
| public ActionTimeout(boolean canTimeout, int hardTimeoutInMilliseconds) { | ||
| this(canTimeout, hardTimeoutInMilliseconds, hardTimeoutInMilliseconds); | ||
| } | ||
|
|
||
| public long getHardTimeout() { | ||
| return this.hardTimeoutInMilliseconds; | ||
| } | ||
| public ActionTimeout(boolean canTimeout, int hardTimeoutInMilliseconds, int softTimeoutInMilliseconds) { | ||
| if (hardTimeoutInMilliseconds < softTimeoutInMilliseconds) { | ||
| throw new IllegalArgumentException( | ||
| "HardTimeout must be greater or equal the SoftTimeout"); | ||
| } | ||
|
|
||
| public long getSoftTimeout() { | ||
| return this.softTimeoutInMilliseconds; | ||
| } | ||
| this.canTimeout = canTimeout; | ||
| this.softTimeoutInMilliseconds = softTimeoutInMilliseconds; | ||
| this.hardTimeoutInMilliseconds = hardTimeoutInMilliseconds; | ||
| } | ||
|
|
||
| public boolean canTimeout() { | ||
| return this.canTimeout; | ||
| } | ||
| public long getHardTimeout() { | ||
| return this.hardTimeoutInMilliseconds; | ||
| } | ||
|
|
||
| public long getTimeDiff() { | ||
| if (this.status == Status.NEW) { | ||
| throw new IllegalStateException("Timeout was never started."); | ||
| public long getSoftTimeout() { | ||
| return this.softTimeoutInMilliseconds; | ||
| } | ||
|
|
||
| if (this.status == Status.STARTED) { | ||
| throw new IllegalStateException("Timeout was not stopped."); | ||
| public boolean canTimeout() { | ||
| return this.canTimeout; | ||
| } | ||
|
|
||
| return stopTimestamp - startTimestamp; | ||
| } | ||
| public long getTimeDiff() { | ||
| if (this.status == Status.NEW) { | ||
| throw new IllegalStateException("Timeout was never started."); | ||
| } | ||
|
|
||
| if (this.status == Status.STARTED) { | ||
| throw new IllegalStateException("Timeout was not stopped."); | ||
| } | ||
|
|
||
| public synchronized boolean didTimeout() { | ||
| return this.canTimeout() && this.getTimeDiff() > this.softTimeoutInMilliseconds; | ||
| } | ||
|
|
||
| public synchronized void stop() { | ||
| if (this.status == Status.NEW) { | ||
| throw new IllegalStateException("Timeout was never started."); | ||
| logger.info("Garbage collection time: {} {} {}", gcStopTime, gcStartTime, gcStopTime - gcStartTime); | ||
| logger.info("Real time difference: {}", stopTimestamp - startTimestamp); | ||
| logger.info("Unaltered time difference: {}", stopTimestamp - startTimestamp - gcStopTime + gcStartTime); | ||
| // Subtract the garbage collection time for an unaltered time | ||
| return stopTimestamp - startTimestamp - gcStopTime + gcStartTime; | ||
| } | ||
|
|
||
| if (this.status == Status.STOPPED) { | ||
| logger.warn("Redundant stop: Timeout was already stopped."); | ||
| return; | ||
| public synchronized boolean didTimeout() { | ||
| return this.canTimeout() && this.getTimeDiff() > this.softTimeoutInMilliseconds; | ||
| } | ||
|
|
||
| this.stopTimestamp = System.currentTimeMillis(); | ||
| this.status = Status.STOPPED; | ||
| public synchronized void stop() { | ||
| if (this.status == Status.NEW) { | ||
| throw new IllegalStateException("Timeout was never started."); | ||
| } | ||
|
|
||
| if (this.status == Status.STOPPED) { | ||
| logger.warn("Redundant stop: Timeout was already stopped."); | ||
| return; | ||
| } | ||
|
|
||
| this.stopTimestamp = System.currentTimeMillis(); | ||
| this.status = Status.STOPPED; | ||
|
|
||
| if (this.timeoutThread != null) { | ||
| this.timeoutThread.interrupt(); | ||
| } | ||
|
|
||
| if (this.timeoutThread != null) { | ||
| this.timeoutThread.interrupt(); | ||
| // Get the garbage collection time | ||
| this.gcStopTime = getGCTime(); | ||
| } | ||
| } | ||
|
|
||
| public synchronized void start(final Runnable onTimeout) { | ||
| if (this.status != Status.NEW) { | ||
| throw new IllegalStateException("Redundant start: was already started!"); | ||
| public synchronized void start(final Runnable onTimeout) { | ||
| if (this.status != Status.NEW) { | ||
| throw new IllegalStateException("Redundant start: was already started!"); | ||
| } | ||
|
|
||
| if (canTimeout()) { | ||
| this.timeoutThread = new Thread(() -> { | ||
| try { | ||
| Thread.sleep(getHardTimeout()); | ||
| stop(); | ||
| onTimeout.run(); | ||
| } catch (InterruptedException e) { | ||
| logger.info("HardTimeout wasn't reached."); | ||
| } | ||
| }); | ||
|
|
||
| // Get the garbage collection time | ||
| this.gcStartTime = getGCTime(); | ||
| this.timeoutThread.start(); | ||
| } | ||
|
|
||
| this.startTimestamp = System.currentTimeMillis(); | ||
| this.status = Status.STARTED; | ||
| } | ||
|
|
||
| if (canTimeout()) { | ||
| this.timeoutThread = new Thread(() -> { | ||
| try { | ||
| Thread.sleep(getHardTimeout()); | ||
| stop(); | ||
| onTimeout.run(); | ||
| } catch (InterruptedException e) { | ||
| logger.info("HardTimout wasn't reached."); | ||
| /** | ||
| * Measures the total garbage collection time. | ||
| * This is meant to be used as a value relative to a start time. | ||
| * @return The total garbage collection time | ||
| */ | ||
| private long getGCTime() { | ||
| long gcTime = 0; | ||
| for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { | ||
| gcTime += gcBean.getCollectionTime(); | ||
| } | ||
| }); | ||
| this.timeoutThread.start(); | ||
| return gcTime; | ||
| } | ||
|
|
||
| this.startTimestamp = System.currentTimeMillis(); | ||
| this.status = Status.STARTED; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ActionTimeout{" + | ||
| "canTimeout=" + canTimeout + | ||
| ", status=" + status + | ||
| ", start=" + startTimestamp + | ||
| ", stop=" + stopTimestamp + | ||
| '}'; | ||
| } | ||
| } | ||
| @Override | ||
| public String toString() { | ||
| return "ActionTimeout{" + | ||
| "canTimeout=" + canTimeout + | ||
| ", status=" + status + | ||
| ", start=" + startTimestamp + | ||
| ", stop=" + stopTimestamp + | ||
| ", gcStart=" + gcStartTime + | ||
| ", gcStop=" + gcStopTime + | ||
| '}'; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.