From d08449ff6e15c7d5cfec802855652ee9007fe11d Mon Sep 17 00:00:00 2001 From: Mahesh Kumar Behera Date: Mon, 13 May 2019 11:33:29 +0530 Subject: [PATCH] HIVE-21717 : Rename is failing for directory in move task. (Mahesh Kumar Behera reviewed by Sankar Hariappan --- .../apache/hadoop/hive/ql/metadata/Hive.java | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index 2d461ada49cd..fc969fc651cb 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -3744,6 +3744,19 @@ public void recycleDirToCmPath(Path dataPath, boolean isPurge) throws HiveExcept } } + private static void deleteAndRename(FileSystem destFs, Path destFile, FileStatus srcStatus, Path destPath) + throws IOException { + if (destFs.exists(destFile)) { + // rename cannot overwrite non empty destination directory, so deleting the destination before renaming. + destFs.delete(destFile); + LOG.info("Deleting destination file" + destFile.toUri()); + } + if(!destFs.rename(srcStatus.getPath(), destFile)) { + throw new IOException("rename for src path: " + srcStatus.getPath() + " to dest:" + + destPath + " returned false"); + } + } + //it is assumed that parent directory of the destf should already exist when this //method is called. when the replace value is true, this method works a little different //from mv command if the destf is a directory, it replaces the destf instead of moving under @@ -3829,37 +3842,14 @@ public static boolean moveFile(final HiveConf conf, Path srcf, final Path destf, "Unable to move source " + srcStatus.getPath() + " to destination " + destFile; if (null == pool) { - boolean success = false; - if (destFs instanceof DistributedFileSystem) { - ((DistributedFileSystem)destFs).rename(srcStatus.getPath(), destFile, Options.Rename.OVERWRITE); - success = true; - } else { - destFs.delete(destFile, false); - success = destFs.rename(srcStatus.getPath(), destFile); - } - if(!success) { - throw new IOException("rename for src path: " + srcStatus.getPath() + " to dest:" - + destf + " returned false"); - } + deleteAndRename(destFs, destFile, srcStatus, destf); } else { futures.add(pool.submit(new Callable() { @Override public Void call() throws HiveException { SessionState.setCurrentSessionState(parentSession); try { - boolean success = false; - if (destFs instanceof DistributedFileSystem) { - ((DistributedFileSystem)destFs).rename(srcStatus.getPath(), destFile, Options.Rename.OVERWRITE); - success = true; - } else { - destFs.delete(destFile, false); - success = destFs.rename(srcStatus.getPath(), destFile); - } - if (!success) { - throw new IOException( - "rename for src path: " + srcStatus.getPath() + " to dest path:" - + destFile + " returned false"); - } + deleteAndRename(destFs, destFile, srcStatus, destf); } catch (Exception e) { throw getHiveException(e, poolMsg); }