From 45a753ccf79d334513c7bc8f2b81c89a4697075d Mon Sep 17 00:00:00 2001 From: Xiaoyu Yao Date: Mon, 2 May 2016 19:30:47 -0700 Subject: [PATCH] HDFS-10344. DistributedFileSystem#getTrashRoots should skip encryption zone that does not have .Trash. Contributed by Xiaoyu Yao. --- .../hadoop/hdfs/DistributedFileSystem.java | 3 ++ .../hadoop/hdfs/TestEncryptionZones.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DistributedFileSystem.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DistributedFileSystem.java index a3a8ba043ab..7a562654d46 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DistributedFileSystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DistributedFileSystem.java @@ -2410,6 +2410,9 @@ public Collection getTrashRoots(boolean allUsers) { while (it.hasNext()) { Path ezTrashRoot = new Path(it.next().getPath(), FileSystem.TRASH_PREFIX); + if (!exists(ezTrashRoot)) { + continue; + } if (allUsers) { for (FileStatus candidate : listStatus(ezTrashRoot)) { if (exists(candidate.getPath())) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java index c8d98ee130c..ce2befde759 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java @@ -28,6 +28,7 @@ import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -1442,6 +1443,43 @@ public void testRootDirEZTrash() throws Exception { verifyShellDeleteWithTrash(shell, encFile); } + @Test(timeout = 120000) + public void testGetTrashRoots() throws Exception { + final HdfsAdmin dfsAdmin = + new HdfsAdmin(FileSystem.getDefaultUri(conf), conf); + Path ezRoot1 = new Path("/ez1"); + fs.mkdirs(ezRoot1); + dfsAdmin.createEncryptionZone(ezRoot1, TEST_KEY); + Path ezRoot2 = new Path("/ez2"); + fs.mkdirs(ezRoot2); + dfsAdmin.createEncryptionZone(ezRoot2, TEST_KEY); + Path ezRoot3 = new Path("/ez3"); + fs.mkdirs(ezRoot3); + dfsAdmin.createEncryptionZone(ezRoot3, TEST_KEY); + Collection trashRootsBegin = fs.getTrashRoots(true); + assertEquals("Unexpected getTrashRoots result", 0, trashRootsBegin.size()); + + final Path encFile = new Path(ezRoot2, "encFile"); + final int len = 8192; + DFSTestUtil.createFile(fs, encFile, len, (short) 1, 0xFEED); + Configuration clientConf = new Configuration(conf); + clientConf.setLong(FS_TRASH_INTERVAL_KEY, 1); + FsShell shell = new FsShell(clientConf); + verifyShellDeleteWithTrash(shell, encFile); + + Collection trashRootsDelete1 = fs.getTrashRoots(true); + assertEquals("Unexpected getTrashRoots result", 1, + trashRootsDelete1.size()); + + final Path nonEncFile = new Path("/nonEncFile"); + DFSTestUtil.createFile(fs, nonEncFile, len, (short) 1, 0xFEED); + verifyShellDeleteWithTrash(shell, nonEncFile); + + Collection trashRootsDelete2 = fs.getTrashRoots(true); + assertEquals("Unexpected getTrashRoots result", 2, + trashRootsDelete2.size()); + } + private void verifyShellDeleteWithTrash(FsShell shell, Path path) throws Exception{ try {