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());
}
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());
}
Aggregations