use of com.tencent.polaris.api.pojo.InstanceLocalValue in project polaris-java by polarismesh.
the class InMemoryRegistry method updateInstances.
@Override
public void updateInstances(ServiceUpdateRequest request) {
Collection<InstanceProperty> instanceProperties = request.getProperties();
if (CollectionUtils.isEmpty(instanceProperties)) {
return;
}
RegistryCacheValue cacheValue = getResource(new ServiceEventKey(request.getServiceKey(), EventType.INSTANCE), true, true);
if (null == cacheValue) {
// 服务不存在,忽略
return;
}
for (InstanceProperty instanceProperty : instanceProperties) {
InstanceByProto instance = (InstanceByProto) instanceProperty.getInstance();
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
Map<String, Object> properties = instanceProperty.getProperties();
LOG.info("update instance properties for instance {}, properties {}", instance.getId(), properties);
for (Map.Entry<String, Object> entry : properties.entrySet()) {
switch(entry.getKey()) {
case InstanceProperty.PROPERTY_CIRCUIT_BREAKER_STATUS:
onCircuitBreakStatus(entry.getValue(), instanceLocalValue, instance);
break;
case InstanceProperty.PROPERTY_DETECT_RESULT:
instanceLocalValue.setDetectResult((DetectResult) entry.getValue());
break;
default:
break;
}
}
}
}
use of com.tencent.polaris.api.pojo.InstanceLocalValue 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.InstanceLocalValue in project polaris-java by polarismesh.
the class AbstractStateMachine method getHalfOpenCounter.
protected HalfOpenCounter getHalfOpenCounter(int pluginId, Instance instance) {
InstanceByProto instanceByProto = (InstanceByProto) instance;
InstanceLocalValue instanceLocalValue = instanceByProto.getInstanceLocalValue();
if (null == instanceLocalValue) {
return null;
}
Object pluginValue = instanceLocalValue.getPluginValue(pluginId, null);
if (null == pluginValue) {
return null;
}
return (HalfOpenCounter) pluginValue;
}
use of com.tencent.polaris.api.pojo.InstanceLocalValue 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;
}
Aggregations