Search in sources :

Example 1 with ServiceUpdateRequest

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);
    }
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) DetectResult(com.tencent.polaris.api.pojo.DetectResult)

Example 2 with ServiceUpdateRequest

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);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) ArrayList(java.util.ArrayList) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) DetectResult(com.tencent.polaris.api.pojo.DetectResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ServiceUpdateRequest

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);
}
Also used : CircuitBreaker(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker) HashMap(java.util.HashMap) Instance(com.tencent.polaris.api.pojo.Instance) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) CircuitBreakResult(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey) HashSet(java.util.HashSet)

Aggregations

ServiceUpdateRequest (com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest)3 Instance (com.tencent.polaris.api.pojo.Instance)3 HashMap (java.util.HashMap)3 DetectResult (com.tencent.polaris.api.pojo.DetectResult)2 CircuitBreakResult (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult)1 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)1 CircuitBreaker (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker)1 InstanceProperty (com.tencent.polaris.api.plugin.registry.InstanceProperty)1 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1