org.apache.flink.queryablestate.network.messages.MessageSerializer Java Examples

The following examples show how to use org.apache.flink.queryablestate.network.messages.MessageSerializer. 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: Client.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an established connection with the given channel.
 *
 * @param serverAddress Address of the server connected to
 * @param channel The actual TCP channel
 */
EstablishedConnection(
		final InetSocketAddress serverAddress,
		final MessageSerializer<REQ, RESP> serializer,
		final Channel channel) {

	this.serverAddress = Preconditions.checkNotNull(serverAddress);
	this.channel = Preconditions.checkNotNull(channel);

	// Add the client handler with the callback
	channel.pipeline().addLast(
			getClientName() + " Handler",
			new ClientHandler<>(clientName, serializer, this)
	);

	stats.reportActiveConnection();
}
 
Example #2
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization.
 */
@Test
public void testRequestSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = randomByteArray(1024);

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #3
Source File: AbstractServerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public AbstractServerHandler<TestMessage, TestMessage> initializeHandler() {
	return new AbstractServerHandler<TestMessage, TestMessage>(
			this,
			new MessageSerializer<>(new TestMessage.TestMessageDeserializer(), new TestMessage.TestMessageDeserializer()),
			requestStats) {

		@Override
		public CompletableFuture<TestMessage> handleRequest(long requestId, TestMessage request) {
			TestMessage response = new TestMessage(getServerName() + '-' + request.getMessage());
			return CompletableFuture.completedFuture(response);
		}

		@Override
		public CompletableFuture<Void> shutdown() {
			return CompletableFuture.completedFuture(null);
		}
	};
}
 
Example #4
Source File: MessageSerializerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization with zero-length serialized key and namespace.
 */
@Test
public void testRequestSerializationWithZeroLengthKeyAndNamespace() throws Exception {

	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = new byte[0];

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #5
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 #6
Source File: MessageSerializerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization.
 */
@Test
public void testRequestSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = randomByteArray(1024);

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #7
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization with zero-length serialized key and namespace.
 */
@Test
public void testRequestSerializationWithZeroLengthKeyAndNamespace() throws Exception {

	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = new byte[0];

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #8
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests response serialization.
 */
@Test
public void testResponseSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 72727278L;
	byte[] serializedResult = randomByteArray(1024);

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

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

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateResponse responseDeser = serializer.deserializeResponse(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertArrayEquals(serializedResult, responseDeser.getContent());
}
 
Example #9
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 #10
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 #11
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization.
 */
@Test
public void testRequestSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = randomByteArray(1024);

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #12
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests request serialization with zero-length serialized key and namespace.
 */
@Test
public void testRequestSerializationWithZeroLengthKeyAndNamespace() throws Exception {

	long requestId = Integer.MAX_VALUE + 1337L;
	KvStateID kvStateId = new KvStateID();
	byte[] serializedKeyAndNamespace = new byte[0];

	final KvStateInternalRequest request = new KvStateInternalRequest(kvStateId, serializedKeyAndNamespace);
	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());

	ByteBuf buf = MessageSerializer.serializeRequest(alloc, requestId, request);

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateInternalRequest requestDeser = serializer.deserializeRequest(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertEquals(kvStateId, requestDeser.getKvStateId());
	assertArrayEquals(serializedKeyAndNamespace, requestDeser.getSerializedKeyAndNamespace());
}
 
Example #13
Source File: MessageSerializerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests response serialization.
 */
@Test
public void testResponseSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 72727278L;
	byte[] serializedResult = randomByteArray(1024);

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

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

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateResponse responseDeser = serializer.deserializeResponse(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertArrayEquals(serializedResult, responseDeser.getContent());
}
 
Example #14
Source File: AbstractServerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public AbstractServerHandler<TestMessage, TestMessage> initializeHandler() {
	return new AbstractServerHandler<TestMessage, TestMessage>(
			this,
			new MessageSerializer<>(new TestMessage.TestMessageDeserializer(), new TestMessage.TestMessageDeserializer()),
			requestStats) {

		@Override
		public CompletableFuture<TestMessage> handleRequest(long requestId, TestMessage request) {
			TestMessage response = new TestMessage(getServerName() + '-' + request.getMessage());
			return CompletableFuture.completedFuture(response);
		}

		@Override
		public CompletableFuture<Void> shutdown() {
			return CompletableFuture.completedFuture(null);
		}
	};
}
 
Example #15
Source File: AbstractServerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public AbstractServerHandler<TestMessage, TestMessage> initializeHandler() {
	return new AbstractServerHandler<TestMessage, TestMessage>(
			this,
			new MessageSerializer<>(new TestMessage.TestMessageDeserializer(), new TestMessage.TestMessageDeserializer()),
			requestStats) {

		@Override
		public CompletableFuture<TestMessage> handleRequest(long requestId, TestMessage request) {
			TestMessage response = new TestMessage(getServerName() + '-' + request.getMessage());
			return CompletableFuture.completedFuture(response);
		}

		@Override
		public CompletableFuture<Void> shutdown() {
			return CompletableFuture.completedFuture(null);
		}
	};
}
 
Example #16
Source File: QueryableStateClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the Queryable State Client.
 * @param remoteAddress the {@link InetAddress address} of the {@code Client Proxy} to connect to.
 * @param remotePort the port of the proxy to connect to.
 */
public QueryableStateClient(final InetAddress remoteAddress, final int remotePort) {
	Preconditions.checkArgument(NetUtils.isValidHostPort(remotePort),
			"Remote Port " + remotePort + " is out of valid port range [0-65535].");

	this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort);

	final MessageSerializer<KvStateRequest, KvStateResponse> messageSerializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());

	this.client = new Client<>(
			"Queryable State Client",
			1,
			messageSerializer,
			new DisabledKvStateRequestStats());
}
 
