Search in sources :

Example 11 with NoSuchUniqueName

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

the class BaseTsdbTest method setupTagkMaps.

/** Adds the static UIDs to the tag keys UID mock object */
void setupTagkMaps() {
    when(tag_names.getId(TAGK_STRING)).thenReturn(TAGK_BYTES);
    when(tag_names.getOrCreateId(TAGK_STRING)).thenReturn(TAGK_BYTES);
    when(tag_names.getIdAsync(TAGK_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(TAGK_BYTES);
        }
    });
    when(tag_names.getOrCreateIdAsync(TAGK_STRING)).thenReturn(Deferred.fromResult(TAGK_BYTES));
    when(tag_names.getId(TAGK_B_STRING)).thenReturn(TAGK_B_BYTES);
    when(tag_names.getOrCreateId(TAGK_B_STRING)).thenReturn(TAGK_B_BYTES);
    when(tag_names.getIdAsync(TAGK_B_STRING)).thenAnswer(new Answer<Deferred<byte[]>>() {

        @Override
        public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
            return Deferred.fromResult(TAGK_B_BYTES);
        }
    });
    when(tag_names.getOrCreateIdAsync(TAGK_B_STRING)).thenReturn(Deferred.fromResult(TAGK_B_BYTES));
    when(tag_names.getNameAsync(TAGK_BYTES)).thenAnswer(new Answer<Deferred<String>>() {

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

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

            @Override
            public Deferred<byte[]> answer(InvocationOnMock invocation) throws Throwable {
                return Deferred.fromResult(uid.getValue());
            }
        });
        when(tag_names.getOrCreateId(uid.getKey())).thenReturn(uid.getValue());
        when(tag_names.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 12 with NoSuchUniqueName

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

the class TestTSDB method renameUidNonexistentTagk.

@Test(expected = IllegalArgumentException.class)
public void renameUidNonexistentTagk() {
    when(tag_names.getId("datacenter")).thenThrow(new NoSuchUniqueName("tagk", "datacenter"));
    tsdb.renameUid("tagk", "datacenter", "datacluster");
}
Also used : NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with NoSuchUniqueName

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

the class TestTSDB method assignUidTagv.

@Test
public void assignUidTagv() {
    when(tag_values.getId("localhost")).thenThrow(new NoSuchUniqueName("tagv", "localhost"));
    when(tag_values.getOrCreateId("localhost")).thenReturn(new byte[] { 0, 0, 2 });
    assertArrayEquals(new byte[] { 0, 0, 2 }, tsdb.assignUid("tagv", "localhost"));
}
Also used : NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with NoSuchUniqueName

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

the class TsdbQuery method setTimeSeries.

@Override
public void setTimeSeries(final String metric, final Map<String, String> tags, final Aggregator function, final boolean rate, final RateOptions rate_options) throws NoSuchUniqueName {
    if (filters == null) {
        filters = new ArrayList<TagVFilter>(tags.size());
    }
    TagVFilter.tagsToFilters(tags, filters);
    try {
        for (final TagVFilter filter : this.filters) {
            filter.resolveTagkName(tsdb).join();
        }
    } catch (final InterruptedException e) {
        LOG.warn("Interrupted", e);
        Thread.currentThread().interrupt();
    } catch (final NoSuchUniqueName e) {
        throw e;
    } catch (final Exception e) {
        if (e instanceof DeferredGroupException) {
            // rollback to the actual case. The DGE missdirects
            Throwable ex = e.getCause();
            while (ex != null && ex instanceof DeferredGroupException) {
                ex = ex.getCause();
            }
            if (ex != null) {
                throw (RuntimeException) ex;
            }
        }
        LOG.error("Unexpected exception processing group bys", e);
        throw new RuntimeException(e);
    }
    findGroupBys();
    this.metric = tsdb.metrics.getId(metric);
    aggregator = function;
    this.rate = rate;
    this.rate_options = rate_options;
}
Also used : TagVFilter(net.opentsdb.query.filter.TagVFilter) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException) HBaseException(org.hbase.async.HBaseException) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Example 15 with NoSuchUniqueName

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

the class PutDataPointRpc method execute.

/**
   * Handles HTTP RPC put requests
   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query from the user
   * @throws IOException if there is an error parsing the query or formatting 
   * the output
   * @throws BadRequestException if the user supplied bad data
   * @since 2.0
   */
