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

Add drop schema refresher and adjust param name. #17112

Merged
merged 3 commits into from
Apr 26, 2022
Merged
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 @@ -18,19 +18,16 @@
package org.apache.shardingsphere.infra.metadata.schema.event;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Add schema event.
*/
@RequiredArgsConstructor
@Getter
public final class AddSchemaEvent {

private final String databaseName;

private final String schemaName;

public AddSchemaEvent(final String databaseName, final String schemaName) {
this.databaseName = databaseName;
this.schemaName = schemaName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.schema.event;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Collection;

/**
* Drop schema event.
*/
@RequiredArgsConstructor
@Getter
public final class DropSchemaEvent {

private final String databaseName;

private final Collection<String> schemaNames;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class MetaDataRefreshEngine {

private static final Set<Class<? extends SQLStatement>> IGNORABLE_SQL_STATEMENT_CLASSES = Collections.newSetFromMap(new ConcurrentHashMap<>());

private final ShardingSphereMetaData schemaMetaData;
private final ShardingSphereMetaData metaData;

private final FederationDatabaseMetaData federationMetaData;

Expand All @@ -74,8 +74,8 @@ public void refresh(final SQLStatementContext<?> sqlStatementContext, final Supp
}
Optional<MetaDataRefresher> schemaRefresher = TypedSPIRegistry.findRegisteredService(MetaDataRefresher.class, sqlStatementClass.getSuperclass().getName());
if (schemaRefresher.isPresent()) {
String schemaName = sqlStatementContext.getTablesContext().getSchemaName().orElse(getSchemaName(sqlStatementContext.getDatabaseType(), schemaMetaData.getDatabaseName()));
schemaRefresher.get().refresh(schemaMetaData, federationMetaData, optimizerPlanners, logicDataSourceNamesSupplier.get(), schemaName, sqlStatementContext.getSqlStatement(), props);
String schemaName = sqlStatementContext.getTablesContext().getSchemaName().orElse(getSchemaName(sqlStatementContext.getDatabaseType(), metaData.getDatabaseName()));
schemaRefresher.get().refresh(metaData, federationMetaData, optimizerPlanners, logicDataSourceNamesSupplier.get(), schemaName, sqlStatementContext.getSqlStatement(), props);
} else {
IGNORABLE_SQL_STATEMENT_CLASSES.add(sqlStatementClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface MetaDataRefresher<T extends SQLStatement> extends StatelessType
/**
* Refresh ShardingSphere schema.
*
* @param schemaMetaData schema meta data
* @param metaData meta data
* @param database federation database meta data
* @param optimizerPlanners optimizer planners
* @param logicDataSourceNames route data source names
Expand All @@ -47,6 +47,6 @@ public interface MetaDataRefresher<T extends SQLStatement> extends StatelessType
* @param props configuration properties
* @throws SQLException SQL exception
*/
void refresh(ShardingSphereMetaData schemaMetaData, FederationDatabaseMetaData database, Map<String, OptimizerPlannerContext> optimizerPlanners,
void refresh(ShardingSphereMetaData metaData, FederationDatabaseMetaData database, Map<String, OptimizerPlannerContext> optimizerPlanners,
Collection<String> logicDataSourceNames, String schemaName, T sqlStatement, ConfigurationProperties props) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public final class AlterIndexStatementSchemaRefresher implements MetaDataRefresh
private static final String TYPE = AlterIndexStatement.class.getName();

@Override
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
public void refresh(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
final Collection<String> logicDataSourceNames, final String schemaName, final AlterIndexStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
Optional<IndexSegment> renameIndex = AlterIndexStatementHandler.getRenameIndexSegment(sqlStatement);
if (!sqlStatement.getIndex().isPresent() || !renameIndex.isPresent()) {
return;
}
String indexName = sqlStatement.getIndex().get().getIdentifier().getValue();
Optional<String> logicTableName = findLogicTableName(schemaMetaData.getSchemaByName(schemaName), indexName);
Optional<String> logicTableName = findLogicTableName(metaData.getSchemaByName(schemaName), indexName);
if (logicTableName.isPresent()) {
TableMetaData tableMetaData = schemaMetaData.getSchemaByName(schemaName).get(logicTableName.get());
TableMetaData tableMetaData = metaData.getSchemaByName(schemaName).get(logicTableName.get());
Preconditions.checkNotNull(tableMetaData, "Can not get the table '%s' metadata!", logicTableName.get());
tableMetaData.getIndexes().remove(indexName);
String renameIndexName = renameIndex.get().getIdentifier().getValue();
tableMetaData.getIndexes().put(renameIndexName, new IndexMetaData(renameIndexName));
SchemaAlteredEvent event = new SchemaAlteredEvent(schemaMetaData.getDatabaseName(), schemaName);
SchemaAlteredEvent event = new SchemaAlteredEvent(metaData.getDatabaseName(), schemaName);
event.getAlteredTables().add(tableMetaData);
ShardingSphereEventBus.getInstance().post(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,49 +47,49 @@ public final class AlterTableStatementSchemaRefresher implements MetaDataRefresh
private static final String TYPE = AlterTableStatement.class.getName();

@Override
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
public void refresh(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
final Collection<String> logicDataSourceNames, final String schemaName, final AlterTableStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
SchemaAlteredEvent event = new SchemaAlteredEvent(schemaMetaData.getDatabaseName(), schemaName);
SchemaAlteredEvent event = new SchemaAlteredEvent(metaData.getDatabaseName(), schemaName);
if (sqlStatement.getRenameTable().isPresent()) {
String renameTable = sqlStatement.getRenameTable().get().getTableName().getIdentifier().getValue();
putTableMetaData(schemaMetaData, database, optimizerPlanners, logicDataSourceNames, schemaName, renameTable, props);
removeTableMetaData(schemaMetaData, database, optimizerPlanners, tableName);
event.getAlteredTables().add(schemaMetaData.getDefaultSchema().get(renameTable));
putTableMetaData(metaData, database, optimizerPlanners, logicDataSourceNames, schemaName, renameTable, props);
removeTableMetaData(metaData, database, optimizerPlanners, schemaName, tableName);
event.getAlteredTables().add(metaData.getDefaultSchema().get(renameTable));
event.getDroppedTables().add(tableName);
} else {
putTableMetaData(schemaMetaData, database, optimizerPlanners, logicDataSourceNames, schemaName, tableName, props);
event.getAlteredTables().add(schemaMetaData.getDefaultSchema().get(tableName));
putTableMetaData(metaData, database, optimizerPlanners, logicDataSourceNames, schemaName, tableName, props);
event.getAlteredTables().add(metaData.getDefaultSchema().get(tableName));
}
ShardingSphereEventBus.getInstance().post(event);
}

private void removeTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database,
final Map<String, OptimizerPlannerContext> optimizerPlanners, final String tableName) {
schemaMetaData.getDefaultSchema().remove(tableName);
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.remove(tableName));
database.remove(tableName);
private void removeTableMetaData(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database,
final Map<String, OptimizerPlannerContext> optimizerPlanners, final String schemaName, final String tableName) {
metaData.getSchemaByName(schemaName).remove(tableName);
metaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.remove(tableName));
database.remove(schemaName, tableName);
optimizerPlanners.put(database.getName(), OptimizerPlannerContextFactory.create(database));
}

private void putTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
private void putTableMetaData(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
final Collection<String> logicDataSourceNames, final String schemaName, final String tableName, final ConfigurationProperties props) throws SQLException {
if (!containsInDataNodeContainedRule(tableName, schemaMetaData)) {
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.put(tableName, logicDataSourceNames.iterator().next()));
if (!containsInDataNodeContainedRule(tableName, metaData)) {
metaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.put(tableName, logicDataSourceNames.iterator().next()));
}
SchemaBuilderMaterials materials = new SchemaBuilderMaterials(
schemaMetaData.getResource().getDatabaseType(), schemaMetaData.getResource().getDataSources(), schemaMetaData.getRuleMetaData().getRules(), props, schemaName);
Map<String, SchemaMetaData> schemaMetaDataMap = TableMetaDataBuilder.load(Collections.singletonList(tableName), materials);
Optional<TableMetaData> actualTableMetaData = Optional.ofNullable(schemaMetaDataMap.get(schemaName)).map(optional -> optional.getTables().get(tableName));
metaData.getResource().getDatabaseType(), metaData.getResource().getDataSources(), metaData.getRuleMetaData().getRules(), props, schemaName);
Map<String, SchemaMetaData> metaDataMap = TableMetaDataBuilder.load(Collections.singletonList(tableName), materials);
Optional<TableMetaData> actualTableMetaData = Optional.ofNullable(metaDataMap.get(schemaName)).map(optional -> optional.getTables().get(tableName));
actualTableMetaData.ifPresent(tableMetaData -> {
schemaMetaData.getSchemaByName(schemaName).put(tableName, tableMetaData);
database.put(tableMetaData);
metaData.getSchemaByName(schemaName).put(tableName, tableMetaData);
database.put(schemaName, tableMetaData);
optimizerPlanners.put(database.getName(), OptimizerPlannerContextFactory.create(database));
});
}

private boolean containsInDataNodeContainedRule(final String tableName, final ShardingSphereMetaData schemaMetaData) {
return schemaMetaData.getRuleMetaData().findRules(DataNodeContainedRule.class).stream().anyMatch(each -> each.getAllTables().contains(tableName));
private boolean containsInDataNodeContainedRule(final String tableName, final ShardingSphereMetaData metaData) {
return metaData.getRuleMetaData().findRules(DataNodeContainedRule.class).stream().anyMatch(each -> each.getAllTables().contains(tableName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,50 @@ public final class AlterViewStatementSchemaRefresher implements MetaDataRefreshe
private static final String TYPE = AlterViewStatement.class.getName();

@Override
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
public void refresh(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
final Collection<String> logicDataSourceNames, final String schemaName, final AlterViewStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
String viewName = sqlStatement.getView().getTableName().getIdentifier().getValue();
SchemaAlteredEvent event = new SchemaAlteredEvent(schemaMetaData.getDatabaseName(), schemaName);
SchemaAlteredEvent event = new SchemaAlteredEvent(metaData.getDatabaseName(), schemaName);
Optional<SimpleTableSegment> renameView = AlterViewStatementHandler.getRenameView(sqlStatement);
if (renameView.isPresent()) {
String renameViewName = renameView.get().getTableName().getIdentifier().getValue();
putTableMetaData(schemaMetaData, database, optimizerPlanners, logicDataSourceNames, schemaName, renameViewName, props);
removeTableMetaData(schemaMetaData, database, optimizerPlanners, schemaName, viewName);
event.getAlteredTables().add(schemaMetaData.getSchemaByName(schemaName).get(renameViewName));
putTableMetaData(metaData, database, optimizerPlanners, logicDataSourceNames, schemaName, renameViewName, props);
removeTableMetaData(metaData, database, optimizerPlanners, schemaName, viewName);
event.getAlteredTables().add(metaData.getSchemaByName(schemaName).get(renameViewName));
event.getDroppedTables().add(viewName);
} else {
putTableMetaData(schemaMetaData, database, optimizerPlanners, logicDataSourceNames, schemaName, viewName, props);
event.getAlteredTables().add(schemaMetaData.getDefaultSchema().get(viewName));
putTableMetaData(metaData, database, optimizerPlanners, logicDataSourceNames, schemaName, viewName, props);
event.getAlteredTables().add(metaData.getDefaultSchema().get(viewName));
}
ShardingSphereEventBus.getInstance().post(event);
}

private void removeTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database,
private void removeTableMetaData(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database,
final Map<String, OptimizerPlannerContext> optimizerPlanners, final String schemaName, final String viewName) {
schemaMetaData.getSchemaByName(schemaName).remove(viewName);
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.remove(viewName));
database.remove(viewName);
metaData.getSchemaByName(schemaName).remove(viewName);
metaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.remove(viewName));
database.remove(schemaName, viewName);
optimizerPlanners.put(database.getName(), OptimizerPlannerContextFactory.create(database));
}

private void putTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
private void putTableMetaData(final ShardingSphereMetaData metaData, final FederationDatabaseMetaData database, final Map<String, OptimizerPlannerContext> optimizerPlanners,
final Collection<String> logicDataSourceNames, final String schemaName, final String viewName, final ConfigurationProperties props) throws SQLException {
if (!containsInDataNodeContainedRule(viewName, schemaMetaData)) {
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.put(viewName, logicDataSourceNames.iterator().next()));
if (!containsInDataNodeContainedRule(viewName, metaData)) {
metaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.put(viewName, logicDataSourceNames.iterator().next()));
}
SchemaBuilderMaterials materials = new SchemaBuilderMaterials(
schemaMetaData.getResource().getDatabaseType(), schemaMetaData.getResource().getDataSources(), schemaMetaData.getRuleMetaData().getRules(), props, schemaName);
Map<String, SchemaMetaData> schemaMetaDataMap = TableMetaDataBuilder.load(Collections.singletonList(viewName), materials);
Optional<TableMetaData> actualViewMetaData = Optional.ofNullable(schemaMetaDataMap.get(schemaName)).map(optional -> optional.getTables().get(viewName));
metaData.getResource().getDatabaseType(), metaData.getResource().getDataSources(), metaData.getRuleMetaData().getRules(), props, schemaName);
Map<String, SchemaMetaData> metaDataMap = TableMetaDataBuilder.load(Collections.singletonList(viewName), materials);
Optional<TableMetaData> actualViewMetaData = Optional.ofNullable(metaDataMap.get(schemaName)).map(optional -> optional.getTables().get(viewName));
actualViewMetaData.ifPresent(viewMetaData -> {
schemaMetaData.getSchemaByName(schemaName).put(viewName, viewMetaData);
metaData.getSchemaByName(schemaName).put(viewName, viewMetaData);
database.put(schemaName, viewMetaData);
optimizerPlanners.put(database.getName(), OptimizerPlannerContextFactory.create(database));
});
}

private boolean containsInDataNodeContainedRule(final String viewName, final ShardingSphereMetaData schemaMetaData) {
return schemaMetaData.getRuleMetaData().findRules(DataNodeContainedRule.class).stream().anyMatch(each -> each.getAllTables().contains(viewName));
private boolean containsInDataNodeContainedRule(final String viewName, final ShardingSphereMetaData metaData) {
return metaData.getRuleMetaData().findRules(DataNodeContainedRule.class).stream().anyMatch(each -> each.getAllTables().contains(viewName));
}

@Override
Expand Down
Loading