Search in sources :

Example 61 with SofaRpcException

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

the class InvocationStatDimensionStatTest method prepareInvokeContext.

private void prepareInvokeContext() {
    final RpcInvokeContext context = new RpcInvokeContext();
    context.setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(final Object appResponse, String methodName, RequestBase request) {
            // 放到 future 中方便测试.
            LOGGER.info("回调成功" + appResponse);
            context.setFuture(new ResponseFuture<String>() {

                @Override
                public ResponseFuture addListeners(List<SofaResponseCallback> sofaResponseCallbacks) {
                    return null;
                }

                @Override
                public ResponseFuture addListener(SofaResponseCallback sofaResponseCallback) {
                    return null;
                }

                @Override
                public boolean cancel(boolean mayInterruptIfRunning) {
                    return false;
                }

                @Override
                public boolean isCancelled() {
                    return false;
                }

                @Override
                public boolean isDone() {
                    return false;
                }

                @Override
                public String get() throws InterruptedException, ExecutionException {
                    return (String) appResponse;
                }

                @Override
                public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                    return null;
                }
            });
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("回调发生应用异常" + throwable);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("回调发生sofa异常" + sofaException);
        }
    });
    RpcInvokeContext.setContext(context);
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) TimeUnit(java.util.concurrent.TimeUnit) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) List(java.util.List) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 62 with SofaRpcException

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

the class InvocationStatDimensionStatTest method testInvocationStatStatic.

