List of usage examples for io.netty.buffer ByteBufAllocator heapBuffer
ByteBuf heapBuffer(int initialCapacity, int maxCapacity);
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testOomWithFallbackAndNoMoreHeap() { ByteBufAllocator baseAlloc = mock(ByteBufAllocator.class); when(baseAlloc.directBuffer(anyInt(), anyInt())).thenThrow(outOfDirectMemException); ByteBufAllocator heapAlloc = mock(ByteBufAllocator.class); OutOfMemoryError noHeapError = new OutOfMemoryError("no more heap"); when(heapAlloc.heapBuffer(anyInt(), anyInt())).thenThrow(noHeapError); AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>(); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().pooledAllocator(baseAlloc) .unpooledAllocator(heapAlloc).outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap) .outOfMemoryListener((e) -> { receivedException.set(e); }).build();/*from w w w. j a va 2 s. c om*/ try { alloc.buffer(); fail("Should have thrown exception"); } catch (OutOfMemoryError e) { // Expected assertEquals(noHeapError, e); } // Ensure the notification was triggered even when exception is thrown assertEquals(noHeapError, receivedException.get()); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorBuilderTest.java
License:Apache License
@Test public void testOomUnpooledWithHeap() { ByteBufAllocator heapAlloc = mock(ByteBufAllocator.class); OutOfMemoryError noHeapError = new OutOfMemoryError("no more heap"); when(heapAlloc.heapBuffer(anyInt(), anyInt())).thenThrow(noHeapError); AtomicReference<OutOfMemoryError> receivedException = new AtomicReference<>(); ByteBufAllocator alloc = ByteBufAllocatorBuilder.create().poolingPolicy(PoolingPolicy.UnpooledHeap) .unpooledAllocator(heapAlloc).outOfMemoryPolicy(OutOfMemoryPolicy.FallbackToHeap) .outOfMemoryListener((e) -> { receivedException.set(e); }).build();//from w w w. java 2 s . com try { alloc.heapBuffer(); fail("Should have thrown exception"); } catch (OutOfMemoryError e) { // Expected assertEquals(noHeapError, e); } // Ensure the notification was triggered even when exception is thrown assertEquals(noHeapError, receivedException.get()); }
From source file:org.apache.bookkeeper.common.allocator.impl.ByteBufAllocatorImpl.java
License:Apache License
@Override protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) { try {//w w w. j av a2s . c o m // There are few cases in which we ask explicitly for a pooled // heap buffer. ByteBufAllocator alloc = (poolingPolicy == PoolingPolicy.PooledDirect) ? pooledAllocator : unpooledAllocator; return alloc.heapBuffer(initialCapacity, maxCapacity); } catch (OutOfMemoryError e) { outOfMemoryListener.accept(e); throw e; } }
From source file:org.apache.bookkeeper.proto.BookieProtoEncoding.java
License:Apache License
private static ByteBuf serializeProtobuf(MessageLite msg, ByteBufAllocator allocator) { int size = msg.getSerializedSize(); ByteBuf buf = allocator.heapBuffer(size, size); try {//from w ww .ja v a 2 s . com msg.writeTo(CodedOutputStream.newInstance(buf.array(), buf.arrayOffset() + buf.writerIndex(), size)); } catch (IOException e) { // This is in-memory serialization, should not fail throw new RuntimeException(e); } // Advance writer idx buf.writerIndex(buf.capacity()); return buf; }