Java Code Examples for org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#release()

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyMessage.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
ByteBuf write(ByteBufAllocator allocator) throws IOException {
	ByteBuf result = null;

	try {
		result = allocateBuffer(allocator, ID, 16 + 16 + 4 + 16);

		partitionId.getPartitionId().writeTo(result);
		partitionId.getProducerId().writeTo(result);
		result.writeInt(credit);
		receiverId.writeTo(result);

		return result;
	}
	catch (Throwable t) {
		if (result != null) {
			result.release();
		}

		throw new IOException(t);
	}
}
 
Example 2
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
private void testGetReadOnlyDst(boolean direct) {
    byte[] bytes = { 'a', 'b', 'c', 'd' };

    ByteBuf buffer = newBuffer(bytes.length);
    buffer.writeBytes(bytes);

    ByteBuffer dst = direct ? ByteBuffer.allocateDirect(bytes.length) : ByteBuffer.allocate(bytes.length);
    ByteBuffer readOnlyDst = dst.asReadOnlyBuffer();
    try {
        buffer.getBytes(0, readOnlyDst);
        fail();
    } catch (ReadOnlyBufferException e) {
        // expected
    }
    assertEquals(0, readOnlyDst.position());
    buffer.release();
}
 
Example 3
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * The similar test case with {@link #testDiscardReadBytes()} but this one
 * discards a large chunk at once.
 */
@Test
public void testDiscardReadBytes2() {
    buffer.writerIndex(0);
    for (int i = 0; i < buffer.capacity(); i ++) {
        buffer.writeByte((byte) i);
    }
    ByteBuf copy = copiedBuffer(buffer);

    // Discard the first (CAPACITY / 2 - 1) bytes.
    buffer.setIndex(CAPACITY / 2 - 1, CAPACITY - 1);
    buffer.discardReadBytes();
    assertEquals(0, buffer.readerIndex());
    assertEquals(CAPACITY / 2, buffer.writerIndex());
    for (int i = 0; i < CAPACITY / 2; i ++) {
        assertEquals(copy.slice(CAPACITY / 2 - 1 + i, CAPACITY / 2 - i), buffer.slice(i, CAPACITY / 2 - i));
    }
    copy.release();
}
 
Example 4
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCapacityIncrease() {
    ByteBuf buffer = newBuffer(3, 13);
    assertEquals(13, buffer.maxCapacity());
    assertEquals(3, buffer.capacity());
    try {
        buffer.capacity(4);
        assertEquals(4, buffer.capacity());
        assertEquals(13, buffer.maxCapacity());
    } finally {
        buffer.release();
    }
}
 
Example 5
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalReferenceCountException.class)
public void testReadBytesAfterRelease3() {
    ByteBuf buffer = buffer(8);
    try {
        releasedBuffer().readBytes(buffer);
    } finally {
        buffer.release();
    }
}
 
Example 6
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalReferenceCountException.class)
public void testReadBytesAfterRelease4() {
    ByteBuf buffer = buffer(8);
    try {
        releasedBuffer().readBytes(buffer, 0, 1);
    } finally {
        buffer.release();
    }
}
 
Example 7
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSliceReleaseOriginal(boolean retainedSlice1, boolean retainedSlice2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected1 = newBuffer(3).resetWriterIndex();
    ByteBuf expected2 = newBuffer(2).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected1.writeBytes(new byte[] {6, 7, 8});
    expected2.writeBytes(new byte[] {7, 8});
    ByteBuf slice1 = retainedSlice1 ? buf.retainedSlice(buf.readerIndex() + 5, 3)
                                    : buf.slice(buf.readerIndex() + 5, 3).retain();
    assertEquals(0, slice1.compareTo(expected1));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf slice2 = retainedSlice2 ? slice1.retainedSlice(slice1.readerIndex() + 1, 2)
                                    : slice1.slice(slice1.readerIndex() + 1, 2).retain();
    assertEquals(0, slice2.compareTo(expected2));

    // Cleanup the expected buffers used for testing.
    assertTrue(expected1.release());
    assertTrue(expected2.release());

    // The handler created a slice of the slice and is now done with it.
    slice2.release();

    // The handler is now done with the original slice
    assertTrue(slice1.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, slice1.refCnt());
    assertEquals(0, slice2.refCnt());
}
 
Example 8
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalReferenceCountException.class)
public void testReadBytesAfterRelease4() {
    ByteBuf buffer = buffer(8);
    try {
        releasedBuffer().readBytes(buffer, 0, 1);
    } finally {
        buffer.release();
    }
}
 
Example 9
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testDuplicateCapacityChange(boolean retainedDuplicate) {
    ByteBuf buf = newBuffer(8);
    ByteBuf dup = retainedDuplicate ? buf.retainedDuplicate() : buf.duplicate();
    try {
        dup.capacity(10);
        assertEquals(buf.capacity(), dup.capacity());
        dup.capacity(5);
        assertEquals(buf.capacity(), dup.capacity());
    } finally {
        if (retainedDuplicate) {
            dup.release();
        }
        buf.release();
    }
}
 
