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

Refactor StorageNodeName #28613

Merged
merged 6 commits into from
Sep 28, 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 @@ -18,10 +18,12 @@
package org.apache.shardingsphere.infra.metadata.database.resource.node;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Storage node.
*/
@RequiredArgsConstructor
@Getter
public final class StorageNode {

Expand All @@ -31,13 +33,7 @@ public final class StorageNode {

private final String catalog;

public StorageNode(final String name, final String url) {
public StorageNode(final StorageNodeName name, final String url) {
this(name, url, null);
}

public StorageNode(final String name, final String url, final String catalog) {
this.name = new StorageNodeName(name);
this.url = url;
this.catalog = catalog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public final class StorageNodeName {

private final String name;

public StorageNodeName(final String hostname, final int port, final String username) {
name = String.format("%s_%s_%s", hostname, port, username);
}

@Override
public boolean equals(final Object obj) {
return obj instanceof StorageNodeName && ((StorageNodeName) obj).name.equalsIgnoreCase(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static Map<String, StorageUnitNodeMapper> fromDataSources(final Map<Strin
private static StorageUnitNodeMapper fromDataSource(final String storageUnitName, final DataSource dataSource) {
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSource);
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
return new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageUnitName, url));
return new StorageUnitNodeMapper(storageUnitName, new StorageNode(new StorageNodeName(storageUnitName), url));
}

/**
Expand All @@ -77,24 +77,21 @@ private static StorageUnitNodeMapper fromDataSourcePoolProperties(final String s
Map<String, Object> standardProps = props.getConnectionPropertySynonyms().getStandardProperties();
String url = standardProps.get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
String storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable);
StorageNodeName storageNodeName = getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable);
return createStorageUnitNodeMapper(storageNodeName, storageUnitName, url, isInstanceConnectionAvailable);
}

private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
private static StorageNodeName getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
try {
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
return isInstanceConnectionAvailable ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName;
return isInstanceConnectionAvailable ? new StorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : new StorageNodeName(dataSourceName);
} catch (final UnrecognizedDatabaseURLException ex) {
return dataSourceName;
return new StorageNodeName(dataSourceName);
}
}

private static String generateStorageNodeName(final String hostname, final int port, final String username) {
return String.format("%s_%s_%s", hostname, port, username);
}

private static StorageUnitNodeMapper createStorageUnitNodeMapper(final String storageNodeName, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) {
private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNodeName storageNodeName,
final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) {
return isInstanceConnectionAvailable
? new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url, new StandardJdbcUrlParser().parse(url).getDatabase()))
: new StorageUnitNodeMapper(storageUnitName, new StorageNode(storageNodeName, url));
Expand All @@ -112,7 +109,7 @@ public static Map<StorageNodeName, DataSourcePoolProperties> getStorageNodeDataS
Map<String, Object> standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties();
String url = standardProps.get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
StorageNodeName storageNodeName = new StorageNodeName(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable));
StorageNodeName storageNodeName = getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable);
result.putIfAbsent(storageNodeName, entry.getValue());
}
return result;
Expand Down
Loading