Search in sources :

Example 26 with GetRequest

use of org.hbase.async.GetRequest in project opentsdb by OpenTSDB.

the class TSMeta method metaExistsInStorage.

/**
 * Determines if an entry exists in storage or not.
 * This is used by the UID Manager tool to determine if we need to write a
 * new TSUID entry or not. It will not attempt to verify if the stored data is
 * valid, just checks to see if something is stored in the proper column.
 * @param tsdb  The TSDB to use for storage access
 * @param tsuid The UID of the meta to verify
 * @return True if data was found, false if not
 * @throws HBaseException if there was an issue fetching
 */
public static Deferred<Boolean> metaExistsInStorage(final TSDB tsdb, final String tsuid) {
    final GetRequest get = new GetRequest(tsdb.metaTable(), UniqueId.stringToUid(tsuid));
    get.family(FAMILY);
    get.qualifier(META_QUALIFIER);
    /**
     * Callback from the GetRequest that simply determines if the row is empty
     * or not
     */
    final class ExistsCB implements Callback<Boolean, ArrayList<KeyValue>> {

        @Override
        public Boolean call(ArrayList<KeyValue> row) throws Exception {
            if (row == null || row.isEmpty() || row.get(0).value() == null) {
                return false;
            }
            return true;
        }
    }
    return tsdb.getClient().get(get).addCallback(new ExistsCB());
}
Also used : Callback(com.stumbleupon.async.Callback) KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) ArrayList(java.util.ArrayList)

Example 27 with GetRequest

use of org.hbase.async.GetRequest in project opentsdb by OpenTSDB.

the class TSMeta method storeIfNecessary.

/**
 * Attempts to fetch the meta column and if null, attempts to write a new
 * column using {@link #storeNew}.
 * @param tsdb The TSDB instance to use for access.
 * @param tsuid The TSUID of the time series.
 * @return A deferred with a true if the meta exists or was created, false
 * if the meta did not exist and writing failed.
 */
public static Deferred<Boolean> storeIfNecessary(final TSDB tsdb, final byte[] tsuid) {
    final GetRequest get = new GetRequest(tsdb.metaTable(), tsuid);
    get.family(FAMILY);
    get.qualifier(META_QUALIFIER);
    final class CreateNewCB implements Callback<Deferred<Boolean>, Object> {

        @Override
        public Deferred<Boolean> call(Object arg0) throws Exception {
            final TSMeta meta = new TSMeta(tsuid, System.currentTimeMillis() / 1000);
            final class FetchNewCB implements Callback<Deferred<Boolean>, TSMeta> {

                @Override
                public Deferred<Boolean> call(TSMeta stored_meta) throws Exception {
                    // pass to the search plugin
                    tsdb.indexTSMeta(stored_meta);
                    // pass through the trees
                    tsdb.processTSMetaThroughTrees(stored_meta);
                    return Deferred.fromResult(true);
                }
            }
            final class StoreNewCB implements Callback<Deferred<Boolean>, Boolean> {

                @Override
                public Deferred<Boolean> call(Boolean success) throws Exception {
                    if (!success) {
                        LOG.warn("Unable to save metadata: " + meta);
                        return Deferred.fromResult(false);
                    }
                    LOG.info("Successfullly created new TSUID entry for: " + meta);
                    return new LoadUIDs(tsdb, UniqueId.uidToString(tsuid)).call(meta).addCallbackDeferring(new FetchNewCB());
                }
            }
            return meta.storeNew(tsdb).addCallbackDeferring(new StoreNewCB());
        }
    }
    final class ExistsCB implements Callback<Deferred<Boolean>, ArrayList<KeyValue>> {

        @Override
        public Deferred<Boolean> call(ArrayList<KeyValue> row) throws Exception {
            if (row == null || row.isEmpty() || row.get(0).value() == null) {
                return new CreateNewCB().call(null);
            }
            return Deferred.fromResult(true);
        }
    }
    return tsdb.getClient().get(get).addCallbackDeferring(new ExistsCB());
}
Also used : Callback(com.stumbleupon.async.Callback) KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) ArrayList(java.util.ArrayList)