@Test
public void testInvocationStatStatic() {
    InvocationStatDimension invocation = new InvocationStatDimension(ProviderHelper.toProviderInfo("ip"), consumerConfig);
    InvocationStat invocationStat = new ServiceExceptionInvocationStat(invocation);
    /**
     *test info static
     */
    for (int i = 0; i < 10; i++) {
        invocationStat.invoke();
    }
    for (int i = 0; i < 5; i++) {
        invocationStat.catchException(new SofaTimeOutException(""));
    }
    for (int i = 0; i < 3; i++) {
        invocationStat.catchException(new SofaRpcException(RpcErrorType.SERVER_BUSY, ""));
    }
    Assert.assertTrue(10 == invocationStat.getInvokeCount());
    Assert.assertTrue(8 == invocationStat.getExceptionCount());
    Assert.assertTrue(0.8 == invocationStat.getExceptionRate());
    /**
     *test window update
     */
    InvocationStat snapshot = invocationStat.snapshot();
    Assert.assertTrue(10 == snapshot.getInvokeCount());
    Assert.assertTrue(8 == snapshot.getExceptionCount());
    Assert.assertTrue(0.8 == snapshot.getExceptionRate());
    for (int i = 0; i < 15; i++) {
        invocationStat.invoke();
    }
    for (int i = 0; i < 8; i++) {
        invocationStat.catchException(new SofaTimeOutException(""));
    }
    for (int i = 0; i < 2; i++) {
        invocationStat.catchException(new SofaRpcException(RpcErrorType.SERVER_BUSY, ""));
    }
    Assert.assertTrue(25 == invocationStat.getInvokeCount());
    Assert.assertTrue(18 == invocationStat.getExceptionCount());
    Assert.assertTrue(0.72 == invocationStat.getExceptionRate());
    // 时间窗口更新
    invocationStat.update(snapshot);
    Assert.assertTrue(15 == invocationStat.getInvokeCount());
    Assert.assertTrue(10 == invocationStat.getExceptionCount());
    Assert.assertTrue(0.67 == invocationStat.getExceptionRate());
}
Also used : ServiceExceptionInvocationStat(com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat) ServiceExceptionInvocationStat(com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 63 with SofaRpcException

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

the class InvocationStatDimensionStatTest method testFuture.

@Test
public void testFuture() throws SofaRpcException, ExecutionException, InterruptedException {
    consumerConfig.setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
    consumerConfig.unRefer();
    helloService = consumerConfig.refer();
    for (int i = 0; i < 5; i++) {
        helloService.sayHello("liangen");
        try {
            RpcInvokeContext.getContext().getFuture().get();
        } catch (Exception e) {
            LOGGER.info("future超时");
        }
    }
    Thread.sleep(1000);
    final ProviderInfo providerInfo = getProviderInfoByHost(consumerConfig, "127.0.0.1");
    InvocationStatDimension statDimension = new InvocationStatDimension(providerInfo, consumerConfig);
    InvocationStat invocationStat = InvocationStatFactory.getInvocationStat(statDimension);
    Assert.assertEquals(5, delayGetCount(invocationStat, 5));
    InvocationStatFactory.removeInvocationStat(invocationStat);
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ServiceExceptionInvocationStat(com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 64 with SofaRpcException

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

the class MeasureStrategyTest method testAllNotReachLeastWindowCountIgnore.

@Test
public void testAllNotReachLeastWindowCountIgnore() {
    FaultToleranceConfig config = new FaultToleranceConfig();
    config.setLeastWindowCount(10);
    config.setLeastWindowExceptionRateMultiple(3D);
    FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
    InvocationStatDimension invocation1 = new InvocationStatDimension(ProviderHelper.toProviderInfo("ip1"), consumerConfig);
    InvocationStatDimension invocation2 = new InvocationStatDimension(ProviderHelper.toProviderInfo("ip2"), consumerConfig);
    InvocationStatDimension invocation3 = new InvocationStatDimension(ProviderHelper.toProviderInfo("ip3"), consumerConfig);
    InvocationStatDimension invocation4 = new InvocationStatDimension(ProviderHelper.toProviderInfo("ip4"), consumerConfig);
    InvocationStat InvocationStat1 = InvocationStatFactory.getInvocationStat(invocation1);
    InvocationStat InvocationStat2 = InvocationStatFactory.getInvocationStat(invocation2);
    InvocationStat InvocationStat3 = InvocationStatFactory.getInvocationStat(invocation3);
    InvocationStat InvocationStat4 = InvocationStatFactory.getInvocationStat(invocation4);
    MeasureModel measureModel = new MeasureModel(APP_NAME1, "service");
    measureModel.addInvocationStat(InvocationStat1);
    measureModel.addInvocationStat(InvocationStat2);
    measureModel.addInvocationStat(InvocationStat3);
    measureModel.addInvocationStat(InvocationStat4);
    /**
     *统计1-4都调用9次,异常5次
     */
    for (int i = 0; i < 9; i++) {
        InvocationStat1.invoke();
        InvocationStat2.invoke();
        InvocationStat3.invoke();
        InvocationStat4.invoke();
    }
    for (int i = 0; i < 5; i++) {
        InvocationStat1.catchException(new SofaTimeOutException(""));
        InvocationStat2.catchException(new SofaTimeOutException(""));
        InvocationStat3.catchException(new SofaRpcException(RpcErrorType.SERVER_BUSY, ""));
        InvocationStat4.catchException(new SofaRpcException(RpcErrorType.SERVER_BUSY, ""));
    }
    /**
     *度量
     */
    MeasureStrategy measureStrategy = new ServiceHorizontalMeasureStrategy();
    MeasureResult measureResult = measureStrategy.measure(measureModel);
    /**
     *校验结果
     */
    List<MeasureResultDetail> measureDetais = measureResult.getAllMeasureResultDetails();
    for (MeasureResultDetail measureResultDetail : measureDetais) {
        MeasureState measureState = measureResultDetail.getMeasureState();
        if (measureResultDetail.getInvocationStatDimension().equals(invocation1)) {
            Assert.assertTrue(measureState.equals(MeasureState.IGNORE));
        } else if (measureResultDetail.getInvocationStatDimension().equals(invocation2)) {
            Assert.assertTrue(measureState.equals(MeasureState.IGNORE));
        } else if (measureResultDetail.getInvocationStatDimension().equals(invocation3)) {
            Assert.assertTrue(measureState.equals(MeasureState.IGNORE));
        } else if (measureResultDetail.getInvocationStatDimension().equals(invocation4)) {
            Assert.assertTrue(measureState.equals(MeasureState.IGNORE));
        } else {
            Assert.fail("期望的度量目标与实际的度量结果目标不符");
        }
    }
}
Also used : ServiceExceptionInvocationStat(com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat) ServiceHorizontalMeasureStrategy(com.alipay.sofa.rpc.client.aft.impl.ServiceHorizontalMeasureStrategy) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServiceHorizontalMeasureStrategy(com.alipay.sofa.rpc.client.aft.impl.ServiceHorizontalMeasureStrategy) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) Test(org.junit.Test)

Example 65 with SofaRpcException

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

the class DiscardTimeoutTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(5).setCoreThreads(1).setMaxThreads(1);
    // 发布一个服务,每个请求要执行2秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(5000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    final AtomicInteger success = new AtomicInteger();
    final AtomicInteger failure = new AtomicInteger();
    int times = 4;
    final CountDownLatch latch = new CountDownLatch(times);
    for (int i = 0; i < times; i++) {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    helloService.sayHello("xxx", 22);
                    success.incrementAndGet();
                } catch (Exception e) {
                    failure.incrementAndGet();
                    Assert.assertTrue(e instanceof SofaRpcException);
                    Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.CLIENT_TIMEOUT);
                } finally {
                    latch.countDown();
                }
            }
        }, "T1");
        thread1.start();
    }
    try {
        // 此时客户端提前抛出超时异常
        latch.await(10000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 2秒1个 超时3秒  超时3个
    // 第一个请求正常
    // 第二个请求返回时超时
    // 第三个请求返回时超时
    // 第四个请求已超时
    Assert.assertEquals(success.get(), 2);
    Assert.assertEquals(failure.get(), 2);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7