use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method getOrCreateIdRandomWithRaceCondition.
@Test
public void getOrCreateIdRandomWithRaceCondition() {
PowerMockito.mockStatic(RandomUniqueId.class);
uid = new UniqueId(client, table, METRIC, 3, true);
final long id = 24L;
final byte[] id_array = { 0, 0, 0x2A };
final byte[] byte_name = { 'f', 'o', 'o' };
ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(byte_name, ID, METRIC_ARRAY, id_array));
when(RandomUniqueId.getRandomUID()).thenReturn(id);
when(client.get(any(GetRequest.class))).thenReturn(Deferred.fromResult((ArrayList<KeyValue>) null)).thenReturn(Deferred.fromResult(kvs));
when(client.compareAndSet(any(PutRequest.class), any(byte[].class))).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(false));
assertArrayEquals(id_array, uid.getOrCreateId("foo"));
assertEquals(0, uid.cacheHits());
assertEquals(2, uid.cacheMisses());
assertEquals(2, uid.cacheSize());
assertEquals(1, uid.randomIdCollisions());
// ... so verify there was only one HBase Get.
verify(client, times(2)).get(any(GetRequest.class));
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method rename.
@Test
public void rename() throws Exception {
uid = new UniqueId(client, 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.get(anyGet())).thenReturn(Deferred.fromResult(kvs)).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client.put(anyPut())).thenAnswer(answerTrue());
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 getNameWithErrorDuringHBaseLookup.
@Test
public void getNameWithErrorDuringHBaseLookup() {
uid = new UniqueId(client, table, METRIC, 3);
final byte[] id = { 0, 'a', 0x42 };
final byte[] byte_name = { 'f', 'o', 'o' };
HBaseException hbe = mock(HBaseException.class);
ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(id, ID, METRIC_ARRAY, byte_name));
when(client.get(anyGet())).thenThrow(hbe).thenReturn(Deferred.fromResult(kvs));
// 1st calls fails.
try {
uid.getName(id);
fail("HBaseException should have been thrown.");
} catch (HBaseException e) {
// OK.
assertSame(hbe, e);
}
// 2nd call succeeds.
assertEquals("foo", uid.getName(id));
assertEquals(0, uid.cacheHits());
// 1st (failed) attempt + 2nd.
assertEquals(2, uid.cacheMisses());
assertEquals(2, uid.cacheSize());
verify(client, times(2)).get(anyGet());
}
use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.
the class TestUniqueId method renameNewNameExists.
@Test(expected = IllegalArgumentException.class)
public void renameNewNameExists() throws Exception {
uid = new UniqueId(client, table, METRIC, 3);
final byte[] foo_id = { 0, 'a', 0x42 };
final byte[] foo_name = { 'f', 'o', 'o' };
final byte[] bar_id = { 1, 'b', 0x43 };
final byte[] bar_name = { 'b', 'a', 'r' };
ArrayList<KeyValue> foo_kvs = new ArrayList<KeyValue>(1);
ArrayList<KeyValue> bar_kvs = new ArrayList<KeyValue>(1);
foo_kvs.add(new KeyValue(foo_name, ID, METRIC_ARRAY, foo_id));
bar_kvs.add(new KeyValue(bar_name, ID, METRIC_ARRAY, bar_id));
when(client.get(anyGet())).thenReturn(Deferred.fromResult(foo_kvs)).thenReturn(Deferred.fromResult(bar_kvs));
when(client.put(anyPut())).thenAnswer(answerTrue());
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 getOrCreateIdAssignFilterBlocked.
@Test(expected = FailedToAssignUniqueIdException.class)
public void getOrCreateIdAssignFilterBlocked() {
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));
// 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));
uid.getOrCreateId("foo");
}
Aggregations