Java Code Examples for com.alibaba.csp.sentinel.slots.block.RuleConstant#FLOW_GRADE_QPS

The following examples show how to use com.alibaba.csp.sentinel.slots.block.RuleConstant#FLOW_GRADE_QPS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParamFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public static boolean passCheck(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRule rule, /*@Valid*/ int count,
                         Object... args) {
    if (args == null) {
        return true;
    }

    int paramIdx = rule.getParamIdx();
    if (args.length <= paramIdx) {
        return true;
    }

    // Get parameter value. If value is null, then pass.
    Object value = args[paramIdx];
    if (value == null) {
        return true;
    }

    if (rule.isClusterMode() && rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        return passClusterCheck(resourceWrapper, rule, count, value);
    }

    return passLocalCheck(resourceWrapper, rule, count, value);
}
 
Example 2
Source File: ParamFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
static boolean passSingleValueCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount,
                                    Object value) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        if (rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) {
            return passThrottleLocalCheck(resourceWrapper, rule, acquireCount, value);
        } else {
            return passDefaultLocalCheck(resourceWrapper, rule, acquireCount, value);
        }
    } else if (rule.getGrade() == RuleConstant.FLOW_GRADE_THREAD) {
        Set<Object> exclusionItems = rule.getParsedHotItems().keySet();
        long threadCount = getParameterMetric(resourceWrapper).getThreadCount(rule.getParamIdx(), value);
        if (exclusionItems.contains(value)) {
            int itemThreshold = rule.getParsedHotItems().get(value);
            return ++threadCount <= itemThreshold;
        }
        long threshold = (long)rule.getCount();
        return ++threadCount <= threshold;
    }

    return true;
}
 
Example 3
Source File: DefaultController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean canPass(Node node, int acquireCount, boolean prioritized) {
    int curCount = avgUsedTokens(node);
    if (curCount + acquireCount > count) {
        if (prioritized && grade == RuleConstant.FLOW_GRADE_QPS) {
            long currentTime;
            long waitInMs;
            currentTime = TimeUtil.currentTimeMillis();
            waitInMs = node.tryOccupyNext(currentTime, acquireCount, count);
            if (waitInMs < OccupyTimeoutProperty.getOccupyTimeout()) {
                node.addWaitingRequest(currentTime + waitInMs, acquireCount);
                node.addOccupiedPass(acquireCount);
                sleep(waitInMs);

                // PriorityWaitException indicates that the request will pass after waiting for {@link @waitInMs}.
                throw new PriorityWaitException(waitInMs);
            }
        }
        return false;
    }
    return true;
}
 
Example 4
Source File: FlowRuleUtil.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private static TrafficShapingController generateRater(/*@Valid*/ FlowRule rule) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        switch (rule.getControlBehavior()) {
            case RuleConstant.CONTROL_BEHAVIOR_WARM_UP:
                return new WarmUpController(rule.getCount(), rule.getWarmUpPeriodSec(),
                    ColdFactorProperty.coldFactor);
            case RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER:
                return new RateLimiterController(rule.getMaxQueueingTimeMs(), rule.getCount());
            case RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER:
                return new WarmUpRateLimiterController(rule.getCount(), rule.getWarmUpPeriodSec(),
                    rule.getMaxQueueingTimeMs(), ColdFactorProperty.coldFactor);
            case RuleConstant.CONTROL_BEHAVIOR_DEFAULT:
            default:
                // Default mode or unknown mode: default traffic shaping controller (fast-reject).
        }
    }
    return new DefaultController(rule.getCount(), rule.getGrade());
}
 
Example 5
Source File: ParamFlowChecker.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
static boolean passSingleValueCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount,
                                    Object value) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        if (rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) {
            return passThrottleLocalCheck(resourceWrapper, rule, acquireCount, value);
        } else {
            return passDefaultLocalCheck(resourceWrapper, rule, acquireCount, value);
        }
    } else if (rule.getGrade() == RuleConstant.FLOW_GRADE_THREAD) {
        Set<Object> exclusionItems = rule.getParsedHotItems().keySet();
        long threadCount = getParameterMetric(resourceWrapper).getThreadCount(rule.getParamIdx(), value);
        if (exclusionItems.contains(value)) {
            int itemThreshold = rule.getParsedHotItems().get(value);
            return ++threadCount <= itemThreshold;
        }
        long threshold = (long)rule.getCount();
        return ++threadCount <= threshold;
    }

    return true;
}
 
Example 6
Source File: DefaultController.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public boolean canPass(Node node, int acquireCount, boolean prioritized) {
    int curCount = avgUsedTokens(node);
    if (curCount + acquireCount > count) {
        if (prioritized && grade == RuleConstant.FLOW_GRADE_QPS) {
            long currentTime;
            long waitInMs;
            currentTime = TimeUtil.currentTimeMillis();
            waitInMs = node.tryOccupyNext(currentTime, acquireCount, count);
            if (waitInMs < OccupyTimeoutProperty.getOccupyTimeout()) {
                node.addWaitingRequest(currentTime + waitInMs, acquireCount);
                node.addOccupiedPass(acquireCount);
                sleep(waitInMs);

                // PriorityWaitException indicates that the request will pass after waiting for {@link @waitInMs}.
                throw new PriorityWaitException(waitInMs);
            }
        }
        return false;
    }
    return true;
}
 
