Search in sources :

Example 1 with MessageType

use of org.apache.flink.queryablestate.network.messages.MessageType in project flink by apache.

the class AbstractServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    REQ request = null;
    long requestId = UNKNOWN_REQUEST_ID;
    try {
        final ByteBuf buf = (ByteBuf) msg;
        final MessageType msgType = MessageSerializer.deserializeHeader(buf);
        requestId = MessageSerializer.getRequestId(buf);
        LOG.trace("Handling request with ID {}", requestId);
        if (msgType == MessageType.REQUEST) {
            // ------------------------------------------------------------
            // MessageBody
            // ------------------------------------------------------------
            request = serializer.deserializeRequest(buf);
            stats.reportRequest();
            // 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 AsyncRequestTask<>(this, ctx, requestId, request, stats));
        } else {
            // ------------------------------------------------------------
            // Unexpected
            // ------------------------------------------------------------
            final String errMsg = "Unexpected message type " + msgType + ". Expected " + MessageType.REQUEST + ".";
            final ByteBuf failure = MessageSerializer.serializeServerFailure(ctx.alloc(), new IllegalArgumentException(errMsg));
            LOG.debug(errMsg);
            ctx.writeAndFlush(failure);
        }
    } catch (Throwable t) {
        LOG.error("Error while handling request with ID [{}]", requestId == UNKNOWN_REQUEST_ID ? "unknown" : requestId, t);
        final String stringifiedCause = ExceptionUtils.stringifyException(t);
        String errMsg;
        ByteBuf err;
        if (request != null) {
            errMsg = "Failed request with ID " + requestId + ". Caused by: " + stringifiedCause;
            err = MessageSerializer.serializeRequestFailure(ctx.alloc(), requestId, new RuntimeException(errMsg));
            stats.reportFailedRequest();
        } else {
            errMsg = "Failed incoming message. Caused by: " + stringifiedCause;
            err = MessageSerializer.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);
    }
}
Also used : ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) MessageType(org.apache.flink.queryablestate.network.messages.MessageType)

Example 2 with MessageType

use of org.apache.flink.queryablestate.network.messages.MessageType in project flink by apache.

the class ClientHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
        ByteBuf buf = (ByteBuf) msg;
        MessageType msgType = MessageSerializer.deserializeHeader(buf);
        if (msgType == MessageType.REQUEST_RESULT) {
            long requestId = MessageSerializer.getRequestId(buf);
            RESP result = serializer.deserializeResponse(buf);
            callback.onRequestResult(requestId, result);
        } else if (msgType == MessageType.REQUEST_FAILURE) {
            RequestFailure failure = MessageSerializer.deserializeRequestFailure(buf);
            callback.onRequestFailure(failure.getRequestId(), failure.getCause());
        } else if (msgType == MessageType.SERVER_FAILURE) {
            throw MessageSerializer.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);
    }
}
Also used : RequestFailure(org.apache.flink.queryablestate.network.messages.RequestFailure) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) MessageType(org.apache.flink.queryablestate.network.messages.MessageType)

Aggregations

MessageType (org.apache.flink.queryablestate.network.messages.MessageType)2 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)2 RequestFailure (org.apache.flink.queryablestate.network.messages.RequestFailure)1