Search in sources :

Example 1 with DeserializationException

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

the class SimpleMapSerializer method decode.

/**
 * 简单 map 的反序列化过程, 用来反序列化 bolt 的 header
 * <p>
 * {@link SofaRpcSerialization#deserializeHeader(com.alipay.remoting.rpc.RequestCommand)}
 *
 * @param bytes bolt header
 * @return 反序列化后的 Map 对象
 * @throws DeserializationException DeserializationException
 */
public Map<String, String> decode(byte[] bytes) throws DeserializationException {
    Map<String, String> map = new HashMap<String, String>();
    if (bytes == null || bytes.length == 0) {
        return map;
    }
    UnsafeByteArrayInputStream in = new UnsafeByteArrayInputStream(bytes);
    try {
        while (in.available() > 0) {
            String key = readString(in);
            String value = readString(in);
            if (key != null && value != null) {
                map.put(key, value);
            }
        }
        return map;
    } catch (IOException ex) {
        throw new DeserializationException(ex.getMessage(), ex);
    }
}
Also used : HashMap(java.util.HashMap) UnsafeByteArrayInputStream(com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream) IOException(java.io.IOException) DeserializationException(com.alipay.remoting.exception.DeserializationException)

Example 2 with DeserializationException

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

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

the class SofaRpcSerializationTest method deserializeRequestContent.

@Test
public void deserializeRequestContent() {
    String traceId = "traceId";
    String rpcId = "rpcId";
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put("rpc_trace_context.sofaTraceId", traceId);
    headerMap.put("rpc_trace_context.sofaRpcId", rpcId);
    RpcRequestCommand command = new RpcRequestCommand();
    command.setRequestHeader(headerMap);
    SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
    boolean exp = false;
    try {
        sofaRpcSerialization.deserializeContent(command);
    } catch (DeserializationException e) {
        exp = true;
        Assert.assertEquals("Content of request is null, traceId=" + traceId + ", rpcId=" + rpcId, e.getMessage());
    }
    Assert.assertTrue(exp);
}
Also used : HashMap(java.util.HashMap) DeserializationException(com.alipay.remoting.exception.DeserializationException) RpcRequestCommand(com.alipay.remoting.rpc.protocol.RpcRequestCommand) Test(org.junit.Test)

Example 4 with DeserializationException

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

the class BoltClientTransport method convertToRpcException.

/**
 * 转换调用出现的异常为RPC异常
 *
 * @param e 异常
 * @return RPC异常
 */
protected SofaRpcException convertToRpcException(Exception e) {
    SofaRpcException exception;
    if (e instanceof SofaRpcException) {
        exception = (SofaRpcException) e;
    } else // 超时
    if (e instanceof InvokeTimeoutException) {
        exception = new SofaTimeOutException(e);
    } else // 服务器忙
    if (e instanceof InvokeServerBusyException) {
        exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, e);
    } else // 序列化
    if (e instanceof SerializationException) {
        boolean isServer = ((SerializationException) e).isServerSide();
        exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_SERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_SERIALIZE, e);
    } else // 反序列化
    if (e instanceof DeserializationException) {
        boolean isServer = ((DeserializationException) e).isServerSide();
        exception = isServer ? new SofaRpcException(RpcErrorType.SERVER_DESERIALIZE, e) : new SofaRpcException(RpcErrorType.CLIENT_DESERIALIZE, e);
    } else // 长连接断连
    if (e instanceof ConnectionClosedException) {
        exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
    } else // 客户端发送失败
    if (e instanceof InvokeSendFailedException) {
        exception = new SofaRpcException(RpcErrorType.CLIENT_NETWORK, e);
    } else // 服务端未知异常
    if (e instanceof InvokeServerException) {
        exception = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getCause());
    } else // 客户端未知
    {
        exception = new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e);
    }
    return exception;
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SerializationException(com.alipay.remoting.exception.SerializationException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) InvokeSendFailedException(com.alipay.remoting.rpc.exception.InvokeSendFailedException) ConnectionClosedException(com.alipay.remoting.exception.ConnectionClosedException) InvokeServerException(com.alipay.remoting.rpc.exception.InvokeServerException) InvokeServerBusyException(com.alipay.remoting.rpc.exception.InvokeServerBusyException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) DeserializationException(com.alipay.remoting.exception.DeserializationException)

Example 5 with DeserializationException

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

DeserializationException (com.alipay.remoting.exception.DeserializationException)6 SerializationException (com.alipay.remoting.exception.SerializationException)4 HashMap (java.util.HashMap)4 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 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)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 ByteArrayWrapperByteBuf (com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf)2 Map (java.util.Map)2 Test (org.junit.Test)2 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 UnsafeByteArrayInputStream (com.alipay.sofa.rpc.common.struct.UnsafeByteArrayInputStream)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1