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 2021/01/12 14:40:43 UTC

[shardingsphere] branch master updated: 8927 (#8928)

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

zhangliang 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 4d5170b  8927 (#8928)
4d5170b is described below

commit 4d5170b265018aadd6b8ec1e11705fd72f4abd63
Author: tiger <ti...@users.noreply.github.com>
AuthorDate: Tue Jan 12 22:40:22 2021 +0800

    8927 (#8928)
    
    * fix issue 8927
---
 .../protocol/mysql/constant/MySQLServerInfo.java   |  2 +-
 .../mysql/constant/MySQLServerInfoTest.java        |  2 +-
 .../jdbc/metadata/JDBCQueryResultMetaData.java     |  7 +++++-
 .../proxy/backend/context/ProxyContext.java        | 18 +---------------
 .../text/TextProtocolBackendHandlerFactory.java    |  5 ++++-
 .../TextProtocolBackendHandlerFactoryTest.java     | 11 ++++++++++
 .../proxy/backend/context/ProxyContextTest.java    | 16 --------------
 .../impl/AbstractBootstrapInitializer.java         | 25 +++++++++++++++++++++-
 .../client/netty/MySQLNegotiateHandlerTest.java    |  6 +++---
 9 files changed, 51 insertions(+), 41 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
index 21dd3c4..b1514eb 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfo.java
@@ -36,7 +36,7 @@ public final class MySQLServerInfo {
      */
     public static final int CHARSET = 0x21;
     
-    private static final String DEFAULT_MYSQL_VERSION = "8.0.20";
+    private static final String DEFAULT_MYSQL_VERSION = "5.7.22";
     
     private static final String PROXY_VERSION = "5.0.0-beta";
     
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfoTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfoTest.java
index 3b457b6..50db84f 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfoTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerInfoTest.java
@@ -33,6 +33,6 @@ public final class MySQLServerInfoTest {
     @Test
     public void assertSetServerVersionForNull() {
         MySQLServerInfo.setServerVersion(null);
-        assertThat(MySQLServerInfo.getServerVersion(), is("8.0.20-ShardingSphere-Proxy 5.0.0-beta"));
+        assertThat(MySQLServerInfo.getServerVersion(), is("5.7.22-ShardingSphere-Proxy 5.0.0-beta"));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/metadata/JDBCQueryResultMetaData.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/metadata/JDBCQueryResultMetaData.java
index 6a56608..5597dff 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/metadata/JDBCQueryResultMetaData.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/metadata/JDBCQueryResultMetaData.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryRe
 
 import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
 
 /**
  * JDBC query result meta data.
@@ -73,7 +74,11 @@ public final class JDBCQueryResultMetaData implements QueryResultMetaData {
     
     @Override
     public boolean isSigned(final int columnIndex) throws SQLException {
-        return resultSetMetaData.isSigned(columnIndex);
+        try {
+            return resultSetMetaData.isSigned(columnIndex);
+        } catch (SQLFeatureNotSupportedException e) {
+            return false;
+        }
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
index 1df8caf..f28b333 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
@@ -27,11 +27,8 @@ import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedExcep
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.apache.shardingsphere.transaction.context.impl.StandardTransactionContexts;
 
-import javax.sql.DataSource;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
-import java.util.Optional;
 
 /**
  * Proxy context.
@@ -104,18 +101,5 @@ public final class ProxyContext {
     public List<String> getAllSchemaNames() {
         return new ArrayList<>(metaDataContexts.getAllSchemaNames());
     }
-    
-    /**
-     * Get data source sample.
-     * 
-     * @return data source sample
-     */
-    public Optional<DataSource> getDataSourceSample() {
-        List<String> schemaNames = getAllSchemaNames();
-        if (schemaNames.isEmpty()) {
-            return Optional.empty();
-        }
-        Map<String, DataSource> dataSources = getMetaData(schemaNames.get(0)).getResource().getDataSources();
-        return dataSources.values().stream().findFirst();
-    }
+
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index ee200d3..67c5c1e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -23,6 +23,7 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.text.admin.DatabaseAdminBackendHandlerFactory;
 import org.apache.shardingsphere.proxy.backend.text.data.DatabaseBackendHandlerFactory;
 import org.apache.shardingsphere.proxy.backend.text.distsql.DistSQLBackendHandlerFactory;
@@ -60,7 +61,9 @@ public final class TextProtocolBackendHandlerFactory {
         if (trimSQL.toUpperCase().startsWith(ShardingCTLBackendHandlerFactory.SCTL)) {
             return ShardingCTLBackendHandlerFactory.newInstance(trimSQL, backendConnection);
         }
-        SQLStatement sqlStatement = new ShardingSphereSQLParserEngine(databaseType.getName()).parse(sql, false);
+        DatabaseType backendDatabaseType =
+                ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
+        SQLStatement sqlStatement = new ShardingSphereSQLParserEngine(backendDatabaseType.getName()).parse(sql, false);
         if (sqlStatement instanceof TCLStatement) {
             return TransactionBackendHandlerFactory.newInstance((TCLStatement) sqlStatement, sql, backendConnection);
         }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
index 8a00254..f76f9a6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
@@ -18,8 +18,11 @@
 package org.apache.shardingsphere.proxy.backend;
 
 import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
@@ -65,6 +68,14 @@ public final class TextProtocolBackendHandlerFactoryTest {
         when(backendConnection.getTransactionStatus().getTransactionType()).thenReturn(TransactionType.LOCAL);
         setTransactionContexts();
         when(backendConnection.getSchemaName()).thenReturn("schema");
+
+        MetaDataContexts metaDataContexts = mock(MetaDataContexts.class);
+        when(metaDataContexts.getMetaData("schema")).thenReturn(mock(ShardingSphereMetaData.class));
+        when(metaDataContexts.getMetaData("schema").getResource()).thenReturn(mock(ShardingSphereResource.class));
+        when(metaDataContexts.getMetaData("schema").getResource().getDatabaseType()).thenReturn(databaseType);
+        TransactionContexts transactionContexts = mock(TransactionContexts.class);
+        ProxyContext proxyContext = ProxyContext.getInstance();
+        proxyContext.init(metaDataContexts, transactionContexts);
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
index 7df16a7..9dfdf31 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.junit.Test;
@@ -35,11 +34,9 @@ import org.junit.Test;
 import javax.sql.DataSource;
 import java.lang.reflect.Field;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -57,19 +54,6 @@ public final class ProxyContextTest {
     private static final String SCHEMA_PATTERN = "schema_%s";
     
     @Test
-    public void assertGetDataSourceSample() throws NoSuchFieldException, IllegalAccessException {
-        Map<String, DataSource> mockDataSourceMap = new HashMap<>(2, 1);
-        mockDataSourceMap.put("ds_1", new MockedDataSource());
-        mockDataSourceMap.put("ds_2", new MockedDataSource());
-        Field metaDataContexts = ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
-        metaDataContexts.setAccessible(true);
-        metaDataContexts.set(ProxyContext.getInstance(), 
-                new StandardMetaDataContexts(mockMetaDataMap(mockDataSourceMap), mock(ExecutorEngine.class), new DefaultAuthentication(), new ConfigurationProperties(new Properties())));
-        Optional<DataSource> actual = ProxyContext.getInstance().getDataSourceSample();
-        assertThat(actual, is(Optional.of(mockDataSourceMap.get("ds_1"))));
-    }
-    
-    @Test
     public void assertInit() {
         MetaDataContexts metaDataContexts = mock(MetaDataContexts.class);
         TransactionContexts transactionContexts = mock(TransactionContexts.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
index 93916fa..49d870f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContextsBuilder;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -41,6 +42,7 @@ import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.List;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.stream.Collectors;
@@ -100,13 +102,34 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
     }
     
     private void setDatabaseServerInfo() {
-        Optional<DataSource> dataSourceSample = ProxyContext.getInstance().getDataSourceSample();
+        Optional<DataSource> dataSourceSample = getBackendMySQLDatasource();
         if (dataSourceSample.isPresent()) {
             DatabaseServerInfo databaseServerInfo = new DatabaseServerInfo(dataSourceSample.get());
             log.info(databaseServerInfo.toString());
             MySQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
         }
     }
+
+    /**
+     * Get backend MySQL datasource.
+     *
+     * @return datasource
+     */
+    private Optional<DataSource> getBackendMySQLDatasource() {
+        List<String> schemaNames = ProxyContext.getInstance().getAllSchemaNames();
+        if (schemaNames.isEmpty()) {
+            return Optional.empty();
+        }
+        for (String schemaName : schemaNames) {
+            ShardingSphereResource shardingSphereResource =
+                    ProxyContext.getInstance().getMetaData(schemaName).getResource();
+            DatabaseType databaseType = shardingSphereResource.getDatabaseType();
+            if ("MySQL".equals(databaseType.getName())) {
+                return shardingSphereResource.getDataSources().values().stream().findFirst();
+            }
+        }
+        return Optional.empty();
+    }
     
     protected Optional<ServerConfiguration> getScalingConfiguration(final YamlProxyConfiguration yamlConfig) {
         if (null != yamlConfig.getServerConfiguration().getScaling()) {
diff --git a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiateHandlerTest.java b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiateHandlerTest.java
index 32ad0da..5b8ff45 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiateHandlerTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-dialect/shardingsphere-scaling-mysql/src/test/java/org/apache/shardingsphere/scaling/mysql/client/netty/MySQLNegotiateHandlerTest.java
@@ -79,9 +79,9 @@ public final class MySQLNegotiateHandlerTest {
         verify(channel).writeAndFlush(ArgumentMatchers.any(MySQLHandshakeResponse41Packet.class));
         ServerInfo serverInfo = ReflectionUtil.getFieldValue(mysqlNegotiateHandler, "serverInfo", ServerInfo.class);
         assertNotNull(serverInfo);
-        assertThat(serverInfo.getServerVersion().getMajor(), is(8));
-        assertThat(serverInfo.getServerVersion().getMinor(), is(0));
-        assertThat(serverInfo.getServerVersion().getSeries(), is(20));
+        assertThat(serverInfo.getServerVersion().getMajor(), is(5));
+        assertThat(serverInfo.getServerVersion().getMinor(), is(7));
+        assertThat(serverInfo.getServerVersion().getSeries(), is(22));
     }
     
     @Test