Search in sources :

Example 21 with IncomingDataPoint

use of net.opentsdb.core.IncomingDataPoint in project opentsdb by OpenTSDB.

the class RollupDataPointRpc method getDataPointFromString.

/**
 * Converts the string array to an IncomingDataPoint. WARNING: This method
 * does not perform validation. It should only be used by the Telnet style
 * {@code execute} above within the error callback. At that point it means
 * the array parsed correctly as per {@code importDataPoint}.
 * @param tsdb The TSDB for encoding/decoding.
 * @param words The array of strings representing a data point
 * @return An incoming data point object.
 */
@Override
protected IncomingDataPoint getDataPointFromString(final TSDB tsdb, final String[] words) {
    final RollUpDataPoint dp = new RollUpDataPoint();
    final String interval_agg = words[TelnetIndex.INTERVAL_AGG.ordinal()];
    String interval = null;
    String temporal_agg = null;
    String spatial_agg = null;
    // if the interval_agg has a - in it, then it's an interval. If there's a :
    // then it is both. If no dash or colon then it's just a spatial agg.
    final String[] interval_parts = interval_agg.split(":");
    final int dash = interval_parts[0].indexOf("-");
    if (dash > -1) {
        interval = interval_parts[0].substring(0, dash);
        temporal_agg = interval_parts[0].substring(dash + 1);
    } else if (interval_parts.length == 1) {
        spatial_agg = interval_parts[0];
    }
    if (interval_parts.length > 1) {
        spatial_agg = interval_parts[1];
    }
    dp.setInterval(interval);
    dp.setAggregator(temporal_agg);
    dp.setGroupByAggregator(spatial_agg);
    dp.setMetric(words[TelnetIndex.METRIC.ordinal()]);
    if (words[TelnetIndex.TIMESTAMP.ordinal()].contains(".")) {
        dp.setTimestamp(Tags.parseLong(words[TelnetIndex.TIMESTAMP.ordinal()].replace(".", "")));
    } else {
        dp.setTimestamp(Tags.parseLong(words[TelnetIndex.TIMESTAMP.ordinal()]));
    }
    dp.setValue(words[TelnetIndex.VALUE.ordinal()]);
    final HashMap<String, String> tags = new HashMap<String, String>();
    for (int i = TelnetIndex.TAGS.ordinal(); i < words.length; i++) {
        if (!words[i].isEmpty()) {
            Tags.parse(tags, words[i]);
        }
    }
    dp.setTags(tags);
    return dp;
}
Also used : RollUpDataPoint(net.opentsdb.rollup.RollUpDataPoint) HashMap(java.util.HashMap) RollUpDataPoint(net.opentsdb.rollup.RollUpDataPoint) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint)

Example 22 with IncomingDataPoint

use of net.opentsdb.core.IncomingDataPoint 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

IncomingDataPoint (net.opentsdb.core.IncomingDataPoint)22 BaseTsdbTest (net.opentsdb.core.BaseTsdbTest)12 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 HashMap (java.util.HashMap)7 ArrayList (java.util.ArrayList)5 RollUpDataPoint (net.opentsdb.rollup.RollUpDataPoint)5 Callback (com.stumbleupon.async.Callback)4 Deferred (com.stumbleupon.async.Deferred)3 IOException (java.io.IOException)3 TimeoutException (com.stumbleupon.async.TimeoutException)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DataPoint (net.opentsdb.core.DataPoint)2 HistogramPojo (net.opentsdb.core.HistogramPojo)2 NoSuchUniqueName (net.opentsdb.uid.NoSuchUniqueName)2 HBaseException (org.hbase.async.HBaseException)2 Timeout (org.jboss.netty.util.Timeout)2 TimerTask (org.jboss.netty.util.TimerTask)2 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1