Search in sources :

Example 1 with NoSuchUniqueName

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

the class TestPutRpc method before.

@Before
public void before() throws Exception {
    tsdb = NettyMocks.getMockedHTTPTSDB();
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, 42, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, -42, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, 42.2f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, -42.2f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, 4220.0f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, -4220.0f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, .0042f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.nice", 1365465600, -0.0042f, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    when(tsdb.addPoint("sys.cpu.system", 1365465600, 24, TAGS)).thenReturn(Deferred.fromResult(new Object()));
    // errors
    when(tsdb.addPoint("doesnotexist", 1365465600, 42, TAGS)).thenThrow(new NoSuchUniqueName("metric", "doesnotexist"));
    when(tsdb.addPoint("sys.cpu.system", 1365465600, 1, TAGS)).thenReturn(Deferred.fromError(new RuntimeException("Wotcher!")));
    when(tsdb.addPoint("sys.cpu.system", 1365465600, 2, TAGS)).thenReturn(new Deferred<Object>());
    requests = Whitebox.getInternalState(PutDataPointRpc.class, "requests");
    requests.set(0);
    hbase_errors = Whitebox.getInternalState(PutDataPointRpc.class, "hbase_errors");
    hbase_errors.set(0);
    invalid_values = Whitebox.getInternalState(PutDataPointRpc.class, "invalid_values");
    invalid_values.set(0);
    illegal_arguments = Whitebox.getInternalState(PutDataPointRpc.class, "illegal_arguments");
    illegal_arguments.set(0);
    unknown_metrics = Whitebox.getInternalState(PutDataPointRpc.class, "unknown_metrics");
    unknown_metrics.set(0);
    writes_blocked = Whitebox.getInternalState(PutDataPointRpc.class, "writes_blocked");
    writes_blocked.set(0);
    timer = new FakeTaskTimer();
    handler = mock(StorageExceptionHandler.class);
    when(tsdb.getStorageExceptionHandler()).thenReturn(handler);
    when(tsdb.getTimer()).thenReturn(timer);
}
Also used : FakeTaskTimer(net.opentsdb.core.BaseTsdbTest.FakeTaskTimer) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName) Before(org.junit.Before)

Example 2 with NoSuchUniqueName

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

the class AddDataExample method main.

public static void main(final String[] args) throws Exception {
    processArgs(args);
    // 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);
    // Declare new metric
    String metricName = "my.tsdb.test.metric";
    // First check to see it doesn't already exist
    // we don't actually need this for the first
    byte[] byteMetricUID;
    // addPoint().
    try {
        byteMetricUID = tsdb.getUID(UniqueIdType.METRIC, metricName);
    } catch (IllegalArgumentException iae) {
        System.out.println("Metric name not valid.");
        iae.printStackTrace();
        System.exit(1);
    } catch (NoSuchUniqueName nsune) {
        // If not, great. Create it.
        byteMetricUID = tsdb.assignUid("metric", metricName);
    }
    // Make a single datum
    long timestamp = System.currentTimeMillis() / 1000;
    long value = 314159;
    // Make key-val
    Map<String, String> tags = new HashMap<String, String>(1);
    tags.put("script", "example1");
    // Start timer
    long startTime1 = System.currentTimeMillis();
    // Write a number of data points at 30 second intervals. Each write will
    // return a deferred (similar to a Java Future or JS Promise) that will
    // be called on completion with either a "null" value on success or an
    // exception.
    int n = 100;
    ArrayList<Deferred<Object>> deferreds = new ArrayList<Deferred<Object>>(n);
    for (int i = 0; i < n; i++) {
        Deferred<Object> deferred = tsdb.addPoint(metricName, timestamp, value + i, tags);
        deferreds.add(deferred);
        timestamp += 30;
    }
    // Add the callbacks to the deferred object. (They might have already
    // returned, btw)
    // This will cause the calling thread to wait until the add has completed.
    System.out.println("Waiting for deferred result to return...");
    Deferred.groupInOrder(deferreds).addErrback(new AddDataExample().new errBack()).addCallback(new AddDataExample().new succBack()).join();
    // Alternatively you can add another callback here or use a join with a
    // timeout argument.
    // End timer.
    long elapsedTime1 = System.currentTimeMillis() - startTime1;
    System.out.println("\nAdding " + n + " points took: " + elapsedTime1 + " milliseconds.\n");
    // Gracefully shutdown connection to TSDB. This is CRITICAL as it will
    // flush any pending operations to HBase.
    tsdb.shutdown().join();
}
Also used : HashMap(java.util.HashMap) Config(net.opentsdb.utils.Config) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) TSDB(net.opentsdb.core.TSDB) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 3 with NoSuchUniqueName

