Search in sources :

Example 21 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestSpan method downsampler.

@Test
public void downsampler() throws Exception {
    final byte[] val40 = Bytes.fromLong(40L);
    final byte[] val50 = Bytes.fromLong(50L);
    // For a value at the offset 0 seconds from a base timestamp.
    final byte[] qual0 = { 0x00, 0x07 };
    // For a value at the offset 5 seconds from a base timestamp.
    final byte[] qual5 = { 0x00, 0x57 };
    // For a value at the offset 2000 (0x7D0) seconds from a base timestamp.
    final byte[] qual2000 = { 0x7D, 0x07 };
    // For values at the offsets 0 and 2000 seconds from a base timestamp.
    final byte[] qual02000 = MockBase.concatByteArrays(qual0, qual2000);
    // For values at the offsets 0 and 5 seconds from a base timestamp.
    final byte[] qual05 = MockBase.concatByteArrays(qual0, qual5);
    final Span span = new Span(tsdb);
    span.addRow(new KeyValue(HOUR1, FAMILY, qual02000, MockBase.concatByteArrays(val40, val50, ZERO)));
    span.addRow(new KeyValue(HOUR2, FAMILY, qual05, MockBase.concatByteArrays(val40, val50, ZERO)));
    span.addRow(new KeyValue(HOUR3, FAMILY, qual02000, MockBase.concatByteArrays(val40, val50, ZERO)));
    assertEquals(6, span.size());
    long interval_ms = 1000000;
    Aggregator downsampler = Aggregators.get("avg");
    final SeekableView it = span.downsampler(1356998000L, 1357007000L, interval_ms, downsampler, FillPolicy.NONE);
    List<Double> values = Lists.newArrayList();
    List<Long> timestamps_in_millis = Lists.newArrayList();
    while (it.hasNext()) {
        DataPoint dp = it.next();
        assertFalse(dp.isInteger());
        values.add(dp.doubleValue());
        timestamps_in_millis.add(dp.timestamp());
    }
    assertEquals(5, values.size());
    assertEquals(40, values.get(0).longValue());
    assertEquals(1356998000000L, timestamps_in_millis.get(0).longValue());
    assertEquals(50, values.get(1).longValue());
    assertEquals(1357000000000L, timestamps_in_millis.get(1).longValue());
    assertEquals(45, values.get(2).longValue());
    assertEquals(1357002000000L, timestamps_in_millis.get(2).longValue());
    assertEquals(40, values.get(3).longValue());
    assertEquals(1357005000000L, timestamps_in_millis.get(3).longValue());
    assertEquals(50, values.get(4).longValue());
    assertEquals(1357007000000L, timestamps_in_millis.get(4).longValue());
}
Also used : KeyValue(org.hbase.async.KeyValue) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestCliUtils method setupGetDataTableScanners.

private void setupGetDataTableScanners(final long max) {
    final KeyValue kv = new KeyValue(new byte[] {}, TSDB.FAMILY(), "metrics".getBytes(), Bytes.fromLong(max));
    final ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
    kvs.add(kv);
    when(client.get(any(GetRequest.class))).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(kvs));
    start_keys = new ArrayList<byte[]>();
    stop_keys = new ArrayList<byte[]>();
    final Scanner scanner = mock(Scanner.class);
    when(client.newScanner(any(byte[].class))).thenReturn(scanner);
    PowerMockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            start_keys.add((byte[]) invocation.getArguments()[0]);
            return null;
        }
    }).when(scanner).setStartKey(any(byte[].class));
    PowerMockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            stop_keys.add((byte[]) invocation.getArguments()[0]);
            return null;
        }
    }).when(scanner).setStopKey(any(byte[].class));
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GetRequest(org.hbase.async.GetRequest) ArrayList(java.util.ArrayList)

Example 23 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestUniqueId method suggestWithMatches.

@Test
public void suggestWithMatches() {
    uid = new UniqueId(client, table, METRIC, 3);
    final Scanner fake_scanner = mock(Scanner.class);
    when(client.newScanner(table)).thenReturn(fake_scanner);
    final ArrayList<ArrayList<KeyValue>> rows = new ArrayList<ArrayList<KeyValue>>(2);
    final byte[] foo_bar_id = { 0, 0, 1 };
    {
        ArrayList<KeyValue> row = new ArrayList<KeyValue>(1);
        row.add(new KeyValue("foo.bar".getBytes(), ID, METRIC_ARRAY, foo_bar_id));
        rows.add(row);
        row = new ArrayList<KeyValue>(1);
        row.add(new KeyValue("foo.baz".getBytes(), ID, METRIC_ARRAY, new byte[] { 0, 0, 2 }));
        rows.add(row);
    }
    when(fake_scanner.nextRows()).thenReturn(Deferred.<ArrayList<ArrayList<KeyValue>>>fromResult(rows)).thenReturn(Deferred.<ArrayList<ArrayList<KeyValue>>>fromResult(null));
    // Watch this! ______,^   I'm writing C++ in Java!
    final List<String> suggestions = uid.suggest("foo");
    final ArrayList<String> expected = new ArrayList<String>(2);
    expected.add("foo.bar");
    expected.add("foo.baz");
    assertEquals(expected, suggestions);
    // Verify that we cached the forward + backwards mapping for both results
    // we "discovered" as a result of the scan.
    assertEquals(4, uid.cacheSize());
    assertEquals(0, uid.cacheHits());
    // Verify that the cached results are usable.
    // Should be a cache hit ...
    assertArrayEquals(foo_bar_id, uid.getOrCreateId("foo.bar"));
    assertEquals(1, uid.cacheHits());
    // ... so verify there was no HBase Get.
    verify(client, never()).get(anyGet());
}
Also used : Scanner(org.hbase.async.Scanner) KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestUniqueId method getOrCreateIdAssignFilterOK.

