use of com.tencent.polaris.api.pojo.StatusDimension 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.api.pojo.StatusDimension in project polaris-java by polarismesh.
the class ChangeStateUtils method buildCircuitBreakResult.
/**
* 构建熔断结果
*
* @param stateMachine 熔断状态机
* @param instances 实例集
* @param parameter 熔断参数
* @param <T> 配置类型
* @return 结果
*/
public static <T extends Verifier> CircuitBreakResult buildCircuitBreakResult(StateMachine<T> stateMachine, Collection<Instance> instances, StateMachine.Parameter parameter) {
long curTimeMs = System.currentTimeMillis();
CircuitBreakResult result = new CircuitBreakResult(curTimeMs, parameter.getHalfOpenMaxReqCount());
String cbName = parameter.getCircuitBreakerName();
for (Instance instance : instances) {
Set<StatusDimension> statusDimensions = stateMachine.getStatusDimensions(instance, parameter);
if (CollectionUtils.isEmpty(statusDimensions)) {
continue;
}
String instanceId = instance.getId();
for (StatusDimension statusDimension : statusDimensions) {
if (stateMachine.closeToOpen(instance, statusDimension, parameter)) {
result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
LOG.info("circuitBreaker: instance {} and dimension {} changed from close to open by {}", instanceId, statusDimension, cbName);
continue;
}
if (stateMachine.openToHalfOpen(instance, statusDimension, parameter)) {
result.getInstancesToHalfOpen().put(new ResultKey(instanceId, statusDimension), instance);
LOG.info("circuitBreaker: instance {} and dimension {} changed from open to halfOpen by {}", instanceId, statusDimension, cbName);
continue;
}
if (stateMachine.halfOpenToOpen(instance, statusDimension, parameter)) {
result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to open by {}", instanceId, statusDimension, cbName);
continue;
}
if (stateMachine.halfOpenToClose(instance, statusDimension, parameter)) {
result.getInstancesToClose().put(new ResultKey(instanceId, statusDimension), instance);
LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to close by {}", instanceId, statusDimension, cbName);
}
}
}
return result;
}
use of com.tencent.polaris.api.pojo.StatusDimension 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.api.pojo.StatusDimension in project polaris-java by polarismesh.
the class InstancesCircuitBreakTask method buildInstanceProperty.
@SuppressWarnings("unchecked")
private void buildInstanceProperty(long now, Map<ResultKey, Instance> results, int maxRequestAfterHalfOpen, Map<String, InstanceProperty> instanceProperties, String cbName, CircuitBreakerStatus.Status status) {
if (MapUtils.isEmpty(results)) {
return;
}
for (Map.Entry<ResultKey, Instance> entry : results.entrySet()) {
ResultKey resultKey = entry.getKey();
Instance instance = entry.getValue();
String instId = resultKey.getInstId();
InstanceProperty instanceProperty = instanceProperties.get(instId);
if (null == instanceProperty) {
Map<String, Object> properties = new HashMap<>();
properties.put(PROPERTY_CIRCUIT_BREAKER_STATUS, new HashMap<StatusDimension, CircuitBreakerStatus>());
instanceProperty = new InstanceProperty(instance, properties);
instanceProperties.put(instId, instanceProperty);
}
Map<StatusDimension, CircuitBreakerStatus> statusMap = (Map<StatusDimension, CircuitBreakerStatus>) instanceProperty.getProperties().get(PROPERTY_CIRCUIT_BREAKER_STATUS);
statusMap.put(resultKey.getStatusDimension(), new CircuitBreakerStatus(cbName, status, now));
}
}
use of com.tencent.polaris.api.pojo.StatusDimension in project polaris-java-agent by polarismesh.
the class InstanceInvoker method circuitBreakersToString.
public static String circuitBreakersToString(Instance instance) {
List<String> values = new ArrayList<>();
Collection<StatusDimension> statusDimensions = instance.getStatusDimensions();
if (null != statusDimensions && statusDimensions.size() > 0) {
for (StatusDimension statusDimension : statusDimensions) {
CircuitBreakerStatus circuitBreakerStatus = instance.getCircuitBreakerStatus(statusDimension);
if (null != circuitBreakerStatus) {
values.add(statusDimension.getMethod() + SEP_CIRCUIT_BREAKER_VALUE + circuitBreakerStatus.getStatus().name());
}
}
}
if (values.isEmpty()) {
return "";
}
return String.join(SEP_CIRCUIT_BREAKER, values.toArray(new String[0]));
}
Aggregations