Skip to content

Commit dc2fba4

Browse files
authored
HDFS-16832. [SBN READ] Follow-on to HDFS-16732. Fix NPE when check the block location of empty directory (apache#5099)
Signed-off-by: Erik Krogen <[email protected]> Reviewed-by: Zengqiang Xu <[email protected]>
1 parent 069bd97 commit dc2fba4

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9034,9 +9034,15 @@ private boolean isObserver() {
90349034

90359035
private void checkBlockLocationsWhenObserver(LocatedBlocks blocks, String src)
90369036
throws ObserverRetryOnActiveException {
9037-
for (LocatedBlock b : blocks.getLocatedBlocks()) {
9038-
if (b.getLocations() == null || b.getLocations().length == 0) {
9039-
throw new ObserverRetryOnActiveException("Zero blocklocations for " + src);
9037+
if (blocks == null) {
9038+
return;
9039+
}
9040+
List<LocatedBlock> locatedBlockList = blocks.getLocatedBlocks();
9041+
if (locatedBlockList != null) {
9042+
for (LocatedBlock b : locatedBlockList) {
9043+
if (b.getLocations() == null || b.getLocations().length == 0) {
9044+
throw new ObserverRetryOnActiveException("Zero blocklocations for " + src);
9045+
}
90409046
}
90419047
}
90429048
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestObserverNode.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,29 @@ public void run() {
652652
}
653653
}
654654

655+
@Test
656+
public void testSimpleReadEmptyDirOrFile() throws IOException {
657+
// read empty dir
658+
dfs.mkdirs(new Path("/emptyDir"));
659+
assertSentTo(0);
660+
661+
dfs.getClient().listPaths("/", new byte[0], true);
662+
assertSentTo(2);
663+
664+
dfs.getClient().getLocatedFileInfo("/emptyDir", true);
665+
assertSentTo(2);
666+
667+
// read empty file
668+
dfs.create(new Path("/emptyFile"), (short)1);
669+
assertSentTo(0);
670+
671+
dfs.getClient().getLocatedFileInfo("/emptyFile", true);
672+
assertSentTo(2);
673+
674+
dfs.getClient().getBlockLocations("/emptyFile", 0, 1);
675+
assertSentTo(2);
676+
}
677+
655678
private static void assertSentTo(DistributedFileSystem fs, int nnIdx)
656679
throws IOException {
657680
assertTrue("Request was not sent to the expected namenode " + nnIdx,

0 commit comments

Comments
 (0)