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 649d175  Add HintTrafficAlgorithm and SegmentTrafficAlgorithm 
interface and adjust traffic match logic (#14423)
649d175 is described below

commit 649d1758f4521432f34675825ad6c5d61be6c66a
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Thu Dec 30 11:12:27 2021 +0800

    Add HintTrafficAlgorithm and SegmentTrafficAlgorithm interface and adjust 
traffic match logic (#14423)
    
    * Add HintTrafficAlgorithm and SegmentTrafficAlgorithm interface and adjust 
traffic match logic
    
    * add final modifier
    
    * remove useless final
    
    * implement SegmentMatchTrafficAlgorithm
    
    * add java doc
    
    * remove SegmentMatchTrafficAlgorithm and modify SegmentTrafficValue
    
    * remove TrafficSegmentType and TrafficStatementType
---
 .../infra/hint/SQLHintExtractor.java               | 34 +-----------
 .../shardingsphere/infra/hint/SQLHintUtils.java    | 57 ++++++++++++++++++++
 .../api/config/TrafficRuleConfiguration.java       |  1 -
 .../TrafficStrategyConfiguration.java              |  2 +-
 .../TrafficValue.java}                             | 21 ++------
 .../hint/HintTrafficAlgorithm.java}                | 29 +++++------
 .../hint/HintTrafficValue.java}                    | 19 +++----
 .../segment/SegmentTrafficAlgorithm.java}          | 27 ++++------
 .../segment/SegmentTrafficValue.java}              | 18 +++----
 .../RandomTrafficLoadBalanceAlgorithm.java}        | 29 ++++++-----
 .../traffic/hint/SQLHintTrafficAlgorithm.java      | 60 ++++++++++++++++++++++
 .../shardingsphere/traffic/rule/TrafficRule.java   | 40 ++++++++++++++-
 .../TrafficStrategyConfigurationYamlSwapper.java   |  2 +-
 ...che.shardingsphere.traffic.spi.TrafficAlgorithm | 18 +++++++
 ...gsphere.traffic.spi.TrafficLoadBalanceAlgorithm | 18 +++++++
 .../TrafficRuleConfigurationYamlSwapperTest.java   |  2 +-
 16 files changed, 251 insertions(+), 126 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintExtractor.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintExtractor.java
