Search in sources :

Example 6 with RpcException

use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.

the class HuluRpcProtocol method decodeResponse.

@Override
public Response decodeResponse(Object packet, ChannelHandlerContext ctx) throws Exception {
    HuluRpcDecodePacket responsePacket = (HuluRpcDecodePacket) packet;
    ByteBuf metaBuf = responsePacket.getMetaBuf();
    ByteBuf protoAndAttachmentBuf = responsePacket.getProtoAndAttachmentBuf();
    ByteBuf protoBuf = null;
    try {
        RpcResponse rpcResponse = new RpcResponse();
        HuluRpcProto.HuluRpcResponseMeta responseMeta = (HuluRpcProto.HuluRpcResponseMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcResponseMetaInstance);
        Long correlationId = responseMeta.getCorrelationId();
        rpcResponse.setCorrelationId(correlationId);
        ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
        RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
        if (future == null) {
            return rpcResponse;
        }
        rpcResponse.setRpcFuture(future);
        int compressType = responseMeta.getCompressType();
        rpcResponse.setCompressType(compressType);
        try {
            if (responseMeta != null && responseMeta.getErrorCode() == 0) {
                Compress compress = compressManager.getCompress(compressType);
                int userMessageSize = responseMeta.getUserMessageSize();
                if (userMessageSize > 0) {
                    protoBuf = protoAndAttachmentBuf.readSlice(userMessageSize);
                } else {
                    protoBuf = protoAndAttachmentBuf;
                }
                Object responseProto = compress.uncompressOutput(protoBuf, future.getRpcMethodInfo());
                rpcResponse.setResult(responseProto);
                // attachment
                if (responseMeta.getUserMessageSize() > 0) {
                    rpcResponse.setBinaryAttachment(protoAndAttachmentBuf);
                    responsePacket.setProtoAndAttachmentBuf(null);
                }
            } else {
                rpcResponse.setException(new RpcException(RpcException.SERVICE_EXCEPTION, responseMeta.getErrorText()));
            }
        } catch (Exception ex) {
            // 解析失败直接抛异常
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "decode response failed", ex);
        }
        return rpcResponse;
    } finally {
        if (responsePacket.getMetaBuf() != null) {
            responsePacket.getMetaBuf().release();
        }
        if (responsePacket.getProtoAndAttachmentBuf() != null) {
            responsePacket.getProtoAndAttachmentBuf().release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) ChannelInfo(com.baidu.brpc.ChannelInfo) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) IOException(java.io.IOException) RpcException(com.baidu.brpc.exceptions.RpcException) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 7 with RpcException

use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.

the class HuluRpcProtocol method decode.

@Override
public HuluRpcDecodePacket decode(ChannelHandlerContext ctx, DynamicCompositeByteBuf in, boolean isDecodingRequest) throws BadSchemaException, TooBigDataException, NotEnoughDataException {
    if (in.readableBytes() < FIXED_LEN) {
        throw notEnoughDataException;
    }
    ByteBuf fixHeaderBuf = in.retainedSlice(FIXED_LEN);
    try {
        byte[] magic = new byte[4];
        fixHeaderBuf.readBytes(magic);
        if (!Arrays.equals(magic, MAGIC_HEAD)) {
            throw new BadSchemaException("not valid magic head for hulu");
        }
        int bodySize = fixHeaderBuf.readIntLE();
        int metaSize = fixHeaderBuf.readIntLE();
        // 512M
        if (bodySize > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodySize);
        }
        if (in.readableBytes() < FIXED_LEN + bodySize) {
            throw notEnoughDataException;
        }
        in.skipBytes(FIXED_LEN);
        HuluRpcDecodePacket packet = new HuluRpcDecodePacket();
        try {
            // meta
            ByteBuf metaBuf = in.readRetainedSlice(metaSize);
            packet.setMetaBuf(metaBuf);
            // body
            ByteBuf protoAndAttachmentBuf = in.readRetainedSlice(bodySize - metaSize);
            packet.setProtoAndAttachmentBuf(protoAndAttachmentBuf);
            return packet;
        } catch (Exception ex) {
            LOG.warn("decode failed, ex={}", ex.getMessage());
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, ex);
        }
    } finally {
        fixHeaderBuf.release();
    }
}
Also used : TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) RpcException(com.baidu.brpc.exceptions.RpcException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) IOException(java.io.IOException)

