Search in sources :

Example 1 with ProviderInfo

use of com.alipay.sofa.rpc.client.ProviderInfo in project sofa-rpc by sofastack.

the class FaultToleranceSubscriber method onEvent.

@Override
public void onEvent(Event originEvent) {
    Class eventClass = originEvent.getClass();
    if (eventClass == ClientSyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 同步结果
        ClientSyncReceiveEvent event = (ClientSyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ClientAsyncReceiveEvent.class) {
        if (!FaultToleranceConfigManager.isEnable()) {
            return;
        }
        // 异步结果
        ClientAsyncReceiveEvent event = (ClientAsyncReceiveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderInfo providerInfo = event.getProviderInfo();
        InvocationStat result = InvocationStatFactory.getInvocationStat(consumerConfig, providerInfo);
        if (result != null) {
            result.invoke();
            Throwable t = event.getThrowable();
            if (t != null) {
                result.catchException(t);
            }
        }
    } else if (eventClass == ProviderInfoRemoveEvent.class) {
        ProviderInfoRemoveEvent event = (ProviderInfoRemoveEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        ProviderGroup providerGroup = event.getProviderGroup();
        if (!ProviderHelper.isEmpty(providerGroup)) {
            for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
                InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
            }
        }
    } else if (eventClass == ProviderInfoUpdateEvent.class) {
        ProviderInfoUpdateEvent event = (ProviderInfoUpdateEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroup(event.getOldProviderGroup(), event.getNewProviderGroup(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    } else if (eventClass == ProviderInfoUpdateAllEvent.class) {
        ProviderInfoUpdateAllEvent event = (ProviderInfoUpdateAllEvent) originEvent;
        ConsumerConfig consumerConfig = event.getConsumerConfig();
        List<ProviderInfo> add = new ArrayList<ProviderInfo>();
        List<ProviderInfo> remove = new ArrayList<ProviderInfo>();
        ProviderHelper.compareGroups(event.getOldProviderGroups(), event.getNewProviderGroups(), add, remove);
        for (ProviderInfo providerInfo : remove) {
            InvocationStatFactory.removeInvocationStat(consumerConfig, providerInfo);
        }
    }
}
Also used : InvocationStat(com.alipay.sofa.rpc.client.aft.InvocationStat) ArrayList(java.util.ArrayList) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with ProviderInfo

use of com.alipay.sofa.rpc.client.ProviderInfo in project sofa-rpc by sofastack.

the class DegradeStrategyTest method testAll.

@Test
public void testAll() throws InterruptedException {
    FaultToleranceConfig config = new FaultToleranceConfig();
    config.setDegradeEffective(true);
    config.setRegulationEffective(true);
    config.setTimeWindow(3);
    config.setLeastWindowCount(10);
    config.setWeightDegradeRate(0.5D);
    config.setDegradeLeastWeight(30);
    config.setLeastWindowExceptionRateMultiple(1D);
    FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
    /**
     *test degrade normal
     */
    for (int i = 0; i < 10; i++) {
        try {
            helloService.sayHello("liangen");
        } catch (Exception e) {
            LOGGER.info("超时");
        }
    }
    final ProviderInfo providerInfo = getProviderInfoByHost(consumerConfig, "127.0.0.1");
    InvocationStatDimension invocation = new InvocationStatDimension(providerInfo, consumerConfig);
    InvocationStat invocationStat = InvocationStatFactory.getInvocationStat(invocation);
    Assert.assertEquals(10, delayGetCount(invocationStat, 10));
    Assert.assertTrue(invocationStat.getExceptionCount() == 2);
    Assert.assertTrue(invocationStat.getExceptionRate() == 0.2);
    // 第一个窗口结束
    Assert.assertTrue(50 == delayGetWeight(providerInfo, 50, 40));
    /**
     *test degrade at lest
     */
    FaultToleranceConfigManager.getConfig(APP_NAME1).setLeastWindowCount(5L);
    for (int i = 0; i < 5; i++) {
        try {
            helloService.sayHello("liangen");
        } catch (Exception e) {
            LOGGER.info("超时");
        }
    }
    Assert.assertEquals(5, delayGetCount(invocationStat, 5));
    Assert.assertTrue(invocationStat.getExceptionCount() == 1);
    Assert.assertTrue(invocationStat.getExceptionRate() == 0.2);
    // 第二个窗口结束
    Assert.assertTrue(30 == delayGetWeight(providerInfo, 30, 60));
    InvocationStatFactory.removeInvocationStat(invocationStat);
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) Test(org.junit.Test)

Example 3 with ProviderInfo

use of com.alipay.sofa.rpc.client.ProviderInfo in project sofa-rpc by sofastack.

the class FaultBaseTest method getProviderInfoByHost.

static ProviderInfo getProviderInfoByHost(ConsumerConfig consumerConfig, String host) {
    ConsumerBootstrap consumerBootStrap = consumerConfig.getConsumerBootstrap();
    AddressHolder addressHolder = consumerBootStrap.getCluster().getAddressHolder();
    List<ProviderGroup> providerGroups = addressHolder.getProviderGroups();
    for (ProviderGroup providerGroup : providerGroups) {
        for (ProviderInfo providerInfo : providerGroup.getProviderInfos()) {
            if (providerInfo.getHost().equals(host)) {
                return providerInfo;
            }
        }
    }
    return null;
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) AddressHolder(com.alipay.sofa.rpc.client.AddressHolder) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 4 with ProviderInfo

use of com.alipay.sofa.rpc.client.ProviderInfo in project sofa-rpc by sofastack.

the class WeightDegradeStrategy method degrade.

@Override
public void degrade(MeasureResultDetail measureResultDetail) {
    super.degrade(measureResultDetail);
    if (measureResultDetail.isLogOnly()) {
        return;
    }
    InvocationStatDimension statDimension = measureResultDetail.getInvocationStatDimension();
    String appName = statDimension.getAppName();
    ProviderInfo providerInfo = statDimension.getProviderInfo();
    // if provider is removed or provider is warming up
    if (providerInfo == null || providerInfo.getStatus() == ProviderStatus.WARMING_UP) {
        return;
    }
    int currentWeight = ProviderInfoWeightManager.getWeight(providerInfo);
    double weightDegradeRate = FaultToleranceConfigManager.getWeightDegradeRate(appName);
    int degradeLeastWeight = FaultToleranceConfigManager.getDegradeLeastWeight(appName);
    int degradeWeight = CalculateUtils.multiply(currentWeight, weightDegradeRate);
    degradeWeight = degradeWeight < degradeLeastWeight ? degradeLeastWeight : degradeWeight;
    // degrade weight of this provider info
    boolean success = ProviderInfoWeightManager.degradeWeight(providerInfo, degradeWeight);
    if (success && LOGGER.isInfoEnabled(appName)) {
        LOGGER.infoWithApp(appName, "the weight was degraded. serviceUniqueName:[" + statDimension.getService() + "],ip:[" + statDimension.getIp() + "],origin weight:[" + currentWeight + "],degraded weight:[" + degradeWeight + "].");
    }
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) InvocationStatDimension(com.alipay.sofa.rpc.client.aft.InvocationStatDimension)

Example 5 with ProviderInfo

use of com.alipay.sofa.rpc.client.ProviderInfo in project sofa-rpc by sofastack.

the class FaultToleranceSubscriberTest method onProviderEvent.

@Test
public void onProviderEvent() {
    FaultToleranceConfig config = new FaultToleranceConfig();
    config.setRegulationEffective(true);
    FaultToleranceConfigManager.putAppConfig(APP_NAME1, config);
    ProviderInfo providerInfo1 = ProviderHelper.toProviderInfo("127.0.0.1");
    ProviderInfo providerInfo2 = ProviderHelper.toProviderInfo("127.0.0.2");
    ProviderInfo providerInfo3 = ProviderHelper.toProviderInfo("127.0.0.3");
    ProviderInfo providerInfo4 = ProviderHelper.toProviderInfo("127.0.0.4");
    ProviderInfo providerInfo5 = ProviderHelper.toProviderInfo("127.0.0.5");
    FaultToleranceSubscriber subscriber = new FaultToleranceSubscriber();
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo1, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo2, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo3, new SofaRequest(), new SofaResponse(), null));
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo4, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 4);
    subscriber.onEvent(new ProviderInfoRemoveEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateEvent(consumerConfig, new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.3"), ProviderHelper.toProviderInfo("127.0.0.4"))), new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 2);
    subscriber.onEvent(new ClientSyncReceiveEvent(consumerConfig, providerInfo5, new SofaRequest(), new SofaResponse(), null));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 3);
    subscriber.onEvent(new ProviderInfoUpdateAllEvent(consumerConfig, Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.2"), ProviderHelper.toProviderInfo("127.0.0.4"), ProviderHelper.toProviderInfo("127.0.0.5")))), Arrays.asList(new ProviderGroup("x", Arrays.asList(ProviderHelper.toProviderInfo("127.0.0.1"), ProviderHelper.toProviderInfo("127.0.0.4"))))));
    Assert.assertTrue(InvocationStatFactory.ALL_STATS.size() == 1);
}
Also used : ProviderInfoRemoveEvent(com.alipay.sofa.rpc.event.ProviderInfoRemoveEvent) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderInfoUpdateEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateEvent) ProviderInfoUpdateAllEvent(com.alipay.sofa.rpc.event.ProviderInfoUpdateAllEvent) FaultToleranceSubscriber(com.alipay.sofa.rpc.event.FaultToleranceSubscriber) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) ClientSyncReceiveEvent(com.alipay.sofa.rpc.event.ClientSyncReceiveEvent) Test(org.junit.Test)

Aggregations

ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)94 Test (org.junit.Test)38 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)24 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)14 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)13 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)9 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)6 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)6 List (java.util.List)6 Map (java.util.Map)6 AddressHolder (com.alipay.sofa.rpc.client.AddressHolder)5 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)5 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)5 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)5 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)5 ServiceExceptionInvocationStat (com.alipay.sofa.rpc.client.aft.impl.ServiceExceptionInvocationStat)4 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)4