use of com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest in project polaris-java by polarismesh.
the class InstancesDetectTask method doInstanceDetectForService.
private void doInstanceDetectForService(ServiceKey serviceKey) throws PolarisException {
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
ServiceInstances instances = extensions.getLocalRegistry().getInstances(new ResourceFilter(svcEventKey, true, true));
if (!instances.isInitialized() || instances.getInstances().size() == 0) {
return;
}
Map<Instance, DetectResult> aliveResults = new HashMap<>();
for (Instance instance : instances.getInstances()) {
if (destroy.get()) {
// 如果要停止定时任务,则剩下的实例探测也没必要进行下去了
break;
}
DetectResult result = detectInstance(instance);
if (result == null || result.getRetStatus() != RetStatus.RetSuccess) {
continue;
}
aliveResults.put(instance, result);
}
if (MapUtils.isNotEmpty(aliveResults)) {
ServiceUpdateRequest updateRequest = buildInstanceUpdateResult(serviceKey, aliveResults);
LOG.info("update cache for outlier detect, value is {}", updateRequest);
extensions.getLocalRegistry().updateInstances(updateRequest);
}
}
use of com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest in project polaris-java by polarismesh.
the class InstancesDetectTask method buildInstanceUpdateResult.
private ServiceUpdateRequest buildInstanceUpdateResult(ServiceKey serviceKey, Map<Instance, DetectResult> aliveResults) {
List<InstanceProperty> instances = new ArrayList<>();
for (Map.Entry<Instance, DetectResult> entry : aliveResults.entrySet()) {
Map<String, Object> properties = new HashMap<>();
properties.put(PROPERTY_DETECT_RESULT, entry.getValue());
InstanceProperty instanceProperty = new InstanceProperty(entry.getKey(), properties);
instances.add(instanceProperty);
}
return new ServiceUpdateRequest(serviceKey, instances);
}
use of com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest in project polaris-java by polarismesh.
the class InstancesCircuitBreakTask method run.
@Override
public void run() {
Map<String, CircuitBreakResult> allResults = new HashMap<>();
Set<ResultKey> statusChangedInstances = new HashSet<>();
Collection<Instance> targetInstances = instances;
if (StringUtils.isNotEmpty(instId)) {
Instance instance = getInstance();
if (null != instance) {
targetInstances = new ArrayList<>();
targetInstances.add(instance);
}
}
if (CollectionUtils.isEmpty(targetInstances)) {
return;
}
for (CircuitBreaker circuitBreaker : extensions.getCircuitBreakers()) {
if (StringUtils.isNotBlank(cbName) && !cbName.equals(circuitBreaker.getName())) {
continue;
}
CircuitBreakResult circuitBreakResult = circuitBreaker.checkInstance(targetInstances);
if (null == circuitBreakResult || circuitBreakResult.isEmptyResult()) {
continue;
}
cleanInstanceSet(circuitBreakResult.getInstancesToOpen(), statusChangedInstances);
cleanInstanceSet(circuitBreakResult.getInstancesToHalfOpen(), statusChangedInstances);
cleanInstanceSet(circuitBreakResult.getInstancesToClose(), statusChangedInstances);
allResults.put(circuitBreaker.getName(), circuitBreakResult);
}
ServiceUpdateRequest updateRequest = buildServiceUpdateRequest(serviceKey, allResults);
if (CollectionUtils.isEmpty(updateRequest.getProperties())) {
return;
}
LOG.info("update cache for circuitbreaker, value is {}", updateRequest);
extensions.getLocalRegistry().updateInstances(updateRequest);
}
Aggregations