use of org.hbase.async.DeleteRequest in project YCSB by brianfrankcooper.
the class AsyncHBaseClient method delete.
@Override
public Status delete(String table, String key) {
setTable(table);
if (debug) {
System.out.println("Doing delete for key: " + key);
}
final DeleteRequest delete = new DeleteRequest(lastTableBytes, key.getBytes(), columnFamilyBytes);
if (!durability) {
delete.setDurable(false);
}
if (!clientSideBuffering) {
delete.setBufferable(false);
try {
client.delete(delete).join(joinTimeout);
} catch (InterruptedException e) {
System.err.println("Thread interrupted");
Thread.currentThread().interrupt();
} catch (Exception e) {
System.err.println("Failure reading from row with key " + key + ": " + e.getMessage());
return Status.ERROR;
}
} else {
// hooray! Asynchronous write. But without a callback and an async
// YCSB call we don't know whether it succeeded or not
client.delete(delete);
}
return Status.OK;
}
use of org.hbase.async.DeleteRequest in project opentsdb by OpenTSDB.
the class MetaPurge method purgeUIDMeta.
/**
* Scans the entire UID table and removes any UIDMeta objects found.
* @return The total number of columns deleted
*/
public Deferred<Long> purgeUIDMeta() {
// a list to store all pending deletes so we don't exit before they've
// completed
final ArrayList<Deferred<Object>> delete_calls = new ArrayList<Deferred<Object>>();
final Deferred<Long> result = new Deferred<Long>();
/**
* Scanner callback that will recursively call itself and loop through the
* rows of the UID table, issuing delete requests for all of the columns in
* a row that match a meta qualifier.
*/
final class MetaScanner implements Callback<Deferred<Long>, ArrayList<ArrayList<KeyValue>>> {
final Scanner scanner;
public MetaScanner() {
scanner = getScanner(tsdb.uidTable());
}
/**
* Fetches the next group of rows from the scanner and sets this class as
* a callback
* @return The total number of columns deleted after completion
*/
public Deferred<Long> scan() {
return scanner.nextRows().addCallbackDeferring(this);
}
@Override
public Deferred<Long> call(ArrayList<ArrayList<KeyValue>> rows) throws Exception {
if (rows == null) {
result.callback(columns);
return null;
}
for (final ArrayList<KeyValue> row : rows) {
// one delete request per row. We'll almost always delete the whole
// row, so preallocate some ram.
ArrayList<byte[]> qualifiers = new ArrayList<byte[]>(row.size());
for (KeyValue column : row) {
if (Bytes.equals(TSMeta.META_QUALIFIER(), column.qualifier())) {
qualifiers.add(column.qualifier());
} else if (Bytes.equals("metric_meta".getBytes(CHARSET), column.qualifier())) {
qualifiers.add(column.qualifier());
} else if (Bytes.equals("tagk_meta".getBytes(CHARSET), column.qualifier())) {
qualifiers.add(column.qualifier());
} else if (Bytes.equals("tagv_meta".getBytes(CHARSET), column.qualifier())) {
qualifiers.add(column.qualifier());
}
}
if (qualifiers.size() > 0) {
columns += qualifiers.size();
final DeleteRequest delete = new DeleteRequest(tsdb.uidTable(), row.get(0).key(), NAME_FAMILY, qualifiers.toArray(new byte[qualifiers.size()][]));
delete_calls.add(tsdb.getClient().delete(delete));
}
}
/**
* Buffer callback used to wait on all of the delete calls for the
* last set of rows returned from the scanner so we don't fill up the
* deferreds array and OOM out.
*/
final class ContinueCB implements Callback<Deferred<Long>, ArrayList<Object>> {
@Override
public Deferred<Long> call(ArrayList<Object> deletes) throws Exception {
LOG.debug("[" + thread_id + "] Processed [" + deletes.size() + "] delete calls");
delete_calls.clear();
return scan();
}
}
// fetch the next set of rows after waiting for current set of delete
// requests to complete
Deferred.group(delete_calls).addCallbackDeferring(new ContinueCB());
return null;
}
}
// start the scan
new MetaScanner().scan();
return result;
}
use of org.hbase.async.DeleteRequest in project opentsdb by OpenTSDB.
the class MetaPurge method purgeTSMeta.
/**
* Scans the entire UID table and removes any UIDMeta objects found.
* @return The total number of columns deleted
*/
public Deferred<Long> purgeTSMeta() {
// a list to store all pending deletes so we don't exit before they've
// completed
final ArrayList<Deferred<Object>> delete_calls = new ArrayList<Deferred<Object>>();
final Deferred<Long> result = new Deferred<Long>();
/**
* Scanner callback that will recursively call itself and loop through the
* rows of the UID table, issuing delete requests for all of the columns in
* a row that match a meta qualifier.
*/
final class MetaScanner implements Callback<Deferred<Long>, ArrayList<ArrayList<KeyValue>>> {
final Scanner scanner;
public MetaScanner() {
scanner = getScanner(tsdb.metaTable());
}
/**
* Fetches the next group of rows from the scanner and sets this class as
* a callback
* @return The total number of columns deleted after completion
*/
public Deferred<Long> scan() {
return scanner.nextRows().addCallbackDeferring(this);
}
@Override
public Deferred<Long> call(ArrayList<ArrayList<KeyValue>> rows) throws Exception {
if (rows == null) {
result.callback(columns);
return null;
}
for (final ArrayList<KeyValue> row : rows) {
// one delete request per row. We'll almost always delete the whole
// row, so preallocate some ram.
ArrayList<byte[]> qualifiers = new ArrayList<byte[]>(row.size());
for (KeyValue column : row) {
if (Bytes.equals(TSMeta.META_QUALIFIER(), column.qualifier())) {
qualifiers.add(column.qualifier());
} else if (Bytes.equals(TSMeta.COUNTER_QUALIFIER(), column.qualifier())) {
qualifiers.add(column.qualifier());
}
}
if (qualifiers.size() > 0) {
columns += qualifiers.size();
final DeleteRequest delete = new DeleteRequest(tsdb.metaTable(), row.get(0).key(), NAME_FAMILY, qualifiers.toArray(new byte[qualifiers.size()][]));
delete_calls.add(tsdb.getClient().delete(delete));
}
}
/**
* Buffer callback used to wait on all of the delete calls for the
* last set of rows returned from the scanner so we don't fill up the
* deferreds array and OOM out.
*/
final class ContinueCB implements Callback<Deferred<Long>, ArrayList<Object>> {
@Override
public Deferred<Long> call(ArrayList<Object> deletes) throws Exception {
LOG.debug("[" + thread_id + "] Processed [" + deletes.size() + "] delete calls");
delete_calls.clear();
return scan();
}
}
// fetch the next set of rows after waiting for current set of delete
// requests to complete
Deferred.group(delete_calls).addCallbackDeferring(new ContinueCB());
return null;
}
}
// start the scan
new MetaScanner().scan();
return result;
}
use of org.hbase.async.DeleteRequest in project opentsdb by OpenTSDB.
the class DumpSeries method doDump.
private static void doDump(final TSDB tsdb, final HBaseClient client, final byte[] table, final boolean delete, final boolean importformat, final String[] args) throws Exception {
final ArrayList<Query> queries = new ArrayList<Query>();
CliQuery.parseCommandLineQuery(args, tsdb, queries, null, null);
final StringBuilder buf = new StringBuilder();
for (final Query query : queries) {
final List<Scanner> scanners = Internal.getScanners(query);
for (Scanner scanner : scanners) {
ArrayList<ArrayList<KeyValue>> rows;
while ((rows = scanner.nextRows().joinUninterruptibly()) != null) {
for (final ArrayList<KeyValue> row : rows) {
buf.setLength(0);
final byte[] key = row.get(0).key();
final long base_time = Internal.baseTime(tsdb, key);
final String metric = Internal.metricName(tsdb, key);
// Print the row key.
if (!importformat) {
buf.append(Arrays.toString(key)).append(' ').append(metric).append(' ').append(base_time).append(" (").append(date(base_time)).append(") ");
try {
buf.append(Internal.getTags(tsdb, key));
} catch (RuntimeException e) {
buf.append(e.getClass().getName() + ": " + e.getMessage());
}
buf.append('\n');
System.out.print(buf);
}
// Print individual cells.
buf.setLength(0);
if (!importformat) {
buf.append(" ");
}
for (final KeyValue kv : row) {
// Discard everything or keep initial spaces.
buf.setLength(importformat ? 0 : 2);
formatKeyValue(buf, tsdb, importformat, kv, base_time, metric);
if (buf.length() > 0) {
buf.append('\n');
System.out.print(buf);
}
}
if (delete) {
final DeleteRequest del = new DeleteRequest(table, key);
client.delete(del);
}
}
}
}
}
}
use of org.hbase.async.DeleteRequest in project opentsdb by OpenTSDB.
the class Tree method deleteTree.
/**
* Attempts to delete all branches, leaves, collisions and not-matched entries
* for the given tree. Optionally can delete the tree definition and rules as
* well.
* <b>Warning:</b> This call can take a long time to complete so it should
* only be done from a command line or issues once via RPC and allowed to
* process. Multiple deletes running at the same time on the same tree
* shouldn't be an issue but it's a waste of resources.
* @param tsdb The TSDB to use for storage access
* @param tree_id ID of the tree to delete
* @param delete_definition Whether or not the tree definition and rule set
* should be deleted as well
* @return True if the deletion completed successfully, false if there was an
* issue.
* @throws HBaseException if there was an issue
* @throws IllegalArgumentException if the tree ID was invalid
*/
public static Deferred<Boolean> deleteTree(final TSDB tsdb, final int tree_id, final boolean delete_definition) {
if (tree_id < 1 || tree_id > 65535) {
throw new IllegalArgumentException("Invalid Tree ID");
}
// scan all of the rows starting with the tree ID. We can't just delete the
// rows as there may be other types of data. Thus we have to check the
// qualifiers of every column to see if it's safe to delete
final byte[] start = idToBytes(tree_id);
final byte[] end = idToBytes(tree_id + 1);
final Scanner scanner = tsdb.getClient().newScanner(tsdb.treeTable());
scanner.setStartKey(start);
scanner.setStopKey(end);
scanner.setFamily(TREE_FAMILY);
final Deferred<Boolean> completed = new Deferred<Boolean>();
/**
* Scanner callback that loops through all rows between tree id and
* tree id++ searching for tree related columns to delete.
*/
final class DeleteTreeScanner implements Callback<Deferred<Boolean>, ArrayList<ArrayList<KeyValue>>> {
// list where we'll store delete requests for waiting on
private final ArrayList<Deferred<Object>> delete_deferreds = new ArrayList<Deferred<Object>>();
/**
* Fetches the next set of rows from the scanner and adds this class as
* a callback
* @return The list of delete requests when the scanner returns a null set
*/
public Deferred<Boolean> deleteTree() {
return scanner.nextRows().addCallbackDeferring(this);
}
@Override
public Deferred<Boolean> call(ArrayList<ArrayList<KeyValue>> rows) throws Exception {
if (rows == null) {
completed.callback(true);
return null;
}
for (final ArrayList<KeyValue> row : rows) {
// one delete request per row. We'll almost always delete the whole
// row, so just preallocate the entire row.
ArrayList<byte[]> qualifiers = new ArrayList<byte[]>(row.size());
for (KeyValue column : row) {
// tree
if (delete_definition && Bytes.equals(TREE_QUALIFIER, column.qualifier())) {
LOG.trace("Deleting tree defnition in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
// branches
} else if (Bytes.equals(Branch.BRANCH_QUALIFIER(), column.qualifier())) {
LOG.trace("Deleting branch in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
// leaves
} else if (column.qualifier().length > Leaf.LEAF_PREFIX().length && Bytes.memcmp(Leaf.LEAF_PREFIX(), column.qualifier(), 0, Leaf.LEAF_PREFIX().length) == 0) {
LOG.trace("Deleting leaf in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
// collisions
} else if (column.qualifier().length > COLLISION_PREFIX.length && Bytes.memcmp(COLLISION_PREFIX, column.qualifier(), 0, COLLISION_PREFIX.length) == 0) {
LOG.trace("Deleting collision in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
// not matched
} else if (column.qualifier().length > NOT_MATCHED_PREFIX.length && Bytes.memcmp(NOT_MATCHED_PREFIX, column.qualifier(), 0, NOT_MATCHED_PREFIX.length) == 0) {
LOG.trace("Deleting not matched in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
// tree rule
} else if (delete_definition && column.qualifier().length > TreeRule.RULE_PREFIX().length && Bytes.memcmp(TreeRule.RULE_PREFIX(), column.qualifier(), 0, TreeRule.RULE_PREFIX().length) == 0) {
LOG.trace("Deleting tree rule in row: " + Branch.idToString(column.key()));
qualifiers.add(column.qualifier());
}
}
if (qualifiers.size() > 0) {
final DeleteRequest delete = new DeleteRequest(tsdb.treeTable(), row.get(0).key(), TREE_FAMILY, qualifiers.toArray(new byte[qualifiers.size()][]));
delete_deferreds.add(tsdb.getClient().delete(delete));
}
}
/**
* Callback used as a kind of buffer so that we don't wind up loading
* thousands or millions of delete requests into memory and possibly run
* into a StackOverflowError or general OOM. The scanner defaults are
* our limit so each pass of the scanner will wait for the previous set
* of deferreds to complete before continuing
*/
final class ContinueCB implements Callback<Deferred<Boolean>, ArrayList<Object>> {
public Deferred<Boolean> call(ArrayList<Object> objects) {
LOG.debug("Purged [" + objects.size() + "] columns, continuing");
delete_deferreds.clear();
// call ourself again to get the next set of rows from the scanner
return deleteTree();
}
}
// call ourself again after waiting for the existing delete requests
// to complete
Deferred.group(delete_deferreds).addCallbackDeferring(new ContinueCB());
return null;
}
}
// start the scanner
new DeleteTreeScanner().deleteTree();
return completed;
}
Aggregations