Java Code Examples for org.apache.flink.runtime.io.network.buffer.NetworkBuffer#readOnlySlice()
The following examples show how to use
org.apache.flink.runtime.io.network.buffer.NetworkBuffer#readOnlySlice() .
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: NettyMessageSerializationTest.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
private void testEncodeDecodeBuffer(boolean testReadOnlyBuffer) { NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(1024), FreeingBufferRecycler.INSTANCE); for (int i = 0; i < 1024; i += 4) { buffer.writeInt(i); } Buffer testBuffer = testReadOnlyBuffer ? buffer.readOnlySlice() : buffer; NettyMessage.BufferResponse expected = new NettyMessage.BufferResponse( testBuffer, random.nextInt(), new InputChannelID(), random.nextInt()); NettyMessage.BufferResponse actual = encodeAndDecode(expected); // Netty 4.1 is not copying the messages, but retaining slices of them. BufferResponse actual is in this case // holding a reference to the buffer. Buffer will be recycled only once "actual" will be released. assertFalse(buffer.isRecycled()); assertFalse(testBuffer.isRecycled()); final ByteBuf retainedSlice = actual.getNettyBuffer(); // Ensure not recycled and same size as original buffer assertEquals(1, retainedSlice.refCnt()); assertEquals(1024, retainedSlice.readableBytes()); for (int i = 0; i < 1024; i += 4) { assertEquals(i, retainedSlice.readInt()); } // Release the retained slice actual.releaseBuffer(); assertEquals(0, retainedSlice.refCnt()); assertTrue(buffer.isRecycled()); assertTrue(testBuffer.isRecycled()); assertEquals(expected.sequenceNumber, actual.sequenceNumber); assertEquals(expected.receiverId, actual.receiverId); assertEquals(expected.backlog, actual.backlog); }
Example 2
Source File: NettyMessageSerializationTest.java From flink with Apache License 2.0 | 5 votes |
private void testEncodeDecodeBuffer(boolean testReadOnlyBuffer) { NetworkBuffer buffer = new NetworkBuffer(MemorySegmentFactory.allocateUnpooledSegment(1024), FreeingBufferRecycler.INSTANCE); for (int i = 0; i < 1024; i += 4) { buffer.writeInt(i); } Buffer testBuffer = testReadOnlyBuffer ? buffer.readOnlySlice() : buffer; NettyMessage.BufferResponse expected = new NettyMessage.BufferResponse( testBuffer, random.nextInt(), new InputChannelID(), random.nextInt()); NettyMessage.BufferResponse actual = encodeAndDecode(expected); // Netty 4.1 is not copying the messages, but retaining slices of them. BufferResponse actual is in this case // holding a reference to the buffer. Buffer will be recycled only once "actual" will be released. assertFalse(buffer.isRecycled()); assertFalse(testBuffer.isRecycled()); final ByteBuf retainedSlice = actual.getNettyBuffer(); // Ensure not recycled and same size as original buffer assertEquals(1, retainedSlice.refCnt()); assertEquals(1024, retainedSlice.readableBytes()); for (int i = 0; i < 1024; i += 4) { assertEquals(i, retainedSlice.readInt()); } // Release the retained slice actual.releaseBuffer(); assertEquals(0, retainedSlice.refCnt()); assertTrue(buffer.isRecycled()); assertTrue(testBuffer.isRecycled()); assertEquals(expected.sequenceNumber, actual.sequenceNumber); assertEquals(expected.receiverId, actual.receiverId); assertEquals(expected.backlog, actual.backlog); }
Example 3
Source File: NettyMessageClientSideSerializationTest.java From flink with Apache License 2.0 | 4 votes |
private void testBufferResponse(boolean testReadOnlyBuffer, boolean testCompressedBuffer) { checkArgument( !(testReadOnlyBuffer & testCompressedBuffer), "There are no cases with both readonly slice and compression."); NetworkBuffer buffer = new NetworkBuffer( MemorySegmentFactory.allocateUnpooledSegment(BUFFER_SIZE), FreeingBufferRecycler.INSTANCE); for (int i = 0; i < BUFFER_SIZE; i += 8) { buffer.writeLong(i); } Buffer testBuffer = buffer; if (testReadOnlyBuffer) { testBuffer = buffer.readOnlySlice(); } else if (testCompressedBuffer) { testBuffer = COMPRESSOR.compressToOriginalBuffer(buffer); } BufferResponse expected = new BufferResponse( testBuffer, random.nextInt(), inputChannelId, random.nextInt()); BufferResponse actual = encodeAndDecode(expected, channel); assertTrue(buffer.isRecycled()); assertTrue(testBuffer.isRecycled()); assertNotNull( "The request input channel should always have available buffers in this test.", actual.getBuffer()); Buffer decodedBuffer = actual.getBuffer(); if (testCompressedBuffer) { assertTrue(actual.isCompressed); decodedBuffer = decompress(decodedBuffer); } verifyBufferResponseHeader(expected, actual); assertEquals(BUFFER_SIZE, decodedBuffer.readableBytes()); for (int i = 0; i < BUFFER_SIZE; i += 8) { assertEquals(i, decodedBuffer.asByteBuf().readLong()); } // Release the received message. actual.releaseBuffer(); if (testCompressedBuffer) { decodedBuffer.recycleBuffer(); } assertTrue(actual.getBuffer().isRecycled()); }