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

The following examples show how to use org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf#readInt() . 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: MessageSerializerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests response serialization with zero-length serialized result.
 */
@Test
public void testResponseSerializationWithZeroLengthSerializedResult() throws Exception {
	byte[] serializedResult = new byte[0];

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

	ByteBuf buf = MessageSerializer.serializeResponse(alloc, 72727278L, response);

	int frameLength = buf.readInt();

	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	assertEquals(72727278L, MessageSerializer.getRequestId(buf));
	KvStateResponse responseDeser = serializer.deserializeResponse(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertArrayEquals(serializedResult, responseDeser.getContent());
}
 
Example 2
Source File: NettyMessageClientDecoderDelegate.java    From flink with Apache License 2.0 6 votes vote down vote up
private void decodeFrameHeader(ByteBuf data) {
	ByteBuf fullFrameHeaderBuf = ByteBufUtils.accumulate(
		frameHeaderBuffer,
		data,
		FRAME_HEADER_LENGTH,
		frameHeaderBuffer.readableBytes());

	if (fullFrameHeaderBuf != null) {
		int messageAndFrameLength = fullFrameHeaderBuf.readInt();
		checkState(messageAndFrameLength >= 0, "The length field of current message must be non-negative");

		int magicNumber = fullFrameHeaderBuf.readInt();
		checkState(magicNumber == MAGIC_NUMBER, "Network stream corrupted: received incorrect magic number.");

		int msgId = fullFrameHeaderBuf.readByte();
		if (msgId == NettyMessage.BufferResponse.ID) {
			currentDecoder = bufferResponseDecoder;
		} else {
			currentDecoder = nonBufferResponseDecoder;
		}

		currentDecoder.onNewMessageReceived(msgId, messageAndFrameLength - FRAME_HEADER_LENGTH);
	}
}
 
Example 3
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests response serialization with zero-length serialized result.
 */
@Test
public void testResponseSerializationWithZeroLengthSerializedResult() throws Exception {
	byte[] serializedResult = new byte[0];

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

	ByteBuf buf = MessageSerializer.serializeResponse(alloc, 72727278L, response);

	int frameLength = buf.readInt();

	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	assertEquals(72727278L, MessageSerializer.getRequestId(buf));
	KvStateResponse responseDeser = serializer.deserializeResponse(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertArrayEquals(serializedResult, responseDeser.getContent());
}
 
Example 4
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request failure serialization.
 */
@Test
public void testKvStateRequestFailureSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 1111222L;
	IllegalStateException cause = new IllegalStateException("Expected test");

	ByteBuf buf = MessageSerializer.serializeRequestFailure(alloc, requestId, cause);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
	RequestFailure requestFailure = MessageSerializer.deserializeRequestFailure(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(requestId, requestFailure.getRequestId());
	assertEquals(cause.getClass(), requestFailure.getCause().getClass());
	assertEquals(cause.getMessage(), requestFailure.getCause().getMessage());
}
 
Example 5
Source File: NettyMessage.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
static TaskEventRequest readFrom(ByteBuf buffer, ClassLoader classLoader) throws IOException {
	// directly deserialize fromNetty's buffer
	int length = buffer.readInt();
	ByteBuffer serializedEvent = buffer.nioBuffer(buffer.readerIndex(), length);
	// assume this event's content is read from the ByteBuf (positions are not shared!)
	buffer.readerIndex(buffer.readerIndex() + length);

	TaskEvent event =
		(TaskEvent) EventSerializer.fromSerializedEvent(serializedEvent, classLoader);

	ResultPartitionID partitionId =
		new ResultPartitionID(
			IntermediateResultPartitionID.fromByteBuf(buffer),
			ExecutionAttemptID.fromByteBuf(buffer));

	InputChannelID receiverId = InputChannelID.fromByteBuf(buffer);

	return new TaskEventRequest(event, partitionId, receiverId);
}
 
Example 6
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request failure serialization.
 */
@Test
public void testKvStateRequestFailureSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 1111222L;
	IllegalStateException cause = new IllegalStateException("Expected test");

	ByteBuf buf = MessageSerializer.serializeRequestFailure(alloc, requestId, cause);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
	RequestFailure requestFailure = MessageSerializer.deserializeRequestFailure(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(requestId, requestFailure.getRequestId());
	assertEquals(cause.getClass(), requestFailure.getCause().getClass());
	assertEquals(cause.getMessage(), requestFailure.getCause().getMessage());
}
 
Example 7
Source File: NettyMessage.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the message header part and composes a new BufferResponse with an empty data buffer. The
 * data buffer will be filled in later.
 *
 * @param messageHeader the serialized message header.
 * @param bufferAllocator the allocator for network buffer.
 * @return a BufferResponse object with the header parsed and the data buffer to fill in later. The
 *			data buffer will be null if the target channel has been released or the buffer size is 0.
 */
static BufferResponse readFrom(ByteBuf messageHeader, NetworkBufferAllocator bufferAllocator) {
	InputChannelID receiverId = InputChannelID.fromByteBuf(messageHeader);
	int sequenceNumber = messageHeader.readInt();
	int backlog = messageHeader.readInt();
	Buffer.DataType dataType = Buffer.DataType.values()[messageHeader.readByte()];
	boolean isCompressed = messageHeader.readBoolean();
	int size = messageHeader.readInt();

	Buffer dataBuffer = null;

	if (size != 0) {
		if (dataType.isBuffer()) {
			dataBuffer = bufferAllocator.allocatePooledNetworkBuffer(receiverId);
		} else {
			dataBuffer = bufferAllocator.allocateUnPooledNetworkBuffer(size, dataType);
		}
	}

	if (dataBuffer != null) {
		dataBuffer.setCompressed(isCompressed);
	}

	return new BufferResponse(
		dataBuffer,
		dataType,
		isCompressed,
		sequenceNumber,
		receiverId,
		backlog,
		size);
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: MessageSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * De-serializes the header and returns the {@link MessageType}.
 * <pre>
 *  <b>The buffer is expected to be at the header position.</b>
 * </pre>
 * @param buf						The {@link ByteBuf} containing the serialized header.
 * @return							The message type.
 * @throws IllegalStateException	If unexpected message version or message type.
 */
public static MessageType deserializeHeader(final ByteBuf buf) {

	// checking the version
	int version = buf.readInt();
	Preconditions.checkState(version == VERSION,
			"Version Mismatch:  Found " + version + ", Expected: " + VERSION + '.');

	// fetching the message type
	int msgType = buf.readInt();
	MessageType[] values = MessageType.values();
	Preconditions.checkState(msgType >= 0 && msgType < values.length,
			"Illegal message type with index " + msgType + '.');
	return values[msgType];
}
 
Example 13
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests server failure serialization.
 */
@Test
public void testServerFailureSerialization() throws Exception {
	IllegalStateException cause = new IllegalStateException("Expected test");

	ByteBuf buf = MessageSerializer.serializeServerFailure(alloc, cause);

	int frameLength = buf.readInt();
	assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
	Throwable request = MessageSerializer.deserializeServerFailure(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(cause.getClass(), request.getClass());
	assertEquals(cause.getMessage(), request.getMessage());
}
 
Example 14
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 15
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 16
Source File: NettyMessage.java    From flink with Apache License 2.0 5 votes vote down vote up
static AddCredit readFrom(ByteBuf buffer) {
	ResultPartitionID partitionId =
		new ResultPartitionID(
			IntermediateResultPartitionID.fromByteBuf(buffer),
			ExecutionAttemptID.fromByteBuf(buffer));
	int credit = buffer.readInt();
	InputChannelID receiverId = InputChannelID.fromByteBuf(buffer);

	return new AddCredit(partitionId, credit, receiverId);
}
 
Example 17
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests server failure serialization.
 */
@Test
public void testServerFailureSerialization() throws Exception {
	IllegalStateException cause = new IllegalStateException("Expected test");

	ByteBuf buf = MessageSerializer.serializeServerFailure(alloc, cause);

	int frameLength = buf.readInt();
	assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
	Throwable request = MessageSerializer.deserializeServerFailure(buf);
	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(cause.getClass(), request.getClass());
	assertEquals(cause.getMessage(), request.getMessage());
}
 
Example 18
Source File: NettyMessage.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static BufferResponse readFrom(ByteBuf buffer) {
	InputChannelID receiverId = InputChannelID.fromByteBuf(buffer);
	int sequenceNumber = buffer.readInt();
	int backlog = buffer.readInt();
	boolean isBuffer = buffer.readBoolean();
	int size = buffer.readInt();

	ByteBuf retainedSlice = buffer.readSlice(size).retain();
	return new BufferResponse(retainedSlice, isBuffer, sequenceNumber, receiverId, backlog);
}
 
Example 19
Source File: NettyMessage.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
	ByteBuf msg = (ByteBuf) super.decode(ctx, in);
	if (msg == null) {
		return null;
	}

	try {
		int magicNumber = msg.readInt();

		if (magicNumber != MAGIC_NUMBER) {
			throw new IllegalStateException(
				"Network stream corrupted: received incorrect magic number.");
		}

		byte msgId = msg.readByte();

		final NettyMessage decodedMsg;
		switch (msgId) {
			case PartitionRequest.ID:
				decodedMsg = PartitionRequest.readFrom(msg);
				break;
			case TaskEventRequest.ID:
				decodedMsg = TaskEventRequest.readFrom(msg, getClass().getClassLoader());
				break;
			case CancelPartitionRequest.ID:
				decodedMsg = CancelPartitionRequest.readFrom(msg);
				break;
			case CloseRequest.ID:
				decodedMsg = CloseRequest.readFrom(msg);
				break;
			case AddCredit.ID:
				decodedMsg = AddCredit.readFrom(msg);
				break;
			case ResumeConsumption.ID:
				decodedMsg = ResumeConsumption.readFrom(msg);
				break;
			default:
				throw new ProtocolException(
					"Received unknown message from producer: " + msg);
		}

		return decodedMsg;
	} finally {
		// ByteToMessageDecoder cleanup (only the BufferResponse holds on to the decoded
		// msg but already retain()s the buffer once)
		msg.release();
	}
}
 
Example 20
Source File: NettyMessage.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
	ByteBuf msg = (ByteBuf) super.decode(ctx, in);
	if (msg == null) {
		return null;
	}

	try {
		int magicNumber = msg.readInt();

		if (magicNumber != MAGIC_NUMBER) {
			throw new IllegalStateException(
				"Network stream corrupted: received incorrect magic number.");
		}

		byte msgId = msg.readByte();

		final NettyMessage decodedMsg;
		switch (msgId) {
			case BufferResponse.ID:
				decodedMsg = BufferResponse.readFrom(msg);
				break;
			case PartitionRequest.ID:
				decodedMsg = PartitionRequest.readFrom(msg);
				break;
			case TaskEventRequest.ID:
				decodedMsg = TaskEventRequest.readFrom(msg, getClass().getClassLoader());
				break;
			case ErrorResponse.ID:
				decodedMsg = ErrorResponse.readFrom(msg);
				break;
			case CancelPartitionRequest.ID:
				decodedMsg = CancelPartitionRequest.readFrom(msg);
				break;
			case CloseRequest.ID:
				decodedMsg = CloseRequest.readFrom(msg);
				break;
			case AddCredit.ID:
				decodedMsg = AddCredit.readFrom(msg);
				break;
			default:
				throw new ProtocolException(
					"Received unknown message from producer: " + msg);
		}

		return decodedMsg;
	} finally {
		// ByteToMessageDecoder cleanup (only the BufferResponse holds on to the decoded
		// msg but already retain()s the buffer once)
		msg.release();
	}
}