Search in sources :

Example 1 with NoSuchUniqueId

use of net.opentsdb.uid.NoSuchUniqueId in project opentsdb by OpenTSDB.

the class UidManager method extactLookupId.

/**
   * Looks up an ID for a given kind, and prints it if found.
   * @param client The HBase client to use.
   * @param table The name of the HBase table to use.
   * @param idwidth Number of bytes on which the UIDs should be.
   * @param kind The 'kind' of the ID (must not be {@code null}).
   * @param id The ID to look for.
   * @return 0 if the ID for this kind was found, 1 otherwise.
   */
private static int extactLookupId(final HBaseClient client, final byte[] table, final short idwidth, final String kind, final byte[] id) {
    final UniqueId uid = new UniqueId(client, table, kind, (int) idwidth);
    try {
        final String name = uid.getName(id);
        System.out.println(kind + ' ' + name + ": " + Arrays.toString(id));
        return 0;
    } catch (NoSuchUniqueId e) {
        LOG.error(e.getMessage());
        return 1;
    }
}
Also used : UniqueId(net.opentsdb.uid.UniqueId) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId)

Example 2 with NoSuchUniqueId

use of net.opentsdb.uid.NoSuchUniqueId in project opentsdb by OpenTSDB.

the class TimeSeriesLookup method lookupAsync.

/**
   * Lookup time series associated with the given metric, tagk, tagv or tag 
   * pairs. Either the meta table or the data table will be scanned. If no
   * metric is given, a full table scan must be performed and this call may take
   * a long time to complete. 
   * When dumping to stdout, if an ID can't be looked up, it will be logged and
   * skipped.
   * @return A list of TSUIDs matching the given lookup query.
   * @throws NoSuchUniqueName if any of the given names fail to resolve to a 
   * UID.
   * @since 2.2
   */
public Deferred<List<byte[]>> lookupAsync() {
    final Pattern tagv_regex = tagv_filter != null ? Pattern.compile(tagv_filter) : null;
    // we don't really know what size the UIDs will resolve to so just grab
    // a decent amount.
    final StringBuffer buf = to_stdout ? new StringBuffer(2048) : null;
    final long start = System.currentTimeMillis();
    final int limit;
    if (query.getLimit() > 0) {
        if (query.useMeta() || Const.SALT_WIDTH() < 1) {
            limit = query.getLimit();
        } else if (query.getLimit() < Const.SALT_BUCKETS()) {
            limit = 1;
        } else {
            limit = query.getLimit() / Const.SALT_BUCKETS();
        }
    } else {
        limit = 0;
    }
    class ScannerCB implements Callback<Deferred<List<byte[]>>, ArrayList<ArrayList<KeyValue>>> {

        private final Scanner scanner;

        // used to avoid dupes when scanning the data table
        private byte[] last_tsuid = null;

        private int rows_read;

        ScannerCB(final Scanner scanner) {
            this.scanner = scanner;
        }

        Deferred<List<byte[]>> scan() {
            return scanner.nextRows().addCallbackDeferring(this);
        }

        @Override
        public Deferred<List<byte[]>> call(final ArrayList<ArrayList<KeyValue>> rows) throws Exception {
            if (rows == null) {
                scanner.close();
                if (query.useMeta() || Const.SALT_WIDTH() < 1) {
                    LOG.debug("Lookup query matched " + tsuids.size() + " time series in " + (System.currentTimeMillis() - start) + " ms");
                }
                return Deferred.fromResult(tsuids);
            }
            for (final ArrayList<KeyValue> row : rows) {
                if (limit > 0 && rows_read >= limit) {
                    // little recursion to close the scanner and log above.
                    return call(null);
                }
                final byte[] tsuid = query.useMeta() ? row.get(0).key() : UniqueId.getTSUIDFromKey(row.get(0).key(), TSDB.metrics_width(), Const.TIMESTAMP_BYTES);
                // string objects.
                if (tagv_regex != null && !tagv_regex.matcher(new String(tsuid, CHARSET)).find()) {
                    continue;
                }
                if (to_stdout) {
                    if (last_tsuid != null && Bytes.memcmp(last_tsuid, tsuid) == 0) {
                        continue;
                    }
                    last_tsuid = tsuid;
                    try {
                        buf.append(UniqueId.uidToString(tsuid)).append(" ");
                        buf.append(RowKey.metricNameAsync(tsdb, tsuid).joinUninterruptibly());
                        buf.append(" ");
                        final List<byte[]> tag_ids = UniqueId.getTagPairsFromTSUID(tsuid);
                        final Map<String, String> resolved_tags = Tags.resolveIdsAsync(tsdb, tag_ids).joinUninterruptibly();
                        for (final Map.Entry<String, String> tag_pair : resolved_tags.entrySet()) {
                            buf.append(tag_pair.getKey()).append("=").append(tag_pair.getValue()).append(" ");
                        }
                    } catch (NoSuchUniqueId nsui) {
                        LOG.error("Unable to resolve UID in TSUID (" + UniqueId.uidToString(tsuid) + ") " + nsui.getMessage());
                    }
                    // reset the buffer so we can re-use it
                    buf.setLength(0);
                } else {
                    tsuids.add(tsuid);
                }
                ++rows_read;
            }
            return scan();
        }

        @Override
        public String toString() {
            return "Scanner callback";
        }
    }
    class CompleteCB implements Callback<List<byte[]>, ArrayList<List<byte[]>>> {

        @Override
        public List<byte[]> call(final ArrayList<List<byte[]>> unused) throws Exception {
            LOG.debug("Lookup query matched " + tsuids.size() + " time series in " + (System.currentTimeMillis() - start) + " ms");
            return tsuids;
        }

        @Override
        public String toString() {
            return "Final async lookup callback";
        }
    }
    class UIDCB implements Callback<Deferred<List<byte[]>>, Object> {

        @Override
        public Deferred<List<byte[]>> call(Object arg0) throws Exception {
            if (!query.useMeta() && Const.SALT_WIDTH() > 0 && metric_uid != null) {
                final ArrayList<Deferred<List<byte[]>>> deferreds = new ArrayList<Deferred<List<byte[]>>>(Const.SALT_BUCKETS());
                for (int i = 0; i < Const.SALT_BUCKETS(); i++) {
                    deferreds.add(new ScannerCB(getScanner(i)).scan());
                }
                return Deferred.group(deferreds).addCallback(new CompleteCB());
            } else {
                return new ScannerCB(getScanner(0)).scan();
            }
        }

        @Override
        public String toString() {
            return "UID resolution callback";
        }
    }
    return resolveUIDs().addCallbackDeferring(new UIDCB());
}
Also used : Pattern(java.util.regex.Pattern) Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) Callback(com.stumbleupon.async.Callback) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with NoSuchUniqueId

