This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin 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 aaa0a84cf4c Refactor DatabaseTypeEngine logic and update unit test
(#21758)
aaa0a84cf4c is described below
commit aaa0a84cf4c55832f49914ea3d7abe55ba52485f
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Oct 26 11:08:05 2022 +0800
Refactor DatabaseTypeEngine logic and update unit test (#21758)
* Refactor DatabaseTypeEngine logic
* fix unit test
---
.../infra/database/type/DatabaseTypeEngine.java | 41 ++++++++++------------
.../database/ShardingSphereDatabasesFactory.java | 5 ++-
.../resource/ShardingSphereResourceMetaData.java | 6 +---
.../database/type/DatabaseTypeEngineTest.java | 24 ++++++-------
.../datetime/database/DatabaseDatetimeService.java | 2 +-
.../DatabaseDatetimeServiceConfiguration.java | 4 +--
.../DatabaseDatetimeServiceConfigurationTest.java | 2 +-
.../singletable/rule/SingleTableRule.java | 2 +-
.../mode/manager/ContextManagerTest.java | 6 ++--
9 files changed, 41 insertions(+), 51 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
index fb381eade72..da1444ec74b 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngine.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.infra.database.type;
-import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
@@ -52,7 +51,7 @@ public final class DatabaseTypeEngine {
* @return protocol type
*/
public static DatabaseType getProtocolType(final String databaseName,
final DatabaseConfiguration databaseConfig, final ConfigurationProperties
props) {
- return findConfiguredDatabaseType(props).orElseGet(() ->
getDatabaseType(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName,
databaseConfig)));
+ return findConfiguredDatabaseType(props).orElseGet(() ->
getStorageType(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName,
databaseConfig)));
}
/**
@@ -64,7 +63,7 @@ public final class DatabaseTypeEngine {
*/
public static DatabaseType getProtocolType(final Map<String, ? extends
DatabaseConfiguration> databaseConfigs, final ConfigurationProperties props) {
Optional<DatabaseType> configuredDatabaseType =
findConfiguredDatabaseType(props);
- return configuredDatabaseType.orElseGet(() ->
getDatabaseType(getEnabledDataSources(databaseConfigs).values()));
+ return configuredDatabaseType.orElseGet(() ->
getStorageType(getEnabledDataSources(databaseConfigs).values()));
}
private static Map<String, DataSource> getEnabledDataSources(final
Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
@@ -78,13 +77,15 @@ public final class DatabaseTypeEngine {
/**
* Get storage types.
*
- * @param databaseConfigs database configs
+ * @param databaseName database name
+ * @param databaseConfig database configuration
* @return storage types
*/
- public static Map<String, DatabaseType> getStorageTypes(final Map<String,
? extends DatabaseConfiguration> databaseConfigs) {
- Map<String, DatabaseType> result = new
LinkedHashMap<>(databaseConfigs.size(), 1);
- for (Entry<String, DataSource> entry :
getEnabledDataSources(databaseConfigs).entrySet()) {
- result.put(entry.getKey(), getDatabaseType(entry.getValue()));
+ public static Map<String, DatabaseType> getStorageTypes(final String
databaseName, final DatabaseConfiguration databaseConfig) {
+ Map<String, DatabaseType> result = new
LinkedHashMap<>(databaseConfig.getDataSources().size(), 1);
+ Map<String, DataSource> enabledDataSources =
DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
databaseConfig.getDataSources());
+ for (Entry<String, DataSource> entry : enabledDataSources.entrySet()) {
+ result.put(entry.getKey(), getStorageType(entry.getValue()));
}
return result;
}
@@ -100,22 +101,16 @@ public final class DatabaseTypeEngine {
}
/**
- * Get database type.
- *
+ * Get storage type.
+ *
* @param dataSources data sources
- * @return database type
+ * @return storage type
*/
- public static DatabaseType getDatabaseType(final Collection<DataSource>
dataSources) {
- DatabaseType result = null;
- for (DataSource each : dataSources) {
- DatabaseType databaseType = getDatabaseType(each);
- Preconditions.checkState(null == result || result == databaseType,
"Database type inconsistent with '%s' and '%s'", result, databaseType);
- result = databaseType;
- }
- return null == result ?
DatabaseTypeFactory.getInstance(DEFAULT_DATABASE_TYPE) : result;
+ public static DatabaseType getStorageType(final Collection<DataSource>
dataSources) {
+ return dataSources.isEmpty() ?
DatabaseTypeFactory.getInstance(DEFAULT_DATABASE_TYPE) :
getStorageType(dataSources.iterator().next());
}
- private static DatabaseType getDatabaseType(final DataSource dataSource) {
+ private static DatabaseType getStorageType(final DataSource dataSource) {
try (Connection connection = dataSource.getConnection()) {
return getDatabaseType(connection.getMetaData().getURL());
} catch (final SQLException ex) {
@@ -167,10 +162,10 @@ public final class DatabaseTypeEngine {
/**
* Get default schema name.
*
- * @param databaseType database type
+ * @param protocolType protocol type
* @return default schema name
*/
- public static Optional<String> getDefaultSchemaName(final DatabaseType
databaseType) {
- return databaseType instanceof SchemaSupportedDatabaseType ?
Optional.of(((SchemaSupportedDatabaseType) databaseType).getDefaultSchema()) :
Optional.empty();
+ public static Optional<String> getDefaultSchemaName(final DatabaseType
protocolType) {
+ return protocolType instanceof SchemaSupportedDatabaseType ?
Optional.of(((SchemaSupportedDatabaseType) protocolType).getDefaultSchema()) :
Optional.empty();
}
}
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 f798c7cc068..3442f84202d 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
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import java.sql.SQLException;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -48,7 +47,7 @@ public final class ShardingSphereDatabasesFactory {
public static ShardingSphereDatabase create(final String databaseName,
final DatabaseConfiguration databaseConfig,
final ConfigurationProperties
props, final InstanceContext instanceContext) throws SQLException {
DatabaseType protocolType =
DatabaseTypeEngine.getProtocolType(databaseName, databaseConfig, props);
- Map<String, DatabaseType> storageTypes =
DatabaseTypeEngine.getStorageTypes(Collections.singletonMap(databaseName,
databaseConfig));
+ Map<String, DatabaseType> storageTypes =
DatabaseTypeEngine.getStorageTypes(databaseName, databaseConfig);
return ShardingSphereDatabase.create(databaseName, protocolType,
storageTypes, databaseConfig, props, instanceContext);
}
@@ -76,7 +75,7 @@ public final class ShardingSphereDatabasesFactory {
for (Entry<String, DatabaseConfiguration> entry :
databaseConfigMap.entrySet()) {
String databaseName = entry.getKey();
if (!entry.getValue().getDataSources().isEmpty() ||
!protocolType.getSystemSchemas().contains(databaseName)) {
- Map<String, DatabaseType> storageTypes =
DatabaseTypeEngine.getStorageTypes(Collections.singletonMap(entry.getKey(),
entry.getValue()));
+ Map<String, DatabaseType> storageTypes =
DatabaseTypeEngine.getStorageTypes(entry.getKey(), entry.getValue());
result.put(databaseName.toLowerCase(),
ShardingSphereDatabase.create(databaseName, protocolType, storageTypes,
entry.getValue(), props, instanceContext));
}
}
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceMetaData.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceMetaData.java
index 360693433cc..4df73b12a26 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceMetaData.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceMetaData.java
@@ -58,15 +58,11 @@ public final class ShardingSphereResourceMetaData {
private Map<String, DatabaseType> createStorageTypes(final Map<String,
DataSource> dataSources) {
Map<String, DatabaseType> result = new
LinkedHashMap<>(dataSources.size(), 1);
for (Entry<String, DataSource> entry : dataSources.entrySet()) {
- result.put(entry.getKey(),
getDatabaseType(Collections.singletonMap(entry.getKey(), entry.getValue())));
+ result.put(entry.getKey(),
DatabaseTypeEngine.getStorageType(Collections.singletonList(entry.getValue())));
}
return result;
}
- private DatabaseType getDatabaseType(final Map<String, DataSource>
dataSources) {
- return dataSources.isEmpty() ? null :
DatabaseTypeEngine.getDatabaseType(dataSources.values());
- }
-
private Map<String, DataSourceMetaData> createDataSourceMetaDataMap(final
Map<String, DataSource> dataSources, final Map<String, DatabaseType>
storageTypes) {
Map<String, DataSourceMetaData> result = new
LinkedHashMap<>(dataSources.size(), 1);
for (Entry<String, DataSource> entry : dataSources.entrySet()) {
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
index b8004c6221b..a24ea175835 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeEngineTest.java
@@ -65,35 +65,35 @@ public final class DatabaseTypeEngineTest {
}
@Test
- public void assertGetStorageType() throws SQLException {
+ public void assertGetStorageTypes() throws SQLException {
DataSource datasource =
mockDataSource(DatabaseTypeFactory.getInstance("MySQL"));
DatabaseConfiguration databaseConfig = new
DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_db",
datasource), Collections.singletonList(new FixtureRuleConfiguration()));
-
assertTrue(DatabaseTypeEngine.getStorageTypes(Collections.singletonMap("foo_db",
databaseConfig)).containsKey("foo_db"));
-
assertThat(DatabaseTypeEngine.getStorageTypes(Collections.singletonMap("foo_db",
databaseConfig)).get("foo_db"), instanceOf(MySQLDatabaseType.class));
+ assertTrue(DatabaseTypeEngine.getStorageTypes("foo_db",
databaseConfig).containsKey("foo_db"));
+ assertThat(DatabaseTypeEngine.getStorageTypes("foo_db",
databaseConfig).get("foo_db"), instanceOf(MySQLDatabaseType.class));
}
@Test
- public void assertGetDatabaseTypeWithEmptyDataSources() {
-
assertThat(DatabaseTypeEngine.getDatabaseType(Collections.emptyList()).getType(),
is("MySQL"));
+ public void assertGetStorageTypeWithEmptyDataSources() {
+
assertThat(DatabaseTypeEngine.getStorageType(Collections.emptyList()).getType(),
is("MySQL"));
}
@Test
- public void assertGetDatabaseTypeWithDataSources() throws SQLException {
+ public void assertGetStorageTypeWithDataSources() throws SQLException {
Collection<DataSource> dataSources =
Arrays.asList(mockDataSource(DatabaseTypeFactory.getInstance("H2")),
mockDataSource(DatabaseTypeFactory.getInstance("H2")));
- assertThat(DatabaseTypeEngine.getDatabaseType(dataSources).getType(),
is("H2"));
+ assertThat(DatabaseTypeEngine.getStorageType(dataSources).getType(),
is("H2"));
}
- @Test(expected = IllegalStateException.class)
- public void assertGetDatabaseTypeWithDifferentDataSourceTypes() throws
SQLException {
+ @Test
+ public void assertGetStorageTypeWithDifferentDataSourceTypes() throws
SQLException {
Collection<DataSource> dataSources =
Arrays.asList(mockDataSource(DatabaseTypeFactory.getInstance("H2")),
mockDataSource(DatabaseTypeFactory.getInstance("MySQL")));
- DatabaseTypeEngine.getDatabaseType(dataSources);
+ assertThat(DatabaseTypeEngine.getStorageType(dataSources).getType(),
is("H2"));
}
@Test(expected = SQLWrapperException.class)
- public void assertGetDatabaseTypeWhenGetConnectionError() throws
SQLException {
+ public void assertGetStorageTypeWhenGetConnectionError() throws
SQLException {
DataSource dataSource = mock(DataSource.class);
when(dataSource.getConnection()).thenThrow(SQLException.class);
- DatabaseTypeEngine.getDatabaseType(Collections.singleton(dataSource));
+ DatabaseTypeEngine.getStorageType(Collections.singleton(dataSource));
}
@Test
diff --git
a/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java
b/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java
index 6946c6496a4..2f39a21cccb 100644
---
a/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java
+++
b/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/DatabaseDatetimeService.java
@@ -39,7 +39,7 @@ public final class DatabaseDatetimeService implements
DatetimeService {
@Override
public Date getDatetime() {
try {
- return loadDatetime(timeServiceConfig.getDataSource(),
DatetimeLoadingSQLProviderFactory.getInstance(timeServiceConfig.getDatabaseType()).getDatetimeLoadingSQL());
+ return loadDatetime(timeServiceConfig.getDataSource(),
DatetimeLoadingSQLProviderFactory.getInstance(timeServiceConfig.getStorageType()).getDatetimeLoadingSQL());
} catch (final SQLException ex) {
throw new DatetimeLoadingException(ex);
}
diff --git
a/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfiguration.java
b/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfiguration.java
index b136b582a79..1df03a9a747 100644
---
a/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfiguration.java
+++
b/infra/datetime/type/database/src/main/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfiguration.java
@@ -43,11 +43,11 @@ public final class DatabaseDatetimeServiceConfiguration {
private final DataSource dataSource;
- private final DatabaseType databaseType;
+ private final DatabaseType storageType;
private DatabaseDatetimeServiceConfiguration() {
dataSource = DataSourcePoolCreator.create(new
YamlDataSourceConfigurationSwapper().swapToDataSourceProperties(loadDataSourceConfiguration()));
- databaseType =
DatabaseTypeEngine.getDatabaseType(Collections.singleton(dataSource));
+ storageType =
DatabaseTypeEngine.getStorageType(Collections.singletonList(dataSource));
}
@SuppressWarnings("unchecked")
diff --git
a/infra/datetime/type/database/src/test/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfigurationTest.java
b/infra/datetime/type/database/src/test/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfigurationTest.java
index cd04a6133b3..38636868654 100644
---
a/infra/datetime/type/database/src/test/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfigurationTest.java
+++
b/infra/datetime/type/database/src/test/java/org/apache/shardingsphere/datetime/database/config/DatabaseDatetimeServiceConfigurationTest.java
@@ -28,7 +28,7 @@ public final class DatabaseDatetimeServiceConfigurationTest {
@Test
public void assertGetInstance() {
-
assertThat(DatabaseDatetimeServiceConfiguration.getInstance().getDatabaseType().getType(),
is("H2"));
+
assertThat(DatabaseDatetimeServiceConfiguration.getInstance().getStorageType().getType(),
is("H2"));
assertThat(DatabaseDatetimeServiceConfiguration.getInstance().getDataSource(),
instanceOf(HikariDataSource.class));
}
}
diff --git
a/kernel/single-table/core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
b/kernel/single-table/core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
index 2dadda06963..af8a4afdf4b 100644
---
a/kernel/single-table/core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
+++
b/kernel/single-table/core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
@@ -74,7 +74,7 @@ public final class SingleTableRule implements DatabaseRule,
DataNodeContainedRul
Map<String, DataSource> enabledDataSources =
DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
dataSourceMap);
Map<String, DataSource> aggregateDataSourceMap =
getAggregateDataSourceMap(enabledDataSources, builtRules);
dataSourceNames = aggregateDataSourceMap.keySet();
- singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName,
DatabaseTypeEngine.getDatabaseType(enabledDataSources.values()),
aggregateDataSourceMap, getLoadedTables(builtRules));
+ singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName,
DatabaseTypeEngine.getStorageType(enabledDataSources.values()),
aggregateDataSourceMap, getLoadedTables(builtRules));
tableNames =
singleTableDataNodes.entrySet().stream().collect(Collectors.toConcurrentMap(Entry::getKey,
entry -> entry.getValue().iterator().next().getTableName()));
}
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
index 587ec2545fb..5892af11a71 100644
---
a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
@@ -275,9 +275,9 @@ public final class ContextManagerTest {
@Test
public void assertAlterRuleConfiguration() {
ShardingSphereResourceMetaData resourceMetaData =
mock(ShardingSphereResourceMetaData.class);
- Map<String, ShardingSphereDatabase> databases = new HashMap<>();
- databases.put("foo_db", new ShardingSphereDatabase("foo_db", new
MySQLDatabaseType(), resourceMetaData, mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap()));
-
when(metaDataContexts.getMetaData().getDatabases()).thenReturn(databases);
+
when(resourceMetaData.getDataSources()).thenReturn(Collections.singletonMap("ds_0",
new MockedDataSource()));
+ ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db",
new MySQLDatabaseType(), resourceMetaData,
mock(ShardingSphereRuleMetaData.class), Collections.emptyMap());
+
when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database);
when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(metaDataContexts.getPersistService()).thenReturn(mock(MetaDataPersistService.class,
RETURNS_DEEP_STUBS));
when(metaDataContexts.getMetaData().getActualDatabaseName("foo_db")).thenReturn("foo_db");