use of org.apache.flink.runtime.query.netty.message.KvStateRequestType 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 org.apache.flink.runtime.query.netty.message.KvStateRequestType in project flink by apache.
the class KvStateServerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
KvStateRequest request = null;
try {
ByteBuf buf = (ByteBuf) msg;
KvStateRequestType msgType = KvStateRequestSerializer.deserializeHeader(buf);
if (msgType == KvStateRequestType.REQUEST) {
// ------------------------------------------------------------
// Request
// ------------------------------------------------------------
request = KvStateRequestSerializer.deserializeKvStateRequest(buf);
stats.reportRequest();
InternalKvState<?> kvState = registry.getKvState(request.getKvStateId());
if (kvState != null) {
// Execute actual query async, because it is possibly
// blocking (e.g. file I/O).
//
// A submission failure is not treated as fatal.
queryExecutor.submit(new AsyncKvStateQueryTask(ctx, request, kvState, stats));
} else {
ByteBuf unknown = KvStateRequestSerializer.serializeKvStateRequestFailure(ctx.alloc(), request.getRequestId(), new UnknownKvStateID(request.getKvStateId()));
ctx.writeAndFlush(unknown);
stats.reportFailedRequest();
}
} else {
// ------------------------------------------------------------
// Unexpected
// ------------------------------------------------------------
ByteBuf failure = KvStateRequestSerializer.serializeServerFailure(ctx.alloc(), new IllegalArgumentException("Unexpected message type " + msgType + ". KvStateServerHandler expects " + KvStateRequestType.REQUEST + " messages."));
ctx.writeAndFlush(failure);
}
} catch (Throwable t) {
String stringifiedCause = ExceptionUtils.stringifyException(t);
ByteBuf err;
if (request != null) {
String errMsg = "Failed to handle incoming request with ID " + request.getRequestId() + ". Caused by: " + stringifiedCause;
err = KvStateRequestSerializer.serializeKvStateRequestFailure(ctx.alloc(), request.getRequestId(), new RuntimeException(errMsg));
stats.reportFailedRequest();
} else {
String errMsg = "Failed to handle incoming message. Caused by: " + stringifiedCause;
err = KvStateRequestSerializer.serializeServerFailure(ctx.alloc(), new RuntimeException(errMsg));
}
ctx.writeAndFlush(err);
} finally {
// IMPORTANT: We have to always recycle the incoming buffer.
// Otherwise we will leak memory out of Netty's buffer pool.
//
// If any operation ever holds on to the buffer, it is the
// responsibility of that operation to retain the buffer and
// release it later.
ReferenceCountUtil.release(msg);
}
}
Aggregations