use of org.apache.flink.queryablestate.network.messages.MessageSerializer in project flink by apache.
the class KvStateServerHandlerTest method testQueryUnknownKvStateID.
/**
* Tests the failure response with {@link UnknownKvStateIdException} as cause on queries for
* unregistered KvStateIDs.
*/
@Test
public void testQueryUnknownKvStateID() 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);
long requestId = Integer.MAX_VALUE + 182828L;
KvStateInternalRequest request = new KvStateInternalRequest(new KvStateID(), new byte[0]);
ByteBuf serRequest = MessageSerializer.serializeRequest(channel.alloc(), requestId, request);
// Write the request and wait for the response
channel.writeInbound(serRequest);
ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
// skip frame length
buf.skipBytes(4);
// Verify the response
assertEquals(MessageType.REQUEST_FAILURE, MessageSerializer.deserializeHeader(buf));
RequestFailure response = MessageSerializer.deserializeRequestFailure(buf);
buf.release();
assertEquals(requestId, response.getRequestId());
assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateIdException);
assertEquals(1L, stats.getNumRequests());
assertEquals(1L, stats.getNumFailed());
}
use of org.apache.flink.queryablestate.network.messages.MessageSerializer in project flink by apache.
the class KvStateServerHandlerTest method testIncomingBufferIsRecycled.
/**
* 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());
channel.finishAndReleaseAll();
}
use of org.apache.flink.queryablestate.network.messages.MessageSerializer in project flink by apache.
the class MessageSerializerTest method testResponseSerializationWithZeroLengthSerializedResult.
/**
* 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());
}
use of org.apache.flink.queryablestate.network.messages.MessageSerializer in project flink by apache.
the class MessageSerializerTest method testRequestSerialization.
/**
* 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());
}
use of org.apache.flink.queryablestate.network.messages.MessageSerializer in project flink by apache.
the class MessageSerializerTest method testRequestSerializationWithZeroLengthKeyAndNamespace.
/**
* 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());
}
Aggregations