Search in sources :

Example 1 with InvokeTimeoutException

use of com.alipay.remoting.rpc.exception.InvokeTimeoutException 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 2 with InvokeTimeoutException

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

the class FutureTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    // 服务端
    ProviderConfig<HelloService> CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig2);
    CProvider.export();
    Filter filter = new TestAsyncFilter();
    // 客户端
    ConsumerConfig<HelloService> BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setTimeout(5000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22222");
    HelloService helloService = BConsumer.refer();
    // 正常
    boolean error = false;
    try {
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture();
        ret = (String) future.get();
        Assert.assertNotNull(ret);
        // 过滤器生效
        Assert.assertTrue(ret.endsWith("append by async filter"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // 客户端超时,设置 < 等待
    error = false;
    long start = System.currentTimeMillis();
    long end;
    try {
        RpcInvokeContext.getContext().setTimeout(300);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture(true);
        Assert.assertNull(RpcInvokeContext.getContext().getFuture());
        ret = (String) future.get(1000, TimeUnit.MILLISECONDS);
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    } finally {
        end = System.currentTimeMillis();
        LOGGER.info("elapsed time " + (end - start) + "ms");
        Assert.assertTrue((end - start) < 400);
    }
    Assert.assertTrue(error);
    // 客户端超时,设置 > 等待
    error = false;
    start = System.currentTimeMillis();
    try {
        RpcInvokeContext.getContext().setTimeout(1000);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture(true);
        Assert.assertNull(RpcInvokeContext.getContext().getFuture());
        ret = (String) future.get(300, TimeUnit.MILLISECONDS);
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof TimeoutException);
        error = true;
    } finally {
        end = System.currentTimeMillis();
        LOGGER.info("elapsed time " + (end - start) + "ms");
        Assert.assertTrue((end - start) < 400);
    }
    Assert.assertTrue(error);
    // 客户端获取的时候  服务端还没返回
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture();
        // 500ms 过去, 但是 1s 还没到,服务端还没返回
        ret = (String) future.get();
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 客户端获取的时候 服务端已经返回但是超时了
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        // 1s 过去,被rpc设置超时了
        Thread.sleep(1500);
        Future future = SofaResponseFuture.getFuture();
        ret = (String) future.get();
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 服务端超时
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Object ret1 = SofaResponseFuture.getResponse(300, true);
        Assert.assertNotNull(ret1);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 服务端超时
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(300);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Object ret1 = SofaResponseFuture.getResponse(500, true);
        Assert.assertNotNull(ret1);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaResponseFuture(com.alipay.sofa.rpc.api.future.SofaResponseFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 3 with InvokeTimeoutException

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

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

the class BoltInvokerCallback method onException.

@Override
public void onException(Throwable e) {
    if (callback == null) {
        return;
    }
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.classLoader);
        RpcInternalContext.setContext(context);
        if (EventBus.isEnable(ClientAsyncReceiveEvent.class)) {
            EventBus.post(new ClientAsyncReceiveEvent(consumerConfig, providerInfo, request, null, e));
        }
        // do async filter after respond server
        FilterChain chain = consumerConfig.getConsumerBootstrap().getCluster().getFilterChain();
        if (chain != null) {
            chain.onAsyncResponse(consumerConfig, request, null, e);
        }
        recordClientElapseTime();
        if (EventBus.isEnable(ClientEndInvokeEvent.class)) {
            EventBus.post(new ClientEndInvokeEvent(request, null, e));
        }
        // judge is timeout or others
        SofaRpcException sofaRpcException = null;
        if (e instanceof InvokeTimeoutException) {
            sofaRpcException = new SofaTimeOutException(e);
        } else {
            sofaRpcException = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, e.getMessage(), e);
        }
        callback.onSofaException(sofaRpcException, request.getMethodName(), request);
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
        RpcInvokeContext.removeContext();
        RpcInternalContext.removeAllContext();
    }
}
Also used : ClientEndInvokeEvent(com.alipay.sofa.rpc.event.ClientEndInvokeEvent) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) FilterChain(com.alipay.sofa.rpc.filter.FilterChain) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ClientAsyncReceiveEvent(com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)4 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)4 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)3 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)2 DeserializationException (com.alipay.remoting.exception.DeserializationException)2 SerializationException (com.alipay.remoting.exception.SerializationException)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 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)2 Test (org.junit.Test)2 SofaResponseFuture (com.alipay.sofa.rpc.api.future.SofaResponseFuture)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 ClientAsyncReceiveEvent (com.alipay.sofa.rpc.event.ClientAsyncReceiveEvent)1 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)1 Filter (com.alipay.sofa.rpc.filter.Filter)1 FilterChain (com.alipay.sofa.rpc.filter.FilterChain)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)1