You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by mb...@apache.org on 2015/05/29 19:32:41 UTC
[4/5] hbase git commit: HBASE-13779 Calling table.exists() before
table.get() end up with an empty Result
HBASE-13779 Calling table.exists() before table.get() end up with an empty Result
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/d1f761ee
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/d1f761ee
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/d1f761ee
Branch: refs/heads/branch-1.0
Commit: d1f761eeca33610768fd58ced66e7417ffc6055e
Parents: 1c50682
Author: Matteo Bertozzi <ma...@cloudera.com>
Authored: Fri May 29 16:39:03 2015 +0100
Committer: Matteo Bertozzi <ma...@cloudera.com>
Committed: Fri May 29 17:48:28 2015 +0100
----------------------------------------------------------------------
.../org/apache/hadoop/hbase/client/Get.java | 13 +++-
.../org/apache/hadoop/hbase/client/HTable.java | 28 ++++++--
.../hbase/client/TestFromClientSide3.java | 70 ++++++++++++++++++--
3 files changed, 97 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/hbase/blob/d1f761ee/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Get.java
----------------------------------------------------------------------
diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Get.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Get.java
index eb3553b..32e3114 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Get.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Get.java
@@ -107,7 +107,18 @@ public class Get extends Query
this.tr = get.getTimeRange();
this.checkExistenceOnly = get.isCheckExistenceOnly();
this.closestRowBefore = get.isClosestRowBefore();
- this.familyMap = get.getFamilyMap();
+ Map<byte[], NavigableSet<byte[]>> fams = get.getFamilyMap();
+ for (Map.Entry<byte[],NavigableSet<byte[]>> entry : fams.entrySet()) {
+ byte [] fam = entry.getKey();
+ NavigableSet<byte[]> cols = entry.getValue();
+ if (cols != null && cols.size() > 0) {
+ for (byte[] col : cols) {
+ addColumn(fam, col);
+ }
+ } else {
+ addFamily(fam);
+ }
+ }
for (Map.Entry<String, byte[]> attr : get.getAttributesMap().entrySet()) {
setAttribute(attr.getKey(), attr.getValue());
}
http://git-wip-us.apache.org/repos/asf/hbase/blob/d1f761ee/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java
----------------------------------------------------------------------
diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java
index 13827d6..689e827 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java
@@ -74,6 +74,7 @@ import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableDescripto
import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableDescriptorsResponse;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
+import org.apache.hadoop.hbase.util.ReflectionUtils;
import org.apache.hadoop.hbase.util.Threads;
import com.google.common.annotations.VisibleForTesting;
@@ -800,6 +801,7 @@ public class HTable implements HTableInterface, RegionLocator {
if (scan.getBatch() > 0 && scan.isSmall()) {
throw new IllegalArgumentException("Small scan should not be used with batching");
}
+
if (scan.getCaching() <= 0) {
scan.setCaching(getScannerCaching());
}
@@ -858,18 +860,28 @@ public class HTable implements HTableInterface, RegionLocator {
*/
@Override
public Result get(final Get get) throws IOException {
- if (get.getConsistency() == null){
- get.setConsistency(defaultConsistency);
+ return get(get, false);
+ }
+
+ private Result get(Get get, final boolean checkExistenceOnly) throws IOException {
+ // if we are changing settings to the get, clone it.
+ if (get.isCheckExistenceOnly() != checkExistenceOnly || get.getConsistency() == null) {
+ get = ReflectionUtils.newInstance(get.getClass(), get);
+ get.setCheckExistenceOnly(checkExistenceOnly);
+ if (get.getConsistency() == null){
+ get.setConsistency(defaultConsistency);
+ }
}
if (get.getConsistency() == Consistency.STRONG) {
// Good old call.
+ final Get getReq = get;
RegionServerCallable<Result> callable = new RegionServerCallable<Result>(this.connection,
getName(), get.getRow()) {
@Override
public Result call(int callTimeout) throws IOException {
ClientProtos.GetRequest request =
- RequestConverter.buildGetRequest(getLocation().getRegionInfo().getRegionName(), get);
+ RequestConverter.buildGetRequest(getLocation().getRegionInfo().getRegionName(), getReq);
PayloadCarryingRpcController controller = rpcControllerFactory.newController();
controller.setPriority(tableName);
controller.setCallTimeout(callTimeout);
@@ -1377,8 +1389,7 @@ public class HTable implements HTableInterface, RegionLocator {
*/
@Override
public boolean exists(final Get get) throws IOException {
- get.setCheckExistenceOnly(true);
- Result r = get(get);
+ Result r = get(get, true);
assert r.getExists() != null;
return r.getExists();
}
@@ -1391,13 +1402,16 @@ public class HTable implements HTableInterface, RegionLocator {
if (gets.isEmpty()) return new boolean[]{};
if (gets.size() == 1) return new boolean[]{exists(gets.get(0))};
+ ArrayList<Get> exists = new ArrayList<Get>(gets.size());
for (Get g: gets){
- g.setCheckExistenceOnly(true);
+ Get ge = new Get(g);
+ ge.setCheckExistenceOnly(true);
+ exists.add(ge);
}
Object[] r1;
try {
- r1 = batch(gets);
+ r1 = batch(exists);
} catch (InterruptedException e) {
throw (InterruptedIOException)new InterruptedIOException().initCause(e);
}
http://git-wip-us.apache.org/repos/asf/hbase/blob/d1f761ee/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
index e2bd999..4afe3e7 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java
@@ -96,7 +96,10 @@ public class TestFromClientSide3 {
*/
@After
public void tearDown() throws Exception {
- // Nothing to do.
+ for (HTableDescriptor htd: TEST_UTIL.getHBaseAdmin().listTables()) {
+ LOG.info("Tear down, remove table=" + htd.getTableName());
+ TEST_UTIL.deleteTable(htd.getTableName());
+ }
}
private void randomCFPuts(Table table, byte[] row, byte[] family, int nPuts)
@@ -276,20 +279,20 @@ public class TestFromClientSide3 {
// create an empty Put
Put put1 = new Put(ROW);
actions.add(put1);
-
+
Put put2 = new Put(ANOTHERROW);
put2.add(FAMILY, QUALIFIER, VALUE);
actions.add(put2);
-
+
table.batch(actions, results);
fail("Empty Put should have failed the batch call");
} catch (IllegalArgumentException iae) {
-
+
} finally {
table.close();
}
}
-
+
@Test
public void testHTableExistsMethodSingleRegionSingleGet() throws Exception {
@@ -333,6 +336,61 @@ public class TestFromClientSide3 {
}
@Test
+ public void testHTableExistsBeforeGet() throws Exception {
+ Table table = TEST_UTIL.createTable(
+ Bytes.toBytes("testHTableExistsBeforeGet"), new byte[][] { FAMILY });
+ try {
+ Put put = new Put(ROW);
+ put.add(FAMILY, QUALIFIER, VALUE);
+ table.put(put);
+
+ Get get = new Get(ROW);
+
+ boolean exist = table.exists(get);
+ assertEquals(true, exist);
+
+ Result result = table.get(get);
+ assertEquals(false, result.isEmpty());
+ assertTrue(Bytes.equals(VALUE, result.getValue(FAMILY, QUALIFIER)));
+ } finally {
+ table.close();
+ }
+ }
+
+ @Test
+ public void testHTableExistsAllBeforeGet() throws Exception {
+ final byte[] ROW2 = Bytes.add(ROW, Bytes.toBytes("2"));
+ Table table = TEST_UTIL.createTable(
+ Bytes.toBytes("testHTableExistsAllBeforeGet"), new byte[][] { FAMILY });
+ try {
+ Put put = new Put(ROW);
+ put.add(FAMILY, QUALIFIER, VALUE);
+ table.put(put);
+ put = new Put(ROW2);
+ put.add(FAMILY, QUALIFIER, VALUE);
+ table.put(put);
+
+ Get get = new Get(ROW);
+ Get get2 = new Get(ROW2);
+ ArrayList<Get> getList = new ArrayList(2);
+ getList.add(get);
+ getList.add(get2);
+
+ boolean[] exists = table.existsAll(getList);
+ assertEquals(true, exists[0]);
+ assertEquals(true, exists[1]);
+
+ Result[] result = table.get(getList);
+ assertEquals(false, result[0].isEmpty());
+ assertTrue(Bytes.equals(VALUE, result[0].getValue(FAMILY, QUALIFIER)));
+ assertEquals(false, result[1].isEmpty());
+ assertTrue(Bytes.equals(VALUE, result[1].getValue(FAMILY, QUALIFIER)));
+ } finally {
+ table.close();
+ }
+ }
+
+ @Test
public void testHTableExistsMethodMultipleRegionsSingleGet() throws Exception {
Table table = TEST_UTIL.createTable(
@@ -355,7 +413,7 @@ public class TestFromClientSide3 {
@Test
public void testHTableExistsMethodMultipleRegionsMultipleGets() throws Exception {
HTable table = TEST_UTIL.createTable(
- TableName.valueOf("testHTableExistsMethodMultipleRegionsMultipleGets"),
+ TableName.valueOf("testHTableExistsMethodMultipleRegionsMultipleGets"),
new byte[][] { FAMILY }, 1, new byte[] { 0x00 }, new byte[] { (byte) 0xff }, 255);
Put put = new Put(ROW);
put.add(FAMILY, QUALIFIER, VALUE);