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 4f55a0a sharding rule performance optimization (#11777)
4f55a0a is described below
commit 4f55a0a00ecd53843b384a541f8d0e53c5601d1d
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Thu Aug 12 13:09:05 2021 +0800
sharding rule performance optimization (#11777)
* sharding rule performance optimization
* minor refactor
* fix test case
* fix test case
* fix checkstyle
---
.../type/complex/ShardingComplexRoutingEngine.java | 4 +--
.../sharding/rule/BindingTableRule.java | 23 +++++--------
.../shardingsphere/sharding/rule/ShardingRule.java | 38 ++++++++++++++--------
.../sharding/rule/BindingTableRuleTest.java | 29 ++++++++++++-----
.../sharding/rule/ShardingRuleTest.java | 3 +-
.../GovernanceShardingNamespaceTest.java | 4 +--
6 files changed, 58 insertions(+), 43 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
index 4e10bc2..55c34e5 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingComplexRoutingEngine.java
@@ -31,7 +31,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.TreeSet;
-import java.util.stream.Collectors;
/**
* Sharding complex routing engine.
@@ -57,8 +56,7 @@ public final class ShardingComplexRoutingEngine implements
ShardingRouteEngine {
new
ShardingStandardRoutingEngine(tableRule.get().getLogicTable(),
shardingConditions, props).route(newRouteContext, shardingRule);
routeContexts.add(newRouteContext);
}
-
shardingRule.findBindingTableRule(each).ifPresent(bindingTableRule ->
bindingTableNames.addAll(
-
bindingTableRule.getTableRules().stream().map(TableRule::getLogicTable).collect(Collectors.toList())));
+
shardingRule.findBindingTableRule(each).ifPresent(bindingTableRule ->
bindingTableNames.addAll(bindingTableRule.getTableRules().keySet()));
}
}
if (routeContexts.isEmpty()) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java
index 67abab2..e1a6bb6 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/BindingTableRule.java
@@ -23,9 +23,8 @@ import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurat
import java.util.Collection;
import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map;
-import java.util.stream.Collectors;
+import java.util.Optional;
/**
* Binding table rule.
@@ -36,7 +35,7 @@ import java.util.stream.Collectors;
@Getter
public final class BindingTableRule {
- private final List<TableRule> tableRules;
+ private final Map<String, TableRule> tableRules = new LinkedHashMap<>();
/**
* Judge contains this logic table in this rule.
@@ -45,12 +44,7 @@ public final class BindingTableRule {
* @return contains this logic table or not
*/
public boolean hasLogicTable(final String logicTable) {
- for (TableRule each : tableRules) {
- if (each.getLogicTable().equals(logicTable.toLowerCase())) {
- return true;
- }
- }
- return false;
+ return tableRules.containsKey(logicTable.toLowerCase());
}
/**
@@ -63,7 +57,7 @@ public final class BindingTableRule {
*/
public String getBindingActualTable(final String dataSource, final String
logicTable, final String otherActualTable) {
int index = -1;
- for (TableRule each : tableRules) {
+ for (TableRule each : tableRules.values()) {
index = each.findActualTableIndex(dataSource, otherActualTable);
if (-1 != index) {
break;
@@ -72,10 +66,9 @@ public final class BindingTableRule {
if (-1 == index) {
throw new ShardingSphereConfigurationException("Actual table
[%s].[%s] is not in table config", dataSource, otherActualTable);
}
- for (TableRule each : tableRules) {
- if (each.getLogicTable().equals(logicTable.toLowerCase())) {
- return
each.getActualDataNodes().get(index).getTableName().toLowerCase();
- }
+ Optional<TableRule> tableRule =
Optional.ofNullable(tableRules.get(logicTable.toLowerCase()));
+ if (tableRule.isPresent()) {
+ return
tableRule.get().getActualDataNodes().get(index).getTableName().toLowerCase();
}
throw new ShardingSphereConfigurationException("Cannot find binding
actual table, data source: %s, logic table: %s, other actual table: %s",
dataSource, logicTable, otherActualTable);
}
@@ -86,7 +79,7 @@ public final class BindingTableRule {
* @return logical tables.
*/
public Collection<String> getAllLogicTables() {
- return tableRules.stream().map(input ->
input.getLogicTable().toLowerCase()).collect(Collectors.toList());
+ return tableRules.keySet();
}
Map<String, String> getLogicAndActualTables(final String dataSource, final
String logicTable, final String actualTable, final Collection<String>
availableLogicBindingTables) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index fdb2677..a9c2da9 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -96,7 +96,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
config.getKeyGenerators().forEach((key, value) ->
keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value,
KeyGenerateAlgorithm.class)));
tableRules.putAll(createTableRules(config.getTables(),
config.getDefaultKeyGenerateStrategy()));
tableRules.putAll(createAutoTableRules(config.getAutoTables(),
config.getDefaultKeyGenerateStrategy()));
- broadcastTables = config.getBroadcastTables();
+ broadcastTables = createBroadcastTables(config.getBroadcastTables());
bindingTableRules =
createBindingTableRules(config.getBindingTableGroups());
defaultDatabaseShardingStrategyConfig = null ==
config.getDefaultDatabaseShardingStrategy() ? new
NoneShardingStrategyConfiguration() :
config.getDefaultDatabaseShardingStrategy();
defaultTableShardingStrategyConfig = null ==
config.getDefaultTableShardingStrategy() ? new
NoneShardingStrategyConfiguration() : config.getDefaultTableShardingStrategy();
@@ -112,7 +112,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
keyGenerators.putAll(config.getKeyGenerators());
tableRules.putAll(createTableRules(config.getTables(),
config.getDefaultKeyGenerateStrategy()));
tableRules.putAll(createAutoTableRules(config.getAutoTables(),
config.getDefaultKeyGenerateStrategy()));
- broadcastTables = config.getBroadcastTables();
+ broadcastTables = createBroadcastTables(config.getBroadcastTables());
bindingTableRules =
createBindingTableRules(config.getBindingTableGroups());
defaultDatabaseShardingStrategyConfig = null ==
config.getDefaultDatabaseShardingStrategy() ? new
NoneShardingStrategyConfiguration() :
config.getDefaultDatabaseShardingStrategy();
defaultTableShardingStrategyConfig = null ==
config.getDefaultTableShardingStrategy() ? new
NoneShardingStrategyConfiguration() : config.getDefaultTableShardingStrategy();
@@ -166,12 +166,22 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
return
Optional.ofNullable(defaultKeyGenerateStrategyConfig).map(KeyGenerateStrategyConfiguration::getColumn).orElse(null);
}
+ private Collection<String> createBroadcastTables(final Collection<String>
broadcastTables) {
+ Collection<String> result = new
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ result.addAll(broadcastTables);
+ return result;
+ }
+
private Collection<BindingTableRule> createBindingTableRules(final
Collection<String> bindingTableGroups) {
return
bindingTableGroups.stream().map(this::createBindingTableRule).collect(Collectors.toList());
}
private BindingTableRule createBindingTableRule(final String
bindingTableGroup) {
- return new
BindingTableRule(Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream().map(this::getTableRule).collect(Collectors.toList()));
+ Map<String, TableRule> tableRules =
Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream()
+
.map(this::getTableRule).collect(Collectors.toMap(TableRule::getLogicTable,
Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+ BindingTableRule result = new BindingTableRule();
+ result.getTableRules().putAll(tableRules);
+ return result;
}
@Override
@@ -209,7 +219,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return table rule
*/
public Optional<TableRule> findTableRule(final String logicTableName) {
- return
Optional.ofNullable(tableRules.getOrDefault(logicTableName.toLowerCase(),
null));
+ return
Optional.ofNullable(tableRules.get(logicTableName.toLowerCase()));
}
/**
@@ -289,7 +299,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return logic tables is all belong to sharding tables or not
*/
public boolean isAllShardingTables(final Collection<String>
logicTableNames) {
- return logicTableNames.stream().allMatch(each ->
findTableRule(each).isPresent());
+ return !logicTableNames.isEmpty() &&
logicTableNames.stream().allMatch(this::isShardingTable);
}
/**
@@ -299,7 +309,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return logic table is belong to sharding tables or not
*/
public boolean isShardingTable(final String logicTableName) {
- return findTableRule(logicTableName).isPresent();
+ return tableRules.containsKey(logicTableName.toLowerCase());
}
/**
@@ -309,7 +319,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return logic table is belong to broadcast tables or not
*/
public boolean isBroadcastTable(final String logicTableName) {
- return broadcastTables.stream().anyMatch(each ->
each.equalsIgnoreCase(logicTableName));
+ return broadcastTables.contains(logicTableName);
}
/**
@@ -337,7 +347,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return whether a table rule exists for logic tables
*/
public boolean tableRuleExists(final Collection<String> logicTableNames) {
- return logicTableNames.stream().anyMatch(each ->
findTableRule(each).isPresent() || isBroadcastTable(each));
+ return logicTableNames.stream().anyMatch(each -> isShardingTable(each)
|| isBroadcastTable(each));
}
/**
@@ -347,7 +357,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return whether single table rule exists for logic tables
*/
public boolean singleTableRuleExists(final Collection<String>
logicTableNames) {
- Collection<String> shardingBroadcastLogicTableNames =
getShardingBroadcastTableNames(logicTableNames);
+ Collection<String> shardingBroadcastLogicTableNames = new
HashSet<>(getShardingBroadcastTableNames(logicTableNames));
return logicTableNames.stream().anyMatch(each ->
!shardingBroadcastLogicTableNames.contains(each));
}
@@ -359,7 +369,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return is sharding column or not
*/
public boolean isShardingColumn(final String columnName, final String
tableName) {
- return
Optional.ofNullable(tableRules.getOrDefault(tableName.toLowerCase(),
null)).filter(each -> isShardingColumn(each, columnName)).isPresent();
+ return
Optional.ofNullable(tableRules.get(tableName.toLowerCase())).filter(each ->
isShardingColumn(each, columnName)).isPresent();
}
private boolean isShardingColumn(final TableRule tableRule, final String
columnName) {
@@ -386,7 +396,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return is generate key column or not
*/
public boolean isGenerateKeyColumn(final String columnName, final String
tableName) {
- return
Optional.ofNullable(tableRules.getOrDefault(tableName.toLowerCase(),
null)).filter(each -> isGenerateKeyColumn(each, columnName)).isPresent();
+ return
Optional.ofNullable(tableRules.get(tableName.toLowerCase())).filter(each ->
isGenerateKeyColumn(each, columnName)).isPresent();
}
private boolean isGenerateKeyColumn(final TableRule tableRule, final
String columnName) {
@@ -401,7 +411,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return column name of generated key
*/
public Optional<String> findGenerateKeyColumnName(final String
logicTableName) {
- return
Optional.ofNullable(tableRules.getOrDefault(logicTableName.toLowerCase(),
null)).filter(each ->
each.getGenerateKeyColumn().isPresent()).flatMap(TableRule::getGenerateKeyColumn);
+ return
Optional.ofNullable(tableRules.get(logicTableName.toLowerCase())).filter(each
->
each.getGenerateKeyColumn().isPresent()).flatMap(TableRule::getGenerateKeyColumn);
}
/**
@@ -437,7 +447,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return sharding logic table names
*/
public Collection<String> getShardingLogicTableNames(final
Collection<String> logicTableNames) {
- return logicTableNames.stream().filter(each ->
findTableRule(each).isPresent()).collect(Collectors.toCollection(LinkedList::new));
+ return
logicTableNames.stream().filter(this::isShardingTable).collect(Collectors.toCollection(LinkedList::new));
}
/**
@@ -447,7 +457,7 @@ public final class ShardingRule implements FeatureRule,
SchemaRule, DataNodeCont
* @return sharding broadcast table names
*/
public Collection<String> getShardingBroadcastTableNames(final
Collection<String> logicTableNames) {
- return logicTableNames.stream().filter(each ->
findTableRule(each).isPresent() ||
broadcastTables.contains(each)).collect(Collectors.toCollection(LinkedList::new));
+ return logicTableNames.stream().filter(each -> isShardingTable(each)
|| isBroadcastTable(each)).collect(Collectors.toCollection(LinkedList::new));
}
/**
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
index 77541d0..879f4ea 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/BindingTableRuleTest.java
@@ -17,11 +17,19 @@
package org.apache.shardingsphere.sharding.rule;
-import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.junit.Test;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
@@ -57,20 +65,25 @@ public final class BindingTableRuleTest {
@Test
public void assertGetAllLogicTables() {
- assertThat(createBindingTableRule().getAllLogicTables(),
is(Arrays.asList("logic_table", "sub_logic_table")));
+ assertThat(createBindingTableRule().getAllLogicTables(), is(new
LinkedHashSet<>(Arrays.asList("logic_table", "sub_logic_table"))));
}
@Test
public void assertGetTableRules() {
- assertThat(createBindingTableRule().getTableRules().size(), is(2));
-
assertThat(createBindingTableRule().getTableRules().get(0).getLogicTable(),
is(createTableRule().getLogicTable()));
-
assertThat(createBindingTableRule().getTableRules().get(0).getActualDataNodes(),
is(createTableRule().getActualDataNodes()));
-
assertThat(createBindingTableRule().getTableRules().get(1).getLogicTable(),
is(createSubTableRule().getLogicTable()));
-
assertThat(createBindingTableRule().getTableRules().get(1).getActualDataNodes(),
is(createSubTableRule().getActualDataNodes()));
+ List<TableRule> tableRules = new
ArrayList<>(createBindingTableRule().getTableRules().values());
+ assertThat(tableRules.size(), is(2));
+ assertThat(tableRules.get(0).getLogicTable(),
is(createTableRule().getLogicTable()));
+ assertThat(tableRules.get(0).getActualDataNodes(),
is(createTableRule().getActualDataNodes()));
+ assertThat(tableRules.get(1).getLogicTable(),
is(createSubTableRule().getLogicTable()));
+ assertThat(tableRules.get(1).getActualDataNodes(),
is(createSubTableRule().getActualDataNodes()));
}
private BindingTableRule createBindingTableRule() {
- return new BindingTableRule(Arrays.asList(createTableRule(),
createSubTableRule()));
+ Map<String, TableRule> tableRules = Stream.of(createTableRule(),
createSubTableRule())
+ .collect(Collectors.toMap(TableRule::getLogicTable,
Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+ BindingTableRule result = new BindingTableRule();
+ result.getTableRules().putAll(tableRules);
+ return result;
}
private TableRule createTableRule() {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index daab3aa..d2fa21d 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -38,6 +38,7 @@ import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
+import java.util.TreeSet;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
@@ -61,7 +62,7 @@ public final class ShardingRuleTest {
assertThat(actual.getTableRules().size(), is(2));
assertThat(actual.getBindingTableRules().size(), is(1));
assertThat(actual.getBindingTableRules().iterator().next().getTableRules().size(),
is(2));
- assertThat(actual.getBroadcastTables(),
is(Collections.singletonList("BROADCAST_TABLE")));
+ assertThat(actual.getBroadcastTables(), is(new
TreeSet<>(Collections.singletonList("BROADCAST_TABLE"))));
assertThat(actual.getDefaultKeyGenerateAlgorithm(),
instanceOf(IncrementKeyGenerateAlgorithm.class));
assertThat(actual.getDefaultShardingColumn(), is("table_id"));
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-governance-spring/shardingsphere-jdbc-governance-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/governance/GovernanceShardingNamespaceTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-governance-spring/shardingsphere-jdbc-governance-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/governance/GovernanceShardingNamespaceTest.java
index c4f8c59..ad926c9 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-governance-spring/shardingsphere-jdbc-governance-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/governance/GovernanceShardingNamespaceTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-governance-spring/shardingsphere-jdbc-governance-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/governance/GovernanceShardingNamespaceTest.java
@@ -132,8 +132,8 @@ public class GovernanceShardingNamespaceTest extends
AbstractJUnit4SpringContext
public void assertMultiBroadcastTableRulesDatasource() {
ShardingRule shardingRule =
getShardingRule("multiBroadcastTableRulesDatasourceGovernance");
assertThat(shardingRule.getBroadcastTables().size(), is(2));
- assertThat(((LinkedList<String>)
shardingRule.getBroadcastTables()).get(0), is("t_config1"));
- assertThat(((LinkedList<String>)
shardingRule.getBroadcastTables()).get(1), is("t_config2"));
+ assertThat(new LinkedList<>(shardingRule.getBroadcastTables()).get(0),
is("t_config1"));
+ assertThat(new LinkedList<>(shardingRule.getBroadcastTables()).get(1),
is("t_config2"));
}
@Test