diff options
author | Vladimir Sitnikov <sitnikov.vladimir@gmail.com> | 2019-01-28 01:09:07 +0300 |
---|---|---|
committer | Aman Sinha <asinha@maprtech.com> | 2019-02-01 10:16:43 -0800 |
commit | 9c82e06b93cf39bbbe11b90aaa6713806fa4aa36 (patch) | |
tree | 5a914602ae4d69f675a3164f9f901311305aeb29 | |
parent | 02422418edec35f34a547b929f68545c29a6de6a (diff) |
DRILL-7008: Drillbits: clear stale shutdown hooks
ShutdownThread is no longer required when Drillbit#close() is called.
mvn install for Drill project consumed 600MiB (there were 160 shutdown hooks)
close apache/drill#1625
-rw-r--r-- | exec/java-exec/src/main/java/org/apache/drill/exec/server/Drillbit.java | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/server/Drillbit.java b/exec/java-exec/src/main/java/org/apache/drill/exec/server/Drillbit.java index 8d413977f..b840fa6b5 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/server/Drillbit.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/server/Drillbit.java @@ -99,6 +99,7 @@ public class Drillbit implements AutoCloseable { private boolean quiescentMode; private boolean forcefulShutdown = false; private GracefulShutdownThread gracefulShutdownThread; + private Thread shutdownHook; private boolean interruptPollShutdown = true; public void setQuiescentMode(boolean quiescentMode) { @@ -222,7 +223,8 @@ public class Drillbit implements AutoCloseable { // Must start the RM after the above since it needs to read system options. drillbitContext.startRM(); - Runtime.getRuntime().addShutdownHook(new ShutdownThread(this, new StackTrace())); + shutdownHook = new ShutdownThread(this, new StackTrace()); + Runtime.getRuntime().addShutdownHook(shutdownHook); gracefulShutdownThread.start(); logger.info("Startup completed ({} ms).", w.elapsed(TimeUnit.MILLISECONDS)); } @@ -258,6 +260,17 @@ public class Drillbit implements AutoCloseable { } final Stopwatch w = Stopwatch.createStarted(); logger.debug("Shutdown begun."); + // We don't really want for Drillbits to pile up in memory, so the hook should be removed + // It might be better to use PhantomReferences to cleanup as soon as Drillbit becomes + // unreachable, however current approach seems to be good enough. + Thread shutdownHook = this.shutdownHook; + if (shutdownHook != null && Thread.currentThread() != shutdownHook) { + try { + Runtime.getRuntime().removeShutdownHook(shutdownHook); + } catch (IllegalArgumentException e) { + // If shutdown is in progress, just ignore the removal + } + } updateState(State.QUIESCENT); stateManager.setState(DrillbitState.GRACE); waitForGracePeriod(); |