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

The following examples show how to use com.alibaba.csp.sentinel.slots.block.RuleConstant#DEGRADE_GRADE_RT . 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: DegradeRuleManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public static boolean isValidRule(DegradeRule rule) {
    boolean baseValid = rule != null && !StringUtil.isBlank(rule.getResource())
        && rule.getCount() >= 0 && rule.getTimeWindow() > 0;
    if (!baseValid) {
        return false;
    }
    int maxAllowedRt = Constants.TIME_DROP_VALVE;
    if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_RT) {
        if (rule.getRtSlowRequestAmount() <= 0) {
            return false;
        }
        // Warn for RT mode that exceeds the {@code TIME_DROP_VALVE}.
        if (rule.getCount() > maxAllowedRt) {
            RecordLog.warn(String.format("[DegradeRuleManager] WARN: setting large RT threshold (%.1f ms)"
                    + " in RT mode will not take effect since it exceeds the max allowed value (%d ms)",
                rule.getCount(), maxAllowedRt));
        }
    }

    // Check exception ratio mode.
    if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) {
        return rule.getCount() <= 1 && rule.getMinRequestAmount() > 0;
    }
    return true;
}
 
Example 2
Source File: DegradeRuleManager.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static boolean isValidRule(DegradeRule rule) {
    boolean baseValid = rule != null && !StringUtil.isBlank(rule.getResource())
        && rule.getCount() >= 0 && rule.getTimeWindow() > 0;
    if (!baseValid) {
        return false;
    }
    int maxAllowedRt = SentinelConfig.statisticMaxRt();
    if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_RT) {
        if (rule.getRtSlowRequestAmount() <= 0) {
            return false;
        }
        // Warn for RT mode that exceeds the {@code TIME_DROP_VALVE}.
        if (rule.getCount() > maxAllowedRt) {
            RecordLog.warn(String.format("[DegradeRuleManager] WARN: setting large RT threshold (%.1f ms)"
                    + " in RT mode will not take effect since it exceeds the max allowed value (%d ms)",
                rule.getCount(), maxAllowedRt));
        }
    }

    // Check exception ratio mode.
    if (rule.getGrade() == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) {
        return rule.getCount() <= 1 && rule.getMinRequestAmount() > 0;
    }
    return true;
}
 
Example 3
Source File: DegradeController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("/new.json")
public Result<DegradeRuleEntity> add(HttpServletRequest request,
                                     String app, String ip, Integer port, String limitApp, String resource,
                                     Double count, Integer timeWindow, Integer grade) {
    AuthUser authUser = authService.getAuthUser(request);
    authUser.authTarget(app, PrivilegeType.WRITE_RULE);

    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (StringUtil.isBlank(limitApp)) {
        return Result.ofFail(-1, "limitApp can't be null or empty");
    }
    if (StringUtil.isBlank(resource)) {
        return Result.ofFail(-1, "resource can't be null or empty");
    }
    if (count == null) {
        return Result.ofFail(-1, "count can't be null");
    }
    if (timeWindow == null) {
        return Result.ofFail(-1, "timeWindow can't be null");
    }
    if (grade == null) {
        return Result.ofFail(-1, "grade can't be null");
    }
    if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
        return Result.ofFail(-1, "Invalid grade: " + grade);
    }
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    entity.setLimitApp(limitApp.trim());
    entity.setResource(resource.trim());
    entity.setCount(count);
    entity.setTimeWindow(timeWindow);
    entity.setGrade(grade);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.info("publish degrade rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
 
Example 4
Source File: DegradeController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("/save.json")
public Result<DegradeRuleEntity> updateIfNotNull(HttpServletRequest request,
                                                 Long id, String app, String limitApp, String resource,
                                                 Double count, Integer timeWindow, Integer grade) {
    AuthUser authUser = authService.getAuthUser(request);
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    if (grade != null) {
        if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
            return Result.ofFail(-1, "Invalid grade: " + grade);
        }
    }
    DegradeRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    authUser.authTarget(entity.getApp(), PrivilegeType.WRITE_RULE);
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }

    if (StringUtil.isNotBlank(limitApp)) {
        entity.setLimitApp(limitApp.trim());
    }
    if (StringUtil.isNotBlank(resource)) {
        entity.setResource(resource.trim());
    }
    if (count != null) {
        entity.setCount(count);
    }
    if (timeWindow != null) {
        entity.setTimeWindow(timeWindow);
    }
    if (grade != null) {
        entity.setGrade(grade);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish degrade rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
 
Example 5
Source File: DegradeController.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> add(String app, String ip, Integer port, String limitApp, String resource,
                                     Double count, Integer timeWindow, Integer grade) {
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (StringUtil.isBlank(limitApp)) {
        return Result.ofFail(-1, "limitApp can't be null or empty");
    }
    if (StringUtil.isBlank(resource)) {
        return Result.ofFail(-1, "resource can't be null or empty");
    }
    if (count == null) {
        return Result.ofFail(-1, "count can't be null");
    }
    if (timeWindow == null) {
        return Result.ofFail(-1, "timeWindow can't be null");
    }
    if (grade == null) {
        return Result.ofFail(-1, "grade can't be null");
    }
    if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
        return Result.ofFail(-1, "Invalid grade: " + grade);
    }
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    entity.setLimitApp(limitApp.trim());
    entity.setResource(resource.trim());
    entity.setCount(count);
    entity.setTimeWindow(timeWindow);
    entity.setGrade(grade);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.info("publish degrade rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
 
Example 6
Source File: DegradeController.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@ResponseBody
@RequestMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> updateIfNotNull(Long id, String app, String limitApp, String resource,
                                                 Double count, Integer timeWindow, Integer grade) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    if (grade != null) {
        if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
            return Result.ofFail(-1, "Invalid grade: " + grade);
        }
    }
    DegradeRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }

    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }

    if (StringUtil.isNotBlank(limitApp)) {
        entity.setLimitApp(limitApp.trim());
    }
    if (StringUtil.isNotBlank(resource)) {
        entity.setResource(resource.trim());
    }
    if (count != null) {
        entity.setCount(count);
    }
    if (timeWindow != null) {
        entity.setTimeWindow(timeWindow);
    }
    if (grade != null) {
        entity.setGrade(grade);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish degrade rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}