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);
}
}
}
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);
}
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;
}
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 + "].");
}
}
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);
}
Aggregations