From 4eb9545152a8518e5df379472c25c3a9dde34c12 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 19 Jan 2025 22:54:53 +0800 Subject: [PATCH] Add ShardingSphereDatabaseFactory --- .../metadata/ShardingSphereMetaData.java | 3 +- .../database/ShardingSphereDatabase.java | 62 ----------- .../ShardingSphereDatabaseFactory.java | 101 ++++++++++++++++++ .../ShardingSphereDatabasesFactory.java | 8 +- .../metadata/ShardingSphereMetaDataTest.java | 3 +- .../database/ShardingSphereDatabaseTest.java | 4 +- .../global/GlobalRulesBuilderTest.java | 3 +- .../factory/MetaDataContextsFactory.java | 5 +- 8 files changed, 116 insertions(+), 73 deletions(-) create mode 100644 infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java index 160975a4471e4..28a2e965b75f2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier; @@ -109,7 +110,7 @@ public ShardingSphereDatabase getDatabase(final String databaseName) { * @param props configuration properties */ public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) { - ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props); + ShardingSphereDatabase database = ShardingSphereDatabaseFactory.create(databaseName, protocolType, props); databases.put(new ShardingSphereIdentifier(database.getName()), database); globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases.values(), GlobalRuleChangedType.DATABASE_CHANGED)); } 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 7da9ea92999e9..ff1ccfe4ee754 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 @@ -19,26 +19,16 @@ import lombok.AccessLevel; import lombok.Getter; -import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; -import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; 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.builder.GenericSchemaBuilder; -import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial; -import org.apache.shardingsphere.infra.metadata.database.schema.builder.SystemSchemaBuilder; 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.builder.database.DatabaseRulesBuilder; import javax.sql.DataSource; -import java.sql.SQLException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -73,58 +63,6 @@ public ShardingSphereDatabase(final String name, final DatabaseType protocolType this.schemas = new ConcurrentHashMap<>(schemas.stream().collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each))); } - /** - * Create system database. - * - * @param name system database name - * @param protocolType protocol database type - * @param props configuration properties - * @return created system database - */ - public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final ConfigurationProperties props) { - DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(new LinkedHashMap<>(), new LinkedList<>()); - ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); - return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(new LinkedList<>()), SystemSchemaBuilder.build(name, protocolType, props).values()); - } - - /** - * Create database. - * - * @param name database name - * @param protocolType database protocol type - * @param databaseConfig database configuration - * @param props configuration properties - * @param computeNodeInstanceContext compute node instance context - * @return created database - * @throws SQLException SQL exception - */ - public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, - final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { - ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); - Collection databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData); - Map schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType, - new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name)))); - SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent); - return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values()); - } - - /** - * Create database. - * - * @param name database name - * @param protocolType database protocol type - * @param databaseConfig database configuration - * @param computeNodeInstanceContext compute node instance context - * @param schemas schemas - * @return created database - */ - public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, - final ComputeNodeInstanceContext computeNodeInstanceContext, final Collection schemas) { - ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); - Collection rules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData); - return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(rules), schemas); - } - /** * Get all schemas. * diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java new file mode 100644 index 0000000000000..bc4d8ce1ce22b --- /dev/null +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java @@ -0,0 +1,101 @@ +/* + * 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.metadata.database; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; +import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; +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.builder.GenericSchemaBuilder; +import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial; +import org.apache.shardingsphere.infra.metadata.database.schema.builder.SystemSchemaBuilder; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * ShardingSphere database factory. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ShardingSphereDatabaseFactory { + + /** + * Create system database. + * + * @param name system database name + * @param protocolType protocol database type + * @param props configuration properties + * @return created system database + */ + public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final ConfigurationProperties props) { + DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(new LinkedHashMap<>(), new LinkedList<>()); + ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); + return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(new LinkedList<>()), SystemSchemaBuilder.build(name, protocolType, props).values()); + } + + /** + * Create database. + * + * @param name database name + * @param protocolType database protocol type + * @param databaseConfig database configuration + * @param props configuration properties + * @param computeNodeInstanceContext compute node instance context + * @return created database + * @throws SQLException SQL exception + */ + public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, + final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException { + ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); + Collection databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData); + Map schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType, + new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name)))); + SystemSchemaBuilder.build(name, protocolType, props).forEach(schemas::putIfAbsent); + return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values()); + } + + /** + * Create database. + * + * @param name database name + * @param protocolType database protocol type + * @param databaseConfig database configuration + * @param computeNodeInstanceContext compute node instance context + * @param schemas schemas + * @return created database + */ + public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, + final ComputeNodeInstanceContext computeNodeInstanceContext, final Collection schemas) { + ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits()); + Collection rules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData); + return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(rules), schemas); + } +} diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java index c650951b467e2..46e83df134d4b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabasesFactory.java @@ -62,8 +62,8 @@ private static ShardingSphereDatabase create(final String databaseName, final Da final Collection schemas, final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) { return databaseConfig.getStorageUnits().isEmpty() - ? ShardingSphereDatabase.create(databaseName, protocolType, props) - : ShardingSphereDatabase.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, computeNodeInstanceContext, schemas); + ? ShardingSphereDatabaseFactory.create(databaseName, protocolType, props) + : ShardingSphereDatabaseFactory.create(databaseName, DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, computeNodeInstanceContext, schemas); } /** @@ -92,7 +92,7 @@ private static Collection createGenericDatabases(final M for (Entry entry : databaseConfigMap.entrySet()) { String databaseName = entry.getKey(); if (!entry.getValue().getStorageUnits().isEmpty() || !systemDatabase.getSystemSchemas().contains(databaseName)) { - result.add(ShardingSphereDatabase.create(databaseName, protocolType, entry.getValue(), props, instanceContext)); + result.add(ShardingSphereDatabaseFactory.create(databaseName, protocolType, entry.getValue(), props, instanceContext)); } } return result; @@ -103,7 +103,7 @@ private static Collection createSystemDatabases(final Ma Collection result = new HashSet<>(systemDatabase.getSystemDatabaseSchemaMap().size(), 1F); for (String each : systemDatabase.getSystemDatabaseSchemaMap().keySet()) { if (!databaseConfigMap.containsKey(each) || databaseConfigMap.get(each).getStorageUnits().isEmpty()) { - result.add(ShardingSphereDatabase.create(each, protocolType, props)); + result.add(ShardingSphereDatabaseFactory.create(each, protocolType, props)); } } return result; diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java index f1f4a28fbc453..3b5f5c484a880 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; @@ -68,7 +69,7 @@ void assertAddDatabase() { ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule); DatabaseType databaseType = mock(DatabaseType.class); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); - when(ShardingSphereDatabase.create("foo_db", databaseType, configProps)).thenReturn(database); + when(ShardingSphereDatabaseFactory.create("foo_db", databaseType, configProps)).thenReturn(database); Collection databases = new LinkedList<>(Collections.singleton(database)); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps); metaData.addDatabase("foo_db", databaseType, configProps); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java index 03bea57493904..e6d3bd9a63528 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java @@ -154,7 +154,7 @@ void assertReloadRules() { @Test void assertGetPostgreSQLDefaultSchema() throws SQLException { DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"); - ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, + ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("foo_db", databaseType, mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); assertNotNull(actual.getSchema("public")); } @@ -162,7 +162,7 @@ void assertGetPostgreSQLDefaultSchema() throws SQLException { @Test void assertGetMySQLDefaultSchema() throws SQLException { DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); - ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, + ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.create("foo_db", databaseType, mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(ComputeNodeInstanceContext.class)); assertNotNull(actual.getSchema("foo_db")); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java index 6dbf6f6f747c1..94ec9c415236f 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilderTest.java @@ -20,6 +20,7 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.fixture.FixtureGlobalRule; import org.apache.shardingsphere.infra.rule.builder.fixture.FixtureGlobalRuleConfiguration; @@ -51,6 +52,6 @@ void assertBuildSingleRules() { } private ShardingSphereDatabase buildDatabase() { - return ShardingSphereDatabase.create("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), new ConfigurationProperties(new Properties())); + return ShardingSphereDatabaseFactory.create("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), new ConfigurationProperties(new Properties())); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/MetaDataContextsFactory.java index 815670890b780..b9eb4ed10bdcb 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/MetaDataContextsFactory.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; @@ -154,8 +155,8 @@ private ShardingSphereDatabase createChangedDatabase(final String databaseName, final ConfigurationProperties props) throws SQLException { DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(databaseConfig, props); return isLoadSchemasFromRegisterCenter - ? ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, instanceContext, persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName)) - : ShardingSphereDatabase.create(databaseName, protocolType, databaseConfig, props, instanceContext); + ? ShardingSphereDatabaseFactory.create(databaseName, protocolType, databaseConfig, instanceContext, persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName)) + : ShardingSphereDatabaseFactory.create(databaseName, protocolType, databaseConfig, props, instanceContext); } private ResourceMetaData getEffectiveResourceMetaData(final ShardingSphereDatabase database, final SwitchingResource switchingResource) {