hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hashut...@apache.org
Subject svn commit: r1429916 - /hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
Date Mon, 07 Jan 2013 17:39:16 GMT
Author: hashutosh
Date: Mon Jan  7 17:39:15 2013
New Revision: 1429916

URL: http://svn.apache.org/viewvc?rev=1429916&view=rev
Log:
HIVE-3431 : Avoid race conditions while downloading resources from non-local filesystem (Navis
via Ashutosh Chauhan)

Modified:
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java?rev=1429916&r1=1429915&r2=1429916&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java Mon Jan  7
17:39:15 2013
@@ -603,15 +603,35 @@ public class SessionState {
   private String downloadResource(String value, boolean convertToUnix) {
     if (canDownloadResource(value)) {
       getConsole().printInfo("converting to local " + value);
-      File resourceDir = new File(getConf().getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR));
+      String location = getConf().getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR);
+
       String destinationName = new Path(value).getName();
-      File destinationFile = new File(resourceDir, destinationName);
-      if ( resourceDir.exists() && ! resourceDir.isDirectory() ) {
-        throw new RuntimeException("The resource directory is not a directory, resourceDir
is set to" + resourceDir);
+      String prefix = destinationName;
+      String postfix = null;
+      int index = destinationName.lastIndexOf(".");
+      if (index > 0) {
+        prefix = destinationName.substring(0, index);
+        postfix = destinationName.substring(index);
+      }
+      if (prefix.length() < 3) {
+        prefix += ".tmp";   // prefix should be longer than 3
+      }
+
+      File resourceDir = new File(location);
+      if (resourceDir.exists() && !resourceDir.isDirectory()) {
+        throw new RuntimeException("The resource directory is not a directory, " +
+            "resourceDir is set to " + resourceDir);
       }
-      if ( ! resourceDir.exists() && ! resourceDir.mkdirs() ) {
+      if (!resourceDir.exists() && !resourceDir.mkdirs()) {
         throw new RuntimeException("Couldn't create directory " + resourceDir);
       }
+
+      File destinationFile;
+      try {
+        destinationFile = File.createTempFile(prefix, postfix, resourceDir);
+      } catch (Exception e) {
+        throw new RuntimeException("Failed to create temporary file for " + value, e);
+      }
       try {
         FileSystem fs = FileSystem.get(new URI(value), conf);
         fs.copyToLocalFile(new Path(value), new Path(destinationFile.getCanonicalPath()));
@@ -620,7 +640,8 @@ public class SessionState {
           try {
             DosToUnix.convertWindowsScriptToUnix(destinationFile);
           } catch (Exception e) {
-            throw new RuntimeException("Caught exception while converting to unix line endings",
e);
+            throw new RuntimeException("Caught exception while converting file " +
+                destinationFile + " to unix line endings", e);
           }
         }
       } catch (Exception e) {



Mime
View raw message