From f4006b5039170ca1ac8adb63abbc111184e2e9ee Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Wed, 16 May 2018 11:01:21 -0700 Subject: [PATCH] HBASE-20564 Tighter ByteBufferKeyValue Cell Comparator; ADDENDUM2 Add a Test Run meta tests but using our new basis, the ByteBufferKeyValue instead of the old byte array-backed KeyValue so we catch any oddness in the Type processing or in the Comparator. --- .../hadoop/hbase/TestCellComparator.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellComparator.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellComparator.java index 8652d820374..a3185158324 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellComparator.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellComparator.java @@ -21,6 +21,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Set; +import java.util.TreeSet; + import org.apache.hadoop.hbase.KeyValue.Type; import org.apache.hadoop.hbase.testclassification.MiscTests; import org.apache.hadoop.hbase.testclassification.SmallTests; @@ -128,4 +132,119 @@ public class TestCellComparator { assertEquals(0, comparator.compareRows(bbCell2, bbCell3)); assertTrue(comparator.compareRows(bbCell1, bbCell2) < 0); } + + /** + * Test meta comparisons using our new ByteBufferKeyValue Cell type, the type we use everywhere + * in 2.0. + */ + @Test + public void testMetaComparisons() throws Exception { + long now = System.currentTimeMillis(); + + // Meta compares + Cell aaa = createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes("TestScanMultipleVersions,row_0500,1236020145502"), now)); + Cell bbb = createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes("TestScanMultipleVersions,,99999999999999"), now)); + CellComparator c = CellComparatorImpl.META_COMPARATOR; + assertTrue(c.compare(bbb, aaa) < 0); + + Cell ccc = createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("TestScanMultipleVersions,,1236023996656"), + Bytes.toBytes("info"), Bytes.toBytes("regioninfo"), 1236024396271L, + (byte[])null)); + assertTrue(c.compare(ccc, bbb) < 0); + + Cell x = createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("TestScanMultipleVersions,row_0500,1236034574162"), + Bytes.toBytes("info"), Bytes.toBytes(""), 9223372036854775807L, + (byte[])null)); + Cell y = createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("TestScanMultipleVersions,row_0500,1236034574162"), + Bytes.toBytes("info"), Bytes.toBytes("regioninfo"), 1236034574912L, + (byte[])null)); + assertTrue(c.compare(x, y) < 0); + } + + private static Cell createByteBufferKeyValueFromKeyValue(KeyValue kv) { + ByteBuffer bb = ByteBuffer.wrap(kv.getBuffer()); + return new ByteBufferKeyValue(bb, 0, bb.remaining()); + } + + /** + * More tests using ByteBufferKeyValue copied over from TestKeyValue which uses old KVs only. + */ + @Test + public void testMetaComparisons2() { + long now = System.currentTimeMillis(); + CellComparator c = CellComparatorImpl.META_COMPARATOR; + assertTrue(c.compare(createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,1"), now)), + createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,1"), now))) == 0); + Cell a = createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,1"), now)); + Cell b = createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,2"), now)); + assertTrue(c.compare(a, b) < 0); + assertTrue(c.compare(createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,2"), now)), + createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",a,,0,1"), now))) > 0); + assertTrue(c.compare(createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,1"), now)), + createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,1"), now))) == 0); + assertTrue(c.compare(createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,1"), now)), + createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,2"), now))) < 0); + assertTrue(c.compare(createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,2"), now)), + createByteBufferKeyValueFromKeyValue(new KeyValue( + Bytes.toBytes(TableName.META_TABLE_NAME.getNameAsString()+",,1"), now))) > 0); + } + + @Test + public void testBinaryKeys() throws Exception { + Set set = new TreeSet<>(CellComparatorImpl.COMPARATOR); + final byte [] fam = Bytes.toBytes("col"); + final byte [] qf = Bytes.toBytes("umn"); + final byte [] nb = new byte[0]; + Cell [] keys = { + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("aaaaa,\u0000\u0000,2"), fam, qf, 2, nb)), + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("aaaaa,\u0001,3"), fam, qf, 3, nb)), + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("aaaaa,,1"), fam, qf, 1, nb)), + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("aaaaa,\u1000,5"), fam, qf, 5, nb)), + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("aaaaa,a,4"), fam, qf, 4, nb)), + createByteBufferKeyValueFromKeyValue( + new KeyValue(Bytes.toBytes("a,a,0"), fam, qf, 0, nb)), + }; + // Add to set with bad comparator + Collections.addAll(set, keys); + // This will output the keys incorrectly. + boolean assertion = false; + int count = 0; + try { + for (Cell k: set) { + assertTrue("count=" + count + ", " + k.toString(), count++ == k.getTimestamp()); + } + } catch (AssertionError e) { + // Expected + assertion = true; + } + assertTrue(assertion); + // Make set with good comparator + set = new TreeSet<>(CellComparatorImpl.META_COMPARATOR); + Collections.addAll(set, keys); + count = 0; + for (Cell k: set) { + assertTrue("count=" + count + ", " + k.toString(), count++ == k.getTimestamp()); + } + } }