Search in sources :

Example 1 with CircuitBreaker

use of com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker 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)

Example 2 with CircuitBreaker

use of com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker in project polaris-java by polarismesh.

the class ServiceCallResultChecker method onServiceCallResult.

@Override
public void onServiceCallResult(InstanceGauge result) {
    if (null == priorityTaskScheduler) {
        return;
    }
    if (CollectionUtils.isEmpty(extensions.getCircuitBreakers())) {
        return;
    }
    InstancesCircuitBreakTask rtTask = null;
    for (CircuitBreaker circuitBreaker : extensions.getCircuitBreakers()) {
        String cbName = circuitBreaker.getName();
        boolean rtLimit = circuitBreaker.stat(result);
        String instId = result.getInstanceId();
        if (rtLimit && StringUtils.isNotEmpty(instId)) {
            ServiceKey svcKey = new ServiceKey(result.getNamespace(), result.getService());
            rtTask = new InstancesCircuitBreakTask(svcKey, cbName, null, instId, extensions, InstancesCircuitBreakTask.TaskPriority.HIGH);
            break;
        }
    }
    if (null == rtTask) {
        return;
    }
    priorityTaskScheduler.addCircuitBreakTask(rtTask);
}
Also used : CircuitBreaker(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) InstancesCircuitBreakTask(com.tencent.polaris.circuitbreak.client.task.InstancesCircuitBreakTask)

Aggregations

CircuitBreaker (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker)2 CircuitBreakResult (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult)1 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)1 ServiceUpdateRequest (com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest)1 Instance (com.tencent.polaris.api.pojo.Instance)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 InstancesCircuitBreakTask (com.tencent.polaris.circuitbreak.client.task.InstancesCircuitBreakTask)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1