diff --git a/common/src/java/org/apache/hive/common/util/ShutdownHookManager.java b/common/src/java/org/apache/hive/common/util/ShutdownHookManager.java index fd2f20a..fb6672f 100644 --- a/common/src/java/org/apache/hive/common/util/ShutdownHookManager.java +++ b/common/src/java/org/apache/hive/common/util/ShutdownHookManager.java @@ -18,6 +18,7 @@ package org.apache.hive.common.util; +import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -44,20 +45,23 @@ private static final ShutdownHookManager MGR = new ShutdownHookManager(); + private static final DeleteOnExitHook DELETE_ON_EXIT_HOOK = new DeleteOnExitHook(); + private static final Log LOG = LogFactory.getLog(ShutdownHookManager.class); static { + MGR.addShutdownHookInternal(DELETE_ON_EXIT_HOOK, 0); Runtime.getRuntime().addShutdownHook( - new Thread() { - @Override - public void run() { - MGR.shutdownInProgress.set(true); - for (Runnable hook: MGR.getShutdownHooksInOrder()) { - try { - hook.run(); - } catch (Throwable ex) { - LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() + - "' failed, " + ex.toString(), ex); + new Thread() { + @Override + public void run() { + MGR.shutdownInProgress.set(true); + for (Runnable hook : MGR.getShutdownHooksInOrderInternal()) { + try { + hook.run(); + } catch (Throwable ex) { + LOG.warn("ShutdownHook '" + hook.getClass().getSimpleName() + + "' failed, " + ex.toString(), ex); } } } @@ -202,4 +206,29 @@ public static boolean isShutdownInProgress() { private boolean isShutdownInProgressInternal() { return shutdownInProgress.get(); } + + public static void deleteOnExit(File file) { + if (isShutdownInProgress()) { + throw new IllegalStateException("Shutdown in progress, cannot add a deleteOnExit"); + } + DELETE_ON_EXIT_HOOK.deleteTargets.add(file); + } + + public static void cancelDeleteOnExit(File file) { + if (isShutdownInProgress()) { + throw new IllegalStateException("Shutdown in progress, cannot cancel a deleteOnExit"); + } + DELETE_ON_EXIT_HOOK.deleteTargets.remove(file); + } + + private static class DeleteOnExitHook implements Runnable { + private final Set deleteTargets = new HashSet(); + + @Override + public void run() { + for (File deleteTarget : deleteTargets) { + deleteTarget.delete(); + } + } + } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 7ed8e5f..4062bee 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -87,6 +87,7 @@ import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.util.ReflectionUtils; import org.apache.hadoop.util.Shell; +import org.apache.hive.common.util.ShutdownHookManager; import com.google.common.base.Preconditions; @@ -285,6 +286,16 @@ public void setTmpOutputFile(File f) { tmpOutputFile = f; } + public boolean deleteTmpOutputFile() { + if (tmpOutputFile != null) { + tmpOutputFile.delete(); + ShutdownHookManager.cancelDeleteOnExit(tmpOutputFile); + // do not nullify. will be reused + return true; + } + return false; + } + public boolean getIsSilent() { if(conf != null) { return conf.getBoolVar(HiveConf.ConfVars.HIVESESSIONSILENT); @@ -800,7 +811,7 @@ private static File createTempFile(HiveConf conf) throws IOException { } } File tmpFile = File.createTempFile(sessionID, ".pipeout", tmpDir); - tmpFile.deleteOnExit(); + ShutdownHookManager.deleteOnExit(tmpFile); return tmpFile; } @@ -1493,6 +1504,7 @@ public void close() throws IOException { closeSparkSession(); registry.closeCUDFLoaders(); dropSessionPaths(conf); + deleteTmpOutputFile(); } public void closeSparkSession() { diff --git a/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java b/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java index bcc66cf..9f8fae8 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/HiveCommandOperation.java @@ -200,8 +200,7 @@ public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws H private void cleanTmpFile() { resetResultReader(); SessionState sessionState = getParentSession().getSessionState(); - File tmp = sessionState.getTmpOutputFile(); - tmp.delete(); + sessionState.deleteTmpOutputFile(); } private void resetResultReader() { diff --git a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java index cc9df76..bda7f5b 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java @@ -287,9 +287,7 @@ private void cleanup(OperationState state) throws HiveSQLException { driver = null; SessionState ss = SessionState.get(); - if (ss.getTmpOutputFile() != null) { - ss.getTmpOutputFile().delete(); - } + ss.deleteTmpOutputFile(); } @Override