Skip to content

Commit ffeb702

Browse files
zhengchenyulgh
authored andcommitted
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 <xkrogen@apache.org> Reviewed-by: Zengqiang Xu <xuzq_zander@163.com>
1 parent e7ead87 commit ffeb702

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
@@ -8953,9 +8953,15 @@ private boolean isObserver() {
89538953

89548954
private void checkBlockLocationsWhenObserver(LocatedBlocks blocks, String src)
89558955
throws ObserverRetryOnActiveException {
8956-
for (LocatedBlock b : blocks.getLocatedBlocks()) {
8957-
if (b.getLocations() == null || b.getLocations().length == 0) {
8958-
throw new ObserverRetryOnActiveException("Zero blocklocations for " + src);
8956+
if (blocks == null) {
8957+
return;
8958+
}
8959+
List<LocatedBlock> locatedBlockList = blocks.getLocatedBlocks();
8960+
if (locatedBlockList != null) {
8961+
for (LocatedBlock b : locatedBlockList) {
8962+
if (b.getLocations() == null || b.getLocations().length == 0) {
8963+
throw new ObserverRetryOnActiveException("Zero blocklocations for " + src);
8964+
}
89598965
}
89608966
}
89618967
}

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)