This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 3f14971c20e Add ShardingSphereDatabase.checkStorageUnitsExisted()
(#37161)
3f14971c20e is described below
commit 3f14971c20eff3d134b72ea3ef0de4436d869498
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Nov 22 20:18:59 2025 +0800
Add ShardingSphereDatabase.checkStorageUnitsExisted() (#37161)
* Add ShardingSphereDatabase.checkStorageUnitsExisted()
* Add ShardingSphereDatabase.checkStorageUnitsExisted()
* Add ShardingSphereDatabase.checkStorageUnitsExisted()
* Add ShardingSphereDatabase.checkStorageUnitsExisted()
---
.../checker/DatabaseRuleConfigurationCheckEngine.java | 12 +-----------
.../infra/metadata/database/ShardingSphereDatabase.java | 16 ++++++++++++++++
.../DatabaseRuleConfigurationCheckEngineTest.java | 13 -------------
.../distsql/handler/update/LoadSingleTableExecutor.java | 14 ++------------
.../handler/update/LoadSingleTableExecutorTest.java | 7 -------
5 files changed, 19 insertions(+), 43 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngine.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngine.java
index 1143262756d..9eb3e25b17c 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngine.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngine.java
@@ -21,10 +21,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import javax.sql.DataSource;
@@ -55,7 +53,7 @@ public final class DatabaseRuleConfigurationCheckEngine {
}
Collection<String> requiredDataSourceNames =
checker.getRequiredDataSourceNames(ruleConfig);
if (!requiredDataSourceNames.isEmpty()) {
- checkDataSourcesExisted(database, requiredDataSourceNames);
+ database.checkStorageUnitsExisted(requiredDataSourceNames);
}
Collection<String> tableNames = checker.getTableNames(ruleConfig);
if (!tableNames.isEmpty()) {
@@ -65,14 +63,6 @@ public final class DatabaseRuleConfigurationCheckEngine {
checker.check(database.getName(), ruleConfig, dataSources,
database.getRuleMetaData().getRules());
}
- private static void checkDataSourcesExisted(final ShardingSphereDatabase
database, final Collection<String> requiredDataSourceNames) {
- Collection<String> notExistedDataSources =
database.getResourceMetaData().getNotExistedDataSources(requiredDataSourceNames);
- Collection<String> logicDataSources =
database.getRuleMetaData().getAttributes(DataSourceMapperRuleAttribute.class).stream()
- .flatMap(each ->
each.getDataSourceMapper().keySet().stream()).collect(Collectors.toSet());
- notExistedDataSources.removeIf(logicDataSources::contains);
- ShardingSpherePreconditions.checkMustEmpty(notExistedDataSources, ()
-> new MissingRequiredStorageUnitsException(database.getName(),
notExistedDataSources));
- }
-
private static void checkTablesNotDuplicated(final RuleConfiguration
ruleConfig, final String databaseName, final Collection<String> tableNames) {
Collection<String> duplicatedTables = tableNames.stream()
.collect(Collectors.groupingBy(each -> each,
Collectors.counting())).entrySet().stream().filter(each -> each.getValue() >
1L).map(Entry::getKey).collect(Collectors.toSet());
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
index 4a5ddc4aba7..ed864dad6f8 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java
@@ -21,12 +21,15 @@ import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
+import
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import javax.sql.DataSource;
import java.util.Collection;
@@ -145,4 +148,17 @@ public final class ShardingSphereDatabase {
ruleMetaData.getRules().clear();
ruleMetaData.getRules().addAll(rules);
}
+
+ /**
+ * Check storage units existed.
+ *
+ * @param storageUnitNames storage unit names
+ */
+ public void checkStorageUnitsExisted(final Collection<String>
storageUnitNames) {
+ Collection<String> notExistedDataSources =
resourceMetaData.getNotExistedDataSources(storageUnitNames);
+ Collection<String> logicDataSources =
ruleMetaData.getAttributes(DataSourceMapperRuleAttribute.class).stream()
+ .flatMap(each ->
each.getDataSourceMapper().keySet().stream()).collect(Collectors.toSet());
+ notExistedDataSources.removeIf(logicDataSources::contains);
+ ShardingSpherePreconditions.checkMustEmpty(notExistedDataSources, ()
-> new MissingRequiredStorageUnitsException(name, notExistedDataSources));
+ }
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngineTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngineTest.java
index e2223843fc6..d7b16de79be 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngineTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/checker/DatabaseRuleConfigurationCheckEngineTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.infra.config.rule.checker;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -103,18 +102,6 @@ class DatabaseRuleConfigurationCheckEngineTest {
assertDoesNotThrow(() ->
DatabaseRuleConfigurationCheckEngine.check(ruleConfig, database));
}
- @Test
- void assertCheckWithMissingDataSources() {
-
when(OrderedSPILoader.getServicesByClass(DatabaseRuleConfigurationChecker.class,
Collections.singleton(FixtureRuleConfiguration.class)))
-
.thenReturn(Collections.singletonMap(FixtureRuleConfiguration.class, checker));
- Collection<String> requiredDataSources = Arrays.asList("foo_ds",
"bar_ds");
-
when(checker.getRequiredDataSourceNames(any())).thenReturn(requiredDataSources);
-
when(resourceMetaData.getNotExistedDataSources(requiredDataSources)).thenReturn(new
LinkedList<>(Collections.singleton("foo_ds")));
-
when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("bar_ds",
mock(StorageUnit.class)));
- MissingRequiredStorageUnitsException exception =
assertThrows(MissingRequiredStorageUnitsException.class, () ->
DatabaseRuleConfigurationCheckEngine.check(ruleConfig, database));
- assertThat(exception.getMessage(), is("Storage units 'foo_ds' do not
exist in database 'foo_db'."));
- }
-
@Test
void assertCheckWithDuplicatedTableNames() {
when(OrderedSPILoader.getServicesByClass(DatabaseRuleConfigurationChecker.class,
Collections.singleton(FixtureRuleConfiguration.class)))
diff --git
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
index 03f1b1322aa..c4f4bf4c069 100644
---
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
+++
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java
@@ -28,11 +28,9 @@ import
org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundEx
import
org.apache.shardingsphere.infra.exception.kernel.metadata.datanode.InvalidDataNodeFormatException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.EmptyStorageUnitException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.InvalidStorageUnitStatusException;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.PhysicalDataSourceAggregator;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.constant.SingleTableConstants;
import org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader;
@@ -62,7 +60,8 @@ public final class LoadSingleTableExecutor implements
DatabaseRuleCreateExecutor
public void checkBeforeUpdate(final LoadSingleTableStatement sqlStatement)
{
Collection<String> storageUnitNames =
getStorageUnitNames(sqlStatement);
if (!storageUnitNames.isEmpty()) {
- checkShouldExistStorageUnits(storageUnitNames);
+
ShardingSpherePreconditions.checkNotEmpty(database.getResourceMetaData().getStorageUnits(),
() -> new EmptyStorageUnitException(database.getName()));
+ database.checkStorageUnitsExisted(storageUnitNames);
}
String defaultSchemaName = new
DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName());
checkShouldNotExistLogicTables(sqlStatement, defaultSchemaName);
@@ -75,15 +74,6 @@ public final class LoadSingleTableExecutor implements
DatabaseRuleCreateExecutor
return
sqlStatement.getTables().stream().map(SingleTableSegment::getStorageUnitName).filter(each
-> !SingleTableConstants.ASTERISK.equals(each)).collect(Collectors.toSet());
}
- private void checkShouldExistStorageUnits(final Collection<String>
storageUnitNames) {
-
ShardingSpherePreconditions.checkNotEmpty(database.getResourceMetaData().getStorageUnits(),
() -> new EmptyStorageUnitException(database.getName()));
- Collection<String> notExistedStorageUnitNames =
database.getResourceMetaData().getNotExistedDataSources(storageUnitNames);
- Collection<String> logicDataSourceNames =
database.getRuleMetaData().getAttributes(DataSourceMapperRuleAttribute.class).stream()
- .flatMap(each ->
each.getDataSourceMapper().keySet().stream()).collect(Collectors.toSet());
- notExistedStorageUnitNames.removeIf(logicDataSourceNames::contains);
- ShardingSpherePreconditions.checkMustEmpty(notExistedStorageUnitNames,
() -> new MissingRequiredStorageUnitsException(database.getName(),
notExistedStorageUnitNames));
- }
-
private void checkShouldNotExistLogicTables(final LoadSingleTableStatement
sqlStatement, final String defaultSchemaName) {
Collection<SingleTableSegment> tableSegments =
sqlStatement.getTables();
DialectDatabaseMetaData dialectDatabaseMetaData = new
DatabaseTypeRegistry(database.getProtocolType()).getDialectDatabaseMetaData();
diff --git
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
index f65e171ec8b..6cf22fcb2b5 100644
---
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutorTest.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExec
import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
import
org.apache.shardingsphere.infra.exception.kernel.metadata.datanode.InvalidDataNodeFormatException;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.PhysicalDataSourceAggregator;
import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
@@ -96,12 +95,6 @@ class LoadSingleTableExecutorTest {
return result;
}
- @Test
- void assertExecuteUpdateWithNotExistedStorageUnits() {
- LoadSingleTableStatement sqlStatement = new
LoadSingleTableStatement(Collections.singleton(new SingleTableSegment("foo_ds",
"foo_tbl")));
- assertThrows(MissingRequiredStorageUnitsException.class, () -> new
DistSQLUpdateExecuteEngine(sqlStatement, "foo_db",
mockContextManager(mock(SingleRule.class))).executeUpdate());
- }
-
@Test
void assertExecuteUpdateWithInvalidTableNodeFormatWhenSchemaNotSupported()
{
when(schema.containsTable("foo_tbl")).thenReturn(true);