diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/Utilities.java ql/src/java/org/apache/hadoop/hive/ql/exec/Utilities.java index fc8c948..1a16a7a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/Utilities.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/Utilities.java @@ -2504,5 +2504,33 @@ public static String formatMsecToStr(long msec) { return sb.toString(); } + + + /** + * Create a temp dir in specified baseDir + * This can go away once hive moves to support only JDK 7 + * and can use Files.createTempDirectory + * Guava Files.createTempDir() does not take a base dir + * @param baseDir - directory under which new temp dir will be created + * @return File object for new temp dir + */ + public static File createTempDir(String baseDir){ + //try creating the temp dir MAX_ATTEMPTS times + final int MAX_ATTEMPS = 30; + for(int i = 0; i < MAX_ATTEMPS; i++){ + //pick a random file name + String tempDirName = "tmp_" + ((int)(100000 * Math.random())); + + //return if dir could successfully be created with that file name + File tempDir = new File(baseDir, tempDirName); + if(tempDir.mkdir()){ + return tempDir; + } + } + throw new IllegalStateException("Failed to create a temp dir under " + + baseDir + " Giving up after " + MAX_ATTEMPS + " attemps"); + + } + } diff --git ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java index 41c663c..c292796 100644 --- ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java +++ ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java @@ -1225,9 +1225,12 @@ public QTestSetup() { public void preTest(HiveConf conf) throws Exception { if (zooKeeperCluster == null) { - String tmpdir = System.getProperty("user.dir")+"/../build/ql/tmp"; + //create temp dir + String tmpBaseDir = System.getProperty("user.dir")+"/../build/ql/tmp"; + File tmpDir = Utilities.createTempDir(tmpBaseDir); + zooKeeperCluster = new MiniZooKeeperCluster(); - zkPort = zooKeeperCluster.startup(new File(tmpdir, "zookeeper")); + zkPort = zooKeeperCluster.startup(tmpDir); } if (zooKeeper != null) {