From c693db840a0e58244ab7c1e0c435689ee07357a4 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 28 Sep 2023 12:21:57 +0800 Subject: [PATCH] Refactor StorageNodeName (#28613) * Rename StorageNodeIdentifier * Refactor StorageNode * Refactor StorageNodeName * Refactor StorageNodeName * Refactor StorageNodeName --- .../database/resource/node/StorageNode.java | 10 +++------- .../resource/node/StorageNodeName.java | 4 ++++ .../unit/StorageUnitNodeMapperUtils.java | 19 ++++++++----------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java index 98e0b0337299e..fa836f44b44f9 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java @@ -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 { @@ -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; - } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java index ec6941710ba83..3ec99ec0e1a63 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeName.java @@ -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); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java index e5c6144e3f92e..2bb78e1275e01 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java @@ -55,7 +55,7 @@ public static Map fromDataSources(final Map 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)); @@ -112,7 +109,7 @@ public static Map getStorageNodeDataS Map 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;