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());
}
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));
}
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());
}
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));
}
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());
}
Aggregations