diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Get.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Get.java index 2e8b7957a3d..0136a2923af 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Get.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Get.java @@ -375,6 +375,10 @@ public class Get extends OperationWithAttributes if (this.filter != null) { map.put("filter", this.filter.toString()); } + // add the id if set + if (getId() != null) { + map.put("id", getId()); + } return map; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Mutation.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Mutation.java index eb5c481947d..3f32ac9cdc9 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Mutation.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Mutation.java @@ -106,6 +106,10 @@ public abstract class Mutation extends OperationWithAttributes implements Row { } } map.put("totalColumns", colCount); + // add the id if set + if (getId() != null) { + map.put("id", getId()); + } return map; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/OperationWithAttributes.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/OperationWithAttributes.java index 92abc287922..c6ffd99e448 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/OperationWithAttributes.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/OperationWithAttributes.java @@ -39,6 +39,9 @@ public abstract class OperationWithAttributes extends Operation implements Attri // a opaque blob of attributes private Map attributes; + // used for uniquely identifying an operation + static public String ID_ATRIBUTE = "_operation.attributes.id"; + public void setAttribute(String name, byte[] value) { if (attributes == null && value == null) { return; @@ -108,4 +111,27 @@ public abstract class OperationWithAttributes extends Operation implements Attri } } } + + /** + * This method allows you to set an identifier on an operation. The original + * motivation for this was to allow the identifier to be used in slow query + * logging, but this could obviously be useful in other places. One use of + * this could be to put a class.method identifier in here to see where the + * slow query is coming from. + * @param id + * id to set for the scan + */ + public void setId(String id) { + setAttribute(ID_ATRIBUTE, Bytes.toBytes(id)); + } + + /** + * This method allows you to retrieve the identifier for the operation if one + * was set. + * @return the id or null if not set + */ + public String getId() { + byte[] attr = getAttribute(ID_ATRIBUTE); + return attr == null? null: Bytes.toString(attr); + } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Scan.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Scan.java index 3bd9dda0424..f55dd11a8bf 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Scan.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/Scan.java @@ -560,6 +560,10 @@ public class Scan extends OperationWithAttributes implements Writable { if (this.filter != null) { map.put("filter", this.filter.toString()); } + // add the id if set + if (getId() != null) { + map.put("id", getId()); + } return map; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAttributes.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAttributes.java index 4821ceaf06a..e071b769736 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAttributes.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAttributes.java @@ -154,6 +154,46 @@ public class TestAttributes { Assert.assertNull(del.getAttributesMap().get("attribute1")); } + @Test + public void testGetId() { + Get get = new Get(); + Assert.assertNull("Make sure id is null if unset", get.toMap().get("id")); + get.setId("myId"); + Assert.assertEquals("myId", get.toMap().get("id")); + } + + @Test + public void testAppendId() { + Append append = new Append(); + Assert.assertNull("Make sure id is null if unset", append.toMap().get("id")); + append.setId("myId"); + Assert.assertEquals("myId", append.toMap().get("id")); + } + + @Test + public void testDeleteId() { + Delete delete = new Delete(); + Assert.assertNull("Make sure id is null if unset", delete.toMap().get("id")); + delete.setId("myId"); + Assert.assertEquals("myId", delete.toMap().get("id")); + } + + @Test + public void testPutId() { + Put put = new Put(); + Assert.assertNull("Make sure id is null if unset", put.toMap().get("id")); + put.setId("myId"); + Assert.assertEquals("myId", put.toMap().get("id")); + } + + @Test + public void testScanId() { + Scan scan = new Scan(); + Assert.assertNull("Make sure id is null if unset", scan.toMap().get("id")); + scan.setId("myId"); + Assert.assertEquals("myId", scan.toMap().get("id")); + } + @org.junit.Rule public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();