Search in sources :

Example 1 with HTableFactory

use of org.apache.phoenix.hbase.index.table.HTableFactory in project phoenix by apache.

the class TestCachingHTableFactory method testCacheCorrectlyExpiresTable.

@Test
public void testCacheCorrectlyExpiresTable() throws Exception {
    // setup the mocks for the tables we will request
    HTableFactory delegate = Mockito.mock(HTableFactory.class);
    RegionCoprocessorEnvironment e = Mockito.mock(RegionCoprocessorEnvironment.class);
    Configuration conf = new Configuration();
    Mockito.when(e.getConfiguration()).thenReturn(conf);
    Mockito.when(e.getSharedData()).thenReturn(new ConcurrentHashMap<String, Object>());
    ImmutableBytesPtr t1 = new ImmutableBytesPtr(Bytes.toBytes("t1"));
    ImmutableBytesPtr t2 = new ImmutableBytesPtr(Bytes.toBytes("t2"));
    ImmutableBytesPtr t3 = new ImmutableBytesPtr(Bytes.toBytes("t3"));
    HTableInterface table1 = Mockito.mock(HTableInterface.class);
    HTableInterface table2 = Mockito.mock(HTableInterface.class);
    HTableInterface table3 = Mockito.mock(HTableInterface.class);
    // setup our factory with a cache size of 2
    CachingHTableFactory factory = new CachingHTableFactory(delegate, 2, e);
    Mockito.when(delegate.getTable(t1, factory.getPool())).thenReturn(table1);
    Mockito.when(delegate.getTable(t2, factory.getPool())).thenReturn(table2);
    Mockito.when(delegate.getTable(t3, factory.getPool())).thenReturn(table3);
    HTableInterface ft1 = factory.getTable(t1);
    HTableInterface ft2 = factory.getTable(t2);
    ft1.close();
    HTableInterface ft3 = factory.getTable(t3);
    // get the same table a second time, after it has gone out of cache
    factory.getTable(t1);
    Mockito.verify(delegate, Mockito.times(2)).getTable(t1, factory.getPool());
    Mockito.verify(delegate, Mockito.times(1)).getTable(t2, factory.getPool());
    Mockito.verify(delegate, Mockito.times(1)).getTable(t3, factory.getPool());
    Mockito.verify(table1).close();
}
Also used : RegionCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment) Configuration(org.apache.hadoop.conf.Configuration) CachingHTableFactory(org.apache.phoenix.hbase.index.table.CachingHTableFactory) CachingHTableFactory(org.apache.phoenix.hbase.index.table.CachingHTableFactory) HTableFactory(org.apache.phoenix.hbase.index.table.HTableFactory) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Test(org.junit.Test)

Aggregations

Configuration (org.apache.hadoop.conf.Configuration)1 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)1 RegionCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment)1 CachingHTableFactory (org.apache.phoenix.hbase.index.table.CachingHTableFactory)1 HTableFactory (org.apache.phoenix.hbase.index.table.HTableFactory)1 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)1 Test (org.junit.Test)1