Search in sources :

Example 1 with RAMQueueEntry

use of org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry 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 RAMQueueEntry

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

the class TestBucketWriterThread method testCacheFullException.

/**
 * Do Cache full exception
 * @throws IOException
 * @throws InterruptedException
 */
@Test
public void testCacheFullException() throws IOException, InterruptedException {
    this.bc.cacheBlock(this.plainKey, plainCacheable);
    RAMQueueEntry rqe = q.remove();
    RAMQueueEntry spiedRqe = Mockito.spy(rqe);
    final CacheFullException cfe = new CacheFullException(0, 0);
    BucketEntry mockedBucketEntry = Mockito.mock(BucketEntry.class);
    Mockito.doThrow(cfe).doReturn(mockedBucketEntry).when(spiedRqe).writeToCache(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    this.q.add(spiedRqe);
    doDrainOfOneEntry(bc, wt, q);
}
Also used : RAMQueueEntry(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry) Test(org.junit.Test)

Example 3 with RAMQueueEntry

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

the class TestBucketWriterThread method testIOE.

/**
 * Do IOE. Take the RAMQueueEntry that was on the queue, doctor it to throw exception, then
 * put it back and process it.
 * @throws IOException
 * @throws InterruptedException
 */
@SuppressWarnings("unchecked")
@Test
public void testIOE() throws IOException, InterruptedException {
    this.bc.cacheBlock(this.plainKey, plainCacheable);
    RAMQueueEntry rqe = q.remove();
    RAMQueueEntry spiedRqe = Mockito.spy(rqe);
    Mockito.doThrow(new IOException("Mocked!")).when(spiedRqe).writeToCache(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
    this.q.add(spiedRqe);
    doDrainOfOneEntry(bc, wt, q);
    // Cache disabled when ioes w/o ever healing.
    assertTrue(!bc.isCacheEnabled());
}
Also used : RAMQueueEntry(org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with RAMQueueEntry

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

the class TestBucketCache method testFreeBlockWhenIOEngineWriteFailure.

@Test
public void testFreeBlockWhenIOEngineWriteFailure() throws IOException {
    // initialize an block.
    int size = 100, offset = 20;
    int length = HConstants.HFILEBLOCK_HEADER_SIZE + size;
    ByteBuffer buf = ByteBuffer.allocate(length);
    HFileContext meta = new HFileContextBuilder().build();
    HFileBlock block = new HFileBlock(BlockType.DATA, size, size, -1, ByteBuff.wrap(buf), HFileBlock.FILL_HEADER, offset, 52, -1, meta, ByteBuffAllocator.HEAP);
    // initialize an mocked ioengine.
    IOEngine ioEngine = Mockito.mock(IOEngine.class);
    Mockito.when(ioEngine.usesSharedMemory()).thenReturn(false);
    // Mockito.doNothing().when(ioEngine).write(Mockito.any(ByteBuffer.class), Mockito.anyLong());
    Mockito.doThrow(RuntimeException.class).when(ioEngine).write(Mockito.any(ByteBuffer.class), Mockito.anyLong());
    Mockito.doThrow(RuntimeException.class).when(ioEngine).write(Mockito.any(ByteBuff.class), Mockito.anyLong());
    // create an bucket allocator.
    long availableSpace = 1024 * 1024 * 1024L;
    BucketAllocator allocator = new BucketAllocator(availableSpace, null);
    BlockCacheKey key = new BlockCacheKey("dummy", 1L);
    RAMQueueEntry re = new RAMQueueEntry(key, block, 1, true);
    Assert.assertEquals(0, allocator.getUsedSize());
    try {
        re.writeToCache(ioEngine, allocator, null, null, ByteBuffer.allocate(HFileBlock.BLOCK_METADATA_SPACE));
        Assert.fail();
    } catch (Exception e) {
    }
    Assert.assertEquals(0, allocator.getUsedSize());
}
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) ByteBuff(org.apache.hadoop.hbase.nio.ByteBuff) ByteBuffer(java.nio.ByteBuffer) BlockCacheKey(org.apache.hadoop.hbase.io.hfile.BlockCacheKey) IOException(java.io.IOException) HFileContext(org.apache.hadoop.hbase.io.hfile.HFileContext) Test(org.junit.Test)

Example 5 with RAMQueueEntry

use of org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.RAMQueueEntry 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

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