Skip to content

Commit

Permalink
Refactor ShardingSphereStatisticsCollector (#33884)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereStatisticsRefreshEngine

* Refactor ShardingSphereStatisticsCollector

* Refactor ShardingSphereStatisticsCollector
  • Loading branch information
terrymanu authored Dec 2, 2024
1 parent 25658a1 commit 0406b05
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
Expand All @@ -49,15 +49,14 @@ public final class ShardingStatisticsTableCollector implements ShardingSphereSta
private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

@Override
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table,
final Map<String, ShardingSphereDatabase> databases, final RuleMetaData globalRuleMetaData) throws SQLException {
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
ShardingSphereTableData result = new ShardingSphereTableData(SHARDING_TABLE_STATISTICS);
DatabaseType protocolType = databases.values().iterator().next().getProtocolType();
DatabaseType protocolType = metaData.getAllDatabases().iterator().next().getProtocolType();
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData();
if (dialectDatabaseMetaData.getDefaultSchema().isPresent()) {
collectFromDatabase(databases.get(databaseName), result);
collectFromDatabase(metaData.getDatabase(databaseName), result);
} else {
for (ShardingSphereDatabase each : databases.values()) {
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
collectFromDatabase(each, result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.sharding.metadata.data;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand All @@ -41,6 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -65,7 +68,9 @@ void setUp() {
void assertCollectWithoutShardingRule() throws SQLException {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getProtocolType()).thenReturn(databaseType);
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertFalse(actual.isPresent());
}

Expand All @@ -78,7 +83,9 @@ void assertCollectWithShardingRule() throws SQLException {
storageUnits.put("ds_1", mock(StorageUnit.class, RETURNS_DEEP_STUBS));
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("sharding_table_statistics"));
List<ShardingSphereRowData> actualRows = new ArrayList<>(actual.get().getRows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.sharding.metadata.data.dialect.type;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand All @@ -44,6 +46,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -72,7 +75,9 @@ void assertCollect() throws SQLException {
storageUnits.put("ds_1", mockStorageUnit(mockResultSet()));
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("sharding_table_statistics"));
List<ShardingSphereRowData> actualRows = new ArrayList<>(actual.get().getRows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.sharding.metadata.data.dialect.type;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand All @@ -44,6 +46,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -73,7 +76,9 @@ void assertCollectWithoutExistedTables() throws SQLException {
storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), false));
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("sharding_table_statistics"));
List<ShardingSphereRowData> actualRows = new ArrayList<>(actual.get().getRows());
Expand All @@ -91,7 +96,9 @@ void assertCollectWithExistedTables() throws SQLException {
storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), true));
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("sharding_table_statistics"));
List<ShardingSphereRowData> actualRows = new ArrayList<>(actual.get().getRows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.shardingsphere.sharding.metadata.data.dialect.type;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand All @@ -44,6 +46,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -72,7 +75,9 @@ void assertCollect() throws SQLException {
storageUnits.put("ds_1", mockStorageUnit(mockResultSet()));
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), Collections.singletonMap("foo_db", database), mock(RuleMetaData.class));
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
assertTrue(actual.isPresent());
assertThat(actual.get().getName(), is("sharding_table_statistics"));
List<ShardingSphereRowData> actualRows = new ArrayList<>(actual.get().getRows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

package org.apache.shardingsphere.infra.metadata.statistics.collector;

import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;

import java.sql.SQLException;
import java.util.Map;
import java.util.Optional;

/**
Expand All @@ -39,10 +37,9 @@ public interface ShardingSphereStatisticsCollector extends TypedSPI {
*
* @param databaseName database name
* @param table table
* @param databases databases
* @param globalRuleMetaData global rule meta data
* @param metaData ShardingSphere meta data
* @return ShardingSphere table data
* @throws SQLException SQL exception
*/
Optional<ShardingSphereTableData> collect(String databaseName, ShardingSphereTable table, Map<String, ShardingSphereDatabase> databases, RuleMetaData globalRuleMetaData) throws SQLException;
Optional<ShardingSphereTableData> collect(String databaseName, ShardingSphereTable table, ShardingSphereMetaData metaData) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package org.apache.shardingsphere.infra.metadata.statistics.collector.tables;

import com.cedarsoftware.util.CaseInsensitiveMap;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
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.ShardingSphereRowData;
Expand All @@ -45,10 +44,9 @@ public final class PgClassTableCollector implements ShardingSphereStatisticsColl
private static final Long PUBLIC_SCHEMA_OID = 0L;

@Override
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final Map<String, ShardingSphereDatabase> databases,
final RuleMetaData globalRuleMetaData) throws SQLException {
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
ShardingSphereTableData result = new ShardingSphereTableData(PG_CLASS);
ShardingSphereSchema publicSchema = databases.get(databaseName).getSchema(PUBLIC_SCHEMA);
ShardingSphereSchema publicSchema = metaData.getDatabase(databaseName).getSchema(PUBLIC_SCHEMA);
if (null != publicSchema) {
result.getRows().addAll(collectForSchema(0L, PUBLIC_SCHEMA_OID, publicSchema, table));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package org.apache.shardingsphere.infra.metadata.statistics.collector.tables;

import com.cedarsoftware.util.CaseInsensitiveMap;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
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.ShardingSphereRowData;
Expand All @@ -44,11 +43,10 @@ public final class PgNamespaceTableCollector implements ShardingSphereStatistics
private static final Long PUBLIC_SCHEMA_OID = 0L;

@Override
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final Map<String, ShardingSphereDatabase> databases,
final RuleMetaData globalRuleMetaData) throws SQLException {
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
ShardingSphereTableData result = new ShardingSphereTableData(PG_NAMESPACE);
long oid = 1L;
for (ShardingSphereSchema each : databases.get(databaseName).getAllSchemas()) {
for (ShardingSphereSchema each : metaData.getDatabase(databaseName).getAllSchemas()) {
result.getRows().add(new ShardingSphereRowData(getRow(PUBLIC_SCHEMA.equalsIgnoreCase(each.getName()) ? PUBLIC_SCHEMA_OID : oid++, each.getName(), table)));
}
return Optional.of(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void collectForTable(final String databaseName, final String schemaName,
Optional<ShardingSphereTableData> tableData = Optional.empty();
if (statisticsCollector.isPresent()) {
try {
tableData = statisticsCollector.get().collect(databaseName, table, metaData.getDatabases(), metaData.getGlobalRuleMetaData());
tableData = statisticsCollector.get().collect(databaseName, table, metaData);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

package org.apache.shardingsphere.mode.fixture;

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

/**
Expand All @@ -35,8 +33,7 @@
public final class StatisticsCollectorFixture implements ShardingSphereStatisticsCollector {

@Override
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final Map<String, ShardingSphereDatabase> databases,
final RuleMetaData globalRuleMetaData) throws SQLException {
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
ShardingSphereTableData shardingSphereTableData = new ShardingSphereTableData("test_table");
shardingSphereTableData.getRows().add(new ShardingSphereRowData(Arrays.asList("1", "2")));
return Optional.of(shardingSphereTableData);
Expand Down

0 comments on commit 0406b05

Please sign in to comment.