This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 ed49a138d56 Filter empty rules before build database rules (#38249)
ed49a138d56 is described below
commit ed49a138d568a96d4f4b9956b91cf3857e2fe77c
Author: Haoran Meng <[email protected]>
AuthorDate: Fri Feb 27 21:55:54 2026 +0800
Filter empty rules before build database rules (#38249)
---
.../builder/database/DatabaseRulesBuilder.java | 21 +++++++++-
.../builder/database/DatabaseRulesBuilderTest.java | 29 ++++++++++++--
.../fixture/ToggleFixtureDatabaseRuleBuilder.java | 46 ++++++++++++++++++++++
.../ToggleFixtureDatabaseRuleConfiguration.java | 34 ++++++++++++++++
...xtureDatabaseRuleConfigurationEmptyChecker.java | 33 ++++++++++++++++
.../rule/builder/fixture/ToggleFixtureRule.java | 40 +++++++++++++++++++
....checker.DatabaseRuleConfigurationEmptyChecker} | 2 +-
...infra.rule.builder.database.DatabaseRuleBuilder | 1 +
.../datasource/ShardingSphereDataSourceTest.java | 4 +-
9 files changed, 202 insertions(+), 8 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java
index 1aa88558f79..43df1c96d87 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilder.java
@@ -23,12 +23,15 @@ import
org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationChecker;
+import
org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker;
import
org.apache.shardingsphere.infra.config.rule.function.DistributedRuleConfiguration;
import
org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import java.util.Arrays;
import java.util.Collection;
@@ -96,13 +99,27 @@ public final class DatabaseRulesBuilder {
@SuppressWarnings("rawtypes")
private static Map<RuleConfiguration, DatabaseRuleBuilder>
getRuleBuilderMap(final DatabaseConfiguration databaseConfig) {
+ Collection<RuleConfiguration> filteredRuleConfigs =
filterEmptyRuleConfigurations(databaseConfig.getRuleConfigurations());
Map<RuleConfiguration, DatabaseRuleBuilder> result = new
LinkedHashMap<>();
-
result.putAll(getDistributedRuleBuilderMap(databaseConfig.getRuleConfigurations()));
-
result.putAll(getEnhancedRuleBuilderMap(databaseConfig.getRuleConfigurations()));
+ result.putAll(getDistributedRuleBuilderMap(filteredRuleConfigs));
+ result.putAll(getEnhancedRuleBuilderMap(filteredRuleConfigs));
result.putAll(getMissedDefaultRuleBuilderMap(result.values()));
return result;
}
+ private static Collection<RuleConfiguration>
filterEmptyRuleConfigurations(final Collection<RuleConfiguration> ruleConfigs) {
+ return ruleConfigs.stream().filter(each ->
!isRuleConfigurationEmpty(each)).collect(Collectors.toList());
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private static boolean isRuleConfigurationEmpty(final RuleConfiguration
ruleConfig) {
+ if (!(ruleConfig instanceof DatabaseRuleConfiguration)) {
+ return false;
+ }
+ return
TypedSPILoader.findService(DatabaseRuleConfigurationEmptyChecker.class,
ruleConfig.getClass())
+ .map(checker -> checker.isEmpty((DatabaseRuleConfiguration)
ruleConfig)).orElse(false);
+ }
+
@SuppressWarnings("rawtypes")
private static Map<RuleConfiguration, DatabaseRuleBuilder>
getDistributedRuleBuilderMap(final Collection<RuleConfiguration> ruleConfigs) {
Collection<RuleConfiguration> distributedRuleConfigs =
ruleConfigs.stream().filter(each -> isAssignableFrom(each,
DistributedRuleConfiguration.class)).collect(Collectors.toList());
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java
index 55914ea5291..33ada466754 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/database/DatabaseRulesBuilderTest.java
@@ -20,8 +20,11 @@ package
org.apache.shardingsphere.infra.rule.builder.database;
import
org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.fixture.FixtureRule;
import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.builder.fixture.FixtureDatabaseRuleConfiguration;
+import
org.apache.shardingsphere.infra.rule.builder.fixture.ToggleFixtureDatabaseRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.fixture.ToggleFixtureRule;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
@@ -31,21 +34,39 @@ import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class DatabaseRulesBuilderTest {
+ private static final ResourceMetaData EMPTY_RESOURCE_META_DATA = new
ResourceMetaData(Collections.emptyMap(), Collections.emptyMap());
+
@Test
void assertBuildMultipleRules() {
- List<ShardingSphereRule> actual = new
ArrayList<>(DatabaseRulesBuilder.build("foo_db", mock(),
- new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new FixtureRuleConfiguration())), mock(), mock()));
+ List<ShardingSphereRule> actual = new
ArrayList<>(DatabaseRulesBuilder.build("foo_db", null,
+ new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new FixtureRuleConfiguration())), null,
EMPTY_RESOURCE_META_DATA));
assertThat(actual.size(), is(1));
assertThat(actual.get(0), isA(FixtureRule.class));
}
@Test
void assertBuildSingleRule() {
- ShardingSphereRule actual = DatabaseRulesBuilder.build("foo_db",
mock(), Collections.emptyList(), new FixtureDatabaseRuleConfiguration(),
mock(), mock());
+ ShardingSphereRule actual = DatabaseRulesBuilder.build("foo_db", null,
Collections.emptyList(), new FixtureDatabaseRuleConfiguration(), null,
EMPTY_RESOURCE_META_DATA);
assertThat(actual, isA(FixtureRule.class));
}
+
+ @Test
+ void assertBuildWithEmptyDatabaseRuleConfiguration() {
+ List<ShardingSphereRule> actual = new
ArrayList<>(DatabaseRulesBuilder.build("foo_db", null,
+ new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new ToggleFixtureDatabaseRuleConfiguration(true))), null,
EMPTY_RESOURCE_META_DATA));
+ assertThat(actual.size(), is(1));
+ assertThat(actual.get(0), isA(FixtureRule.class));
+ }
+
+ @Test
+ void assertBuildWithNonEmptyDatabaseRuleConfiguration() {
+ List<ShardingSphereRule> actual = new
ArrayList<>(DatabaseRulesBuilder.build("foo_db", null,
+ new
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(),
Collections.singleton(new ToggleFixtureDatabaseRuleConfiguration(false))),
null, EMPTY_RESOURCE_META_DATA));
+ assertThat(actual.size(), is(2));
+
assertTrue(actual.stream().anyMatch(ToggleFixtureRule.class::isInstance));
+ }
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleBuilder.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleBuilder.java
new file mode 100644
index 00000000000..60a446bb6f0
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleBuilder.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.rule.builder.fixture;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import
org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder;
+
+import java.util.Collection;
+
+public final class ToggleFixtureDatabaseRuleBuilder implements
DatabaseRuleBuilder<ToggleFixtureDatabaseRuleConfiguration> {
+
+ @Override
+ public ToggleFixtureRule build(final
ToggleFixtureDatabaseRuleConfiguration ruleConfig, final String databaseName,
final DatabaseType protocolType,
+ final ResourceMetaData resourceMetaData,
final Collection<ShardingSphereRule> builtRules,
+ final ComputeNodeInstanceContext
computeNodeInstanceContext) {
+ return new ToggleFixtureRule(ruleConfig);
+ }
+
+ @Override
+ public int getOrder() {
+ return 2;
+ }
+
+ @Override
+ public Class<ToggleFixtureDatabaseRuleConfiguration> getTypeClass() {
+ return ToggleFixtureDatabaseRuleConfiguration.class;
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfiguration.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfiguration.java
new file mode 100644
index 00000000000..c66631ba823
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfiguration.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.rule.builder.fixture;
+
+import
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;
+
+public final class ToggleFixtureDatabaseRuleConfiguration implements
DatabaseRuleConfiguration, EnhancedRuleConfiguration {
+
+ private final boolean empty;
+
+ public ToggleFixtureDatabaseRuleConfiguration(final boolean empty) {
+ this.empty = empty;
+ }
+
+ public boolean isEmpty() {
+ return empty;
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfigurationEmptyChecker.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfigurationEmptyChecker.java
new file mode 100644
index 00000000000..b749fe878f7
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureDatabaseRuleConfigurationEmptyChecker.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.rule.builder.fixture;
+
+import
org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker;
+
+public final class ToggleFixtureDatabaseRuleConfigurationEmptyChecker
implements
DatabaseRuleConfigurationEmptyChecker<ToggleFixtureDatabaseRuleConfiguration> {
+
+ @Override
+ public boolean isEmpty(final ToggleFixtureDatabaseRuleConfiguration
ruleConfig) {
+ return ruleConfig.isEmpty();
+ }
+
+ @Override
+ public Class<ToggleFixtureDatabaseRuleConfiguration> getType() {
+ return ToggleFixtureDatabaseRuleConfiguration.class;
+ }
+}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureRule.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureRule.java
new file mode 100644
index 00000000000..37cda8e6e93
--- /dev/null
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/ToggleFixtureRule.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.infra.rule.builder.fixture;
+
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
+
+public final class ToggleFixtureRule implements DatabaseRule {
+
+ private final RuleConfiguration ruleConfig;
+
+ public ToggleFixtureRule(final RuleConfiguration ruleConfig) {
+ this.ruleConfig = ruleConfig;
+ }
+
+ @Override
+ public RuleConfiguration getConfiguration() {
+ return ruleConfig;
+ }
+
+ @Override
+ public int getOrder() {
+ return 2;
+ }
+}
diff --git
a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker
similarity index 88%
copy from
infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
copy to
infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker
index 4f1bf075c45..0ece9b5aa07 100644
---
a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
+++
b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.DatabaseRuleConfigurationEmptyChecker
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.infra.rule.builder.fixture.FixtureDatabaseRuleBuilder
+org.apache.shardingsphere.infra.rule.builder.fixture.ToggleFixtureDatabaseRuleConfigurationEmptyChecker
diff --git
a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
index 4f1bf075c45..18b375a5137 100644
---
a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
+++
b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
@@ -16,3 +16,4 @@
#
org.apache.shardingsphere.infra.rule.builder.fixture.FixtureDatabaseRuleBuilder
+org.apache.shardingsphere.infra.rule.builder.fixture.ToggleFixtureDatabaseRuleBuilder
diff --git
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
index 9dc3f95b413..45501aa7da5 100644
---
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
+++
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
@@ -84,9 +84,11 @@ class ShardingSphereDataSourceTest {
when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
CacheOption cacheOption = new CacheOption(1024, 1024L);
SQLParserRuleConfiguration sqlParserRuleConfig = new
SQLParserRuleConfiguration(cacheOption, cacheOption);
+ ShardingRuleConfiguration shardingRuleConfig = new
ShardingRuleConfiguration();
+ shardingRuleConfig.setDefaultShardingColumn("user_id");
try (
ShardingSphereDataSource actual = new
ShardingSphereDataSource("foo_db",
- null, Collections.singletonMap("ds", new
MockedDataSource(connection)),
Arrays.asList(mock(ShardingRuleConfiguration.class), sqlParserRuleConfig), new
Properties())) {
+ null, Collections.singletonMap("ds", new
MockedDataSource(connection)), Arrays.asList(shardingRuleConfig,
sqlParserRuleConfig), new Properties())) {
assertThat(getContextManager(actual).getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getConfigurations().size(),
is(2));
}
}