You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/01/11 02:33:53 UTC
[shardingsphere] branch master updated: add SQLMatchTrafficAlgorithm for traffic (#14679)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 1f4fa9c add SQLMatchTrafficAlgorithm for traffic (#14679)
1f4fa9c is described below
commit 1f4fa9cca5ea9f7bf3ed512a4afbb640fa47835d
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Tue Jan 11 10:32:44 2022 +0800
add SQLMatchTrafficAlgorithm for traffic (#14679)
---
.../RoundRobinReplicaLoadBalanceAlgorithm.java | 7 ---
.../RoundRobinTrafficLoadBalanceAlgorithm.java | 7 ---
.../traffic/segment/SQLMatchTrafficAlgorithm.java | 72 ++++++++++++++++++++++
...che.shardingsphere.traffic.spi.TrafficAlgorithm | 1 +
.../traffic/SQLHintTrafficAlgorithmTest.java | 49 +++++++++++++++
.../traffic/SQLMatchTrafficAlgorithmTest.java | 60 ++++++++++++++++++
6 files changed, 182 insertions(+), 14 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/RoundRobinReplicaLoadBalanceAlgorithm.java b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/RoundRobinReplicaLoadBalanceAlgorithm.java
index 55238cc..56b5e26 100644
--- a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/RoundRobinReplicaLoadBalanceAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/RoundRobinReplicaLoadBalanceAlgorithm.java
@@ -17,26 +17,19 @@
package org.apache.shardingsphere.readwritesplitting.algorithm;
-import lombok.Getter;
-import lombok.Setter;
import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
import java.util.List;
-import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Round-robin replica load-balance algorithm.
*/
-@Getter
-@Setter
public final class RoundRobinReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
private static final ConcurrentHashMap<String, AtomicInteger> COUNTS = new ConcurrentHashMap<>();
- private Properties props = new Properties();
-
@Override
public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0);
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
index adb726d..509ea4a 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
@@ -17,26 +17,19 @@
package org.apache.shardingsphere.traffic.algorithm.loadbalance;
-import lombok.Getter;
-import lombok.Setter;
import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
import java.util.List;
-import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Round-robin traffic load balance algorithm.
*/
-@Getter
-@Setter
public final class RoundRobinTrafficLoadBalanceAlgorithm implements TrafficLoadBalanceAlgorithm {
private static final ConcurrentHashMap<String, AtomicInteger> COUNTS = new ConcurrentHashMap<>();
- private Properties props = new Properties();
-
@Override
public String getInstanceId(final String name, final List<String> instanceIds) {
AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0);
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java
new file mode 100644
index 0000000..d43bb7d
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/segment/SQLMatchTrafficAlgorithm.java
@@ -0,0 +1,72 @@
+/*
+ * 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 com.google.common.base.CharMatcher;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
+import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficAlgorithm;
+import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+
+import java.util.Collection;
+import java.util.Properties;
+import java.util.TreeSet;
+
+/**
+ * SQL match traffic algorithm.
+ */
+@Getter
+@Setter
+public final class SQLMatchTrafficAlgorithm implements SegmentTrafficAlgorithm {
+
+ private static final String SQL_PROPS_KEY = "sql";
+
+ private static final String EXCLUDED_CHARACTERS = "[]`'\" ";
+
+ private Properties props = new Properties();
+
+ private Collection<String> sql;
+
+ @Override
+ public void init() {
+ Preconditions.checkArgument(props.containsKey(SQL_PROPS_KEY), "%s cannot be null.", SQL_PROPS_KEY);
+ sql = getExactlySQL(props.getProperty(SQL_PROPS_KEY));
+ }
+
+ private Collection<String> getExactlySQL(final String value) {
+ Collection<String> values = Splitter.on(";").trimResults().omitEmptyStrings().splitToList(value);
+ Collection<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ for (String each : values) {
+ result.add(CharMatcher.anyOf(EXCLUDED_CHARACTERS).removeFrom(each));
+ }
+ return result;
+ }
+
+ @Override
+ public boolean match(final SegmentTrafficValue segmentTrafficValue) {
+ return sql.contains(SQLUtil.trimSemicolon(CharMatcher.anyOf(EXCLUDED_CHARACTERS).removeFrom(segmentTrafficValue.getSql())));
+ }
+
+ @Override
+ public String getType() {
+ return "SQL_MATCH";
+ }
+}
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 e0228e0..b20c713 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
@@ -16,3 +16,4 @@
#
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm
+org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLMatchTrafficAlgorithm
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
new file mode 100644
index 0000000..089b108
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLHintTrafficAlgorithmTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm;
+import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public final class SQLHintTrafficAlgorithmTest {
+
+ private SQLHintTrafficAlgorithm sqlHintAlgorithm;
+
+ @Before
+ public void setUp() {
+ sqlHintAlgorithm = new SQLHintTrafficAlgorithm();
+ sqlHintAlgorithm.getProps().put("foo", "bar");
+ sqlHintAlgorithm.getProps().put("test", "234");
+ sqlHintAlgorithm.init();
+ }
+
+ @Test
+ public void assertMatchWhenSQLHintAllMatch() {
+ assertTrue(sqlHintAlgorithm.match(new HintTrafficValue<>("/* ShardingSphere hint: foo=bar , test=234 */")));
+ }
+
+ @Test
+ public void assertMatchWhenSQLHintOneMatch() {
+ assertFalse(sqlHintAlgorithm.match(new HintTrafficValue<>("/* ShardingSphere hint: foo=bar */")));
+ }
+}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java
new file mode 100644
index 0000000..6d48f51
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/algorithm/traffic/SQLMatchTrafficAlgorithmTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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;
+
+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.algorithm.traffic.segment.SQLMatchTrafficAlgorithm;
+import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class SQLMatchTrafficAlgorithmTest {
+
+ private SQLMatchTrafficAlgorithm sqlMatchAlgorithm;
+
+ @Before
+ public void setUp() {
+ sqlMatchAlgorithm = new SQLMatchTrafficAlgorithm();
+ sqlMatchAlgorithm.getProps().put("sql", "SELECT * FROM t_order; UPDATE t_order SET order_id = ? WHERE user_id = ?;");
+ sqlMatchAlgorithm.init();
+ }
+
+ @Test
+ public void assertMatchWhenExistSQLMatch() {
+ SQLStatement sqlStatement = mock(SelectStatement.class);
+ assertTrue(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "SELECT * FROM t_order")));
+ assertTrue(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "select * from t_order;")));
+ assertTrue(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "select * from `t_order`;")));
+ assertTrue(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE t_order SET order_id = ? WHERE user_id = ?;")));
+ assertTrue(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "UPDATE `t_order` SET `order_id` = ? WHERE user_id = ?;")));
+ }
+
+ @Test
+ public void assertMatchWhenNotExistSQLMatch() {
+ SQLStatement sqlStatement = mock(SelectStatement.class);
+ assertFalse(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "select * from `t_order` where order_id = ?;")));
+ assertFalse(sqlMatchAlgorithm.match(new SegmentTrafficValue(sqlStatement, "TRUNCATE TABLE `t_order` ")));
+ 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 = ?;")));
+ }
+}