Skip to content

Commit

Permalink
Refactor RuleDefinitionBackendHandler to support old metadata (apache…
Browse files Browse the repository at this point in the history
…#29865)

* Refactor RuleDefinitionBackendHandler to support old metadata

* Fix unit test

* Fix checkstyle
  • Loading branch information
zhaojinchao95 authored Jan 29, 2024
1 parent 47e9685 commit cd9c360
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.distsql.handler.checker.ReadwriteSplittingRuleStatementChecker;
Expand Down Expand Up @@ -137,6 +138,11 @@ public boolean hasAnyOneToBeDropped(final DropReadwriteSplittingRuleStatement sq
.map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toSet()), sqlStatement.getNames()).isEmpty();
}

@Override
public void operate(final DropReadwriteSplittingRuleStatement sqlStatement, final ShardingSphereDatabase database) {
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class).ifPresent(optional -> sqlStatement.getNames().forEach(optional::cleanStorageNodeDataSource));
}

@Override
public Class<ReadwriteSplittingRuleConfiguration> getRuleConfigurationClass() {
return ReadwriteSplittingRuleConfiguration.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.database.DatabaseRuleDefinitionExecuteEngine;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.global.GlobalRuleDefinitionExecuteEngine;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy.LegacyDatabaseRuleDefinitionExecuteEngine;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy.LegacyGlobalRuleDefinitionExecuteEngine;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
Expand Down Expand Up @@ -50,18 +51,36 @@ public abstract class RuleDefinitionExecuteEngine {
public void executeUpdate() {
Optional<DatabaseRuleDefinitionExecutor> databaseExecutor = TypedSPILoader.findService(DatabaseRuleDefinitionExecutor.class, sqlStatement.getClass());
if (databaseExecutor.isPresent()) {
executeDatabaseRule(databaseExecutor.get());
} else {
executeGlobalRule(TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class, sqlStatement.getClass()));
}
}

@SuppressWarnings("rawtypes")
private void executeDatabaseRule(final DatabaseRuleDefinitionExecutor databaseExecutor) {
if (isNormalRuleUpdater()) {
new DatabaseRuleDefinitionExecuteEngine(
sqlStatement, contextManager, getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, currentDatabaseName)), databaseExecutor.get()).executeUpdate();
sqlStatement, contextManager, getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, currentDatabaseName)), databaseExecutor).executeUpdate();
} else {
new LegacyDatabaseRuleDefinitionExecuteEngine(
sqlStatement, contextManager, getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, currentDatabaseName)), databaseExecutor).executeUpdate();
}
}

@SuppressWarnings("rawtypes")
private void executeGlobalRule(final GlobalRuleDefinitionExecutor globalExecutor) {
if (isNormalRuleUpdater()) {
new GlobalRuleDefinitionExecuteEngine(sqlStatement, contextManager, globalExecutor).executeUpdate();
} else {
String modeType = contextManager.getInstanceContext().getModeConfiguration().getType();
GlobalRuleDefinitionExecutor globalExecutor = TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class, sqlStatement.getClass());
if ("Cluster".equals(modeType) || "Standalone".equals(modeType)) {
new GlobalRuleDefinitionExecuteEngine(sqlStatement, contextManager, globalExecutor).executeUpdate();
} else {
new LegacyGlobalRuleDefinitionExecuteEngine(sqlStatement, contextManager).executeUpdate();
}
new LegacyGlobalRuleDefinitionExecuteEngine(sqlStatement, contextManager, globalExecutor).executeUpdate();
}
}

private boolean isNormalRuleUpdater() {
String modeType = contextManager.getInstanceContext().getModeConfiguration().getType();
return "Cluster".equals(modeType) || "Standalone".equals(modeType);
}