Example 8 with RpcException

use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.

the class PublicPbrpcProtocol method decodeRequest.

@Override
public Request decodeRequest(Object packet) throws Exception {
    Request request = this.createRequest();
    PublicPbRpcPacket pbPacket = (PublicPbRpcPacket) packet;
    ByteBuf bodyBuf = pbPacket.getBody();
    try {
        PublicPbrpcRequest pbRequest = (PublicPbrpcRequest) ProtobufUtils.parseFrom(bodyBuf, PublicPbrpcRequest.getDefaultInstance());
        RequestBody body = pbRequest.getRequestBody(0);
        RequestHead head = pbRequest.getRequestHead();
        request.setCorrelationId(body.getId());
        request.setLogId(head.getLogId());
        int compressType = head.getCompressType();
        request.setCompressType(compressType);
        // service info
        ServiceManager serviceManager = ServiceManager.getInstance();
        RpcMethodInfo rpcMethodInfo = serviceManager.getService(body.getService(), String.valueOf(body.getMethodId()));
        if (rpcMethodInfo == null) {
            String errorMsg = String.format("Fail to find service=%s, methodIndex=%s", body.getService(), body.getMethodId());
            request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
            return request;
        }
        request.setRpcMethodInfo(rpcMethodInfo);
        request.setTargetMethod(rpcMethodInfo.getMethod());
        request.setTarget(rpcMethodInfo.getTarget());
        // proto body
        try {
            Compress compress = compressManager.getCompress(compressType);
            Object requestProto = compress.uncompressInput(body.getSerializedRequest().toByteArray(), rpcMethodInfo);
            request.setArgs(new Object[] { requestProto });
        } catch (Exception ex) {
            String errorMsg = String.format("decode failed, msg=%s", ex.getMessage());
            log.error(errorMsg);
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg, ex);
        }
        return request;
    } finally {
        if (bodyBuf != null) {
            bodyBuf.release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) RpcMethodInfo(com.baidu.brpc.RpcMethodInfo) PublicPbrpcRequest(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.PublicPbrpcRequest) Request(com.baidu.brpc.protocol.Request) ByteString(com.google.protobuf.ByteString) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) PublicPbrpcRequest(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.PublicPbrpcRequest) ServiceManager(com.baidu.brpc.server.ServiceManager) RpcException(com.baidu.brpc.exceptions.RpcException) RequestBody(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.RequestBody) RequestHead(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.RequestHead)

Example 9 with RpcException

use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.

the class PublicPbrpcProtocol method decodeResponse.

@Override
public Response decodeResponse(Object in, ChannelHandlerContext ctx) throws Exception {
    PublicPbRpcPacket packet = (PublicPbRpcPacket) in;
    RpcResponse rpcResponse = new RpcResponse();
    ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
    ByteBuf bodyBuf = packet.getBody();
    try {
        PublicPbrpcResponse pbResponse = (PublicPbrpcResponse) ProtobufUtils.parseFrom(bodyBuf, PublicPbrpcResponse.getDefaultInstance());
        ResponseBody body = pbResponse.getResponseBody(0);
        ResponseHead head = pbResponse.getResponseHead();
        if (head.getCode() != 0) {
            rpcResponse.setException(new RpcException(head.getText()));
        } else {
            rpcResponse.setCorrelationId(body.getId());
            RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
            if (future == null) {
                return rpcResponse;
            }
            rpcResponse.setRpcFuture(future);
            Object responseBody;
            int compressType = head.getCompressType();
            try {
                Compress compress = compressManager.getCompress(compressType);
                responseBody = compress.uncompressOutput(body.getSerializedResponse().toByteArray(), future.getRpcMethodInfo());
            } catch (Exception ex) {
                String errorMsg = String.format("decode failed, msg=%s", ex.getMessage());
                log.error(errorMsg);
                throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg, ex);
            }
            rpcResponse.setResult(responseBody);
        }
    } finally {
        if (bodyBuf != null) {
            bodyBuf.release();
        }
    }
    return rpcResponse;
}
Also used : Compress(com.baidu.brpc.compress.Compress) ChannelInfo(com.baidu.brpc.ChannelInfo) ByteString(com.google.protobuf.ByteString) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) ResponseBody(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.ResponseBody) ResponseHead(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.ResponseHead) RpcException(com.baidu.brpc.exceptions.RpcException) PublicPbrpcResponse(com.baidu.brpc.protocol.pbrpc.PublicPbrpcProto.PublicPbrpcResponse) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 10 with RpcException

