From 8d1a09b7418d5cb512affaede0bb1ae494502967 Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Tue, 1 Nov 2011 00:38:57 +0000 Subject: [PATCH] HDFS-2065. Add null checks in DFSClient.getFileChecksum(..). Contributed by Uma Maheswara Rao G git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1195731 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ .../org/apache/hadoop/hdfs/DFSClient.java | 14 ++++++++++---- .../hdfs/TestDistributedFileSystem.java | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index aa5cd5cd586..80804ae482d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -1232,6 +1232,9 @@ Release 0.23.0 - Unreleased getFileStatus on non-existing files; fix bugs in getBlockLocations; and changed getFileChecksum json response root to "FileChecksum". (szetszwo) + HDFS-2065. Add null checks in DFSClient.getFileChecksum(..). (Uma + Maheswara Rao G via szetszwo) + BREAKDOWN OF HDFS-1073 SUBTASKS HDFS-1521. Persist transaction ID on disk between NN restarts. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java index 65a9faeff47..a29ff869847 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java @@ -1106,8 +1106,11 @@ public class DFSClient implements java.io.Closeable { ClientProtocol namenode, SocketFactory socketFactory, int socketTimeout ) throws IOException { //get all block locations - List locatedblocks - = callGetBlockLocations(namenode, src, 0, Long.MAX_VALUE).getLocatedBlocks(); + LocatedBlocks blockLocations = callGetBlockLocations(namenode, src, 0, Long.MAX_VALUE); + if (null == blockLocations) { + throw new FileNotFoundException("File does not exist: " + src); + } + List locatedblocks = blockLocations.getLocatedBlocks(); final DataOutputBuffer md5out = new DataOutputBuffer(); int bytesPerCRC = 0; long crcPerBlock = 0; @@ -1117,8 +1120,11 @@ public class DFSClient implements java.io.Closeable { //get block checksum for each block for(int i = 0; i < locatedblocks.size(); i++) { if (refetchBlocks) { // refetch to get fresh tokens - locatedblocks = callGetBlockLocations(namenode, src, 0, Long.MAX_VALUE) - .getLocatedBlocks(); + blockLocations = callGetBlockLocations(namenode, src, 0, Long.MAX_VALUE); + if (null == blockLocations) { + throw new FileNotFoundException("File does not exist: " + src); + } + locatedblocks = blockLocations.getLocatedBlocks(); refetchBlocks = false; } LocatedBlock lb = locatedblocks.get(i); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java index 0ea9f017865..aaa085f1e7f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java @@ -412,6 +412,25 @@ public class TestDistributedFileSystem { final UserGroupInformation ugi = UserGroupInformation.createUserForTesting( current.getShortUserName() + "x", new String[]{"user"}); + try { + ((DistributedFileSystem) hdfs).getFileChecksum(new Path( + "/test/TestNonExistingFile")); + fail("Expecting FileNotFoundException"); + } catch (FileNotFoundException e) { + assertTrue("Not throwing the intended exception message", e.getMessage() + .contains("File does not exist: /test/TestNonExistingFile")); + } + + try { + Path path = new Path("/test/TestExistingDir/"); + hdfs.mkdirs(path); + ((DistributedFileSystem) hdfs).getFileChecksum(path); + fail("Expecting FileNotFoundException"); + } catch (FileNotFoundException e) { + assertTrue("Not throwing the intended exception message", e.getMessage() + .contains("File does not exist: /test/TestExistingDir")); + } + //hftp final String hftpuri = "hftp://" + nnAddr; System.out.println("hftpuri=" + hftpuri);