// Test the creation of an ID with no problem.
@Test
public void getOrCreateIdAssignFilterOK() {
    uid = new UniqueId(client, table, METRIC, 3);
    final byte[] id = { 0, 0, 5 };
    final Config config = mock(Config.class);
    when(config.enable_realtime_uid()).thenReturn(false);
    final TSDB tsdb = mock(TSDB.class);
    when(tsdb.getConfig()).thenReturn(config);
    uid.setTSDB(tsdb);
    final UniqueIdFilterPlugin filter = mock(UniqueIdFilterPlugin.class);
    when(filter.fillterUIDAssignments()).thenReturn(true);
    when(filter.allowUIDAssignment(any(UniqueIdType.class), anyString(), anyString(), anyMapOf(String.class, String.class))).thenReturn(Deferred.fromResult(true));
    when(tsdb.getUidFilter()).thenReturn(filter);
    // null  =>  ID doesn't exist.
    when(client.get(anyGet())).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
    // Watch this! ______,^   I'm writing C++ in Java!
    when(client.atomicIncrement(incrementForRow(MAXID))).thenReturn(Deferred.fromResult(5L));
    when(client.compareAndSet(anyPut(), emptyArray())).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(true));
    assertArrayEquals(id, uid.getOrCreateId("foo"));
    // Should be a cache hit since we created that entry.
    assertArrayEquals(id, uid.getOrCreateId("foo"));
    // Should be a cache hit too for the same reason.
    assertEquals("foo", uid.getName(id));
    // Initial Get.
    verify(client).get(anyGet());
    verify(client).atomicIncrement(incrementForRow(MAXID));
    // Reverse + forward mappings.
    verify(client, times(2)).compareAndSet(anyPut(), emptyArray());
    verify(filter, times(1)).allowUIDAssignment(any(UniqueIdType.class), anyString(), anyString(), anyMapOf(String.class, String.class));
}
Also used : KeyValue(org.hbase.async.KeyValue) Config(net.opentsdb.utils.Config) UniqueIdType(net.opentsdb.uid.UniqueId.UniqueIdType) TSDB(net.opentsdb.core.TSDB) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestUniqueId method getOrCreateIdWithExistingId.

@Test
public void getOrCreateIdWithExistingId() {
    uid = new UniqueId(client, table, METRIC, 3);
    final byte[] id = { 0, 'a', 0x42 };
    final byte[] byte_name = { 'f', 'o', 'o' };
    ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
    kvs.add(new KeyValue(byte_name, ID, METRIC_ARRAY, id));
    when(client.get(anyGet())).thenReturn(Deferred.fromResult(kvs));
    assertArrayEquals(id, uid.getOrCreateId("foo"));
    // Should be a cache hit ...
    assertArrayEquals(id, uid.getOrCreateId("foo"));
    assertEquals(1, uid.cacheHits());
    assertEquals(1, uid.cacheMisses());
    assertEquals(2, uid.cacheSize());
    // ... so verify there was only one HBase Get.
    verify(client).get(anyGet());
}
Also used : KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

KeyValue (org.hbase.async.KeyValue)171 Test (org.junit.Test)127 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)121 ArrayList (java.util.ArrayList)101 Annotation (net.opentsdb.meta.Annotation)50 Callback (com.stumbleupon.async.Callback)30 GetRequest (org.hbase.async.GetRequest)21 Scanner (org.hbase.async.Scanner)19 Deferred (com.stumbleupon.async.Deferred)14 HBaseException (org.hbase.async.HBaseException)13 TSDB (net.opentsdb.core.TSDB)12 Matchers.anyString (org.mockito.Matchers.anyString)11 Config (net.opentsdb.utils.Config)10 UniqueIdType (net.opentsdb.uid.UniqueId.UniqueIdType)9 DeleteRequest (org.hbase.async.DeleteRequest)8 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 PutRequest (org.hbase.async.PutRequest)6 List (java.util.List)5