com.stumbleupon.async.Deferred Java Examples

The following examples show how to use com.stumbleupon.async.Deferred. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HBaseStore.java    From qmq with Apache License 2.0 6 votes vote down vote up
@Override
protected void doBatchSave(byte[] table, byte[][] keys, byte[] family, byte[][] qualifiers, byte[][][] values) {
    List<Deferred<Object>> deferreds = new ArrayList<>(keys.length);
    for (int i = 0; i < keys.length; ++i) {
        if (keys[i] == null) continue;
        if (values[i] == null) continue;

        PutRequest request = new PutRequest(table, keys[i], family, qualifiers, values[i]);
        Deferred<Object> future = client.put(request);
        deferreds.add(future);
    }

    try {
        Deferred.group(deferreds).join(30 * 1000);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: AsyncTest.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorBack() throws InterruptedException {
    final String err = "runtime error";
    final Deferred<Object> deferred = new Deferred<>();
    final boolean[] enter = {false};
    final Object[] result = {new Exception()};
    deferred.addErrback(eb -> {
        enter[0] = true;
        result[0] = eb;
        return eb;
    });
    new Thread(() -> deferred.callback(new RuntimeException(err))).start();
    assertFalse(enter[0]);
    assertNull(((Exception) result[0]).getMessage());
    Thread.sleep(10);
    assertTrue(enter[0]);
    assertTrue(((Exception) result[0]).getMessage().endsWith(err));
}
 
Example #3
Source File: AsyncTest.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() throws InterruptedException {
    final String msg = "success";
    final Deferred<Object> deferred = new Deferred<>();
    final boolean[] enter = {false};
    final Object[] result = {""};
    deferred.addCallback(cb -> {
        enter[0] = true;
        result[0] = cb;
        return cb;
    });
    new Thread(() -> deferred.callback(msg)).start();
    assertFalse(enter[0]);
    assertNotEquals(msg, result[0].toString());
    Thread.sleep(10);
    assertTrue(enter[0]);
    assertEquals(msg, result[0].toString());
}
 
Example #4
Source File: TestTagsets.java    From kudu-ts with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 100000)
public void testHashCollisions() throws Exception {
  try (KuduTS ts = KuduTS.openOrCreate(ImmutableList.of(masterAddresses),
                                       "testHashCollisions",
                                       CreateOptions.defaults())) {
    Tagsets tagsets = ts.getTagsets();

    int numTagsets = 100;
    tagsets.setHashForTesting(0);

    List<Deferred<Integer>> deferreds = new ArrayList<>();
    for (int i = 0; i <= numTagsets; i++) {
      deferreds.add(tagsets.getTagsetID(ImmutableSortedMap.of("key", Integer.toString(i))));
    }

    List<Integer> ids = Deferred.group(deferreds).join();
    Collections.sort(ids);

    for (int i = 0; i < numTagsets; i++) {
      assertEquals(i, ids.get(i).intValue());
    }
  }
}
 
Example #5
Source File: TestTagsets.java    From kudu-ts with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000)
public void testConcurrentLookup() throws Exception {
  try (KuduTS ts = KuduTS.openOrCreate(ImmutableList.of(masterAddresses),
                                       "testConcurrentLookup",
                                       CreateOptions.defaults())) {
    Tagsets tagsets = ts.getTagsets();
    SortedMap<String, String> tagset = ImmutableSortedMap.of("k1", "v1");

    List<Deferred<Integer>> deferreds = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
      deferreds.add(tagsets.getTagsetID(tagset));
      tagsets.clear();
    }

    assertEquals(1, ImmutableSet.copyOf(Deferred.group(deferreds).join()).size());
  }
}
 
Example #6
Source File: KuduTS.java    From kudu-ts with Apache License 2.0 6 votes vote down vote up
private static Deferred<KuduTable> openOrCreateTable(final AsyncKuduClient client,
                                                     final String table,
                                                     final Schema schema,
                                                     final CreateTableOptions options) throws Exception {
  class CreateTableErrback implements Callback<Deferred<KuduTable>, Exception> {
    @Override
    public Deferred<KuduTable> call(Exception e) throws Exception {
      // TODO(danburkert): we should only do this if the error is "not found"
      LOG.debug("Creating table {}", table);
      return client.createTable(table, schema, options);
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).add("table", table).toString();
    }
  }

  return client.openTable(table).addErrback(new CreateTableErrback());
}
 
