diff --git a/examples/shardingsphere-jdbc-example/other-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/table/raw/jdbc/config/MasterSlaveShadowDatabasesConfiguration.java b/examples/shardingsphere-jdbc-example/other-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/table/raw/jdbc/config/MasterSlaveShadowDatabasesConfiguration.java index b8efa6049ac23..36f833b26ffa5 100644 --- a/examples/shardingsphere-jdbc-example/other-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/table/raw/jdbc/config/MasterSlaveShadowDatabasesConfiguration.java +++ b/examples/shardingsphere-jdbc-example/other-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/table/raw/jdbc/config/MasterSlaveShadowDatabasesConfiguration.java @@ -35,16 +35,19 @@ public final class MasterSlaveShadowDatabasesConfiguration implements ExampleCon @Override public DataSource getDataSource() throws SQLException { - ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration("shadow", new HashMap(){{ - put("ds_master", "shadow_ds_master"); - put("ds_slave", "shadow_ds_slave"); - }}); Map dataSourceMap = new HashMap<>(); dataSourceMap.put("ds_master", DataSourceUtil.createDataSource("demo_ds_master")); dataSourceMap.put("ds_slave", DataSourceUtil.createDataSource("demo_ds_slave")); dataSourceMap.put("shadow_ds_master", DataSourceUtil.createDataSource("demo_shadow_ds_master")); dataSourceMap.put("shadow_ds_slave", DataSourceUtil.createDataSource("demo_shadow_ds_slave")); - return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Arrays.asList(shadowRuleConfiguration, getMasterSlaveRuleConfiguration()), null); + return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Arrays.asList(new ShadowRuleConfiguration("shadow", createShadowMappings()), getMasterSlaveRuleConfiguration()), null); + } + + private Map createShadowMappings() { + Map result = new HashMap<>(); + result.put("ds_master", "shadow_ds_master"); + result.put("ds_slave", "shadow_ds_slave"); + return result; } private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() { diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/integer/MySQLBitBinlogProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/integer/MySQLBitBinlogProtocolValueTest.java index 376b66ef9cd70..cdb99b19932e8 100644 --- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/integer/MySQLBitBinlogProtocolValueTest.java +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/column/value/integer/MySQLBitBinlogProtocolValueTest.java @@ -41,7 +41,7 @@ public final class MySQLBitBinlogProtocolValueTest { private MySQLBitBinlogProtocolValue actual; @Before - public void setUp() throws Exception { + public void setUp() { actual = new MySQLBitBinlogProtocolValue(); } diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-route/src/main/java/org/apache/shardingsphere/masterslave/route/engine/MasterSlaveRouteDecorator.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-route/src/main/java/org/apache/shardingsphere/masterslave/route/engine/MasterSlaveRouteDecorator.java index b9c9d41214f88..4618272c4bd32 100644 --- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-route/src/main/java/org/apache/shardingsphere/masterslave/route/engine/MasterSlaveRouteDecorator.java +++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-route/src/main/java/org/apache/shardingsphere/masterslave/route/engine/MasterSlaveRouteDecorator.java @@ -17,16 +17,17 @@ package org.apache.shardingsphere.masterslave.route.engine; -import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule; -import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule; -import org.apache.shardingsphere.masterslave.route.engine.impl.MasterSlaveDataSourceRouter; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.DefaultSchema; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteResult; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.infra.route.decorator.RouteDecorator; +import org.apache.shardingsphere.masterslave.route.engine.impl.MasterSlaveDataSourceRouter; +import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule; +import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule; import java.util.Collection; import java.util.Collections; @@ -43,7 +44,7 @@ public RouteContext decorate(final RouteContext routeContext, final ShardingSphe if (routeContext.getRouteResult().getRouteUnits().isEmpty()) { String dataSourceName = new MasterSlaveDataSourceRouter(masterSlaveRule.getSingleDataSourceRule()).route(routeContext.getSqlStatementContext().getSqlStatement()); RouteResult routeResult = new RouteResult(); - routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.emptyList())); + routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(DefaultSchema.LOGIC_NAME, dataSourceName), Collections.emptyList())); return new RouteContext(routeContext.getSqlStatementContext(), routeContext.getParameters(), routeResult); } Collection toBeRemoved = new LinkedList<>(); diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRule.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRule.java index 6f78c0ccb7873..9098166e75ca7 100644 --- a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRule.java +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRule.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; /** @@ -50,15 +51,30 @@ public Collection getSingleReplicaDataSources() { } /** - * Find data replica data sources. + * Find replica data sources. * * @param dataSourceName data source name * @return replica data source names */ - public Optional> findDataSourceRule(final String dataSourceName) { + public Optional> findReplicaDataSources(final String dataSourceName) { return Optional.ofNullable(dataSourceRules.get(dataSourceName)); } + /** + * Find logic data source name. + * + * @param replicaDataSourceName replica data source name + * @return logic data source name + */ + public Optional findLogicDataSource(final String replicaDataSourceName) { + for (Entry> entry : dataSourceRules.entrySet()) { + if (entry.getValue().contains(replicaDataSourceName)) { + return Optional.of(entry.getKey()); + } + } + return Optional.empty(); + } + @Override public Map> getDataSourceMapper() { return dataSourceRules; diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRuleBuilder.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRuleBuilder.java index 78bb0d02ac8f9..06b90deb4c5fb 100644 --- a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/rule/ReplicaRuleBuilder.java @@ -34,7 +34,7 @@ public ReplicaRule build(final ReplicaRuleConfiguration ruleConfiguration, final @Override public int getOrder() { - return 5; + return 7; } @Override diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapper.java index 4bd608f9f5a8a..47702f8092452 100644 --- a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapper.java +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/main/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapper.java @@ -73,6 +73,6 @@ public String getRuleTagName() { @Override public int getOrder() { - return 10; + return 12; } } diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/pom.xml b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/pom.xml index a247d8cf9cfb8..af86a5a369f9c 100644 --- a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/pom.xml +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/pom.xml @@ -27,4 +27,17 @@ shardingsphere-replica-execute ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-replica-common + ${project.version} + + + org.apache.shardingsphere + shardingsphere-infra-executor + ${project.version} + + diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/executor/ReplicaSQLExecutorCallback.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/executor/ReplicaSQLExecutorCallback.java new file mode 100644 index 0000000000000..0b465e053eb4a --- /dev/null +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/executor/ReplicaSQLExecutorCallback.java @@ -0,0 +1,40 @@ +/* + * 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.replica.execute.executor; + +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.executor.SQLExecutorCallback; +import org.apache.shardingsphere.infra.spi.order.OrderedSPI; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +/** + * SQL executor callback for replica. + * + * @param class type of return value + */ +public abstract class ReplicaSQLExecutorCallback implements SQLExecutorCallback, OrderedSPI { + + @Override + public final int getOrder() { + return 5; + } + + @Override + public final Class getTypeClass() { + return ReplicaRule.class; + } +} diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/group/ReplicaExecuteGroupDecorator.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/group/ReplicaExecuteGroupDecorator.java new file mode 100644 index 0000000000000..6f76d003f5d26 --- /dev/null +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/java/org/apache/shardingsphere/replica/execute/group/ReplicaExecuteGroupDecorator.java @@ -0,0 +1,62 @@ +/* + * 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.replica.execute.group; + +import org.apache.shardingsphere.infra.executor.kernel.InputGroup; +import org.apache.shardingsphere.infra.executor.sql.StorageResourceExecuteUnit; +import org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Execute group decorator for replica. + * + * @param type of input value + */ +public final class ReplicaExecuteGroupDecorator implements ExecuteGroupDecorator { + + @Override + public Collection> decorate(final ReplicaRule rule, final Collection> inputGroups) { + Map> result = new LinkedHashMap<>(inputGroups.size(), 1); + for (InputGroup each : inputGroups) { + T sample = each.getInputs().get(0); + String dataSourceName = sample.getExecutionUnit().getDataSourceName(); + Optional logicDataSource = rule.findLogicDataSource(dataSourceName); + if (logicDataSource.isPresent() && result.containsKey(dataSourceName)) { + result.get(dataSourceName).getInputs().addAll(each.getInputs()); + } else { + result.put(dataSourceName, each); + } + } + return result.values(); + } + + @Override + public int getOrder() { + return 5; + } + + @Override + public Class getTypeClass() { + return ReplicaRule.class; + } +} diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator new file mode 100644 index 0000000000000..609aae08d32ff --- /dev/null +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-execute/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.group.ExecuteGroupDecorator @@ -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.replica.execute.group.ReplicaExecuteGroupDecorator diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/pom.xml b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/pom.xml index df5fca7dd4426..d2de19da81b72 100644 --- a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/pom.xml +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/pom.xml @@ -27,4 +27,17 @@ shardingsphere-replica-route ${project.artifactId} + + + + org.apache.shardingsphere + shardingsphere-infra-route + ${project.version} + + + org.apache.shardingsphere + shardingsphere-replica-common + ${project.version} + + diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/java/org/apache/shardingsphere/replica/route/engine/ReplicaRouteDecorator.java b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/java/org/apache/shardingsphere/replica/route/engine/ReplicaRouteDecorator.java new file mode 100644 index 0000000000000..8911576007404 --- /dev/null +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/java/org/apache/shardingsphere/replica/route/engine/ReplicaRouteDecorator.java @@ -0,0 +1,76 @@ +/* + * 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.replica.route.engine; + +import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.DefaultSchema; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.context.RouteMapper; +import org.apache.shardingsphere.infra.route.context.RouteResult; +import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.route.decorator.RouteDecorator; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.Optional; + +/** + * Route decorator for replica. + */ +public final class ReplicaRouteDecorator implements RouteDecorator { + + @Override + public RouteContext decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final ReplicaRule replicaRule, final ConfigurationProperties properties) { + if (routeContext.getRouteResult().getRouteUnits().isEmpty()) { + RouteResult routeResult = new RouteResult(); + for (String each : replicaRule.getSingleReplicaDataSources()) { + routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(DefaultSchema.LOGIC_NAME, each), Collections.emptyList())); + } + return new RouteContext(routeContext.getSqlStatementContext(), Collections.emptyList(), routeResult); + } + Collection toBeRemoved = new LinkedList<>(); + Collection toBeAdded = new LinkedList<>(); + for (RouteUnit each : routeContext.getRouteResult().getRouteUnits()) { + String dataSourceName = each.getDataSourceMapper().getLogicName(); + Optional> replicaDataSources = replicaRule.findReplicaDataSources(dataSourceName); + if (!replicaDataSources.isPresent()) { + continue; + } + toBeRemoved.add(each); + for (String replicaDataSource : replicaDataSources.get()) { + toBeAdded.add(new RouteUnit(new RouteMapper(dataSourceName, replicaDataSource), each.getTableMappers())); + } + } + routeContext.getRouteResult().getRouteUnits().removeAll(toBeRemoved); + routeContext.getRouteResult().getRouteUnits().addAll(toBeAdded); + return routeContext; + } + + @Override + public int getOrder() { + return 12; + } + + @Override + public Class getTypeClass() { + return ReplicaRule.class; + } +} diff --git a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.route.decorator.RouteDecorator b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.route.decorator.RouteDecorator new file mode 100644 index 0000000000000..c66a60b3c20ab --- /dev/null +++ b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-route/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.route.decorator.RouteDecorator @@ -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.replica.route.engine.ReplicaRouteDecorator diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupDecorator.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupDecorator.java index 5a39439ec0c28..4568465b5f097 100644 --- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupDecorator.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupDecorator.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.infra.executor.sql.group; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.order.OrderedSPI; import org.apache.shardingsphere.infra.executor.kernel.InputGroup; @@ -25,15 +26,17 @@ /** * Execute group decorator. * - * @param type of input value + * @param type of input value + * @param type of ShardingSphere rule */ -public interface ExecuteGroupDecorator extends OrderedSPI { +public interface ExecuteGroupDecorator extends OrderedSPI { /** * Decorate input groups. * + * @param rule ShardingSphere rule * @param inputGroups input groups to be decorated * @return decorated input groups. */ - Collection> decorate(Collection> inputGroups); + Collection> decorate(R rule, Collection> inputGroups); } diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupEngine.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupEngine.java index b1564b0ac21a2..7c12152897cdb 100644 --- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupEngine.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/group/ExecuteGroupEngine.java @@ -70,20 +70,11 @@ public ExecuteGroupEngine(final int maxConnectionsSizePerQuery, final Collection * @throws SQLException SQL exception */ public Collection> generate(final Collection executionUnits, final E executionConnection, final O option) throws SQLException { - Collection> inputGroups = new LinkedList<>(); + Collection> result = new LinkedList<>(); for (Entry> entry : generateSQLUnitGroups(executionUnits).entrySet()) { - inputGroups.addAll(generateSQLExecuteGroups(entry.getKey(), entry.getValue(), executionConnection, option)); + result.addAll(generateSQLExecuteGroups(entry.getKey(), entry.getValue(), executionConnection, option)); } - return decorate(inputGroups); - } - - @SuppressWarnings("unchecked") - private Collection> decorate(final Collection> inputGroups) { - Collection> result = inputGroups; - for (Entry each : decorators.entrySet()) { - result = each.getValue().decorate(result); - } - return result; + return decorate(result); } private Map> generateSQLUnitGroups(final Collection executionUnits) { @@ -120,4 +111,13 @@ private InputGroup generateSQLExecuteGroup(final String dataSourceName, final } protected abstract U createStorageResourceExecuteUnit(ExecutionUnit executionUnit, E executionConnection, C connection, ConnectionMode connectionMode, O option) throws SQLException; + + @SuppressWarnings("unchecked") + private Collection> decorate(final Collection> inputGroups) { + Collection> result = inputGroups; + for (Entry each : decorators.entrySet()) { + result = each.getValue().decorate(each.getKey(), result); + } + return result; + } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml index e2cb3f63e8d8a..f47135d3e6793 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml @@ -102,6 +102,16 @@ shardingsphere-encrypt-merge ${project.version} + + org.apache.shardingsphere + shardingsphere-replica-route + ${project.version} + + + org.apache.shardingsphere + shardingsphere-replica-execute + ${project.version} + com.zaxxer diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java index 6d42aebefa8c3..1aac267d493de 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java @@ -93,7 +93,7 @@ private DefaultSQLExecutorCallback createDefaultSQLExecutorCallback @Override protected QueryResult executeSQL(final String sql, final Statement statement, final ConnectionMode connectionMode) throws SQLException { - return getQueryResult(statement, connectionMode); + return createQueryResult(statement, connectionMode); } }; } @@ -104,7 +104,7 @@ private SQLExecutorCallback getExecuteQueryExecutorCallback(final D return callbackMap.isEmpty() ? callback : callbackMap.values().iterator().next(); } - private QueryResult getQueryResult(final Statement statement, final ConnectionMode connectionMode) throws SQLException { + private QueryResult createQueryResult(final Statement statement, final ConnectionMode connectionMode) throws SQLException { PreparedStatement preparedStatement = (PreparedStatement) statement; ResultSet resultSet = preparedStatement.executeQuery(); return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet) : new MemoryQueryResult(resultSet); diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java index 96a190200c7f8..dfa21096f6749 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java @@ -160,7 +160,7 @@ public int executeUpdate(final Collection> inpu @SuppressWarnings("unchecked") private int executeUpdate(final Collection> inputGroups, final Updater updater, final SQLStatementContext sqlStatementContext) throws SQLException { - final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); + boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); DatabaseType databaseType = schemaContexts.getDefaultSchemaContext().getSchema().getDatabaseType(); SQLExecutorCallback sqlExecutorCallback = getExecuteUpdateExecutorCallback(new DefaultSQLExecutorCallback(databaseType, isExceptionThrown) { diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteBatchExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteBatchExecutorCallback.java index 8830a3ac54c3e..9c2ebe6b92721 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteBatchExecutorCallback.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteBatchExecutorCallback.java @@ -23,6 +23,8 @@ /** * Rule based SQL executor callback for execute batch. + * + * @param type of sharding sphere rule */ -public interface RuleExecuteBatchExecutorCallback extends SQLExecutorCallback, OrderedSPI { +public interface RuleExecuteBatchExecutorCallback extends SQLExecutorCallback, OrderedSPI { } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteExecutorCallback.java index ad15b7789fb52..acdfc369c860a 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteExecutorCallback.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteExecutorCallback.java @@ -23,6 +23,8 @@ /** * Rule based SQL executor callback for execute. + * + * @param type of sharding sphere rule */ -public interface RuleExecuteExecutorCallback extends SQLExecutorCallback, OrderedSPI { +public interface RuleExecuteExecutorCallback extends SQLExecutorCallback, OrderedSPI { } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteQueryExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteQueryExecutorCallback.java index 43b52b3b872d0..9b4de69119920 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteQueryExecutorCallback.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteQueryExecutorCallback.java @@ -24,6 +24,8 @@ /** * Rule based SQL executor callback for execute query. + * + * @param type of sharding sphere rule */ -public interface RuleExecuteQueryExecutorCallback extends SQLExecutorCallback, OrderedSPI { +public interface RuleExecuteQueryExecutorCallback extends SQLExecutorCallback, OrderedSPI { } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteUpdateExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteUpdateExecutorCallback.java index ab31d58e82e33..d441661382660 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteUpdateExecutorCallback.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/RuleExecuteUpdateExecutorCallback.java @@ -23,6 +23,8 @@ /** * Rule based SQL executor callback for execute update. + * + * @param type of sharding sphere rule */ -public interface RuleExecuteUpdateExecutorCallback extends SQLExecutorCallback, OrderedSPI { +public interface RuleExecuteUpdateExecutorCallback extends SQLExecutorCallback, OrderedSPI { } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteBatchExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteBatchExecutorCallback.java new file mode 100644 index 0000000000000..19927cd266d12 --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteBatchExecutorCallback.java @@ -0,0 +1,41 @@ +/* + * 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.driver.executor.callback.replica; + +import org.apache.shardingsphere.driver.executor.callback.RuleExecuteBatchExecutorCallback; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.StatementExecuteUnit; +import org.apache.shardingsphere.replica.execute.executor.ReplicaSQLExecutorCallback; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; + +/** + * SQL executor callback for execute batch for replica. + */ +public final class ReplicaExecuteBatchExecutorCallback extends ReplicaSQLExecutorCallback implements RuleExecuteBatchExecutorCallback { + + @Override + public Collection execute(final Collection inputs, final boolean isTrunkThread, final Map dataMap) throws SQLException { + Collection result = new LinkedList<>(); + // TODO + return result; + } +} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteExecutorCallback.java new file mode 100644 index 0000000000000..9bc981f856627 --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteExecutorCallback.java @@ -0,0 +1,41 @@ +/* + * 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.driver.executor.callback.replica; + +import org.apache.shardingsphere.driver.executor.callback.RuleExecuteExecutorCallback; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.StatementExecuteUnit; +import org.apache.shardingsphere.replica.execute.executor.ReplicaSQLExecutorCallback; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; + +/** + * SQL executor callback for execute for replica. + */ +public final class ReplicaExecuteExecutorCallback extends ReplicaSQLExecutorCallback implements RuleExecuteExecutorCallback { + + @Override + public Collection execute(final Collection inputs, final boolean isTrunkThread, final Map dataMap) throws SQLException { + Collection result = new LinkedList<>(); + // TODO + return result; + } +} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteQueryExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteQueryExecutorCallback.java new file mode 100644 index 0000000000000..1f02079771084 --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteQueryExecutorCallback.java @@ -0,0 +1,65 @@ +/* + * 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.driver.executor.callback.replica; + +import org.apache.shardingsphere.driver.executor.callback.RuleExecuteQueryExecutorCallback; +import org.apache.shardingsphere.infra.executor.sql.ConnectionMode; +import org.apache.shardingsphere.infra.executor.sql.QueryResult; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.StatementExecuteUnit; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.queryresult.MemoryQueryResult; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.queryresult.StreamQueryResult; +import org.apache.shardingsphere.replica.execute.executor.ReplicaSQLExecutorCallback; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; + +/** + * SQL executor callback for execute query for replica. + */ +public final class ReplicaExecuteQueryExecutorCallback extends ReplicaSQLExecutorCallback implements RuleExecuteQueryExecutorCallback { + + @Override + public Collection execute(final Collection inputs, final boolean isTrunkThread, final Map dataMap) throws SQLException { + Collection result = new LinkedList<>(); + for (StatementExecuteUnit each : inputs) { + if (each.getStorageResource() instanceof PreparedStatement) { + result.add(createQueryResult(each.getStorageResource(), each.getConnectionMode())); + } else { + result.add(createQueryResult(each.getExecutionUnit().getSqlUnit().getSql(), each.getStorageResource(), each.getConnectionMode())); + } + } + return result; + } + + private QueryResult createQueryResult(final Statement statement, final ConnectionMode connectionMode) throws SQLException { + PreparedStatement preparedStatement = (PreparedStatement) statement; + ResultSet resultSet = preparedStatement.executeQuery(); + return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet) : new MemoryQueryResult(resultSet); + } + + private QueryResult createQueryResult(final String sql, final Statement statement, final ConnectionMode connectionMode) throws SQLException { + ResultSet resultSet = statement.executeQuery(sql); + return ConnectionMode.MEMORY_STRICTLY == connectionMode ? new StreamQueryResult(resultSet) : new MemoryQueryResult(resultSet); + } +} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteUpdateExecutorCallback.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteUpdateExecutorCallback.java new file mode 100644 index 0000000000000..dff32b9c53382 --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/callback/replica/ReplicaExecuteUpdateExecutorCallback.java @@ -0,0 +1,48 @@ +/* + * 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.driver.executor.callback.replica; + +import org.apache.shardingsphere.driver.executor.callback.RuleExecuteUpdateExecutorCallback; +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.StatementExecuteUnit; +import org.apache.shardingsphere.replica.execute.executor.ReplicaSQLExecutorCallback; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; + +/** + * SQL executor callback for execute update for replica. + */ +public final class ReplicaExecuteUpdateExecutorCallback extends ReplicaSQLExecutorCallback implements RuleExecuteUpdateExecutorCallback { + + @Override + public Collection execute(final Collection inputs, final boolean isTrunkThread, final Map dataMap) throws SQLException { + Collection result = new LinkedList<>(); + for (StatementExecuteUnit each : inputs) { + if (each.getStorageResource() instanceof PreparedStatement) { + result.add(((PreparedStatement) each.getStorageResource()).executeUpdate()); + } else { + result.add(each.getStorageResource().executeUpdate(each.getExecutionUnit().getSqlUnit().getSql())); + } + } + return result; + } +} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java index 9d59156e6ecf3..b2d38acb35bc2 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java @@ -112,8 +112,7 @@ public ResultSet executeQuery(final String sql) throws SQLException { ResultSet result; try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); List queryResults = statementExecutor.executeQuery(inputGroups); MergedResult mergedResult = mergeQuery(queryResults); @@ -129,8 +128,7 @@ public ResultSet executeQuery(final String sql) throws SQLException { public int executeUpdate(final String sql) throws SQLException { try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.executeUpdate(inputGroups, executionContext.getSqlStatementContext()); } finally { @@ -145,8 +143,7 @@ public int executeUpdate(final String sql, final int autoGeneratedKeys) throws S } try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.executeUpdate(inputGroups, executionContext.getSqlStatementContext(), autoGeneratedKeys); } finally { @@ -159,8 +156,7 @@ public int executeUpdate(final String sql, final int[] columnIndexes) throws SQL returnGeneratedKeys = true; try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.executeUpdate(inputGroups, executionContext.getSqlStatementContext(), columnIndexes); } finally { @@ -173,8 +169,7 @@ public int executeUpdate(final String sql, final String[] columnNames) throws SQ returnGeneratedKeys = true; try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.executeUpdate(inputGroups, executionContext.getSqlStatementContext(), columnNames); } finally { @@ -186,8 +181,7 @@ public int executeUpdate(final String sql, final String[] columnNames) throws SQ public boolean execute(final String sql) throws SQLException { try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.execute(inputGroups, executionContext.getSqlStatementContext()); } finally { @@ -202,8 +196,7 @@ public boolean execute(final String sql, final int autoGeneratedKeys) throws SQL } try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.execute(inputGroups, executionContext.getSqlStatementContext(), autoGeneratedKeys); } finally { @@ -216,8 +209,7 @@ public boolean execute(final String sql, final int[] columnIndexes) throws SQLEx returnGeneratedKeys = true; try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.execute(inputGroups, executionContext.getSqlStatementContext(), columnIndexes); } finally { @@ -230,8 +222,7 @@ public boolean execute(final String sql, final String[] columnNames) throws SQLE returnGeneratedKeys = true; try { executionContext = createExecutionContext(sql); - Collection> inputGroups = getInputGroups( - schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); + Collection> inputGroups = getInputGroups(schemaContexts.getProperties().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)); cacheStatements(inputGroups); return statementExecutor.execute(inputGroups, executionContext.getSqlStatementContext(), columnNames); } finally { diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteBatchExecutorCallback b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteBatchExecutorCallback new file mode 100644 index 0000000000000..5f95c9b889bce --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteBatchExecutorCallback @@ -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.driver.executor.callback.replica.ReplicaExecuteBatchExecutorCallback diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteExecutorCallback b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteExecutorCallback new file mode 100644 index 0000000000000..63aa35b4ac4da --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteExecutorCallback @@ -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.driver.executor.callback.replica.ReplicaExecuteExecutorCallback diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteQueryExecutorCallback b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteQueryExecutorCallback new file mode 100644 index 0000000000000..2e67120c6ec63 --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteQueryExecutorCallback @@ -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.driver.executor.callback.replica.ReplicaExecuteQueryExecutorCallback diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteUpdateExecutorCallback b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteUpdateExecutorCallback new file mode 100644 index 0000000000000..94d5b5ffd6d3d --- /dev/null +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.executor.callback.RuleExecuteUpdateExecutorCallback @@ -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.driver.executor.callback.replica.ReplicaExecuteUpdateExecutorCallback diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml index 05005a788f207..bcdbb6b2f1629 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml @@ -102,6 +102,16 @@ shardingsphere-encrypt-merge ${project.version} + + org.apache.shardingsphere + shardingsphere-replica-route + ${project.version} + + + org.apache.shardingsphere + shardingsphere-replica-execute + ${project.version} + org.apache.shardingsphere shardingsphere-transaction-xa-core diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/RuleProxySQLExecutorCallback.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/RuleProxySQLExecutorCallback.java index 65dd9682e0e6b..d00b35bede254 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/RuleProxySQLExecutorCallback.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/RuleProxySQLExecutorCallback.java @@ -24,6 +24,8 @@ /** * Rule based SQL executor callback for Proxy. + * + * @param type of ShardingSphere rule */ -public interface RuleProxySQLExecutorCallback extends SQLExecutorCallback, OrderedSPI { +public interface RuleProxySQLExecutorCallback extends SQLExecutorCallback, OrderedSPI { } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/replica/ReplicaProxySQLExecutorCallback.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/replica/ReplicaProxySQLExecutorCallback.java new file mode 100644 index 0000000000000..6f0c7ec3ca1c7 --- /dev/null +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/callback/replica/ReplicaProxySQLExecutorCallback.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.proxy.backend.communication.jdbc.execute.callback.replica; + +import org.apache.shardingsphere.infra.executor.sql.execute.jdbc.StatementExecuteUnit; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.callback.RuleProxySQLExecutorCallback; +import org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.response.ExecuteResponse; +import org.apache.shardingsphere.replica.rule.ReplicaRule; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.Map; + +/** + * Replica SQL executor callback for Proxy. + */ +public final class ReplicaProxySQLExecutorCallback implements RuleProxySQLExecutorCallback { + + @Override + public Collection execute(final Collection inputs, final boolean isTrunkThread, final Map dataMap) throws SQLException { + // TODO + return null; + } + + @Override + public int getOrder() { + return 0; + } + + @Override + public Class getTypeClass() { + return ReplicaRule.class; + } +} diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.callback.RuleProxySQLExecutorCallback b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.callback.RuleProxySQLExecutorCallback new file mode 100644 index 0000000000000..01e455a1ec4f2 --- /dev/null +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.callback.RuleProxySQLExecutorCallback @@ -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.proxy.backend.communication.jdbc.execute.callback.replica.ReplicaProxySQLExecutorCallback