Example 10
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testSetGetCharSequence(Charset charset) {
    ByteBuf buf = newBuffer(1024);
    CharBuffer sequence = CharsetUtil.US_ASCII.equals(charset)
            ? ASCII_CHARS : EXTENDED_ASCII_CHARS;
    int bytes = buf.setCharSequence(1, sequence, charset);
    assertEquals(sequence, CharBuffer.wrap(buf.getCharSequence(1, bytes, charset)));
    buf.release();
}
 
Example 11
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testDuplicateReleaseOriginal(boolean retainedDuplicate1, boolean retainedDuplicate2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected = newBuffer(8).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected.writeBytes(buf, buf.readerIndex(), buf.readableBytes());
    ByteBuf dup1 = retainedDuplicate1 ? buf.retainedDuplicate()
                                      : buf.duplicate().retain();
    assertEquals(0, dup1.compareTo(expected));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf dup2 = retainedDuplicate2 ? dup1.retainedDuplicate()
                                      : dup1.duplicate().retain();
    assertEquals(0, dup2.compareTo(expected));

    // Cleanup the expected buffers used for testing.
    assertTrue(expected.release());

    // The handler created a slice of the slice and is now done with it.
    dup2.release();

    // The handler is now done with the original slice
    assertTrue(dup1.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, dup1.refCnt());
    assertEquals(0, dup2.refCnt());
}
 
Example 12
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() {
    ByteBuf copied = copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1);
    buffer.clear();
    buffer.writeBytes(copied);
    assertEquals("Hello, World!", buffer.toString(CharsetUtil.ISO_8859_1));
    copied.release();
}
 
Example 13
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalReferenceCountException.class)
public void testSetBytesAfterRelease2() {
    ByteBuf buffer = buffer();
    try {
        releasedBuffer().setBytes(0, buffer, 1);
    } finally {
        buffer.release();
    }
}
 
Example 14
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testCapacityNegative() {
    ByteBuf buffer = newBuffer(3, 13);
    assertEquals(13, buffer.maxCapacity());
    assertEquals(3, buffer.capacity());
    try {
        buffer.capacity(-1);
    } finally {
        buffer.release();
    }
}
 
Example 15
Source File: NettyMessage.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
ByteBuf write(ByteBufAllocator allocator) throws IOException {
	final ByteBuf result = allocateBuffer(allocator, ID);

	try (ObjectOutputStream oos = new ObjectOutputStream(new ByteBufOutputStream(result))) {
		oos.writeObject(cause);

		if (receiverId != null) {
			result.writeBoolean(true);
			receiverId.writeTo(result);
		} else {
			result.writeBoolean(false);
		}

		// Update frame length...
		result.setInt(0, result.readableBytes());
		return result;
	}
	catch (Throwable t) {
		result.release();

		if (t instanceof IOException) {
			throw (IOException) t;
		} else {
			throw new IOException(t);
		}
	}
}
 
Example 16
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testRetainedDuplicateUnreleasable(boolean initRetainedDuplicate, boolean finalRetainedDuplicate) {
    ByteBuf buf = newBuffer(8);
    ByteBuf buf1 = initRetainedDuplicate ? buf.retainedDuplicate() : buf.duplicate().retain();
    ByteBuf buf2 = unreleasableBuffer(buf1);
    ByteBuf buf3 = finalRetainedDuplicate ? buf2.retainedDuplicate() : buf2.duplicate().retain();
    assertFalse(buf3.release());
    assertFalse(buf2.release());
    buf1.release();
    assertTrue(buf.release());
    assertEquals(0, buf1.refCnt());
    assertEquals(0, buf.refCnt());
}
 
Example 17
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the failure response with {@link UnknownKeyOrNamespaceException} as cause
 * on queries for non-existing keys.
 */
@Test
public void testQueryUnknownKey() throws Exception {
	KvStateRegistry registry = new KvStateRegistry();
	AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();

	MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	KvStateServerHandler handler = new KvStateServerHandler(testServer, registry, serializer, stats);
	EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);

	int numKeyGroups = 1;
	AbstractStateBackend abstractBackend = new MemoryStateBackend();
	DummyEnvironment dummyEnv = new DummyEnvironment("test", 1, 0);
	dummyEnv.setKvStateRegistry(registry);
	KeyedStateBackend<Integer> backend = createKeyedStateBackend(registry, numKeyGroups, abstractBackend, dummyEnv);

	final TestRegistryListener registryListener = new TestRegistryListener();
	registry.registerListener(dummyEnv.getJobID(), registryListener);

	// Register state
	ValueStateDescriptor<Integer> desc = new ValueStateDescriptor<>("any", IntSerializer.INSTANCE);
	desc.setQueryable("vanilla");

	backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, desc);

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			1238283,
			IntSerializer.INSTANCE,
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE);

	long requestId = Integer.MAX_VALUE + 22982L;

	assertTrue(registryListener.registrationName.equals("vanilla"));

	KvStateInternalRequest request = new KvStateInternalRequest(registryListener.kvStateId, serializedKeyAndNamespace);
	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);

	// Write the request and wait for the response
	channel.writeInbound(serRequest);

	ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
	buf.skipBytes(4); // skip frame length

	// Verify the response
	assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
	RequestFailure response = MessageSerializer.deserializeRequestFailure(buf);
	buf.release();

	assertEquals(requestId, response.getRequestId());

	assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKeyOrNamespaceException);

	assertEquals(1L, stats.getNumRequests());
	assertEquals(1L, stats.getNumFailed());
}
 
