Index: common/src/java/org/apache/hadoop/hive/conf/HiveConf.java =================================================================== --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java (revision 1158629) +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java (working copy) @@ -469,6 +469,8 @@ // The class responsible for logging client side performance metrics // Must be a subclass of org.apache.hadoop.hive.ql.log.PerfLogger HIVE_PERF_LOGGER("hive.exec.perf.logger", "org.apache.hadoop.hive.ql.log.PerfLogger"), + // Whether to delete the scrachdir while startup + HIVE_START_CLEANUP_SCRACHDIR("hive.start.cleanup.scrachdir", false), ; public final String varname; Index: conf/hive-default.xml =================================================================== --- conf/hive-default.xml (revision 1158629) +++ conf/hive-default.xml (working copy) @@ -1163,4 +1163,10 @@ The class responsible logging client side performance metrics. Must be a subclass of org.apache.hadoop.hive.ql.log.PerfLogger + + hive.start.cleanup.scrachdir + false + To cleanup the hive scrachdir while starting the hive server + + Index: service/src/java/org/apache/hadoop/hive/service/HiveServer.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveServer.java (revision 1158629) +++ service/src/java/org/apache/hadoop/hive/service/HiveServer.java (working copy) @@ -60,6 +60,8 @@ import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportFactory; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; import com.facebook.fb303.fb_status; @@ -660,10 +662,13 @@ HiveServerHandler.LOG.warn(e.getMessage()); } + HiveConf conf = new HiveConf(HiveServerHandler.class); + if (conf.getBoolVar(HiveConf.ConfVars.HIVE_START_CLEANUP_SCRACHDIR)) { + cleanUpScratchDir(conf); + } + TServerTransport serverTransport = new TServerSocket(cli.port); - HiveConf conf = new HiveConf(HiveServerHandler.class); - // set all properties specified on the command line for (Map.Entry item : hiveconf.entrySet()) { conf.set((String) item.getKey(), (String) item.getValue()); @@ -692,4 +697,18 @@ x.printStackTrace(); } } + + private static void cleanUpScratchDir(HiveConf hiveConf) { + 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) { + HiveServerHandler.LOG.warn("Unable to delete scratchDir : " + hiveScratchDir, e); + } + } } Index: service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java =================================================================== --- service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java (revision 1158629) +++ 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; @@ -390,4 +391,43 @@ } } + public void testScratchDirShouldNotClearWhileStartup() throws Exception { + FileSystem fs = FileSystem.get(conf); + Path scratchDirPath = new Path(HiveConf.getVar(conf, + HiveConf.ConfVars.SCRATCHDIR)); + boolean fileExists = fs.exists(scratchDirPath); + if (!fileExists) { + fileExists = fs.mkdirs(scratchDirPath); + } + new Thread() { + public void run() { + HiveServer.main(new String[0]); + } + }.start(); + Thread.sleep(10000); + assertTrue("Scratch dir is not available after startup", fs.exists(scratchDirPath)); + } + + public void testScratchDirShouldClearWhileStartup() throws Exception { + FileSystem fs = FileSystem.get(conf); + Path scratchDirPath = new Path(HiveConf.getVar(conf, + HiveConf.ConfVars.SCRATCHDIR)); + boolean fileExists = fs.exists(scratchDirPath); + if (!fileExists) { + fileExists = fs.mkdirs(scratchDirPath); + } + try { + System.setProperty(HiveConf.ConfVars.HIVE_START_CLEANUP_SCRACHDIR.toString(), "true"); + new Thread() { + public void run() { + HiveServer.main(new String[0]); + } + }.start(); + Thread.sleep(10000); + } finally { + System.setProperty(HiveConf.ConfVars.HIVE_START_CLEANUP_SCRACHDIR.toString(), "false"); + } + assertFalse("Scratch dir is available after startup", fs.exists(scratchDirPath)); + } + }