From d7213cdc2eed3e10b8dceca6b5b3201803719733 Mon Sep 17 00:00:00 2001 From: Sakthi Date: Thu, 22 Mar 2018 15:43:17 -0700 Subject: [PATCH] HBASE-20135 Fixed NullPointerException during reading bloom filter when upgraded from hbase-1 to hbase-2 --- .../hadoop/hbase/io/hfile/FixedFileTrailer.java | 12 +++++++- .../hbase/io/hfile/TestFixedFileTrailer.java | 33 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java index 55b2ee05f1e15645998bffbd456459c5b2d6dad5..3c74d114e1e8d80903c292786ce1c7fd8f2e4657 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/FixedFileTrailer.java @@ -38,6 +38,8 @@ import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; import org.apache.hadoop.hbase.shaded.protobuf.generated.HFileProtos; import org.apache.hadoop.hbase.util.Bytes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The {@link HFile} has a fixed trailer which contains offsets to other @@ -53,6 +55,8 @@ import org.apache.hadoop.hbase.util.Bytes; */ @InterfaceAudience.Private public class FixedFileTrailer { + private static final Logger LOG = LoggerFactory.getLogger(FixedFileTrailer.class); + /** * We store the comparator class name as a fixed-length field in the trailer. */ @@ -623,7 +627,13 @@ public class FixedFileTrailer { public static CellComparator createComparator( String comparatorClassName) throws IOException { try { - return getComparatorClass(comparatorClassName).getDeclaredConstructor().newInstance(); + + Class comparatorClass = getComparatorClass(comparatorClassName); + if(comparatorClass != null){ + return comparatorClass.getDeclaredConstructor().newInstance(); + } + LOG.warn("No Comparator class for " + comparatorClassName + ". Returning Null."); + return null; } catch (Exception e) { throw new IOException("Comparator class " + comparatorClassName + " is not instantiable", e); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestFixedFileTrailer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestFixedFileTrailer.java index 9643ac7ba2acbcc649598069af22128cc8db8cd4..085b19ef1bd0527a08a4cf4ba24d22b84f1393b3 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestFixedFileTrailer.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestFixedFileTrailer.java @@ -33,6 +33,7 @@ import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.CellComparator; import org.apache.hadoop.hbase.CellComparatorImpl; import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseTestingUtility; @@ -108,6 +109,38 @@ public class TestFixedFileTrailer { pb.getComparatorClassName()); } + @Test + public void testCreateComparator() { + FixedFileTrailer t = new FixedFileTrailer(version, HFileReaderImpl.PBUF_TRAILER_MINOR_VERSION); + try { + assertEquals(CellComparatorImpl.class, + t.createComparator(KeyValue.COMPARATOR.getLegacyKeyComparatorName()).getClass()); + assertEquals(CellComparatorImpl.class, + t.createComparator(KeyValue.COMPARATOR.getClass().getName()).getClass()); + assertEquals(CellComparatorImpl.class, + t.createComparator(CellComparator.class.getName()).getClass()); + assertEquals(CellComparatorImpl.MetaCellComparator.class, + t.createComparator(KeyValue.META_COMPARATOR.getLegacyKeyComparatorName()).getClass()); + assertEquals(CellComparatorImpl.MetaCellComparator.class, + t.createComparator(KeyValue.META_COMPARATOR.getClass().getName()).getClass()); + assertEquals(CellComparatorImpl.MetaCellComparator.class, t.createComparator( + CellComparatorImpl.MetaCellComparator.META_COMPARATOR.getClass().getName()).getClass()); + assertEquals(null, t.createComparator(Bytes.BYTES_RAWCOMPARATOR.getClass().getName())); + assertEquals(null, t.createComparator("org.apache.hadoop.hbase.KeyValue$RawBytesComparator")); + + } catch (IOException e) { + fail("Unexpected exception while testing FixedFileTrailer#createComparator()"); + } + try { + // Test an invalid comparatorClassName + t.createComparator(""); + fail("Exception expected"); + } catch (IOException e){ + // Got an expected exception + assertTrue(true); + } + } + @Test public void testTrailer() throws IOException { FixedFileTrailer t = new FixedFileTrailer(version, -- 2.16.2