HDFS-16832. [SBN READ] Follow-on to HDFS-16732. Fix NPE when check the block location of empty directory (#5099)

Signed-off-by: Erik Krogen <xkrogen@apache.org>
Reviewed-by: Zengqiang Xu <xuzq_zander@163.com>
This commit is contained in:
zhengchenyu 2022-11-22 00:26:16 +08:00 committed by GitHub
parent 069bd973d8
commit dc2fba45fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 3 deletions

View File

@ -9034,9 +9034,15 @@ private boolean isObserver() {
private void checkBlockLocationsWhenObserver(LocatedBlocks blocks, String src) private void checkBlockLocationsWhenObserver(LocatedBlocks blocks, String src)
throws ObserverRetryOnActiveException { throws ObserverRetryOnActiveException {
for (LocatedBlock b : blocks.getLocatedBlocks()) { if (blocks == null) {
if (b.getLocations() == null || b.getLocations().length == 0) { return;
throw new ObserverRetryOnActiveException("Zero blocklocations for " + src); }
List<LocatedBlock> 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);
}
} }
} }
} }

View File

@ -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) private static void assertSentTo(DistributedFileSystem fs, int nnIdx)
throws IOException { throws IOException {
assertTrue("Request was not sent to the expected namenode " + nnIdx, assertTrue("Request was not sent to the expected namenode " + nnIdx,