From 5087e61857f4840d1c1aaa53301a74cd2c2542a6 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 27 Sep 2023 19:17:01 +0800 Subject: [PATCH] Refactor ResourceMetaData --- .../metadata/database/resource/ResourceMetaData.java | 6 +----- .../metadata/database/ShardingSphereDatabaseTest.java | 8 ++++---- .../executor/sql/context/ExecutionContextBuilderTest.java | 4 ++-- .../rule/builder/TransactionRuleBuilderTest.java | 2 +- .../shardingsphere/mode/manager/ContextManagerTest.java | 2 +- .../mode/manager/switcher/ResourceSwitchManagerTest.java | 4 ++-- .../manager/cluster/ClusterModeContextManagerTest.java | 2 +- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index 7cd19ee08f617..e96ce55845dab 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java @@ -48,12 +48,8 @@ public final class ResourceMetaData { private final StorageUnitMetaData storageUnitMetaData; public ResourceMetaData(final Map dataSources) { - this(null, dataSources); - } - - public ResourceMetaData(final String databaseName, final Map dataSources) { dataSourceMap = StorageNodeUtils.getStorageNodeDataSources(dataSources); - storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, dataSources.entrySet().stream() + storageUnitMetaData = new StorageUnitMetaData(null, dataSourceMap, dataSources.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), StorageUnitNodeMapperUtils.fromDataSources(dataSources)); } 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 a96960679cb8d..ccc5c9a84a065 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 @@ -57,28 +57,28 @@ class ShardingSphereDatabaseTest { @Test void assertIsComplete() { - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource())); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); assertTrue(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete()); } @Test void assertIsNotCompleteWithoutRule() { - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource())); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList()); assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete()); } @Test void assertIsNotCompleteWithoutDataSource() { - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap()); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete()); } @Test void assertReloadRules() { - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("ds", new MockedDataSource())); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); Collection rules = new LinkedList<>(); rules.add(mock(MutableDataNodeRule.class, RETURNS_DEEP_STUBS)); rules.add(mock(DataSourceContainedRule.class, RETURNS_DEEP_STUBS)); diff --git a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java index bfbd0fa430353..0c7fdcbe73011 100644 --- a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java +++ b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java @@ -71,7 +71,7 @@ void assertBuildRouteSQLRewriteResult() { Map sqlRewriteUnits = new HashMap<>(2, 1F); sqlRewriteUnits.put(routeUnit1, sqlRewriteUnit1); sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2); - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap()); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabase()); Collection actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class)); @@ -89,7 +89,7 @@ void assertBuildRouteSQLRewriteResultWithEmptyPrimaryKeyMeta() { SQLRewriteUnit sqlRewriteUnit2 = new SQLRewriteUnit("sql2", Collections.singletonList("parameter2")); Map sqlRewriteUnits = new HashMap<>(2, 1F); sqlRewriteUnits.put(routeUnit2, sqlRewriteUnit2); - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.emptyMap()); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, ruleMetaData, buildDatabaseWithoutPrimaryKey()); Collection actual = ExecutionContextBuilder.build(database, new RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class)); diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java index 252f6d72b4d2e..61385c7450192 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java @@ -45,7 +45,7 @@ class TransactionRuleBuilderTest { @Test void assertBuild() { TransactionRuleConfiguration ruleConfig = new TransactionRuleConfiguration("LOCAL", "provider", new Properties()); - ShardingSphereDatabase database = new ShardingSphereDatabase("logic_db", null, new ResourceMetaData("db", createDataSourceMap()), + ShardingSphereDatabase database = new ShardingSphereDatabase("logic_db", null, new ResourceMetaData(createDataSourceMap()), new RuleMetaData(Collections.singletonList(mock(ShardingSphereRule.class))), Collections.singletonMap("test", mock(ShardingSphereSchema.class))); TransactionRule rule = new TransactionRuleBuilder().build(ruleConfig, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ConfigurationProperties.class)); assertNotNull(rule.getConfiguration()); 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 2044d6402ea2b..0247ffeeea7a7 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 @@ -113,7 +113,7 @@ private ShardingSphereDatabase mockDatabase() { @Test void assertGetDataSourceMap() { - ResourceMetaData resourceMetaData = new ResourceMetaData("sharding_db", Collections.singletonMap("foo_ds", new MockedDataSource())); + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("foo_ds", new MockedDataSource())); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java index c279a12f585da..5175a66badd0f 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java @@ -41,7 +41,7 @@ class ResourceSwitchManagerTest { @Test void assertCreate() { Map dataSourceMap = createDataSourceMap(); - SwitchingResource actual = new ResourceSwitchManager().create(new ResourceMetaData("sharding_db", dataSourceMap), createToBeChangedDataSourcePoolPropertiesMap()); + SwitchingResource actual = new ResourceSwitchManager().create(new ResourceMetaData(dataSourceMap), createToBeChangedDataSourcePoolPropertiesMap()); assertNewDataSources(actual); actual.closeStaleDataSources(); assertStaleDataSources(dataSourceMap); @@ -52,7 +52,7 @@ void assertCreateByAlterDataSourcePoolProperties() { Map dataSourceMap = new HashMap<>(3, 1F); dataSourceMap.put("ds_0", new MockedDataSource()); dataSourceMap.put("ds_1", new MockedDataSource()); - SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData("sharding_db", dataSourceMap), Collections.emptyMap()); + SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData(dataSourceMap), Collections.emptyMap()); assertTrue(actual.getNewStorageResource().getDataSourceMap().isEmpty()); assertThat(actual.getStaleStorageResource().getDataSourceMap().size(), is(2)); actual.closeStaleDataSources(); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java index 2c15d64164a89..ff36d8962f2d7 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterModeContextManagerTest.java @@ -366,7 +366,7 @@ void assertAlterRuleConfiguration() { private ShardingSphereMetaData createShardingSphereMetaData() { return new ShardingSphereMetaData(Collections.singletonMap("db", new ShardingSphereDatabase("db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), - new ResourceMetaData("db", Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyMap())), + new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyMap())), mock(ResourceMetaData.class), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties())); }