This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 b9db57c Add SQLRegexTrafficAlgorithm for traffic rule (#15194)
b9db57c is described below
commit b9db57c2d9b5a2fbd8a54f2833a97137c7f61bf6
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Sat Jan 29 20:01:33 2022 +0800
Add SQLRegexTrafficAlgorithm for traffic rule (#15194)
* Add SQLRegexTrafficAlgorithm for traffic rule
* move pattern compile to init method in ColumnRegexMatchShadowAlgorithm
* optimize logic
---
.../column/ColumnRegexMatchShadowAlgorithm.java | 7 ++-
.../traffic/segment/SQLRegexTrafficAlgorithm.java | 30 +++++++---
...che.shardingsphere.traffic.spi.TrafficAlgorithm | 1 +
.../segment/SQLRegexTrafficAlgorithmTest.java | 66 ++++++++++++++++++++++
4 files changed, 95 insertions(+), 9 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
index 63976ea..b8236df 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
@@ -19,6 +19,8 @@ package
org.apache.shardingsphere.shadow.algorithm.shadow.column;
import com.google.common.base.Preconditions;
+import java.util.regex.Pattern;
+
/**
* Column regex match shadow algorithm.
*/
@@ -26,14 +28,17 @@ public final class ColumnRegexMatchShadowAlgorithm extends
AbstractColumnMatchSh
private static final String REGEX_PROPS_KEY = "regex";
+ private Pattern regex;
+
@Override
protected void checkProps() {
Preconditions.checkNotNull(getProps().get(REGEX_PROPS_KEY), "Column
regex match shadow algorithm regex cannot be null.");
+ regex =
Pattern.compile(String.valueOf(getProps().get(REGEX_PROPS_KEY)));
}
@Override
protected boolean isMatchValue(final Comparable<?> value) {
- return
String.valueOf(value).matches(String.valueOf(getProps().get(REGEX_PROPS_KEY)));
+ return regex.matcher(String.valueOf(value)).matches();
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
similarity index 52%
copy from
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
copy to
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
index 63976ea..47a310d 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithm.java
+++
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithm.java
@@ -15,29 +15,43 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.algorithm.shadow.column;
+package org.apache.shardingsphere.traffic.algorithm.traffic.segment;
import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficAlgorithm;
+import
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+
+import java.util.Properties;
+import java.util.regex.Pattern;
/**
- * Column regex match shadow algorithm.
+ * SQL regex traffic algorithm.
*/
-public final class ColumnRegexMatchShadowAlgorithm extends
AbstractColumnMatchShadowAlgorithm {
+@Getter
+@Setter
+public final class SQLRegexTrafficAlgorithm implements SegmentTrafficAlgorithm
{
private static final String REGEX_PROPS_KEY = "regex";
+ private Properties props = new Properties();
+
+ private Pattern regex;
+
@Override
- protected void checkProps() {
- Preconditions.checkNotNull(getProps().get(REGEX_PROPS_KEY), "Column
regex match shadow algorithm regex cannot be null.");
+ public void init() {
+ Preconditions.checkArgument(props.containsKey(REGEX_PROPS_KEY), "%s
cannot be null.", REGEX_PROPS_KEY);
+ regex = Pattern.compile(String.valueOf(props.get(REGEX_PROPS_KEY)));
}
@Override
- protected boolean isMatchValue(final Comparable<?> value) {
- return
String.valueOf(value).matches(String.valueOf(getProps().get(REGEX_PROPS_KEY)));
+ public boolean match(final SegmentTrafficValue segmentTrafficValue) {
+ return regex.matcher(segmentTrafficValue.getSql()).matches();
}
@Override
public String getType() {
- return "REGEX_MATCH";
+ return "SQL_REGEX";
}
}
diff --git
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
index b20c713..2b854c9 100644
---
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
+++
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
@@ -17,3 +17,4 @@
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm
org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLMatchTrafficAlgorithm
+org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLRegexTrafficAlgorithm
diff --git
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
new file mode 100644
index 0000000..08487ee
--- /dev/null
+++
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLRegexTrafficAlgorithmTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.algorithm.traffic.segment;
+
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class SQLRegexTrafficAlgorithmTest {
+
+ private SQLRegexTrafficAlgorithm sqlRegexAlgorithm;
+
+ @Before
+ public void setUp() {
+ sqlRegexAlgorithm = new SQLRegexTrafficAlgorithm();
+ sqlRegexAlgorithm.getProps().put("regex", "(?i)^(UPDATE|SELECT).*WHERE
user_id.*");
+ sqlRegexAlgorithm.init();
+ }
+
+ @Test
+ public void assertMatchWhenExistSQLRegexMatch() {
+ SQLStatement sqlStatement = mock(SelectStatement.class);
+ assertTrue(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "UPDATE t_order SET order_id = ? WHERE
user_id = ?;")));
+ assertTrue(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "update `t_order` SET `order_id` = ? WHERE
user_id = ?;")));
+ 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
+ public void assertMatchWhenNotExistSQLRegexMatch() {
+ SQLStatement sqlStatement = mock(SelectStatement.class);
+ assertFalse(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "SELECT * FROM t_order")));
+ assertFalse(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "select * from t_order;")));
+ assertFalse(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "select * from `t_order`;")));
+ assertFalse(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "TRUNCATE TABLE `t_order` ")));
+ assertFalse(sqlRegexAlgorithm.match(new
SegmentTrafficValue(sqlStatement, "UPDATE `t_order` SET `order_id` = ?;")));
+ }
+
+ @Test
+ public void assertGetType() {
+ assertThat(sqlRegexAlgorithm.getType(), is("SQL_REGEX"));
+ }
+}