开发者社区 > 云原生 > 正文

真实的qps有时和FlowRule中设置的qps的不一致

我将wiki中的案例代码拷贝到我的IDEA中,然后稍作修改,具体如下:

public static void main(String[] args) { String resourceName = "helloWorld"; initFlowRules(resourceName);

    long begin = System.currentTimeMillis();
    long end = begin + 1000;
    int success = 0;
    int fail = 0;

    while (System.currentTimeMillis() < end) {
        Entry entry = null;
        try {
            entry = SphU.entry(resourceName);
            success++;
        } catch (BlockException e1) {
            fail++;
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
    }

    System.out.println("Begin time: " + begin);
    System.out.println("End time: " + end);
    System.out.println("Success: " + success);
    System.out.println("Fail: " + fail);
    System.exit(0);
}

private static void initFlowRules(String name){
    List<FlowRule> rules = new ArrayList<>();
    FlowRule rule = new FlowRule();
    rule.setResource(name);
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    // Set limit QPS to 20.
    rule.setCount(20);
    rules.add(rule);
    FlowRuleManager.loadRules(rules);
}

初始化FlowRule时,设置的qps为20,我期待程序运行结束后,success输出的值也应该为20, 然而实际运行过程中,日志显示success的值 有时为20,有时为40.

Begin time: 1533132761258 End time: 1533132762258 Success: 40 Fail: 140633

原提问者GitHub用户xflonga

展开
收起
码字王 2023-05-19 19:39:11 161 0
1 条回答
写回答
取消 提交回答
  • Sentinel 指标统计的滑动窗口中每个小窗口的初始时间都是经过对齐的(间隔默认 500 ms),而不是当前时间,因此会存在时间偏差导致多通过的情况。

    比如流量的时间范围(取后四位):1258 - 2258

    理想的情况是:

    瞬间20个请求 这段时间无法通过 ↓ ↓ | o | | 1258 1758 2258 (windowStart)

    但实际的窗口是:

    瞬间20个请求 这段时间无法通过 第一个窗口过期,因此又可以进入 20 个 ↓ ↓ ↓ | o | | o | 1000 1500 2000 2500 (windowStart)

    原回答者GitHub用户sczyh30

    2023-05-19 22:09:30
    赞同 展开评论 打赏
问答分类:
问答地址:

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载