Search in sources :

Example 26 with Callback

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

the class QueryExecutor method execute.

/**
   * Execute the RPC and serialize the response
   * @param query The HTTP query to parse and and return results to
   */
public void execute(final HttpQuery query) {
    http_query = query;
    final QueryStats query_stats = new QueryStats(query.getRemoteAddress(), ts_query, query.getHeaders());
    ts_query.setQueryStats(query_stats);
    /**
     * Sends the serialized results to the caller. This should be the very
     * last callback executed.
     */
    class CompleteCB implements Callback<Object, ChannelBuffer> {

        @Override
        public Object call(final ChannelBuffer cb) throws Exception {
            query.sendReply(cb);
            return null;
        }
    }
    /**
     * After all of the queries have run and we have data (or not) then we
     * need to compile the iterators.
     * This class could probably be improved:
     * First we iterate over the results AND for each result, iterate over
     * the expressions, giving a time synced iterator to each expression that 
     * needs the result set.
     * THEN we iterate over the expressions again and build a DAG to determine
     * if any of the expressions require the output of an expression. If so
     * then we add the expressions to the proper parent and compile them in
     * order.
     * After all of that we're ready to start serializing and iterating
     * over the results.
     */
    class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {

        public Object call(final ArrayList<DataPoints[]> query_results) throws Exception {
            for (int i = 0; i < query_results.size(); i++) {
                final TSSubQuery sub = ts_query.getQueries().get(i);
                Iterator<Entry<String, TSSubQuery>> it = sub_queries.entrySet().iterator();
                while (it.hasNext()) {
                    final Entry<String, TSSubQuery> entry = it.next();
                    if (entry.getValue().equals(sub)) {
                        sub_query_results.put(entry.getKey(), query_results.get(i));
                        for (final ExpressionIterator ei : expressions.values()) {
                            if (ei.getVariableNames().contains(entry.getKey())) {
                                final TimeSyncedIterator tsi = new TimeSyncedIterator(entry.getKey(), sub.getFilterTagKs(), query_results.get(i));
                                final NumericFillPolicy fill = fills.get(entry.getKey());
                                if (fill != null) {
                                    tsi.setFillPolicy(fill);
                                }
                                ei.addResults(entry.getKey(), tsi);
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Added results for " + entry.getKey() + " to " + ei.getId());
                                }
                            }
                        }
                    }
                }
            }
            // handle nested expressions
            final DirectedAcyclicGraph<String, DefaultEdge> graph = new DirectedAcyclicGraph<String, DefaultEdge>(DefaultEdge.class);
            for (final Entry<String, ExpressionIterator> eii : expressions.entrySet()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Expression entry key is %s, value is %s", eii.getKey(), eii.getValue().toString()));
                    LOG.debug(String.format("Time to loop through the variable names " + "for %s", eii.getKey()));
                }
                if (!graph.containsVertex(eii.getKey())) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Adding vertex " + eii.getKey());
                    }
                    graph.addVertex(eii.getKey());
                }
                for (final String var : eii.getValue().getVariableNames()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("var is %s", var));
                    }
                    final ExpressionIterator ei = expressions.get(var);
                    if (ei != null) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(String.format("The expression iterator for %s is %s", var, ei.toString()));
                        }
                        // TODO - really ought to calculate this earlier
                        if (eii.getKey().equals(var)) {
                            throw new IllegalArgumentException("Self referencing expression found: " + eii.getKey());
                        }
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Nested expression detected. " + eii.getKey() + " depends on " + var);
                        }
                        if (!graph.containsVertex(eii.getKey())) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Added vertex " + eii.getKey());
                            }
                            graph.addVertex(eii.getKey());
                        } else if (LOG.isDebugEnabled()) {
                            LOG.debug("Already contains vertex " + eii.getKey());
                        }
                        if (!graph.containsVertex(var)) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Added vertex " + var);
                            }
                            graph.addVertex(var);
                        } else if (LOG.isDebugEnabled()) {
                            LOG.debug("Already contains vertex " + var);
                        }
                        try {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Added Edge " + eii.getKey() + " - " + var);
                            }
                            graph.addDagEdge(eii.getKey(), var);
                        } catch (CycleFoundException cfe) {
                            throw new IllegalArgumentException("Circular reference found: " + eii.getKey(), cfe);
                        }
                    } else if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("The expression iterator for %s is null", var));
                    }
                }
            }
            // compile all of the expressions
            final long intersect_start = DateTime.currentTimeMillis();
            final Integer expressionLength = expressions.size();
            final ExpressionIterator[] compile_stack = new ExpressionIterator[expressionLength];
            final TopologicalOrderIterator<String, DefaultEdge> it = new TopologicalOrderIterator<String, DefaultEdge>(graph);
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("Expressions Size is %d", expressionLength));
                LOG.debug(String.format("Topology Iterator %s", it.toString()));
            }
            int i = 0;
            while (it.hasNext()) {
                String next = it.next();
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Expression: %s", next));
                }
                ExpressionIterator ei = expressions.get(next);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Expression Iterator: %s", ei.toString()));
                }
                if (ei == null) {
                    LOG.error(String.format("The expression iterator for %s is null", next));
                }
                compile_stack[i] = ei;
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Added expression %s to compile_stack[%d]", next, i));
                }
                i++;
            }
            if (i != expressionLength) {
                throw new IOException(String.format(" Internal Error: Fewer " + "expressions where added to the compile stack than " + "expressions.size (%d instead of %d)", i, expressionLength));
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("compile stack length: %d", compile_stack.length));
            }
            for (int x = compile_stack.length - 1; x >= 0; x--) {
                if (compile_stack[x] == null) {
                    throw new NullPointerException(String.format("Item %d in " + "compile_stack[] is null", x));
                }
                // look for and add expressions
                for (final String var : compile_stack[x].getVariableNames()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(String.format("Looking for variable %s for %s", var, compile_stack[x].getId()));
                    }
                    ExpressionIterator source = expressions.get(var);
                    if (source != null) {
                        compile_stack[x].addResults(var, source.getCopy());
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(String.format("Adding expression %s to %s", source.getId(), compile_stack[x].getId()));
                        }
                    }
                }
                compile_stack[x].compile();
                if (LOG.isDebugEnabled()) {
                    LOG.debug(String.format("Successfully compiled %s", compile_stack[x].getId()));
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Finished compilations in " + (DateTime.currentTimeMillis() - intersect_start) + " ms");
            }
            return serialize().addCallback(new CompleteCB()).addErrback(new ErrorCB());
        }
    }
    /**
     * Callback executed after we have resolved the metric, tag names and tag
     * values to their respective UIDs. This callback then runs the actual 
     * queries and fetches their results.
     */
    class BuildCB implements Callback<Deferred<Object>, net.opentsdb.core.Query[]> {

        @Override
        public Deferred<Object> call(final net.opentsdb.core.Query[] queries) {
            final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(queries.length);
            for (final net.opentsdb.core.Query query : queries) {
                deferreds.add(query.runAsync());
            }
            return Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).addErrback(new ErrorCB());
        }
    }
    // TODO - only run the ones that will be involved in an output. Folks WILL
    // ask for stuff they don't need.... *sigh*
    ts_query.buildQueriesAsync(tsdb).addCallback(new BuildCB()).addErrback(new ErrorCB());
}
Also used : ExpressionIterator(net.opentsdb.query.expression.ExpressionIterator) Query(net.opentsdb.query.pojo.Query) TSQuery(net.opentsdb.core.TSQuery) TSSubQuery(net.opentsdb.core.TSSubQuery) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) DataPoints(net.opentsdb.core.DataPoints) TimeSyncedIterator(net.opentsdb.query.expression.TimeSyncedIterator) DirectedAcyclicGraph(org.jgrapht.experimental.dag.DirectedAcyclicGraph) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Entry(java.util.Map.Entry) CycleFoundException(org.jgrapht.experimental.dag.DirectedAcyclicGraph.CycleFoundException) DefaultEdge(org.jgrapht.graph.DefaultEdge) IOException(java.io.IOException) TSSubQuery(net.opentsdb.core.TSSubQuery) DataPoint(net.opentsdb.core.DataPoint) ExpressionDataPoint(net.opentsdb.query.expression.ExpressionDataPoint) Callback(com.stumbleupon.async.Callback) QueryStats(net.opentsdb.stats.QueryStats) NumericFillPolicy(net.opentsdb.query.expression.NumericFillPolicy)

