Index: common/src/java/org/apache/hadoop/hive/conf/HiveConf.java =================================================================== --- common/src/java/org/apache/hadoop/hive/conf/HiveConf.java (revision 1156493) +++ common/src/java/org/apache/hadoop/hive/conf/HiveConf.java (working copy) @@ -458,6 +458,7 @@ HIVE_MAPPER_CANNOT_SPAN_MULTIPLE_PARTITIONS("hive.mapper.cannot.span.multiple.partitions", false), HIVE_REWORK_MAPREDWORK("hive.rework.mapredwork", false), HIVE_CONCATENATE_CHECK_INDEX ("hive.exec.concatenate.check.index", true), + HIVE_START_CLEANUP_SCRACHDIR("hive.start.cleanup.scrachdir", false) ; public final String varname; Index: conf/hive-default.xml =================================================================== --- conf/hive-default.xml (revision 1156493) +++ conf/hive-default.xml (working copy) @@ -1144,4 +1144,10 @@ by record readers + + hive.start.cleanup.scrachdir + false + To cleanup the hive scrachdir while starting the hive server + + Index: data/conf/hive-site.xml =================================================================== --- data/conf/hive-site.xml (revision 1156493) +++ data/conf/hive-site.xml (working copy) @@ -176,4 +176,10 @@ The default input format, if it is not specified, the system assigns it. It is set to HiveInputFormat for hadoop versions 17, 18 and 19, whereas it is set to CombineHiveInputFormat for hadoop 20. The user can always overwrite it - if there is a bug in CombineHiveInputFormat, it can always be manually set to HiveInputFormat. + + hive.start.cleanup.scrachdir + true + 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 1156493) +++ 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,19 @@ 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) { + String exMessage = "Unable to delete scratchDir : " + hiveScratchDir; + HiveServerHandler.LOG.warn(exMessage, e); + } + } } Index: service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java =================================================================== --- service/src/test/org/apache/hadoop/hive/service/TestHiveServer.java (revision 1156493) +++ 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,21 @@ } } + public void testShouldClearScratchDirWhileStartup() 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); + assertFalse("Scratch dir is available after startup", fs.exists(scratchDirPath)); + } + }