HBASE-5609 Add the ability to pass additional information for slow query logging

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1346459 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2012-06-05 16:38:51 +00:00
parent 9a1ba2926a
commit 52b6797947
5 changed files with 78 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -39,6 +39,9 @@ public abstract class OperationWithAttributes extends Operation implements Attri
// a opaque blob of attributes
private Map<String, byte[]> 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);
}
}

View File

@ -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;
}

View File

@ -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();