Search in sources :

Example 1 with TimeSeriesLookup

use of net.opentsdb.search.TimeSeriesLookup in project opentsdb by OpenTSDB.

the class Search method lookup.

/**
   * Performs a time series lookup given a query like "metric tagk=tagv" where
   * a list of all time series containing the given metric and tag pair will be
   * dumped to standard out. Tag pairs can be given with empty tagk or tagvs to
   * and the metric is option. E.g. a query of "=web01" will return all time 
   * series with a tag value of "web01".
   * By default the lookup is performed against the tsdb-meta table. If the 
   * "--use_data_table" flag is supplied, the main data table will be scanned.
   * @param tsdb The TSDB to use for communication
   * @param use_data_table Whether or not lookups should be done on the full
   * data table
   * @param args Arguments to parse
   * @return An exit code
   */
private static int lookup(final TSDB tsdb, final boolean use_data_table, final String[] args) throws Exception {
    if (!use_data_table) {
        tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.meta_table")).joinUninterruptibly();
    }
    final SearchQuery query = new SearchQuery();
    query.setType(SearchType.LOOKUP);
    int index = 1;
    if (!args[index].contains("=")) {
        query.setMetric(args[index++]);
    }
    final List<Pair<String, String>> tags = new ArrayList<Pair<String, String>>(args.length - index);
    for (; index < args.length; index++) {
        Tags.parse(tags, args[index]);
    }
    query.setTags(tags);
    if (use_data_table) {
        query.setUseMeta(false);
        LOG.warn("NOTE: Scanning the full data table may take a long time");
    }
    final TimeSeriesLookup lookup = new TimeSeriesLookup(tsdb, query);
    lookup.setToStdout(true);
    lookup.lookup();
    return 0;
}
Also used : SearchQuery(net.opentsdb.search.SearchQuery) TimeSeriesLookup(net.opentsdb.search.TimeSeriesLookup) ArrayList(java.util.ArrayList) Pair(net.opentsdb.utils.Pair)

Example 2 with TimeSeriesLookup

use of net.opentsdb.search.TimeSeriesLookup in project opentsdb by OpenTSDB.

the class SearchRpc method processLookup.

/**
   * Processes a lookup query against the tsdb-meta table, returning (and 
   * resolving) the TSUIDs of any series that matched the query.
   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query to work with
   * @param search_query A search query configured with at least a metric
   * or a list of tag pairs. If neither are set, the method will throw an error.
   * @throws BadRequestException if the metric and tags are null or empty or
   * a UID fails to resolve.
   * @since 2.1
   */
private void processLookup(final TSDB tsdb, final HttpQuery query, final SearchQuery search_query) {
    if (search_query.getMetric() == null && (search_query.getTags() == null || search_query.getTags().size() < 1)) {
        throw new BadRequestException("Missing metric and tags. Please supply at least one value.");
    }
    final long start = System.currentTimeMillis();
    class MetricCB implements Callback<Object, String> {

        final Map<String, Object> series;

        MetricCB(final Map<String, Object> series) {
            this.series = series;
        }

        @Override
        public Object call(final String name) throws Exception {
            series.put("metric", name);
            return null;
        }
    }
    class TagsCB implements Callback<Object, HashMap<String, String>> {

        final Map<String, Object> series;

        TagsCB(final Map<String, Object> series) {
            this.series = series;
        }

        @Override
        public Object call(final HashMap<String, String> names) throws Exception {
            series.put("tags", names);
            return null;
        }
    }
    class Serialize implements Callback<Object, ArrayList<Object>> {

        final List<Object> results;

        Serialize(final List<Object> results) {
            this.results = results;
        }

        @Override
        public Object call(final ArrayList<Object> ignored) throws Exception {
            search_query.setResults(results);
            search_query.setTime(System.currentTimeMillis() - start);
            query.sendReply(query.serializer().formatSearchResultsV1(search_query));
            return null;
        }
    }
    class LookupCB implements Callback<Deferred<Object>, List<byte[]>> {

        @Override
        public Deferred<Object> call(final List<byte[]> tsuids) throws Exception {
            final List<Object> results = new ArrayList<Object>(tsuids.size());
            search_query.setTotalResults(tsuids.size());
            final ArrayList<Deferred<Object>> deferreds = new ArrayList<Deferred<Object>>(tsuids.size());
            for (final byte[] tsuid : tsuids) {
                // has to be concurrent if the uid table is split across servers
                final Map<String, Object> series = new ConcurrentHashMap<String, Object>(3);
                results.add(series);
                series.put("tsuid", UniqueId.uidToString(tsuid));
                byte[] metric_uid = Arrays.copyOfRange(tsuid, 0, TSDB.metrics_width());
                deferreds.add(tsdb.getUidName(UniqueIdType.METRIC, metric_uid).addCallback(new MetricCB(series)));
                final List<byte[]> tag_ids = UniqueId.getTagPairsFromTSUID(tsuid);
                deferreds.add(Tags.resolveIdsAsync(tsdb, tag_ids).addCallback(new TagsCB(series)));
            }
            return Deferred.group(deferreds).addCallback(new Serialize(results));
        }
    }
    class ErrCB implements Callback<Object, Exception> {

        @Override
        public Object call(final Exception e) throws Exception {
            if (e instanceof NoSuchUniqueId) {
                query.sendReply(HttpResponseStatus.NOT_FOUND, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to resolve one or more TSUIDs", (NoSuchUniqueId) e)));
            } else if (e instanceof NoSuchUniqueName) {
                query.sendReply(HttpResponseStatus.NOT_FOUND, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to resolve one or more UIDs", (NoSuchUniqueName) e)));
            } else if (e instanceof DeferredGroupException) {
                final Throwable ex = Exceptions.getCause((DeferredGroupException) e);
                if (ex instanceof NoSuchUniqueId) {
                    query.sendReply(HttpResponseStatus.NOT_FOUND, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to resolve one or more TSUIDs", (NoSuchUniqueId) ex)));
                } else if (ex instanceof NoSuchUniqueName) {
                    query.sendReply(HttpResponseStatus.NOT_FOUND, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to resolve one or more UIDs", (NoSuchUniqueName) ex)));
                } else {
                    query.sendReply(HttpResponseStatus.INTERNAL_SERVER_ERROR, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Unexpected exception", ex)));
                }
            } else {
                query.sendReply(HttpResponseStatus.INTERNAL_SERVER_ERROR, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Unexpected exception", e)));
            }
            return null;
        }
    }
    new TimeSeriesLookup(tsdb, search_query).lookupAsync().addCallback(new LookupCB()).addErrback(new ErrCB());
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TimeSeriesLookup(net.opentsdb.search.TimeSeriesLookup) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) Callback(com.stumbleupon.async.Callback) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Aggregations

ArrayList (java.util.ArrayList)2 TimeSeriesLookup (net.opentsdb.search.TimeSeriesLookup)2 Callback (com.stumbleupon.async.Callback)1 Deferred (com.stumbleupon.async.Deferred)1 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SearchQuery (net.opentsdb.search.SearchQuery)1 NoSuchUniqueId (net.opentsdb.uid.NoSuchUniqueId)1 NoSuchUniqueName (net.opentsdb.uid.NoSuchUniqueName)1 Pair (net.opentsdb.utils.Pair)1