Example #17
Source File: Client.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an established connection with the given channel.
 *
 * @param serverAddress Address of the server connected to
 * @param channel The actual TCP channel
 */
EstablishedConnection(
		final InetSocketAddress serverAddress,
		final MessageSerializer<REQ, RESP> serializer,
		final Channel channel) {

	this.serverAddress = Preconditions.checkNotNull(serverAddress);
	this.channel = Preconditions.checkNotNull(channel);

	// Add the client handler with the callback
	channel.pipeline().addLast(
			getClientName() + " Handler",
			new ClientHandler<>(clientName, serializer, this)
	);

	stats.reportActiveConnection();
}
 
Example #18
Source File: Client.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an established connection with the given channel.
 *
 * @param serverAddress Address of the server connected to
 * @param channel The actual TCP channel
 */
EstablishedConnection(
		final InetSocketAddress serverAddress,
		final MessageSerializer<REQ, RESP> serializer,
		final Channel channel) {

	this.serverAddress = Preconditions.checkNotNull(serverAddress);
	this.channel = Preconditions.checkNotNull(channel);

	// Add the client handler with the callback
	channel.pipeline().addLast(
			getClientName() + " Handler",
			new ClientHandler<>(clientName, serializer, this)
	);

	stats.reportActiveConnection();
}
 
Example #19
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the channel is closed if an Exception reaches the channel handler.
 */
@Test
public void testCloseChannelOnExceptionCaught() 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(handler);

	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));

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

	// Verify the response
	assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
	Throwable response = MessageSerializer.deserializeServerFailure(buf);

	assertTrue(response.getMessage().contains("Expected test Exception"));

	channel.closeFuture().await(READ_TIMEOUT_MILLIS);
	assertFalse(channel.isActive());
}
 
Example #20
Source File: QueryableStateClient.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the Queryable State Client.
 * @param remoteAddress the {@link InetAddress address} of the {@code Client Proxy} to connect to.
 * @param remotePort the port of the proxy to connect to.
 */
public QueryableStateClient(final InetAddress remoteAddress, final int remotePort) {
	Preconditions.checkArgument(remotePort >= 0 && remotePort <= 65536,
			"Remote Port " + remotePort + " is out of valid port range (0-65536).");

	this.remoteAddress = new InetSocketAddress(remoteAddress, remotePort);

	final MessageSerializer<KvStateRequest, KvStateResponse> messageSerializer =
			new MessageSerializer<>(
					new KvStateRequest.KvStateRequestDeserializer(),
					new KvStateResponse.KvStateResponseDeserializer());

	this.client = new Client<>(
			"Queryable State Client",
			1,
			messageSerializer,
			new DisabledKvStateRequestStats());
}
 
