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

Throws SQL Exception for alter and drop rule configuration #32154

Merged
merged 1 commit into from
Jul 18, 2024
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 @@ -66,7 +66,7 @@ public void executeUpdate() throws SQLException {
}

@SuppressWarnings("rawtypes")
private void executeRuleDefinitionUpdate() {
private void executeRuleDefinitionUpdate() throws SQLException {
Optional<DatabaseRuleDefinitionExecutor> databaseExecutor = TypedSPILoader.findService(DatabaseRuleDefinitionExecutor.class, sqlStatement.getClass());
if (databaseExecutor.isPresent()) {
new DatabaseRuleDefinitionExecuteEngine((DatabaseRuleDefinitionStatement) sqlStatement, contextManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.mode.manager.ContextManager;

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

/**
Expand All @@ -46,9 +47,10 @@ public final class DatabaseRuleDefinitionExecuteEngine {

/**
* Execute update.
* @throws SQLException SQL Exception
*/
@SuppressWarnings("unchecked")
public void executeUpdate() {
public void executeUpdate() throws SQLException {
executor.setDatabase(database);
Optional<ShardingSphereRule> rule = database.getRuleMetaData().findSingleRule(executor.getRuleClass());
executor.setRule(rule.orElse(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;

import java.sql.SQLException;
import java.util.Collection;

/**
Expand All @@ -36,6 +37,7 @@ public interface DatabaseRuleOperator {
* @param database database
* @param currentRuleConfig current rule configuration
* @return meta data versions
* @throws SQLException SQL Exception
*/
Collection<MetaDataVersion> operate(DatabaseRuleDefinitionStatement sqlStatement, ShardingSphereDatabase database, RuleConfiguration currentRuleConfig);
Collection<MetaDataVersion> operate(DatabaseRuleDefinitionStatement sqlStatement, ShardingSphereDatabase database, RuleConfiguration currentRuleConfig) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
Expand All @@ -48,7 +49,8 @@ public final class AlterDatabaseRuleOperator implements DatabaseRuleOperator {

@Override
@SuppressWarnings("unchecked")
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) {
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database,
final RuleConfiguration currentRuleConfig) throws SQLException {
RuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
RuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(toBeAlteredRuleConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;

import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
Expand All @@ -48,7 +49,8 @@ public final class CreateDatabaseRuleOperator implements DatabaseRuleOperator {

@Override
@SuppressWarnings("unchecked")
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) {
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database,
final RuleConfiguration currentRuleConfig) throws SQLException {
RuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(sqlStatement);
MetaDataContexts originalMetaDataContexts = contextManager.getMetaDataContexts();
MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.mode.manager.ContextManager;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
Expand All @@ -51,7 +52,8 @@ public final class DropDatabaseRuleOperator implements DatabaseRuleOperator {

@Override
@SuppressWarnings("unchecked")
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) {
public Collection<MetaDataVersion> operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database,
final RuleConfiguration currentRuleConfig) throws SQLException {
if (!executor.hasAnyOneToBeDropped(sqlStatement)) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,51 +76,45 @@ public DatabaseRuleConfigurationManager(final AtomicReference<MetaDataContexts>
*
* @param databaseName database name
* @param ruleConfig rule configurations
* @throws SQLException SQL Exception
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public synchronized void alterRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) {
public synchronized void alterRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) throws SQLException {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
Collection<ShardingSphereRule> rules = new LinkedList<>(database.getRuleMetaData().getRules());
Optional<ShardingSphereRule> toBeChangedRule = rules.stream().filter(each -> each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
if (toBeChangedRule.isPresent() && toBeChangedRule.get() instanceof PartialRuleUpdateSupported && ((PartialRuleUpdateSupported) toBeChangedRule.get()).partialUpdate(ruleConfig)) {
((PartialRuleUpdateSupported) toBeChangedRule.get()).updateConfiguration(ruleConfig);
return;
}
try {
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(),
ruleConfig, computeNodeInstanceContext, database.getResourceMetaData()));
refreshMetadata(databaseName, database, rules);
} catch (final SQLException ex) {
log.error("Alter database: {} rule configurations failed", databaseName, ex);
}
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(),
ruleConfig, computeNodeInstanceContext, database.getResourceMetaData()));
refreshMetadata(databaseName, database, rules);
}

/**
* Drop rule configuration.
*
* @param databaseName database name
* @param ruleConfig rule configurations
* @throws SQLException SQL Exception
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public synchronized void dropRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) {
public synchronized void dropRuleConfiguration(final String databaseName, final RuleConfiguration ruleConfig) throws SQLException {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
Collection<ShardingSphereRule> rules = new LinkedList<>(database.getRuleMetaData().getRules());
Optional<ShardingSphereRule> toBeChangedRule = rules.stream().filter(each -> each.getConfiguration().getClass().equals(ruleConfig.getClass())).findFirst();
if (toBeChangedRule.isPresent() && toBeChangedRule.get() instanceof PartialRuleUpdateSupported && ((PartialRuleUpdateSupported) toBeChangedRule.get()).partialUpdate(ruleConfig)) {
((PartialRuleUpdateSupported) toBeChangedRule.get()).updateConfiguration(ruleConfig);
return;
}
try {
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
if (!((DatabaseRuleConfiguration) ruleConfig).isEmpty()) {
rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(),
ruleConfig, computeNodeInstanceContext, database.getResourceMetaData()));
}
refreshMetadata(databaseName, database, rules);
} catch (final SQLException ex) {
log.error("Drop database: {} rule configurations failed", databaseName, ex);
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
if (!((DatabaseRuleConfiguration) ruleConfig).isEmpty()) {
rules.addAll(DatabaseRulesBuilder.build(databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(),
ruleConfig, computeNodeInstanceContext, database.getResourceMetaData()));
}
refreshMetadata(databaseName, database, rules);
}

private void refreshMetadata(final String databaseName, final ShardingSphereDatabase database, final Collection<ShardingSphereRule> rules) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;

import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand All @@ -49,9 +50,10 @@ public RuleItemManager(final AtomicReference<MetaDataContexts> metaDataContexts,
* Alter with rule item.
*
* @param event alter rule item event
* @throws SQLException SQL Exception
*/
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public void alterRuleItem(final AlterRuleItemEvent event) {
public void alterRuleItem(final AlterRuleItemEvent event) throws SQLException {
if (!event.getActiveVersion().equals(metaDataPersistService.getMetaDataVersionPersistService()
.getActiveVersionByFullPath(event.getActiveVersionKey()))) {
return;
Expand All @@ -71,9 +73,10 @@ public void alterRuleItem(final AlterRuleItemEvent event) {
* Drop with rule item.
*
* @param event drop rule item event
* @throws SQLException SQL Exception
*/
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public void dropRuleItem(final DropRuleItemEvent event) {
public void dropRuleItem(final DropRuleItemEvent event) throws SQLException {
String databaseName = event.getDatabaseName();
if (!metaDataContexts.get().getMetaData().containsDatabase(databaseName)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTableStatement;

import java.sql.SQLException;
import java.util.Collection;

/**
Expand All @@ -36,7 +37,7 @@ public final class DropTableStatementSchemaRefresher implements MetaDataRefreshe

@Override
public void refresh(final MetaDataManagerPersistService metaDataManagerPersistService, final ShardingSphereDatabase database, final Collection<String> logicDataSourceNames,
final String schemaName, final DatabaseType databaseType, final DropTableStatement sqlStatement, final ConfigurationProperties props) {
final String schemaName, final DatabaseType databaseType, final DropTableStatement sqlStatement, final ConfigurationProperties props) throws SQLException {
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName);
sqlStatement.getTables().forEach(each -> alterSchemaMetaDataPOJO.getDroppedTables().add(each.getTableName().getIdentifier().getValue()));
boolean isRuleRefreshRequired = TableRefreshUtils.isRuleRefreshRequired(database.getRuleMetaData(), schemaName, sqlStatement.getTables());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,28 @@ public interface MetaDataManagerPersistService {
*
* @param databaseName database name
* @param ruleConfigs rule configs
* @throws SQLException SQL exception
*/
void alterSingleRuleConfiguration(String databaseName, Collection<RuleConfiguration> ruleConfigs);
void alterSingleRuleConfiguration(String databaseName, Collection<RuleConfiguration> ruleConfigs) throws SQLException;

/**
* Alter rule configuration.
*
* @param databaseName database name
* @param toBeAlteredRuleConfig to be altered rule config
* @return meta data versions
* @throws SQLException SQL exception
*/
Collection<MetaDataVersion> alterRuleConfiguration(String databaseName, RuleConfiguration toBeAlteredRuleConfig);
Collection<MetaDataVersion> alterRuleConfiguration(String databaseName, RuleConfiguration toBeAlteredRuleConfig) throws SQLException;

/**
* Remove rule configuration item.
*
* @param databaseName database name
* @param toBeRemovedRuleConfig to be removed rule config
* @throws SQLException SQL exception
*/
void removeRuleConfigurationItem(String databaseName, RuleConfiguration toBeRemovedRuleConfig);
void removeRuleConfigurationItem(String databaseName, RuleConfiguration toBeRemovedRuleConfig) throws SQLException;

/**
* Remove rule configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
import org.apache.shardingsphere.mode.manager.ContextManager;

import java.sql.SQLException;

/**
* Rule item changed subscriber.
*/
Expand All @@ -36,19 +38,21 @@ public final class RuleItemChangedSubscriber implements EventSubscriber {
* Renew with alter rule item.
*
* @param event alter rule item event
* @throws SQLException SQL Exception
*/
@Subscribe
public void renew(final AlterRuleItemEvent event) {
public void renew(final AlterRuleItemEvent event) throws SQLException {
contextManager.getMetaDataContextManager().getRuleItemManager().alterRuleItem(event);
}

/**
* Renew with drop rule item.
*
* @param event drop rule item event
* @throws SQLException SQL Exception
*/
@Subscribe
public void renew(final DropRuleItemEvent event) {
public void renew(final DropRuleItemEvent event) throws SQLException {
contextManager.getMetaDataContextManager().getRuleItemManager().dropRuleItem(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void unregisterStorageUnits(final String databaseName, final Collection<S
}

@Override
public void alterSingleRuleConfiguration(final String databaseName, final Collection<RuleConfiguration> ruleConfigs) {
public void alterSingleRuleConfiguration(final String databaseName, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
ruleConfigs.removeIf(each -> !each.getClass().isAssignableFrom(SingleRuleConfiguration.class));
Collection<MetaDataVersion> metaDataVersions = metaDataPersistService.getDatabaseRulePersistService()
.persistConfigurations(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName).getName(), ruleConfigs);
Expand All @@ -280,7 +280,7 @@ public void alterSingleRuleConfiguration(final String databaseName, final Collec
}

@Override
public Collection<MetaDataVersion> alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) {
public Collection<MetaDataVersion> alterRuleConfiguration(final String databaseName, final RuleConfiguration toBeAlteredRuleConfig) throws SQLException {
if (null != toBeAlteredRuleConfig) {
Collection<MetaDataVersion> metaDataVersions = metaDataPersistService.getDatabaseRulePersistService()
.persistConfigurations(metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName).getName(), Collections.singletonList(toBeAlteredRuleConfig));
Expand All @@ -301,7 +301,7 @@ private Optional<DispatchEvent> buildAlterRuleItemEvent(final String databaseNam
}

@Override
public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) {
public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) throws SQLException {
if (null != toBeRemovedRuleConfig) {
Collection<MetaDataVersion> metaDataVersions = metaDataPersistService.getDatabaseRulePersistService()
.deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig));
Expand Down