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/06/12 10:54:16 UTC
[shardingsphere] branch master updated: Add unsupported storage node type (#26196)
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 2a0a83f6669 Add unsupported storage node type (#26196)
2a0a83f6669 is described below
commit 2a0a83f66690327e92abda3c16cf4644463833f5
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Mon Jun 12 18:54:08 2023 +0800
Add unsupported storage node type (#26196)
* Add unsupported storage node type
* Add unsupported storage node type
* fix
* Add unsupported storage node type
* Fix
* Fix
* Fix
---
.../database/type/checker/DatabaseTypeChecker.java | 99 ++++++++++++++++++++++
.../props/DataSourcePropertiesValidator.java | 10 +--
.../exception/UnsupportedStorageTypeException.java | 4 +
.../metadata/factory/ExternalMetaDataFactory.java | 34 +-------
.../test/fixture/jdbc/MockedDriver.java | 5 +-
5 files changed, 114 insertions(+), 38 deletions(-)
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/checker/DatabaseTypeChecker.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/checker/DatabaseTypeChecker.java
new file mode 100644
index 00000000000..58d88d61a87
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/type/checker/DatabaseTypeChecker.java
@@ -0,0 +1,99 @@
+/*
+ * 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.database.type.checker;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.exception.UnsupportedStorageTypeException;
+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.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Database type checker.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DatabaseTypeChecker {
+
+ private static final Collection<String> MOCKED_URL_PREFIXES = new HashSet<>(Arrays.asList("jdbc:fixture", "jdbc:mock", "mock:jdbc"));
+
+ private static final Collection<String> UNSUPPORTED_URL_PREFIXES = Collections.singletonList("jdbc:mysql:aws");
+
+ private static final Collection<DatabaseType> SUPPORTED_STORAGE_TYPES = new HashSet<>(8, 1F);
+
+ private static volatile boolean isChecked;
+
+ static {
+ Arrays.asList("MySQL", "PostgreSQL", "openGauss", "Oracle", "SQLServer", "H2", "MariaDB")
+ .forEach(each -> TypedSPILoader.findService(DatabaseType.class, each).ifPresent(SUPPORTED_STORAGE_TYPES::add));
+ }
+
+ /**
+ * Check supported storage types.
+ *
+ * @param dataSources data sources
+ * @param databaseName database name
+ * @param storageTypes storage types
+ * @throws SQLException SQL exception
+ */
+ public static void checkSupportedStorageTypes(final Map<String, DataSource> dataSources, final String databaseName, final Map<String, DatabaseType> storageTypes) throws SQLException {
+ if (isChecked || dataSources.isEmpty()) {
+ return;
+ }
+ for (Entry<String, DataSource> entry : dataSources.entrySet()) {
+ try (Connection connection = entry.getValue().getConnection()) {
+ String url = connection.getMetaData().getURL();
+ if (MOCKED_URL_PREFIXES.stream().anyMatch(url::startsWith)) {
+ return;
+ }
+ ShardingSpherePreconditions.checkState(UNSUPPORTED_URL_PREFIXES.stream()
+ .noneMatch(url::startsWith), () -> new UnsupportedStorageTypeException(databaseName, entry.getKey()));
+ }
+ }
+ storageTypes.forEach((key, value) -> ShardingSpherePreconditions.checkState(SUPPORTED_STORAGE_TYPES.stream()
+ .anyMatch(each -> each.getClass().equals(value.getClass())), () -> new UnsupportedStorageTypeException(databaseName, key)));
+ isChecked = true;
+ }
+
+ /**
+ * Check supported storage type.
+ *
+ * @param url URL
+ * @param dataSourceName data source name
+ */
+ public static void checkSupportedStorageType(final String url, final String dataSourceName) {
+ if (MOCKED_URL_PREFIXES.stream().anyMatch(url::startsWith)) {
+ return;
+ }
+ ShardingSpherePreconditions.checkState(UNSUPPORTED_URL_PREFIXES.stream()
+ .noneMatch(url::startsWith), () -> new UnsupportedStorageTypeException(dataSourceName));
+ ShardingSpherePreconditions.checkState(SUPPORTED_STORAGE_TYPES.stream()
+ .flatMap(storageType -> storageType.getJdbcUrlPrefixes().stream())
+ .anyMatch(url::startsWith), () -> new UnsupportedStorageTypeException(dataSourceName));
+ }
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
index b04abb9494c..5268c3c2f5a 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidator.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.datasource.props;
+import org.apache.shardingsphere.infra.database.type.checker.DatabaseTypeChecker;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
@@ -73,7 +74,7 @@ public final class DataSourcePropertiesValidator {
DataSource dataSource = null;
try {
dataSource = DataSourcePoolCreator.create(dataSourceProps);
- checkFailFast(dataSource);
+ checkFailFast(dataSourceName, dataSource);
// CHECKSTYLE:OFF
} catch (final SQLException | RuntimeException ex) {
// CHECKSTYLE:ON
@@ -85,10 +86,9 @@ public final class DataSourcePropertiesValidator {
}
}
- private void checkFailFast(final DataSource dataSource) throws SQLException {
- // CHECKSTYLE:OFF
- try (Connection ignored = dataSource.getConnection()) {
- // CHECKSTYLE:ON
+ private void checkFailFast(final String dataSourceName, final DataSource dataSource) throws SQLException {
+ try (Connection connection = dataSource.getConnection()) {
+ DatabaseTypeChecker.checkSupportedStorageType(connection.getMetaData().getURL(), dataSourceName);
}
}
}
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
index aba75247ba1..6bc42c0a898 100644
--- 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
@@ -26,6 +26,10 @@ public final class UnsupportedStorageTypeException extends ConnectionSQLExceptio
private static final long serialVersionUID = 8981789100727786183L;
+ public UnsupportedStorageTypeException(final String dataSourceName) {
+ super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 40, "Unsupported storage type of `%s`.", dataSourceName);
+ }
+
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 6ff59711536..e93c9357bbc 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,19 +23,12 @@ 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.database.type.checker.DatabaseTypeChecker;
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;
@@ -46,15 +39,6 @@ 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, 1F);
-
- 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.
*
@@ -96,27 +80,13 @@ 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);
+ DatabaseTypeChecker.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(), 1F);
for (String each : protocolType.getSystemDatabaseSchemaMap().keySet()) {
diff --git a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
index 1119d9b46da..b7600b66c96 100644
--- a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
+++ b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
@@ -27,6 +27,7 @@ import java.util.logging.Logger;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
/**
* Mocked driver.
@@ -46,7 +47,9 @@ public final class MockedDriver implements Driver {
if (url.contains("invalid")) {
throw new SQLException("Invalid URL.");
}
- return mock(Connection.class, RETURNS_DEEP_STUBS);
+ Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaData().getURL()).thenReturn(url);
+ return result;
}
@Override