Example #7
Source File: KuduTS.java    From kudu-ts with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a Kudu TS instance on a Kudu cluster.
 *
 * @param kuduMasterAddressess list of "host:port" pair master addresses
 * @param name the name of the Kudu timeseries store. Multiple instances of
 *             Kudu TS can occupy the same Kudu cluster by using a different name.
 * @return the opened {@code KuduTS}.
 * @throws Exception on error
 */
public static KuduTS open(List<String> kuduMasterAddressess, String name) throws Exception {
  AsyncKuduClient client = new AsyncKuduClient.AsyncKuduClientBuilder(kuduMasterAddressess).build();

  Deferred<KuduTable> metricsDeferred = client.openTable(Tables.metricsTableName(name));
  Deferred<KuduTable> tagsetsDeferred = client.openTable(Tables.tagsetsTableName(name));
  Deferred<KuduTable> tagsDeferred = client.openTable(Tables.tagsTableName(name));
  KuduTable metricsTable = metricsDeferred.join(client.getDefaultAdminOperationTimeoutMs());
  KuduTable tagsetsTable = tagsetsDeferred.join(client.getDefaultAdminOperationTimeoutMs());
  KuduTable tagsTable = tagsDeferred.join(client.getDefaultAdminOperationTimeoutMs());

  Tags tags = new Tags(client, tagsTable);
  Tagsets tagsets = new Tagsets(client, tags, tagsetsTable);
  Metrics metrics = new Metrics(client, metricsTable, tagsets);
  return new KuduTS(client, name, metrics, tagsets, tags);
}
 
Example #8
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 6 votes vote down vote up
Tagsets(AsyncKuduClient client, Tags tags, KuduTable tagsetsTable) {
  this.client = client;
  this.tagsetsTable = tagsetsTable;
  this.tags = tags;
  this.columnIndexes = ImmutableList.of(Tables.TAGSETS_ID_INDEX,
                                        Tables.TAGSETS_TAGSET_INDEX);
  this.tagsets = CacheBuilder.newBuilder()
      .maximumSize(1024 * 1024)
      .build(new CacheLoader<SerializedTagset, Deferred<Integer>>() {
        @Override
        public Deferred<Integer> load(SerializedTagset tagset) {
          return lookupOrInsertTagset(tagset, hashForTesting == null ?
                                              tagset.hashCode() : hashForTesting);
        }
      });
}
 
Example #9
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
private void setupHistogramData(final boolean requeued) {
  config.overrideConfig("tsd.core.histograms.config", 
      "{\"net.opentsdb.core.SimpleHistogramDecoder\": 42}");
  final HistogramCodecManager manager;
  manager = new HistogramCodecManager(tsdb);
  when(tsdb.histogramManager()).thenReturn(manager);
  when(tsdb.addHistogramPoint(anyString(), anyLong(), any(byte[].class), 
      anyMap()))
    .thenReturn(Deferred.fromResult(null));
  final Histogram histo = new Histogram();
  histo.setMetric(METRIC);
  histo.setTimestamp(TS);
  histo.setTags(new HashMap<String, String>(TAGS));
  histo.setOverflow(1);
  histo.setBuckets(ImmutableMap.<String, Long>builder()
      .put("0,1", 42L)
      .put("1,5", 24L)
      .build());
  data = histo;
  if (requeued) {
    data.setRequeueTS(TS + 60);
  }
  when(message.message()).thenReturn(JSON.serializeToBytes(data));
}
 
Example #10
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
private void setupRawDataList(final boolean requeued) {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
          .thenReturn(Deferred.fromResult(null));

  TypedIncomingData ev1 = new Metric(METRIC, TS, "42", TAGS);
  TypedIncomingData ev2 = new Metric(METRIC2, TS, "42", TAGS);

  if (requeued) {
    ev1.setRequeueTS(TS + 60);
    ev2.setRequeueTS(TS + 60);
  }

  StringBuilder sb = new StringBuilder();
  sb.append("[");
  sb.append(new String(JSON.serializeToBytes(ev1)));
  sb.append(",");
  sb.append(new String(JSON.serializeToBytes(ev2)));
  sb.append("]");

  when(message.message()).thenReturn(sb.toString().getBytes());
}
 
