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/15 10:35:02 UTC

[shardingsphere] branch master updated: Optimize performance of single table ddl refresher (#26367)

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 213fb16bd61 Optimize performance of single table ddl refresher (#26367)
213fb16bd61 is described below

commit 213fb16bd6189042734ddfe30de335b4087fabed
Author: Raigor <ra...@gmail.com>
AuthorDate: Thu Jun 15 18:34:54 2023 +0800

    Optimize performance of single table ddl refresher (#26367)
    
    * Optimize performance of single table ddl refresher
    
    * Optimize performance of single table ddl refresher
    
    * Optimize performance of single table ddl refresher
    
    * Update java doc
---
 infra/context/pom.xml                              |  5 ++
 .../table/CreateTableStatementSchemaRefresher.java |  3 +-
 .../table/DropTableStatementSchemaRefresher.java   |  5 +-
 .../refresher/util/TableRefreshUtils.java          | 73 ++++++++++++++++++++++
 .../single/api}/constant/SingleTableConstants.java |  2 +-
 .../single/datanode/SingleTableDataNodeLoader.java |  2 +-
 .../single/util/SingleTableLoadUtils.java          |  2 +-
 .../update/LoadSingleTableStatementUpdater.java    |  2 +-
 .../update/UnloadSingleTableStatementUpdater.java  |  2 +-
 9 files changed, 89 insertions(+), 7 deletions(-)

diff --git a/infra/context/pom.xml b/infra/context/pom.xml
index 5be35e6bb0e..8821f28bd68 100644
--- a/infra/context/pom.xml
+++ b/infra/context/pom.xml
@@ -53,6 +53,11 @@
             <artifactId>shardingsphere-authority-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-single-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-transaction-core</artifactId>
diff --git a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/CreateTableStatementSchemaRefresher.java b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/CreateTableStatementSchemaRefresher.java
index 64ee7d51382..65adc62c0be 100644
--- a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/CreateTableStatementSchemaRefresher.java
+++ b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/CreateTableStatementSchemaRefresher.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.connection.refresher.type.table;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.connection.refresher.MetaDataRefresher;
+import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
 import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -60,7 +61,7 @@ public final class CreateTableStatementSchemaRefresher implements MetaDataRefres
             AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName, logicDataSourceNames);
             alterSchemaMetaDataPOJO.getAlteredTables().add(actualTableMetaData.get());
             modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
-            if (isSingleTable) {
+            if (isSingleTable && TableRefreshUtils.isRuleRefreshRequired(ruleMetaData, schemaName, tableName)) {
                 modeContextManager.alterRuleConfiguration(database.getName(), ruleMetaData.getConfigurations());
             }
         }
diff --git a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
index 3d68549f3c6..38dd649b3d6 100644
--- a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
+++ b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.connection.refresher.type.table;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.connection.refresher.MetaDataRefresher;
+import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
 import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -42,7 +43,9 @@ public final class DropTableStatementSchemaRefresher implements MetaDataRefreshe
         modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
         ShardingSphereRuleMetaData ruleMetaData = database.getRuleMetaData();
         for (SimpleTableSegment each : sqlStatement.getTables()) {
-            if (isSingleTable(each.getTableName().getIdentifier().getValue(), ruleMetaData)) {
+            String tableName = each.getTableName().getIdentifier().getValue();
+            if (isSingleTable(tableName, ruleMetaData)
+                    && TableRefreshUtils.isRuleRefreshRequired(ruleMetaData, schemaName, tableName)) {
                 modeContextManager.alterRuleConfiguration(database.getName(), ruleMetaData.getConfigurations());
                 break;
             }
diff --git a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/util/TableRefreshUtils.java b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/util/TableRefreshUtils.java
new file mode 100644
index 00000000000..86d33f8b85d
--- /dev/null
+++ b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/util/TableRefreshUtils.java
@@ -0,0 +1,73 @@
+/*
+ * 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.infra.connection.refresher.util;
+
+import com.google.common.base.Joiner;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
+import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
+import org.apache.shardingsphere.single.api.constant.SingleTableConstants;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Table refresh utils.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TableRefreshUtils {
+    
+    /**
+     * Judge whether the rule need to be refreshed.
+     * 
+     * @param ruleMetaData rule meta data
+     * @param schemaName schema name
+     * @param tableName table name
+     * @return whether the rule need to be refreshed
+     */
+    public static boolean isRuleRefreshRequired(final ShardingSphereRuleMetaData ruleMetaData, final String schemaName, final String tableName) {
+        Optional<MutableDataNodeRule> singleRule = ruleMetaData.findSingleRule(MutableDataNodeRule.class);
+        if (!singleRule.isPresent()) {
+            return false;
+        }
+        RuleConfiguration ruleConfiguration = singleRule.get().getConfiguration();
+        if (!(ruleConfiguration instanceof SingleRuleConfiguration)) {
+            return false;
+        }
+        Collection<String> tablesConfig = ((SingleRuleConfiguration) ruleConfiguration).getTables();
+        if (tablesConfig.contains(SingleTableConstants.ALL_TABLES) || tablesConfig.contains(SingleTableConstants.ALL_SCHEMA_TABLES)) {
+            return false;
+        }
+        Optional<DataNode> dataNode = singleRule.get().findTableDataNode(schemaName, tableName);
+        if (!dataNode.isPresent()) {
+            return false;
+        }
+        DataNode actualNode = dataNode.get();
+        return !tablesConfig.contains(joinDataNodeSegments(actualNode.getDataSourceName(), SingleTableConstants.ASTERISK))
+                && !tablesConfig.contains(joinDataNodeSegments(actualNode.getDataSourceName(), SingleTableConstants.ASTERISK, SingleTableConstants.ASTERISK))
+                && !tablesConfig.contains(joinDataNodeSegments(actualNode.getDataSourceName(), actualNode.getSchemaName(), SingleTableConstants.ASTERISK));
+    }
+    
+    private static String joinDataNodeSegments(final String... segments) {
+        return Joiner.on(".").join(segments);
+    }
+}
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/constant/SingleTableConstants.java b/kernel/single/api/src/main/java/org/apache/shardingsphere/single/api/constant/SingleTableConstants.java
similarity index 95%
rename from kernel/single/core/src/main/java/org/apache/shardingsphere/single/constant/SingleTableConstants.java
rename to kernel/single/api/src/main/java/org/apache/shardingsphere/single/api/constant/SingleTableConstants.java
index c9056d797de..7fd11af7cad 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/constant/SingleTableConstants.java
+++ b/kernel/single/api/src/main/java/org/apache/shardingsphere/single/api/constant/SingleTableConstants.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.single.constant;
+package org.apache.shardingsphere.single.api.constant;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java
index 8d913cafe97..43d5c3c5a1e 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/datanode/SingleTableDataNodeLoader.java
@@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.metadata.database.schema.loader.common.SchemaMetaDataLoader;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.single.constant.SingleTableConstants;
+import org.apache.shardingsphere.single.api.constant.SingleTableConstants;
 import org.apache.shardingsphere.single.exception.SingleTablesLoadingException;
 import org.apache.shardingsphere.single.util.SingleTableLoadUtils;
 
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/util/SingleTableLoadUtils.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/util/SingleTableLoadUtils.java
index 560baa32cbc..a92eae45c5e 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/util/SingleTableLoadUtils.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/util/SingleTableLoadUtils.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
 import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
-import org.apache.shardingsphere.single.constant.SingleTableConstants;
+import org.apache.shardingsphere.single.api.constant.SingleTableConstants;
 
 import javax.sql.DataSource;
 import java.util.Collection;
diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
index c9605609153..3c9a1f83ab7 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
@@ -29,7 +29,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
 import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
 import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
-import org.apache.shardingsphere.single.constant.SingleTableConstants;
+import org.apache.shardingsphere.single.api.constant.SingleTableConstants;
 import org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader;
 import org.apache.shardingsphere.single.distsql.handler.exception.MissingRequiredSingleTableException;
 import org.apache.shardingsphere.single.distsql.segment.SingleTableSegment;
diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableStatementUpdater.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableStatementUpdater.java
index 7eac0a535c7..e3628661c1e 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableStatementUpdater.java
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableStatementUpdater.java
@@ -28,7 +28,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
 import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
-import org.apache.shardingsphere.single.constant.SingleTableConstants;
+import org.apache.shardingsphere.single.api.constant.SingleTableConstants;
 import org.apache.shardingsphere.single.distsql.statement.rdl.UnloadSingleTableStatement;
 import org.apache.shardingsphere.single.exception.SingleTableNotFoundException;
 import org.apache.shardingsphere.single.rule.SingleRule;