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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readBytes() . 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: PartitionRequestClientHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a deserialized buffer message as it would be received during runtime.
 */
static BufferResponse createBufferResponse(
		Buffer buffer,
		int sequenceNumber,
		InputChannelID receivingChannelId,
		int backlog) throws IOException {

	// Mock buffer to serialize
	BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId, backlog);

	ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT);

	// Skip general header bytes
	serialized.readBytes(NettyMessage.FRAME_HEADER_LENGTH);

	// Deserialize the bytes again. We have to go this way, because we only partly deserialize
	// the header of the response and wait for a buffer from the buffer pool to copy the payload
	// data into.
	BufferResponse deserialized = BufferResponse.readFrom(serialized);

	return deserialized;
}
 
Example 2
Source File: CreditBasedPartitionRequestClientHandlerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a deserialized buffer message as it would be received during runtime.
 */
private static BufferResponse createBufferResponse(
		Buffer buffer,
		int sequenceNumber,
		InputChannelID receivingChannelId,
		int backlog,
		NetworkBufferAllocator allocator) throws IOException {
	// Mock buffer to serialize
	BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId, backlog);

	ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT);

	// Skip general header bytes
	serialized.readBytes(NettyMessage.FRAME_HEADER_LENGTH);

	// Deserialize the bytes to construct the BufferResponse.
	return BufferResponse.readFrom(serialized, allocator);
}
 
Example 3
Source File: PartitionRequestClientHandlerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a deserialized buffer message as it would be received during runtime.
 */
static BufferResponse createBufferResponse(
		Buffer buffer,
		int sequenceNumber,
		InputChannelID receivingChannelId,
		int backlog) throws IOException {

	// Mock buffer to serialize
	BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId, backlog);

	ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT);

	// Skip general header bytes
	serialized.readBytes(NettyMessage.FRAME_HEADER_LENGTH);

	// Deserialize the bytes again. We have to go this way, because we only partly deserialize
	// the header of the response and wait for a buffer from the buffer pool to copy the payload
	// data into.
	BufferResponse deserialized = BufferResponse.readFrom(serialized);

	return deserialized;
}
 
Example 4
Source File: KvStateRequest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateRequest deserializeMessage(ByteBuf buf) {
	JobID jobId = new JobID(buf.readLong(), buf.readLong());

	int statenameLength = buf.readInt();
	Preconditions.checkArgument(statenameLength >= 0,
			"Negative length for state name. " +
					"This indicates a serialization error.");

	String stateName = "";
	if (statenameLength > 0) {
		byte[] name = new byte[statenameLength];
		buf.readBytes(name);
		stateName = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}

	int keyHashCode = buf.readInt();

	int knamespaceLength = buf.readInt();
	Preconditions.checkArgument(knamespaceLength >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[knamespaceLength];
	if (knamespaceLength > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateRequest(jobId, stateName, keyHashCode, serializedKeyAndNamespace);
}
 
Example 5
Source File: KvStateResponse.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateResponse deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for state content. " +
					"This indicates a serialization error.");
	byte[] content = new byte[length];
	buf.readBytes(content);

	return new KvStateResponse(content);
}
 
Example 6
Source File: AbstractServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TestMessage deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	String message = "";
	if (length > 0) {
		byte[] name = new byte[length];
		buf.readBytes(name);
		message = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}
	return new TestMessage(message);
}
 
