Search in sources :

Example 11 with SofaTimeOutException

use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.

the class AsyncCallbackTest method testNoProviderException.

@Test
public void testNoProviderException() {
    // use bolt, so callback will throw connection closed exception
    serverConfig = new ServerConfig().setPort(22222).setDaemon(false).setProtocol("rest");
    serverConfig.buildIfAbsent().start();
    // B调C的客户端
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22222");
    HelloService helloService = BConsumer.refer();
    final CountDownLatch latch = new CountDownLatch(1);
    final String[] ret = { null };
    final boolean[] hasExp = { false };
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            if ((sofaException instanceof SofaTimeOutException)) {
                hasExp[0] = false;
            } else {
                hasExp[0] = true;
            }
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(1500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 一定是一个超时异常
    assertTrue(hasExp[0]);
    RpcInvokeContext.removeContext();
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 12 with SofaTimeOutException

use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException in project sofa-rpc by sofastack.

the class AsyncCallbackTest method testTimeoutException.

@Test
public void testTimeoutException() {
    serverConfig = new ServerConfig().setPort(22221).setDaemon(false);
    // C服务的服务端
    CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(500)).setServer(serverConfig);
    CProvider.export();
    // B调C的客户端
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22221");
    HelloService helloService = BConsumer.refer();
    final CountDownLatch latch = new CountDownLatch(1);
    final String[] ret = { null };
    final boolean[] hasExp = { false };
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            if (sofaException instanceof SofaTimeOutException) {
                hasExp[0] = true;
            }
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(2000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 一定是一个超时异常
    assertTrue(hasExp[0]);
    RpcInvokeContext.removeContext();
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 13 with SofaTimeOutException

use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException 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 14 with SofaTimeOutException

use of com.alipay.sofa.rpc.core.exception.SofaTimeOutException 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

SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)10 Test (org.junit.Test)10 ServiceExceptionInvocationStat (com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)4 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)4 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)3 ServiceHorizontalMeasureStrategy (com.alipay.sofa.rpc.client.aft.impl.ServiceHorizontalMeasureStrategy)3 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)3 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)3 CountDownLatch (java.util.concurrent.CountDownLatch)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 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)2