diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java index 99f2089fe8..5b8bc0ac61 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java @@ -9034,9 +9034,15 @@ private boolean isObserver() { private void checkBlockLocationsWhenObserver(LocatedBlocks blocks, String src) throws ObserverRetryOnActiveException { - for (LocatedBlock b : blocks.getLocatedBlocks()) { - if (b.getLocations() == null || b.getLocations().length == 0) { - throw new ObserverRetryOnActiveException("Zero blocklocations for " + src); + if (blocks == null) { + return; + } + List locatedBlockList = blocks.getLocatedBlocks(); + if (locatedBlockList != null) { + for (LocatedBlock b : locatedBlockList) { + if (b.getLocations() == null || b.getLocations().length == 0) { + throw new ObserverRetryOnActiveException("Zero blocklocations for " + src); + } } } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestObserverNode.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestObserverNode.java index 60728284e5..8b691a1172 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestObserverNode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestObserverNode.java @@ -652,6 +652,29 @@ public void run() { } } + @Test + public void testSimpleReadEmptyDirOrFile() throws IOException { + // read empty dir + dfs.mkdirs(new Path("/emptyDir")); + assertSentTo(0); + + dfs.getClient().listPaths("/", new byte[0], true); + assertSentTo(2); + + dfs.getClient().getLocatedFileInfo("/emptyDir", true); + assertSentTo(2); + + // read empty file + dfs.create(new Path("/emptyFile"), (short)1); + assertSentTo(0); + + dfs.getClient().getLocatedFileInfo("/emptyFile", true); + assertSentTo(2); + + dfs.getClient().getBlockLocations("/emptyFile", 0, 1); + assertSentTo(2); + } + private static void assertSentTo(DistributedFileSystem fs, int nnIdx) throws IOException { assertTrue("Request was not sent to the expected namenode " + nnIdx,