From c5dc97cdb1911791d4e4a47fe847dd546a8c8808 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 22 Sep 2024 13:21:02 +0800 Subject: [PATCH] Refactor DataNode.format() (#32955) --- .../query/ShowShardingTableNodesExecutor.java | 9 ++++----- .../shardingsphere/infra/datanode/DataNode.java | 9 --------- ...nsupportedActualDataNodeStructureException.java | 5 ++--- .../database/schema/util/SchemaMetaDataUtils.java | 3 ++- .../infra/datanode/DataNodeTest.java | 14 -------------- 5 files changed, 8 insertions(+), 32 deletions(-) diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java index 69cdef27dbd0d..b344604ca4618 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java @@ -20,7 +20,6 @@ import lombok.Setter; import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware; import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor; -import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableNodesStatement; @@ -49,12 +48,12 @@ public Collection getColumnNames(final ShowShardingTableNodesStatement s public Collection getRows(final ShowShardingTableNodesStatement sqlStatement, final ContextManager contextManager) { String tableName = sqlStatement.getTableName(); return null == tableName - ? rule.getShardingTables().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(entry.getValue()))).collect(Collectors.toList()) - : Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(rule.getShardingTable(tableName)))); + ? rule.getShardingTables().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(sqlStatement, entry.getValue()))).collect(Collectors.toList()) + : Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(sqlStatement, rule.getShardingTable(tableName)))); } - private String getTableNodes(final ShardingTable shardingTable) { - return shardingTable.getActualDataNodes().stream().map(DataNode::format).collect(Collectors.joining(", ")); + private String getTableNodes(final ShowShardingTableNodesStatement sqlStatement, final ShardingTable shardingTable) { + return shardingTable.getActualDataNodes().stream().map(each -> each.format(sqlStatement.getDatabaseType())).collect(Collectors.joining(", ")); } @Override diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java index cf4a2be276df2..008f9fde04003 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java @@ -102,15 +102,6 @@ private boolean isActualDataNodesIncludedDataSourceInstance(final String actualD return isValidDataNode(actualDataNodes, 3); } - /** - * Format data node as string. - * - * @return formatted data node - */ - public String format() { - return dataSourceName + DELIMITER + tableName; - } - /** * Format data node as string. * diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/datanode/UnsupportedActualDataNodeStructureException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/datanode/UnsupportedActualDataNodeStructureException.java index d2bb0835550db..760d0fbcd1de3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/datanode/UnsupportedActualDataNodeStructureException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/datanode/UnsupportedActualDataNodeStructureException.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.infra.exception.kernel.metadata.datanode; -import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState; import java.util.Collection; @@ -29,7 +28,7 @@ public final class UnsupportedActualDataNodeStructureException extends DataNodeD private static final long serialVersionUID = -8921823916974492519L; - public UnsupportedActualDataNodeStructureException(final DataNode dataNode, final Collection jdbcUrlPrefixes) { - super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 1, "Can not support 3-tier structure for actual data node '%s' with JDBC '%s'.", dataNode.format(), jdbcUrlPrefixes); + public UnsupportedActualDataNodeStructureException(final String dataSourceName, final String tableName, final Collection jdbcUrlPrefixes) { + super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 1, "Can not support 3-tier structure for actual data node '%s.%s' with JDBC '%s'.", dataSourceName, tableName, jdbcUrlPrefixes); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtils.java index c292869a1e64d..63d51985a177e 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtils.java @@ -84,7 +84,8 @@ private static void checkDataSourceTypeIncludeInstanceAndSetDatabaseTableMap(fin final String tableName) { for (DataNode dataNode : dataNodes.getDataNodes(tableName)) { ShardingSpherePreconditions.checkState(notSupportThreeTierStructureStorageTypes.isEmpty() || !dataNode.getDataSourceName().contains("."), - () -> new UnsupportedActualDataNodeStructureException(dataNode, notSupportThreeTierStructureStorageTypes.iterator().next().getJdbcUrlPrefixes())); + () -> new UnsupportedActualDataNodeStructureException( + dataNode.getDataSourceName(), dataNode.getTableName(), notSupportThreeTierStructureStorageTypes.iterator().next().getJdbcUrlPrefixes())); if (dataNode.getDataSourceName().contains(".")) { String database = dataNode.getDataSourceName().split("\\.")[1]; GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().put(dataNode.getTableName(), database); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java index a3e03d4af953a..abc6544a16689 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java @@ -80,13 +80,6 @@ void assertEmptyTableDataNode() { assertThrows(InvalidDataNodeFormatException.class, () -> new DataNode("ds_0.")); } - @Test - void assertFormat() { - String expected = "ds_0.tbl_0"; - DataNode dataNode = new DataNode(expected); - assertThat(dataNode.format(), is(expected)); - } - @Test void assertNewValidDataNodeIncludeInstance() { DataNode dataNode = new DataNode("ds_0.db_0.tbl_0"); @@ -103,11 +96,4 @@ void assertHashCodeIncludeInstance() { void assertToStringIncludeInstance() { assertThat(new DataNode("ds_0.db_0.tbl_0").toString(), is("DataNode(dataSourceName=ds_0.db_0, tableName=tbl_0, schemaName=null)")); } - - @Test - void assertFormatIncludeInstance() { - String expected = "ds_0.db_0.tbl_0"; - DataNode dataNode = new DataNode(expected); - assertThat(dataNode.format(), is(expected)); - } }