use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequestFailure.
/**
* Allocates a buffer and serializes the KvState request failure into it.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param requestId ID of the request responding to
* @param cause Failure cause
* @return Serialized KvState request failure message
* @throws IOException Serialization failures are forwarded
*/
public static ByteBuf serializeKvStateRequestFailure(ByteBufAllocator alloc, long requestId, Throwable cause) throws IOException {
ByteBuf buf = alloc.ioBuffer();
// Frame length is set at the end
buf.writeInt(0);
writeHeader(buf, KvStateRequestType.REQUEST_FAILURE);
// Message
buf.writeLong(requestId);
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 org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequest.
// ------------------------------------------------------------------------
// Serialization
// ------------------------------------------------------------------------
/**
* Allocates a buffer and serializes the KvState request into it.
*
* @param alloc ByteBuf allocator for the buffer to
* serialize message into
* @param requestId ID for this request
* @param kvStateId ID of the requested KvState instance
* @param serializedKeyAndNamespace Serialized key and namespace to request
* from the KvState instance.
* @return Serialized KvState request message
*/
public static ByteBuf serializeKvStateRequest(ByteBufAllocator alloc, long requestId, KvStateID kvStateId, byte[] serializedKeyAndNamespace) {
// Header + request ID + KvState ID + Serialized namespace
int frameLength = HEADER_LENGTH + 8 + (8 + 8) + (4 + serializedKeyAndNamespace.length);
// +4 for frame length
ByteBuf buf = alloc.ioBuffer(frameLength + 4);
buf.writeInt(frameLength);
writeHeader(buf, KvStateRequestType.REQUEST);
buf.writeLong(requestId);
buf.writeLong(kvStateId.getLowerPart());
buf.writeLong(kvStateId.getUpperPart());
buf.writeInt(serializedKeyAndNamespace.length);
buf.writeBytes(serializedKeyAndNamespace);
return buf;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project flink by apache.
the class KvStateRequestSerializer method serializeKvStateRequestResult.
/**
* Allocates a buffer and serializes the KvState request result into it.
*
* @param alloc ByteBuf allocator for the buffer to serialize message into
* @param requestId ID for this request
* @param serializedResult Serialized Result
* @return Serialized KvState request result message
*/
public static ByteBuf serializeKvStateRequestResult(ByteBufAllocator alloc, long requestId, byte[] serializedResult) {
Preconditions.checkNotNull(serializedResult, "Serialized result");
// Header + request ID + serialized result
int frameLength = HEADER_LENGTH + 8 + 4 + serializedResult.length;
ByteBuf buf = alloc.ioBuffer(frameLength);
buf.writeInt(frameLength);
writeHeader(buf, KvStateRequestType.REQUEST_RESULT);
buf.writeLong(requestId);
buf.writeInt(serializedResult.length);
buf.writeBytes(serializedResult);
return buf;
}
use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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);
}
}
Aggregations