Skip to content

Commit

Permalink
Remove useless StorageUnit.storageNode (#28636)
Browse files Browse the repository at this point in the history
* Remove useless StorageUnit.storageNode

* Remove useless StorageUnit.storageNode

* Remove useless StorageUnit.storageNode

* Fix test cases
  • Loading branch information
terrymanu authored Oct 4, 2023
1 parent 14d2274 commit 87c78b3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,31 +42,28 @@ public final class StorageUnit {

private final DataSourcePoolProperties dataSourcePoolProperties;

private final StorageNode storageNode;

private final DatabaseType storageType;

private final ConnectionProperties connectionProperties;

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<String, Object> standardProps = DataSourcePoolPropertiesCreator.create(dataSource).getConnectionPropertySynonyms().getStandardProperties();
Map<String, Object> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ void assertAddDatabase() {
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
when(ShardingSphereDatabase.create("foo_db", databaseType, configProps)).thenReturn(database);
Map<String, ShardingSphereDatabase> 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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ private DataNode createDataNode(final String dataSourceName) {
}

private ShardingSphereDatabase mockDatabaseWithMultipleResources() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
Map<String, StorageUnitMetaData> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ private ShardingSphereDatabase createDatabase() {
}

private ResourceMetaData createResourceMetaData() {
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
Map<String, StorageUnitMetaData> 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;
}
Expand All @@ -115,10 +117,12 @@ private ShardingSphereDatabase createAddDatabase() {
}

private ResourceMetaData createAddResourceMetaData() {
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
Map<String, StorageUnitMetaData> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, StorageUnitMetaData> 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;
}
Expand Down Expand Up @@ -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<String, DataSourcePoolProperties> 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")));
Expand All @@ -256,8 +258,10 @@ private ResourceMetaData createOriginalResource() {
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources);
Map<String, StorageUnitMetaData> metaDataMap = new LinkedHashMap<>(2, 1F);
for (Entry<String, StorageNode> 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);
Expand All @@ -271,6 +275,14 @@ private RuleMetaData createOriginalRuleMetaData() {
return result;
}

private Map<String, DataSourcePoolProperties> getToBeAlteredDataSourcePoolPropertiesMap() {
Map<String, DataSourcePoolProperties> 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());
Expand Down Expand Up @@ -307,9 +319,9 @@ void assertReloadTable() {

private Map<String, Object> createProperties(final String username, final String password) {
Map<String, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ private ContextManager mockContextManager(final String feature) {

private Map<String, StorageUnitMetaData> createStorageUnitMetaDataMap() {
Map<String, StorageUnitMetaData> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 87c78b3

Please sign in to comment.