Skip to content

Commit

Permalink
Refactor StorageNodeName (#28613)
Browse files Browse the repository at this point in the history
* Rename StorageNodeIdentifier

* Refactor StorageNode

* Refactor StorageNodeName

* Refactor StorageNodeName

* Refactor StorageNodeName
  • Loading branch information
terrymanu authored Sep 28, 2023
1 parent 576e75b commit c693db8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
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

0 comments on commit c693db8

Please sign in to comment.