You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/20 05:27:27 UTC

[shardingsphere] branch master updated: Add new global rule RAL backend handler for new metadata structure (#26444)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 ce0fd0879e1 Add new global rule RAL backend handler for new metadata structure (#26444)
ce0fd0879e1 is described below

commit ce0fd0879e14e9551b77783f15f746f91cefb14a
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Tue Jun 20 13:27:20 2023 +0800

    Add new global rule RAL backend handler for new metadata structure (#26444)
    
    * Add new global rule RAL backend handler for new metadata structure
    
    * Update comments
    
    * Fix code style
---
 .../NewUpdatableGlobalRuleRALBackendHandler.java   | 77 ++++++++++++++++++++++
 .../distsql/ral/RALBackendHandlerFactory.java      |  4 ++
 2 files changed, 81 insertions(+)

diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
new file mode 100644
index 00000000000..135c0cfc62d
--- /dev/null
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/NewUpdatableGlobalRuleRALBackendHandler.java
@@ -0,0 +1,77 @@
+/*
+ * 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.ral;
+
+import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+import org.apache.shardingsphere.distsql.handler.ral.update.GlobalRuleRALUpdater;
+import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+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 java.util.Collection;
+
+/**
+ * New updatable RAL backend handler for global rule.
+ */
+public final class NewUpdatableGlobalRuleRALBackendHandler implements DistSQLBackendHandler {
+    
+    private final UpdatableGlobalRuleRALStatement sqlStatement;
+    
+    public NewUpdatableGlobalRuleRALBackendHandler(final UpdatableGlobalRuleRALStatement sqlStatement) {
+        this.sqlStatement = sqlStatement;
+    }
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    @Override
+    public ResponseHeader execute() {
+        GlobalRuleRALUpdater globalRuleUpdater = TypedSPILoader.getService(GlobalRuleRALUpdater.class, sqlStatement.getClass().getName());
+        Class<? extends RuleConfiguration> ruleConfigClass = globalRuleUpdater.getRuleConfigurationClass();
+        ContextManager contextManager = ProxyContext.getInstance().getContextManager();
+        Collection<RuleConfiguration> ruleConfigurations = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
+        RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(ruleConfigurations, ruleConfigClass);
+        globalRuleUpdater.checkSQLStatement(currentRuleConfig, sqlStatement);
+        contextManager.getInstanceContext().getModeContextManager()
+                .alterGlobalRuleConfiguration(processUpdate(ruleConfigurations, sqlStatement, globalRuleUpdater, currentRuleConfig));
+        // TODO switch active version
+        return new UpdateResponseHeader(sqlStatement);
+    }
+    
+    private RuleConfiguration findCurrentRuleConfiguration(final Collection<RuleConfiguration> ruleConfigurations, final Class<? extends RuleConfiguration> ruleConfigClass) {
+        for (RuleConfiguration each : ruleConfigurations) {
+            if (ruleConfigClass.isAssignableFrom(each.getClass())) {
+                return each;
+            }
+        }
+        throw new MissingRequiredRuleException(ruleConfigClass.getSimpleName());
+    }
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private RuleConfiguration processUpdate(final Collection<RuleConfiguration> ruleConfigurations, final RALStatement sqlStatement, final GlobalRuleRALUpdater globalRuleUpdater,
+                                                        final RuleConfiguration currentRuleConfig) {
+        RuleConfiguration result = globalRuleUpdater.buildAlteredRuleConfiguration(currentRuleConfig, sqlStatement);
+        ruleConfigurations.remove(currentRuleConfig);
+        ruleConfigurations.add(result);
+        return result;
+    }
+}
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index bdca8ad7753..428e3b63901 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.distsql.parser.statement.ral.QueryableRALStatem
 import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 
@@ -44,6 +45,9 @@ public final class RALBackendHandlerFactory {
             return new QueryableRALBackendHandler<>((QueryableRALStatement) sqlStatement, connectionSession);
         }
         if (sqlStatement instanceof UpdatableGlobalRuleRALStatement) {
+            if ("New_Cluster".equals(ProxyContext.getInstance().getContextManager().getInstanceContext().getModeConfiguration().getType())) {
+                return new NewUpdatableGlobalRuleRALBackendHandler((UpdatableGlobalRuleRALStatement) sqlStatement);
+            }
             return new UpdatableGlobalRuleRALBackendHandler((UpdatableGlobalRuleRALStatement) sqlStatement);
         }
         return new UpdatableRALBackendHandler<>((UpdatableRALStatement) sqlStatement, connectionSession);