Example #11
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void runStorageFailureHistogram() throws Exception {
  when(group.getConsumerType()).thenReturn(TsdbConsumerType.ROLLUP);
  setupHistogramData(false);
  when(tsdb.addHistogramPoint(anyString(), anyLong(), any(byte[].class), 
      anyMap()))
    .thenReturn(Deferred.fromResult(mock(HBaseException.class)));
  KafkaRpcPluginThread writer = Mockito.spy(
      new KafkaRpcPluginThread(group, 1, TOPICS));
  writer.run();

  verifyMessageRead(writer, true);
  verify(tsdb, times(1)).addHistogramPoint(eq(METRIC), eq(TS), 
      any(byte[].class), eq(TAGS));
  verifyCtrsInc(new String[]{ "readHistogramCounter", "requeuedHistogramCounter",
    "storageExceptionCounter" });
}
 
Example #12
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void runStorageFailurePreAgg() throws Exception {
  when(group.getConsumerType()).thenReturn(TsdbConsumerType.ROLLUP);
  setupAggData(false, true);
  when(tsdb.addAggregatePoint(anyString(), anyLong(), anyLong(), anyMap(), 
      anyBoolean(), anyString(), anyString(), anyString()))
    .thenReturn(Deferred.fromResult(mock(HBaseException.class)));
  KafkaRpcPluginThread writer = Mockito.spy(
      new KafkaRpcPluginThread(group, 1, TOPICS));
  writer.run();

  verifyMessageRead(writer, true);
  verify(tsdb, times(1)).addAggregatePoint(METRIC, TS, 42L, TAGS, true, 
      null, null, "sum");
  verifyCtrsInc(new String[]{ "readAggregateCounter", "requeuedAggregateCounter",
    "storageExceptionCounter" });
}
 
Example #13
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void runStorageFailureRollup() throws Exception {
  when(group.getConsumerType()).thenReturn(TsdbConsumerType.ROLLUP);
  setupAggData(false, false);
  when(tsdb.addAggregatePoint(anyString(), anyLong(), anyLong(), anyMap(), 
      anyBoolean(), anyString(), anyString(), anyString()))
    .thenReturn(Deferred.fromResult(mock(HBaseException.class)));
  KafkaRpcPluginThread writer = Mockito.spy(
      new KafkaRpcPluginThread(group, 1, TOPICS));
  writer.run();

  verifyMessageRead(writer, true);
  verify(tsdb, times(1)).addAggregatePoint(METRIC, TS, 42L, TAGS, false, 
      "1h", "sum", null);
  verifyCtrsInc(new String[]{ "readRollupCounter", "requeuedRollupCounter",
    "storageExceptionCounter" });
}
 
Example #14
Source File: TestKafkaRpcPluginThread.java    From opentsdb-rpc-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void runEmptyMetric() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
    .thenReturn(Deferred.fromResult(null));
  data = new Metric(null, TS, "42", TAGS);
  when(message.message()).thenReturn(JSON.serializeToBytes(data));
  KafkaRpcPluginThread writer = Mockito.spy(
      new KafkaRpcPluginThread(group, 1, TOPICS));

  writer.run();
  verify(tsdb, never()).addPoint(anyString(), anyLong(), anyLong(), anyMap());
  verify(tsdb, never()).addHistogramPoint(anyString(), anyLong(), 
      any(byte[].class), anyMap());
  verify(tsdb, never()).addAggregatePoint(anyString(), anyLong(), anyLong(), 
      anyMap(), anyBoolean(), anyString(), anyString(), anyString());
  verifyMessageRead(writer, false);
}
 
Example #15
Source File: TestMetric.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void processDataUnknownException() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
    .thenReturn(Deferred.fromResult(new RuntimeException()));
  
  final Metric dp = new Metric(METRIC, TS, "0", TAGS);
  dp.processData(consumer, 0);
  verify(tsdb, times(1)).addPoint(METRIC, TS, 0L, TAGS);
  verify(seh, times(1)).handleError(eq(dp), any(Exception.class));
  verify(consumer, times(1)).incrementNamespaceCounter(CounterType.UnknownException, METRIC);
}
 
Example #16
Source File: AsyncTest.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
@Test
public void testGroup() throws Exception {
    LinkedList<Deferred<byte[]>> deferredList = new LinkedList<>();
    deferredList.add(buildDeferred("1"));
    deferredList.add(buildDeferred("2"));
    deferredList.add(buildDeferred("3"));
    Deferred.group(deferredList).joinUninterruptibly(10);
}
 