Example 27 with Callback

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

the class QueryRpc method handleQuery.

/**
   * Processing for a data point query
   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query to parse/respond
   * @param allow_expressions Whether or not expressions should be parsed
   * (based on the endpoint)
   */
private void handleQuery(final TSDB tsdb, final HttpQuery query, final boolean allow_expressions) {
    final long start = DateTime.currentTimeMillis();
    final TSQuery data_query;
    final List<ExpressionTree> expressions;
    if (query.method() == HttpMethod.POST) {
        switch(query.apiVersion()) {
            case 0:
            case 1:
                data_query = query.serializer().parseQueryV1();
                break;
            default:
                query_invalid.incrementAndGet();
                throw new BadRequestException(HttpResponseStatus.NOT_IMPLEMENTED, "Requested API version not implemented", "Version " + query.apiVersion() + " is not implemented");
        }
        expressions = null;
    } else {
        expressions = new ArrayList<ExpressionTree>();
        data_query = parseQuery(tsdb, query, expressions);
    }
    if (query.getAPIMethod() == HttpMethod.DELETE && tsdb.getConfig().getBoolean("tsd.http.query.allow_delete")) {
        data_query.setDelete(true);
    }
    // validate and then compile the queries
    try {
        LOG.debug(data_query.toString());
        data_query.validateAndSetQuery();
    } catch (Exception e) {
        throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, e.getMessage(), data_query.toString(), e);
    }
    // if the user tried this query multiple times from the same IP and src port
    // they'll be rejected on subsequent calls
    final QueryStats query_stats = new QueryStats(query.getRemoteAddress(), data_query, query.getPrintableHeaders());
    data_query.setQueryStats(query_stats);
    query.setStats(query_stats);
    final int nqueries = data_query.getQueries().size();
    final ArrayList<DataPoints[]> results = new ArrayList<DataPoints[]>(nqueries);
    final List<Annotation> globals = new ArrayList<Annotation>();
    /** This has to be attached to callbacks or we may never respond to clients */
    class ErrorCB implements Callback<Object, Exception> {

        public Object call(final Exception e) throws Exception {
            Throwable ex = e;
            try {
                LOG.error("Query exception: ", e);
                if (ex instanceof DeferredGroupException) {
                    ex = e.getCause();
                    while (ex != null && ex instanceof DeferredGroupException) {
                        ex = ex.getCause();
                    }
                    if (ex == null) {
                        LOG.error("The deferred group exception didn't have a cause???");
                    }
                }
                if (ex instanceof RpcTimedOutException) {
                    query_stats.markSerialized(HttpResponseStatus.REQUEST_TIMEOUT, ex);
                    query.badRequest(new BadRequestException(HttpResponseStatus.REQUEST_TIMEOUT, ex.getMessage()));
                    query_exceptions.incrementAndGet();
                } else if (ex instanceof HBaseException) {
                    query_stats.markSerialized(HttpResponseStatus.FAILED_DEPENDENCY, ex);
                    query.badRequest(new BadRequestException(HttpResponseStatus.FAILED_DEPENDENCY, ex.getMessage()));
                    query_exceptions.incrementAndGet();
                } else if (ex instanceof QueryException) {
                    query_stats.markSerialized(((QueryException) ex).getStatus(), ex);
                    query.badRequest(new BadRequestException(((QueryException) ex).getStatus(), ex.getMessage()));
                    query_exceptions.incrementAndGet();
                } else if (ex instanceof BadRequestException) {
                    query_stats.markSerialized(((BadRequestException) ex).getStatus(), ex);
                    query.badRequest((BadRequestException) ex);
                    query_invalid.incrementAndGet();
                } else if (ex instanceof NoSuchUniqueName) {
                    query_stats.markSerialized(HttpResponseStatus.BAD_REQUEST, ex);
                    query.badRequest(new BadRequestException(ex));
                    query_invalid.incrementAndGet();
                } else {
                    query_stats.markSerialized(HttpResponseStatus.INTERNAL_SERVER_ERROR, ex);
                    query.badRequest(new BadRequestException(ex));
                    query_exceptions.incrementAndGet();
                }
            } catch (RuntimeException ex2) {
                LOG.error("Exception thrown during exception handling", ex2);
                query_stats.markSerialized(HttpResponseStatus.INTERNAL_SERVER_ERROR, ex2);
                query.sendReply(HttpResponseStatus.INTERNAL_SERVER_ERROR, ex2.getMessage().getBytes());
                query_exceptions.incrementAndGet();
            }
            return null;
        }
    }
    /**
     * After all of the queries have run, we get the results in the order given
     * and add dump the results in an array
     */
    class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {

        public Object call(final ArrayList<DataPoints[]> query_results) throws Exception {
            if (allow_expressions) {
                // process each of the expressions into a new list, then merge it
                // with the original. This avoids possible recursion loops.
                final List<DataPoints[]> expression_results = new ArrayList<DataPoints[]>(expressions.size());
                // let exceptions bubble up
                for (final ExpressionTree expression : expressions) {
                    expression_results.add(expression.evaluate(query_results));
                }
                results.addAll(expression_results);
            } else {
                results.addAll(query_results);
            }
            /** Simply returns the buffer once serialization is complete and logs it */
            class SendIt implements Callback<Object, ChannelBuffer> {

                public Object call(final ChannelBuffer buffer) throws Exception {
                    query.sendReply(buffer);
                    query_success.incrementAndGet();
                    return null;
                }
            }
            switch(query.apiVersion()) {
                case 0:
                case 1:
                    query.serializer().formatQueryAsyncV1(data_query, results, globals).addCallback(new SendIt()).addErrback(new ErrorCB());
                    break;
                default:
                    query_invalid.incrementAndGet();
                    throw new BadRequestException(HttpResponseStatus.NOT_IMPLEMENTED, "Requested API version not implemented", "Version " + query.apiVersion() + " is not implemented");
            }
            return null;
        }
    }
    /**
     * Callback executed after we have resolved the metric, tag names and tag
     * values to their respective UIDs. This callback then runs the actual 
     * queries and fetches their results.
     */
    class BuildCB implements Callback<Deferred<Object>, Query[]> {

        @Override
        public Deferred<Object> call(final Query[] queries) {
            final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(queries.length);
            for (final Query query : queries) {
                deferreds.add(query.runAsync());
            }
            return Deferred.groupInOrder(deferreds).addCallback(new QueriesCB());
        }
    }
    /** Handles storing the global annotations after fetching them */
    class GlobalCB implements Callback<Object, List<Annotation>> {

        public Object call(final List<Annotation> annotations) throws Exception {
            globals.addAll(annotations);
            return data_query.buildQueriesAsync(tsdb).addCallback(new BuildCB());
        }
    }
    // when complete
    if (!data_query.getNoAnnotations() && data_query.getGlobalAnnotations()) {
        Annotation.getGlobalAnnotations(tsdb, data_query.startTime() / 1000, data_query.endTime() / 1000).addCallback(new GlobalCB()).addErrback(new ErrorCB());
    } else {
        data_query.buildQueriesAsync(tsdb).addCallback(new BuildCB()).addErrback(new ErrorCB());
    }
}
Also used : Query(net.opentsdb.core.Query) TSUIDQuery(net.opentsdb.meta.TSUIDQuery) TSQuery(net.opentsdb.core.TSQuery) TSSubQuery(net.opentsdb.core.TSSubQuery) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) DataPoints(net.opentsdb.core.DataPoints) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) TSQuery(net.opentsdb.core.TSQuery) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) ExpressionTree(net.opentsdb.query.expression.ExpressionTree) ArrayList(java.util.ArrayList) List(java.util.List) RpcTimedOutException(org.hbase.async.RpcTimedOutException) QueryException(net.opentsdb.core.QueryException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) IOException(java.io.IOException) RpcTimedOutException(org.hbase.async.RpcTimedOutException) HBaseException(org.hbase.async.HBaseException) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint) Annotation(net.opentsdb.meta.Annotation) QueryException(net.opentsdb.core.QueryException) Callback(com.stumbleupon.async.Callback) QueryStats(net.opentsdb.stats.QueryStats) HBaseException(org.hbase.async.HBaseException) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 28 with Callback

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

