Search in sources :

Example 1 with RAMCache

use of org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMCache in project hbase by apache.

the class TestRAMCache method testAtomicRAMCache.

@Test
public void testAtomicRAMCache() throws Exception {
    int size = 100;
    int length = HConstants.HFILEBLOCK_HEADER_SIZE + size;
    byte[] byteArr = new byte[length];
    RAMCache cache = new RAMCache();
    BlockCacheKey key = new BlockCacheKey("file-1", 1);
    MockHFileBlock blk = new MockHFileBlock(BlockType.DATA, size, size, -1, ByteBuffer.wrap(byteArr, 0, size), HFileBlock.FILL_HEADER, -1, 52, -1, new HFileContextBuilder().build(), ByteBuffAllocator.HEAP);
    RAMQueueEntry re = new RAMQueueEntry(key, blk, 1, false);
    Assert.assertNull(cache.putIfAbsent(key, re));
    Assert.assertEquals(cache.putIfAbsent(key, re), re);
    CountDownLatch latch = new CountDownLatch(1);
    blk.setLatch(latch);
    AtomicBoolean error = new AtomicBoolean(false);
    Thread t1 = new Thread(() -> {
        try {
            cache.get(key);
        } catch (Exception e) {
            error.set(true);
        }
    });
    t1.start();
    Thread.sleep(200);
    AtomicBoolean removed = new AtomicBoolean(false);
    Thread t2 = new Thread(() -> {
        cache.remove(key);
        removed.set(true);
    });
    t2.start();
    Thread.sleep(200);
    Assert.assertFalse(removed.get());
    latch.countDown();
    Thread.sleep(200);
    Assert.assertTrue(removed.get());
    Assert.assertFalse(error.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RAMQueueEntry(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry) HFileContextBuilder(org.apache.hadoop.hbase.io.hfile.HFileContextBuilder) RAMCache(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMCache) CountDownLatch(java.util.concurrent.CountDownLatch) BlockCacheKey(org.apache.hadoop.hbase.io.hfile.BlockCacheKey) Test(org.junit.Test)

Example 2 with RAMCache

use of org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMCache in project hbase by apache.

the class TestBucketCache method testRAMCache.

@Test
public void testRAMCache() {
    int size = 100;
    int length = HConstants.HFILEBLOCK_HEADER_SIZE + size;
    byte[] byteArr = new byte[length];
    ByteBuffer buf = ByteBuffer.wrap(byteArr, 0, size);
    HFileContext meta = new HFileContextBuilder().build();
    RAMCache cache = new RAMCache();
    BlockCacheKey key1 = new BlockCacheKey("file-1", 1);
    BlockCacheKey key2 = new BlockCacheKey("file-2", 2);
    HFileBlock blk1 = new HFileBlock(BlockType.DATA, size, size, -1, ByteBuff.wrap(buf), HFileBlock.FILL_HEADER, -1, 52, -1, meta, ByteBuffAllocator.HEAP);
    HFileBlock blk2 = new HFileBlock(BlockType.DATA, size, size, -1, ByteBuff.wrap(buf), HFileBlock.FILL_HEADER, -1, -1, -1, meta, ByteBuffAllocator.HEAP);
    RAMQueueEntry re1 = new RAMQueueEntry(key1, blk1, 1, false);
    RAMQueueEntry re2 = new RAMQueueEntry(key1, blk2, 1, false);
    assertFalse(cache.containsKey(key1));
    assertNull(cache.putIfAbsent(key1, re1));
    assertEquals(2, ((HFileBlock) re1.getData()).getBufferReadOnly().refCnt());
    assertNotNull(cache.putIfAbsent(key1, re2));
    assertEquals(2, ((HFileBlock) re1.getData()).getBufferReadOnly().refCnt());
    assertEquals(1, ((HFileBlock) re2.getData()).getBufferReadOnly().refCnt());
    assertNull(cache.putIfAbsent(key2, re2));
    assertEquals(2, ((HFileBlock) re1.getData()).getBufferReadOnly().refCnt());
    assertEquals(2, ((HFileBlock) re2.getData()).getBufferReadOnly().refCnt());
    cache.remove(key1);
    assertEquals(1, ((HFileBlock) re1.getData()).getBufferReadOnly().refCnt());
    assertEquals(2, ((HFileBlock) re2.getData()).getBufferReadOnly().refCnt());
    cache.clear();
    assertEquals(1, ((HFileBlock) re1.getData()).getBufferReadOnly().refCnt());
    assertEquals(1, ((HFileBlock) re2.getData()).getBufferReadOnly().refCnt());
}
Also used : HFileBlock(org.apache.hadoop.hbase.io.hfile.HFileBlock) RAMQueueEntry(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry) HFileContextBuilder(org.apache.hadoop.hbase.io.hfile.HFileContextBuilder) RAMCache(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMCache) ByteBuffer(java.nio.ByteBuffer) BlockCacheKey(org.apache.hadoop.hbase.io.hfile.BlockCacheKey) HFileContext(org.apache.hadoop.hbase.io.hfile.HFileContext) Test(org.junit.Test)

Aggregations

BlockCacheKey (org.apache.hadoop.hbase.io.hfile.BlockCacheKey)2 HFileContextBuilder (org.apache.hadoop.hbase.io.hfile.HFileContextBuilder)2 RAMCache (org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMCache)2 RAMQueueEntry (org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry)2 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 HFileBlock (org.apache.hadoop.hbase.io.hfile.HFileBlock)1 HFileContext (org.apache.hadoop.hbase.io.hfile.HFileContext)1