Example #17
Source File: KuduConnector.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public boolean writeRow(KuduRow row) throws Exception {
    final Operation operation = KuduMapper.toOperation(table, writeMode, row);

    Deferred<OperationResponse> response = session.apply(operation);

    if (KuduConnector.Consistency.EVENTUAL.equals(consistency)) {
        pendingTransactions.incrementAndGet();
        response.addCallback(defaultCB);
    } else {
        processResponse(response.join());
    }

    return !errorTransactions.get();

}
 
Example #18
Source File: KuduConnector.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public boolean writeRow(KuduRow row) throws Exception {
    final Operation operation = KuduMapper.toOperation(table, writeMode, row);

    Deferred<OperationResponse> response = session.apply(operation);

    if (KuduConnector.Consistency.EVENTUAL.equals(consistency)) {
        pendingTransactions.incrementAndGet();
        response.addCallback(defaultCB);
    } else {
        processResponse(response.join());
    }

    return !errorTransactions.get();

}
 
Example #19
Source File: TestTagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testHashCollisionsWraparound() throws Exception {
  try (KuduTS ts = KuduTS.openOrCreate(ImmutableList.of(masterAddresses),
                                       "testHashCollisionsWraparound",
                                       CreateOptions.defaults()
                                                    .setNumTagsetsTablets(2))) {
    Tagsets tagsets = ts.getTagsets();

    int numTagsets = 30;
    int offset = 15;

    tagsets.setHashForTesting(Integer.MAX_VALUE - offset);

    List<Deferred<Integer>> deferreds = new ArrayList<>();
    for (int i = 0; i < numTagsets; i++) {
      deferreds.add(tagsets.getTagsetID(ImmutableSortedMap.of("key", Integer.toString(i))));
    }

    List<Integer> ids = Deferred.group(deferreds).join();
    Collections.sort(ids);

    List<Integer> expectedIds = new ArrayList<>();
    for (int i = 0; i < numTagsets; i++) {
      expectedIds.add((Integer.MAX_VALUE - offset) + i);
    }
    Collections.sort(expectedIds);
    assertEquals(expectedIds, ids);
  }
}
 
Example #20
Source File: KafkaStorageExceptionHandler.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the producer if it's not null already
 */
public Deferred<Object> shutdown() {
  LOG.warn("Shutting down Kafka requeue producer");
  if (producer != null) {
    producer.close();
  }
  return Deferred.fromResult(null);
}
 
Example #21
Source File: KuduTS.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Creates (if necessary) and opens a Kudu TS instance on a Kudu cluster.
 *
 * @param kuduMasterAddressess list of "host:port" pair master addresses
 * @param name the name of the Kudu timeseries store. Multiple instances of
 *             Kudu TS can occupy the same Kudu cluster by using a different name.
 * @return the opened {@code KuduTS}.
 * @throws Exception on error
 */
public static KuduTS openOrCreate(List<String> kuduMasterAddressess,
                                  String name,
                                  CreateOptions options) throws Exception {

  AsyncKuduClient client = new AsyncKuduClient.AsyncKuduClientBuilder(kuduMasterAddressess).build();

  int numTabletServers = client.listTabletServers()
                               .joinUninterruptibly(client.getDefaultAdminOperationTimeoutMs())
                               .getTabletServersCount();

  Deferred<KuduTable> metricsDeferred = openOrCreateTable(client,
                                                          Tables.metricsTableName(name),
                                                          Tables.METRICS_SCHEMA,
                                                          Tables.metricsCreateTableOptions(options, numTabletServers));
  Deferred<KuduTable> tagsetsDeferred = openOrCreateTable(client,
                                                          Tables.tagsetsTableName(name),
                                                          Tables.TAGSETS_SCHEMA,
                                                          Tables.tagsetsCreateTableOptions(options, numTabletServers));
  Deferred<KuduTable> tagsDeferred = openOrCreateTable(client,
                                                       Tables.tagsTableName(name),
                                                       Tables.TAGS_SCHEMA,
                                                       Tables.tagsCreateTableOptions(options, numTabletServers));
  KuduTable metricsTable = metricsDeferred.join(client.getDefaultAdminOperationTimeoutMs());
  KuduTable tagsetsTable = tagsetsDeferred.join(client.getDefaultAdminOperationTimeoutMs());
  KuduTable tagsTable = tagsDeferred.join(client.getDefaultAdminOperationTimeoutMs());

  Tags tags = new Tags(client, tagsTable);
  Tagsets tagsets = new Tagsets(client, tags, tagsetsTable);
  Metrics metrics = new Metrics(client, metricsTable, tagsets);
  return new KuduTS(client, name, metrics, tagsets, tags);
}
 
