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

Refactor CreateDatabaseRuleRDLExecuteEngine and AlterDatabaseRuleRDLExecuteEngine #29829

Merged
merged 2 commits into from
Jan 24, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
package org.apache.shardingsphere.distsql.statement.rul.sql;

import lombok.Getter;
import org.apache.shardingsphere.distsql.statement.rul.SQLRULStatement;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;

/**
* Format statement.
*/
@RequiredArgsConstructor
@Getter
public final class FormatStatement extends SQLRULStatement {
public final class FormatStatement extends RULStatement {

public FormatStatement(final String sql) {
super(sql);
}
private final String sql;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
package org.apache.shardingsphere.distsql.statement.rul.sql;

import lombok.Getter;
import org.apache.shardingsphere.distsql.statement.rul.SQLRULStatement;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;

/**
* Parse statement.
*/
@RequiredArgsConstructor
@Getter
public final class ParseStatement extends SQLRULStatement {
public final class ParseStatement extends RULStatement {

public ParseStatement(final String sql) {
super(sql);
}
private final String sql;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
package org.apache.shardingsphere.distsql.statement.rul.sql;

import lombok.Getter;
import org.apache.shardingsphere.distsql.statement.rul.SQLRULStatement;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;

/**
* Preview statement.
*/
@RequiredArgsConstructor
@Getter
public final class PreviewStatement extends SQLRULStatement {
public final class PreviewStatement extends RULStatement {

public PreviewStatement(final String sql) {
super(sql);
}
private final String sql;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
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.rdl.rule.database.execute.DatabaseRuleRDLExecuteEngine;
import org.apache.shardingsphere.single.distsql.statement.rdl.UnloadSingleTableStatement;
import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -50,14 +50,18 @@ public Collection<MetaDataVersion> execute(final RuleDefinitionStatement sqlStat
RuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
ModeContextManager modeContextManager = ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
if (sqlStatement instanceof UnloadSingleTableStatement) {
if (isSinglePersistNode(currentRuleConfig)) {
return modeContextManager.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(database, currentRuleConfig));
}
RuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
modeContextManager.removeRuleConfigurationItem(database.getName(), toBeDroppedRuleConfig);
return modeContextManager.alterRuleConfiguration(database.getName(), toBeAlteredRuleConfig);
}

private boolean isSinglePersistNode(final RuleConfiguration currentRuleConfig) {
return currentRuleConfig instanceof SingleRuleConfiguration;
}

@SuppressWarnings("unchecked")
private RuleConfiguration decorateRuleConfiguration(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
return TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()).map(optional -> optional.decorate(database.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
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.rdl.rule.database.execute.DatabaseRuleRDLExecuteEngine;
import org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
import org.apache.shardingsphere.single.distsql.statement.rdl.SetDefaultSingleTableStorageUnitStatement;
import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -51,20 +50,21 @@ public Collection<MetaDataVersion> execute(final RuleDefinitionStatement sqlStat
RuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement);
ModeContextManager modeContextManager = ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
if (null == currentRuleConfig) {
return modeContextManager.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(sqlStatement, database, toBeCreatedRuleConfig));
return modeContextManager.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(database, toBeCreatedRuleConfig));
}
executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
return modeContextManager.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(sqlStatement, database, currentRuleConfig));
return modeContextManager.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(database, isSinglePersistNode(currentRuleConfig) ? currentRuleConfig : toBeCreatedRuleConfig));
}

private boolean isSinglePersistNode(final RuleConfiguration currentRuleConfig) {
return currentRuleConfig instanceof SingleRuleConfiguration;
}

@SuppressWarnings("unchecked")
private RuleConfiguration decorateRuleConfiguration(final RuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
if (sqlStatement instanceof LoadSingleTableStatement || sqlStatement instanceof SetDefaultSingleTableStorageUnitStatement) {
return TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()).map(optional -> optional.decorate(database.getName(),
database.getResourceMetaData().getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig);
}
return ruleConfig;
private RuleConfiguration decorateRuleConfiguration(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) {
return TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()).map(optional -> optional.decorate(database.getName(),
database.getResourceMetaData().getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rul.RULStatement;
import org.apache.shardingsphere.distsql.statement.rul.SQLRULStatement;
import org.apache.shardingsphere.distsql.statement.rul.sql.FormatStatement;
import org.apache.shardingsphere.distsql.statement.rul.sql.ParseStatement;
import org.apache.shardingsphere.distsql.statement.rul.sql.PreviewStatement;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.impl.SQLRULStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type.FormatSQLStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type.ParseStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type.PreviewStatementAssert;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.SQLParserTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rul.FormatSQLStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rul.ParseStatementTestCase;
import org.apache.shardingsphere.test.it.sql.parser.internal.cases.parser.jaxb.statement.rul.PreviewStatementTestCase;

/**
* RUL statement assert.
Expand All @@ -39,8 +46,12 @@ public final class RULStatementAssert {
* @param expected expected RUL statement test case
*/
public static void assertIs(final SQLCaseAssertContext assertContext, final RULStatement actual, final SQLParserTestCase expected) {
if (actual instanceof SQLRULStatement) {
SQLRULStatementAssert.assertIs(assertContext, (SQLRULStatement) actual, expected);
if (actual instanceof PreviewStatement) {
PreviewStatementAssert.assertIs(assertContext, (PreviewStatement) actual, (PreviewStatementTestCase) expected);
} else if (actual instanceof ParseStatement) {
ParseStatementAssert.assertIs(assertContext, (ParseStatement) actual, (ParseStatementTestCase) expected);
} else if (actual instanceof FormatStatement) {
FormatSQLStatementAssert.assertIs(assertContext, (FormatStatement) actual, (FormatSQLStatementTestCase) expected);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.impl.sql;
package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.impl.sql;
package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.impl.sql;
package org.apache.shardingsphere.test.it.sql.parser.internal.asserts.statement.rul.type;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down