index 05f1406..1036743 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintExtractor.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintExtractor.java
@@ -29,50 +29,20 @@ import java.util.Properties;
  */
 public final class SQLHintExtractor {
     
-    private static final String SQL_COMMENT_SUFFIX = "*/";
-    
-    private static final String SQL_HINT_TOKEN = "shardingsphere hint:";
-    
-    private static final String SQL_HINT_SPLIT = "=";
-    
     private final SQLHintProperties sqlHintProperties;
     
     public SQLHintExtractor(final SQLStatement sqlStatement) {
         sqlHintProperties = sqlStatement instanceof AbstractSQLStatement ? 
extract((AbstractSQLStatement) sqlStatement) : new SQLHintProperties(new 
Properties());
     }
     
-    /**
-     * Extract from statement.
-     *
-     * @param statement statement
-     * @return sql hint properties
-     */
-    public SQLHintProperties extract(final AbstractSQLStatement statement) {
+    private SQLHintProperties extract(final AbstractSQLStatement statement) {
         Properties properties = new Properties();
         for (CommentSegment each : statement.getCommentSegments()) {
-            appendHintProperties(each.getText(), properties);
+            properties.putAll(SQLHintUtils.getSQLHintProps(each.getText()));
         }
         return new SQLHintProperties(properties);
     }
     
-    private void appendHintProperties(final String comment, final Properties 
properties) {
-        int startIndex = comment.toLowerCase().indexOf(SQL_HINT_TOKEN);
-        if (startIndex < 0) {
-            return;
-        }
-        startIndex = startIndex + SQL_HINT_TOKEN.length();
-        int endIndex = comment.endsWith(SQL_COMMENT_SUFFIX) ? 
comment.indexOf(SQL_COMMENT_SUFFIX) : comment.length();
-        String[] hintValue = comment.substring(startIndex, 
endIndex).trim().split(SQL_HINT_SPLIT);
-        if (2 == hintValue.length && hintValue[0].trim().length() > 0 && 
hintValue[1].trim().length() > 0) {
-            if 
(SQLHintPropertiesKey.DATASOURCE_NAME_KEY.getKey().equalsIgnoreCase(hintValue[0].trim()))
 {
-                
properties.setProperty(SQLHintPropertiesKey.DATASOURCE_NAME_KEY.getKey(), 
hintValue[1].trim());
-            }
-            if 
(SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY.getKey().equalsIgnoreCase(hintValue[0].trim()))
 {
-                
properties.setProperty(SQLHintPropertiesKey.WRITE_ROUTE_ONLY_KEY.getKey(), 
hintValue[1].trim());
-            }
-        }
-    }
-    
     /**
      * Find hint data source name.
      *
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
new file mode 100644
index 0000000..a0bb40e
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/hint/SQLHintUtils.java
@@ -0,0 +1,57 @@
+/*
+ * 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.infra.hint;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Properties;
+
+/**
+ * SQL hint utils.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class SQLHintUtils {
+    
+    private static final String SQL_COMMENT_SUFFIX = "*/";
+    
+    private static final String SQL_HINT_TOKEN = "shardingsphere hint:";
+    
+    private static final String SQL_HINT_SPLIT = "=";
+    
+    /**
+     * Get SQL hint props.
+     *
+     * @param comment SQL comment
+     * @return SQL hint props
+     */
+    public static Properties getSQLHintProps(final String comment) {
+        Properties result = new Properties();
+        int startIndex = comment.toLowerCase().indexOf(SQL_HINT_TOKEN);
+        if (startIndex < 0) {
+            return result;
+        }
+        startIndex = startIndex + SQL_HINT_TOKEN.length();
+        int endIndex = comment.endsWith(SQL_COMMENT_SUFFIX) ? 
comment.indexOf(SQL_COMMENT_SUFFIX) : comment.length();
+        String[] hintValue = comment.substring(startIndex, 
endIndex).trim().split(SQL_HINT_SPLIT);
+        if (2 == hintValue.length && hintValue[0].trim().length() > 0 && 
hintValue[1].trim().length() > 0) {
+            result.put(hintValue[0].trim(), hintValue[1].trim());
+        }
+        return result;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
index a0bf4dd..765a0af 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.traffic.api.config;
 import lombok.Getter;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
-import 
org.apache.shardingsphere.traffic.api.config.strategy.TrafficStrategyConfiguration;
 
 import java.util.Collection;
 import java.util.LinkedHashMap;
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficStrategyConfiguration.java
similarity index 95%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficStrategyConfiguration.java
index a12d5c5..6f1ac75 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficStrategyConfiguration.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.api.config;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/TrafficValue.java
similarity index 64%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/TrafficValue.java
index a12d5c5..2a6fdc3 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/TrafficValue.java
@@ -15,25 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+package org.apache.shardingsphere.traffic.api.traffic;
 
 /**
- * Traffic strategy configuration.
+ * Traffic value.
  */
-@RequiredArgsConstructor
-@Getter
-public final class TrafficStrategyConfiguration {
-    
-    private final String name;
-    
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
-    
-    private final String loadBalancerName;
+public interface TrafficValue {
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficAlgorithm.java
similarity index 61%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficAlgorithm.java
index a12d5c5..7c412ba 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficAlgorithm.java
@@ -15,25 +15,22 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.api.traffic.hint;
 
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
 
 /**
- * Traffic strategy configuration.
+ * Hint traffic algorithm.
+ *
+ * @param <T> class type of hint value
  */
-@RequiredArgsConstructor
-@Getter
-public final class TrafficStrategyConfiguration {
-    
-    private final String name;
-    
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
+public interface HintTrafficAlgorithm<T extends Comparable<?>> extends 
TrafficAlgorithm {
     
-    private final String loadBalancerName;
+    /**
+     * Judge hint traffic value is match or not.
+     * 
+     * @param hintTrafficValue hint traffic value
+     * @return hint traffic value is match or not
+     */
+    boolean match(HintTrafficValue<T> hintTrafficValue);
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficValue.java
similarity index 72%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficValue.java
index a12d5c5..2d511d1 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/hint/HintTrafficValue.java
@@ -15,25 +15,20 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.api.traffic.hint;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+import org.apache.shardingsphere.traffic.api.traffic.TrafficValue;
 
 /**
- * Traffic strategy configuration.
+ * Hint traffic value.
+ *
+ * @param <T> class type of hint value
  */
 @RequiredArgsConstructor
 @Getter
-public final class TrafficStrategyConfiguration {
-    
-    private final String name;
-    
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
+public final class HintTrafficValue<T extends Comparable<?>> implements 
TrafficValue {
     
-    private final String loadBalancerName;
+    private final T value;
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficAlgorithm.java
similarity index 63%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficAlgorithm.java
index a12d5c5..c302e94 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficAlgorithm.java
@@ -15,25 +15,20 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.api.traffic.segment;
 
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
 
 /**
- * Traffic strategy configuration.
+ * Segment traffic algorithm.
  */
-@RequiredArgsConstructor
-@Getter
-public final class TrafficStrategyConfiguration {
-    
-    private final String name;
-    
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
+public interface SegmentTrafficAlgorithm extends TrafficAlgorithm {
     
-    private final String loadBalancerName;
+    /**
+     * Judge segment traffic value is match or not.
+     * 
+     * @param segmentTrafficValue segment traffic value
+     * @return segment traffic value is match or not
+     */
+    boolean match(SegmentTrafficValue segmentTrafficValue);
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficValue.java
similarity index 72%
copy from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficValue.java
index a12d5c5..9df41d0 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/traffic/segment/SegmentTrafficValue.java
@@ -15,25 +15,19 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.api.traffic.segment;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.traffic.api.traffic.TrafficValue;
 
 /**
- * Traffic strategy configuration.
+ * Segment traffic value.
  */
 @RequiredArgsConstructor
 @Getter
-public final class TrafficStrategyConfiguration {
-    
-    private final String name;
-    
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
+public final class SegmentTrafficValue implements TrafficValue {
     
-    private final String loadBalancerName;
+    private final SQLStatement statement;
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
similarity index 52%
rename from 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
rename to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
index a12d5c5..4e19121 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
@@ -15,25 +15,26 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.traffic.api.config.strategy;
+package org.apache.shardingsphere.traffic.algorithm.loadbalance;
 
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
 
-import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
 
 /**
- * Traffic strategy configuration.
+ * Random traffic load balance algorithm.
  */
-@RequiredArgsConstructor
-@Getter
-public final class TrafficStrategyConfiguration {
+public final class RandomTrafficLoadBalanceAlgorithm implements 
TrafficLoadBalanceAlgorithm {
     
-    private final String name;
+    @Override
+    public DataSourceConfiguration getDataSourceConfig(final 
List<DataSourceConfiguration> dataSourceConfigs) {
+        return 
dataSourceConfigs.get(ThreadLocalRandom.current().nextInt(dataSourceConfigs.size()));
+    }
     
-    private final Collection<String> labels;
-    
-    private final String algorithmName;
-    
-    private final String loadBalancerName;
+    @Override
+    public String getType() {
+        return "RANDOM";
+    }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithm.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithm.java
new file mode 100644
index 0000000..e61356e
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/algorithm/traffic/hint/SQLHintTrafficAlgorithm.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.hint;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.hint.SQLHintUtils;
+import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficAlgorithm;
+import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue;
+
+import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Simple hint traffic algorithm.
+ */
+@Getter
+@Setter
+public final class SQLHintTrafficAlgorithm implements 
HintTrafficAlgorithm<String> {
+    
+    private Properties props = new Properties();
+    
+    @Override
+    public void init() {
+        Preconditions.checkState(!props.isEmpty(), "Simple hint traffic 
algorithm props cannot be empty.");
+    }
+    
+    @Override
+    public boolean match(final HintTrafficValue<String> hintTrafficValue) {
+        Properties sqlHintProps = 
SQLHintUtils.getSQLHintProps(hintTrafficValue.getValue());
+        for (Entry<Object, Object> each : props.entrySet()) {
+            if (!Objects.equals(each.getValue(), 
sqlHintProps.get(String.valueOf(each.getKey())))) {
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    @Override
+    public String getType() {
+        return "SQL_HINT";
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
index e7f2b6b6..b5e7f9e 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -19,10 +19,17 @@ package org.apache.shardingsphere.traffic.rule;
 
 import com.google.common.base.Preconditions;
 import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
 import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
 import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficAlgorithm;
+import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue;
+import 
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficAlgorithm;
+import 
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue;
 import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
 import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
 
@@ -69,12 +76,41 @@ public final class TrafficRule implements GlobalRule {
         for (TrafficStrategyRule each : trafficStrategyRules) {
             TrafficAlgorithm trafficAlgorithm = 
trafficAlgorithms.get(each.getAlgorithmName());
             Preconditions.checkState(null != trafficAlgorithm, "Traffic 
strategy rule configuration must match traffic algorithm.");
-            // TODO add trafficAlgorithm match logic
-            return Optional.of(each);
+            if (match(trafficAlgorithm, logicSQL.getSqlStatementContext())) {
+                return Optional.of(each);
+            }
         }
         return Optional.empty();
     }
     
+    @SuppressWarnings("unchecked")
+    private boolean match(final TrafficAlgorithm trafficAlgorithm, final 
SQLStatementContext<?> statementContext) {
+        if (trafficAlgorithm instanceof HintTrafficAlgorithm) {
+            HintTrafficAlgorithm<Comparable<?>> hintTrafficAlgorithm = 
(HintTrafficAlgorithm<Comparable<?>>) trafficAlgorithm;
+            for (HintTrafficValue<Comparable<?>> each : 
getHintTrafficValues(statementContext)) {
+                if (hintTrafficAlgorithm.match(each)) {
+                    return true;
+                }
+            }
+        }
+        if (trafficAlgorithm instanceof SegmentTrafficAlgorithm) {
+            SegmentTrafficAlgorithm segmentTrafficAlgorithm = 
(SegmentTrafficAlgorithm) trafficAlgorithm;
+            SegmentTrafficValue segmentTrafficValue = new 
SegmentTrafficValue(statementContext.getSqlStatement());
+            return segmentTrafficAlgorithm.match(segmentTrafficValue);
+        }
+        return false;
+    }
+    
+    private Collection<HintTrafficValue<Comparable<?>>> 
getHintTrafficValues(final SQLStatementContext<?> statementContext) {
+        Collection<HintTrafficValue<Comparable<?>>> result = new 
LinkedList<>();
+        if (statementContext.getSqlStatement() instanceof 
AbstractSQLStatement) {
+            for (CommentSegment each : ((AbstractSQLStatement) 
statementContext.getSqlStatement()).getCommentSegments()) {
+                result.add(new HintTrafficValue<>(each.getText()));
+            }
+        }
+        return result;
+    }
+    
     /**
      * Find load balancer.
      * 
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
index 182fc99..8dddea6 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.traffic.yaml.swapper;
 
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwapper;
-import 
org.apache.shardingsphere.traffic.api.config.strategy.TrafficStrategyConfiguration;
+import 
org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
 import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
 
 /**
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
new file mode 100644
index 0000000..e0228e0
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficAlgorithm
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
new file mode 100644
index 0000000..4e99f13
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.traffic.algorithm.loadbalance.RandomTrafficLoadBalanceAlgorithm
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
index b3cde23..b134983 100644
--- 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.traffic.yaml.swapper;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
-import 
org.apache.shardingsphere.traffic.api.config.strategy.TrafficStrategyConfiguration;
+import 
org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration;
 import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
 import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
 import org.junit.Test;

Reply via email to