Example 7
Source File: KvStateInternalRequest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateInternalRequest deserializeMessage(ByteBuf buf) {
	KvStateID kvStateId = new KvStateID(buf.readLong(), buf.readLong());

	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[length];
	if (length > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
}
 
Example 8
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBytes() {
    ByteBuf buffer = newBuffer(8);
    byte[] bytes = new byte[8];
    buffer.writeBytes(bytes);

    ByteBuf buffer2 = buffer.readBytes(4);
    assertSame(buffer.alloc(), buffer2.alloc());
    assertEquals(4, buffer.readerIndex());
    assertTrue(buffer.release());
    assertEquals(0, buffer.refCnt());
    assertTrue(buffer2.release());
    assertEquals(0, buffer2.refCnt());
}
 
Example 9
Source File: KvStateRequest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateRequest deserializeMessage(ByteBuf buf) {
	JobID jobId = new JobID(buf.readLong(), buf.readLong());

	int statenameLength = buf.readInt();
	Preconditions.checkArgument(statenameLength >= 0,
			"Negative length for state name. " +
					"This indicates a serialization error.");

	String stateName = "";
	if (statenameLength > 0) {
		byte[] name = new byte[statenameLength];
		buf.readBytes(name);
		stateName = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}

	int keyHashCode = buf.readInt();

	int knamespaceLength = buf.readInt();
	Preconditions.checkArgument(knamespaceLength >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[knamespaceLength];
	if (knamespaceLength > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateRequest(jobId, stateName, keyHashCode, serializedKeyAndNamespace);
}
 
Example 10
Source File: KvStateResponse.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateResponse deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for state content. " +
					"This indicates a serialization error.");
	byte[] content = new byte[length];
	buf.readBytes(content);

	return new KvStateResponse(content);
}
 
Example 11
Source File: AbstractServerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TestMessage deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	String message = "";
	if (length > 0) {
		byte[] name = new byte[length];
		buf.readBytes(name);
		message = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}
	return new TestMessage(message);
}
 
