use of com.tencent.polaris.client.pojo.InstanceByProto 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.client.pojo.InstanceByProto 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.client.pojo.InstanceByProto in project polaris-java by polarismesh.
the class AbstractStateMachine method openToHalfOpen.
@Override
public boolean openToHalfOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
String cbName = parameter.getCircuitBreakerName();
CircuitBreakerStatus cbStatus = instance.getCircuitBreakerStatus(statusDimension);
if (null == cbStatus || !cbStatus.getCircuitBreaker().equals(cbName) || cbStatus.getStatus() != CircuitBreakerStatus.Status.OPEN) {
return false;
}
boolean detectSuccess = false;
if (instance instanceof InstanceByProto) {
DetectResult detectResult = ((InstanceByProto) instance).getDetectResult();
detectSuccess = null != detectResult && detectResult.getRetStatus() == RetStatus.RetSuccess;
}
// 清空halfOpen的计数器
HalfOpenCounter halfOpenCounter = getHalfOpenCounter(parameter.getPluginId(), instance);
if (null == halfOpenCounter) {
return false;
}
long startTimeMs = cbStatus.getStartTimeMs();
HalfOpenConfig halfOpenConfig = getHalfOpenConfigOnHalfOpen(instance, statusDimension);
boolean matched;
if (detectSuccess && halfOpenConfig.getWhenToDetect() != When.never) {
matched = true;
} else {
matched = parameter.getCurrentTimeMs() - startTimeMs >= halfOpenConfig.getSleepWindowMs();
}
if (matched) {
// 时间窗已经过去, 则恢复半开
halfOpenCounter.resetHalfOpen(statusDimension);
}
return matched;
}
use of com.tencent.polaris.client.pojo.InstanceByProto 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.client.pojo.InstanceByProto in project polaris-java by polarismesh.
the class ChangeStateUtils method getInstance.
/**
* 获取实例ID
*
* @param instanceGauge 实例统计数据
* @param localRegistry 本地缓存插件
* @return ID
*/
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
ServiceInstances instances = localRegistry.getInstances(resourceFilter);
if (!instances.isInitialized()) {
return null;
}
ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
Instance instance = instanceGauge.getInstance();
if (instance instanceof InstanceByProto) {
return (InstanceByProto) instance;
}
InstanceByProto instanceByProto;
String instanceId = instanceGauge.getInstanceId();
if (StringUtils.isNotBlank(instanceId)) {
instanceByProto = serviceInstancesByProto.getInstance(instanceId);
} else {
Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
}
if (null != instanceByProto) {
instanceGauge.setInstance(instanceByProto);
}
return instanceByProto;
}
Aggregations