Example #22
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
@Override
public Deferred<TagsetLookupResult> call(RowResultIterator rows) {
  for (RowResult row : rows) {
    int id = row.getInt(Tables.TAGSETS_ID_INDEX);
    Preconditions.checkState(id >= probe);
    if (id != probe) {
      // We found a hole in the table where we expected the tagset.
      return Deferred.fromResult(new TagsetLookupResult(false, probe));
    }

    if (tagset.equals(row.getBinary(Tables.TAGSETS_TAGSET_INDEX))) {
      return Deferred.fromResult(new TagsetLookupResult(true, id));
    }

    probe++;
  }

  // We probed through the entire RowResult and didn't find the tagset.
  if (!scanner.hasMoreRows()) {
    if (probe <= Ints.saturatedCast((long) id + TAGSETS_PER_SCAN)) {
      // We found a hole at the end of the scan.
      return Deferred.fromResult(new TagsetLookupResult(false, probe));
    }
    // The current scanner has been exhausted; create a new scanner from the
    // latest probe point.
    scanner = tagsetScanner(probe);
    id = probe;
  }
  return scanner.nextRows().addCallbackDeferring(this);
}
 
Example #23
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to insert the provided tagset and ID. Returns {@code true} if the
 * write was successful, or {@code false} if the write failed due to a tagset
 * with the same ID already existing in the table.
 *
 * @param tagset the tagset to insert
 * @param id     the ID to insert the tagset with
 * @return whether the write succeeded
 */
private Deferred<Boolean> insertTagset(final SerializedTagset tagset, final int id) throws KuduException {
  final class InsertTagsetCB implements Callback<Deferred<Boolean>, OperationResponse> {
    @Override
    public Deferred<Boolean> call(OperationResponse response) {
      if (response.hasRowError()) {
        if (response.getRowError().getErrorStatus().isAlreadyPresent()) {
          LOG.info("Attempted to insert duplicate tagset; id: {}, tagset: {}", id, tagset);
          // TODO: Consider adding a backoff with jitter before attempting
          //       the insert again (if the lookup fails).
          return Deferred.fromResult(false);
        }
        return Deferred.fromError(new RuntimeException(
            String.format("Unable to insert tagset; id: %s, tagset: %s, error: %s",
                          id, tagset, response.getRowError())));
      } else {
        return Deferred.fromResult(true);
      }
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).toString();
    }
  }

  LOG.debug("Inserting tagset; id: {}, tags: {}", id, tagset);
  final AsyncKuduSession session = client.newSession();
  try {
    // We don't have to handle PleaseThrottleException because we are only
    // inserting a single row.
    final Insert insert = tagsetsTable.newInsert();
    insert.getRow().addInt(Tables.TAGSETS_ID_INDEX, id);
    insert.getRow().addBinary(Tables.TAGSETS_TAGSET_INDEX, tagset.getBytes());
    return session.apply(insert).addCallbackDeferring(new InsertTagsetCB());
  } finally {
    session.close();
  }
}
 
Example #24
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
private Deferred<TagsetLookupResult> lookupTagset(SerializedTagset tagset, int id) {
  LOG.debug("Looking up tagset; id: {}, tags: {}", id, tagset);
  AsyncKuduScanner tagsetScanner = tagsetScanner(id);

  return tagsetScanner.nextRows().addCallbackDeferring(
      new TagsetScanCB(tagset, id, tagsetScanner));
}
 
Example #25
Source File: TestMetric.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void processDataSuccessfullLong() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
    .thenReturn(Deferred.fromResult(null));
  
  final Metric dp = new Metric(METRIC, TS, "42", TAGS);
  dp.processData(consumer, 0);
  verify(tsdb, times(1)).addPoint(METRIC, TS, 42L, TAGS);
  verify(seh, never()).handleError(eq(dp), any(Exception.class));
  verify(consumer, times(1)).incrementNamespaceCounter(CounterType.StoredRaw, METRIC);
}
 
