From 9630621be76182b26755e732f822f29f72454ed8 Mon Sep 17 00:00:00 2001 From: Nandakumar Date: Sat, 7 Oct 2017 10:29:57 +0530 Subject: [PATCH] HDFS-12610. Ozone: OzoneClient: RpcClient list calls throw NPE when iterating over empty list. Contributed by Nandakumar. --- .../hadoop/ozone/client/ObjectStore.java | 3 +- .../hadoop/ozone/client/OzoneBucket.java | 3 +- .../hadoop/ozone/client/OzoneVolume.java | 3 +- .../ozone/client/rpc/TestOzoneRpcClient.java | 33 +++++++++++++++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/ObjectStore.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/ObjectStore.java index 2e661dba1c1..1ed41f5c8f5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/ObjectStore.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/ObjectStore.java @@ -174,7 +174,8 @@ public class ObjectStore { public boolean hasNext() { if(!currentIterator.hasNext()) { currentIterator = getNextListOfVolumes( - currentValue.getName()).iterator(); + currentValue != null ? currentValue.getName() : null) + .iterator(); } return currentIterator.hasNext(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java index e46fe1cfd97..a5dd1d093ff 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java @@ -277,7 +277,8 @@ public class OzoneBucket { public boolean hasNext() { if(!currentIterator.hasNext()) { currentIterator = getNextListOfKeys( - currentValue.getName()).iterator(); + currentValue != null ? currentValue.getName() : null) + .iterator(); } return currentIterator.hasNext(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneVolume.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneVolume.java index f074fb20dff..bf500576624 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneVolume.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/ozone/client/OzoneVolume.java @@ -247,7 +247,8 @@ public class OzoneVolume { public boolean hasNext() { if(!currentIterator.hasNext()) { currentIterator = getNextListOfBuckets( - currentValue.getName()).iterator(); + currentValue != null ? currentValue.getName() : null) + .iterator(); } return currentIterator.hasNext(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/client/rpc/TestOzoneRpcClient.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/client/rpc/TestOzoneRpcClient.java index e56014233f0..0d8864ede58 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/client/rpc/TestOzoneRpcClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/ozone/client/rpc/TestOzoneRpcClient.java @@ -409,7 +409,7 @@ public class TestOzoneRpcClient { } @Test - public void listVolumeTest() throws IOException, OzoneException { + public void testListVolume() throws IOException, OzoneException { String volBase = "vol-" + RandomStringUtils.randomNumeric(3); //Create 10 volume vol--a-0- to vol--a-9- String volBaseNameA = volBase + "-a-"; @@ -448,7 +448,7 @@ public class TestOzoneRpcClient { } @Test - public void listBucketTest() + public void testListBucket() throws IOException, OzoneException { String volumeA = "vol-a-" + RandomStringUtils.randomNumeric(5); String volumeB = "vol-b-" + RandomStringUtils.randomNumeric(5); @@ -522,7 +522,19 @@ public class TestOzoneRpcClient { } @Test - public void listKeyTest() + public void testListBucketsOnEmptyVolume() + throws IOException, OzoneException { + String volume = "vol-" + RandomStringUtils.randomNumeric(5); + store.createVolume(volume); + OzoneVolume vol = store.getVolume(volume); + Iterator buckets = vol.listBuckets(""); + while(buckets.hasNext()) { + Assert.fail(); + } + } + + @Test + public void testListKey() throws IOException, OzoneException { String volumeA = "vol-a-" + RandomStringUtils.randomNumeric(5); String volumeB = "vol-b-" + RandomStringUtils.randomNumeric(5); @@ -656,6 +668,21 @@ public class TestOzoneRpcClient { Assert.assertFalse(volABucketBIter.hasNext()); } + @Test + public void testListKeyOnEmptyBucket() + throws IOException, OzoneException { + String volume = "vol-" + RandomStringUtils.randomNumeric(5); + String bucket = "buc-" + RandomStringUtils.randomNumeric(5); + store.createVolume(volume); + OzoneVolume vol = store.getVolume(volume); + vol.createBucket(bucket); + OzoneBucket buc = vol.getBucket(bucket); + Iterator keys = buc.listKeys(""); + while(keys.hasNext()) { + Assert.fail(); + } + } + /** * Close OzoneClient and shutdown MiniOzoneCluster. */