use of com.baidu.brpc.exceptions.RpcException in project brpc-java by baidu.

the class DefaultServerPushProtocol method decodeResponse.

@Override
public Response decodeResponse(Object in, ChannelHandlerContext ctx) throws Exception {
    DefaultServerPushPacket packet = (DefaultServerPushPacket) in;
    RpcResponse rpcResponse = new RpcResponse();
    // channel info是在客户端生成连接池的时候生成的
    ChannelInfo channelInfo = ChannelInfo.getClientChannelInfo(ctx.channel());
    Long correlationId = packet.getSpHead().getCorrelationId();
    rpcResponse.setCorrelationId(correlationId);
    RpcFuture future = channelInfo.removeRpcFuture(rpcResponse.getCorrelationId());
    if (future == null || future.getRpcMethodInfo() == null) {
        // rpc method info为null,表示register请求
        try {
            ByteBuf byteBuf = packet.getBodyBuf();
            byteBuf.release();
        } catch (Exception e) {
            throw new RpcException(e);
        }
        return rpcResponse;
    }
    rpcResponse.setRpcFuture(future);
    SPBody spBody = decodeBodyByteBuf(packet.getBodyBuf());
    Object responseBody = spBody.getContent();
    if (responseBody == null) {
        return null;
    }
    rpcResponse.setResult(responseBody);
    return rpcResponse;
}
Also used : RpcException(com.baidu.brpc.exceptions.RpcException) ChannelInfo(com.baidu.brpc.ChannelInfo) RpcFuture(com.baidu.brpc.client.RpcFuture) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcResponse(com.baidu.brpc.protocol.RpcResponse) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException)

Aggregations

RpcException (com.baidu.brpc.exceptions.RpcException)68 ByteBuf (io.netty.buffer.ByteBuf)22 BadSchemaException (com.baidu.brpc.exceptions.BadSchemaException)20 NotEnoughDataException (com.baidu.brpc.exceptions.NotEnoughDataException)20 TooBigDataException (com.baidu.brpc.exceptions.TooBigDataException)20 DynamicCompositeByteBuf (com.baidu.brpc.buffer.DynamicCompositeByteBuf)16 ChannelInfo (com.baidu.brpc.ChannelInfo)13 ArrayList (java.util.ArrayList)12 Compress (com.baidu.brpc.compress.Compress)10 RpcMethodInfo (com.baidu.brpc.RpcMethodInfo)9 RpcClient (com.baidu.brpc.client.RpcClient)9 RpcFuture (com.baidu.brpc.client.RpcFuture)9 Request (com.baidu.brpc.protocol.Request)9 Interceptor (com.baidu.brpc.interceptor.Interceptor)8 Response (com.baidu.brpc.protocol.Response)8 RpcCallback (com.baidu.brpc.client.RpcCallback)7 RpcClientOptions (com.baidu.brpc.client.RpcClientOptions)7 RpcResponse (com.baidu.brpc.protocol.RpcResponse)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7