use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method getOrCreateIdPutsReverseMappingFirst.
// Test that the reverse mapping is created before the forward one.
@Test
public void getOrCreateIdPutsReverseMappingFirst() {
uid = new UniqueId(client, table, METRIC, 3);
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);
// 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(6L));
when(client.compareAndSet(anyPut(), emptyArray())).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(true));
final byte[] id = { 0, 0, 6 };
final byte[] row = { 'f', 'o', 'o' };
assertArrayEquals(id, uid.getOrCreateId("foo"));
final InOrder order = inOrder(client);
// Initial Get.
order.verify(client).get(anyGet());
order.verify(client).atomicIncrement(incrementForRow(MAXID));
order.verify(client).compareAndSet(putForRow(id), emptyArray());
order.verify(client).compareAndSet(putForRow(row), emptyArray());
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method getNameSuccessfulHBaseLookup.
@Test
public void getNameSuccessfulHBaseLookup() {
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(id, ID, METRIC_ARRAY, byte_name));
when(client.get(anyGet())).thenReturn(Deferred.fromResult(kvs));
assertEquals("foo", uid.getName(id));
// Should be a cache hit ...
assertEquals("foo", uid.getName(id));
assertEquals(1, uid.cacheHits());
assertEquals(1, uid.cacheMisses());
assertEquals(2, uid.cacheSize());
// ... so verify there was only one HBase Get.
verify(client).get(anyGet());
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method renameRaceCondition.
@Test(expected = IllegalStateException.class)
public void renameRaceCondition() throws Exception {
// Simulate a race between client A(default) and client B.
// A and B rename same UID to different name.
// B waits till A start to invoke PutRequest to start.
uid = new UniqueId(client, table, METRIC, 3);
HBaseClient client_b = mock(HBaseClient.class);
final UniqueId uid_b = new UniqueId(client_b, table, METRIC, 3);
final byte[] foo_id = { 0, 'a', 0x42 };
final byte[] foo_name = { 'f', 'o', 'o' };
ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(foo_name, ID, METRIC_ARRAY, foo_id));
when(client_b.get(anyGet())).thenReturn(Deferred.fromResult(kvs)).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client_b.put(anyPut())).thenAnswer(answerTrue());
when(client_b.delete(anyDelete())).thenAnswer(answerTrue());
final Answer<Deferred<Boolean>> the_race = new Answer<Deferred<Boolean>>() {
public Deferred<Boolean> answer(final InvocationOnMock inv) throws Exception {
uid_b.rename("foo", "xyz");
return Deferred.fromResult(true);
}
};
when(client.get(anyGet())).thenReturn(Deferred.fromResult(kvs)).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client.put(anyPut())).thenAnswer(the_race);
when(client.delete(anyDelete())).thenAnswer(answerTrue());
uid.rename("foo", "bar");
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method getOrCreateIdAsyncAssignFilterBlocked.
@Test(expected = FailedToAssignUniqueIdException.class)
public void getOrCreateIdAsyncAssignFilterBlocked() throws Exception {
uid = new UniqueId(client, table, METRIC, 3);
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(false));
when(tsdb.getUidFilter()).thenReturn(filter);
// null => ID doesn't exist.
when(client.get(anyGet())).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client.atomicIncrement(incrementForRow(MAXID))).thenReturn(Deferred.fromResult(5L));
when(client.compareAndSet(anyPut(), emptyArray())).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(true));
uid.getOrCreateIdAsync("foo").join();
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method getOrCreateIdWithICVFailure.
// ICV throws an exception, we can't get an ID.
@Test
public void getOrCreateIdWithICVFailure() {
uid = new UniqueId(client, table, METRIC, 3);
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);
// null => ID doesn't exist.
when(client.get(anyGet())).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
// Watch this! ______,^ I'm writing C++ in Java!
// Update once HBASE-2292 is fixed:
HBaseException hbe = fakeHBaseException();
when(client.atomicIncrement(incrementForRow(MAXID))).thenReturn(Deferred.<Long>fromError(hbe)).thenReturn(Deferred.fromResult(5L));
when(client.compareAndSet(anyPut(), emptyArray())).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(true));
final byte[] id = { 0, 0, 5 };
assertArrayEquals(id, uid.getOrCreateId("foo"));
// Initial Get.
verify(client, times(1)).get(anyGet());
// First increment (failed) + retry.
verify(client, times(2)).atomicIncrement(incrementForRow(MAXID));
// Reverse + forward mappings.
verify(client, times(2)).compareAndSet(anyPut(), emptyArray());
}
Aggregations