the class QueryRpc method handleLastDataPointQuery.

/**
   * Processes a last data point query
   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query to parse/respond
   */
private void handleLastDataPointQuery(final TSDB tsdb, final HttpQuery query) {
    final LastPointQuery data_query;
    if (query.method() == HttpMethod.POST) {
        switch(query.apiVersion()) {
            case 0:
            case 1:
                data_query = query.serializer().parseLastPointQueryV1();
                break;
            default:
                throw new BadRequestException(HttpResponseStatus.NOT_IMPLEMENTED, "Requested API version not implemented", "Version " + query.apiVersion() + " is not implemented");
        }
    } else {
        data_query = this.parseLastPointQuery(tsdb, query);
    }
    if (data_query.sub_queries == null || data_query.sub_queries.isEmpty()) {
        throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, "Missing sub queries");
    }
    // a list of deferreds to wait on
    final ArrayList<Deferred<Object>> calls = new ArrayList<Deferred<Object>>();
    // final results for serialization
    final List<IncomingDataPoint> results = new ArrayList<IncomingDataPoint>();
    /**
     * Used to catch exceptions 
     */
    final class ErrBack implements Callback<Object, Exception> {

        public Object call(final Exception e) throws Exception {
            Throwable ex = e;
            while (ex.getClass().equals(DeferredGroupException.class)) {
                if (ex.getCause() == null) {
                    LOG.warn("Unable to get to the root cause of the DGE");
                    break;
                }
                ex = ex.getCause();
            }
            if (ex instanceof RuntimeException) {
                throw new BadRequestException(ex);
            } else {
                throw e;
            }
        }

        @Override
        public String toString() {
            return "Error back";
        }
    }
    final class FetchCB implements Callback<Deferred<Object>, ArrayList<IncomingDataPoint>> {

        @Override
        public Deferred<Object> call(final ArrayList<IncomingDataPoint> dps) throws Exception {
            synchronized (results) {
                for (final IncomingDataPoint dp : dps) {
                    if (dp != null) {
                        results.add(dp);
                    }
                }
            }
            return Deferred.fromResult(null);
        }

        @Override
        public String toString() {
            return "Fetched data points CB";
        }
    }
    /**
     * Called after scanning the tsdb-meta table for TSUIDs that match the given
     * metric and/or tags. If matches were found, it fires off a number of
     * getLastPoint requests, adding the deferreds to the calls list
     */
    final class TSUIDQueryCB implements Callback<Deferred<Object>, ByteMap<Long>> {

        public Deferred<Object> call(final ByteMap<Long> tsuids) throws Exception {
            if (tsuids == null || tsuids.isEmpty()) {
                return null;
            }
            final ArrayList<Deferred<IncomingDataPoint>> deferreds = new ArrayList<Deferred<IncomingDataPoint>>(tsuids.size());
            for (Map.Entry<byte[], Long> entry : tsuids.entrySet()) {
                deferreds.add(TSUIDQuery.getLastPoint(tsdb, entry.getKey(), data_query.getResolveNames(), data_query.getBackScan(), entry.getValue()));
            }
            return Deferred.group(deferreds).addCallbackDeferring(new FetchCB());
        }

        @Override
        public String toString() {
            return "TSMeta scan CB";
        }
    }
    /**
     * Used to wait on the list of data point deferreds. Once they're all done
     * this will return the results to the call via the serializer
     */
    final class FinalCB implements Callback<Object, ArrayList<Object>> {

        public Object call(final ArrayList<Object> done) throws Exception {
            query.sendReply(query.serializer().formatLastPointQueryV1(results));
            return null;
        }

        @Override
        public String toString() {
            return "Final CB";
        }
    }
    try {
        // start executing the queries
        for (final LastPointSubQuery sub_query : data_query.getQueries()) {
            final ArrayList<Deferred<IncomingDataPoint>> deferreds = new ArrayList<Deferred<IncomingDataPoint>>();
            // process the TSUIDs and ignore the metric/tags
            if (sub_query.getTSUIDs() != null && !sub_query.getTSUIDs().isEmpty()) {
                for (final String tsuid : sub_query.getTSUIDs()) {
                    final TSUIDQuery tsuid_query = new TSUIDQuery(tsdb, UniqueId.stringToUid(tsuid));
                    deferreds.add(tsuid_query.getLastPoint(data_query.getResolveNames(), data_query.getBackScan()));
                }
            } else {
                @SuppressWarnings("unchecked") final TSUIDQuery tsuid_query = new TSUIDQuery(tsdb, sub_query.getMetric(), sub_query.getTags() != null ? sub_query.getTags() : Collections.EMPTY_MAP);
                if (data_query.getBackScan() > 0) {
                    deferreds.add(tsuid_query.getLastPoint(data_query.getResolveNames(), data_query.getBackScan()));
                } else {
                    calls.add(tsuid_query.getLastWriteTimes().addCallbackDeferring(new TSUIDQueryCB()));
                }
            }
            if (deferreds.size() > 0) {
                calls.add(Deferred.group(deferreds).addCallbackDeferring(new FetchCB()));
            }
        }
        Deferred.group(calls).addCallback(new FinalCB()).addErrback(new ErrBack()).joinUninterruptibly();
    } catch (Exception e) {
        Throwable ex = e;
        while (ex.getClass().equals(DeferredGroupException.class)) {
            if (ex.getCause() == null) {
                LOG.warn("Unable to get to the root cause of the DGE");
                break;
            }
            ex = ex.getCause();
        }
        if (ex instanceof RuntimeException) {
            throw new BadRequestException(ex);
        } else {
            throw new RuntimeException("Shouldn't be here", e);
        }
    }
}
Also used : Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) TSUIDQuery(net.opentsdb.meta.TSUIDQuery) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint) QueryException(net.opentsdb.core.QueryException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) IOException(java.io.IOException) RpcTimedOutException(org.hbase.async.RpcTimedOutException) HBaseException(org.hbase.async.HBaseException) Callback(com.stumbleupon.async.Callback) ByteMap(org.hbase.async.Bytes.ByteMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) HashMap(java.util.HashMap) Map(java.util.Map) ByteMap(org.hbase.async.Bytes.ByteMap)

