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 2020/07/06 07:11:34 UTC

[shardingsphere] branch master updated: Fix 6239 (#6270)

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

zhangyonglun 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 7809caa  Fix 6239 (#6270)
7809caa is described below

commit 7809caa085355a81f81544f7101aab42a01d37fa
Author: xbkaishui <xb...@126.com>
AuthorDate: Mon Jul 6 15:11:20 2020 +0800

    Fix 6239 (#6270)
    
    * add usage for proxy startup command
    
    * dynamic set server version when proxy startup
    
    * fix check style
    
    * fix code review suggestions
    
    * fix code review suggestions
    
    * fix code review suggestions
---
 .../protocol/mysql/constant/MySQLServerInfo.java   | 26 +++++++++++++++++++---
 .../packet/handshake/MySQLHandshakePacket.java     |  2 +-
 .../packet/handshake/MySQLHandshakePacketTest.java | 12 +++++-----
 .../org/apache/shardingsphere/proxy/Bootstrap.java | 24 ++++++++++++++++++++
 .../frontend/command/CommandExecutorTask.java      |  4 +---
 5 files changed, 55 insertions(+), 13 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 ea29f76..e74f00a 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
@@ -32,12 +32,32 @@ public final class MySQLServerInfo {
     public static final int PROTOCOL_VERSION = 0x0A;
     
     /**
+     * Charset code 0x21 is utf8_general_ci.
+     */
+    public static final int CHARSET = 0x21;
+    
+    /**
      * Server version.
      */
-    public static final String SERVER_VERSION = "8.0.20-ShardingSphere-Proxy 5.0.0-RC1";
+    private static String serverVersion = "8.0.20-ShardingSphere-Proxy 5.0.0-RC1";
     
     /**
-     * Charset code 0x21 is utf8_general_ci.
+     * Set server version.
+     *
+     * @param serverVersion server version
      */
-    public static final int CHARSET = 0x21;
+    public static void setServerVersion(final String serverVersion) {
+        if (null != serverVersion) {
+            MySQLServerInfo.serverVersion = String.format("%s-ShardingSphere-Proxy 5.0.0-RC1", serverVersion);
+        }
+    }
+    
+    /**
+     * Get current server version.
+     *
+     * @return server version
+     */
+    public static String getServerVersion() {
+        return serverVersion;
+    }
 }
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
index f241e8f..386a892 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
@@ -53,7 +53,7 @@ public final class MySQLHandshakePacket implements MySQLPacket {
     private String authPluginName;
     
     public MySQLHandshakePacket(final int connectionId, final MySQLAuthPluginData authPluginData) {
-        this.serverVersion = MySQLServerInfo.SERVER_VERSION;
+        this.serverVersion = MySQLServerInfo.getServerVersion();
         this.connectionId = connectionId;
         this.capabilityFlagsLower = MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower();
         this.characterSet = MySQLServerInfo.CHARSET;
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
index cdc9bba..7035710 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
@@ -45,14 +45,14 @@ public final class MySQLHandshakePacketTest {
     @Test
     public void assertNewWithPayload() {
         when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.CHARSET, 0);
-        when(payload.readStringNul()).thenReturn(MySQLServerInfo.SERVER_VERSION);
+        when(payload.readStringNul()).thenReturn(MySQLServerInfo.getServerVersion());
         when(payload.readStringNulByBytes()).thenReturn(part1, part2);
         when(payload.readInt4()).thenReturn(1000);
         when(payload.readInt2()).thenReturn(
                 MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower(), MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
         MySQLHandshakePacket actual = new MySQLHandshakePacket(payload);
         assertThat(actual.getSequenceId(), is(0));
-        assertThat(actual.getServerVersion(), is(MySQLServerInfo.SERVER_VERSION));
+        assertThat(actual.getServerVersion(), is(MySQLServerInfo.getServerVersion()));
         assertThat(actual.getCapabilityFlagsLower(), is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
         assertThat(actual.getConnectionId(), is(1000));
         assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
@@ -66,14 +66,14 @@ public final class MySQLHandshakePacketTest {
     @Test
     public void assertNewWithClientPluginAuthPayload() {
         when(payload.readInt1()).thenReturn(0, MySQLServerInfo.PROTOCOL_VERSION, MySQLServerInfo.CHARSET, 0);
-        when(payload.readStringNul()).thenReturn(MySQLServerInfo.SERVER_VERSION, MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName());
+        when(payload.readStringNul()).thenReturn(MySQLServerInfo.getServerVersion(), MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName());
         when(payload.readStringNulByBytes()).thenReturn(part1, part2);
         when(payload.readInt4()).thenReturn(1000);
         when(payload.readInt2()).thenReturn(
             MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower(), MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >> 16);
         MySQLHandshakePacket actual = new MySQLHandshakePacket(payload);
         assertThat(actual.getSequenceId(), is(0));
-        assertThat(actual.getServerVersion(), is(MySQLServerInfo.SERVER_VERSION));
+        assertThat(actual.getServerVersion(), is(MySQLServerInfo.getServerVersion()));
         assertThat(actual.getCapabilityFlagsLower(), is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
         assertThat(actual.getConnectionId(), is(1000));
         assertThat(actual.getCharacterSet(), is(MySQLServerInfo.CHARSET));
@@ -90,7 +90,7 @@ public final class MySQLHandshakePacketTest {
         MySQLAuthPluginData authPluginData = new MySQLAuthPluginData(part1, part2);
         new MySQLHandshakePacket(1000, authPluginData).write(payload);
         verify(payload).writeInt1(MySQLServerInfo.PROTOCOL_VERSION);
-        verify(payload).writeStringNul(MySQLServerInfo.SERVER_VERSION);
+        verify(payload).writeStringNul(MySQLServerInfo.getServerVersion());
         verify(payload).writeInt4(1000);
         verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart1()));
         verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
@@ -109,7 +109,7 @@ public final class MySQLHandshakePacketTest {
         actual.setAuthPluginName(MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION);
         actual.write(payload);
         verify(payload).writeInt1(MySQLServerInfo.PROTOCOL_VERSION);
-        verify(payload).writeStringNul(MySQLServerInfo.SERVER_VERSION);
+        verify(payload).writeStringNul(MySQLServerInfo.getServerVersion());
         verify(payload).writeInt4(1000);
         verify(payload).writeStringNul(new String(authPluginData.getAuthPluginDataPart1()));
         verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
index 4b8df01..693b9d9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy;
 import com.google.common.primitives.Ints;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
 import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
@@ -28,6 +29,7 @@ import org.apache.shardingsphere.cluster.facade.ClusterFacade;
 import org.apache.shardingsphere.control.panel.spi.FacadeConfiguration;
 import org.apache.shardingsphere.control.panel.spi.engine.ControlPanelFacadeEngine;
 import org.apache.shardingsphere.control.panel.spi.opentracing.OpenTracingConfiguration;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
 import org.apache.shardingsphere.infra.auth.Authentication;
 import org.apache.shardingsphere.infra.auth.yaml.config.YamlAuthenticationConfiguration;
 import org.apache.shardingsphere.infra.auth.yaml.swapper.AuthenticationYamlSwapper;
@@ -63,6 +65,8 @@ import org.apache.shardingsphere.proxy.frontend.bootstrap.ShardingSphereProxy;
 
 import javax.sql.DataSource;
 import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.LinkedHashMap;
@@ -78,6 +82,7 @@ import java.util.stream.Collectors;
  * ShardingSphere-Proxy Bootstrap.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
+@Slf4j
 public final class Bootstrap {
     
     private static final int DEFAULT_PORT = 3307;
@@ -157,6 +162,25 @@ public final class Bootstrap {
         log(authentication, properties);
         initControlPanelFacade(metricsConfiguration);
         initCluster(cluster);
+        updateServerInfo();
+    }
+    
+    private static void updateServerInfo() {
+        List<String> schemaNames = ProxySchemaContexts.getInstance().getSchemaNames();
+        if (CollectionUtils.isEmpty(schemaNames)) {
+            return;
+        }
+        Map<String, DataSource> dataSources = ProxySchemaContexts.getInstance().getSchema(schemaNames.get(0)).getSchema().getDataSources();
+        DataSource singleDataSource = dataSources.values().iterator().next();
+        try (Connection connection = singleDataSource.getConnection()) {
+            DatabaseMetaData databaseMetaData = connection.getMetaData();
+            String databaseName = databaseMetaData.getDatabaseProductName();
+            String databaseVersion = databaseMetaData.getDatabaseProductVersion();
+            log.info("database name {} , database version {}", databaseName, databaseVersion);
+            MySQLServerInfo.setServerVersion(databaseVersion);
+        } catch (final SQLException ex) {
+            throw new ShardingSphereException("Get database server info failed", ex);
+        }
     }
     
     private static void initControlPanelFacade(final MetricsConfiguration metricsConfiguration) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
index 641c500..4ad2527 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/command/CommandExecutorTask.java
@@ -106,9 +106,7 @@ public final class CommandExecutorTask implements Runnable {
         if (responsePackets.isEmpty()) {
             return false;
         }
-        for (DatabasePacket each : responsePackets) {
-            context.write(each);
-        }
+        responsePackets.forEach(context::write);
         if (commandExecutor instanceof QueryCommandExecutor) {
             commandExecuteEngine.writeQueryData(context, backendConnection, (QueryCommandExecutor) commandExecutor, responsePackets.size());
             return true;