use of net.opentsdb.uid.NoSuchUniqueName 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);
    }
    checkAuthorization(tsdb, query.channel(), data_query);
    // 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) {
                // call different interfaces basing on whether it is a percentile query
                if (!query.isHistogramQuery()) {
                    deferreds.add(query.runAsync());
                } else {
                    deferreds.add(query.runHistogramAsync());
                }
            }
            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 4 with NoSuchUniqueName

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

the class UidManager method extactLookupName.

/**
 * Looks up a name for a given kind, and prints it if found.
 * @param client The HBase client 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 name The name to look for.
 * @return 0 if the name for this kind was found, 1 otherwise.
 */
private static int extactLookupName(final HBaseClient client, final byte[] table, final short idwidth, final String kind, final String name) {
    final UniqueId uid = new UniqueId(client, table, kind, (int) idwidth);
    try {
        final byte[] id = uid.getId(name);
        System.out.println(kind + ' ' + name + ": " + Arrays.toString(id));
        return 0;
    } catch (NoSuchUniqueName e) {
        LOG.error(e.getMessage());
        return 1;
    }
}
Also used : UniqueId(net.opentsdb.uid.UniqueId) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 5 with NoSuchUniqueName

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

the class UidManager method rename.

/**
 * Implements the {@code rename} subcommand.
 * @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 args Command line arguments ({@code assign name [names]}).
 * @return The exit status of the command (0 means success).
 */
private static int rename(final HBaseClient client, final byte[] table, final short idwidth, final String[] args) {
    final String kind = args[1];
    final String oldname = args[2];
    final String newname = args[3];
    final UniqueId uid = new UniqueId(client, table, kind, (int) idwidth);
    try {
        uid.rename(oldname, newname);
    } catch (HBaseException e) {
        LOG.error("error while processing renaming " + oldname + " to " + newname, e);
        return 3;
    } catch (NoSuchUniqueName e) {
        LOG.error(e.getMessage());
        return 1;
    }
    System.out.println(kind + ' ' + oldname + " -> " + newname);
    return 0;
}
Also used : UniqueId(net.opentsdb.uid.UniqueId) NoSuchUniqueId(net.opentsdb.uid.NoSuchUniqueId) HBaseException(org.hbase.async.HBaseException) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Aggregations

NoSuchUniqueName (net.opentsdb.uid.NoSuchUniqueName)28 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 NoSuchUniqueId (net.opentsdb.uid.NoSuchUniqueId)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Deferred (com.stumbleupon.async.Deferred)6 TSDB (net.opentsdb.core.TSDB)6 Config (net.opentsdb.utils.Config)6 Before (org.junit.Before)6 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)5 Field (java.lang.reflect.Field)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MockBase (net.opentsdb.storage.MockBase)5 UniqueId (net.opentsdb.uid.UniqueId)5 Callback (com.stumbleupon.async.Callback)4 IOException (java.io.IOException)4 FakeTaskTimer (net.opentsdb.core.BaseTsdbTest.FakeTaskTimer)4 HBaseException (org.hbase.async.HBaseException)4 IncomingDataPoint (net.opentsdb.core.IncomingDataPoint)3