Search in sources :

Example 1 with Request

use of com.baidu.brpc.protocol.Request in project skywalking-java by apache.

the class ClientInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    InetSocketAddress remoteAddress = (InetSocketAddress) request.getChannel().remoteAddress();
    InetAddress address = remoteAddress.getAddress();
    final ContextCarrier contextCarrier = new ContextCarrier();
    AbstractSpan span = ContextManager.createExitSpan(generateOperationName(request), contextCarrier, address.getHostAddress() + ":" + remoteAddress.getPort());
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        if (request.getKvAttachment() == null) {
            request.setKvAttachment(new HashMap<>());
        }
        request.getKvAttachment().put(next.getHeadKey(), next.getHeadValue());
    }
    span.setComponent(ComponentsDefine.BRPC_JAVA);
    SpanLayer.asRPCFramework(span);
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) InetSocketAddress(java.net.InetSocketAddress) Request(com.baidu.brpc.protocol.Request) InetAddress(java.net.InetAddress) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 2 with Request

use of com.baidu.brpc.protocol.Request in project skywalking-java by apache.

the class ServerInterceptor method beforeMethod.

@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    if (request.getKvAttachment() == null) {
        request.setKvAttachment(new HashMap<>());
    }
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue((String) request.getKvAttachment().get(next.getHeadKey()));
    }
    AbstractSpan span = ContextManager.createEntrySpan(generateOperationName(request), contextCarrier);
    SpanLayer.asRPCFramework(span);
    span.setComponent(ComponentsDefine.BRPC_JAVA);
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Request(com.baidu.brpc.protocol.Request) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 3 with Request

use of com.baidu.brpc.protocol.Request in project brpc-java by baidu.

the class HuluRpcProtocol method decodeRequest.

@Override
public Request decodeRequest(Object packet) throws Exception {
    Request request = this.createRequest();
    HuluRpcDecodePacket requestPacket = (HuluRpcDecodePacket) packet;
    ByteBuf metaBuf = requestPacket.getMetaBuf();
    ByteBuf protoAndAttachmentBuf = requestPacket.getProtoAndAttachmentBuf();
    ByteBuf protoBuf = null;
    try {
        HuluRpcProto.HuluRpcRequestMeta requestMeta = (HuluRpcProto.HuluRpcRequestMeta) ProtobufUtils.parseFrom(metaBuf, defaultRpcRequestMetaInstance);
        request.setCorrelationId(requestMeta.getCorrelationId());
        request.setLogId(requestMeta.getLogId());
        int compressType = requestMeta.getCompressType();
        request.setCompressType(compressType);
        // service info
        ServiceManager serviceManager = ServiceManager.getInstance();
        RpcMethodInfo rpcMethodInfo = serviceManager.getService(requestMeta.getServiceName(), String.valueOf(requestMeta.getMethodIndex()));
        if (rpcMethodInfo == null) {
            String errorMsg = String.format("Fail to find service=%s, methodIndex=%s", requestMeta.getServiceName(), requestMeta.getMethodIndex());
            request.setException(new RpcException(RpcException.SERVICE_EXCEPTION, errorMsg));
            return request;
        }
        if (requestMeta.hasTraceId()) {
            request.setTraceId(requestMeta.getTraceId());
        }
        if (requestMeta.hasSpanId()) {
            request.setSpanId(request.getSpanId());
        }
        if (requestMeta.hasParentSpanId()) {
            request.setParentSpanId(requestMeta.getParentSpanId());
        }
        if (requestMeta.getExtFieldsCount() > 0) {
            if (request.getKvAttachment() == null) {
                request.setKvAttachment(new HashMap<String, Object>());
            }
            for (HuluRpcProto.HuluRpcRequestMetaExtField extField : requestMeta.getExtFieldsList()) {
                request.getKvAttachment().put(extField.getKey(), extField.getValue());
            }
        }
        request.setServiceName(rpcMethodInfo.getServiceName());
        request.setMethodName(rpcMethodInfo.getMethodName());
        request.setRpcMethodInfo(rpcMethodInfo);
        request.setTargetMethod(rpcMethodInfo.getMethod());
        request.setTarget(rpcMethodInfo.getTarget());
        // proto body
        try {
            Compress compress = compressManager.getCompress(compressType);
            int userMessageSize = requestMeta.getUserMessageSize();
            if (userMessageSize > 0) {
                protoBuf = protoAndAttachmentBuf.readSlice(userMessageSize);
            } else {
                protoBuf = protoAndAttachmentBuf;
            }
            Object requestProto = compress.uncompressInput(protoBuf, rpcMethodInfo);
            request.setArgs(new Object[] { requestProto });
            // attachment
            if (userMessageSize > 0) {
                request.setBinaryAttachment(protoAndAttachmentBuf);
                protoAndAttachmentBuf = null;
            }
        } 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 (metaBuf != null) {
            metaBuf.release();
        }
        if (protoAndAttachmentBuf != null) {
            protoAndAttachmentBuf.release();
        }
    }
}
Also used : Compress(com.baidu.brpc.compress.Compress) RpcMethodInfo(com.baidu.brpc.RpcMethodInfo) 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) IOException(java.io.IOException) ServiceManager(com.baidu.brpc.server.ServiceManager) RpcException(com.baidu.brpc.exceptions.RpcException)

Example 4 with Request

use of com.baidu.brpc.protocol.Request 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 5 with Request

use of com.baidu.brpc.protocol.Request 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)

Aggregations

Request (com.baidu.brpc.protocol.Request)22 ByteBuf (io.netty.buffer.ByteBuf)10 RpcException (com.baidu.brpc.exceptions.RpcException)9 RpcMethodInfo (com.baidu.brpc.RpcMethodInfo)8 Response (com.baidu.brpc.protocol.Response)7 RpcRequest (com.baidu.brpc.protocol.RpcRequest)7 DynamicCompositeByteBuf (com.baidu.brpc.buffer.DynamicCompositeByteBuf)5 ServiceManager (com.baidu.brpc.server.ServiceManager)5 Test (org.junit.Test)5 Compress (com.baidu.brpc.compress.Compress)4 BadSchemaException (com.baidu.brpc.exceptions.BadSchemaException)4 NotEnoughDataException (com.baidu.brpc.exceptions.NotEnoughDataException)4 TooBigDataException (com.baidu.brpc.exceptions.TooBigDataException)4 HttpRequest (com.baidu.brpc.protocol.HttpRequest)3 NSHead (com.baidu.brpc.protocol.nshead.NSHead)3 SPHead (com.baidu.brpc.protocol.push.SPHead)3 ServerPushProtocol (com.baidu.brpc.protocol.push.ServerPushProtocol)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 ProtobufRpcMethodInfo (com.baidu.brpc.ProtobufRpcMethodInfo)2