You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/04/10 13:54:00 UTC
[shardingsphere] branch master updated: Improve properties verification of TrafficAlgorithm #24754 (#25036)
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 7d4fddc6381 Improve properties verification of TrafficAlgorithm #24754 (#25036)
7d4fddc6381 is described below
commit 7d4fddc63813ae03e8ed9584ad675d168728798e
Author: Ntmac <47...@users.noreply.github.com>
AuthorDate: Mon Apr 10 21:53:43 2023 +0800
Improve properties verification of TrafficAlgorithm #24754 (#25036)
* solve qt#24754
* Improve properties verification of TrafficAlgorithm #24754
* Improve properties verification of TrafficAlgorithm #24754(Sorry, I am not famaliar with the operation of Github)
* Improve properties verification of TrafficAlgorithm #24754
* verify the test code #24754
* verify the test code #24754
* 1
* add Exception.class
* modify sql information , exception type and code
* modify regex information
* modify test
* modify test import
* change CRLF style to LF style
* modify the Preconditions to ShardingSpherePreconditions
* modify the properties of RegexTest
* remove sqlMatchAlgorithm and sqlRegexAlgorithm
* modify the message
* add license
* .
---
.../traffic/segment/SQLMatchTrafficAlgorithm.java | 9 ++++--
.../traffic/segment/SQLRegexTrafficAlgorithm.java | 9 ++++--
...entTrafficAlgorithmInitializationException.java | 33 ++++++++++++++++++++++
.../segment/SQLMatchTrafficAlgorithmTest.java | 7 +++++
.../segment/SQLRegexTrafficAlgorithmTest.java | 11 ++++++--
5 files changed, 63 insertions(+), 6 deletions(-)
diff --git a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java
index 66090ca27c1..f3ab37ec020 100644
--- a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java
+++ b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java
@@ -18,11 +18,13 @@
package org.apache.shardingsphere.traffic.algorithm.traffic.segment;
import com.google.common.base.CharMatcher;
-import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficAlgorithm;
import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+import org.apache.shardingsphere.traffic.exception.segment.SegmentTrafficAlgorithmInitializationException;
import java.util.Collection;
import java.util.Properties;
@@ -41,8 +43,11 @@ public final class SQLMatchTrafficAlgorithm implements SegmentTrafficAlgorithm {
@Override
public void init(final Properties props) {
- Preconditions.checkArgument(props.containsKey(SQL_PROPS_KEY), "%s cannot be null.", SQL_PROPS_KEY);
+ ShardingSpherePreconditions.checkState(props.containsKey(SQL_PROPS_KEY),
+ () -> new SegmentTrafficAlgorithmInitializationException(SQLMatchTrafficAlgorithm.class.getName(), String.format("%s cannot be null", SQL_PROPS_KEY)));
sql = getExactlySQL(props.getProperty(SQL_PROPS_KEY));
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(String.valueOf(sql)),
+ () -> new SegmentTrafficAlgorithmInitializationException(SQLMatchTrafficAlgorithm.class.getName(), "sql must be not empty"));
}
private Collection<String> getExactlySQL(final String value) {
diff --git a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
index 1543c05d17e..963bb201402 100644
--- a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
+++ b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
@@ -17,9 +17,11 @@
package org.apache.shardingsphere.traffic.algorithm.traffic.segment;
-import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficAlgorithm;
import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+import org.apache.shardingsphere.traffic.exception.segment.SegmentTrafficAlgorithmInitializationException;
import java.util.Properties;
import java.util.regex.Pattern;
@@ -35,8 +37,11 @@ public final class SQLRegexTrafficAlgorithm implements SegmentTrafficAlgorithm {
@Override
public void init(final Properties props) {
- Preconditions.checkArgument(props.containsKey(REGEX_PROPS_KEY), "%s can not be null.", REGEX_PROPS_KEY);
+ ShardingSpherePreconditions.checkState(props.containsKey(REGEX_PROPS_KEY),
+ () -> new SegmentTrafficAlgorithmInitializationException(SQLRegexTrafficAlgorithm.class.getName(), String.format("%s cannot be null", REGEX_PROPS_KEY)));
regex = Pattern.compile(props.getProperty(REGEX_PROPS_KEY));
+ ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(String.valueOf(regex)),
+ () -> new SegmentTrafficAlgorithmInitializationException(SQLRegexTrafficAlgorithm.class.getName(), "regex must be not empty"));
}
@Override
diff --git a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/exception/segment/SegmentTrafficAlgorithmInitializationException.java b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/exception/segment/SegmentTrafficAlgorithmInitializationException.java
new file mode 100644
index 00000000000..7f83664d7f8
--- /dev/null
+++ b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/exception/segment/SegmentTrafficAlgorithmInitializationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.traffic.exception.segment;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.traffic.exception.TrafficException;
+
+/**
+ * Segment traffic algorithm initialization exception.
+ */
+public final class SegmentTrafficAlgorithmInitializationException extends TrafficException {
+
+ private static final long serialVersionUID = 7514112348846701284L;
+
+ public SegmentTrafficAlgorithmInitializationException(final String segmentTrafficType, final String reason) {
+ super(XOpenSQLState.GENERAL_ERROR, 98, "Segmentation traffic algorithm `%s` initialization failed, reason is: %s.", segmentTrafficType, reason);
+ }
+}
diff --git a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
index 16b4e4d4732..55579f8302c 100644
--- a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
+++ b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithmTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
class SQLMatchTrafficAlgorithmTest {
@@ -59,4 +60,10 @@ class SQLMatchTrafficAlgorithmTest {
assertFalse(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE `t_order` SET `order_id` = ?;")));
assertFalse(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE `t_order_item` SET `order_id` = ? WHERE user_id = ?;")));
}
+
+ @Test
+ void assertInitWithIllegalProps() {
+ assertThrows(IllegalArgumentException.class, () -> TypedSPILoader.getService(SQLMatchTrafficAlgorithm.class, "SQL_MATCH",
+ PropertiesBuilder.build(new Property("sql", ""))));
+ }
}
diff --git a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
index b8b23b77da3..87020291f85 100644
--- a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
+++ b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
class SQLRegexTrafficAlgorithmTest {
@@ -40,7 +41,7 @@ class SQLRegexTrafficAlgorithmTest {
sqlRegexAlgorithm = (SQLRegexTrafficAlgorithm) TypedSPILoader.getService(
TrafficAlgorithm.class, "SQL_REGEX", PropertiesBuilder.build(new Property("regex", "(?i)^(UPDATE|SELECT).*WHERE user_id.*")));
}
-
+
@Test
void assertMatchWhenExistSQLRegexMatch() {
SQLStatement sqlStatement = mock(SelectStatement.class);
@@ -49,7 +50,7 @@ class SQLRegexTrafficAlgorithmTest {
assertTrue(sqlRegexAlgorithm.match(new SegmentTrafficValue(sqlStatement, "select * from `t_order` where user_id = ?;")));
assertTrue(sqlRegexAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE `t_order_item` SET `order_id` = ? WHERE user_id = ?;")));
}
-
+
@Test
void assertMatchWhenNotExistSQLRegexMatch() {
SQLStatement sqlStatement = mock(SelectStatement.class);
@@ -59,4 +60,10 @@ class SQLRegexTrafficAlgorithmTest {
assertFalse(sqlRegexAlgorithm.match(new SegmentTrafficValue(sqlStatement, "TRUNCATE TABLE `t_order` ")));
assertFalse(sqlRegexAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE `t_order` SET `order_id` = ?;")));
}
+
+ @Test
+ void assertInitWithIllegalProps() {
+ assertThrows(IllegalArgumentException.class, () -> TypedSPILoader.getService(SQLRegexTrafficAlgorithm.class, "SQL_REGEX",
+ PropertiesBuilder.build(new Property("regex", ""))));
+ }
}