Example 7
Source File: FlowRuleUtil.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private static TrafficShapingController generateRater(/*@Valid*/ FlowRule rule) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        switch (rule.getControlBehavior()) {
            case RuleConstant.CONTROL_BEHAVIOR_WARM_UP:
                return new WarmUpController(rule.getCount(), rule.getWarmUpPeriodSec(),
                    ColdFactorProperty.coldFactor);
            case RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER:
                return new RateLimiterController(rule.getMaxQueueingTimeMs(), rule.getCount());
            case RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER:
                return new WarmUpRateLimiterController(rule.getCount(), rule.getWarmUpPeriodSec(),
                    rule.getMaxQueueingTimeMs(), ColdFactorProperty.coldFactor);
            case RuleConstant.CONTROL_BEHAVIOR_DEFAULT:
            default:
                // Default mode or unknown mode: default traffic shaping controller (fast-reject).
        }
    }
    return new DefaultController(rule.getCount(), rule.getGrade());
}
 
Example 8
Source File: ParamFlowRuleController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private <R> Result<R> checkEntityInternal(ParamFlowRuleEntity entity) {
    if (entity == null) {
        return Result.ofFail(-1, "bad rule body");
    }
    if (StringUtil.isBlank(entity.getApp())) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(entity.getIp())) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (entity.getPort() == null || entity.getPort() <= 0) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (entity.getRule() == null) {
        return Result.ofFail(-1, "rule can't be null");
    }
    if (StringUtil.isBlank(entity.getResource())) {
        return Result.ofFail(-1, "resource name cannot be null or empty");
    }
    if (entity.getCount() < 0) {
        return Result.ofFail(-1, "count should be valid");
    }
    if (entity.getGrade() != RuleConstant.FLOW_GRADE_QPS) {
        return Result.ofFail(-1, "Unknown mode (blockGrade) for parameter flow control");
    }
    if (entity.getParamIdx() == null || entity.getParamIdx() < 0) {
        return Result.ofFail(-1, "paramIdx should be valid");
    }
    if (entity.getDurationInSec() <= 0) {
        return Result.ofFail(-1, "durationInSec should be valid");
    }
    if (entity.getControlBehavior() < 0) {
        return Result.ofFail(-1, "controlBehavior should be valid");
    }
    return null;
}
 
Example 9
Source File: DefaultControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPassForQps() {
    double threshold = 10;
    TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_QPS);
    Node node = mock(Node.class);
    when(node.passQps()).thenReturn(threshold - 1)
        .thenReturn(threshold);

    assertTrue(controller.canPass(node, 1));
    assertFalse(controller.canPass(node, 1));
}
 
Example 10
Source File: ParamFlowRuleController.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private <R> Result<R> checkEntityInternal(ParamFlowRuleEntity entity) {
    if (entity == null) {
        return Result.ofFail(-1, "bad rule body");
    }
    if (StringUtil.isBlank(entity.getApp())) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(entity.getIp())) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (entity.getPort() == null || entity.getPort() <= 0) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (entity.getRule() == null) {
        return Result.ofFail(-1, "rule can't be null");
    }
    if (StringUtil.isBlank(entity.getResource())) {
        return Result.ofFail(-1, "resource name cannot be null or empty");
    }
    if (entity.getCount() < 0) {
        return Result.ofFail(-1, "count should be valid");
    }
    if (entity.getGrade() != RuleConstant.FLOW_GRADE_QPS) {
        return Result.ofFail(-1, "Unknown mode (blockGrade) for parameter flow control");
    }
    if (entity.getParamIdx() == null || entity.getParamIdx() < 0) {
        return Result.ofFail(-1, "paramIdx should be valid");
    }
    if (entity.getDurationInSec() <= 0) {
        return Result.ofFail(-1, "durationInSec should be valid");
    }
    if (entity.getControlBehavior() < 0) {
        return Result.ofFail(-1, "controlBehavior should be valid");
    }
    return null;
}
 
Example 11
Source File: ParamFlowChecker.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public static boolean passCheck(ResourceWrapper resourceWrapper, /*@Valid*/ ParamFlowRule rule, /*@Valid*/ int count,
                         Object... args) {
    if (args == null) {
        return true;
    }

    int paramIdx = rule.getParamIdx();
    if (args.length <= paramIdx) {
        return true;
    }

    // Get parameter value.
    Object value = args[paramIdx];

    // Assign value with the result of paramFlowKey method
    if (value instanceof ParamFlowArgument) {
        value = ((ParamFlowArgument) value).paramFlowKey();
    }
    // If value is null, then pass
    if (value == null) {
        return true;
    }

    if (rule.isClusterMode() && rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        return passClusterCheck(resourceWrapper, rule, count, value);
    }

    return passLocalCheck(resourceWrapper, rule, count, value);
}
 
Example 12
Source File: DefaultControllerTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPassForQps() {
    double threshold = 10;
    TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_QPS);
    Node node = mock(Node.class);
    when(node.passQps()).thenReturn(threshold - 1)
        .thenReturn(threshold);

    assertTrue(controller.canPass(node, 1));
    assertFalse(controller.canPass(node, 1));
}