use of net.opentsdb.uid.NoSuchUniqueId 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)

Example 4 with NoSuchUniqueId

use of net.opentsdb.uid.NoSuchUniqueId in project opentsdb by OpenTSDB.

the class BaseTsdbTest method setupTagvMaps.

/** Adds the static UIDs to the tag values UID mock object */
void setupTagvMaps() {
    when(tag_values.getId(TAGV_STRING)).thenReturn(TAGV_BYTES);
    when(tag_values.getOrCreateId(TAGV_STRING)).thenReturn(TAGV_BYTES);
    when(tag_values.getIdAsync(TAGV_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(TAGV_BYTES);
        }
    });
    when(tag_values.getOrCreateIdAsync(TAGV_STRING)).thenReturn(Deferred.fromResult(TAGV_BYTES));
    when(tag_values.getId(TAGV_B_STRING)).thenReturn(TAGV_B_BYTES);
    when(tag_values.getOrCreateId(TAGV_B_STRING)).thenReturn(TAGV_B_BYTES);
    when(tag_values.getIdAsync(TAGV_B_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(TAGV_B_BYTES);
        }
    });
    when(tag_values.getOrCreateIdAsync(TAGV_B_STRING)).thenReturn(Deferred.fromResult(TAGV_B_BYTES));
    when(tag_values.getNameAsync(TAGV_BYTES)).thenReturn(Deferred.fromResult(TAGV_STRING));
    when(tag_values.getNameAsync(TAGV_B_BYTES)).thenReturn(Deferred.fromResult(TAGV_B_STRING));
    when(tag_values.getNameAsync(NSUI_TAGV)).thenThrow(new NoSuchUniqueId("tagv", NSUI_TAGV));
    final NoSuchUniqueName nsun = new NoSuchUniqueName(NSUN_TAGV, "tagv");
    when(tag_values.getId(NSUN_TAGV)).thenThrow(nsun);
    when(tag_values.getIdAsync(NSUN_TAGV)).thenReturn(Deferred.<byte[]>fromError(nsun));
    // Iterate over the tagv UIDs and handle both forward and reverse
    for (final Map.Entry<String, byte[]> uid : TAGV_UIDS.entrySet()) {
        when(tag_values.getId(uid.getKey())).thenReturn(uid.getValue());
        when(tag_values.getIdAsync(uid.getKey())).thenAnswer(new Answer<Deferred<byte[]>>() {

            @Override
            public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
                return Deferred.fromResult(uid.getValue());
            }
        });
        when(tag_values.getOrCreateId(uid.getKey())).thenReturn(uid.getValue());
        when(tag_values.getNameAsync(uid.getValue())).thenAnswer(new Answer<Deferred<String>>() {

            @Override
            public Deferred<String> answer(InvocationOnMock invocation) throws Throwable {
                return Deferred.fromResult(uid.getKey());
            }
        });
    }
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) Deferred(com.stumbleupon.async.Deferred) HashMap(java.util.HashMap) Map(java.util.Map) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 5 with NoSuchUniqueId