Example #26
Source File: Tags.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the tagset IDs of all tagsets which contain all of the specified tags.
 * The tagset IDs are returned in sorted order.
 *
 * @param tags the tags to filter by
 * @return the sorted tagset IDs
 */
public Deferred<IntVec> getTagsetIDsForTags(Map<String, String> tags) {

  List<Deferred<IntVec>> deferreds = new ArrayList<>(tags.size());
  for (Map.Entry<String, String> tag : tags.entrySet()) {
    deferreds.add(getTagsetIDsForTag(tag.getKey(), tag.getValue()));
  }
  return Deferred.group(deferreds).addCallback(new IntersectTagsetsCB());
}
 
Example #27
Source File: Tags.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the tagset IDs of all tagsets which contain the specified tag.
 * The tagset IDs are returned in sorted order.
 *
 * @param key the tag key
 * @param value the tag value
 * @return the sorted tagset IDs
 */
public Deferred<IntVec> getTagsetIDsForTag(final String key, final String value) {
  AsyncKuduScanner.AsyncKuduScannerBuilder scan = client.newScannerBuilder(table);
  scan.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGS_KEY_COLUMN,
                                                         ComparisonOp.EQUAL, key));
  scan.addPredicate(KuduPredicate.newComparisonPredicate(Tables.TAGS_VALUE_COLUMN,
                                                         ComparisonOp.EQUAL, value));
  scan.setProjectedColumnIndexes(TAGSET_ID_PROJECTION);
  final AsyncKuduScanner scanner = scan.build();

  class GetTagCB implements Callback<Deferred<IntVec>, RowResultIterator> {
    private final IntVec tagsetIDs = IntVec.create();
    @Override
    public Deferred<IntVec> call(RowResultIterator results) {
      for (RowResult result : results) {
        tagsetIDs.push(result.getInt(0));
      }
      if (scanner.hasMoreRows()) {
        return scanner.nextRows().addCallbackDeferring(this);
      }
      // The Kudu java client doesn't yet allow us to specify a sorted
      // (fault-tolerant) scan, so have to sort manually.
      tagsetIDs.sort();
      return Deferred.fromResult(tagsetIDs);
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).add("key", key).add("value", value).toString();
    }
  }

  return scanner.nextRows().addCallbackDeferring(new GetTagCB());
}
 
Example #28
Source File: TestMetric.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void processDataHBaseException() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
    .thenReturn(Deferred.fromResult(mock(HBaseException.class)));
  
  final Metric dp = new Metric(METRIC, TS, "0", TAGS);
  dp.processData(consumer, 0);
  verify(tsdb, times(1)).addPoint(METRIC, TS, 0L, TAGS);
  verify(seh, times(1)).handleError(eq(dp), any(Exception.class));
  verify(consumer, times(1)).incrementNamespaceCounter(CounterType.StorageException, METRIC);
}
 
Example #29
Source File: TestMetric.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void processDataThrottled() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyLong(), anyMap()))
    .thenReturn(Deferred.fromResult(mock(PleaseThrottleException.class)));
  
  final Metric dp = new Metric(METRIC, TS, "0", TAGS);
  dp.processData(consumer, 0);
  verify(tsdb, times(1)).addPoint(METRIC, TS, 0L, TAGS);
  verify(seh, times(1)).handleError(eq(dp), any(Exception.class));
  verify(consumer, times(1)).incrementNamespaceCounter(CounterType.PleaseThrottle, METRIC);
}
 
Example #30
Source File: TestMetric.java    From opentsdb-rpc-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void processDataSuccessfullFloat() throws Exception {
  when(tsdb.addPoint(anyString(), anyLong(), anyFloat(), anyMap()))
    .thenReturn(Deferred.fromResult(null));
  
  final Metric dp = new Metric(METRIC, TS, "42.5", TAGS);
  dp.processData(consumer, 0);
  verify(tsdb, times(1)).addPoint(METRIC, TS, 42.5F, TAGS);
  verify(seh, never()).handleError(eq(dp), any(Exception.class));
  verify(consumer, times(1)).incrementNamespaceCounter(CounterType.StoredRaw, METRIC);
}