Example #21
Source File: KvStateServerHandlerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the channel is closed if an Exception reaches the channel handler.
 */
@Test
public void testCloseChannelOnExceptionCaught() 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(handler);

	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));

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

	// Verify the response
	assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
	Throwable response = MessageSerializer.deserializeServerFailure(buf);

	assertTrue(response.getMessage().contains("Expected test Exception"));

	channel.closeFuture().await(READ_TIMEOUT_MILLIS);
	assertFalse(channel.isActive());
}
 
Example #22
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 #23
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 #24
Source File: MessageSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests response serialization.
 */
@Test
public void testResponseSerialization() throws Exception {
	long requestId = Integer.MAX_VALUE + 72727278L;
	byte[] serializedResult = randomByteArray(1024);

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

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

	int frameLength = buf.readInt();
	assertEquals(MessageType.REQUEST_RESULT, MessageSerializer.deserializeHeader(buf));
	assertEquals(requestId, MessageSerializer.getRequestId(buf));
	KvStateResponse responseDeser = serializer.deserializeResponse(buf);

	assertEquals(buf.readerIndex(), frameLength + 4);

	assertArrayEquals(serializedResult, responseDeser.getContent());
}
 
Example #25
Source File: MessageSerializerTest.java    From Flink-CEPplus 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 #26
Source File: Client.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a pending connection to the given server.
 *
 * @param serverAddress Address of the server to connect to.
 */
private PendingConnection(
		final InetSocketAddress serverAddress,
		final MessageSerializer<REQ, RESP> serializer) {
	this.serverAddress = serverAddress;
	this.serializer = serializer;
}
 
Example #27
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the channel is closed if an Exception reaches the channel handler.
 */
@Test
public void testCloseChannelOnExceptionCaught() 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(handler);

	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));

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

	// Verify the response
	assertEquals(MessageType.SERVER_FAILURE, MessageSerializer.deserializeHeader(buf));
	Throwable response = MessageSerializer.deserializeServerFailure(buf);
	buf.release();

	assertTrue(response.getMessage().contains("Expected test Exception"));

	channel.closeFuture().await(READ_TIMEOUT_MILLIS);
	assertFalse(channel.isActive());
}
 
Example #28
Source File: KvStateClientProxyHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create the handler used by the {@link KvStateClientProxyImpl}.
 *
 * @param proxy the {@link KvStateClientProxyImpl proxy} using the handler.
 * @param queryExecutorThreads the number of threads used to process incoming requests.
 * @param serializer the {@link MessageSerializer} used to (de-) serialize the different messages.
 * @param stats server statistics collector.
 */
public KvStateClientProxyHandler(
		final KvStateClientProxyImpl proxy,
		final int queryExecutorThreads,
		final MessageSerializer<KvStateRequest, KvStateResponse> serializer,
		final KvStateRequestStats stats) {

	super(proxy, serializer, stats);
	this.proxy = Preconditions.checkNotNull(proxy);
	this.kvStateClient = createInternalClient(queryExecutorThreads);
}
 
Example #29
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 #30
Source File: KvStateServerHandlerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that incoming buffer instances are recycled.
 */
@Test
public void testIncomingBufferIsRecycled() 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);

	KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);
	ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), 282872L, request);

	assertEquals(1L, serRequest.refCnt());

	// Write regular request
	channel.writeInbound(serRequest);
	assertEquals("Buffer not recycled", 0L, serRequest.refCnt());

	// Write unexpected msg
	ByteBuf unexpected = channel.alloc().buffer(8);
	unexpected.writeInt(4);
	unexpected.writeInt(4);

	assertEquals(1L, unexpected.refCnt());

	channel.writeInbound(unexpected);
	assertEquals("Buffer not recycled", 0L, unexpected.refCnt());
}