Search in sources :

Example 1 with Serializer

use of com.alipay.sofa.rpc.codec.Serializer in project sofa-rpc by sofastack.

the class SofaRpcSerialization method serializeContent.

@Override
public <Request extends RequestCommand> boolean serializeContent(Request request, InvokeContext invokeContext) throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object requestObject = requestCommand.getRequestObject();
        byte serializerCode = requestCommand.getSerializer();
        long serializeStartTime = System.nanoTime();
        try {
            Map<String, String> header = (Map<String, String>) requestCommand.getRequestHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE, (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(requestObject, header);
            request.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            throw new SerializationException(ex.getMessage(), ex);
        } finally {
            // R5:record request serialization time
            recordSerializeRequest(requestCommand, invokeContext, serializeStartTime);
        }
    }
    return false;
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SerializationException(com.alipay.remoting.exception.SerializationException) HashMap(java.util.HashMap) Map(java.util.Map) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Example 2 with Serializer

use of com.alipay.sofa.rpc.codec.Serializer in project sofa-rpc by sofastack.

the class SofaRpcSerialization method serializeContent.

@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response) throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializerCode = response.getSerializer();
        long serializeStartTime = System.nanoTime();
        try {
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
            responseCommand.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
            String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
            LOGGER.error("traceId={}, rpcId={}, Response serializeContent exception, msg = {}", traceId, rpcId, ex.getMessage(), ex);
            throw new SerializationException(ex.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, ex);
        } finally {
            // R6:Record response serialization time
            recordSerializeResponse(responseCommand, serializeStartTime);
        }
    }
    return false;
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException) Serializer(com.alipay.sofa.rpc.codec.Serializer) DefaultCustomSerializer(com.alipay.remoting.DefaultCustomSerializer)

Example 3 with Serializer

use of com.alipay.sofa.rpc.codec.Serializer in project sofa-rpc by sofastack.

the class AbstractHttpClientHandler method decode.

protected void decode(SofaResponse response) {
    AbstractByteBuf byteBuffer = response.getData();
    if (byteBuffer != null) {
        try {
            Map<String, String> context = new HashMap<String, String>(4);
            if (response.isError()) {
                context.put(RemotingConstants.HEAD_RESPONSE_ERROR, response.isError() + "");
                String errorMsg = StringSerializer.decode(byteBuffer.array());
                response.setAppResponse(new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, errorMsg));
            } else {
                context.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
                context.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
                Serializer serializer = SerializerFactory.getSerializer(response.getSerializeType());
                serializer.decode(byteBuffer, response, context);
            }
        } finally {
            byteBuffer.release();
            response.setData(null);
        }
    }
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) HashMap(java.util.HashMap) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) StringSerializer(com.alipay.sofa.rpc.codec.common.StringSerializer) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 4 with Serializer

use of com.alipay.sofa.rpc.codec.Serializer in project sofa-rpc by sofastack.

the class TripleClientInvoker method invoke.

@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
    if (!useGeneric) {
        SofaResponse sofaResponse = new SofaResponse();
        Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
        final Method method = sofaRequest.getMethod();
        Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
        sofaResponse.setAppResponse(appResponse);
        return sofaResponse;
    } else {
        String serviceName = sofaRequest.getInterfaceName();
        String methodName = sofaRequest.getMethodName();
        MethodDescriptor.Marshaller<?> requestMarshaller = null;
        MethodDescriptor.Marshaller<?> responseMarshaller = null;
        requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
        responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
        String fullMethodName = generateFullMethodName(serviceName, methodName);
        MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
        Request request = getRequest(sofaRequest, serialization, serializer);
        Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
        SofaResponse sofaResponse = new SofaResponse();
        byte[] responseDate = response.getData().toByteArray();
        Class returnType = sofaRequest.getMethod().getReturnType();
        if (returnType != void.class) {
            if (responseDate != null && responseDate.length > 0) {
                Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
                Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
                sofaResponse.setAppResponse(appResponse);
            }
        }
        return sofaResponse;
    }
}
Also used : Request(triple.Request) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Method(java.lang.reflect.Method) ByteString(com.google.protobuf.ByteString) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) MethodDescriptor(io.grpc.MethodDescriptor) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 5 with Serializer

use of com.alipay.sofa.rpc.codec.Serializer in project sofa-rpc by sofastack.

the class GenericServiceImplTest method getReturnValue.

private Object getReturnValue(Method method) {
    Object appResponse = null;
    Response response = responseObserver.getValue();
    byte[] responseDate = response.getData().toByteArray();
    Class returnType = method.getReturnType();
    if (returnType != void.class) {
        if (responseDate != null && responseDate.length > 0) {
            Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
            appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
        }
    }
    return appResponse;
}
Also used : Response(triple.Response) ByteArrayWrapperByteBuf(com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf) SofaHessianSerializer(com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Aggregations

Serializer (com.alipay.sofa.rpc.codec.Serializer)10 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)5 HashMap (java.util.HashMap)5 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)4 DeserializationException (com.alipay.remoting.exception.DeserializationException)4 SerializationException (com.alipay.remoting.exception.SerializationException)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)4 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)4 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 Response (triple.Response)3 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)2 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)2 StringSerializer (com.alipay.sofa.rpc.codec.common.StringSerializer)2 ByteString (com.google.protobuf.ByteString)2 SofaHessianSerializer (com.alipay.sofa.rpc.codec.sofahessian.SofaHessianSerializer)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1