use of com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter in project polaris-java by polarismesh.
the class ErrRateCircuitBreaker method stat.
@Override
public boolean stat(InstanceGauge gauge) {
InstanceByProto instance = ChangeStateUtils.getInstance(gauge, localRegistry);
if (null == instance) {
return false;
}
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
if (null == instanceLocalValue) {
return false;
}
ConfigSet<Config> configSet = CircuitBreakUtils.getConfigSet(gauge, this);
StatusDimension statusDimension = ChangeStateUtils.buildStatusDimension(gauge, configSet.getLevel());
if (CircuitBreakUtils.instanceClose(instance, statusDimension)) {
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
ErrRateCounter errRateCounter = (ErrRateCounter) pluginValue;
SliceWindow metricWindow = errRateCounter.getSliceWindow(statusDimension);
metricWindow.addGauge((bucket -> {
bucket.addMetric(Dimension.keyRequestCount.ordinal(), 1);
if (gauge.getRetStatus() == RetStatus.RetFail) {
return bucket.addMetric(Dimension.keyFailCount.ordinal(), 1);
}
return bucket.getMetric(Dimension.keyFailCount.ordinal());
}));
} else if (CircuitBreakUtils.instanceHalfOpen(instance, statusDimension)) {
// 半开计数器
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
HalfOpenCounter consecutiveCounter = (HalfOpenCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
return consecutiveCounter.triggerHalfOpenConversion(statusDimension, retStatus, configSet.getHalfOpenConfig());
}
return false;
}
use of com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter in project polaris-java by polarismesh.
the class StateMachineImpl method closeToOpen.
@Override
public boolean closeToOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
HalfOpenCounter halfOpenCounter = getHalfOpenCounterOnClose(instance, statusDimension);
if (halfOpenCounter == null) {
return false;
}
ConfigSet<Config> configSet = getConfigSetByLocator(instance, statusDimension, configSetLocator);
Config plugConfig = configSet.getPlugConfig();
ErrRateCounter errRateCounter = (ErrRateCounter) halfOpenCounter;
SliceWindow metricWindow = errRateCounter.getSliceWindow(statusDimension);
long currentTimeMs = parameter.getCurrentTimeMs();
TimeRange timeRange = new TimeRange(currentTimeMs - metricTimeWindowMs, currentTimeMs);
long requestCount = metricWindow.calcMetricsBothIncluded(Dimension.keyRequestCount.ordinal(), timeRange);
if (requestCount == 0 || requestCount < plugConfig.getRequestVolumeThreshold()) {
// 未达到其实请求数阈值
return false;
}
long failCount = metricWindow.calcMetricsBothIncluded(Dimension.keyFailCount.ordinal(), timeRange);
double failRatio = (double) failCount / (double) requestCount;
LOG.debug("errRate statistic: request count {}, fail count {}, instance {}:{}, dimension {}, failRatio {}", requestCount, failCount, instance.getHost(), instance.getPort(), statusDimension, failRatio);
// 错误率达标
return failRatio >= plugConfig.getErrRate();
}
use of com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter in project polaris-java by polarismesh.
the class ConsecutiveCircuitBreaker method stat.
@Override
public boolean stat(InstanceGauge gauge) {
InstanceByProto instance = ChangeStateUtils.getInstance(gauge, localRegistry);
if (null == instance) {
return false;
}
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
if (null == instanceLocalValue) {
return false;
}
ConfigSet<Config> configSet = CircuitBreakUtils.getConfigSet(gauge, this);
StatusDimension statusDimension = ChangeStateUtils.buildStatusDimension(gauge, configSet.getLevel());
if (CircuitBreakUtils.instanceClose(instance, statusDimension)) {
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
ConsecutiveCounter consecutiveCounter = (ConsecutiveCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
int failCount;
if (retStatus == RetStatus.RetFail) {
failCount = consecutiveCounter.onFail(statusDimension);
} else {
consecutiveCounter.resetCounter(statusDimension);
failCount = 0;
}
return failCount == configSet.getPlugConfig().getContinuousErrorThreshold();
} else if (CircuitBreakUtils.instanceHalfOpen(instance, statusDimension)) {
// 半开计数器
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
HalfOpenCounter consecutiveCounter = (HalfOpenCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
return consecutiveCounter.triggerHalfOpenConversion(statusDimension, retStatus, configSet.getHalfOpenConfig());
}
return false;
}
use of com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter in project polaris-java by polarismesh.
the class StateMachineImpl method closeToOpen.
@Override
public boolean closeToOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
HalfOpenCounter halfOpenCounter = getHalfOpenCounterOnClose(instance, statusDimension);
if (halfOpenCounter == null) {
return false;
}
ConfigSet<Config> configSet = getConfigSetByLocator(instance, statusDimension, configSetLocator);
return ((ConsecutiveCounter) halfOpenCounter).getConsecutiveErrorCount(statusDimension) >= configSet.getPlugConfig().getContinuousErrorThreshold();
}
Aggregations