HBASE-18617 FuzzyRowKeyFilter should not modify the filter pairs (vinisha)

This commit is contained in:
tedyu 2017-08-18 10:02:06 -07:00
parent deeda60c69
commit 64bef67558
2 changed files with 32 additions and 16 deletions

View File

@ -77,20 +77,27 @@ public class FuzzyRowFilter extends FilterBase {
private RowTracker tracker; private RowTracker tracker;
public FuzzyRowFilter(List<Pair<byte[], byte[]>> fuzzyKeysData) { public FuzzyRowFilter(List<Pair<byte[], byte[]>> fuzzyKeysData) {
Pair<byte[], byte[]> p; List<Pair<byte[], byte[]>> fuzzyKeyDataCopy = new ArrayList<>(fuzzyKeysData.size());
for (int i = 0; i < fuzzyKeysData.size(); i++) {
p = fuzzyKeysData.get(i); for (Pair<byte[], byte[]> aFuzzyKeysData : fuzzyKeysData) {
if (p.getFirst().length != p.getSecond().length) { if (aFuzzyKeysData.getFirst().length != aFuzzyKeysData.getSecond().length) {
Pair<String, String> readable = Pair<String, String> readable =
new Pair<String, String>(Bytes.toStringBinary(p.getFirst()), Bytes.toStringBinary(p new Pair<>(Bytes.toStringBinary(aFuzzyKeysData.getFirst()), Bytes.toStringBinary(aFuzzyKeysData.getSecond()));
.getSecond()));
throw new IllegalArgumentException("Fuzzy pair lengths do not match: " + readable); throw new IllegalArgumentException("Fuzzy pair lengths do not match: " + readable);
} }
Pair<byte[], byte[]> p = new Pair<>();
// create a copy of pair bytes so that they are not modified by the filter.
p.setFirst(Arrays.copyOf(aFuzzyKeysData.getFirst(), aFuzzyKeysData.getFirst().length));
p.setSecond(Arrays.copyOf(aFuzzyKeysData.getSecond(), aFuzzyKeysData.getSecond().length));
// update mask ( 0 -> -1 (0xff), 1 -> 2) // update mask ( 0 -> -1 (0xff), 1 -> 2)
p.setSecond(preprocessMask(p.getSecond())); p.setSecond(preprocessMask(p.getSecond()));
preprocessSearchKey(p); preprocessSearchKey(p);
fuzzyKeyDataCopy.add(p);
} }
this.fuzzyKeysData = fuzzyKeysData; this.fuzzyKeysData = fuzzyKeyDataCopy;
this.tracker = new RowTracker(); this.tracker = new RowTracker();
} }

View File

@ -17,13 +17,7 @@
*/ */
package org.apache.hadoop.hbase.filter; package org.apache.hadoop.hbase.filter;
import static org.junit.Assert.assertEquals; import com.google.common.collect.Lists;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -33,11 +27,11 @@ import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Durability; import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FilterList.Operator; import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy; import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
@ -53,7 +47,13 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import com.google.common.collect.Lists; import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
/** /**
*/ */
@ -136,6 +136,11 @@ public class TestFuzzyRowFilterEndToEnd {
List<Pair<byte[], byte[]>> data = new ArrayList<Pair<byte[], byte[]>>(); List<Pair<byte[], byte[]>> data = new ArrayList<Pair<byte[], byte[]>>();
byte[] fuzzyKey = Bytes.toBytesBinary("\\x9B\\x00\\x044e"); byte[] fuzzyKey = Bytes.toBytesBinary("\\x9B\\x00\\x044e");
byte[] mask = new byte[] { 0, 0, 0, 0, 0 }; byte[] mask = new byte[] { 0, 0, 0, 0, 0 };
// copy the fuzzy key and mask to test HBASE-18617
byte[] copyFuzzyKey = Arrays.copyOf(fuzzyKey, fuzzyKey.length);
byte[] copyMask = Arrays.copyOf(mask, mask.length);
data.add(new Pair<byte[], byte[]>(fuzzyKey, mask)); data.add(new Pair<byte[], byte[]>(fuzzyKey, mask));
FuzzyRowFilter filter = new FuzzyRowFilter(data); FuzzyRowFilter filter = new FuzzyRowFilter(data);
@ -148,6 +153,10 @@ public class TestFuzzyRowFilterEndToEnd {
total++; total++;
} }
assertEquals(2, total); assertEquals(2, total);
assertEquals(true, Arrays.equals(copyFuzzyKey, fuzzyKey));
assertEquals(true, Arrays.equals(copyMask, mask));
TEST_UTIL.deleteTable(TableName.valueOf(table)); TEST_UTIL.deleteTable(TableName.valueOf(table));
} }