This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 5a3fb41db44 Fix no spi implement exception when config different
protocolType and storageType (#21776)
5a3fb41db44 is described below
commit 5a3fb41db44821c52a21fd8432162e7af3b65cb7
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Oct 26 20:29:43 2022 +0800
Fix no spi implement exception when config different protocolType and
storageType (#21776)
---
.../proxy/version/ShardingSphereProxyVersion.java | 22 +++--
.../version/ShardingSphereProxyVersionTest.java | 93 ++++++++++++++++++++++
...oxy.frontend.spi.DatabaseProtocolFrontendEngine | 18 +++++
3 files changed, 125 insertions(+), 8 deletions(-)
diff --git
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
index a27014c8928..3f669543715 100644
---
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
+++
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
@@ -21,15 +21,17 @@ import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.db.protocol.CommonConstants;
import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.datasource.state.DataSourceStateManager;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.database.DatabaseServerInfo;
import
org.apache.shardingsphere.proxy.frontend.protocol.DatabaseProtocolFrontendEngineFactory;
import javax.sql.DataSource;
-
-import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -45,8 +47,7 @@ public final class ShardingSphereProxyVersion {
*/
public static void setVersion(final ContextManager contextManager) {
CommonConstants.PROXY_VERSION.set(ShardingSphereProxyVersion.getProxyVersion());
-
contextManager.getMetaDataContexts().getMetaData().getDatabases().keySet()
- .forEach(each -> setDatabaseVersion(each,
DataSourceStateManager.getInstance().getEnabledDataSourceMap(each,
contextManager.getDataSourceMap(each))));
+
contextManager.getMetaDataContexts().getMetaData().getDatabases().values().forEach(ShardingSphereProxyVersion::setDatabaseVersion);
}
private static String getProxyVersion() {
@@ -59,15 +60,20 @@ public final class ShardingSphereProxyVersion {
return result;
}
- private static void setDatabaseVersion(final String databaseName, final
Map<String, DataSource> dataSources) {
- Optional<DataSource> dataSource =
dataSources.values().stream().findFirst();
+ private static void setDatabaseVersion(final ShardingSphereDatabase
database) {
+ Optional<DataSource> dataSource =
findDataSourceByProtocolType(database.getName(),
database.getResourceMetaData(), database.getProtocolType());
if (!dataSource.isPresent()) {
return;
}
DatabaseServerInfo databaseServerInfo = new
DatabaseServerInfo(dataSource.get());
- log.info("{}, database name is `{}`", databaseServerInfo,
databaseName);
+ log.info("{}, database name is `{}`", databaseServerInfo,
database.getName());
DatabaseProtocolFrontendEngineFactory
.newInstance(DatabaseTypeEngine.getTrunkDatabaseType(databaseServerInfo.getDatabaseName()))
- .setDatabaseVersion(databaseName,
databaseServerInfo.getDatabaseVersion());
+ .setDatabaseVersion(database.getName(),
databaseServerInfo.getDatabaseVersion());
+ }
+
+ private static Optional<DataSource> findDataSourceByProtocolType(final
String databaseName, final ShardingSphereResourceMetaData resourceMetaData,
final DatabaseType protocolType) {
+ Optional<String> dataSourceName =
resourceMetaData.getStorageTypes().entrySet().stream().filter(entry ->
entry.getValue().equals(protocolType)).map(Entry::getKey).findFirst();
+ return dataSourceName.flatMap(optional ->
Optional.ofNullable(DataSourceStateManager.getInstance().getEnabledDataSourceMap(databaseName,
resourceMetaData.getDataSources()).get(optional)));
}
}
diff --git
a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java
b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java
new file mode 100644
index 00000000000..9669981841c
--- /dev/null
+++
b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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.version;
+
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeFactory;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.Test;
+import org.mockito.MockedStatic;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+public final class ShardingSphereProxyVersionTest {
+
+ @Test
+ public void assertSetVersionWhenStorageTypeSameWithProtocolType() throws
SQLException {
+ ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("sharding_db");
+ ShardingSphereResourceMetaData resourceMetaData =
mock(ShardingSphereResourceMetaData.class);
+
when(resourceMetaData.getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
DatabaseTypeFactory.getInstance("MySQL")));
+ DataSource dataSource = createDataSource("MySQL", "5.7.32");
+
when(resourceMetaData.getDataSources()).thenReturn(Collections.singletonMap("ds_0",
dataSource));
+ when(database.getResourceMetaData()).thenReturn(resourceMetaData);
+
when(database.getProtocolType()).thenReturn(DatabaseTypeFactory.getInstance("MySQL"));
+
when(contextManager.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.singletonMap("sharding_db",
database));
+ try (MockedStatic<ProxyContext> mockedStatic =
mockStatic(ProxyContext.class)) {
+ ProxyContext proxyContext = mock(ProxyContext.class,
RETURNS_DEEP_STUBS);
+
mockedStatic.when(ProxyContext::getInstance).thenReturn(proxyContext);
+
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.PROXY_MYSQL_DEFAULT_VERSION)).thenReturn("5.7.22");
+ ShardingSphereProxyVersion.setVersion(contextManager);
+ }
+ assertThat(MySQLServerInfo.getServerVersion("sharding_db"),
startsWith("5.7.32"));
+ }
+
+ @Test
+ public void assertSetVersionWhenStorageTypeDifferentWithProtocolType()
throws SQLException {
+ ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("sharding_db");
+ ShardingSphereResourceMetaData resourceMetaData =
mock(ShardingSphereResourceMetaData.class);
+
when(resourceMetaData.getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
DatabaseTypeEngine.getDatabaseType("Oracle")));
+ DataSource dataSource = createDataSource("Oracle", "12.0.0");
+
when(resourceMetaData.getDataSources()).thenReturn(Collections.singletonMap("ds_0",
dataSource));
+ when(database.getResourceMetaData()).thenReturn(resourceMetaData);
+
when(database.getProtocolType()).thenReturn(DatabaseTypeFactory.getInstance("MySQL"));
+
when(contextManager.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.singletonMap("sharding_db",
database));
+ ShardingSphereProxyVersion.setVersion(contextManager);
+ assertThat(MySQLServerInfo.getServerVersion("sharding_db"),
startsWith("5.7.22"));
+ }
+
+ private static DataSource createDataSource(final String
databaseProductName, final String databaseProductVersion) throws SQLException {
+ DataSource result = mock(DataSource.class);
+ Connection connection = mock(Connection.class);
+ DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
+
when(databaseMetaData.getDatabaseProductName()).thenReturn(databaseProductName);
+
when(databaseMetaData.getDatabaseProductVersion()).thenReturn(databaseProductVersion);
+ when(connection.getMetaData()).thenReturn(databaseMetaData);
+ when(result.getConnection()).thenReturn(connection);
+ return result;
+ }
+}
diff --git
a/proxy/bootstrap/src/test/resources/META-INF/services/org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine
b/proxy/bootstrap/src/test/resources/META-INF/services/org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine
new file mode 100644
index 00000000000..bf5a12edc51
--- /dev/null
+++
b/proxy/bootstrap/src/test/resources/META-INF/services/org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.proxy.frontend.mysql.MySQLFrontendEngine