This is an automated email from the ASF dual-hosted git repository.

menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ab2057f3de Refactor QPSJobRateLimitAlgorithm and 
TPSJobRateLimitAlgorithm (#33306)
0ab2057f3de is described below

commit 0ab2057f3de8df4fe03968227553972dcdb2fe90
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Fri Oct 18 18:34:18 2024 +0800

    Refactor QPSJobRateLimitAlgorithm and TPSJobRateLimitAlgorithm (#33306)
---
 .../ratelimit/type/QPSJobRateLimitAlgorithm.java   |  5 ++--
 .../ratelimit/type/TPSJobRateLimitAlgorithm.java   |  9 ++-----
 .../ratelimit/QPSJobRateLimitAlgorithmTest.java    | 29 +++++++++-------------
 .../ratelimit/TPSJobRateLimitAlgorithmTest.java    | 28 ++++++++-------------
 4 files changed, 27 insertions(+), 44 deletions(-)

diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/QPSJobRateLimitAlgorithm.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/QPSJobRateLimitAlgorithm.java
index 3b595947f1a..c8719f677ab 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/QPSJobRateLimitAlgorithm.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/QPSJobRateLimitAlgorithm.java
@@ -49,10 +49,9 @@ public final class QPSJobRateLimitAlgorithm implements 
JobRateLimitAlgorithm {
     
     @Override
     public void intercept(final PipelineSQLOperationType type, final Number 
data) {
-        if (type != PipelineSQLOperationType.SELECT) {
-            return;
+        if (type == PipelineSQLOperationType.SELECT) {
+            rateLimiter.acquire(null == data ? 1 : data.intValue());
         }
-        rateLimiter.acquire(null != data ? data.intValue() : 1);
     }
     
     @Override
diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/TPSJobRateLimitAlgorithm.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/TPSJobRateLimitAlgorithm.java
index 7dfc48b85dd..74bcc23503b 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/TPSJobRateLimitAlgorithm.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/type/TPSJobRateLimitAlgorithm.java
@@ -49,13 +49,8 @@ public final class TPSJobRateLimitAlgorithm implements 
JobRateLimitAlgorithm {
     
     @Override
     public void intercept(final PipelineSQLOperationType type, final Number 
data) {
-        switch (type) {
-            case INSERT:
-            case DELETE:
-            case UPDATE:
-                rateLimiter.acquire(null != data ? data.intValue() : 1);
-                break;
-            default:
+        if (type != PipelineSQLOperationType.SELECT) {
+            rateLimiter.acquire(null == data ? 1 : data.intValue());
         }
     }
     
diff --git 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithmTest.java
 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithmTest.java
index 781bbddb07c..673869cae31 100644
--- 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithmTest.java
+++ 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithmTest.java
@@ -18,43 +18,38 @@
 package org.apache.shardingsphere.data.pipeline.core.ratelimit;
 
 import 
org.apache.shardingsphere.data.pipeline.core.constant.PipelineSQLOperationType;
-import 
org.apache.shardingsphere.data.pipeline.core.ratelimit.type.QPSJobRateLimitAlgorithm;
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.util.Properties;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class QPSJobRateLimitAlgorithmTest {
     
-    private QPSJobRateLimitAlgorithm qpsJobRateLimitAlgorithm;
-    
-    @BeforeEach
-    void setup() {
-        qpsJobRateLimitAlgorithm = (QPSJobRateLimitAlgorithm) 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "QPS");
-    }
+    private final JobRateLimitAlgorithm algorithm = 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "QPS");
     
     @Test
-    void assertInit() {
-        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("qps", "1"));
-        assertThat(TypedSPILoader.getService(JobRateLimitAlgorithm.class, 
"QPS", props), instanceOf(QPSJobRateLimitAlgorithm.class));
+    void assertInitFailed() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "0"));
+        assertThrows(AlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
     }
     
     @Test
-    void assertJobRateLimitWithWrongArgumentForQPS() {
-        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("qps", "0"));
-        assertThrows(AlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "QPS", props));
+    void assertInitSuccess() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "1"));
+        assertDoesNotThrow(() -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
     }
     
     @Test
     void assertIntercept() {
-        assertDoesNotThrow(() -> 
qpsJobRateLimitAlgorithm.intercept(PipelineSQLOperationType.UPDATE, 1));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.SELECT, null));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.SELECT, 1));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.INSERT, null));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.UPDATE, 1));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.DELETE, 2));
     }
 }
diff --git 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithmTest.java
 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithmTest.java
index beb64714db6..344f2e270b8 100644
--- 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithmTest.java
+++ 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithmTest.java
@@ -18,43 +18,37 @@
 package org.apache.shardingsphere.data.pipeline.core.ratelimit;
 
 import 
org.apache.shardingsphere.data.pipeline.core.constant.PipelineSQLOperationType;
-import 
org.apache.shardingsphere.data.pipeline.core.ratelimit.type.TPSJobRateLimitAlgorithm;
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import java.util.Properties;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class TPSJobRateLimitAlgorithmTest {
     
-    private TPSJobRateLimitAlgorithm tpsJobRateLimitAlgorithm;
-    
-    @BeforeEach
-    void setup() {
-        tpsJobRateLimitAlgorithm = (TPSJobRateLimitAlgorithm) 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS");
-    }
+    private final JobRateLimitAlgorithm algorithm = 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS");
     
     @Test
-    void assertInit() {
-        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "1"));
-        assertThat(TypedSPILoader.getService(JobRateLimitAlgorithm.class, 
"TPS", props), instanceOf(TPSJobRateLimitAlgorithm.class));
+    void assertInitFailed() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "0"));
+        assertThrows(AlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
     }
     
     @Test
-    void assertJobRateLimitWithWrongArgumentForTPS() {
-        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "0"));
-        assertThrows(AlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
+    void assertInitSuccess() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "1"));
+        assertDoesNotThrow(() -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
     }
     
     @Test
     void assertIntercept() {
-        assertDoesNotThrow(() -> 
tpsJobRateLimitAlgorithm.intercept(PipelineSQLOperationType.UPDATE, 1));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.INSERT, null));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.UPDATE, 1));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.DELETE, 2));
+        assertDoesNotThrow(() -> 
algorithm.intercept(PipelineSQLOperationType.SELECT, null));
     }
 }

Reply via email to