protected abstract ShardingSphereDatabase getDatabase(String databaseName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public final class GlobalRuleDefinitionExecuteEngine {
*/
@SuppressWarnings("unchecked")
public void executeUpdate() {
Class<? extends RuleConfiguration> ruleConfigClass = executor.getRuleConfigurationClass();
Collection<RuleConfiguration> ruleConfigs = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(ruleConfigs, ruleConfigClass);
RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(ruleConfigs, executor.getRuleConfigurationClass());
executor.checkBeforeUpdate(currentRuleConfig, sqlStatement);
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs, sqlStatement, currentRuleConfig));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,20 @@
* limitations under the License.
*/

package org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule;
package org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor;
import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
import org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.readwritesplitting.distsql.handler.update.DropReadwriteSplittingRuleExecutor;
import org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Collection;
Expand All @@ -48,29 +40,33 @@

// TODO Remove when metadata structure adjustment completed. #25485
/**
* Legacy rule definition backend handler.
* Legacy rule definition execute engine.
*/
@RequiredArgsConstructor
public final class LegacyRuleDefinitionBackendHandler implements DistSQLBackendHandler {
public final class LegacyDatabaseRuleDefinitionExecuteEngine {

private final RuleDefinitionStatement sqlStatement;

private final ConnectionSession connectionSession;
private final ContextManager contextManager;

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public ResponseHeader execute() {
ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement, connectionSession.getDatabaseName()));
DatabaseRuleDefinitionExecutor executor = TypedSPILoader.getService(DatabaseRuleDefinitionExecutor.class, sqlStatement.getClass());
private final ShardingSphereDatabase database;

@SuppressWarnings("rawtypes")
private final DatabaseRuleDefinitionExecutor executor;

/**
* Execute update.
*/
@SuppressWarnings("unchecked")
public void executeUpdate() {
Class<? extends RuleConfiguration> ruleConfigClass = executor.getRuleConfigurationClass();
RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null);
executor.setDatabase(database);
executor.checkBeforeUpdate(sqlStatement, currentRuleConfig);
if (getRefreshStatus(sqlStatement, currentRuleConfig, executor)) {
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager()
contextManager.getInstanceContext().getModeContextManager()
.alterRuleConfiguration(database.getName(), processSQLStatement(database, sqlStatement, executor, currentRuleConfig));
}
return new UpdateResponseHeader(sqlStatement);
}

private Optional<RuleConfiguration> findCurrentRuleConfiguration(final ShardingSphereDatabase database, final Class<? extends RuleConfiguration> ruleConfigClass) {
Expand Down Expand Up @@ -130,10 +126,7 @@ private void processDrop(final ShardingSphereDatabase database, final Collection
if (executor.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig)) {
configs.remove(currentRuleConfig);
}
if (executor instanceof DropReadwriteSplittingRuleExecutor) {
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class)
.ifPresent(optional -> ((DropReadwriteSplittingRuleStatement) sqlStatement).getNames().forEach(optional::cleanStorageNodeDataSource));
}
executor.operate(sqlStatement, database);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
import org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;

import java.util.Collection;
Expand All @@ -39,15 +38,16 @@ public final class LegacyGlobalRuleDefinitionExecuteEngine {

private final ContextManager contextManager;

@SuppressWarnings("rawtypes")
private final GlobalRuleDefinitionExecutor executor;

/**
* Execute update.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@SuppressWarnings("unchecked")
public void executeUpdate() {
GlobalRuleDefinitionExecutor executor = TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class, sqlStatement.getClass());
Class<? extends RuleConfiguration> ruleConfigClass = executor.getRuleConfigurationClass();
Collection<RuleConfiguration> ruleConfigs = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(ruleConfigs, ruleConfigClass);
RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(ruleConfigs, executor.getRuleConfigurationClass());
executor.checkBeforeUpdate(currentRuleConfig, sqlStatement);
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs, sqlStatement, executor, currentRuleConfig));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database;

import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;

import java.util.Collection;
Expand Down Expand Up @@ -95,4 +96,14 @@ default boolean hasAnyOneToBeDropped(final T sqlStatement, final R currentRuleCo
default Collection<String> getIdenticalData(final Collection<String> currentRules, final Collection<String> toBeDroppedRules) {
return currentRules.stream().filter(each -> toBeDroppedRules.stream().anyMatch(each::equalsIgnoreCase)).collect(Collectors.toSet());
}

// TODO Remove when metadata structure adjustment completed. #25485
/**
* Drop rule configuration operate.
*
* @param sqlStatement SQL statement
* @param database database
*/
default void operate(final T sqlStatement, final ShardingSphereDatabase database) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
import org.apache.shardingsphere.distsql.statement.rdl.RDLStatement;
import org.apache.shardingsphere.distsql.statement.rdl.resource.ResourceDefinitionStatement;
import org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLUpdateBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.LegacyRuleDefinitionBackendHandler;
import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.RuleDefinitionBackendHandler;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;

Expand All @@ -47,15 +44,6 @@ public static ProxyBackendHandler newInstance(final RDLStatement sqlStatement, f
if (sqlStatement instanceof ResourceDefinitionStatement) {
return new DistSQLUpdateBackendHandler(sqlStatement, connectionSession);
}
return getRuleBackendHandler((RuleDefinitionStatement) sqlStatement, connectionSession);
}

private static DistSQLBackendHandler getRuleBackendHandler(final RuleDefinitionStatement sqlStatement, final ConnectionSession connectionSession) {
// TODO Remove when metadata structure adjustment completed. #25485
String modeType = ProxyContext.getInstance().getContextManager().getInstanceContext().getModeConfiguration().getType();
if ("Cluster".equals(modeType) || "Standalone".equals(modeType)) {
return new RuleDefinitionBackendHandler(sqlStatement, connectionSession);
}
return new LegacyRuleDefinitionBackendHandler(sqlStatement, connectionSession);
return new RuleDefinitionBackendHandler((RuleDefinitionStatement) sqlStatement, connectionSession);
}
}

This file was deleted.

0 comments on commit cd9c360

Please sign in to comment.