This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 19a0ffb9ecf Add DatabaseRuleRDLExecuteEngine (#29815)
19a0ffb9ecf is described below
commit 19a0ffb9ecfa2b5833be62d13ce937d0b8aaa011
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Jan 23 18:55:23 2024 +0800
Add DatabaseRuleRDLExecuteEngine (#29815)
* Fix sonar issue
* Refactor GlobalRuleUpdater
* Refactor DatabaseRuleUpdater
* Add DatabaseRuleRDLExecuteEngine
---
.../distsql/rdl/rule/DatabaseRuleUpdater.java | 168 ---------------------
.../rdl/rule/RuleDefinitionBackendHandler.java | 2 +
.../rdl/rule/database/DatabaseRuleUpdater.java | 71 +++++++++
.../execute/DatabaseRuleRDLExecuteEngine.java | 41 +++++
.../DatabaseRuleRDLExecuteEngineFactory.java | 56 +++++++
.../type/AlterDatabaseRuleRDLExecuteEngine.java | 68 +++++++++
.../type/CreateDatabaseRuleRDLExecuteEngine.java | 74 +++++++++
.../type/DropDatabaseRuleRDLExecuteEngine.java | 71 +++++++++
.../rdl/rule/{ => global}/GlobalRuleUpdater.java | 13 +-
.../rdl/rule/legacy/LegacyGlobalRuleUpdater.java | 1 +
.../legacy/LegacyRuleDefinitionBackendHandler.java | 1 +
11 files changed, 391 insertions(+), 175 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/DatabaseRuleUpdater.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/DatabaseRuleUpdater.java
deleted file mode 100644
index 1733240057f..00000000000
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/DatabaseRuleUpdater.java
+++ /dev/null
@@ -1,168 +0,0 @@
-/*
- * 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.proxy.backend.handler.distsql.rdl.rule;
-
-import lombok.RequiredArgsConstructor;
-import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLAlterExecutor;
-import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLCreateExecutor;
-import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLDropExecutor;
-import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLExecutor;
-import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
-import
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
-import
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
-import
org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapperEngine;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import org.apache.shardingsphere.proxy.backend.util.DatabaseNameUtils;
-import
org.apache.shardingsphere.readwritesplitting.distsql.handler.update.DropReadwriteSplittingRuleExecutor;
-import
org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement;
-import
org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
-import
org.apache.shardingsphere.single.distsql.statement.rdl.SetDefaultSingleTableStorageUnitStatement;
-import
org.apache.shardingsphere.single.distsql.statement.rdl.UnloadSingleTableStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map.Entry;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
-/**
- * TODO Rename to RuleDefinitionBackendHandler when metadata structure
adjustment completed. #25485
- * Database rule updater.
- *
- * @param <T> type of rule definition statement
- */
-@RequiredArgsConstructor
-public final class DatabaseRuleUpdater<T extends RuleDefinitionStatement> {
-
- private final T sqlStatement;
-
- private final ConnectionSession connectionSession;
-
- @SuppressWarnings("rawtypes")
- private final DatabaseRuleRDLExecutor executor;
-
- /**
- * Execute update.
- */
- @SuppressWarnings("unchecked")
- public void executeUpdate() {
- ShardingSphereDatabase database =
ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
connectionSession));
- Class<? extends RuleConfiguration> ruleConfigClass =
executor.getRuleConfigurationClass();
- RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null);
- executor.checkSQLStatement(database, sqlStatement, currentRuleConfig);
- if (getRefreshStatus(sqlStatement, currentRuleConfig, executor)) {
-
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getPersistService().getMetaDataVersionPersistService().switchActiveVersion(
- processSQLStatement(database, sqlStatement, executor,
currentRuleConfig));
- }
- }
-
- private Optional<RuleConfiguration> findCurrentRuleConfiguration(final
ShardingSphereDatabase database, final Class<? extends RuleConfiguration>
ruleConfigClass) {
- return
database.getRuleMetaData().getConfigurations().stream().filter(each ->
ruleConfigClass.isAssignableFrom(each.getClass())).findFirst();
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- private boolean getRefreshStatus(final SQLStatement sqlStatement, final
RuleConfiguration currentRuleConfig, final DatabaseRuleRDLExecutor<?, ?>
executor) {
- return !(executor instanceof DatabaseRuleRDLDropExecutor) ||
((DatabaseRuleRDLDropExecutor) executor).hasAnyOneToBeDropped(sqlStatement,
currentRuleConfig);
- }
-
- @SuppressWarnings("rawtypes")
- private Collection<MetaDataVersion> processSQLStatement(final
ShardingSphereDatabase database,
- final T
sqlStatement, final DatabaseRuleRDLExecutor executor, final RuleConfiguration
currentRuleConfig) {
- if (executor instanceof DatabaseRuleRDLCreateExecutor) {
- return processCreate(database, sqlStatement,
(DatabaseRuleRDLCreateExecutor) executor, currentRuleConfig);
- }
- if (executor instanceof DatabaseRuleRDLAlterExecutor) {
- return processAlter(database, sqlStatement,
(DatabaseRuleRDLAlterExecutor) executor, currentRuleConfig);
- }
- if (executor instanceof DatabaseRuleRDLDropExecutor) {
- return processDrop(database, sqlStatement,
(DatabaseRuleRDLDropExecutor) executor, currentRuleConfig);
- }
- throw new UnsupportedSQLOperationException(String.format("Cannot
support RDL executor type `%s`", executor.getClass().getName()));
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- private Collection<MetaDataVersion> processCreate(final
ShardingSphereDatabase database, final T sqlStatement, final
DatabaseRuleRDLCreateExecutor executor,
- final RuleConfiguration
currentRuleConfig) {
- RuleConfiguration toBeCreatedRuleConfig =
executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement);
- if (null != currentRuleConfig) {
- executor.updateCurrentRuleConfiguration(currentRuleConfig,
toBeCreatedRuleConfig);
- }
- if (sqlStatement instanceof LoadSingleTableStatement || sqlStatement
instanceof SetDefaultSingleTableStorageUnitStatement) {
- return
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().alterRuleConfiguration(database.getName(),
- null == currentRuleConfig ?
decorateRuleConfiguration(database, toBeCreatedRuleConfig) :
decorateRuleConfiguration(database, currentRuleConfig));
- }
- return
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().alterRuleConfiguration(database.getName(),
toBeCreatedRuleConfig);
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- private Collection<MetaDataVersion> processAlter(final
ShardingSphereDatabase database,
- final T sqlStatement,
final DatabaseRuleRDLAlterExecutor executor, final RuleConfiguration
currentRuleConfig) {
- RuleConfiguration toBeAlteredRuleConfig =
executor.buildToBeAlteredRuleConfiguration(sqlStatement);
- executor.updateCurrentRuleConfiguration(currentRuleConfig,
toBeAlteredRuleConfig);
- if (sqlStatement instanceof UnloadSingleTableStatement) {
- return
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager()
- .alterRuleConfiguration(database.getName(),
decorateRuleConfiguration(database, currentRuleConfig));
- }
- RuleConfiguration toBeDroppedRuleConfig =
executor.buildToBeDroppedRuleConfiguration(currentRuleConfig,
toBeAlteredRuleConfig);
-
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().removeRuleConfigurationItem(database.getName(),
toBeDroppedRuleConfig);
- return
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().alterRuleConfiguration(database.getName(),
toBeAlteredRuleConfig);
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- private Collection<MetaDataVersion> processDrop(final
ShardingSphereDatabase database,
- final T sqlStatement,
final DatabaseRuleRDLDropExecutor executor, final RuleConfiguration
currentRuleConfig) {
- if (!executor.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig)) {
- return Collections.emptyList();
- }
- ModeContextManager modeContextManager =
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
- RuleConfiguration toBeDroppedRuleConfig =
executor.buildToBeDroppedRuleConfiguration(currentRuleConfig, sqlStatement);
- // TODO remove updateCurrentRuleConfiguration after update refactor
completed.
- if (executor.updateCurrentRuleConfiguration(sqlStatement,
currentRuleConfig) && ((DatabaseRuleConfiguration)
currentRuleConfig).isEmpty()) {
- modeContextManager.removeRuleConfigurationItem(database.getName(),
toBeDroppedRuleConfig);
- new
NewYamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(Collections.singleton(currentRuleConfig)).values().stream().findFirst()
- .ifPresent(swapper ->
modeContextManager.removeRuleConfiguration(database.getName(),
swapper.getRuleTagName().toLowerCase()));
- return Collections.emptyList();
- }
- if (executor instanceof DropReadwriteSplittingRuleExecutor) {
-
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class)
- .ifPresent(optional ->
((DropReadwriteSplittingRuleStatement)
sqlStatement).getNames().forEach(optional::cleanStorageNodeDataSource));
- // TODO refactor to new metadata refresh way
- }
- modeContextManager.removeRuleConfigurationItem(database.getName(),
toBeDroppedRuleConfig);
- RuleConfiguration toBeAlteredRuleConfig =
executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, sqlStatement);
- return modeContextManager.alterRuleConfiguration(database.getName(),
toBeAlteredRuleConfig);
- }
-
- @SuppressWarnings("unchecked")
- 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);
- }
-}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java
index 04a0360806f..906a3292eb0 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java
@@ -24,6 +24,8 @@ import
org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
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.handler.distsql.rdl.rule.database.DatabaseRuleUpdater;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.global.GlobalRuleUpdater;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.legacy.LegacyGlobalRuleUpdater;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/DatabaseRuleUpdater.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/DatabaseRuleUpdater.java
new file mode 100644
index 00000000000..a74c4b9e99a
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/DatabaseRuleUpdater.java
@@ -0,0 +1,71 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database;
+
+import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLDropExecutor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLExecutor;
+import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.database.execute.DatabaseRuleRDLExecuteEngineFactory;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.util.DatabaseNameUtils;
+
+import java.util.Optional;
+
+/**
+ * Database rule updater.
+ *
+ * @param <T> type of rule definition statement
+ */
+@RequiredArgsConstructor
+public final class DatabaseRuleUpdater<T extends RuleDefinitionStatement> {
+
+ private final T sqlStatement;
+
+ private final ConnectionSession connectionSession;
+
+ @SuppressWarnings("rawtypes")
+ private final DatabaseRuleRDLExecutor executor;
+
+ /**
+ * Execute update.
+ */
+ @SuppressWarnings("unchecked")
+ public void executeUpdate() {
+ ShardingSphereDatabase database =
ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
connectionSession));
+ Class<? extends RuleConfiguration> ruleConfigClass =
executor.getRuleConfigurationClass();
+ RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null);
+ executor.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+ if (getRefreshStatus(currentRuleConfig)) {
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getPersistService().getMetaDataVersionPersistService()
+
.switchActiveVersion(DatabaseRuleRDLExecuteEngineFactory.newInstance(executor).execute(sqlStatement,
database, currentRuleConfig));
+ }
+ }
+
+ private Optional<RuleConfiguration> findCurrentRuleConfiguration(final
ShardingSphereDatabase database, final Class<? extends RuleConfiguration>
ruleConfigClass) {
+ return
database.getRuleMetaData().getConfigurations().stream().filter(each ->
ruleConfigClass.isAssignableFrom(each.getClass())).findFirst();
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private boolean getRefreshStatus(final RuleConfiguration
currentRuleConfig) {
+ return !(executor instanceof DatabaseRuleRDLDropExecutor) ||
((DatabaseRuleRDLDropExecutor) executor).hasAnyOneToBeDropped(sqlStatement,
currentRuleConfig);
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngine.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngine.java
new file mode 100644
index 00000000000..624f67ddf40
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngine.java
@@ -0,0 +1,41 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database.execute;
+
+import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+
+import java.util.Collection;
+
+/**
+ * Database rule RDL execute engine.
+ */
+public interface DatabaseRuleRDLExecuteEngine {
+
+ /**
+ * Execute when rule changed.
+ *
+ * @param sqlStatement SQL statement
+ * @param database database
+ * @param currentRuleConfig current rule configuration
+ * @return meta data versions
+ */
+ Collection<MetaDataVersion> execute(RuleDefinitionStatement sqlStatement,
ShardingSphereDatabase database, RuleConfiguration currentRuleConfig);
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngineFactory.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngineFactory.java
new file mode 100644
index 00000000000..9d655057a21
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/DatabaseRuleRDLExecuteEngineFactory.java
@@ -0,0 +1,56 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database.execute;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLAlterExecutor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLCreateExecutor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLDropExecutor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLExecutor;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.database.execute.type.AlterDatabaseRuleRDLExecuteEngine;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.database.execute.type.CreateDatabaseRuleRDLExecuteEngine;
+import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.database.execute.type.DropDatabaseRuleRDLExecuteEngine;
+
+/**
+ * Database rule RDL execute engine factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DatabaseRuleRDLExecuteEngineFactory {
+
+ /**
+ * Create new instance of database RDL execute engine.
+ * @param executor database rule RDL executor
+ * @return created instance
+ * @throws UnsupportedSQLOperationException if invalid database rule RDL
executor
+ */
+ @SuppressWarnings("rawtypes")
+ public static DatabaseRuleRDLExecuteEngine newInstance(final
DatabaseRuleRDLExecutor executor) {
+ if (executor instanceof DatabaseRuleRDLCreateExecutor) {
+ return new
CreateDatabaseRuleRDLExecuteEngine((DatabaseRuleRDLCreateExecutor) executor);
+ }
+ if (executor instanceof DatabaseRuleRDLAlterExecutor) {
+ return new
AlterDatabaseRuleRDLExecuteEngine((DatabaseRuleRDLAlterExecutor) executor);
+ }
+ if (executor instanceof DatabaseRuleRDLDropExecutor) {
+ return new
DropDatabaseRuleRDLExecuteEngine((DatabaseRuleRDLDropExecutor) executor);
+ }
+ throw new UnsupportedSQLOperationException(String.format("Cannot
support RDL executor type `%s`", executor.getClass().getName()));
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/AlterDatabaseRuleRDLExecuteEngine.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/AlterDatabaseRuleRDLExecuteEngine.java
new file mode 100644
index 00000000000..ef111b8ecd0
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/AlterDatabaseRuleRDLExecuteEngine.java
@@ -0,0 +1,68 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database.execute.type;
+
+import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLAlterExecutor;
+import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
+import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+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 java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+/**
+ * Alter database rule RDL execute engine.
+ */
+@RequiredArgsConstructor
+public final class AlterDatabaseRuleRDLExecuteEngine implements
DatabaseRuleRDLExecuteEngine {
+
+ @SuppressWarnings("rawtypes")
+ private final DatabaseRuleRDLAlterExecutor executor;
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Collection<MetaDataVersion> execute(final RuleDefinitionStatement
sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration
currentRuleConfig) {
+ RuleConfiguration toBeAlteredRuleConfig =
executor.buildToBeAlteredRuleConfiguration(sqlStatement);
+ executor.updateCurrentRuleConfiguration(currentRuleConfig,
toBeAlteredRuleConfig);
+ ModeContextManager modeContextManager =
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
+ if (sqlStatement instanceof UnloadSingleTableStatement) {
+ 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);
+ }
+
+ @SuppressWarnings("unchecked")
+ 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);
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/CreateDatabaseRuleRDLExecuteEngine.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/CreateDatabaseRuleRDLExecuteEngine.java
new file mode 100644
index 00000000000..f7a3157cbc6
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/CreateDatabaseRuleRDLExecuteEngine.java
@@ -0,0 +1,74 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database.execute.type;
+
+import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLCreateExecutor;
+import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
+import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+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 java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+/**
+ * Create database rule RDL execute engine.
+ */
+@RequiredArgsConstructor
+public final class CreateDatabaseRuleRDLExecuteEngine implements
DatabaseRuleRDLExecuteEngine {
+
+ @SuppressWarnings("rawtypes")
+ private final DatabaseRuleRDLCreateExecutor executor;
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Collection<MetaDataVersion> execute(final RuleDefinitionStatement
sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration
currentRuleConfig) {
+ RuleConfiguration toBeCreatedRuleConfig =
executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement);
+ if (null != currentRuleConfig) {
+ executor.updateCurrentRuleConfiguration(currentRuleConfig,
toBeCreatedRuleConfig);
+ }
+ ModeContextManager modeContextManager =
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
+ return modeContextManager.alterRuleConfiguration(database.getName(),
getToBeCreatedRuleConfiguration(sqlStatement, database, currentRuleConfig,
toBeCreatedRuleConfig));
+ }
+
+ private RuleConfiguration getToBeCreatedRuleConfiguration(final
RuleDefinitionStatement sqlStatement,
+ final
ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig,
final RuleConfiguration toBeCreatedRuleConfig) {
+ if (sqlStatement instanceof LoadSingleTableStatement || sqlStatement
instanceof SetDefaultSingleTableStorageUnitStatement) {
+ return null == currentRuleConfig ?
decorateRuleConfiguration(database, toBeCreatedRuleConfig) :
decorateRuleConfiguration(database, currentRuleConfig);
+ }
+ return toBeCreatedRuleConfig;
+ }
+
+ @SuppressWarnings("unchecked")
+ 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);
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/DropDatabaseRuleRDLExecuteEngine.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/DropDatabaseRuleRDLExecuteEngine.java
new file mode 100644
index 00000000000..c781f2daeae
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/database/execute/type/DropDatabaseRuleRDLExecuteEngine.java
@@ -0,0 +1,71 @@
+/*
+ * 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.proxy.backend.handler.distsql.rdl.rule.database.execute.type;
+
+import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.database.DatabaseRuleRDLDropExecutor;
+import org.apache.shardingsphere.distsql.statement.rdl.RuleDefinitionStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
+import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
+import
org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
+import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapperEngine;
+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.readwritesplitting.distsql.handler.update.DropReadwriteSplittingRuleExecutor;
+import
org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Drop database rule RDL execute engine.
+ */
+@RequiredArgsConstructor
+public final class DropDatabaseRuleRDLExecuteEngine implements
DatabaseRuleRDLExecuteEngine {
+
+ @SuppressWarnings("rawtypes")
+ private final DatabaseRuleRDLDropExecutor executor;
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Collection<MetaDataVersion> execute(final RuleDefinitionStatement
sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration
currentRuleConfig) {
+ if (!executor.hasAnyOneToBeDropped(sqlStatement, currentRuleConfig)) {
+ return Collections.emptyList();
+ }
+ ModeContextManager modeContextManager =
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager();
+ RuleConfiguration toBeDroppedRuleConfig =
executor.buildToBeDroppedRuleConfiguration(currentRuleConfig, sqlStatement);
+ // TODO remove updateCurrentRuleConfiguration after update refactor
completed.
+ if (executor.updateCurrentRuleConfiguration(sqlStatement,
currentRuleConfig) && ((DatabaseRuleConfiguration)
currentRuleConfig).isEmpty()) {
+ modeContextManager.removeRuleConfigurationItem(database.getName(),
toBeDroppedRuleConfig);
+ new
NewYamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(Collections.singleton(currentRuleConfig)).values().stream().findFirst()
+ .ifPresent(swapper ->
modeContextManager.removeRuleConfiguration(database.getName(),
swapper.getRuleTagName().toLowerCase()));
+ return Collections.emptyList();
+ }
+ if (executor instanceof DropReadwriteSplittingRuleExecutor) {
+
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class)
+ .ifPresent(optional ->
((DropReadwriteSplittingRuleStatement)
sqlStatement).getNames().forEach(optional::cleanStorageNodeDataSource));
+ // TODO refactor to new metadata refresh way
+ }
+ modeContextManager.removeRuleConfigurationItem(database.getName(),
toBeDroppedRuleConfig);
+ RuleConfiguration toBeAlteredRuleConfig =
executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, sqlStatement);
+ return modeContextManager.alterRuleConfiguration(database.getName(),
toBeAlteredRuleConfig);
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/GlobalRuleUpdater.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/global/GlobalRuleUpdater.java
similarity index 88%
rename from
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/GlobalRuleUpdater.java
rename to
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/global/GlobalRuleUpdater.java
index 7a366edf6a6..cec8a682522 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/GlobalRuleUpdater.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/global/GlobalRuleUpdater.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule;
+package
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.global;
import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
@@ -48,7 +48,7 @@ public final class GlobalRuleUpdater {
Collection<RuleConfiguration> ruleConfigs =
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigs, ruleConfigClass);
executor.checkSQLStatement(currentRuleConfig, sqlStatement);
-
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs,
sqlStatement, executor, currentRuleConfig));
+
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs,
sqlStatement, currentRuleConfig));
}
private RuleConfiguration findCurrentRuleConfiguration(final
Collection<RuleConfiguration> ruleConfigs, final Class<? extends
RuleConfiguration> ruleConfigClass) {
@@ -60,12 +60,11 @@ public final class GlobalRuleUpdater {
throw new
MissingRequiredRuleException(ruleConfigClass.getSimpleName());
}
- @SuppressWarnings({"rawtypes", "unchecked"})
- private RuleConfiguration processUpdate(final
Collection<RuleConfiguration> ruleConfigurations, final RuleDefinitionStatement
sqlStatement, final GlobalRuleRDLExecutor executor,
- final RuleConfiguration
currentRuleConfig) {
+ @SuppressWarnings("unchecked")
+ private RuleConfiguration processUpdate(final
Collection<RuleConfiguration> ruleConfigs, final RuleDefinitionStatement
sqlStatement, final RuleConfiguration currentRuleConfig) {
RuleConfiguration result =
executor.buildAlteredRuleConfiguration(currentRuleConfig, sqlStatement);
- ruleConfigurations.remove(currentRuleConfig);
- ruleConfigurations.add(result);
+ ruleConfigs.remove(currentRuleConfig);
+ ruleConfigs.add(result);
return result;
}
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyGlobalRuleUpdater.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyGlobalRuleUpdater.java
index 01b41843a86..0087cdc76bc 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyGlobalRuleUpdater.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyGlobalRuleUpdater.java
@@ -29,6 +29,7 @@ import
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import java.util.Collection;
import java.util.LinkedList;
+// TODO Remove when metadata structure adjustment completed. #25485
/**
* Legacy global rule updater.
*/
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyRuleDefinitionBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyRuleDefinitionBackendHandler.java
index dc4fa0e3008..d828438ad0b 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyRuleDefinitionBackendHandler.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/legacy/LegacyRuleDefinitionBackendHandler.java
@@ -46,6 +46,7 @@ import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
+// TODO Remove when metadata structure adjustment completed. #25485
/**
* Legacy rule definition backend handler.
*