Example 28 with GetRequest

use of org.hbase.async.GetRequest in project opentsdb by OpenTSDB.

the class TSUIDQuery method getLastPoint.

/**
 * Attempts to fetch the last data point for the given metric or TSUID.
 * If back_scan == 0 and meta is enabled via
 * "tsd.core.meta.enable_tsuid_tracking" or
 * "tsd.core.meta.enable_tsuid_incrementing" then we will look up the metric
 * or TSUID in the meta table first and use the counter there to get the
 * last write time.
 * <p>
 * However if backscan is set, then we'll start with the current time and
 * iterate back "back_scan" number of hours until we find a value.
 * <p>
 * @param resolve_names Whether or not to resolve the UIDs back to their
 * names when we find a value.
 * @param back_scan The number of hours back in time to scan
 * @return A data point if found, null if not. Or an exception if something
 * went pear shaped.
 */
public Deferred<IncomingDataPoint> getLastPoint(final boolean resolve_names, final int back_scan) {
    if (back_scan < 0) {
        throw new IllegalArgumentException("Backscan must be zero or a positive number");
    }
    this.resolve_names = resolve_names;
    this.back_scan = back_scan;
    final boolean meta_enabled = tsdb.getConfig().enable_tsuid_tracking() || tsdb.getConfig().enable_tsuid_incrementing();
    class TSUIDCB implements Callback<Deferred<IncomingDataPoint>, byte[]> {

        @Override
        public Deferred<IncomingDataPoint> call(final byte[] incoming_tsuid) throws Exception {
            if (tsuid == null && incoming_tsuid == null) {
                return Deferred.fromError(new RuntimeException("Both incoming and " + "supplied TSUIDs were null for " + TSUIDQuery.this));
            } else if (incoming_tsuid != null) {
                setTSUID(incoming_tsuid);
            }
            if (back_scan < 1 && meta_enabled) {
                final GetRequest get = new GetRequest(tsdb.metaTable(), tsuid);
                get.family(TSMeta.FAMILY());
                get.qualifier(TSMeta.COUNTER_QUALIFIER());
                return tsdb.getClient().get(get).addCallbackDeferring(new MetaCB());
            }
            if (last_timestamp > 0) {
                last_timestamp = Internal.baseTime(last_timestamp);
            } else {
                last_timestamp = Internal.baseTime(DateTime.currentTimeMillis());
            }
            final byte[] key = RowKey.rowKeyFromTSUID(tsdb, tsuid, last_timestamp);
            final GetRequest get = new GetRequest(tsdb.dataTable(), key);
            get.family(TSDB.FAMILY());
            return tsdb.getClient().get(get).addCallbackDeferring(new LastPointCB());
        }

        @Override
        public String toString() {
            return "TSUID callback";
        }
    }
    if (tsuid == null) {
        return tsuidFromMetric(tsdb, metric, tags).addCallbackDeferring(new TSUIDCB());
    }
    try {
        // damn typed exceptions....
        return new TSUIDCB().call(null);
    } catch (Exception e) {
        return Deferred.fromError(e);
    }
}
Also used : Callback(com.stumbleupon.async.Callback) GetRequest(org.hbase.async.GetRequest) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint)

Aggregations

GetRequest (org.hbase.async.GetRequest)28 ArrayList (java.util.ArrayList)23 KeyValue (org.hbase.async.KeyValue)20 Callback (com.stumbleupon.async.Callback)17 List (java.util.List)7 Map (java.util.Map)5 HashMap (java.util.HashMap)4 TreeMap (java.util.TreeMap)4 ByteMap (org.hbase.async.Bytes.ByteMap)4 Test (org.junit.Test)4 Matchers.anyList (org.mockito.Matchers.anyList)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 MultiGetTask (net.opentsdb.core.MultiGetQuery.MultiGetTask)3 HBaseException (org.hbase.async.HBaseException)3 SortedMap (java.util.SortedMap)2 ByteSet (net.opentsdb.utils.ByteSet)2 Deferred (com.stumbleupon.async.Deferred)1