Search in sources :

Example 1 with SerializationException

use of com.alipay.remoting.exception.SerializationException 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 SerializationException

use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.

the class SofaRpcSerialization method serializeHeader.

@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response) throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();
        Object responseObject = ((RpcResponseCommand) response).getResponseObject();
        if (responseObject instanceof SofaResponse) {
            SofaResponse sofaResponse = (SofaResponse) responseObject;
            if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
                sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
            }
            byte[] header = null;
            try {
                header = mapSerializer.encode(sofaResponse.getResponseProps());
            } catch (Exception e) {
                String traceId = (String) RpcInternalContext.getContext().getAttachment("_trace_id");
                String rpcId = (String) RpcInternalContext.getContext().getAttachment("_span_id");
                LOGGER.error("traceId={}, rpcId={}, Response serializeHeader exception, msg={}", traceId, rpcId, e.getMessage(), e);
                throw new SerializationException(e.getMessage() + ", traceId=" + traceId + ", rpcId=" + rpcId, e);
            }
            response.setHeader(header);
        }
        return true;
    }
    return false;
}
Also used : SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) DeserializationException(com.alipay.remoting.exception.DeserializationException) SerializationException(com.alipay.remoting.exception.SerializationException)

Example 3 with SerializationException

use of com.alipay.remoting.exception.SerializationException 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 4 with SerializationException

use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.

the class BoltClientTransportTest method testConvertToRpcException.

@Test
public void testConvertToRpcException() {
    ClientTransportConfig config1 = new ClientTransportConfig();
    config1.setProviderInfo(new ProviderInfo().setHost("127.0.0.1").setPort(12222)).setContainer("bolt");
    BoltClientTransport transport = new BoltClientTransport(config1);
    Assert.assertTrue(transport.convertToRpcException(new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "")) instanceof SofaRpcException);
    Assert.assertTrue(transport.convertToRpcException(new InvokeTimeoutException()) instanceof SofaTimeOutException);
    Assert.assertTrue(transport.convertToRpcException(new InvokeServerBusyException()).getErrorType() == RpcErrorType.SERVER_BUSY);
    Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_SERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new SerializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_SERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", true)).getErrorType() == RpcErrorType.SERVER_DESERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new DeserializationException("xx", false)).getErrorType() == RpcErrorType.CLIENT_DESERIALIZE);
    Assert.assertTrue(transport.convertToRpcException(new ConnectionClosedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
    Assert.assertTrue(transport.convertToRpcException(new InvokeSendFailedException()).getErrorType() == RpcErrorType.CLIENT_NETWORK);
    Assert.assertTrue(transport.convertToRpcException(new InvokeServerException()).getErrorType() == RpcErrorType.SERVER_UNDECLARED_ERROR);
    Assert.assertTrue(transport.convertToRpcException(new UnsupportedOperationException()).getErrorType() == RpcErrorType.CLIENT_UNDECLARED_ERROR);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 5 with SerializationException

use of com.alipay.remoting.exception.SerializationException in project sofa-rpc by sofastack.

the class SofaRpcSerializationTest method serializeResponseContent.

@Test
public void serializeResponseContent() {
    String traceId = "traceId";
    String rpcId = "rpcId";
    RpcInternalContext.getContext().setAttachment("_trace_id", traceId);
    RpcInternalContext.getContext().setAttachment("_span_id", rpcId);
    RpcResponseCommand command = new RpcResponseCommand();
    SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
    boolean exp = false;
    try {
        sofaRpcSerialization.serializeContent(command);
    } catch (SerializationException e) {
        exp = true;
        Assert.assertTrue(e.getMessage().contains("traceId=" + traceId + ", rpcId=" + rpcId));
    }
    Assert.assertTrue(exp);
}
Also used : SerializationException(com.alipay.remoting.exception.SerializationException) RpcResponseCommand(com.alipay.remoting.rpc.protocol.RpcResponseCommand) Test(org.junit.Test)

Aggregations

SerializationException (com.alipay.remoting.exception.SerializationException)7 DeserializationException (com.alipay.remoting.exception.DeserializationException)5 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)3 DefaultCustomSerializer (com.alipay.remoting.DefaultCustomSerializer)2 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)2 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)2 InvokeServerBusyException (com.alipay.remoting.rpc.exception.InvokeServerBusyException)2 InvokeServerException (com.alipay.remoting.rpc.exception.InvokeServerException)2 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)2 Serializer (com.alipay.sofa.rpc.codec.Serializer)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)2 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 UnsafeByteArrayOutputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayOutputStream)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1