Example 29 with Callback

use of com.stumbleupon.async.Callback 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 30 with Callback

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

the class QueryExample method main.

public static void main(final String[] args) throws IOException {
    // Set these as arguments so you don't have to keep path information in
    // source files 
    String pathToConfigFile = (args != null && args.length > 0 ? args[0] : null);
    // Create a config object with a path to the file for parsing. Or manually
    // override settings.
    // e.g. config.overrideConfig("tsd.storage.hbase.zk_quorum", "localhost");
    final Config config;
    if (pathToConfigFile != null && !pathToConfigFile.isEmpty()) {
        config = new Config(pathToConfigFile);
    } else {
        // Search for a default config from /etc/opentsdb/opentsdb.conf, etc.
        config = new Config(true);
    }
    final TSDB tsdb = new TSDB(config);
    // main query
    final TSQuery query = new TSQuery();
    // use any string format from
    // http://opentsdb.net/docs/build/html/user_guide/query/dates.html
    query.setStart("1h-ago");
    // Optional: set other global query params
    // at least one sub query required. This is where you specify the metric and
    // tags
    final TSSubQuery subQuery = new TSSubQuery();
    subQuery.setMetric("my.tsdb.test.metric");
    // filters are optional but useful.
    final List<TagVFilter> filters = new ArrayList<TagVFilter>(1);
    filters.add(new TagVFilter.Builder().setType("literal_or").setFilter("example1").setTagk("script").setGroupBy(true).build());
    subQuery.setFilters(filters);
    // you do have to set an aggregator. Just provide the name as a string
    subQuery.setAggregator("sum");
    // IMPORTANT: don't forget to add the subQuery
    final ArrayList<TSSubQuery> subQueries = new ArrayList<TSSubQuery>(1);
    subQueries.add(subQuery);
    query.setQueries(subQueries);
    // otherwise we aggregate on the second. 
    query.setMsResolution(true);
    // make sure the query is valid. This will throw exceptions if something
    // is missing
    query.validateAndSetQuery();
    // compile the queries into TsdbQuery objects behind the scenes
    Query[] tsdbqueries = query.buildQueries(tsdb);
    // create some arrays for storing the results and the async calls
    final int nqueries = tsdbqueries.length;
    final ArrayList<DataPoints[]> results = new ArrayList<DataPoints[]>(nqueries);
    final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(nqueries);
    // deferred in an array so we can wait for them to complete.
    for (int i = 0; i < nqueries; i++) {
        deferreds.add(tsdbqueries[i].runAsync());
    }
    // Start timer
    long startTime = DateTime.nanoTime();
    // query has finished
    class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {

        public Object call(final ArrayList<DataPoints[]> queryResults) throws Exception {
            results.addAll(queryResults);
            return null;
        }
    }
    // Make sure to handle any errors that might crop up
    class QueriesEB implements Callback<Object, Exception> {

        @Override
        public Object call(final Exception e) throws Exception {
            System.err.println("Queries failed");
            e.printStackTrace();
            return null;
        }
    }
    // have completed.
    try {
        Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).addErrback(new QueriesEB()).join();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // End timer.
    double elapsedTime = DateTime.msFromNanoDiff(DateTime.nanoTime(), startTime);
    System.out.println("Query returned in: " + elapsedTime + " milliseconds.");
    // results and do any processing necessary.
    for (final DataPoints[] dataSets : results) {
        for (final DataPoints data : dataSets) {
            System.out.print(data.metricName());
            Map<String, String> resolvedTags = data.getTags();
            for (final Map.Entry<String, String> pair : resolvedTags.entrySet()) {
                System.out.print(" " + pair.getKey() + "=" + pair.getValue());
            }
            System.out.print("\n");
            final SeekableView it = data.iterator();
            /*
         * An important point about SeekableView:
         * Because no data is copied during iteration and no new object gets
         * created, the DataPoint returned must not be stored and gets
         * invalidated as soon as next is called on the iterator (actually it
         * doesn't get invalidated but rather its contents changes). If you want
         * to store individual data points, you need to copy the timestamp and
         * value out of each DataPoint into your own data structures.
         * 
         * In the vast majority of cases, the iterator will be used to go once
         * through all the data points, which is why it's not a problem if the
         * iterator acts just as a transient "view". Iterating will be very
         * cheap since no memory allocation is required (except to instantiate
         * the actual iterator at the beginning).
         */
            while (it.hasNext()) {
                final DataPoint dp = it.next();
                System.out.println("  " + dp.timestamp() + " " + (dp.isInteger() ? dp.longValue() : dp.doubleValue()));
            }
            System.out.println("");
        }
    }
    // Gracefully shutdown connection to TSDB
    try {
        tsdb.shutdown().join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Query(net.opentsdb.core.Query) TSQuery(net.opentsdb.core.TSQuery) TSSubQuery(net.opentsdb.core.TSSubQuery) Config(net.opentsdb.utils.Config) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) DataPoints(net.opentsdb.core.DataPoints) TSQuery(net.opentsdb.core.TSQuery) TagVFilter(net.opentsdb.query.filter.TagVFilter) DataPoint(net.opentsdb.core.DataPoint) TSDB(net.opentsdb.core.TSDB) SeekableView(net.opentsdb.core.SeekableView) TSSubQuery(net.opentsdb.core.TSSubQuery) DataPoint(net.opentsdb.core.DataPoint) IOException(java.io.IOException) Callback(com.stumbleupon.async.Callback) Map(java.util.Map)

Aggregations

Callback (com.stumbleupon.async.Callback)59 ArrayList (java.util.ArrayList)47 KeyValue (org.hbase.async.KeyValue)30 Deferred (com.stumbleupon.async.Deferred)29 GetRequest (org.hbase.async.GetRequest)17 HashMap (java.util.HashMap)15 Map (java.util.Map)15 IOException (java.io.IOException)13 Scanner (org.hbase.async.Scanner)12 PutRequest (org.hbase.async.PutRequest)11 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)9 List (java.util.List)9 HBaseException (org.hbase.async.HBaseException)9 TreeMap (java.util.TreeMap)8 DeleteRequest (org.hbase.async.DeleteRequest)7 IncomingDataPoint (net.opentsdb.core.IncomingDataPoint)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