public void execute(final TSDB tsdb, final HttpQuery query) throws IOException {
    requests.incrementAndGet();
    // only accept POST
    if (query.method() != HttpMethod.POST) {
        throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED, "Method not allowed", "The HTTP method [" + query.method().getName() + "] is not permitted for this endpoint");
    }
    final List<IncomingDataPoint> dps = query.serializer().parsePutV1();
    if (dps.size() < 1) {
        throw new BadRequestException("No datapoints found in content");
    }
    final boolean show_details = query.hasQueryStringParam("details");
    final boolean show_summary = query.hasQueryStringParam("summary");
    final boolean synchronous = query.hasQueryStringParam("sync");
    final int sync_timeout = query.hasQueryStringParam("sync_timeout") ? Integer.parseInt(query.getQueryStringParam("sync_timeout")) : 0;
    // this is used to coordinate timeouts
    final AtomicBoolean sending_response = new AtomicBoolean();
    sending_response.set(false);
    final ArrayList<HashMap<String, Object>> details = show_details ? new ArrayList<HashMap<String, Object>>() : null;
    int queued = 0;
    final List<Deferred<Boolean>> deferreds = synchronous ? new ArrayList<Deferred<Boolean>>(dps.size()) : null;
    for (final IncomingDataPoint dp : dps) {
        /** Handles passing a data point to the storage exception handler if 
       * we were unable to store it for any reason */
        final class PutErrback implements Callback<Boolean, Exception> {

            public Boolean call(final Exception arg) {
                handleStorageException(tsdb, dp, arg);
                hbase_errors.incrementAndGet();
                if (show_details) {
                    details.add(getHttpDetails("Storage exception: " + arg.getMessage(), dp));
                }
                return false;
            }

            public String toString() {
                return "HTTP Put Exception CB";
            }
        }
        /** Simply marks the put as successful */
        final class SuccessCB implements Callback<Boolean, Object> {

            @Override
            public Boolean call(final Object obj) {
                return true;
            }

            public String toString() {
                return "HTTP Put success CB";
            }
        }
        try {
            if (dp.getMetric() == null || dp.getMetric().isEmpty()) {
                if (show_details) {
                    details.add(this.getHttpDetails("Metric name was empty", dp));
                }
                LOG.warn("Metric name was empty: " + dp);
                illegal_arguments.incrementAndGet();
                continue;
            }
            if (dp.getTimestamp() <= 0) {
                if (show_details) {
                    details.add(this.getHttpDetails("Invalid timestamp", dp));
                }
                LOG.warn("Invalid timestamp: " + dp);
                illegal_arguments.incrementAndGet();
                continue;
            }
            if (dp.getValue() == null || dp.getValue().isEmpty()) {
                if (show_details) {
                    details.add(this.getHttpDetails("Empty value", dp));
                }
                LOG.warn("Empty value: " + dp);
                invalid_values.incrementAndGet();
                continue;
            }
            if (dp.getTags() == null || dp.getTags().size() < 1) {
                if (show_details) {
                    details.add(this.getHttpDetails("Missing tags", dp));
                }
                LOG.warn("Missing tags: " + dp);
                illegal_arguments.incrementAndGet();
                continue;
            }
            final Deferred<Object> deferred;
            if (Tags.looksLikeInteger(dp.getValue())) {
                deferred = tsdb.addPoint(dp.getMetric(), dp.getTimestamp(), Tags.parseLong(dp.getValue()), dp.getTags());
            } else {
                deferred = tsdb.addPoint(dp.getMetric(), dp.getTimestamp(), Float.parseFloat(dp.getValue()), dp.getTags());
            }
            if (synchronous) {
                deferreds.add(deferred.addCallback(new SuccessCB()));
            }
            deferred.addErrback(new PutErrback());
            ++queued;
        } catch (NumberFormatException x) {
            if (show_details) {
                details.add(this.getHttpDetails("Unable to parse value to a number", dp));
            }
            LOG.warn("Unable to parse value to a number: " + dp);
            invalid_values.incrementAndGet();
        } catch (IllegalArgumentException iae) {
            if (show_details) {
                details.add(this.getHttpDetails(iae.getMessage(), dp));
            }
            LOG.warn(iae.getMessage() + ": " + dp);
            illegal_arguments.incrementAndGet();
        } catch (NoSuchUniqueName nsu) {
            if (show_details) {
                details.add(this.getHttpDetails("Unknown metric", dp));
            }
            LOG.warn("Unknown metric: " + dp);
            unknown_metrics.incrementAndGet();
        }
    }
    /** A timer task that will respond to the user with the number of timeouts
     * for synchronous writes. */
    class PutTimeout implements TimerTask {

        final int queued;

        public PutTimeout(final int queued) {
            this.queued = queued;
        }

        @Override
        public void run(final Timeout timeout) throws Exception {
            if (sending_response.get()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Put data point call " + query + " already responded successfully");
                }
                return;
            } else {
                sending_response.set(true);
            }
            // figure out how many writes are outstanding
            int good_writes = 0;
            int failed_writes = 0;
            int timeouts = 0;
            for (int i = 0; i < deferreds.size(); i++) {
                try {
                    if (deferreds.get(i).join(1)) {
                        ++good_writes;
                    } else {
                        ++failed_writes;
                    }
                } catch (TimeoutException te) {
                    if (show_details) {
                        details.add(getHttpDetails("Write timedout", dps.get(i)));
                    }
                    ++timeouts;
                }
            }
            writes_timedout.addAndGet(timeouts);
            final int failures = dps.size() - queued;
            if (!show_summary && !show_details) {
                throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, "The put call has timedout with " + good_writes + " successful writes, " + failed_writes + " failed writes and " + timeouts + " timed out writes.", "Please see the TSD logs or append \"details\" to the put request");
            } else {
                final HashMap<String, Object> summary = new HashMap<String, Object>();
                summary.put("success", good_writes);
                summary.put("failed", failures + failed_writes);
                summary.put("timeouts", timeouts);
                if (show_details) {
                    summary.put("errors", details);
                }
                query.sendReply(HttpResponseStatus.BAD_REQUEST, query.serializer().formatPutV1(summary));
            }
        }
    }
    // now after everything has been sent we can schedule a timeout if so
    // the caller asked for a synchronous write.
    final Timeout timeout = sync_timeout > 0 ? tsdb.getTimer().newTimeout(new PutTimeout(queued), sync_timeout, TimeUnit.MILLISECONDS) : null;
    /** Serializes the response to the client */
    class GroupCB implements Callback<Object, ArrayList<Boolean>> {

        final int queued;

        public GroupCB(final int queued) {
            this.queued = queued;
        }

        @Override
        public Object call(final ArrayList<Boolean> results) {
            if (sending_response.get()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Put data point call " + query + " was marked as timedout");
                }
                return null;
            } else {
                sending_response.set(true);
                if (timeout != null) {
                    timeout.cancel();
                }
            }
            int good_writes = 0;
            int failed_writes = 0;
            for (final boolean result : results) {
                if (result) {
                    ++good_writes;
                } else {
                    ++failed_writes;
                }
            }
            final int failures = dps.size() - queued;
            if (!show_summary && !show_details) {
                if (failures + failed_writes > 0) {
                    query.sendReply(HttpResponseStatus.BAD_REQUEST, query.serializer().formatErrorV1(new BadRequestException(HttpResponseStatus.BAD_REQUEST, "One or more data points had errors", "Please see the TSD logs or append \"details\" to the put request")));
                } else {
                    query.sendReply(HttpResponseStatus.NO_CONTENT, "".getBytes());
                }
            } else {
                final HashMap<String, Object> summary = new HashMap<String, Object>();
                if (sync_timeout > 0) {
                    summary.put("timeouts", 0);
                }
                summary.put("success", results.isEmpty() ? queued : good_writes);
                summary.put("failed", failures + failed_writes);
                if (show_details) {
                    summary.put("errors", details);
                }
                if (failures > 0) {
                    query.sendReply(HttpResponseStatus.BAD_REQUEST, query.serializer().formatPutV1(summary));
                } else {
                    query.sendReply(query.serializer().formatPutV1(summary));
                }
            }
            return null;
        }

        @Override
        public String toString() {
            return "put data point serialization callback";
        }
    }
    /** Catches any unexpected exceptions thrown in the callback chain */
    class ErrCB implements Callback<Object, Exception> {

        @Override
        public Object call(final Exception e) throws Exception {
            if (sending_response.get()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Put data point call " + query + " was marked as timedout");
                }
                return null;
            } else {
                sending_response.set(true);
                if (timeout != null) {
                    timeout.cancel();
                }
            }
            LOG.error("Unexpected exception", e);
            throw new RuntimeException("Unexpected exception", e);
        }

        @Override
        public String toString() {
            return "put data point error callback";
        }
    }
    if (synchronous) {
        Deferred.groupInOrder(deferreds).addCallback(new GroupCB(queued)).addErrback(new ErrCB());
    } else {
        new GroupCB(queued).call(EMPTY_DEFERREDS);
    }
}
Also used : HashMap(java.util.HashMap) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) TimerTask(org.jboss.netty.util.TimerTask) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(com.stumbleupon.async.TimeoutException) Timeout(org.jboss.netty.util.Timeout) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint) IncomingDataPoint(net.opentsdb.core.IncomingDataPoint) IOException(java.io.IOException) TimeoutException(com.stumbleupon.async.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(com.stumbleupon.async.Callback) NoSuchUniqueName(net.opentsdb.uid.NoSuchUniqueName)

Aggregations

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