From f7531132ba50424349d212bd13fc2758bc26a773 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 16 Jan 2025 23:46:37 +0800 Subject: [PATCH] Refactor ShardingSphereStatisticsBuilder (#34376) * Refactor ShardingSphereStatisticsBuilder * Refactor ShardingSphereStatisticsBuilder * Refactor ShardingSphereStatisticsBuilder --- ...er.java => DialectStatisticsAppender.java} | 10 ++--- ...lder.java => MySQLStatisticsAppender.java} | 10 ++--- ...java => PostgreSQLStatisticsAppender.java} | 13 +++--- ...tistics.builder.DialectStatisticsAppender} | 4 +- ...QLShardingSphereStatisticsBuilderTest.java | 45 ------------------- ... => PostgreSQLStatisticsAppenderTest.java} | 8 ++-- .../mode/metadata/MetaDataContextManager.java | 2 +- .../ShardingSphereStatisticsFactory.java | 14 +++--- 8 files changed, 30 insertions(+), 76 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/{ShardingSphereStatisticsBuilder.java => DialectStatisticsAppender.java} (81%) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/{MySQLShardingSphereStatisticsBuilder.java => MySQLStatisticsAppender.java} (73%) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/{PostgreSQLShardingSphereStatisticsBuilder.java => PostgreSQLStatisticsAppender.java} (82%) rename infra/common/src/main/resources/META-INF/services/{org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder => org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender} (91%) delete mode 100644 infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilderTest.java rename infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/{PostgreSQLShardingSphereStatisticsBuilderTest.java => PostgreSQLStatisticsAppenderTest.java} (87%) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/DialectStatisticsAppender.java similarity index 81% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsBuilder.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/DialectStatisticsAppender.java index 16cfbd4f6eba5..b67029b39d0cf 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/ShardingSphereStatisticsBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/DialectStatisticsAppender.java @@ -23,16 +23,16 @@ import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; /** - * ShardingSphere statistics builder. + * Dialect statistics appender. */ @SingletonSPI -public interface ShardingSphereStatisticsBuilder extends DatabaseTypedSPI { +public interface DialectStatisticsAppender extends DatabaseTypedSPI { /** - * Build ShardingSphere data. + * Append database data. * + * @param databaseData to be appended database data * @param database database - * @return built ShardingSphere data */ - ShardingSphereDatabaseData build(ShardingSphereDatabase database); + void append(ShardingSphereDatabaseData databaseData, ShardingSphereDatabase database); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLStatisticsAppender.java similarity index 73% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilder.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLStatisticsAppender.java index 825227ce02779..04aaf1081a3cd 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLStatisticsAppender.java @@ -19,17 +19,15 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; +import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender; /** - * ShardingSphere statistics builder for MySQL. + * Statistics appender for MySQL. */ -public final class MySQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder { +public final class MySQLStatisticsAppender implements DialectStatisticsAppender { @Override - public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) { - return new ShardingSphereDefaultStatisticsBuilder().build(database); + public void append(final ShardingSphereDatabaseData databaseData, final ShardingSphereDatabase database) { } @Override diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppender.java similarity index 82% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppender.java index 1b41914687341..333015baa8296 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppender.java @@ -23,8 +23,7 @@ import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; +import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender; import java.util.Arrays; import java.util.Collection; @@ -33,9 +32,9 @@ import java.util.Map.Entry; /** - * ShardingSphere statistics builder for PostgreSQL. + * Statistics appender for PostgreSQL. */ -public final class PostgreSQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder { +public final class PostgreSQLStatisticsAppender implements DialectStatisticsAppender { private static final Map> INIT_DATA_SCHEMA_TABLES = new LinkedHashMap<>(); @@ -44,16 +43,14 @@ public final class PostgreSQLShardingSphereStatisticsBuilder implements Sharding } @Override - public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) { - ShardingSphereDatabaseData result = new ShardingSphereDefaultStatisticsBuilder().build(database); + public void append(final ShardingSphereDatabaseData databaseData, final ShardingSphereDatabase database) { for (Entry> entry : INIT_DATA_SCHEMA_TABLES.entrySet()) { ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData(); if (null != database.getSchema(entry.getKey())) { initTables(database.getSchema(entry.getKey()), entry.getValue(), schemaData); - result.putSchema(entry.getKey(), schemaData); + databaseData.putSchema(entry.getKey(), schemaData); } } - return result; } private void initTables(final ShardingSphereSchema schema, final Collection tables, final ShardingSphereSchemaData schemaData) { diff --git a/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder b/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender similarity index 91% rename from infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder rename to infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender index 32aa5824f8277..4a1ec578b81ff 100644 --- a/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder +++ b/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender @@ -15,5 +15,5 @@ # limitations under the License. # -org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.MySQLShardingSphereStatisticsBuilder -org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.PostgreSQLShardingSphereStatisticsBuilder +org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.MySQLStatisticsAppender +org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.PostgreSQLStatisticsAppender diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilderTest.java deleted file mode 100644 index 71eb24da30b57..0000000000000 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/MySQLShardingSphereStatisticsBuilderTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect; - -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; -import org.junit.jupiter.api.Test; - -class MySQLShardingSphereStatisticsBuilderTest { - - @Test - void assertBuild() { - ShardingSphereDatabase database = mockDatabase(); - ShardingSphereDatabaseData databaseData = new MySQLShardingSphereStatisticsBuilder().build(database); - assertTrue(databaseData.getSchemaData().containsKey("shardingsphere")); - assertTrue(databaseData.getSchemaData().get("shardingsphere").getTableData().containsKey("cluster_information")); - assertTrue(databaseData.getSchemaData().get("shardingsphere").getTableData().containsKey("sharding_table_statistics")); - } - - private ShardingSphereDatabase mockDatabase() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); - when(database.containsSchema("shardingsphere")).thenReturn(true); - return database; - } -} diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppenderTest.java similarity index 87% rename from infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java rename to infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppenderTest.java index 67bccef0de101..bd8d677d51530 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLShardingSphereStatisticsBuilderTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/builder/dialect/PostgreSQLStatisticsAppenderTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; +import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -30,12 +31,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -class PostgreSQLShardingSphereStatisticsBuilderTest { +class PostgreSQLStatisticsAppenderTest { @Test - void assertBuild() { + void assertAppend() { ShardingSphereDatabase database = mockDatabase(); - ShardingSphereDatabaseData databaseData = new PostgreSQLShardingSphereStatisticsBuilder().build(database); + ShardingSphereDatabaseData databaseData = new ShardingSphereDefaultStatisticsBuilder().build(database); + new PostgreSQLStatisticsAppender().append(databaseData, database); assertTrue(databaseData.getSchemaData().containsKey("pg_catalog")); assertTrue(databaseData.getSchemaData().get("pg_catalog").getTableData().containsKey("pg_class")); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index 21ebc96d79aaa..431b2ad3a1c8b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -44,7 +44,7 @@ import java.util.Map; /** - * Meta data context manager.. + * Meta data context manager. */ @Getter @Slf4j diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/ShardingSphereStatisticsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/ShardingSphereStatisticsFactory.java index a5a6dc188cded..6ab5f32b39334 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/ShardingSphereStatisticsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/ShardingSphereStatisticsFactory.java @@ -27,7 +27,8 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; -import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; +import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder; +import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService; @@ -52,20 +53,21 @@ public static ShardingSphereStatistics create(final MetaDataPersistService persi if (metaData.getAllDatabases().isEmpty()) { return new ShardingSphereStatistics(); } - Optional statisticsBuilder = DatabaseTypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, getDatabaseType(metaData)); - if (!statisticsBuilder.isPresent()) { + Optional dialectStatisticsAppender = DatabaseTypedSPILoader.findService(DialectStatisticsAppender.class, getDatabaseType(metaData)); + if (!dialectStatisticsAppender.isPresent()) { return new ShardingSphereStatistics(); } ShardingSphereStatistics loadedStatistics = persistService.getShardingSphereDataPersistService().load(metaData).orElse(new ShardingSphereStatistics()); Collection unloadedDatabases = metaData.getAllDatabases().stream().filter(each -> !loadedStatistics.containsDatabase(each.getName())).collect(Collectors.toList()); - return create(statisticsBuilder.get(), unloadedDatabases, loadedStatistics); + return create(dialectStatisticsAppender.get(), unloadedDatabases, loadedStatistics); } - private static ShardingSphereStatistics create(final ShardingSphereStatisticsBuilder statisticsBuilder, + private static ShardingSphereStatistics create(final DialectStatisticsAppender statisticsBuilder, final Collection unloadedDatabases, final ShardingSphereStatistics loadedStatistics) { ShardingSphereStatistics result = new ShardingSphereStatistics(); for (ShardingSphereDatabase each : unloadedDatabases) { - ShardingSphereDatabaseData databaseData = statisticsBuilder.build(each); + ShardingSphereDatabaseData databaseData = new ShardingSphereDefaultStatisticsBuilder().build(each); + statisticsBuilder.append(databaseData, each); if (!databaseData.getSchemaData().isEmpty()) { result.putDatabase(each.getName(), databaseData); }