use of net.opentsdb.uid.NoSuchUniqueId in project opentsdb by OpenTSDB.

the class BaseTsdbTest method setupMetricMaps.

/** Adds the static UIDs to the metrics UID mock object */
void setupMetricMaps() {
    when(metrics.getId(METRIC_STRING)).thenReturn(METRIC_BYTES);
    when(metrics.getIdAsync(METRIC_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(METRIC_BYTES);
        }
    });
    when(metrics.getOrCreateId(METRIC_STRING)).thenReturn(METRIC_BYTES);
    when(metrics.getId(METRIC_B_STRING)).thenReturn(METRIC_B_BYTES);
    when(metrics.getIdAsync(METRIC_B_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(METRIC_B_BYTES);
        }
    });
    when(metrics.getOrCreateId(METRIC_B_STRING)).thenReturn(METRIC_B_BYTES);
    when(metrics.getNameAsync(METRIC_BYTES)).thenAnswer(new Answer<Deferred<String>>() {

        @Override
        public Deferred<String> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(METRIC_STRING);
        }
    });
    when(metrics.getNameAsync(METRIC_B_BYTES)).thenAnswer(new Answer<Deferred<String>>() {

        @Override
        public Deferred<String> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(METRIC_B_STRING);
        }
    });
    when(metrics.getNameAsync(NSUI_METRIC)).thenThrow(new NoSuchUniqueId("metrics", NSUI_METRIC));
    final NoSuchUniqueName nsun = new NoSuchUniqueName(NSUN_METRIC, "metrics");
    when(metrics.getId(NSUN_METRIC)).thenThrow(nsun);
    when(metrics.getIdAsync(NSUN_METRIC)).thenReturn(Deferred.<byte[]>fromError(nsun));
    when(metrics.getOrCreateId(NSUN_METRIC)).thenThrow(nsun);
    // Iterate over the metric UIDs and handle both forward and reverse
    for (final Map.Entry<String, byte[]> uid : METRIC_UIDS.entrySet()) {
        when(metrics.getId(uid.getKey())).thenReturn(uid.getValue());
        when(metrics.getIdAsync(uid.getKey())).thenAnswer(new Answer<Deferred<byte[]>>() {

            @Override
            public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
                return Deferred.fromResult(uid.getValue());
            }
        });
        when(metrics.getOrCreateId(uid.getKey())).thenReturn(uid.getValue());
        when(metrics.getNameAsync(uid.getValue())).thenAnswer(new Answer<Deferred<String>>() {

            @Override
            public Deferred<String> answer(InvocationOnMock invocation) throws Throwable {
                return Deferred.fromResult(uid.getKey());
            }
        });
    }
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) Deferred(com.stumbleupon.async.Deferred) HashMap(java.util.HashMap) Map(java.util.Map) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Aggregations

NoSuchUniqueId (net.opentsdb.uid.NoSuchUniqueId)17 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Deferred (com.stumbleupon.async.Deferred)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)5 HashMap (java.util.HashMap)4 NoSuchUniqueName (net.opentsdb.uid.NoSuchUniqueName)4 DataPoints (net.opentsdb.core.DataPoints)3 TSQuery (net.opentsdb.core.TSQuery)3 Annotation (net.opentsdb.meta.Annotation)3 MockDataPoints (net.opentsdb.storage.MockDataPoints)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Callback (com.stumbleupon.async.Callback)2 List (java.util.List)2 Matchers.anyString (org.mockito.Matchers.anyString)2 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Pattern (java.util.regex.Pattern)1