Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@
<suppress checks="ImportOrder" message="Extra separation in import group before"/>
<suppress checks="MethodLength" files="org.apache.hadoop.hbase.thrift.DemoClient.java"/>
<suppress checks="LineLength" files="org.apache.hadoop.hbase.util.ZKDataMigrator.java"/>
<suppress checks="InnerAssignment" files="org.apache.hadoop.hbase.Waiter"/>
<suppress checks="EmptyBlock" files="org.apache.hadoop.hbase.TestTimeout"/>
</suppressions>
133 changes: 63 additions & 70 deletions hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -50,47 +49,46 @@
public class TestKeyValue {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestKeyValue.class);
HBaseClassTestRule.forClass(TestKeyValue.class);
private static final Logger LOG = LoggerFactory.getLogger(TestKeyValue.class);

@Test
public void testColumnCompare() throws Exception {
final byte [] a = Bytes.toBytes("aaa");
byte [] family1 = Bytes.toBytes("abc");
byte [] qualifier1 = Bytes.toBytes("def");
byte [] family2 = Bytes.toBytes("abcd");
byte [] qualifier2 = Bytes.toBytes("ef");
public void testColumnCompare() {
final byte[] a = Bytes.toBytes("aaa");
byte[] family1 = Bytes.toBytes("abc");
byte[] qualifier1 = Bytes.toBytes("def");
byte[] family2 = Bytes.toBytes("abcd");
byte[] qualifier2 = Bytes.toBytes("ef");

KeyValue aaa = new KeyValue(a, family1, qualifier1, 0L, KeyValue.Type.Put, a);
assertFalse(CellUtil.matchingColumn(aaa, family2, qualifier2));
assertTrue(CellUtil.matchingColumn(aaa, family1, qualifier1));
aaa = new KeyValue(a, family2, qualifier2, 0L, KeyValue.Type.Put, a);
assertFalse(CellUtil.matchingColumn(aaa, family1, qualifier1));
assertTrue(CellUtil.matchingColumn(aaa, family2,qualifier2));
byte [] nullQualifier = new byte[0];
byte[] nullQualifier = new byte[0];
aaa = new KeyValue(a, family1, nullQualifier, 0L, KeyValue.Type.Put, a);
assertTrue(CellUtil.matchingColumn(aaa, family1,null));
assertFalse(CellUtil.matchingColumn(aaa, family2,qualifier2));
}

/**
* Test a corner case when the family qualifier is a prefix of the
* column qualifier.
* Test a corner case when the family qualifier is a prefix of the column qualifier.
*/
@Test
public void testColumnCompare_prefix() throws Exception {
final byte [] a = Bytes.toBytes("aaa");
byte [] family1 = Bytes.toBytes("abc");
byte [] qualifier1 = Bytes.toBytes("def");
byte [] family2 = Bytes.toBytes("ab");
byte [] qualifier2 = Bytes.toBytes("def");
public void testColumnCompare_prefix() {
final byte[] a = Bytes.toBytes("aaa");
byte[] family1 = Bytes.toBytes("abc");
byte[] qualifier1 = Bytes.toBytes("def");
byte[] family2 = Bytes.toBytes("ab");
byte[] qualifier2 = Bytes.toBytes("def");

KeyValue aaa = new KeyValue(a, family1, qualifier1, 0L, KeyValue.Type.Put, a);
assertFalse(CellUtil.matchingColumn(aaa, family2, qualifier2));
}

@Test
public void testBasics() throws Exception {
public void testBasics() {
LOG.info("LOWKEY: " + KeyValue.LOWESTKEY.toString());
String name = "testBasics";
check(Bytes.toBytes(name),
Expand All @@ -117,11 +115,11 @@ private void check(final byte [] row, final byte [] family, byte [] qualifier,
}

@Test
public void testPlainCompare() throws Exception {
final byte [] a = Bytes.toBytes("aaa");
final byte [] b = Bytes.toBytes("bbb");
final byte [] fam = Bytes.toBytes("col");
final byte [] qf = Bytes.toBytes("umn");
public void testPlainCompare() {
final byte[] a = Bytes.toBytes("aaa");
final byte[] b = Bytes.toBytes("bbb");
final byte[] fam = Bytes.toBytes("col");
final byte[] qf = Bytes.toBytes("umn");
KeyValue aaa = new KeyValue(a, fam, qf, a);
KeyValue bbb = new KeyValue(b, fam, qf, b);
assertTrue(CellComparatorImpl.COMPARATOR.compare(aaa, bbb) < 0);
Expand All @@ -145,7 +143,7 @@ public void testPlainCompare() throws Exception {
}

@Test
public void testMoreComparisons() throws Exception {
public void testMoreComparisons() {
long now = System.currentTimeMillis();

// Meta compares
Expand Down Expand Up @@ -187,10 +185,9 @@ public void testMetaComparatorTableKeysWithCommaOk() {
/**
* Tests cases where rows keys have characters below the ','.
* See HBASE-832
* @throws IOException
*/
@Test
public void testKeyValueBorderCases() throws IOException {
public void testKeyValueBorderCases() {
// % sorts before , so if we don't do special comparator, rowB would
// come before rowA.
KeyValue rowA = new KeyValue(Bytes.toBytes("testtable,www.hbase.org/,1234"),
Expand All @@ -204,7 +201,6 @@ public void testKeyValueBorderCases() throws IOException {
rowB = new KeyValue(Bytes.toBytes("testtable,$www.hbase.org/,99999"),
Bytes.toBytes("fam"), Bytes.toBytes(""), Long.MAX_VALUE, (byte[])null);
assertTrue(CellComparatorImpl.META_COMPARATOR.compare(rowA, rowB) < 0);

}

private void metacomparisons(final CellComparatorImpl c) {
Expand Down Expand Up @@ -241,12 +237,12 @@ private void comparisons(final CellComparatorImpl c) {
}

@Test
public void testBinaryKeys() throws Exception {
public void testBinaryKeys() {
Set<KeyValue> set = new TreeSet<>(CellComparatorImpl.COMPARATOR);
final byte [] fam = Bytes.toBytes("col");
final byte [] qf = Bytes.toBytes("umn");
final byte [] nb = new byte[0];
KeyValue [] keys = {new KeyValue(Bytes.toBytes("aaaaa,\u0000\u0000,2"), fam, qf, 2, nb),
final byte[] fam = Bytes.toBytes("col");
final byte[] qf = Bytes.toBytes("umn");
final byte[] nb = new byte[0];
KeyValue[] keys = {new KeyValue(Bytes.toBytes("aaaaa,\u0000\u0000,2"), fam, qf, 2, nb),
new KeyValue(Bytes.toBytes("aaaaa,\u0001,3"), fam, qf, 3, nb),
new KeyValue(Bytes.toBytes("aaaaa,,1"), fam, qf, 1, nb),
new KeyValue(Bytes.toBytes("aaaaa,\u1000,5"), fam, qf, 5, nb),
Expand All @@ -259,8 +255,8 @@ public void testBinaryKeys() throws Exception {
boolean assertion = false;
int count = 0;
try {
for (KeyValue k: set) {
assertTrue(count++ == k.getTimestamp());
for (KeyValue k : set) {
assertEquals(count++, k.getTimestamp());
}
} catch (java.lang.AssertionError e) {
// Expected
Expand All @@ -271,8 +267,8 @@ public void testBinaryKeys() throws Exception {
set = new TreeSet<>(CellComparatorImpl.META_COMPARATOR);
Collections.addAll(set, keys);
count = 0;
for (KeyValue k: set) {
assertTrue(count++ == k.getTimestamp());
for (KeyValue k : set) {
assertEquals(count++, k.getTimestamp());
}
}

Expand All @@ -281,27 +277,22 @@ public void testStackedUpKeyValue() {
// Test multiple KeyValues in a single blob.

// TODO actually write this test!

}

private final byte[] rowA = Bytes.toBytes("rowA");
private final byte[] rowB = Bytes.toBytes("rowB");

private final byte[] family = Bytes.toBytes("family");
private final byte[] qualA = Bytes.toBytes("qfA");
private final byte[] qualB = Bytes.toBytes("qfB");

private void assertKVLess(CellComparator c,
KeyValue less,
KeyValue greater) {
private void assertKVLess(CellComparator c, KeyValue less, KeyValue greater) {
int cmp = c.compare(less,greater);
assertTrue(cmp < 0);
cmp = c.compare(greater,less);
assertTrue(cmp > 0);
}

private void assertKVLessWithoutRow(CellComparator c, int common, KeyValue less,
KeyValue greater) {
private void assertKVLessWithoutRow(CellComparator c, KeyValue less, KeyValue greater) {
int cmp = c.compare(less, greater);
assertTrue(cmp < 0);
cmp = c.compare(greater, less);
Expand Down Expand Up @@ -334,26 +325,26 @@ public void testCompareWithoutRow() {
KeyValue kv1_0 = new KeyValue(row, fami1, qual0, ts, KeyValue.Type.Put);

// 'fami:qf1' < 'fami:qf2'
assertKVLessWithoutRow(c, 0, kv0_1, kv0_2);
assertKVLessWithoutRow(c, kv0_1, kv0_2);
// 'fami:qf1' < 'fami1:'
assertKVLessWithoutRow(c, 0, kv0_1, kv1_0);
assertKVLessWithoutRow(c, kv0_1, kv1_0);

// Test comparison by skipping the same prefix bytes.
/***
/*
* KeyValue Format and commonLength:
* |_keyLen_|_valLen_|_rowLen_|_rowKey_|_famiLen_|_fami_|_Quali_|....
* ------------------|-------commonLength--------|--------------
*/
int commonLength = KeyValue.ROW_LENGTH_SIZE + KeyValue.FAMILY_LENGTH_SIZE
+ row.length;
// 'fa:' < 'fami:'. They have commonPrefix + 2 same prefix bytes.
assertKVLessWithoutRow(c, commonLength + 2, kv_0, kv0_0);
assertKVLessWithoutRow(c, kv_0, kv0_0);
// 'fami:' < 'fami:qf1'. They have commonPrefix + 4 same prefix bytes.
assertKVLessWithoutRow(c, commonLength + 4, kv0_0, kv0_1);
assertKVLessWithoutRow(c, kv0_0, kv0_1);
// 'fami:qf1' < 'fami1:'. They have commonPrefix + 4 same prefix bytes.
assertKVLessWithoutRow(c, commonLength + 4, kv0_1, kv1_0);
assertKVLessWithoutRow(c, kv0_1, kv1_0);
// 'fami:qf1' < 'fami:qf2'. They have commonPrefix + 6 same prefix bytes.
assertKVLessWithoutRow(c, commonLength + 6, kv0_1, kv0_2);
assertKVLessWithoutRow(c, kv0_1, kv0_2);
}

@Test
Expand Down Expand Up @@ -404,12 +395,12 @@ public void testFirstLastOnRow() {
}

@Test
public void testCreateKeyOnly() throws Exception {
public void testCreateKeyOnly() {
long ts = 1;
byte [] value = Bytes.toBytes("a real value");
byte [] evalue = new byte[0]; // empty value
byte[] value = Bytes.toBytes("a real value");
byte[] evalue = new byte[0]; // empty value

for (byte[] val : new byte[][]{value, evalue}) {
for (byte[] val : new byte[][] { value, evalue }) {
for (boolean useLen : new boolean[]{false,true}) {
KeyValue kv1 = new KeyValue(rowA, family, qualA, ts, val);
KeyValue kv1ko = kv1.createKeyOnly(useLen);
Expand Down Expand Up @@ -470,7 +461,8 @@ public void testKVsWithTags() {
byte[] metaValue1 = Bytes.toBytes("metaValue1");
byte[] metaValue2 = Bytes.toBytes("metaValue2");
KeyValue kv = new KeyValue(row, cf, q, HConstants.LATEST_TIMESTAMP, value, new Tag[] {
new ArrayBackedTag((byte) 1, metaValue1), new ArrayBackedTag((byte) 2, metaValue2) });
new ArrayBackedTag((byte) 1, metaValue1), new ArrayBackedTag((byte) 2, metaValue2)
});
assertTrue(kv.getTagsLength() > 0);
assertTrue(Bytes.equals(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(), row, 0,
row.length));
Expand Down Expand Up @@ -498,7 +490,7 @@ public void testKVsWithTags() {
assertTrue(meta1Ok);
assertTrue(meta2Ok);
Iterator<Tag> tagItr = PrivateCellUtil.tagsIterator(kv);
//Iterator<Tag> tagItr = kv.tagsIterator();

assertTrue(tagItr.hasNext());
Tag next = tagItr.next();
assertEquals(10, next.getValueLength());
Expand Down Expand Up @@ -572,7 +564,7 @@ public void testMetaKeyComparator() {
}

@Test
public void testEqualsAndHashCode() throws Exception {
public void testEqualsAndHashCode() {
KeyValue kvA1 = new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"),
Bytes.toBytes("qualA"), Bytes.toBytes("1"));
KeyValue kvA2 = new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"),
Expand All @@ -587,24 +579,24 @@ public void testEqualsAndHashCode() throws Exception {
assertNotEquals(kvA1, kvB);
assertEquals(kvA1.hashCode(), kvA2.hashCode());
assertNotEquals(kvA1.hashCode(), kvB.hashCode());

}

@Test
public void testKeyValueSerialization() throws Exception {
KeyValue[] keyValues = new KeyValue[] {
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
Bytes.toBytes("1")),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
Bytes.toBytes("2")),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
System.currentTimeMillis(), Bytes.toBytes("2"),
new Tag[] { new ArrayBackedTag((byte) 120, "tagA"),
new ArrayBackedTag((byte) 121, Bytes.toBytes("tagB")) }),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
new Tag[] {
new ArrayBackedTag((byte) 120, "tagA"),
new ArrayBackedTag((byte) 121, Bytes.toBytes("tagB")) }),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
System.currentTimeMillis(), Bytes.toBytes("2"),
new Tag[] { new ArrayBackedTag((byte) 0, "tagA") }),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes(""),
new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes(""),
Bytes.toBytes("1")) };
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
for (KeyValue kv : keyValues) {
Expand All @@ -625,11 +617,11 @@ public void testKeyValueSerialization() throws Exception {

@Test
public void testNullByteArrayKeyValueFailure() {
//can't add to testCheckKeyValueBytesFailureCase because it
//goes through the InputStream KeyValue API which can't produce a null buffer
// can't add to testCheckKeyValueBytesFailureCase because it
// goes through the InputStream KeyValue API which can't produce a null buffer
try {
KeyValue kv = new KeyValue(null, 0, 0);
} catch (IllegalArgumentException iae){
new KeyValue(null, 0, 0);
} catch (IllegalArgumentException iae) {
assertEquals("Invalid to have null byte array in KeyValue.", iae.getMessage());
return;
}
Expand Down Expand Up @@ -724,7 +716,8 @@ public void testCheckKeyValueBytesFailureCase() throws Exception {
"Invalid tag length at position=28, tagLength=3",
"Invalid tag length at position=34, tagLength=4",
"Some redundant bytes in KeyValue's buffer, startOffset=41, endOffset=42", null, null,
null, };
null,
};
assertEquals(inputs.length, outputs.length);
assertEquals(withTagsInputs.length, withTagsOutputs.length);

Expand Down
43 changes: 22 additions & 21 deletions hbase-common/src/test/java/org/apache/hadoop/hbase/TestTimeout.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({SmallTests.class})
@Category(SmallTests.class)
public class TestTimeout {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestTimeout.class);

@Test
public void run1() throws InterruptedException {
Thread.sleep(100);
}
@Test
public void run1() throws InterruptedException {
Thread.sleep(100);
}

/**
* Enable to check if timeout works.
* Can't enable as it waits 30seconds and expected doesn't do Exception catching
*/
@Ignore @Test
public void infiniteLoop() {
// Launch a background non-daemon thread.
Thread t = new Thread("HangingThread") {
public void run() {
synchronized(this) {
while(true) {}
}
/**
* Enable to check if timeout works.
* Can't enable as it waits 30seconds and expected doesn't do Exception catching
*/
@Ignore
@Test
public void infiniteLoop() {
// Launch a background non-daemon thread.
Thread t = new Thread("HangingThread") {
public void run() {
synchronized(this) {
while (true) {}
}
};
t.start();
while (true) {}
}
}
};
t.start();
while (true) {}
}
}
Loading