From 2a979e8bb0c197834156b1c61c0b4ffe0c012883 Mon Sep 17 00:00:00 2001 From: shahrs87 Date: Thu, 18 Mar 2021 12:27:16 -0700 Subject: [PATCH] HBASE-25622 Result#compareResults should compare tags. (#3004) Signed-off-by Anoop Sam John --- .../apache/hadoop/hbase/client/Result.java | 3 +- .../org/apache/hadoop/hbase/CellUtil.java | 8 ++++ .../hadoop/hbase/client/TestResult.java | 47 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Result.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Result.java index d14171f8ddb..aaf7abfde00 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Result.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Result.java @@ -855,7 +855,8 @@ public class Result implements CellScannable, CellScanner { Cell[] replicatedKVs = res2.rawCells(); for (int i = 0; i < res1.size(); i++) { if (!ourKVs[i].equals(replicatedKVs[i]) || - !CellUtil.matchingValue(ourKVs[i], replicatedKVs[i])) { + !CellUtil.matchingValue(ourKVs[i], replicatedKVs[i]) || + !CellUtil.matchingTags(ourKVs[i], replicatedKVs[i])) { throw new Exception("This result was different: " + res1.toString() + " compared to " + res2.toString()); } diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java index 7dd7a923131..9cc981dac91 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/CellUtil.java @@ -470,6 +470,14 @@ public final class CellUtil { buf.length); } + /* + Compares tags in both cells. + */ + public static boolean matchingTags(final Cell left, final Cell right) { + return Bytes.equals(left.getTagsArray(), left.getTagsOffset(), left.getTagsLength(), + right.getTagsArray(), right.getTagsOffset(), right.getTagsLength()); + } + public static boolean matchingTimestamp(Cell a, Cell b) { return CellComparator.compareTimestamps(a, b) == 0; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestResult.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestResult.java index 5e73be3cdef..418b0fb5e16 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestResult.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestResult.java @@ -34,10 +34,13 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellScanner; import org.apache.hadoop.hbase.CellUtil; +import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.KeyValue; +import org.apache.hadoop.hbase.Tag; import org.apache.hadoop.hbase.protobuf.generated.ClientProtos; import org.apache.hadoop.hbase.testclassification.SmallTests; import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Test; import org.junit.experimental.categories.Category; @Category(SmallTests.class) @@ -247,6 +250,50 @@ public class TestResult extends TestCase { } } + /** + * Test Result#compareResults when tags are present. + * @throws Exception Exception + */ + @Test + public void testCompareResultsWithTags() throws Exception { + byte[] qualifier = Bytes.toBytes("q"); + String tagValue1 = "tagV1"; + String tagValue2 = "tagV2"; + // Create cell with 2 tags + KeyValue kv1 = new KeyValue(row, family, qualifier, HConstants.LATEST_TIMESTAMP, + value, new Tag[] {new Tag((byte) 1, tagValue1), new Tag((byte) 2, tagValue2)}); + + // Create cell with no tags. + KeyValue kv2 = new KeyValue(row, family, qualifier, HConstants.LATEST_TIMESTAMP, + value); + + Result r1 = Result.create(new KeyValue[] {kv1}); + Result r2 = Result.create(new KeyValue[] {kv2}); + // Compare result with self. + Result.compareResults(r1, r1); + + try { + // Compare result with 2 tags v/s no tags. + Result.compareResults(r1, r2); + fail(); + } catch (Exception e) { + assertTrue(e.getMessage().startsWith("This result was different:")); + } + + // Create cell with just 1 tag. + String tagValue3 = "tagV3"; + KeyValue kv3 = new KeyValue(row, family, qualifier, HConstants.LATEST_TIMESTAMP, + value, new Tag[] {new Tag((byte) 1, tagValue3)}); + Result r3 = Result.create(new KeyValue[] {kv3}); + try { + // Compare result with 2 tags v/s 1 tag. + Result.compareResults(r1, r3); + fail(); + } catch (Exception e) { + assertTrue(e.getMessage().startsWith("This result was different:")); + } + } + /** * Verifies that one can't modify instance of EMPTY_RESULT. */