Skip to content

Commit

Permalink
Refactor usages of ShardingSphereMetaData.databases (#33871)
Browse files Browse the repository at this point in the history
* Refactor usages of ShardingSphereMetaData.databases

* Refactor usages of ShardingSphereMetaData.databases
  • Loading branch information
terrymanu authored Dec 1, 2024
1 parent 2274dba commit f94cc0f
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginTyp
GaugeMetricFamilyMetricsCollector result = MetricsCollectorRegistry.get(config, pluginType);
result.cleanMetrics();
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
result.addMetric(Collections.singletonList("database_count"), metaDataContexts.getMetaData().getDatabases().size());
result.addMetric(Collections.singletonList("database_count"), metaDataContexts.getMetaData().getAllDatabases().size());
result.addMetric(Collections.singletonList("storage_unit_count"), getStorageUnitCount(metaDataContexts));
return Optional.of(result);
}

private int getStorageUnitCount(final MetaDataContexts metaDataContexts) {
return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnits().size()).reduce(0, Integer::sum);
return metaDataContexts.getMetaData().getAllDatabases().stream().map(each -> each.getResourceMetaData().getStorageUnits().size()).reduce(0, Integer::sum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private ContextManager mockContextManager() {
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class)));
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database));
when(metaData.getAllDatabases()).thenReturn(Collections.singleton(database));
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData);
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts()).thenReturn(metaDataContexts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -68,6 +69,15 @@ public ShardingSphereMetaData(final Map<String, ShardingSphereDatabase> database
temporaryProps = new TemporaryConfigurationProperties(props.getProps());
}

/**
* Get all databases.
*
* @return all databases
*/
public Collection<ShardingSphereDatabase> getAllDatabases() {
return databases.values();
}

/**
* Judge contains database from meta data or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;

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

/**
* ShardingSphere statistics builder for MySQL.
Expand All @@ -46,11 +45,11 @@ public final class MySQLShardingSphereStatisticsBuilder implements ShardingSpher
@Override
public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (Entry<String, ShardingSphereDatabase> entry : metaData.getDatabases().entrySet()) {
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
initSchemas(entry.getValue(), databaseData);
initSchemas(each, databaseData);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(entry.getKey(), databaseData);
result.putDatabase(each.getName(), databaseData);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public final class PostgreSQLShardingSphereStatisticsBuilder implements Sharding
@Override
public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (Entry<String, ShardingSphereDatabase> entry : metaData.getDatabases().entrySet()) {
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
initSchemas(entry.getValue(), databaseData);
initSchemas(each, databaseData);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(entry.getKey(), databaseData);
result.putDatabase(each.getName(), databaseData);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand All @@ -45,17 +45,18 @@ void assertBuild() {

private ShardingSphereMetaData mockMetaData() {
ShardingSphereMetaData result = mock(ShardingSphereMetaData.class);
Map<String, ShardingSphereDatabase> databaseMap = mockDatabaseMap();
when(result.getDatabases()).thenReturn(databaseMap);
Collection<ShardingSphereDatabase> databases = mockDatabases();
when(result.getAllDatabases()).thenReturn(databases);
return result;
}

private Map<String, ShardingSphereDatabase> mockDatabaseMap() {
private Collection<ShardingSphereDatabase> mockDatabases() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("logic_db");
ShardingSphereSchema schema = mockSchema();
when(database.getAllSchemas()).thenReturn(Collections.singleton(schema));
when(database.getSchema("pg_catalog")).thenReturn(schema);
return Collections.singletonMap("logic_db", database);
return Collections.singleton(database);
}

private ShardingSphereSchema mockSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public static ShardingSphereTableData assembleTableData(final ShardingSphereTabl
// TODO move this logic to ShardingSphere statistics
ShardingSphereTableData result = new ShardingSphereTableData(table.getName());
if (EnumerableConstants.PG_DATABASE.equalsIgnoreCase(table.getName())) {
assembleOpenGaussDatabaseData(result, metaData.getDatabases().values());
assembleOpenGaussDatabaseData(result, metaData.getAllDatabases());
} else if (EnumerableConstants.PG_TABLES.equalsIgnoreCase(table.getName())) {
for (ShardingSphereDatabase each : metaData.getDatabases().values()) {
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
assembleOpenGaussTableData(result, each.getAllSchemas());
}
} else if (EnumerableConstants.PG_ROLES.equalsIgnoreCase(table.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void persistTable(final ShardingSphereDatabase database, final String sc
* @return pre-selected database name
*/
public String getPreSelectedDatabaseName() {
return InstanceType.JDBC == computeNodeInstanceContext.getInstance().getMetaData().getType() ? metaDataContexts.get().getMetaData().getDatabases().keySet().iterator().next() : null;
return InstanceType.JDBC == computeNodeInstanceContext.getInstance().getMetaData().getType() ? metaDataContexts.get().getMetaData().getAllDatabases().iterator().next().getName() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void close() {

private Collection<ShardingSphereRule> getAllRules() {
Collection<ShardingSphereRule> result = new LinkedList<>(metaData.getGlobalRuleMetaData().getRules());
metaData.getDatabases().values().stream().map(each -> each.getRuleMetaData().getRules()).forEach(result::addAll);
metaData.getAllDatabases().stream().map(each -> each.getRuleMetaData().getRules()).forEach(result::addAll);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ private static void closeGeneratedDataSources(final String databaseName, final M
}

private static ShardingSphereStatistics initStatistics(final MetaDataPersistService persistService, final ShardingSphereMetaData metaData) {
if (metaData.getDatabases().isEmpty()) {
if (metaData.getAllDatabases().isEmpty()) {
return new ShardingSphereStatistics();
}
DatabaseType protocolType = metaData.getDatabases().values().iterator().next().getProtocolType();
DatabaseType protocolType = metaData.getAllDatabases().iterator().next().getProtocolType();
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(protocolType).getDialectDatabaseMetaData();
// TODO can `protocolType instanceof SchemaSupportedDatabaseType ? "PostgreSQL" : protocolType.getType()` replace to trunk database type?
DatabaseType databaseType = dialectDatabaseMetaData.getDefaultSchema().isPresent() ? TypedSPILoader.getService(DatabaseType.class, "PostgreSQL") : protocolType;
Expand Down Expand Up @@ -238,7 +238,7 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat
}

private static void persistMetaData(final MetaDataContexts metaDataContexts, final MetaDataPersistService persistService) {
metaDataContexts.getMetaData().getDatabases().values().forEach(each -> each.getAllSchemas().forEach(schema -> {
metaDataContexts.getMetaData().getAllDatabases().forEach(each -> each.getAllSchemas().forEach(schema -> {
if (schema.isEmpty()) {
persistService.getDatabaseMetaDataFacade().getSchema().add(each.getName(), schema.getName());
}
Expand All @@ -247,7 +247,7 @@ private static void persistMetaData(final MetaDataContexts metaDataContexts, fin
for (Entry<String, ShardingSphereDatabaseData> databaseDataEntry : metaDataContexts.getStatistics().getDatabaseData().entrySet()) {
for (Entry<String, ShardingSphereSchemaData> schemaDataEntry : databaseDataEntry.getValue().getSchemaData().entrySet()) {
persistService.getShardingSphereDataPersistService().persist(
metaDataContexts.getMetaData().getDatabases().get(databaseDataEntry.getKey().toLowerCase()), schemaDataEntry.getKey(), schemaDataEntry.getValue());
metaDataContexts.getMetaData().getDatabase(databaseDataEntry.getKey()), schemaDataEntry.getKey(), schemaDataEntry.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void setUp() throws SQLException {
ShardingSphereDatabase database = mockDatabase();
when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true);
when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database);
when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database));
when(metaDataContexts.getMetaData().getAllDatabases()).thenReturn(Collections.singleton(database));
when(computeNodeInstanceContext.getInstance()).thenReturn(new ComputeNodeInstance(new ProxyInstanceMetaData("foo_id", 3307), Collections.emptyList()));
when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class));
contextManager = new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class));
Expand Down Expand Up @@ -174,7 +174,9 @@ void assertReloadTableWithDataSourceName() throws SQLException {
@Test
void assertGetPreSelectedDatabaseNameWithJDBC() {
when(computeNodeInstanceContext.getInstance()).thenReturn(new ComputeNodeInstance(new JDBCInstanceMetaData("foo_id", "foo_db"), Collections.emptyList()));
when(metaDataContexts.getMetaData().getDatabases()).thenReturn(Collections.singletonMap("foo_db", mock(ShardingSphereDatabase.class)));
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(database.getName()).thenReturn("foo_db");
when(metaDataContexts.getMetaData().getAllDatabases()).thenReturn(Collections.singleton(database));
assertThat(contextManager.getPreSelectedDatabaseName(), is("foo_db"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void setUp() {
ShardingSphereDatabase database = mockDatabase();
when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true);
when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database);
when(metaDataContexts.getMetaData().getDatabases().values()).thenReturn(Collections.singleton(database));
when(metaDataContexts.getMetaData().getAllDatabases()).thenReturn(Collections.singleton(database));
schemaMetaDataManager = new SchemaMetaDataManager(new AtomicReference<>(metaDataContexts), mock(PersistRepository.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private ShardingSphereMetaData mockMetaData() {
ShardingSphereMetaData result = mock(ShardingSphereMetaData.class);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
when(database.getName()).thenReturn("logic_db");
when(result.getDatabases()).thenReturn(Collections.singletonMap("logic_db", database));
when(result.getAllDatabases()).thenReturn(Collections.singleton(database));
when(result.getDatabase("logic_db")).thenReturn(database);
when(result.containsDatabase("logic_db")).thenReturn(true);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void afterStorageUnitsAltered(final String databaseName, final MetaDataC
.getSchema().alterByRuleAltered(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), each));
Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName))
.ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService()
.persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData)));
.persist(originalMetaDataContexts.getMetaData().getDatabase(databaseName), schemaName, schemaData)));
metaDataPersistService.persistReloadDatabaseByAlter(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
originalMetaDataContexts.getMetaData().getDatabase(databaseName));
}
Expand All @@ -194,7 +194,7 @@ private void afterStorageUnitsDropped(final String databaseName, final MetaDataC
.getSchema().alterByRuleDropped(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), each));
Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName))
.ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService()
.persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData)));
.persist(originalMetaDataContexts.getMetaData().getDatabase(databaseName), schemaName, schemaData)));
metaDataPersistService.persistReloadDatabaseByDrop(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
originalMetaDataContexts.getMetaData().getDatabase(databaseName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ private DatabaseType getProtocolTypeType() {
if (null != configuredDatabaseType) {
return configuredDatabaseType;
}
if (ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().isEmpty()) {
if (ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getAllDatabases().isEmpty()) {
return TypedSPILoader.getService(DatabaseType.class, "MySQL");
}
return ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().values().iterator().next().getProtocolType();
return ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getAllDatabases().iterator().next().getProtocolType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean databaseExists(final String name) {
* @return all database names
*/
public Collection<String> getAllDatabaseNames() {
return contextManager.getMetaDataContexts().getMetaData().getDatabases().values().stream().map(ShardingSphereDatabase::getName).collect(Collectors.toList());
return contextManager.getMetaDataContexts().getMetaData().getAllDatabases().stream().map(ShardingSphereDatabase::getName).collect(Collectors.toList());
}

/**
Expand All @@ -97,7 +97,7 @@ public Optional<InstanceStateContext> getInstanceStateContext() {
* @return database type
*/
public DatabaseType getDatabaseType() {
Collection<ShardingSphereDatabase> databases = contextManager.getMetaDataContexts().getMetaData().getDatabases().values();
Collection<ShardingSphereDatabase> databases = contextManager.getMetaDataContexts().getMetaData().getAllDatabases();
return databases.stream().flatMap(each -> each.getResourceMetaData().getStorageUnits().values().stream()).findFirst().map(StorageUnit::getStorageType)
.orElseGet(DatabaseTypeEngine::getDefaultStorageType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ private String generateExportData(final ShardingSphereMetaData metaData, final E

private Map<String, Collection<ExportedStorageNode>> getAllStorageNodes(final ShardingSphereMetaData metaData) {
Map<String, Collection<ExportedStorageNode>> storageNodes = new LinkedHashMap<>();
metaData.getDatabases().values().forEach(each -> {
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
if (each.getResourceMetaData().getAllInstanceDataSourceNames().isEmpty()) {
return;
continue;
}
storageNodes.putAll(generateDatabaseExportStorageNodesData(each));
});
}
return storageNodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void setUp() {
private ContextManager mockContextManager() {
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
when(metaData.getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS));
when(metaData.getDatabases().values().iterator().next().getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
when(metaData.getAllDatabases().iterator().next().getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
when(metaData.getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)).thenReturn(0);
TransactionRule transactionRule = mock(TransactionRule.class);
when(transactionRule.getDefaultType()).thenReturn(TransactionType.LOCAL);
Expand Down
Loading

0 comments on commit f94cc0f

Please sign in to comment.