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 2021/12/29 00:39:33 UTC
[shardingsphere] branch master updated: Add TrafficEngine and TrafficExecutor process logic (#14379)
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 5c891b1 Add TrafficEngine and TrafficExecutor process logic (#14379)
5c891b1 is described below
commit 5c891b178024ff7d43bc854b8109475a9af3330b
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Wed Dec 29 08:38:37 2021 +0800
Add TrafficEngine and TrafficExecutor process logic (#14379)
---
.../driver/executor/DriverExecutor.java | 5 ++
.../statement/ShardingSpherePreparedStatement.java | 23 ++++-
.../{ => api}/config/TrafficRuleConfiguration.java | 3 +-
.../strategy}/TrafficStrategyConfiguration.java | 2 +-
.../traffic/spi/TrafficLoadBalanceAlgorithm.java | 11 +++
.../shardingsphere-traffic-core/pom.xml | 10 +++
.../traffic/context/TrafficContext.java} | 28 ++++---
.../traffic/engine/TrafficEngine.java | 63 ++++++++++++++
.../traffic/executor/TrafficExecutor.java | 70 ++++++++++++++++
.../traffic/executor/TrafficExecutorFactory.java} | 32 +++----
.../traffic/executor/jdbc/JDBCTrafficExecutor.java | 98 ++++++++++++++++++++++
.../shardingsphere/traffic/rule/TrafficRule.java | 38 ++++++++-
.../traffic/rule/TrafficStrategyRule.java} | 6 +-
.../DefaultTrafficRuleConfigurationBuilder.java | 2 +-
.../traffic/rule/builder/TrafficRuleBuilder.java | 2 +-
.../yaml/config/YamlTrafficRuleConfiguration.java | 2 +-
.../TrafficRuleConfigurationYamlSwapper.java | 2 +-
.../TrafficStrategyConfigurationYamlSwapper.java | 2 +-
.../traffic/rule/TrafficRuleTest.java | 2 +-
...DefaultTrafficRuleConfigurationBuilderTest.java | 2 +-
.../rule/builder/TrafficRuleBuilderTest.java | 2 +-
.../TrafficRuleConfigurationYamlSwapperTest.java | 4 +-
22 files changed, 364 insertions(+), 45 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverExecutor.java
index d7a35ab..76f9988 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverExecutor.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverExecutor.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.raw.RawExecut
import org.apache.shardingsphere.infra.federation.executor.FederationExecutor;
import org.apache.shardingsphere.infra.federation.executor.FederationExecutorFactory;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.traffic.executor.TrafficExecutor;
+import org.apache.shardingsphere.traffic.executor.TrafficExecutorFactory;
import java.sql.SQLException;
@@ -39,12 +41,15 @@ public final class DriverExecutor implements AutoCloseable {
private final FederationExecutor federationExecutor;
+ private final TrafficExecutor trafficExecutor;
+
public DriverExecutor(final ShardingSphereConnection connection) {
MetaDataContexts metaDataContexts = connection.getContextManager().getMetaDataContexts();
JDBCExecutor jdbcExecutor = new JDBCExecutor(metaDataContexts.getExecutorEngine(), connection.isHoldTransaction());
regularExecutor = new DriverJDBCExecutor(connection.getSchema(), metaDataContexts, jdbcExecutor);
rawExecutor = new RawExecutor(metaDataContexts.getExecutorEngine(), connection.isHoldTransaction(), metaDataContexts.getProps());
federationExecutor = FederationExecutorFactory.newInstance(connection.getSchema(), metaDataContexts.getOptimizerContext(), metaDataContexts.getProps(), jdbcExecutor);
+ trafficExecutor = TrafficExecutorFactory.newInstance();
}
/**
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 6c3b28f..4528e7d 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -73,6 +73,9 @@ import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
+import org.apache.shardingsphere.traffic.context.TrafficContext;
+import org.apache.shardingsphere.traffic.engine.TrafficEngine;
+import org.apache.shardingsphere.traffic.rule.TrafficRule;
import java.sql.Connection;
import java.sql.ParameterMetaData;
@@ -180,6 +183,10 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
clearPrevious();
LogicSQL logicSQL = createLogicSQL();
+ TrafficContext trafficContext = createTrafficContext(logicSQL);
+ if (trafficContext.getDataSourceConfig().isPresent()) {
+ return executor.getTrafficExecutor().executeQuery(logicSQL, trafficContext.getDataSourceConfig().get());
+ }
// TODO move federation route logic to binder
executionContext = createExecutionContext(logicSQL);
if (executionContext.getRouteContext().isFederated()) {
@@ -193,6 +200,11 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
}
+ private TrafficContext createTrafficContext(final LogicSQL logicSQL) {
+ Optional<TrafficRule> trafficRule = metaDataContexts.getGlobalRuleMetaData().findSingleRule(TrafficRule.class);
+ return trafficRule.map(optional -> new TrafficEngine(optional).dispatch(logicSQL)).orElse(new TrafficContext());
+ }
+
private void resetParameters() throws SQLException {
parameterSets.clear();
parameterSets.add(getParameters());
@@ -240,7 +252,12 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
return statements.iterator().next().executeUpdate();
}
clearPrevious();
- executionContext = createExecutionContext(createLogicSQL());
+ LogicSQL logicSQL = createLogicSQL();
+ TrafficContext trafficContext = createTrafficContext(logicSQL);
+ if (trafficContext.getDataSourceConfig().isPresent()) {
+ return executor.getTrafficExecutor().executeUpdate(logicSQL, trafficContext.getDataSourceConfig().get());
+ }
+ executionContext = createExecutionContext(logicSQL);
if (metaDataContexts.getMetaData(connection.getSchema()).getRuleMetaData().getRules().stream().anyMatch(each -> each instanceof RawExecutionRule)) {
Collection<ExecuteResult> executeResults = executor.getRawExecutor().execute(createRawExecutionGroupContext(), executionContext.getLogicSQL(), new RawSQLExecutorCallback());
return accumulate(executeResults);
@@ -287,6 +304,10 @@ public final class ShardingSpherePreparedStatement extends AbstractPreparedState
}
clearPrevious();
LogicSQL logicSQL = createLogicSQL();
+ TrafficContext trafficContext = createTrafficContext(logicSQL);
+ if (trafficContext.getDataSourceConfig().isPresent()) {
+ return executor.getTrafficExecutor().execute(logicSQL, trafficContext.getDataSourceConfig().get());
+ }
executionContext = createExecutionContext(logicSQL);
if (metaDataContexts.getMetaData(connection.getSchema()).getRuleMetaData().getRules().stream().anyMatch(each -> each instanceof RawExecutionRule)) {
// TODO process getStatement
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
similarity index 91%
rename from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
index 5c96233..a0bf4dd 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/TrafficRuleConfiguration.java
@@ -15,11 +15,12 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.config;
+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/config/TrafficStrategyConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
similarity index 95%
copy from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
copy to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
index 0b1699d..a12d5c5 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/api/config/strategy/TrafficStrategyConfiguration.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.config;
+package org.apache.shardingsphere.traffic.api.config.strategy;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
index f7e8393..834c761 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
@@ -18,10 +18,21 @@
package org.apache.shardingsphere.traffic.spi;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.spi.required.RequiredSPI;
+import java.util.List;
+
/**
* Traffic load balance algorithm.
*/
public interface TrafficLoadBalanceAlgorithm extends ShardingSphereAlgorithm, RequiredSPI {
+
+ /**
+ * Get dataSource config.
+ *
+ * @param dataSourceConfigs dataSource config collection
+ * @return dataSource config
+ */
+ DataSourceConfiguration getDataSourceConfig(List<DataSourceConfiguration> dataSourceConfigs);
}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
index 83eda10..5155f45 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
@@ -35,8 +35,18 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-infra-binder</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-traffic-api</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/context/TrafficContext.java
similarity index 62%
copy from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
copy to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/context/TrafficContext.java
index 0b1699d..dbace8b 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/context/TrafficContext.java
@@ -15,25 +15,29 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.config;
+package org.apache.shardingsphere.traffic.context;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import java.util.Collection;
+import java.util.Optional;
/**
- * Traffic strategy configuration.
+ * Traffic context.
*/
-@RequiredArgsConstructor
@Getter
-public final class TrafficStrategyConfiguration {
+@Setter
+public final class TrafficContext {
- private final String name;
+ private DataSourceConfiguration dataSourceConfig;
- private final Collection<String> labels;
-
- private final String algorithmName;
-
- private final String loadBalancerName;
+ /**
+ * Get data source config.
+ *
+ * @return data source config
+ */
+ public Optional<DataSourceConfiguration> getDataSourceConfig() {
+ return Optional.ofNullable(dataSourceConfig);
+ }
}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
new file mode 100644
index 0000000..064de47
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
@@ -0,0 +1,63 @@
+/*
+ * 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.engine;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.traffic.context.TrafficContext;
+import org.apache.shardingsphere.traffic.rule.TrafficRule;
+import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
+import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Traffic engine.
+ */
+@RequiredArgsConstructor
+public final class TrafficEngine {
+
+ private final TrafficRule trafficRule;
+
+ /**
+ * Dispatch.
+ *
+ * @param logicSQL logic SQL
+ * @return traffic context
+ */
+ public TrafficContext dispatch(final LogicSQL logicSQL) {
+ Optional<TrafficStrategyRule> strategyRule = trafficRule.findMatchedStrategyRule(logicSQL);
+ TrafficContext result = new TrafficContext();
+ if (!strategyRule.isPresent()) {
+ return result;
+ }
+ List<DataSourceConfiguration> dataSourceConfigs = getDataSourceConfigsByLabels(strategyRule.get().getLabels());
+ TrafficLoadBalanceAlgorithm loadBalancer = trafficRule.findLoadBalancer(strategyRule.get().getLoadBalancerName());
+ result.setDataSourceConfig(loadBalancer.getDataSourceConfig(dataSourceConfigs));
+ return result;
+ }
+
+ private List<DataSourceConfiguration> getDataSourceConfigsByLabels(final Collection<String> labels) {
+ // TODO implements this logic when provide mode api to get dataSource configs by labels
+ return Collections.emptyList();
+ }
+}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutor.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutor.java
new file mode 100644
index 0000000..70bdee7
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutor.java
@@ -0,0 +1,70 @@
+/*
+ * 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.executor;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Traffic executor.
+ */
+public interface TrafficExecutor extends AutoCloseable {
+
+ /**
+ * Execute query.
+ *
+ * @param logicSQL logic SQL
+ * @param dataSourceConfig dataSource config
+ * @return result set
+ * @throws SQLException SQL exception
+ */
+ ResultSet executeQuery(LogicSQL logicSQL, DataSourceConfiguration dataSourceConfig) throws SQLException;
+
+ /**
+ * Execute update.
+ *
+ * @param logicSQL logic SQL
+ * @param dataSourceConfig dataSource config
+ * @return update count
+ * @throws SQLException SQL exception
+ */
+ int executeUpdate(LogicSQL logicSQL, DataSourceConfiguration dataSourceConfig) throws SQLException;
+
+ /**
+ * Execute.
+ *
+ * @param logicSQL logic SQL
+ * @param dataSourceConfig dataSource config
+ * @return whether execute success or not
+ */
+ boolean execute(LogicSQL logicSQL, DataSourceConfiguration dataSourceConfig);
+
+ /**
+ * Get result set.
+ *
+ * @return result set
+ * @throws SQLException SQL exception
+ */
+ ResultSet getResultSet() throws SQLException;
+
+ @Override
+ void close() throws SQLException;
+}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutorFactory.java
similarity index 55%
copy from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
copy to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutorFactory.java
index 0b1699d..f8a3016 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/TrafficExecutorFactory.java
@@ -15,25 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.config;
+package org.apache.shardingsphere.traffic.executor;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-import java.util.Collection;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
+import org.apache.shardingsphere.traffic.executor.jdbc.JDBCTrafficExecutor;
/**
- * Traffic strategy configuration.
+ * Traffic executor factory.
*/
-@RequiredArgsConstructor
-@Getter
-public final class TrafficStrategyConfiguration {
-
- private final String name;
-
- private final Collection<String> labels;
-
- private final String algorithmName;
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TrafficExecutorFactory {
- private final String loadBalancerName;
+ /**
+ * Create new instance of traffic executor factory.
+ *
+ * @return new instance of traffic executor
+ */
+ public static TrafficExecutor newInstance() {
+ return new JDBCTrafficExecutor(new DataSourceConfigurationValidator());
+ }
}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/jdbc/JDBCTrafficExecutor.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/jdbc/JDBCTrafficExecutor.java
new file mode 100644
index 0000000..9a3d9b8
--- /dev/null
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/executor/jdbc/JDBCTrafficExecutor.java
@@ -0,0 +1,98 @@
+/*
+ * 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.executor.jdbc;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
+import org.apache.shardingsphere.traffic.executor.TrafficExecutor;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * JDBC traffic executor.
+ */
+@RequiredArgsConstructor
+public final class JDBCTrafficExecutor implements TrafficExecutor {
+
+ private static final String TRAFFIC_DATASOURCE_NAME = "TRAFFIC";
+
+ private final DataSourceConfigurationValidator dataSourceConfigValidator;
+
+ private Statement statement;
+
+ @Override
+ public ResultSet executeQuery(final LogicSQL logicSQL, final DataSourceConfiguration dataSourceConfig) throws SQLException {
+ dataSourceConfigValidator.validate(createDataSourceConfigs(dataSourceConfig));
+ DataSource dataSource = DataSourceConverter.getDataSource(dataSourceConfig);
+ Connection connection = dataSource.getConnection();
+ PreparedStatement statement = connection.prepareStatement(logicSQL.getSql());
+ this.statement = statement;
+ setParameters(statement, logicSQL.getParameters());
+ return statement.executeQuery();
+ }
+
+ @Override
+ public int executeUpdate(final LogicSQL logicSQL, final DataSourceConfiguration dataSourceConfig) throws SQLException {
+ // TODO implement executeUpdate method
+ return 0;
+ }
+
+ @Override
+ public boolean execute(final LogicSQL logicSQL, final DataSourceConfiguration dataSourceConfig) {
+ // TODO implement execute method
+ return false;
+ }
+
+ private void setParameters(final PreparedStatement statement, final List<Object> parameters) throws SQLException {
+ int index = 1;
+ for (Object each : parameters) {
+ statement.setObject(index++, each);
+ }
+ }
+
+ private Map<String, DataSourceConfiguration> createDataSourceConfigs(final DataSourceConfiguration dataSourceConfig) {
+ Map<String, DataSourceConfiguration> result = new HashMap<>(1, 1);
+ result.put(TRAFFIC_DATASOURCE_NAME, dataSourceConfig);
+ return result;
+ }
+
+ @Override
+ public ResultSet getResultSet() throws SQLException {
+ return statement.getResultSet();
+ }
+
+ @Override
+ public void close() throws SQLException {
+ if (null != statement) {
+ Connection connection = statement.getConnection();
+ statement.close();
+ connection.close();
+ }
+ }
+}
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 a77cc2f..e7f2b6b6 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
@@ -17,15 +17,20 @@
package org.apache.shardingsphere.traffic.rule;
+import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.infra.binder.LogicSQL;
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.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
+import java.util.Collection;
import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.Map;
+import java.util.Optional;
/**
* Traffic rule.
@@ -37,11 +42,14 @@ public final class TrafficRule implements GlobalRule {
ShardingSphereServiceLoader.register(TrafficLoadBalanceAlgorithm.class);
}
+ private final Collection<TrafficStrategyRule> trafficStrategyRules = new LinkedList<>();
+
private final Map<String, TrafficAlgorithm> trafficAlgorithms = new LinkedHashMap<>();
private final Map<String, TrafficLoadBalanceAlgorithm> loadBalancers = new LinkedHashMap<>();
public TrafficRule(final TrafficRuleConfiguration config) {
+ config.getTrafficStrategies().forEach(each -> trafficStrategyRules.add(new TrafficStrategyRule(each.getName(), each.getLabels(), each.getAlgorithmName(), each.getLoadBalancerName())));
config.getTrafficAlgorithms().forEach((key, value) -> trafficAlgorithms.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, TrafficAlgorithm.class)));
config.getLoadBalancers().forEach((key, value) -> loadBalancers.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, TrafficLoadBalanceAlgorithm.class)));
}
@@ -50,4 +58,32 @@ public final class TrafficRule implements GlobalRule {
public String getType() {
return TrafficRule.class.getSimpleName();
}
+
+ /**
+ * Find matched strategy rule.
+ *
+ * @param logicSQL logic SQL
+ * @return matched strategy rule
+ */
+ public Optional<TrafficStrategyRule> findMatchedStrategyRule(final LogicSQL logicSQL) {
+ 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);
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Find load balancer.
+ *
+ * @param loadBalancerName load balancer name
+ * @return load balancer
+ */
+ public TrafficLoadBalanceAlgorithm findLoadBalancer(final String loadBalancerName) {
+ TrafficLoadBalanceAlgorithm loadBalanceAlgorithm = loadBalancers.get(loadBalancerName);
+ Preconditions.checkState(null != loadBalanceAlgorithm, "Traffic load balance algorithm can not be null.");
+ return loadBalanceAlgorithm;
+ }
}
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
similarity index 89%
rename from shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
rename to shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
index 0b1699d..7a2c17e 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.traffic.config;
+package org.apache.shardingsphere.traffic.rule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -23,11 +23,11 @@ import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
- * Traffic strategy configuration.
+ * Traffic strategy rule.
*/
@RequiredArgsConstructor
@Getter
-public final class TrafficStrategyConfiguration {
+public final class TrafficStrategyRule {
private final String name;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
index 88e4390..32fabb8 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.traffic.rule.builder;
import org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.constant.TrafficOrder;
/**
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
index 8959908..145a58c 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.traffic.rule.builder;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.constant.TrafficOrder;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
index 45d50e5..5d264f1 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
@@ -21,7 +21,7 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import java.util.LinkedHashMap;
import java.util.Map;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
index 979a474..62586b2c 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.traffic.yaml.swapper;
import org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.ShardingSphereAlgorithmConfigurationYamlSwapper;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.constant.TrafficOrder;
import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
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 669cbf5..182fc99 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.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.api.config.strategy.TrafficStrategyConfiguration;
import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
/**
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
index 3e71569..0dd2a14 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.traffic.rule;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
index ee5117a..205fb62 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.traffic.rule.builder;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
diff --git a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
index fab8768..466d5ca 100644
--- a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.traffic.rule.builder;
-import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
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 8e8b234..b3cde23 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
@@ -19,8 +19,8 @@ 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.config.TrafficRuleConfiguration;
-import org.apache.shardingsphere.traffic.config.TrafficStrategyConfiguration;
+import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.api.config.strategy.TrafficStrategyConfiguration;
import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
import org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
import org.junit.Test;