Repository: alibaba/Sentinel
Pull request #2455 (2021-11-22 04:14:42)

fix system rule inbound QPS limit bug: use passQps instead of successQps
<!--  Thanks for submitting a pull request! Here are some tips for you:
1. Please make sure you have read and understood the contributing guidelines: https://github.com/alibaba/Sentinel/blob/master/CONTRIBUTING.md
2. Please make sure the PR has a corresponding issue.
-->

### Describe what this PR does / why we need it
系统保护规则限制qps失败

### Does this pull request fix one issue?
修复了系统保护规则限制qps功能。
<!--If that, add "Fixes #xxxx" below in the next line. For example, Fixes #15. Otherwise, add "NONE" -->

### Describe how you did it


### Describe how to verify it


### Special notes for reviews


CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455) before we can accept your contribution.<br/><hr/>**wucheng** seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please [add the email address used for this commit to your account](https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user/#commits-are-not-linked-to-any-user).<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/alibaba/Sentinel?pullRequest=2455) it.</sub>
sczyh30: Hi, thanks for contributing. Could you please [sign the CLA here](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455)? And please make sure the email of your commits match your GitHub email. You may refer to the instruction [here](https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user/#commits-are-not-linked-to-any-user), or rebase your commits with the correct email.

---

感谢贡献，请将 commit 对应的 email 调整成与 GitHub 的 email 相匹配并 [确认一下 CLA](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455)。
wucheng1997: > Hi, thanks for contributing. Could you please [sign the CLA here](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455)? And please make sure the email of your commits match your GitHub email. You may refer to the instruction [here](https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user/#commits-are-not-linked-to-any-user), or rebase your commits with the correct email.
> 
> 感谢贡献，请将 commit 对应的 email 调整成与 GitHub 的 email 相匹配并 [确认一下 CLA](https://cla-assistant.io/alibaba/Sentinel?pullRequest=2455)。

已经修改完毕，谢谢。

Patch:
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManager.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManager.java
index d860344e..4ee5f17c 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManager.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManager.java
@@ -287,7 +287,7 @@ public final class SystemRuleManager {
      * @param resourceWrapper the resource.
      * @throws BlockException when any system rule's threshold is exceeded.
      */
-    public static void checkSystem(ResourceWrapper resourceWrapper) throws BlockException {
+    public static void checkSystem(ResourceWrapper resourceWrapper, int count) throws BlockException {
         if (resourceWrapper == null) {
             return;
         }
@@ -302,8 +302,8 @@ public final class SystemRuleManager {
         }
 
         // total qps
-        double currentQps = Constants.ENTRY_NODE == null ? 0.0 : Constants.ENTRY_NODE.successQps();
-        if (currentQps > qps) {
+        double currentQps = Constants.ENTRY_NODE == null ? 0.0 : Constants.ENTRY_NODE.passQps();
+        if (currentQps + count > qps) {
             throw new SystemBlockException(resourceWrapper.getName(), "qps");
         }
 
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java
index c98e491c..44b54727 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/system/SystemSlot.java
@@ -35,7 +35,7 @@ public class SystemSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
     @Override
     public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
                       boolean prioritized, Object... args) throws Throwable {
-        SystemRuleManager.checkSystem(resourceWrapper);
+        SystemRuleManager.checkSystem(resourceWrapper, count);
         fireEntry(context, resourceWrapper, node, count, prioritized, args);
     }
 


Test patch:
diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManagerTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManagerTest.java
index fcd1f665..2e7ac3a3 100644
--- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManagerTest.java
+++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/system/SystemRuleManagerTest.java
@@ -92,7 +92,8 @@ public class SystemRuleManagerTest {
 
         boolean blocked = false;
         try {
-            SystemRuleManager.checkSystem(new StringResourceWrapper("testCheckMaxCpuUsageNotBBR", EntryType.IN));
+            StringResourceWrapper resourceWrapper = new StringResourceWrapper("testCheckMaxCpuUsageNotBBR", EntryType.IN);
+            SystemRuleManager.checkSystem(resourceWrapper, 1);
         } catch (BlockException ex) {
             blocked = true;
         }