diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java index d6417aaf5a372..8bcd35b05a1d5 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java @@ -24,7 +24,6 @@ import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; -import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; @@ -43,8 +42,6 @@ public final class StorageUnit { private final DataSourcePoolProperties dataSourcePoolProperties; - private final StorageNode storageNode; - private final DatabaseType storageType; private final ConnectionProperties connectionProperties; @@ -52,22 +49,21 @@ public final class StorageUnit { public StorageUnit(final String databaseName, final DataSource dataSource, final DataSourcePoolProperties dataSourcePoolProperties, final StorageNode storageNode) { this.dataSource = new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()); this.dataSourcePoolProperties = dataSourcePoolProperties; - this.storageNode = storageNode; boolean isDataSourceEnabled = !DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, Collections.singletonMap(storageNode.getName().getName(), dataSource)).isEmpty(); storageType = createStorageType(isDataSourceEnabled); - connectionProperties = createConnectionProperties(isDataSourceEnabled); + connectionProperties = createConnectionProperties(isDataSourceEnabled, storageNode); } private DatabaseType createStorageType(final boolean isDataSourceEnabled) { return DatabaseTypeEngine.getStorageType(isDataSourceEnabled ? Collections.singleton(dataSource) : Collections.emptyList()); } - private ConnectionProperties createConnectionProperties(final boolean isDataSourceEnabled) { + private ConnectionProperties createConnectionProperties(final boolean isDataSourceEnabled, final StorageNode storageNode) { if (!isDataSourceEnabled) { return null; } - Map standardProps = DataSourcePoolPropertiesCreator.create(dataSource).getConnectionPropertySynonyms().getStandardProperties(); + Map standardProps = dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties(); ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType); - return parser.parse(standardProps.get("url").toString(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog()); + return parser.parse(standardProps.getOrDefault("url", "").toString(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog()); } } 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 e604ce6411b6e..0f209791126c4 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 @@ -63,8 +63,7 @@ void assertAddDatabase() { ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); when(ShardingSphereDatabase.create("foo_db", databaseType, configProps)).thenReturn(database); Map databases = new HashMap<>(Collections.singletonMap("foo_db", database)); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), - new RuleMetaData(Collections.singleton(globalResourceHeldRule)), configProps); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalResourceHeldRule)), configProps); metaData.addDatabase("foo_db", databaseType, configProps); assertThat(metaData.getDatabases(), is(databases)); verify(globalResourceHeldRule).addResource(database); @@ -90,7 +89,9 @@ private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaD ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); when(result.getName()).thenReturn("foo_db"); when(result.getResourceMetaData()).thenReturn(resourceMetaData); - StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), dataSource); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, dataSource); when(result.getResourceMetaData().getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("foo_db", storageUnitMetaData)); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule))); return result; diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java index cdbe185717246..cecb20d261cd1 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java @@ -147,10 +147,12 @@ private DataNode createDataNode(final String dataSourceName) { } private ShardingSphereDatabase mockDatabaseWithMultipleResources() { - ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); Map metaDataMap = new HashMap<>(2, 1F); - metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); - metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getResourceMetaData().getStorageUnitMetaDataMap()).thenReturn(metaDataMap); when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java index 1a53301b0c970..67fa7b3433c0f 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java @@ -98,10 +98,12 @@ private ShardingSphereDatabase createDatabase() { } private ResourceMetaData createResourceMetaData() { - ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); Map metaDataMap = new HashMap<>(2, 1F); - metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); - metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnitMetaDataMap()).thenReturn(metaDataMap); return result; } @@ -115,10 +117,12 @@ private ShardingSphereDatabase createAddDatabase() { } private ResourceMetaData createAddResourceMetaData() { - ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); Map metaDataMap = new HashMap<>(2, 1F); - metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); - metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + metaDataMap.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + metaDataMap.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnitMetaDataMap()).thenReturn(metaDataMap); return result; } 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 b6a29531c9174..b9329f189fe00 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 @@ -107,8 +107,10 @@ private ShardingSphereDatabase mockDatabase() { StorageUnitMetaData storageUnitMetaData = mock(StorageUnitMetaData.class, RETURNS_DEEP_STUBS); when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(result.getResourceMetaData().getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); Map metaDataMap = Collections.singletonMap("foo_ds", - new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); when(result.getResourceMetaData().getStorageUnitMetaDataMap()).thenReturn(metaDataMap); return result; } @@ -241,8 +243,8 @@ void assertAlterDataSourceConfiguration() { "foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), createOriginalResource(), createOriginalRuleMetaData(), Collections.emptyMap()); when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(originalDatabaseMetaData); when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); - contextManager.getConfigurationContextManager().alterDataSourceUnitsConfiguration("foo_db", - Collections.singletonMap("foo_ds", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test")))); + Map dataSourcePoolPropsMap = getToBeAlteredDataSourcePoolPropertiesMap(); + contextManager.getConfigurationContextManager().alterDataSourceUnitsConfiguration("foo_db", dataSourcePoolPropsMap); assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaDataMap().size(), is(3)); assertAlteredDataSource((MockedDataSource) contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db") .getResourceMetaData().getDataSources().get(new StorageNodeName("foo_ds"))); @@ -256,8 +258,10 @@ private ResourceMetaData createOriginalResource() { Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); Map metaDataMap = new LinkedHashMap<>(2, 1F); for (Entry entry : storageUnitNodeMap.entrySet()) { + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); metaDataMap.put(entry.getKey(), new StorageUnitMetaData( - "foo_db", storageUnitNodeMap.get(entry.getKey()), mock(DataSourcePoolProperties.class), storageNodeDataSourceMap.get(entry.getValue().getName()))); + "foo_db", storageUnitNodeMap.get(entry.getKey()), dataSourcePoolProps, storageNodeDataSourceMap.get(entry.getValue().getName()))); } ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnitMetaDataMap()).thenReturn(metaDataMap); @@ -271,6 +275,14 @@ private RuleMetaData createOriginalRuleMetaData() { return result; } + private Map getToBeAlteredDataSourcePoolPropertiesMap() { + Map result = new HashMap<>(3, 1F); + result.put("foo_ds", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test"))); + result.put("ds_1", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test"))); + result.put("ds_2", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test"))); + return result; + } + @Test void assertAlterGlobalRuleConfiguration() { RuleConfiguration ruleConfig = new TransactionRuleConfiguration("LOCAL", null, new Properties()); @@ -307,9 +319,9 @@ void assertReloadTable() { private Map createProperties(final String username, final String password) { Map result = new HashMap<>(3, 1F); - result.putIfAbsent("url", "jdbc:mock://127.0.0.1/foo_ds"); - result.putIfAbsent("username", username); - result.putIfAbsent("password", password); + result.put("url", "jdbc:mock://127.0.0.1/foo_ds"); + result.put("username", username); + result.put("password", password); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java index 988adfaa10858..a3d5b1bfd40df 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java @@ -120,8 +120,10 @@ private ContextManager mockContextManager(final String feature) { private Map createStorageUnitMetaDataMap() { Map result = new LinkedHashMap<>(2, 1F); - result.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); - result.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + result.put("ds_0", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); + result.put("ds_1", new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource())); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java index 2ddca79406746..8ba32c9e5bb84 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java @@ -85,7 +85,9 @@ class UnregisterStorageUnitBackendHandlerTest { @BeforeEach void setUp() { resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, new MockedDataSource()); when(resourceMetaData.getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); contextManager = mockContextManager(); diff --git a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java index cd4178533cb4a..758a49dd3a947 100644 --- a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java +++ b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java @@ -83,7 +83,9 @@ private ShardingSphereDatabase mockDatabase(final String databaseProductName, fi private ResourceMetaData mockResourceMetaData(final String databaseProductName, final String databaseProductVersion) throws SQLException { ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); DataSource dataSource = createDataSource(databaseProductName, databaseProductVersion); - StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), dataSource); + DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); + when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); + StorageUnitMetaData storageUnitMetaData = new StorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), dataSourcePoolProps, dataSource); when(result.getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); return result; }