From 411e2b2e7cd63273a3819f99a3275821dfbcc9ce Mon Sep 17 00:00:00 2001 From: Uma Mahesh Date: Mon, 23 Nov 2015 17:54:31 -0800 Subject: [PATCH] HDFS-9433. DFS getEZForPath API on a non-existent file should throw FileNotFoundException (Rakesh R via umamahesh) --- .../org/apache/hadoop/hdfs/DFSClient.java | 4 +- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 ++ .../namenode/FSDirEncryptionZoneOp.java | 4 ++ .../hadoop/hdfs/TestEncryptionZones.java | 50 ++++++++++++++++--- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java index ca0538ef0f8..2c42cbe5bb0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClient.java @@ -2671,8 +2671,8 @@ public class DFSClient implements java.io.Closeable, RemotePeerFactory, try (TraceScope ignored = newPathTraceScope("getEZForPath", src)) { return namenode.getEZForPath(src); } catch (RemoteException re) { - throw re.unwrapRemoteException(AccessControlException.class, - UnresolvedPathException.class); + throw re.unwrapRemoteException(FileNotFoundException.class, + AccessControlException.class, UnresolvedPathException.class); } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 47b9b086fad..e10191313ea 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -2365,6 +2365,9 @@ Release 2.8.0 - UNRELEASED HDFS-9423. Fix intermittent failure of TestEditLogTailer. (Masatake Iwasaki via waltersu4549) + HDFS-9433. DFS getEZForPath API on a non-existent file should throw FileNotFoundException + (Rakesh R via umamahesh) + Release 2.7.3 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java index 0f0b629e71f..b663415a822 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java @@ -19,6 +19,7 @@ package org.apache.hadoop.hdfs.server.namenode; import static org.apache.hadoop.hdfs.server.common.HdfsServerConstants.CRYPTO_XATTR_FILE_ENCRYPTION_INFO; +import java.io.FileNotFoundException; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.AbstractMap; @@ -172,6 +173,9 @@ final class FSDirEncryptionZoneOp { try { src = fsd.resolvePath(pc, srcArg, pathComponents); iip = fsd.getINodesInPath(src, true); + if (iip.getLastINode() == null) { + throw new FileNotFoundException("Path not found: " + iip.getPath()); + } if (fsd.isPermissionEnabled()) { fsd.checkPathAccess(pc, iip, FsAction.READ); } 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 64daeb3b96f..3630f19302c 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 @@ -19,6 +19,7 @@ package org.apache.hadoop.hdfs; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; @@ -475,8 +476,13 @@ public class TestEncryptionZones { assertExceptionContains("Permission denied:", e); } - assertNull("expected null for nonexistent path", - userAdmin.getEncryptionZoneForPath(nonexistent)); + try { + userAdmin.getEncryptionZoneForPath(nonexistent); + fail("FileNotFoundException should be thrown for a non-existent" + + " file path"); + } catch (FileNotFoundException e) { + assertExceptionContains("Path not found: " + nonexistent, e); + } // Check operation with non-ez paths assertNull("expected null for non-ez path", @@ -504,10 +510,20 @@ public class TestEncryptionZones { assertEquals("expected ez path", allPath.toString(), userAdmin.getEncryptionZoneForPath( new Path(snapshottedAllPath)).getPath().toString()); - assertNull("expected null for deleted file path", - userAdmin.getEncryptionZoneForPath(allPathFile)); - assertNull("expected null for deleted directory path", - userAdmin.getEncryptionZoneForPath(allPath)); + try { + userAdmin.getEncryptionZoneForPath(allPathFile); + fail("FileNotFoundException should be thrown for a non-existent" + + " file path"); + } catch (FileNotFoundException e) { + assertExceptionContains("Path not found: " + allPathFile, e); + } + try { + userAdmin.getEncryptionZoneForPath(allPath); + fail("FileNotFoundException should be thrown for a non-existent" + + " file path"); + } catch (FileNotFoundException e) { + assertExceptionContains("Path not found: " + allPath, e); + } return null; } }); @@ -1331,4 +1347,26 @@ public class TestEncryptionZones { assertEquals("Got unexpected ez path", "/somewhere/base/zone", dfsAdmin .getEncryptionZoneForPath(zoneDir).getPath().toString()); } + + @Test(timeout = 60000) + public void testGetEncryptionZoneOnANonExistentZoneFile() throws Exception { + final Path ez = new Path("/ez"); + fs.mkdirs(ez); + dfsAdmin.createEncryptionZone(ez, TEST_KEY); + Path zoneFile = new Path(ez, "file"); + try { + fs.getEZForPath(zoneFile); + fail("FileNotFoundException should be thrown for a non-existent" + + " file path"); + } catch (FileNotFoundException e) { + assertExceptionContains("Path not found: " + zoneFile, e); + } + try { + dfsAdmin.getEncryptionZoneForPath(zoneFile); + fail("FileNotFoundException should be thrown for a non-existent" + + " file path"); + } catch (FileNotFoundException e) { + assertExceptionContains("Path not found: " + zoneFile, e); + } + } }