From e0ae232f669b2e2a6654cfacff22a090c462effc Mon Sep 17 00:00:00 2001 From: Ayush Saxena Date: Sat, 23 May 2020 18:38:32 +0530 Subject: [PATCH] HDFS-15093. RENAME.TO_TRASH is ignored When RENAME.OVERWRITE is specified. Contributed by Ayush Saxena. --- .../ClientNamenodeProtocolTranslatorPB.java | 3 ++- ...amenodeProtocolServerSideTranslatorPB.java | 3 ++- .../org/apache/hadoop/hdfs/TestDFSRename.java | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolTranslatorPB.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolTranslatorPB.java index 572c65b67b..d44469211b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolTranslatorPB.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolTranslatorPB.java @@ -609,7 +609,8 @@ public void rename2(String src, String dst, Rename... options) for (Rename option : options) { if (option == Rename.OVERWRITE) { overwrite = true; - } else if (option == Rename.TO_TRASH) { + } + if (option == Rename.TO_TRASH) { toTrash = true; } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolServerSideTranslatorPB.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolServerSideTranslatorPB.java index 9bd82485a5..e0afe006a2 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolServerSideTranslatorPB.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/protocolPB/ClientNamenodeProtocolServerSideTranslatorPB.java @@ -688,7 +688,8 @@ public Rename2ResponseProto rename2(RpcController controller, ArrayList optionList = new ArrayList(); if(req.getOverwriteDest()) { optionList.add(Rename.OVERWRITE); - } else if(req.hasMoveToTrash() && req.getMoveToTrash()) { + } + if (req.hasMoveToTrash() && req.getMoveToTrash()) { optionList.add(Rename.TO_TRASH); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSRename.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSRename.java index e7002c301c..fe2eee28b7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSRename.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSRename.java @@ -30,7 +30,9 @@ import org.apache.hadoop.fs.Options.Rename; import org.apache.hadoop.hdfs.protocol.LocatedBlocks; import org.apache.hadoop.hdfs.server.blockmanagement.BlockManager; +import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter; +import org.apache.hadoop.test.GenericTestUtils; import org.junit.Test; public class TestDFSRename { @@ -175,4 +177,23 @@ public void testRenameWithOverwrite() throws Exception { } } } + + @Test + public void testRename2Options() throws Exception { + try (MiniDFSCluster cluster = new MiniDFSCluster.Builder( + new HdfsConfiguration()).build()) { + cluster.waitActive(); + final DistributedFileSystem dfs = cluster.getFileSystem(); + Path path = new Path("/test"); + dfs.mkdirs(path); + GenericTestUtils.LogCapturer auditLog = + GenericTestUtils.LogCapturer.captureLogs(FSNamesystem.auditLog); + dfs.rename(path, new Path("/dir1"), + new Rename[] {Rename.OVERWRITE, Rename.TO_TRASH}); + String auditOut = auditLog.getOutput(); + assertTrue("Rename should have both OVERWRITE and TO_TRASH " + + "flags at namenode but had only " + auditOut, + auditOut.contains("options=[OVERWRITE, TO_TRASH]")); + } + } }