Index: service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java =================================================================== --- service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java (revision 1127457) +++ service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java (working copy) @@ -23,6 +23,7 @@ import junit.framework.TestCase; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.api.FieldSchema; @@ -385,4 +386,16 @@ } } + public void testShouldClearScratchDirWhileStartup() throws Exception { + FileSystem fs = FileSystem.get(conf); + assertTrue("Scratch dir is not available at startup",fs.exists(new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIR)))); + new Thread(){ + @Override + public void run() { + HiveServer.main(new String[0]); + } + }.start(); + Thread.sleep(10000); + assertFalse("Scratch dir is available after startup",fs.exists(new Path(HiveConf.getVar(conf, HiveConf.ConfVars.SCRATCHDIR)))); + } } Index: service/src/java/org/apache/hadoop/hive/service/HiveServer.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveServer.java (revision 1127457) +++ service/src/java/org/apache/hadoop/hive/service/HiveServer.java (working copy) @@ -31,6 +31,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.HiveMetaStore; import org.apache.hadoop.hive.metastore.api.MetaException; @@ -527,6 +529,7 @@ public static void main(String[] args) { try { + cleanUpScratchDir(); //Initializing log for the HiveServer Mode SessionState.initHiveLog4j(); @@ -551,4 +554,23 @@ x.printStackTrace(); } } + + private static void cleanUpScratchDir() { + HiveConf hiveConf = new HiveConf(HiveServer.class); + String hiveScratchDir = hiveConf.get(HiveConf.ConfVars.SCRATCHDIR.varname); + try { + Path jobScratchDir = new Path(hiveScratchDir); + HiveServerHandler.LOG.info("Cleaning scratchDir : " + hiveScratchDir); + FileSystem fileSystem = jobScratchDir.getFileSystem(hiveConf); + fileSystem.delete(jobScratchDir, true); + } + // Even if the cleanup throws some exception we can start. + catch (Throwable e) { + String exMessage = "Unable to delete scratchDir : "; + HiveServerHandler.LOG.warn(exMessage + hiveScratchDir); + if (HiveServerHandler.LOG.isDebugEnabled()) { + HiveServerHandler.LOG.debug(exMessage + hiveScratchDir, e); + } + } + } }