From 4ba0febd580da7c17b2d3dc82439be41e67af2c8 Mon Sep 17 00:00:00 2001 From: Zhihai Xu Date: Thu, 24 Sep 2015 11:48:11 -0700 Subject: [PATCH] HADOOP-12252. LocalDirAllocator should not throw NPE with empty string configuration. Contributed by Zhihai Xu (cherry picked from commit 52c1f272ecb6c29c81898a1ff50d03a1296df1f7) Conflicts: hadoop-common-project/hadoop-common/CHANGES.txt --- .../hadoop-common/CHANGES.txt | 3 +++ .../apache/hadoop/fs/LocalDirAllocator.java | 4 +-- .../hadoop/fs/TestLocalDirAllocator.java | 26 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 53311b9adaf..513d377835c 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -579,6 +579,9 @@ Release 2.8.0 - UNRELEASED required context item is not configured (Brahma Reddy Battula via harsh) + HADOOP-12252. LocalDirAllocator should not throw NPE with empty string + configuration. (Zhihai Xu) + OPTIMIZATIONS HADOOP-12051. ProtobufRpcEngine.invoke() should use Exception.toString() diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocalDirAllocator.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocalDirAllocator.java index 8f011ce409f..ccea6e5a2be 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocalDirAllocator.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/LocalDirAllocator.java @@ -250,9 +250,9 @@ private static class AllocatorPerContext { private int dirNumLastAccessed; private Random dirIndexRandomizer = new Random(); private FileSystem localFS; - private DF[] dirDF; + private DF[] dirDF = new DF[0]; private String contextCfgItemName; - private String[] localDirs; + private String[] localDirs = new String[0]; private String savedLocalDirs = ""; public AllocatorPerContext(String contextCfgItemName) { diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalDirAllocator.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalDirAllocator.java index 624fa14497e..5f56f9a0fb6 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalDirAllocator.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestLocalDirAllocator.java @@ -26,6 +26,7 @@ import java.util.NoSuchElementException; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.util.DiskChecker.DiskErrorException; import org.apache.hadoop.util.Shell; import org.junit.runner.RunWith; @@ -312,7 +313,30 @@ public void testShouldNotthrowNPE() throws Exception { } catch (IOException e) { assertEquals(CONTEXT + " not configured", e.getMessage()); } catch (NullPointerException e) { - fail("Lack of configuration should not have thrown an NPE."); + fail("Lack of configuration should not have thrown a NPE."); + } + + String NEW_CONTEXT = CONTEXT + ".new"; + conf1.set(NEW_CONTEXT, ""); + LocalDirAllocator newDirAllocator = new LocalDirAllocator(NEW_CONTEXT); + try { + newDirAllocator.getLocalPathForWrite("/test", conf1); + fail("Exception not thrown when " + NEW_CONTEXT + + " is set to empty string"); + } catch (IOException e) { + assertTrue(e instanceof DiskErrorException); + } catch (NullPointerException e) { + fail("Wrong configuration should not have thrown a NPE."); + } + + try { + newDirAllocator.getLocalPathToRead("/test", conf1); + fail("Exception not thrown when " + NEW_CONTEXT + + " is set to empty string"); + } catch (IOException e) { + assertTrue(e instanceof DiskErrorException); + } catch (NullPointerException e) { + fail("Wrong configuration should not have thrown a NPE."); } }