Skip to content

Commit

Permalink
Add test cases for SchemaMetaDataManager (#32906)
Browse files Browse the repository at this point in the history
* Add test cases for SchemaMetaDataManager

* Add test cases for SchemaMetaDataManager

* Add test cases for SchemaMetaDataManager
  • Loading branch information
terrymanu authored Sep 16, 2024
1 parent 97f53dc commit a955180
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static Collection<SchemaMetaData> loadByDefault(final MetaDataLoaderMate
for (String each : material.getActualTableNames()) {
TableMetaDataLoader.load(material.getDataSource(), each, material.getStorageType()).ifPresent(tableMetaData::add);
}
return Collections.singletonList(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaData));
return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaData));
}

private static void merge(final Map<String, SchemaMetaData> schemaMetaDataMap, final Collection<SchemaMetaData> addedSchemaMetaDataList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@ public void reloadSchema(final ShardingSphereDatabase database, final String sch
ShardingSphereSchema reloadedSchema = loadSchema(database, schemaName, dataSourceName);
if (reloadedSchema.getTables().isEmpty()) {
database.dropSchema(schemaName);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(database.getName(),
schemaName);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().dropSchema(database.getName(), schemaName);
} else {
database.addSchema(schemaName, reloadedSchema);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService()
.compareAndPersist(database.getName(), schemaName, reloadedSchema);
persistServiceFacade.getMetaDataPersistService().getDatabaseMetaDataService().compareAndPersist(database.getName(), schemaName, reloadedSchema);
}
} catch (final SQLException ex) {
log.error("Reload meta data of database: {} schema: {} with data source: {} failed", database.getName(), schemaName, dataSourceName, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public class MetaDataContextManager {

private final RuleConfigurationPersistDecorateEngine ruleConfigPersistDecorateEngine;

public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext,
final PersistRepository repository) {
public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext, final PersistRepository repository) {
this.metaDataContexts = metaDataContexts;
this.computeNodeInstanceContext = computeNodeInstanceContext;
resourceSwitchManager = new ResourceSwitchManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SchemaMetaDataManager(final AtomicReference<MetaDataContexts> metaDataCon
/**
* Add database.
*
* @param databaseName database name
* @param databaseName to be added database name
*/
public synchronized void addDatabase(final String databaseName) {
if (metaDataContexts.get().getMetaData().containsDatabase(databaseName)) {
Expand All @@ -69,7 +69,7 @@ public synchronized void addDatabase(final String databaseName) {
/**
* Drop database.
*
* @param databaseName database name
* @param databaseName to be dropped database name
*/
public synchronized void dropDatabase(final String databaseName) {
if (!metaDataContexts.get().getMetaData().containsDatabase(databaseName)) {
Expand All @@ -81,8 +81,8 @@ public synchronized void dropDatabase(final String databaseName) {
/**
* Add schema.
*
* @param databaseName database name
* @param schemaName schema name
* @param databaseName to be added database name
* @param schemaName to be added schema name
*/
public synchronized void addSchema(final String databaseName, final String schemaName) {
ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData();
Expand All @@ -97,14 +97,11 @@ public synchronized void addSchema(final String databaseName, final String schem
/**
* Drop schema.
*
* @param databaseName database name
* @param schemaName schema name
* @param databaseName to be dropped database name
* @param schemaName to be dropped schema name
*/
public synchronized void dropSchema(final String databaseName, final String schemaName) {
ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData();
if (!metaData.containsDatabase(databaseName)) {
return;
}
ShardingSphereDatabase database = metaData.getDatabase(databaseName);
if (!database.containsSchema(schemaName)) {
return;
Expand All @@ -118,17 +115,17 @@ public synchronized void dropSchema(final String databaseName, final String sche
*
* @param databaseName database name
* @param schemaName schema name
* @param toBeDeletedTableName to be deleted table name
* @param toBeDeletedViewName to be deleted view name
* @param toBeChangedTable to be changed table
* @param toBeChangedView to be changed view
*/
public synchronized void alterSchema(final String databaseName, final String schemaName, final String toBeDeletedTableName, final String toBeDeletedViewName) {
public synchronized void alterSchema(final String databaseName, final String schemaName, final ShardingSphereTable toBeChangedTable, final ShardingSphereView toBeChangedView) {
ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData();
if (!metaData.containsDatabase(databaseName) || !metaData.getDatabase(databaseName).containsSchema(schemaName)) {
if (!metaData.getDatabase(databaseName).containsSchema(schemaName)) {
return;
}
Optional.ofNullable(toBeDeletedTableName).ifPresent(optional -> dropTable(databaseName, schemaName, optional));
Optional.ofNullable(toBeDeletedViewName).ifPresent(optional -> dropView(databaseName, schemaName, optional));
if (!Strings.isNullOrEmpty(toBeDeletedTableName) || !Strings.isNullOrEmpty(toBeDeletedViewName)) {
Optional.ofNullable(toBeChangedTable).ifPresent(optional -> alterTable(databaseName, schemaName, optional));
Optional.ofNullable(toBeChangedView).ifPresent(optional -> alterView(databaseName, schemaName, optional));
if (null != toBeChangedTable || null != toBeChangedView) {
metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED));
}
}
Expand All @@ -138,33 +135,21 @@ public synchronized void alterSchema(final String databaseName, final String sch
*
* @param databaseName database name
* @param schemaName schema name
* @param toBeChangedTable to be changed table
* @param toBeChangedView to be changed view
* @param toBeDeletedTableName to be deleted table name
* @param toBeDeletedViewName to be deleted view name
*/
public synchronized void alterSchema(final String databaseName, final String schemaName, final ShardingSphereTable toBeChangedTable, final ShardingSphereView toBeChangedView) {
public synchronized void alterSchema(final String databaseName, final String schemaName, final String toBeDeletedTableName, final String toBeDeletedViewName) {
ShardingSphereMetaData metaData = metaDataContexts.get().getMetaData();
if (!metaData.containsDatabase(databaseName) || !metaData.getDatabase(databaseName).containsSchema(schemaName)) {
if (!metaData.getDatabase(databaseName).containsSchema(schemaName)) {
return;
}
Optional.ofNullable(toBeChangedTable).ifPresent(optional -> alterTable(databaseName, schemaName, optional));
Optional.ofNullable(toBeChangedView).ifPresent(optional -> alterView(databaseName, schemaName, optional));
if (null != toBeChangedTable || null != toBeChangedView) {
Optional.ofNullable(toBeDeletedTableName).ifPresent(optional -> dropTable(databaseName, schemaName, optional));
Optional.ofNullable(toBeDeletedViewName).ifPresent(optional -> dropView(databaseName, schemaName, optional));
if (!Strings.isNullOrEmpty(toBeDeletedTableName) || !Strings.isNullOrEmpty(toBeDeletedViewName)) {
metaData.getGlobalRuleMetaData().getRules().forEach(each -> ((GlobalRule) each).refresh(metaData.getDatabases(), GlobalRuleChangedType.SCHEMA_CHANGED));
}
}

private void dropTable(final String databaseName, final String schemaName, final String toBeDeletedTableName) {
metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeTable(toBeDeletedTableName);
metaDataContexts.get().getMetaData().getDatabase(databaseName)
.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedTableName));
}

private void dropView(final String databaseName, final String schemaName, final String toBeDeletedViewName) {
metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeView(toBeDeletedViewName);
metaDataContexts.get().getMetaData().getDatabase(databaseName)
.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedViewName));
}

private void alterTable(final String databaseName, final String schemaName, final ShardingSphereTable beBoChangedTable) {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
if (TableRefreshUtils.isSingleTable(beBoChangedTable.getName(), database)) {
Expand All @@ -180,4 +165,16 @@ private void alterView(final String databaseName, final String schemaName, final
}
database.getSchema(schemaName).putView(beBoChangedView.getName(), beBoChangedView);
}

private void dropTable(final String databaseName, final String schemaName, final String toBeDeletedTableName) {
metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeTable(toBeDeletedTableName);
metaDataContexts.get().getMetaData().getDatabase(databaseName)
.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedTableName));
}

private void dropView(final String databaseName, final String schemaName, final String toBeDeletedViewName) {
metaDataContexts.get().getMetaData().getDatabase(databaseName).getSchema(schemaName).removeView(toBeDeletedViewName);
metaDataContexts.get().getMetaData().getDatabase(databaseName)
.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.remove(schemaName, toBeDeletedViewName));
}
}
Loading

0 comments on commit a955180

Please sign in to comment.