Skip to content

Commit

Permalink
HBASE-24030 Add necessary validations to HRegion.checkAndMutate() and…
Browse files Browse the repository at this point in the history
… HRegion.checkAndRowMutate() (#1315)

Signed-off-by: Viraj Jasani <vjasani@apache.org>
Signed-off-by: Jan Hentschel <janh@apache.org>
  • Loading branch information
brfrn169 committed Mar 22, 2020
1 parent e6adb79 commit 5104aa8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
import org.apache.hadoop.hbase.client.RegionReplicaUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Row;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.TableDescriptor;
Expand Down Expand Up @@ -4180,7 +4181,6 @@ protected Durability getEffectiveDurability(Durability d) {
@Override
public boolean checkAndMutate(byte[] row, byte[] family, byte[] qualifier, CompareOperator op,
ByteArrayComparable comparator, TimeRange timeRange, Mutation mutation) throws IOException {
checkMutationType(mutation, row);
return doCheckAndRowMutate(row, family, qualifier, op, comparator, null, timeRange, null,
mutation);
}
Expand Down Expand Up @@ -4216,6 +4216,12 @@ private boolean doCheckAndRowMutate(byte[] row, byte[] family, byte[] qualifier,
// need these commented out checks.
// if (rowMutations == null && mutation == null) throw new DoNotRetryIOException("Both null");
// if (rowMutations != null && mutation != null) throw new DoNotRetryIOException("Both set");
if (mutation != null) {
checkMutationType(mutation);
checkRow(mutation, row);
} else {
checkRow(rowMutations, row);
}
checkReadOnly();
// TODO, add check for value length also move this check to the client
checkResources();
Expand Down Expand Up @@ -4331,13 +4337,17 @@ private boolean doCheckAndRowMutate(byte[] row, byte[] family, byte[] qualifier,
}
}

private void checkMutationType(final Mutation mutation, final byte [] row)
private void checkMutationType(final Mutation mutation)
throws DoNotRetryIOException {
boolean isPut = mutation instanceof Put;
if (!isPut && !(mutation instanceof Delete)) {
throw new org.apache.hadoop.hbase.DoNotRetryIOException("Action must be Put or Delete");
}
if (!Bytes.equals(row, mutation.getRow())) {
}

private void checkRow(final Row action, final byte[] row)
throws DoNotRetryIOException {
if (!Bytes.equals(row, action.getRow())) {
throw new org.apache.hadoop.hbase.DoNotRetryIOException("Action's getRow must match");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
import org.apache.hadoop.hbase.DoNotRetryIOException;
import org.apache.hadoop.hbase.DroppedSnapshotException;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseConfiguration;
Expand Down Expand Up @@ -2272,6 +2273,78 @@ public void testCheckAndMutate_WithFiltersAndTimeRange() throws Throwable {
assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("A"))).isEmpty());
}

@Test
public void testCheckAndMutate_wrongMutationType() throws Throwable {
// Setting up region
this.region = initHRegion(tableName, method, CONF, fam1);

try {
region.checkAndMutate(row, fam1, qual1, CompareOperator.EQUAL, new BinaryComparator(value1),
new Increment(row).addColumn(fam1, qual1, 1));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action must be Put or Delete", e.getMessage());
}

try {
region.checkAndMutate(row,
new SingleColumnValueFilter(fam1, qual1, CompareOperator.EQUAL, value1),
new Increment(row).addColumn(fam1, qual1, 1));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action must be Put or Delete", e.getMessage());
}
}

@Test
public void testCheckAndMutate_wrongRow() throws Throwable {
final byte[] wrongRow = Bytes.toBytes("wrongRow");

// Setting up region
this.region = initHRegion(tableName, method, CONF, fam1);

try {
region.checkAndMutate(row, fam1, qual1, CompareOperator.EQUAL, new BinaryComparator(value1),
new Put(wrongRow).addColumn(fam1, qual1, value1));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action's getRow must match", e.getMessage());
}

try {
region.checkAndMutate(row,
new SingleColumnValueFilter(fam1, qual1, CompareOperator.EQUAL, value1),
new Put(wrongRow).addColumn(fam1, qual1, value1));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action's getRow must match", e.getMessage());
}

try {
region.checkAndRowMutate(row, fam1, qual1, CompareOperator.EQUAL,
new BinaryComparator(value1),
new RowMutations(wrongRow)
.add((Mutation) new Put(wrongRow)
.addColumn(fam1, qual1, value1))
.add((Mutation) new Delete(wrongRow).addColumns(fam1, qual2)));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action's getRow must match", e.getMessage());
}

try {
region.checkAndRowMutate(row,
new SingleColumnValueFilter(fam1, qual1, CompareOperator.EQUAL, value1),
new RowMutations(wrongRow)
.add((Mutation) new Put(wrongRow)
.addColumn(fam1, qual1, value1))
.add((Mutation) new Delete(wrongRow).addColumns(fam1, qual2)));
fail("should throw DoNotRetryIOException");
} catch (DoNotRetryIOException e) {
assertEquals("Action's getRow must match", e.getMessage());
}
}

// ////////////////////////////////////////////////////////////////////////////
// Delete tests
// ////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5104aa8

Please sign in to comment.