Example 18
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testMultipleRetainedSliceReleaseOriginal(boolean retainedSlice1, boolean retainedSlice2) {
    ByteBuf buf = newBuffer(8).resetWriterIndex();
    ByteBuf expected1 = newBuffer(3).resetWriterIndex();
    ByteBuf expected2 = newBuffer(2).resetWriterIndex();
    ByteBuf expected3 = newBuffer(2).resetWriterIndex();
    buf.writeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
    expected1.writeBytes(new byte[] {6, 7, 8});
    expected2.writeBytes(new byte[] {7, 8});
    expected3.writeBytes(new byte[] {6, 7});
    ByteBuf slice1 = retainedSlice1 ? buf.retainedSlice(buf.readerIndex() + 5, 3)
                                    : buf.slice(buf.readerIndex() + 5, 3).retain();
    assertEquals(0, slice1.compareTo(expected1));
    // Simulate a handler that releases the original buffer, and propagates a slice.
    buf.release();

    ByteBuf slice2 = retainedSlice2 ? slice1.retainedSlice(slice1.readerIndex() + 1, 2)
                                    : slice1.slice(slice1.readerIndex() + 1, 2).retain();
    assertEquals(0, slice2.compareTo(expected2));

    // The handler created a slice of the slice and is now done with it.
    slice2.release();

    ByteBuf slice3 = slice1.retainedSlice(slice1.readerIndex(), 2);
    assertEquals(0, slice3.compareTo(expected3));

    // The handler created another slice of the slice and is now done with it.
    slice3.release();

    // The handler is now done with the original slice
    assertTrue(slice1.release());

    // Cleanup the expected buffers used for testing.
    assertTrue(expected1.release());
    assertTrue(expected2.release());
    assertTrue(expected3.release());

    // Reference counting may be shared, or may be independently tracked, but at this point all buffers should
    // be deallocated and have a reference count of 0.
    assertEquals(0, buf.refCnt());
    assertEquals(0, slice1.refCnt());
    assertEquals(0, slice2.refCnt());
    assertEquals(0, slice3.refCnt());
}
 
Example 19
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testRefCnt0(final boolean parameter) throws Exception {
    for (int i = 0; i < 10; i++) {
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch innerLatch = new CountDownLatch(1);

        final ByteBuf buffer = newBuffer(4);
        assertEquals(1, buffer.refCnt());
        final AtomicInteger cnt = new AtomicInteger(Integer.MAX_VALUE);
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean released;
                if (parameter) {
                    released = buffer.release(buffer.refCnt());
                } else {
                    released = buffer.release();
                }
                assertTrue(released);
                Thread t2 = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        cnt.set(buffer.refCnt());
                        latch.countDown();
                    }
                });
                t2.start();
                try {
                    // Keep Thread alive a bit so the ThreadLocal caches are not freed
                    innerLatch.await();
                } catch (InterruptedException ignore) {
                    // ignore
                }
            }
        });
        t1.start();

        latch.await();
        assertEquals(0, cnt.get());
        innerLatch.countDown();
    }
}
 
Example 20
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private void testBytesInArrayMultipleThreads(final boolean slice) throws Exception {
    final byte[] bytes = new byte[8];
    random.nextBytes(bytes);

    final ByteBuf buffer = newBuffer(8);
    buffer.writeBytes(bytes);
    final AtomicReference<Throwable> cause = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(60000);
    final CyclicBarrier barrier = new CyclicBarrier(11);
    for (int i = 0; i < 10; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (cause.get() == null && latch.getCount() > 0) {
                    ByteBuf buf;
                    if (slice) {
                        buf = buffer.slice();
                    } else {
                        buf = buffer.duplicate();
                    }

                    byte[] array = new byte[8];
                    buf.readBytes(array);

                    assertArrayEquals(bytes, array);

                    Arrays.fill(array, (byte) 0);
                    buf.getBytes(0, array);
                    assertArrayEquals(bytes, array);

                    latch.countDown();
                }
                try {
                    barrier.await();
                } catch (Exception e) {
                    // ignore
                }
            }
        }).start();
    }
    latch.await(10, TimeUnit.SECONDS);
    barrier.await(5, TimeUnit.SECONDS);
    assertNull(cause.get());
    buffer.release();
}