Search in sources :

Example 1 with TimeRange

use of com.tencent.polaris.plugins.circuitbreaker.common.stat.TimeRange in project polaris-java by polarismesh.

the class StateMachineImpl method closeToOpen.

@Override
public boolean closeToOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
    HalfOpenCounter halfOpenCounter = getHalfOpenCounterOnClose(instance, statusDimension);
    if (halfOpenCounter == null) {
        return false;
    }
    ConfigSet<Config> configSet = getConfigSetByLocator(instance, statusDimension, configSetLocator);
    Config plugConfig = configSet.getPlugConfig();
    ErrRateCounter errRateCounter = (ErrRateCounter) halfOpenCounter;
    SliceWindow metricWindow = errRateCounter.getSliceWindow(statusDimension);
    long currentTimeMs = parameter.getCurrentTimeMs();
    TimeRange timeRange = new TimeRange(currentTimeMs - metricTimeWindowMs, currentTimeMs);
    long requestCount = metricWindow.calcMetricsBothIncluded(Dimension.keyRequestCount.ordinal(), timeRange);
    if (requestCount == 0 || requestCount < plugConfig.getRequestVolumeThreshold()) {
        // 未达到其实请求数阈值
        return false;
    }
    long failCount = metricWindow.calcMetricsBothIncluded(Dimension.keyFailCount.ordinal(), timeRange);
    double failRatio = (double) failCount / (double) requestCount;
    LOG.debug("errRate statistic: request count {}, fail count {}, instance {}:{}, dimension {}, failRatio {}", requestCount, failCount, instance.getHost(), instance.getPort(), statusDimension, failRatio);
    // 错误率达标
    return failRatio >= plugConfig.getErrRate();
}
Also used : TimeRange(com.tencent.polaris.plugins.circuitbreaker.common.stat.TimeRange) HalfOpenCounter(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter) SliceWindow(com.tencent.polaris.plugins.circuitbreaker.common.stat.SliceWindow)

Aggregations

HalfOpenCounter (com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenCounter)1 SliceWindow (com.tencent.polaris.plugins.circuitbreaker.common.stat.SliceWindow)1 TimeRange (com.tencent.polaris.plugins.circuitbreaker.common.stat.TimeRange)1