use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeServerFailure.
/**
* Allocates a buffer and serializes the server failure into it.
*
* <p>The cause must not be or contain any user types as causes.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param cause Failure cause
* @return Serialized server failure message
* @throws IOException Serialization failures are forwarded
*/
public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException {
ByteBuf buf = alloc.ioBuffer();
// Frame length is set at end
buf.writeInt(0);
writeHeader(buf, KvStateRequestType.SERVER_FAILURE);
try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
ObjectOutputStream out = new ObjectOutputStream(bbos)) {
out.writeObject(cause);
}
// Set frame length
int frameLength = buf.readableBytes() - 4;
buf.setInt(0, frameLength);
return buf;
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateClientHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
ByteBuf buf = (ByteBuf) msg;
KvStateRequestType msgType = KvStateRequestSerializer.deserializeHeader(buf);
if (msgType == KvStateRequestType.REQUEST_RESULT) {
KvStateRequestResult result = KvStateRequestSerializer.deserializeKvStateRequestResult(buf);
callback.onRequestResult(result.getRequestId(), result.getSerializedResult());
} else if (msgType == KvStateRequestType.REQUEST_FAILURE) {
KvStateRequestFailure failure = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
callback.onRequestFailure(failure.getRequestId(), failure.getCause());
} else if (msgType == KvStateRequestType.SERVER_FAILURE) {
throw KvStateRequestSerializer.deserializeServerFailure(buf);
} else {
throw new IllegalStateException("Unexpected response type '" + msgType + "'");
}
} catch (Throwable t1) {
try {
callback.onFailure(t1);
} catch (Throwable t2) {
LOG.error("Failed to notify callback about failure", t2);
}
} finally {
ReferenceCountUtil.release(msg);
}
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class PartitionRequestClientHandlerTest method createBufferResponse.
/**
* Returns a deserialized buffer message as it would be received during runtime.
*/
private BufferResponse createBufferResponse(Buffer buffer, int sequenceNumber, InputChannelID receivingChannelId) throws IOException {
// Mock buffer to serialize
BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId);
ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT);
// Skip general header bytes
serialized.readBytes(NettyMessage.HEADER_LENGTH);
BufferResponse deserialized = new BufferResponse();
// 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.
deserialized.readFrom(serialized);
return deserialized;
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateServerHandlerTest method testQueryUnknownKvStateID.
/**
* Tests the failure response with {@link UnknownKvStateID} as cause on
* queries for unregistered KvStateIDs.
*/
@Test
public void testQueryUnknownKvStateID() throws Exception {
KvStateRegistry registry = new KvStateRegistry();
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
long requestId = Integer.MAX_VALUE + 182828L;
ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), requestId, new KvStateID(), new byte[0]);
// Write the request and wait for the response
channel.writeInbound(request);
ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
// skip frame length
buf.skipBytes(4);
// Verify the response
assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
assertEquals(requestId, response.getRequestId());
assertTrue("Did not respond with expected failure cause", response.getCause() instanceof UnknownKvStateID);
assertEquals(1, stats.getNumRequests());
assertEquals(1, stats.getNumFailed());
}
use of io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateServerHandlerTest method testFailureOnGetSerializedValue.
/**
* Tests the failure response on a failure on the {@link InternalKvState#getSerializedValue(byte[])}
* call.
*/
@Test
public void testFailureOnGetSerializedValue() throws Exception {
KvStateRegistry registry = new KvStateRegistry();
AtomicKvStateRequestStats stats = new AtomicKvStateRequestStats();
KvStateServerHandler handler = new KvStateServerHandler(registry, TEST_THREAD_POOL, stats);
EmbeddedChannel channel = new EmbeddedChannel(getFrameDecoder(), handler);
// Failing KvState
InternalKvState<?> kvState = mock(InternalKvState.class);
when(kvState.getSerializedValue(any(byte[].class))).thenThrow(new RuntimeException("Expected test Exception"));
KvStateID kvStateId = registry.registerKvState(new JobID(), new JobVertexID(), new KeyGroupRange(0, 0), "vanilla", kvState);
ByteBuf request = KvStateRequestSerializer.serializeKvStateRequest(channel.alloc(), 282872, kvStateId, new byte[0]);
// Write the request and wait for the response
channel.writeInbound(request);
ByteBuf buf = (ByteBuf) readInboundBlocking(channel);
// skip frame length
buf.skipBytes(4);
// Verify the response
assertEquals(KvStateRequestType.REQUEST_FAILURE, KvStateRequestSerializer.deserializeHeader(buf));
KvStateRequestFailure response = KvStateRequestSerializer.deserializeKvStateRequestFailure(buf);
assertTrue(response.getCause().getMessage().contains("Expected test Exception"));
assertEquals(1, stats.getNumRequests());
assertEquals(1, stats.getNumFailed());
}
Aggregations