Search in sources :

Example 1 with ResultKey

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

the class ChangeStateUtils method buildCircuitBreakResult.

/**
 * 构建熔断结果
 *
 * @param stateMachine 熔断状态机
 * @param instances 实例集
 * @param parameter 熔断参数
 * @param <T> 配置类型
 * @return 结果
 */
public static <T extends Verifier> CircuitBreakResult buildCircuitBreakResult(StateMachine<T> stateMachine, Collection<Instance> instances, StateMachine.Parameter parameter) {
    long curTimeMs = System.currentTimeMillis();
    CircuitBreakResult result = new CircuitBreakResult(curTimeMs, parameter.getHalfOpenMaxReqCount());
    String cbName = parameter.getCircuitBreakerName();
    for (Instance instance : instances) {
        Set<StatusDimension> statusDimensions = stateMachine.getStatusDimensions(instance, parameter);
        if (CollectionUtils.isEmpty(statusDimensions)) {
            continue;
        }
        String instanceId = instance.getId();
        for (StatusDimension statusDimension : statusDimensions) {
            if (stateMachine.closeToOpen(instance, statusDimension, parameter)) {
                result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from close to open by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.openToHalfOpen(instance, statusDimension, parameter)) {
                result.getInstancesToHalfOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from open to halfOpen by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.halfOpenToOpen(instance, statusDimension, parameter)) {
                result.getInstancesToOpen().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to open by {}", instanceId, statusDimension, cbName);
                continue;
            }
            if (stateMachine.halfOpenToClose(instance, statusDimension, parameter)) {
                result.getInstancesToClose().put(new ResultKey(instanceId, statusDimension), instance);
                LOG.info("circuitBreaker: instance {} and dimension {} changed from halfOpen to close by {}", instanceId, statusDimension, cbName);
            }
        }
    }
    return result;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) CircuitBreakResult(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)

Example 2 with ResultKey

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

the class InstancesCircuitBreakTask method cleanInstanceSet.

private void cleanInstanceSet(Map<ResultKey, Instance> instanceSet, Set<ResultKey> allInstances) {
    Set<ResultKey> instIdsToRemove = new HashSet<>();
    for (Map.Entry<ResultKey, Instance> entry : instanceSet.entrySet()) {
        ResultKey resultKey = entry.getKey();
        if (allInstances.contains(resultKey)) {
            instIdsToRemove.add(resultKey);
        } else {
            allInstances.add(resultKey);
        }
    }
    instIdsToRemove.forEach(instanceSet::remove);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) Map(java.util.Map) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey) HashSet(java.util.HashSet)

Example 3 with ResultKey

use of com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey 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 4 with ResultKey

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

the class InstancesCircuitBreakTask method buildInstanceProperty.

@SuppressWarnings("unchecked")
private void buildInstanceProperty(long now, Map<ResultKey, Instance> results, int maxRequestAfterHalfOpen, Map<String, InstanceProperty> instanceProperties, String cbName, CircuitBreakerStatus.Status status) {
    if (MapUtils.isEmpty(results)) {
        return;
    }
    for (Map.Entry<ResultKey, Instance> entry : results.entrySet()) {
        ResultKey resultKey = entry.getKey();
        Instance instance = entry.getValue();
        String instId = resultKey.getInstId();
        InstanceProperty instanceProperty = instanceProperties.get(instId);
        if (null == instanceProperty) {
            Map<String, Object> properties = new HashMap<>();
            properties.put(PROPERTY_CIRCUIT_BREAKER_STATUS, new HashMap<StatusDimension, CircuitBreakerStatus>());
            instanceProperty = new InstanceProperty(instance, properties);
            instanceProperties.put(instId, instanceProperty);
        }
        Map<StatusDimension, CircuitBreakerStatus> statusMap = (Map<StatusDimension, CircuitBreakerStatus>) instanceProperty.getProperties().get(PROPERTY_CIRCUIT_BREAKER_STATUS);
        statusMap.put(resultKey.getStatusDimension(), new CircuitBreakerStatus(cbName, status, now));
    }
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) HashMap(java.util.HashMap) Map(java.util.Map) ResultKey(com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)

Aggregations

ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)4 Instance (com.tencent.polaris.api.pojo.Instance)4 HashMap (java.util.HashMap)3 CircuitBreakResult (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult)2 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 CircuitBreaker (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker)1 InstanceProperty (com.tencent.polaris.api.plugin.registry.InstanceProperty)1 ServiceUpdateRequest (com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1