Example 12
Source File: KvStateInternalRequest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateInternalRequest deserializeMessage(ByteBuf buf) {
	KvStateID kvStateId = new KvStateID(buf.readLong(), buf.readLong());

	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[length];
	if (length > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
}
 
Example 13
Source File: AbstractByteBufTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBytes() {
    ByteBuf buffer = newBuffer(8);
    byte[] bytes = new byte[8];
    buffer.writeBytes(bytes);

    ByteBuf buffer2 = buffer.readBytes(4);
    assertSame(buffer.alloc(), buffer2.alloc());
    assertEquals(4, buffer.readerIndex());
    assertTrue(buffer.release());
    assertEquals(0, buffer.refCnt());
    assertTrue(buffer2.release());
    assertEquals(0, buffer2.refCnt());
}
 
Example 14
Source File: KvStateRequest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateRequest deserializeMessage(ByteBuf buf) {
	JobID jobId = new JobID(buf.readLong(), buf.readLong());

	int statenameLength = buf.readInt();
	Preconditions.checkArgument(statenameLength >= 0,
			"Negative length for state name. " +
					"This indicates a serialization error.");

	String stateName = "";
	if (statenameLength > 0) {
		byte[] name = new byte[statenameLength];
		buf.readBytes(name);
		stateName = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}

	int keyHashCode = buf.readInt();

	int knamespaceLength = buf.readInt();
	Preconditions.checkArgument(knamespaceLength >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[knamespaceLength];
	if (knamespaceLength > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateRequest(jobId, stateName, keyHashCode, serializedKeyAndNamespace);
}
 
Example 15
Source File: KvStateResponse.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateResponse deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for state content. " +
					"This indicates a serialization error.");
	byte[] content = new byte[length];
	buf.readBytes(content);

	return new KvStateResponse(content);
}
 
Example 16
Source File: AbstractServerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public TestMessage deserializeMessage(ByteBuf buf) {
	int length = buf.readInt();
	String message = "";
	if (length > 0) {
		byte[] name = new byte[length];
		buf.readBytes(name);
		message = new String(name, ConfigConstants.DEFAULT_CHARSET);
	}
	return new TestMessage(message);
}
 
Example 17
Source File: KvStateInternalRequest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public KvStateInternalRequest deserializeMessage(ByteBuf buf) {
	KvStateID kvStateId = new KvStateID(buf.readLong(), buf.readLong());

	int length = buf.readInt();
	Preconditions.checkArgument(length >= 0,
			"Negative length for key and namespace. " +
					"This indicates a serialization error.");

	byte[] serializedKeyAndNamespace = new byte[length];
	if (length > 0) {
		buf.readBytes(serializedKeyAndNamespace);
	}
	return new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
}
 
Example 18
Source File: AbstractByteBufTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadBytes() {
    ByteBuf buffer = newBuffer(8);
    byte[] bytes = new byte[8];
    buffer.writeBytes(bytes);

    ByteBuf buffer2 = buffer.readBytes(4);
    assertSame(buffer.alloc(), buffer2.alloc());
    assertEquals(4, buffer.readerIndex());
    assertTrue(buffer.release());
    assertEquals(0, buffer.refCnt());
    assertTrue(buffer2.release());
    assertEquals(0, buffer2.refCnt());
}
 
Example 19
Source File: PartitionRequestClientHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Continues the decoding of a staged buffer after a buffer has become available again.
 *
 * <p>This task is executed by the network I/O thread.
 */
@Override
public void run() {
	boolean success = false;

	Buffer buffer = null;

	try {
		if ((buffer = availableBuffer.getAndSet(null)) == null) {
			throw new IllegalStateException("Running buffer availability task w/o a buffer.");
		}

		ByteBuf nettyBuffer = stagedBufferResponse.getNettyBuffer();
		nettyBuffer.readBytes(buffer.asByteBuf(), nettyBuffer.readableBytes());
		stagedBufferResponse.releaseBuffer();

		RemoteInputChannel inputChannel = inputChannels.get(stagedBufferResponse.receiverId);

		if (inputChannel != null) {
			inputChannel.onBuffer(buffer, stagedBufferResponse.sequenceNumber, -1);

			success = true;
		}
		else {
			cancelRequestFor(stagedBufferResponse.receiverId);
		}

		stagedBufferResponse = null;

		if (stagedMessages.isEmpty()) {
			ctx.channel().config().setAutoRead(true);
			ctx.channel().read();
		}
		else {
			ctx.channel().eventLoop().execute(stagedMessagesHandler);
		}
	}
	catch (Throwable t) {
		notifyAllChannelsOfErrorAndClose(t);
	}
	finally {
		if (!success) {
			if (buffer != null) {
				buffer.recycleBuffer();
			}
		}
	}
}
 
Example 20
Source File: PartitionRequestClientHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Continues the decoding of a staged buffer after a buffer has become available again.
 *
 * <p>This task is executed by the network I/O thread.
 */
@Override
public void run() {
	boolean success = false;

	Buffer buffer = null;

	try {
		if ((buffer = availableBuffer.getAndSet(null)) == null) {
			throw new IllegalStateException("Running buffer availability task w/o a buffer.");
		}

		ByteBuf nettyBuffer = stagedBufferResponse.getNettyBuffer();
		nettyBuffer.readBytes(buffer.asByteBuf(), nettyBuffer.readableBytes());
		stagedBufferResponse.releaseBuffer();

		RemoteInputChannel inputChannel = inputChannels.get(stagedBufferResponse.receiverId);

		if (inputChannel != null) {
			inputChannel.onBuffer(buffer, stagedBufferResponse.sequenceNumber, -1);

			success = true;
		}
		else {
			cancelRequestFor(stagedBufferResponse.receiverId);
		}

		stagedBufferResponse = null;

		if (stagedMessages.isEmpty()) {
			ctx.channel().config().setAutoRead(true);
			ctx.channel().read();
		}
		else {
			ctx.channel().eventLoop().execute(stagedMessagesHandler);
		}
	}
	catch (Throwable t) {
		notifyAllChannelsOfErrorAndClose(t);
	}
	finally {
		if (!success) {
			if (buffer != null) {
				buffer.recycleBuffer();
			}
		}
	}
}