Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move url and catalog from StorageNode to StorageUnit #28649

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@
public final class StorageNode {

private final StorageNodeName name;

private final String url;

private final String catalog;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import lombok.Getter;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser;
import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
Expand All @@ -38,25 +40,32 @@ public final class StorageUnit {

private final StorageNode storageNode;

private final String url;

private final DatabaseType storageType;

private final String catalog;

private final DataSource dataSource;

private final DataSourcePoolProperties dataSourcePoolProperties;

private final DatabaseType storageType;

private final ConnectionProperties connectionProperties;

public StorageUnit(final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) {
this.storageNode = storageNode;
this.dataSource = new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl());
Map<String, Object> standardProps = dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties();
url = standardProps.get("url").toString();
storageType = DatabaseTypeFactory.get(url);
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
catalog = isInstanceConnectionAvailable ? new StandardJdbcUrlParser().parse(url).getDatabase() : null;
this.dataSource = new CatalogSwitchableDataSource(dataSource, catalog, url);
this.dataSourcePoolProperties = dataSourcePoolProperties;
storageType = DatabaseTypeFactory.get(storageNode.getUrl());
connectionProperties = createConnectionProperties(storageNode);
connectionProperties = createConnectionProperties(standardProps);
}

private ConnectionProperties createConnectionProperties(final StorageNode storageNode) {
Map<String, Object> standardProps = dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties();
private ConnectionProperties createConnectionProperties(final Map<String, Object> standardProps) {
ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType);
return parser.parse(storageNode.getUrl(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog());
return parser.parse(url, standardProps.getOrDefault("username", "").toString(), catalog);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
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.metadata.database.resource.node.StorageNodeName;
Expand All @@ -49,14 +48,7 @@ public final class StorageUnitNodeMapUtils {
*/
public static Map<String, StorageNode> fromDataSources(final Map<String, DataSource> dataSources) {
return dataSources.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> fromDataSource(entry.getKey(), entry.getValue()), (oldValue, currentValue) -> currentValue, LinkedHashMap::new));
}

private static StorageNode fromDataSource(final String storageUnitName, final DataSource dataSource) {
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSource);
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
return createStorageNode(new StorageNodeName(storageUnitName), url, isInstanceConnectionAvailable);
.collect(Collectors.toMap(Entry::getKey, entry -> new StorageNode(new StorageNodeName(entry.getKey())), (oldValue, currentValue) -> currentValue, LinkedHashMap::new));
}

/**
Expand All @@ -79,7 +71,7 @@ private static StorageNode fromDataSourcePoolProperties(final String storageUnit
String url = standardProps.get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
StorageNodeName storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable);
return createStorageNode(storageNodeName, url, isInstanceConnectionAvailable);
return new StorageNode(storageNodeName);
}

private static StorageNodeName getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
Expand All @@ -90,8 +82,4 @@ private static StorageNodeName getStorageNodeName(final String dataSourceName, f
return new StorageNodeName(dataSourceName);
}
}

private static StorageNode createStorageNode(final StorageNodeName storageNodeName, final String url, final boolean isInstanceConnectionAvailable) {
return new StorageNode(storageNodeName, url, isInstanceConnectionAvailable ? new StandardJdbcUrlParser().parse(url).getDatabase() : null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,13 @@ void assertDropDatabase() {
verify(globalResourceHeldRule).closeStaleResource("foo_db");
}

private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaData, final DataSource dataSource, final ResourceHeldRule<?> databaseResourceHeldRule) {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getName()).thenReturn("foo_db");
when(result.getResourceMetaData()).thenReturn(resourceMetaData);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
StorageUnit storageUnit = new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_ds", null), dataSourcePoolProps, dataSource);
when(result.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_db", storageUnit));
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule)));
return result;
}

@Test
void assertContainsDatabase() {
ResourceHeldRule<?> globalResourceHeldRule = mock(ResourceHeldRule.class);
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalResourceHeldRule);
Map<String, ShardingSphereDatabase> databases = new HashMap<>(Collections.singletonMap("foo_db", database));
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
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);
assertTrue(metaData.containsDatabase("foo_db"));
}

Expand All @@ -115,8 +102,19 @@ void assertGetDatabase() {
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalResourceHeldRule);
Map<String, ShardingSphereDatabase> databases = new HashMap<>(Collections.singletonMap("foo_db", database));
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
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);
assertThat(metaData.getDatabase("foo_db"), is(database));
}

private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaData, final DataSource dataSource, final ResourceHeldRule<?> databaseResourceHeldRule) {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getName()).thenReturn("foo_db");
when(result.getResourceMetaData()).thenReturn(resourceMetaData);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/foo_ds"));
StorageUnit storageUnit = new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps, dataSource);
when(result.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_db", storageUnit));
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule)));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ private DataNode createDataNode(final String dataSourceName) {

private ShardingSphereDatabase mockDatabaseWithMultipleResources() {
Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource()));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps0 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps0.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_0"));
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps0, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps1 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps1.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_1"));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps1, new MockedDataSource()));
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits);
when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ private ShardingSphereDatabase createDatabase() {

private ResourceMetaData createResourceMetaData() {
Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource()));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps0 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps0.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_0"));
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps0, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps1 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps1.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_1"));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps1, new MockedDataSource()));
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
when(result.getStorageUnits()).thenReturn(storageUnits);
return result;
Expand All @@ -119,10 +121,12 @@ private ShardingSphereDatabase createAddDatabase() {

private ResourceMetaData createAddResourceMetaData() {
Map<String, StorageUnit> storageUnits = new HashMap<>(2, 1F);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource()));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps0 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps0.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_0"));
storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps0, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps1 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps1.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_1"));
storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps1, new MockedDataSource()));
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
when(result.getStorageUnits()).thenReturn(storageUnits);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ private ShardingSphereDatabase mockDatabase() {
when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
when(result.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit));
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
Map<String, StorageUnit> storageUnits = Collections.singletonMap("foo_ds",
new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db", null), dataSourcePoolProps, new MockedDataSource()));
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/foo_db"));
Map<String, StorageUnit> storageUnits = Collections.singletonMap("foo_ds", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps, new MockedDataSource()));
when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits);
return result;
}
Expand Down Expand Up @@ -259,7 +258,7 @@ private ResourceMetaData createOriginalResource() {
Map<String, StorageUnit> storageUnits = 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());
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/foo_db"));
storageUnits.put(entry.getKey(), new StorageUnit(storageUnitNodeMap.get(entry.getKey()), dataSourcePoolProps, storageNodeDataSourceMap.get(entry.getValue().getName())));
}
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ private ContextManager mockContextManager(final String feature) {

private Map<String, StorageUnit> createStorageUnits() {
Map<String, StorageUnit> result = new LinkedHashMap<>(2, 1F);
DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap());
result.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource()));
result.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps0 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps0.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_0"));
result.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps0, new MockedDataSource()));
DataSourcePoolProperties dataSourcePoolProps1 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS);
when(dataSourcePoolProps1.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.singletonMap("url", "jdbc:mock://127.0.0.1/ds_1"));
result.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class)), dataSourcePoolProps1, new MockedDataSource()));
return result;
}

Expand Down
Loading