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

jianglongtao 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 4877161427c Improve properties verification of JobRateLimitAlgorithm 
(#24939)
4877161427c is described below

commit 4877161427c055b6eb38a7fe35e4ee0348a18b36
Author: insist777 <[email protected]>
AuthorDate: Tue Apr 4 00:46:46 2023 +0800

    Improve properties verification of JobRateLimitAlgorithm (#24939)
    
    * Improve properties verification of JobRateLimitAlgorithm
    
    * Improve properties verification of JobRateLimitAlgorithm
    
    * Improve properties verification of JobRateLimitAlgorithm
    
    * Improve UT
    
    * Improve code style
    
    * Improve code style
    
    * Improve code style
    
    * Improve code style
    
    * Improve code style
    
    * Improve code style
---
 ...bRateLimitAlgorithmInitializationException.java | 30 +++++++++++
 .../core/ratelimit/QPSJobRateLimitAlgorithm.java   |  3 ++
 .../core/ratelimit/TPSJobRateLimitAlgorithm.java   |  3 ++
 .../ratelimit/QPSJobRateLimitAlgorithmTest.java    | 59 ++++++++++++++++++++++
 .../ratelimit/TPSJobRateLimitAlgorithmTest.java    | 59 ++++++++++++++++++++++
 5 files changed, 154 insertions(+)

diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/ratelimit/JobRateLimitAlgorithmInitializationException.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/ratelimit/JobRateLimitAlgorithmInitializationException.java
new file mode 100644
index 00000000000..d60c712f663
--- /dev/null
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/exception/job/ratelimit/JobRateLimitAlgorithmInitializationException.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.exception.job.ratelimit;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.sharding.exception.ShardingSQLException;
+
+public final class JobRateLimitAlgorithmInitializationException extends 
ShardingSQLException {
+    
+    private static final long serialVersionUID = 11009186982914568L;
+    
+    public JobRateLimitAlgorithmInitializationException(final String 
jobRateLimitType, final String reason) {
+        super(XOpenSQLState.GENERAL_ERROR, 97, "Job rate limit algorithm `%s` 
initialization failed, reason is: %s.", jobRateLimitType, reason);
+    }
+}
diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithm.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithm.java
index 30000c269a6..29427a325d1 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithm.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithm.java
@@ -20,7 +20,9 @@ package 
org.apache.shardingsphere.data.pipeline.core.ratelimit;
 import com.google.common.base.Strings;
 import com.google.common.util.concurrent.RateLimiter;
 import org.apache.shardingsphere.data.pipeline.api.job.JobOperationType;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.ratelimit.JobRateLimitAlgorithmInitializationException;
 import 
org.apache.shardingsphere.data.pipeline.spi.ratelimit.JobRateLimitAlgorithm;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import java.util.Properties;
 
@@ -40,6 +42,7 @@ public final class QPSJobRateLimitAlgorithm implements 
JobRateLimitAlgorithm {
         String qpsValue = props.getProperty(QPS_KEY);
         if (!Strings.isNullOrEmpty(qpsValue)) {
             qps = Integer.parseInt(qpsValue);
+            ShardingSpherePreconditions.checkState(qps > 0, () -> new 
JobRateLimitAlgorithmInitializationException(getType(), "QPS must be a positive 
number"));
         }
         rateLimiter = RateLimiter.create(qps);
     }
diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithm.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithm.java
index 7a961486f0b..2d01183bb53 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithm.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithm.java
@@ -20,7 +20,9 @@ package 
org.apache.shardingsphere.data.pipeline.core.ratelimit;
 import com.google.common.base.Strings;
 import com.google.common.util.concurrent.RateLimiter;
 import org.apache.shardingsphere.data.pipeline.api.job.JobOperationType;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.ratelimit.JobRateLimitAlgorithmInitializationException;
 import 
org.apache.shardingsphere.data.pipeline.spi.ratelimit.JobRateLimitAlgorithm;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 
 import java.util.Properties;
 
@@ -40,6 +42,7 @@ public final class TPSJobRateLimitAlgorithm implements 
JobRateLimitAlgorithm {
         String tpsValue = props.getProperty(TPS_KEY);
         if (!Strings.isNullOrEmpty(tpsValue)) {
             tps = Integer.parseInt(tpsValue);
+            ShardingSpherePreconditions.checkState(tps > 0, () -> new 
JobRateLimitAlgorithmInitializationException(getType(), "TPS must be a positive 
number"));
         }
         rateLimiter = RateLimiter.create(tps);
     }
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
new file mode 100644
index 00000000000..f958fb0a062
--- /dev/null
+++ 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/QPSJobRateLimitAlgorithmTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.ratelimit;
+
+import org.apache.shardingsphere.data.pipeline.api.job.JobOperationType;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.ratelimit.JobRateLimitAlgorithmInitializationException;
+import 
org.apache.shardingsphere.data.pipeline.spi.ratelimit.JobRateLimitAlgorithm;
+import org.apache.shardingsphere.infra.util.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.assertThrows;
+
+class QPSJobRateLimitAlgorithmTest {
+
+    private QPSJobRateLimitAlgorithm qpsJobRateLimitAlgorithm;
+    
+    @BeforeEach
+    void setup() {
+        qpsJobRateLimitAlgorithm = (QPSJobRateLimitAlgorithm) 
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));
+    }
+    
+    @Test
+    void assertJobRateLimitWithWrongArgumentForQPS() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("qps", "0"));
+        assertThrows(JobRateLimitAlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "QPS", props));
+    }
+    
+    @Test
+    void assertIntercept() {
+        qpsJobRateLimitAlgorithm.intercept(JobOperationType.UPDATE, 1);
+    }
+}
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
new file mode 100644
index 00000000000..4a43bfaba04
--- /dev/null
+++ 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/ratelimit/TPSJobRateLimitAlgorithmTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.data.pipeline.core.ratelimit;
+
+import org.apache.shardingsphere.data.pipeline.api.job.JobOperationType;
+import 
org.apache.shardingsphere.data.pipeline.core.exception.job.ratelimit.JobRateLimitAlgorithmInitializationException;
+import 
org.apache.shardingsphere.data.pipeline.spi.ratelimit.JobRateLimitAlgorithm;
+import org.apache.shardingsphere.infra.util.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.assertThrows;
+
+class TPSJobRateLimitAlgorithmTest {
+
+    private TPSJobRateLimitAlgorithm tpsJobRateLimitAlgorithm;
+    
+    @BeforeEach
+    void setup() {
+        tpsJobRateLimitAlgorithm = (TPSJobRateLimitAlgorithm) 
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));
+    }
+    
+    @Test
+    void assertJobRateLimitWithWrongArgumentForTPS() {
+        Properties props = PropertiesBuilder.build(new 
PropertiesBuilder.Property("tps", "0"));
+        assertThrows(JobRateLimitAlgorithmInitializationException.class, () -> 
TypedSPILoader.getService(JobRateLimitAlgorithm.class, "TPS", props));
+    }
+    
+    @Test
+    void assertIntercept() {
+        tpsJobRateLimitAlgorithm.intercept(JobOperationType.UPDATE, 1);
+    }
+}

Reply via email to