List of usage examples for org.apache.hadoop.util ShutdownHookManager removeShutdownHook
@InterfaceAudience.Public @InterfaceStability.Stable public boolean removeShutdownHook(Runnable shutdownHook)
From source file:co.cask.cdap.app.runtime.spark.SparkRuntimeService.java
License:Apache License
/** * Cleanup all shutdown hooks added by Spark and execute them directly. * This is needed so that for CDAP standalone, it won't leak memory through shutdown hooks. *//*from ww w. ja va2 s .com*/ private void cleanupShutdownHooks() { // With Hadoop 2, Spark uses the Hadoop ShutdownHookManager ShutdownHookManager manager = ShutdownHookManager.get(); try { // Use reflection to get the shutdown hooks Method getShutdownHooksInOrder = manager.getClass().getDeclaredMethod("getShutdownHooksInOrder"); if (!Collection.class.isAssignableFrom(getShutdownHooksInOrder.getReturnType())) { LOG.warn("Unsupported method {}. Spark shutdown hooks cleanup skipped.", getShutdownHooksInOrder); return; } getShutdownHooksInOrder.setAccessible(true); // Filter out hooks that are defined in the same SparkRunnerClassLoader as this SparkProgramRunner class // This is for the case when there are concurrent Spark job running in the same VM List<Runnable> hooks = ImmutableList.copyOf(Iterables.filter( Iterables.filter((Collection<?>) getShutdownHooksInOrder.invoke(manager), Runnable.class), new Predicate<Runnable>() { @Override public boolean apply(Runnable runnable) { return runnable.getClass().getClassLoader() == SparkRuntimeService.this.getClass() .getClassLoader(); } })); for (Runnable hook : hooks) { LOG.debug("Running Spark shutdown hook {}", hook); hook.run(); manager.removeShutdownHook(hook); } } catch (Exception e) { LOG.warn("Failed to cleanup Spark shutdown hooks.", e); } }