You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/04/18 13:12:59 UTC

[shardingsphere] branch master updated: Add unsupported storage type check (#25172)

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

duanzhengqiang 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 9d11b581aae Add unsupported storage type check (#25172)
9d11b581aae is described below

commit 9d11b581aae5118049eb98a6ae375e4b146d21b7
Author: ZhangCheng <fl...@outlook.com>
AuthorDate: Tue Apr 18 21:12:50 2023 +0800

    Add unsupported storage type check (#25172)
    
    * Add unsupported storage type check
    
    * fix
    
    * fix
---
 .../content/features/transaction/appendix.cn.md    |  1 +
 .../content/features/transaction/appendix.en.md    |  1 +
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../exception/UnsupportedStorageTypeException.java | 32 ++++++++++++++++++++++
 .../metadata/factory/ExternalMetaDataFactory.java  | 32 ++++++++++++++++++++++
 6 files changed, 68 insertions(+)

diff --git a/docs/document/content/features/transaction/appendix.cn.md b/docs/document/content/features/transaction/appendix.cn.md
index 886c0f13707..3eba19e6f8c 100644
--- a/docs/document/content/features/transaction/appendix.cn.md
+++ b/docs/document/content/features/transaction/appendix.cn.md
@@ -9,4 +9,5 @@ weight = 3
 - XA 事务中使用 DDL 语句。
 
 XA 事务所需的权限:
+
 在 MySQL8 中需要授予用户 `XA_RECOVER_ADMIN` 权限,否则 XA 事务管理器执行 `XA RECOVER` 语句时会报错。
diff --git a/docs/document/content/features/transaction/appendix.en.md b/docs/document/content/features/transaction/appendix.en.md
index c8579e8fb91..17656434302 100644
--- a/docs/document/content/features/transaction/appendix.en.md
+++ b/docs/document/content/features/transaction/appendix.en.md
@@ -9,4 +9,5 @@ Unsupported SQL:
 - DDL statements are used in XA transactions.
 
 Privileges required for XA transactions:
+
 In MySQL8, you need to grant the user `XA_RECOVER_ADMIN` privilege, otherwise the XA transaction manager will report an error when executing the `XA RECOVER` statement.
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index 9e13db3df32..6e3071cb53f 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -24,6 +24,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 42S02     | 10021       | Single table \`%s\` does not exist.                                            |
 | HY000     | 10022       | Can not load table with database name \`%s\` and data source name \`%s\`.      |
 | 0A000     | 10030       | Can not drop schema \`%s\` because of contains tables.                         |
+| 0A000     | 10040       | Unsupported storage type of \`%s.%s\`.                       |
 
 ### 数据
 
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index adf42c849a6..612b90037eb 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -24,6 +24,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | 42S02     | 10021       | Single table \`%s\` does not exist.                                            |
 | HY000     | 10022       | Can not load table with database name \`%s\` and data source name \`%s\`.      |
 | 0A000     | 10030       | Can not drop schema \`%s\` because of contains tables.                         |
+| 0A000     | 10040       | Unsupported storage type of \`%s.%s\`.                       |
 
 ### Data
 
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnsupportedStorageTypeException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnsupportedStorageTypeException.java
new file mode 100644
index 00000000000..aba75247ba1
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnsupportedStorageTypeException.java
@@ -0,0 +1,32 @@
+/*
+ * 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.exception;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Unsupported storage type exception.
+ */
+public final class UnsupportedStorageTypeException extends ConnectionSQLException {
+    
+    private static final long serialVersionUID = 8981789100727786183L;
+    
+    public UnsupportedStorageTypeException(final String databaseName, final String dataSourceName) {
+        super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 40, "Unsupported storage type of `%s.%s`.", databaseName, dataSourceName);
+    }
+}
diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java
index c9eec05c8bf..f4465709690 100644
--- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java
+++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactory.java
@@ -23,11 +23,19 @@ import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
+import org.apache.shardingsphere.infra.exception.UnsupportedStorageTypeException;
 import org.apache.shardingsphere.infra.instance.InstanceContext;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
 
+import javax.sql.DataSource;
+import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
@@ -38,6 +46,15 @@ import java.util.concurrent.ConcurrentHashMap;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class ExternalMetaDataFactory {
     
+    private static final Collection<String> MOCKED_URL_PREFIXES = new HashSet<>(Arrays.asList("jdbc:fixture", "jdbc:mock"));
+    
+    private static final Collection<DatabaseType> SUPPORTED_STORAGE_TYPES = new HashSet<>(8, 1);
+    
+    static {
+        Arrays.asList("MySQL", "PostgreSQL", "openGauss", "Oracle", "SQLServer", "H2", "MariaDB")
+                .forEach(each -> TypedSPILoader.findService(DatabaseType.class, each).ifPresent(SUPPORTED_STORAGE_TYPES::add));
+    }
+    
     /**
      * Create database meta data for db.
      *
@@ -79,12 +96,27 @@ public final class ExternalMetaDataFactory {
             String databaseName = entry.getKey();
             if (!entry.getValue().getDataSources().isEmpty() || !protocolType.getSystemSchemas().contains(databaseName)) {
                 Map<String, DatabaseType> storageTypes = DatabaseTypeEngine.getStorageTypes(entry.getKey(), entry.getValue());
+                checkSupportedStorageTypes(entry.getValue().getDataSources(), databaseName, storageTypes);
                 result.put(databaseName.toLowerCase(), ShardingSphereDatabase.create(databaseName, protocolType, storageTypes, entry.getValue(), props, instanceContext));
             }
         }
         return result;
     }
     
+    private static void checkSupportedStorageTypes(final Map<String, DataSource> dataSources, final String databaseName, final Map<String, DatabaseType> storageTypes) throws SQLException {
+        if (dataSources.isEmpty()) {
+            return;
+        }
+        try (Connection connection = dataSources.values().iterator().next().getConnection()) {
+            String url = connection.getMetaData().getURL();
+            if (MOCKED_URL_PREFIXES.stream().anyMatch(url::startsWith)) {
+                return;
+            }
+        }
+        storageTypes.forEach((key, value) -> ShardingSpherePreconditions.checkState(SUPPORTED_STORAGE_TYPES.stream()
+                .anyMatch(each -> each.getClass().equals(value.getClass())), () -> new UnsupportedStorageTypeException(databaseName, key)));
+    }
+    
     private static Map<String, ShardingSphereDatabase> createSystemDatabases(final Map<String, DatabaseConfiguration> databaseConfigMap, final DatabaseType protocolType) {
         Map<String, ShardingSphereDatabase> result = new HashMap<>(protocolType.getSystemDatabaseSchemaMap().size(), 1);
         for (String each : protocolType.getSystemDatabaseSchemaMap().keySet()) {