Search in sources :

Example 61 with Callback

use of com.stumbleupon.async.Callback in project opentsdb by OpenTSDB.

the class TSUIDQuery method getLastWriteTimes.

/**
 * Fetches a list of TSUIDs given the metric and optional tag pairs. The query
 * format is similar to TsdbQuery but doesn't support grouping operators for
 * tags. Only TSUIDs that had "ts_counter" qualifiers will be returned.
 * <p>
 * NOTE: If you called {@link #setQuery(String, Map)} successfully this will
 * immediately scan the meta table. But if you used the CTOR to set the
 * metric and tags it will attempt to resolve those and may return an exception.
 * @return A map of TSUIDs to the last timestamp (in milliseconds) when the
 * "ts_counter" was updated. Note that the timestamp will be the time stored
 * by HBase, not the actual timestamp of the data point. If nothing was
 * found, the map will be empty but not null.
 * @throws IllegalArgumentException if the metric was not set or the tag map
 * was null
 */
public Deferred<ByteMap<Long>> getLastWriteTimes() {
    class ResolutionCB implements Callback<Deferred<ByteMap<Long>>, Object> {

        @Override
        public Deferred<ByteMap<Long>> call(Object arg0) throws Exception {
            final Scanner scanner = getScanner();
            scanner.setQualifier(TSMeta.COUNTER_QUALIFIER());
            final Deferred<ByteMap<Long>> results = new Deferred<ByteMap<Long>>();
            final ByteMap<Long> tsuids = new ByteMap<Long>();
            final class ErrBack implements Callback<Object, Exception> {

                @Override
                public Object call(final Exception e) throws Exception {
                    results.callback(e);
                    return null;
                }

                @Override
                public String toString() {
                    return "Error callback";
                }
            }
            /**
             * Scanner callback that will call itself while iterating through the
             * tsdb-meta table
             */
            final class ScannerCB implements Callback<Object, ArrayList<ArrayList<KeyValue>>> {

                /**
                 * Starts the scanner and is called recursively to fetch the next set of
                 * rows from the scanner.
                 * @return The map of spans if loaded successfully, null if no data was
                 * found
                 */
                public Object scan() {
                    return scanner.nextRows().addCallback(this).addErrback(new ErrBack());
                }

                /**
                 * Loops through each row of the scanner results and parses out data
                 * points and optional meta data
                 * @return null if no rows were found, otherwise the TreeMap with spans
                 */
                @Override
                public Object call(final ArrayList<ArrayList<KeyValue>> rows) throws Exception {
                    try {
                        if (rows == null) {
                            results.callback(tsuids);
                            return null;
                        }
                        for (final ArrayList<KeyValue> row : rows) {
                            final byte[] tsuid = row.get(0).key();
                            tsuids.put(tsuid, row.get(0).timestamp());
                        }
                        return scan();
                    } catch (Exception e) {
                        results.callback(e);
                        return null;
                    }
                }
            }
            new ScannerCB().scan();
            return results;
        }

        @Override
        public String toString() {
            return "Last counter time callback";
        }
    }
    if (metric_uid == null) {
        return resolveMetric().addCallbackDeferring(new ResolutionCB());
    }
    try {
        return new ResolutionCB().call(null);
    } catch (Exception e) {
        return Deferred.fromError(e);
    }
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) ByteMap(org.hbase.async.Bytes.ByteMap) Callback(com.stumbleupon.async.Callback)

Example 62 with Callback

use of com.stumbleupon.async.Callback in project opentsdb by OpenTSDB.

the class EDPtoDPS method getAggregatedTagsAsync.

@Override
public Deferred<List<String>> getAggregatedTagsAsync() {
    final ByteSet tagks = edps[index].aggregatedTags();
    final List<String> aggregated_tags = new ArrayList<String>(tagks.size());
    final List<Deferred<String>> names = new ArrayList<Deferred<String>>(tagks.size());
    for (final byte[] tagk : tagks) {
        names.add(tsdb.getUidName(UniqueIdType.TAGK, tagk));
    }
    /**
     * Adds the names to the aggregated_tags list
     */
    final class ResolveCB implements Callback<List<String>, ArrayList<String>> {

        @Override
        public List<String> call(final ArrayList<String> names) throws Exception {
            for (final String name : names) {
                aggregated_tags.add(name);
            }
            return aggregated_tags;
        }
    }
    return Deferred.group(names).addCallback(new ResolveCB());
}
Also used : Callback(com.stumbleupon.async.Callback) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) ByteSet(net.opentsdb.utils.ByteSet)

Aggregations

Callback (com.stumbleupon.async.Callback)62 ArrayList (java.util.ArrayList)48 Deferred (com.stumbleupon.async.Deferred)30 KeyValue (org.hbase.async.KeyValue)29 GetRequest (org.hbase.async.GetRequest)17 HashMap (java.util.HashMap)16 IOException (java.io.IOException)15 Map (java.util.Map)15 PutRequest (org.hbase.async.PutRequest)13 HBaseException (org.hbase.async.HBaseException)11 Scanner (org.hbase.async.Scanner)11 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)10 List (java.util.List)10 TreeMap (java.util.TreeMap)7 IncomingDataPoint (net.opentsdb.core.IncomingDataPoint)7 DeleteRequest (org.hbase.async.DeleteRequest)6 DataPoints (net.opentsdb.core.DataPoints)5 TSSubQuery (net.opentsdb.core.TSSubQuery)5 TSQuery (net.opentsdb.core.TSQuery)4 Annotation (net.opentsdb.meta.Annotation)4