Search in sources :

Example 16 with RpcRequestCommand

use of com.alipay.remoting.rpc.protocol.RpcRequestCommand in project sofa-rpc by sofastack.

the class SofaRpcSerialization method deserializeContent.

@Override
public <Request extends RequestCommand> boolean deserializeContent(Request request) throws DeserializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object header = requestCommand.getRequestHeader();
        if (!(header instanceof Map)) {
            throw new DeserializationException("Head of request is null or is not map");
        }
        Map<String, String> headerMap = (Map<String, String>) header;
        String traceId = headerMap.get("rpc_trace_context.sofaTraceId");
        String rpcId = headerMap.get("rpc_trace_context.sofaRpcId");
        long deserializeStartTime = System.nanoTime();
        try {
            byte[] content = requestCommand.getContent();
            if (content == null || content.length == 0) {
                throw new DeserializationException("Content of request is null");
            }
            String service = headerMap.get(RemotingConstants.HEAD_SERVICE);
            ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
            ClassLoader serviceClassLoader = ReflectCache.getServiceClassLoader(service);
            try {
                Thread.currentThread().setContextClassLoader(serviceClassLoader);
                Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(requestCommand.getSerializer());
                Object sofaRequest = ClassUtils.forName(requestCommand.getRequestClass()).newInstance();
                rpcSerializer.decode(new ByteArrayWrapperByteBuf(requestCommand.getContent()), sofaRequest, headerMap);
                // for service mesh or other scene, we need to add more info from header
                if (sofaRequest instanceof SofaRequest) {
                    setRequestPropertiesWithHeaderInfo(headerMap, (SofaRequest) sofaRequest);
                    parseRequestHeader(headerMap, (SofaRequest) sofaRequest);
                }
                requestCommand.setRequestObject(sofaRequest);
            } finally {
                Thread.currentThread().setContextClassLoader(oldClassLoader);
            }
            return true;
        } catch (Exception ex) {
            LOGGER.error("traceId={}, rpcId={}, Request deserializeContent exception, msg={}", traceId, rpcId, ex.getMessage(), ex);
            throw new DeserializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
        } finally {
            // R6:Record request deserialization time
            recordDeserializeRequest(requestCommand, deserializeStartTime);
        }
    }
    return false;
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) DeserializationException(com.alipay.remoting.exception.DeserializationException) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) HashMap(java.util.HashMap) Map(java.util.Map) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Aggregations

RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)16 Message (com.google.protobuf.Message)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 SerializationException (com.alipay.remoting.exception.SerializationException)2 PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)2 Serializer (com.alipay.sofa.rpc.codec.Serializer)2 Map (java.util.Map)2 RpcCommandFactory (com.alipay.remoting.rpc.RpcCommandFactory)1 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 Status (com.alipay.sofa.jraft.Status)1 AppendEntriesRequest (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest)1 AppendEntriesRequestHeader (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequestHeader)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)1 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)1