Java Code Examples for org.apache.flink.queryablestate.network.messages.MessageSerializer#serializeServerFailure()

The following examples show how to use org.apache.flink.queryablestate.network.messages.MessageSerializer#serializeServerFailure() . 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 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 2
Source File: AbstractServerHandler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example 3
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 4
Source File: AbstractServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example 5
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 6
Source File: AbstractServerHandler.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
	final String msg = "Exception in server pipeline. Caused by: " + ExceptionUtils.stringifyException(cause);
	final ByteBuf err = MessageSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(msg));

	LOG.debug(msg);
	ctx.writeAndFlush(err).addListener(ChannelFutureListener.CLOSE);
}
 
Example 7
Source File: KvStateClientHandlerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that on reads the expected callback methods are called and read
 * buffers are recycled.
 */
@Test
public void testReadCallbacksAndBufferRecycling() throws Exception {
	final ClientHandlerCallback<KvStateResponse> callback = mock(ClientHandlerCallback.class);

	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
	final EmbeddedChannel channel = new EmbeddedChannel(new ClientHandler<>("Test Client", serializer, callback));

	final byte[] content = new byte[0];
	final KvStateResponse response = new KvStateResponse(content);

	//
	// Request success
	//
	ByteBuf buf = MessageSerializer.serializeResponse(channel.alloc(), 1222112277L, response);
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestResult(eq(1222112277L), any(KvStateResponse.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Request failure
	//
	buf = MessageSerializer.serializeRequestFailure(
			channel.alloc(),
			1222112278,
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestFailure(eq(1222112278L), isA(RuntimeException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Server failure
	//
	buf = MessageSerializer.serializeServerFailure(
			channel.alloc(),
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(RuntimeException.class));

	//
	// Unexpected messages
	//
	buf = channel.alloc().buffer(4).writeInt(1223823);

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(IllegalStateException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Exception caught
	//
	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
	verify(callback, times(3)).onFailure(isA(RuntimeException.class));

	//
	// Channel inactive
	//
	channel.pipeline().fireChannelInactive();
	verify(callback, times(1)).onFailure(isA(ClosedChannelException.class));
}
 
Example 8
Source File: KvStateClientHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that on reads the expected callback methods are called and read
 * buffers are recycled.
 */
@Test
public void testReadCallbacksAndBufferRecycling() throws Exception {
	final ClientHandlerCallback<KvStateResponse> callback = mock(ClientHandlerCallback.class);

	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
	final EmbeddedChannel channel = new EmbeddedChannel(new ClientHandler<>("Test Client", serializer, callback));

	final byte[] content = new byte[0];
	final KvStateResponse response = new KvStateResponse(content);

	//
	// Request success
	//
	ByteBuf buf = MessageSerializer.serializeResponse(channel.alloc(), 1222112277L, response);
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestResult(eq(1222112277L), any(KvStateResponse.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Request failure
	//
	buf = MessageSerializer.serializeRequestFailure(
			channel.alloc(),
			1222112278,
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestFailure(eq(1222112278L), isA(RuntimeException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Server failure
	//
	buf = MessageSerializer.serializeServerFailure(
			channel.alloc(),
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(RuntimeException.class));

	//
	// Unexpected messages
	//
	buf = channel.alloc().buffer(4).writeInt(1223823);

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(IllegalStateException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Exception caught
	//
	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
	verify(callback, times(3)).onFailure(isA(RuntimeException.class));

	//
	// Channel inactive
	//
	channel.pipeline().fireChannelInactive();
	verify(callback, times(1)).onFailure(isA(ClosedChannelException.class));
}
 
Example 9
Source File: KvStateClientHandlerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that on reads the expected callback methods are called and read
 * buffers are recycled.
 */
@Test
public void testReadCallbacksAndBufferRecycling() throws Exception {
	final ClientHandlerCallback<KvStateResponse> callback = mock(ClientHandlerCallback.class);

	final MessageSerializer<KvStateInternalRequest, KvStateResponse> serializer =
			new MessageSerializer<>(new KvStateInternalRequest.KvStateInternalRequestDeserializer(), new KvStateResponse.KvStateResponseDeserializer());
	final EmbeddedChannel channel = new EmbeddedChannel(new ClientHandler<>("Test Client", serializer, callback));

	final byte[] content = new byte[0];
	final KvStateResponse response = new KvStateResponse(content);

	//
	// Request success
	//
	ByteBuf buf = MessageSerializer.serializeResponse(channel.alloc(), 1222112277L, response);
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestResult(eq(1222112277L), any(KvStateResponse.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Request failure
	//
	buf = MessageSerializer.serializeRequestFailure(
			channel.alloc(),
			1222112278,
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onRequestFailure(eq(1222112278L), isA(RuntimeException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Server failure
	//
	buf = MessageSerializer.serializeServerFailure(
			channel.alloc(),
			new RuntimeException("Expected test Exception"));
	buf.skipBytes(4); // skip frame length

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(RuntimeException.class));

	//
	// Unexpected messages
	//
	buf = channel.alloc().buffer(4).writeInt(1223823);

	// Verify callback
	channel.writeInbound(buf);
	verify(callback, times(1)).onFailure(isA(IllegalStateException.class));
	assertEquals("Buffer not recycled", 0, buf.refCnt());

	//
	// Exception caught
	//
	channel.pipeline().fireExceptionCaught(new RuntimeException("Expected test Exception"));
	verify(callback, times(3)).onFailure(isA(RuntimeException.class));

	//
	// Channel inactive
	//
	channel.pipeline().fireChannelInactive();
	verify(callback, times(1)).onFailure(isA(ClosedChannelException.class));
}