Search in sources :

Example 36 with KeyValue

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));
}
Also used : KeyValue(org.hbase.async.KeyValue) GetRequest(org.hbase.async.GetRequest) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with KeyValue

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");
}
Also used : KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with KeyValue

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());
}
Also used : KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) HBaseException(org.hbase.async.HBaseException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with KeyValue

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");
}
Also used : KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with KeyValue

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");
}
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)

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