Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShardingSphereStatisticsCollector #34428

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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 org.apache.shardingsphere.infra.metadata.statistics.collector.dialect.shardingsphere.ShardingSphereTableStatisticsCollector;
import org.apache.shardingsphere.infra.rule.attribute.datasource.aggregate.AggregatedDataSourceRuleAttribute;
import org.apache.shardingsphere.sharding.metadata.data.dialect.DialectShardingStatisticsTableCollector;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
Expand All @@ -47,12 +47,13 @@
/**
* Sharding statistics table data collector.
*/
public final class ShardingStatisticsTableCollector implements ShardingSphereStatisticsCollector {
public final class ShardingStatisticsTableCollector implements ShardingSphereTableStatisticsCollector {

private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

@Override
public Optional<ShardingSphereTableData> collect(final String databaseName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
public Optional<ShardingSphereTableData> collect(final String databaseName, final String schemaName,
final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
ShardingSphereTableData result = new ShardingSphereTableData(SHARDING_TABLE_STATISTICS);
DatabaseType protocolType = metaData.getAllDatabases().iterator().next().getProtocolType();
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData();
Expand Down Expand Up @@ -123,6 +124,6 @@ private void addTableRowsAndDataLength(final DatabaseType databaseType, final Da

@Override
public String getType() {
return SHARDING_TABLE_STATISTICS;
return "shardingsphere.sharding_table_statistics";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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 org.apache.shardingsphere.infra.metadata.statistics.collector.dialect.shardingsphere.ShardingSphereTableStatisticsCollector;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
Expand Down Expand Up @@ -57,11 +57,11 @@ class ShardingStatisticsTableCollectorTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

private ShardingSphereStatisticsCollector statisticsCollector;
private ShardingSphereTableStatisticsCollector shardingSphereTableStatisticsCollector;

@BeforeEach
void setUp() {
statisticsCollector = TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, "sharding_table_statistics");
shardingSphereTableStatisticsCollector = TypedSPILoader.getService(ShardingSphereTableStatisticsCollector.class, "shardingsphere.sharding_table_statistics");
}

@Test
Expand All @@ -71,7 +71,7 @@ void assertCollectWithoutShardingRule() throws SQLException {
when(database.getProtocolType()).thenReturn(databaseType);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "foo_db", mock(ShardingSphereTable.class), metaData);
assertFalse(actual.isPresent());
}

Expand All @@ -85,7 +85,7 @@ void assertCollectWithShardingRule() throws SQLException {
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "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 @@ -27,7 +27,7 @@
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 org.apache.shardingsphere.infra.metadata.statistics.collector.dialect.shardingsphere.ShardingSphereTableStatisticsCollector;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
Expand Down Expand Up @@ -59,11 +59,11 @@ class MySQLShardingStatisticsTableCollectorTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL");

private ShardingSphereStatisticsCollector statisticsCollector;
private ShardingSphereTableStatisticsCollector shardingSphereTableStatisticsCollector;

@BeforeEach
void setUp() {
statisticsCollector = TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, "sharding_table_statistics");
shardingSphereTableStatisticsCollector = TypedSPILoader.getService(ShardingSphereTableStatisticsCollector.class, "shardingsphere.sharding_table_statistics");
}

@Test
Expand All @@ -76,7 +76,7 @@ void assertCollect() throws SQLException {
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "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 @@ -27,7 +27,7 @@
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 org.apache.shardingsphere.infra.metadata.statistics.collector.dialect.shardingsphere.ShardingSphereTableStatisticsCollector;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
Expand Down Expand Up @@ -60,11 +60,11 @@ class OpenGaussShardingStatisticsTableCollectorTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "openGauss");

private ShardingSphereStatisticsCollector statisticsCollector;
private ShardingSphereTableStatisticsCollector shardingSphereTableStatisticsCollector;

@BeforeEach
void setUp() {
statisticsCollector = TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, "sharding_table_statistics");
shardingSphereTableStatisticsCollector = TypedSPILoader.getService(ShardingSphereTableStatisticsCollector.class, "shardingsphere.sharding_table_statistics");
}

@Test
Expand All @@ -77,7 +77,7 @@ void assertCollectWithoutExistedTables() throws SQLException {
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "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 @@ -96,7 +96,7 @@ void assertCollectWithExistedTables() throws SQLException {
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "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 @@ -27,7 +27,7 @@
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 org.apache.shardingsphere.infra.metadata.statistics.collector.dialect.shardingsphere.ShardingSphereTableStatisticsCollector;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
Expand Down Expand Up @@ -59,11 +59,11 @@ class PostgreSQLShardingStatisticsTableCollectorTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");

private ShardingSphereStatisticsCollector statisticsCollector;
private ShardingSphereTableStatisticsCollector shardingSphereTableStatisticsCollector;

@BeforeEach
void setUp() {
statisticsCollector = TypedSPILoader.getService(ShardingSphereStatisticsCollector.class, "sharding_table_statistics");
shardingSphereTableStatisticsCollector = TypedSPILoader.getService(ShardingSphereTableStatisticsCollector.class, "shardingsphere.sharding_table_statistics");
}

@Test
Expand All @@ -76,7 +76,7 @@ void assertCollect() throws SQLException {
ShardingSphereDatabase database = new ShardingSphereDatabase(
"foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
Optional<ShardingSphereTableData> actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData);
Optional<ShardingSphereTableData> actual = shardingSphereTableStatisticsCollector.collect("foo_db", "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 @@ -20,6 +20,7 @@
import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.Getter;

import java.util.Collections;
import java.util.Map;

/**
Expand All @@ -28,7 +29,7 @@
@Getter
public final class ShardingSphereDatabaseData {

private final Map<String, ShardingSphereSchemaData> schemaData = new CaseInsensitiveMap<>();
private final Map<String, ShardingSphereSchemaData> schemaData = Collections.synchronizedMap(new CaseInsensitiveMap<>());

/**
* Judge whether to contains schema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.Getter;

import java.util.Collections;
import java.util.Map;

/**
Expand All @@ -28,7 +29,7 @@
@Getter
public final class ShardingSphereSchemaData {

private final Map<String, ShardingSphereTableData> tableData = new CaseInsensitiveMap<>();
private final Map<String, ShardingSphereTableData> tableData = Collections.synchronizedMap(new CaseInsensitiveMap<>());

/**
* Get ShardingSphere table meta data via table name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.Getter;

import java.util.Collections;
import java.util.Map;

/**
Expand All @@ -28,7 +29,7 @@
@Getter
public final class ShardingSphereStatistics {

private final Map<String, ShardingSphereDatabaseData> databaseData = new CaseInsensitiveMap<>();
private final Map<String, ShardingSphereDatabaseData> databaseData = Collections.synchronizedMap(new CaseInsensitiveMap<>());

/**
* Get ShardingSphere database.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.collector;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
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.type.typed.TypedSPILoader;

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

/**
* Statistics collect engine.
*/
@RequiredArgsConstructor
public final class StatisticsCollectEngine {

private final DatabaseType databaseType;

/**
* Collect.
*
* @param databaseName database name
* @param schemaName schema name
* @param table shardingsphere table
* @param metaData shardingsphere metadata
* @return shardingsphere table data
* @throws SQLException SQL exception
*/
public Optional<ShardingSphereTableData> collect(final String databaseName, final String schemaName, final ShardingSphereTable table, final ShardingSphereMetaData metaData) throws SQLException {
Optional<StatisticsCollector> statisticsCollector = TypedSPILoader.findService(StatisticsCollector.class, databaseType.getType());
return statisticsCollector.isPresent() ? statisticsCollector.get().collect(databaseName, schemaName, table, metaData) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
* ShardingSphere statistics collector.
*/
@SingletonSPI
public interface ShardingSphereStatisticsCollector extends TypedSPI {
public interface StatisticsCollector extends TypedSPI {

/**
* Collect statistics.
*
* @param databaseName database name
* @param table table
* @param schemaName schema name
* @param table shardingsphere table
* @param metaData ShardingSphere meta data
* @return ShardingSphere table data
* @throws SQLException SQL exception
*/
Optional<ShardingSphereTableData> collect(String databaseName, ShardingSphereTable table, ShardingSphereMetaData metaData) throws SQLException;
Optional<ShardingSphereTableData> collect(String databaseName, String schemaName, ShardingSphereTable table, ShardingSphereMetaData metaData) throws SQLException;
}
Loading
Loading