Search in sources :

Example 11 with RpcException

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

the class DefaultServerPushProtocol method decodeBodyByteBuf.

public SPBody decodeBodyByteBuf(ByteBuf bodyByteBuf) {
    try {
        int readableBytes = bodyByteBuf.readableBytes();
        byte[] bodyBytes = new byte[readableBytes];
        bodyByteBuf.readBytes(bodyBytes);
        Schema<SPBody> schema = RuntimeSchema.getSchema(SPBody.class);
        SPBody spBody = new SPBody();
        ProtobufIOUtil.mergeFrom(bodyBytes, spBody, schema);
        return spBody;
    } catch (Exception e) {
        throw new RpcException(e);
    } finally {
        if (bodyByteBuf != null) {
            bodyByteBuf.release();
        }
    }
}
Also used : RpcException(com.baidu.brpc.exceptions.RpcException) RpcException(com.baidu.brpc.exceptions.RpcException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException)

Example 12 with RpcException

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

the class SofaRpcProtocol method decodeResponse.

@Override
public RpcResponse decodeResponse(Object packet, ChannelHandlerContext ctx) throws Exception {
    SofaRpcDecodePacket responsePacket = (SofaRpcDecodePacket) packet;
    ByteBuf metaBuf = responsePacket.getMetaBuf();
    ByteBuf protoBuf = responsePacket.getProtoBuf();
    try {
        RpcResponse rpcResponse = new RpcResponse();
        SofaRpcProto.SofaRpcMeta responseMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
        Long correlationId = responseMeta.getSequenceId();
        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 = getStandardCompressType(responseMeta.getCompressType());
        rpcResponse.setCompressType(compressType);
        try {
            if (responseMeta != null && responseMeta.getErrorCode() == 0) {
                Compress compress = compressManager.getCompress(compressType);
                Object result = compress.uncompressOutput(protoBuf, future.getRpcMethodInfo());
                rpcResponse.setResult(result);
            } else {
                rpcResponse.setException(new RpcException(RpcException.SERVICE_EXCEPTION, responseMeta.getReason()));
            }
        } catch (Exception ex) {
            LOG.warn("decode response failed");
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, "decode response failed", ex);
        }
        return rpcResponse;
    } finally {
        if (metaBuf != null) {
            metaBuf.release();
        }
        if (protoBuf != null) {
            protoBuf.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) RpcException(com.baidu.brpc.exceptions.RpcException) RpcFuture(com.baidu.brpc.client.RpcFuture)

Example 13 with RpcException

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

the class SofaRpcProtocol method decodeRequest.

@Override
public Request decodeRequest(Object packet) throws Exception {
    Request request = this.createRequest();
    SofaRpcDecodePacket requestPacket = (SofaRpcDecodePacket) packet;
    ByteBuf metaBuf = requestPacket.getMetaBuf();
    ByteBuf protoBuf = requestPacket.getProtoBuf();
    try {
        SofaRpcProto.SofaRpcMeta requestMeta = (SofaRpcProto.SofaRpcMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcMetaInstance);
        request.setCorrelationId(requestMeta.getSequenceId());
        if (StringUtils.isBlank(requestMeta.getMethod())) {
            String errorMsg = "method is null";
            LOG.error(errorMsg);
            request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
            return request;
        }
        ServiceManager serviceManager = ServiceManager.getInstance();
        RpcMethodInfo rpcMethodInfo = serviceManager.getService(requestMeta.getMethod().toLowerCase());
        if (rpcMethodInfo == null) {
            String errorMsg = String.format("Fail to find method=%s", requestMeta.getMethod());
            LOG.error(errorMsg);
            request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
            return request;
        }
        request.setServiceName(rpcMethodInfo.getServiceName());
        request.setMethodName(rpcMethodInfo.getMethodName());
        request.setRpcMethodInfo(rpcMethodInfo);
        request.setTargetMethod(rpcMethodInfo.getMethod());
        request.setTarget(rpcMethodInfo.getTarget());
        int compressType = getStandardCompressType(requestMeta.getCompressType());
        request.setCompressType(compressType);
        Compress compress = compressManager.getCompress(compressType);
        try {
            Object requestProto = compress.uncompressInput(protoBuf, rpcMethodInfo);
            request.setArgs(new Object[] { requestProto });
        } catch (Exception ex) {
            String errorMsg = String.format("decode request failed, msg=%s", ex.getMessage());
            LOG.error(errorMsg);
            throw new RpcException(RpcException.SERIALIZATION_EXCEPTION, errorMsg);
        }
        return request;
    } finally {
        if (metaBuf != null) {
            metaBuf.release();
        }
        if (protoBuf != null) {
            protoBuf.release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) RpcMethodInfo(com.baidu.brpc.RpcMethodInfo) RpcRequest(com.baidu.brpc.protocol.RpcRequest) Request(com.baidu.brpc.protocol.Request) 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) ServiceManager(com.baidu.brpc.server.ServiceManager) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 14 with RpcException

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

the class SofaRpcProtocol method decode.

@Override
public SofaRpcDecodePacket 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 sofa");
        }
        int metaSize = fixHeaderBuf.readIntLE();
        int bodySize = (int) fixHeaderBuf.readLongLE();
        int msgSize = (int) fixHeaderBuf.readLongLE();
        if (msgSize != metaSize + bodySize) {
            throw new BadSchemaException("msgSize != metaSize + bodySize");
        }
        // 512M
        if (bodySize > 512 * 1024 * 1024) {
            throw new TooBigDataException("to big body size:" + bodySize);
        }
        if (in.readableBytes() < FIXED_LEN + msgSize) {
            throw notEnoughDataException;
        }
        in.skipBytes(FIXED_LEN);
        SofaRpcDecodePacket packet = new SofaRpcDecodePacket();
        try {
            // meta
            ByteBuf metaBuf = in.readRetainedSlice(metaSize);
            packet.setMetaBuf(metaBuf);
            // body
            ByteBuf protoBuf = in.readRetainedSlice(bodySize);
            packet.setProtoBuf(protoBuf);
            return packet;
        } catch (Exception ex) {
            LOG.debug("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)

Example 15 with RpcException

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

the class HttpRpcProtocol method decodeRequest.

@Override
public Request decodeRequest(Object packet) {
    try {
        HttpRequest httpRequest = (HttpRequest) this.createRequest();
        httpRequest.setMsg(packet);
        long correlationId = parseCorrelationId(httpRequest.headers().get(CORRELATION_ID), null);
        httpRequest.setCorrelationId(correlationId);
        String contentTypeAndEncoding = httpRequest.headers().get(HttpHeaderNames.CONTENT_TYPE);
        if (StringUtils.isBlank(contentTypeAndEncoding)) {
            String errMsg = String.format("content-type is null, uri:%s", httpRequest.uri());
            LOG.warn(errMsg);
            httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
            return httpRequest;
        }
        contentTypeAndEncoding = contentTypeAndEncoding.toLowerCase();
        String[] splits = StringUtils.split(contentTypeAndEncoding, ";");
        int protocolType = HttpRpcProtocol.parseProtocolType(splits[0]);
        String encoding = this.encoding;
        for (String split : splits) {
            split = split.trim();
            if (split.startsWith("charset=")) {
                encoding = split.substring("charset=".length());
            }
        }
        httpRequest.headers().set(PROTOCOL_TYPE, protocolType);
        httpRequest.headers().set(HttpHeaderNames.CONTENT_ENCODING, encoding);
        // set http headers to attachment
        if (httpRequest.getKvAttachment() == null) {
            httpRequest.setKvAttachment(new HashMap<String, Object>());
        }
        for (Map.Entry<String, String> entry : httpRequest.headers()) {
            httpRequest.getKvAttachment().put(entry.getKey(), entry.getValue());
        }
        ByteBuf byteBuf = httpRequest.content();
        int bodyLen = byteBuf.readableBytes();
        if (bodyLen == 0) {
            String errMsg = String.format("body should not be null, uri:%s", httpRequest.uri());
            LOG.warn(errMsg);
            httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
            return httpRequest;
        }
        byte[] requestBytes = new byte[bodyLen];
        byteBuf.readBytes(requestBytes, 0, bodyLen);
        Object body = decodeBody(protocolType, encoding, requestBytes);
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri());
        String path = queryStringDecoder.path();
        String serviceName = null;
        String methodName = null;
        if (protocolType == Options.ProtocolType.PROTOCOL_HTTP_PROTOBUF_VALUE || protocolType == Options.ProtocolType.PROTOCOL_HTTP_JSON_VALUE) {
            boolean pathError;
            try {
                serviceName = path.substring(1, path.lastIndexOf("/"));
                methodName = path.substring(path.lastIndexOf("/") + 1);
                pathError = serviceName == null || serviceName.isEmpty() || methodName == null || methodName.isEmpty();
            } catch (Exception e) {
                LOG.warn("url format is error", e);
                pathError = true;
            }
            if (pathError) {
                String errMsg = String.format("url format is error, path:%s", path);
                httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
                return httpRequest;
            }
        } else {
            JsonObject bodyObject = (JsonObject) body;
            methodName = bodyObject.get("method").getAsString();
            serviceName = path;
        }
        ServiceManager serviceManager = ServiceManager.getInstance();
        RpcMethodInfo rpcMethodInfo = serviceManager.getService(serviceName, methodName);
        if (rpcMethodInfo == null) {
            String errMsg = String.format("Fail to find path=%s", path);
            LOG.warn(errMsg);
            httpRequest.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errMsg));
            return httpRequest;
        }
        httpRequest.setServiceName(rpcMethodInfo.getServiceName());
        httpRequest.setMethodName(rpcMethodInfo.getMethodName());
        httpRequest.setRpcMethodInfo(rpcMethodInfo);
        httpRequest.setTargetMethod(rpcMethodInfo.getMethod());
        httpRequest.setTarget(rpcMethodInfo.getTarget());
        try {
            httpRequest.setArgs(parseRequestParam(protocolType, body, rpcMethodInfo));
        } catch (RpcException e) {
            LOG.error("parse request param error", e);
            httpRequest.setException(e);
            return httpRequest;
        }
        return httpRequest;
    } finally {
        ((FullHttpRequest) packet).release();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(com.baidu.brpc.protocol.HttpRequest) ProtobufRpcMethodInfo(com.baidu.brpc.ProtobufRpcMethodInfo) RpcMethodInfo(com.baidu.brpc.RpcMethodInfo) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) JsonObject(com.google.gson.JsonObject) ByteBuf(io.netty.buffer.ByteBuf) DynamicCompositeByteBuf(com.baidu.brpc.buffer.DynamicCompositeByteBuf) RpcException(com.baidu.brpc.exceptions.RpcException) NotEnoughDataException(com.baidu.brpc.exceptions.NotEnoughDataException) TooBigDataException(com.baidu.brpc.exceptions.TooBigDataException) BadSchemaException(com.baidu.brpc.exceptions.BadSchemaException) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) ServiceManager(com.baidu.brpc.server.ServiceManager) RpcException(com.baidu.brpc.exceptions.RpcException) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap)

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