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 43bca30  Add ShardingSphereProxyVersion (#15224)
43bca30 is described below

commit 43bca3085b4ee13a295075a312315742ed5b010c
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jan 31 14:49:11 2022 +0800

    Add ShardingSphereProxyVersion (#15224)
    
    * Refactor YamlProxyDataSourceConfigurationSwapper
    
    * Add ShardingSphereProxyVersion
    
    * Add ShardingSphereProxyVersion
    
    * Refactor ShardingSphereProxyVersion
    
    * Refactor ShardingSphereProxyVersion
    
    * Use SPI to process set database version
    
    * Use SPI to process set database version
---
 .../YamlProxyDataSourceConfigurationSwapper.java   |  3 +-
 .../proxy/initializer/BootstrapInitializer.java    | 52 ++--------------
 .../proxy/version/ShardingSphereProxyVersion.java  | 69 ++++++++++++++++++++++
 .../proxy/frontend/mysql/MySQLFrontendEngine.java  |  6 ++
 .../opengauss/OpenGaussFrontendEngine.java         | 16 +++--
 .../postgresql/PostgreSQLFrontendEngine.java       |  6 ++
 .../spi/DatabaseProtocolFrontendEngine.java        |  8 +++
 7 files changed, 106 insertions(+), 54 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyDataSourceConfigurationSwapper.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyDataSourceConfigurationSwapper.java
index ad9916a..70e07f2 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyDataSourceConfigurationSwapper.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyDataSourceConfigurationSwapper.java
@@ -43,7 +43,6 @@ public final class YamlProxyDataSourceConfigurationSwapper {
     
     private PoolConfiguration swapPoolConfiguration(final 
YamlProxyDataSourceConfiguration yamlConfig) {
         return new 
PoolConfiguration(yamlConfig.getConnectionTimeoutMilliseconds(), 
yamlConfig.getIdleTimeoutMilliseconds(),
-                yamlConfig.getMaxLifetimeMilliseconds(), 
yamlConfig.getMaxPoolSize(), yamlConfig.getMinPoolSize(), 
yamlConfig.getReadOnly(),
-                yamlConfig.getCustomPoolProps());
+                yamlConfig.getMaxLifetimeMilliseconds(), 
yamlConfig.getMaxPoolSize(), yamlConfig.getMinPoolSize(), 
yamlConfig.getReadOnly(), yamlConfig.getCustomPoolProps());
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index f611245..6b3e4d5 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -17,35 +17,26 @@
 
 package org.apache.shardingsphere.proxy.initializer;
 
-import com.google.common.base.Strings;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.db.protocol.CommonConstants;
-import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
-import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
-import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
 import org.apache.shardingsphere.infra.instance.definition.InstanceType;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderFactory;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
 import 
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
 import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
 import 
org.apache.shardingsphere.proxy.config.yaml.swapper.YamlProxyConfigurationSwapper;
-import org.apache.shardingsphere.proxy.database.DatabaseServerInfo;
+import org.apache.shardingsphere.proxy.version.ShardingSphereProxyVersion;
 import org.apache.shardingsphere.spi.singleton.SingletonSPIRegistry;
 
-import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 
 /**
  * Bootstrap initializer.
@@ -66,7 +57,7 @@ public final class BootstrapInitializer {
         ContextManager contextManager = createContextManager(yamlConfig, 
modeConfig, port);
         ProxyContext.getInstance().init(contextManager);
         contextManagerInitializedCallback(modeConfig, contextManager);
-        setDatabaseServerInfo();
+        ShardingSphereProxyVersion.setVersion(contextManager);
     }
     
     private ContextManager createContextManager(final YamlProxyConfiguration 
yamlConfig, final ModeConfiguration modeConfig, final int port) throws 
SQLException {
@@ -82,9 +73,9 @@ public final class BootstrapInitializer {
     }
     
     private void contextManagerInitializedCallback(final ModeConfiguration 
modeConfig, final ContextManager contextManager) {
-        Map<String, ContextManagerLifecycleListener> listenerMap = 
SingletonSPIRegistry.getTypedSingletonInstancesMap(ContextManagerLifecycleListener.class);
-        log.info("listenerMap.keySet={}", listenerMap.keySet());
-        for (Entry<String, ContextManagerLifecycleListener> entry : 
listenerMap.entrySet()) {
+        Map<String, ContextManagerLifecycleListener> listeners = 
SingletonSPIRegistry.getTypedSingletonInstancesMap(ContextManagerLifecycleListener.class);
+        log.info("listeners.keySet={}", listeners.keySet());
+        for (Entry<String, ContextManagerLifecycleListener> entry : 
listeners.entrySet()) {
             try {
                 entry.getValue().onInitialized(modeConfig, contextManager);
                 // CHECKSTYLE:OFF
@@ -94,37 +85,4 @@ public final class BootstrapInitializer {
             }
         }
     }
-    
-    private void setDatabaseServerInfo() {
-        CommonConstants.PROXY_VERSION.set(getShardingSphereVersion());
-        findBackendDataSource().ifPresent(dataSourceSample -> {
-            DatabaseServerInfo databaseServerInfo = new 
DatabaseServerInfo(dataSourceSample);
-            log.info(databaseServerInfo.toString());
-            switch (databaseServerInfo.getDatabaseName()) {
-                case "MySQL":
-                    
MySQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
-                    break;
-                case "PostgreSQL":
-                    
PostgreSQLServerInfo.setServerVersion(databaseServerInfo.getDatabaseVersion());
-                    break;
-                default:
-            }
-        });
-    }
-    
-    private String getShardingSphereVersion() {
-        String result = ShardingSphereVersion.VERSION;
-        if (!ShardingSphereVersion.IS_SNAPSHOT || 
Strings.isNullOrEmpty(ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV)) {
-            return result;
-        }
-        result += ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "";
-        result += "-" + ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV;
-        return result;
-    }
-    
-    private Optional<DataSource> findBackendDataSource() {
-        MetaDataContexts metaDataContexts = 
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
-        Optional<ShardingSphereMetaData> metaData = 
metaDataContexts.getMetaDataMap().values().stream().filter(ShardingSphereMetaData::isComplete).findFirst();
-        return metaData.flatMap(optional -> 
optional.getResource().getDataSources().values().stream().findFirst());
-    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
new file mode 100644
index 0000000..529b37e
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java
@@ -0,0 +1,69 @@
+/*
+ * 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 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.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+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.Optional;
+
+/**
+ * ShardingSphere-Proxy version.
+ */
+@Slf4j
+public final class ShardingSphereProxyVersion {
+    
+    /**
+     * Set version.
+     * 
+     * @param contextManager context manager
+     */
+    public static void setVersion(final ContextManager contextManager) {
+        
CommonConstants.PROXY_VERSION.set(ShardingSphereProxyVersion.getProxyVersion());
+        Optional<DataSource> sampleDataSource = 
findSampleBackendDataSource(contextManager);
+        if (!sampleDataSource.isPresent()) {
+            return;
+        }
+        DatabaseServerInfo databaseServerInfo = new 
DatabaseServerInfo(sampleDataSource.get());
+        log.info(databaseServerInfo.toString());
+        
DatabaseProtocolFrontendEngineFactory.newInstance(DatabaseTypeRegistry.getTrunkDatabaseType(databaseServerInfo.getDatabaseName())).setDatabaseVersion(databaseServerInfo.getDatabaseVersion());
+    }
+    
+    private static String getProxyVersion() {
+        String result = ShardingSphereVersion.VERSION;
+        if (!ShardingSphereVersion.IS_SNAPSHOT || 
Strings.isNullOrEmpty(ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV)) {
+            return result;
+        }
+        result += ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "";
+        result += "-" + ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV;
+        return result;
+    }
+    
+    private static Optional<DataSource> findSampleBackendDataSource(final 
ContextManager contextManager) {
+        Optional<ShardingSphereMetaData> metaData = 
contextManager.getMetaDataContexts().getMetaDataMap().values().stream().filter(ShardingSphereMetaData::isComplete).findFirst();
+        return metaData.flatMap(optional -> 
optional.getResource().getDataSources().values().stream().findFirst());
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
index ee2d8a2..f544e1c 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.mysql;
 import lombok.Getter;
 import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
 import 
org.apache.shardingsphere.db.protocol.mysql.codec.MySQLPacketCodecEngine;
+import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
 import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.MySQLPreparedStatementRegistry;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
@@ -45,6 +46,11 @@ public final class MySQLFrontendEngine implements 
DatabaseProtocolFrontendEngine
     private final DatabasePacketCodecEngine<MySQLPacket> codecEngine = new 
MySQLPacketCodecEngine();
     
     @Override
+    public void setDatabaseVersion(final String databaseVersion) {
+        MySQLServerInfo.setServerVersion(databaseVersion);
+    }
+    
+    @Override
     public void release(final ConnectionSession connectionSession) {
         
MySQLPreparedStatementRegistry.getInstance().unregisterConnection(connectionSession.getConnectionId());
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
index 263d580..d42dd61 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/OpenGaussFrontendEngine.java
@@ -17,8 +17,10 @@
 
 package org.apache.shardingsphere.proxy.frontend.opengauss;
 
+import lombok.AccessLevel;
 import lombok.Getter;
 import 
org.apache.shardingsphere.db.protocol.opengauss.codec.OpenGaussPacketCodecEngine;
+import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.proxy.frontend.context.FrontendContext;
 import 
org.apache.shardingsphere.proxy.frontend.opengauss.authentication.OpenGaussAuthenticationEngine;
@@ -29,18 +31,17 @@ import 
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngi
 /**
  * Frontend engine for openGauss.
  */
+@Getter
 public final class OpenGaussFrontendEngine implements 
DatabaseProtocolFrontendEngine {
     
+    @Getter(AccessLevel.NONE)
     private final PostgreSQLFrontendEngine postgreSQLFrontendEngine = new 
PostgreSQLFrontendEngine();
     
-    @Getter
-    private final OpenGaussPacketCodecEngine codecEngine = new 
OpenGaussPacketCodecEngine();
+    private final OpenGaussAuthenticationEngine authenticationEngine = new 
OpenGaussAuthenticationEngine();
     
-    @Getter
     private final OpenGaussCommandExecuteEngine commandExecuteEngine = new 
OpenGaussCommandExecuteEngine();
     
-    @Getter
-    private final OpenGaussAuthenticationEngine authenticationEngine = new 
OpenGaussAuthenticationEngine();
+    private final OpenGaussPacketCodecEngine codecEngine = new 
OpenGaussPacketCodecEngine();
     
     @Override
     public FrontendContext getFrontendContext() {
@@ -48,6 +49,11 @@ public final class OpenGaussFrontendEngine implements 
DatabaseProtocolFrontendEn
     }
     
     @Override
+    public void setDatabaseVersion(final String databaseVersion) {
+        PostgreSQLServerInfo.setServerVersion(databaseVersion);
+    }
+    
+    @Override
     public void release(final ConnectionSession connectionSession) {
         postgreSQLFrontendEngine.release(connectionSession);
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index f73922d..198b8fc 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.postgresql;
 import lombok.Getter;
 import org.apache.shardingsphere.db.protocol.codec.DatabasePacketCodecEngine;
 import 
org.apache.shardingsphere.db.protocol.postgresql.codec.PostgreSQLPacketCodecEngine;
+import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.PostgreSQLPacket;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLPreparedStatementRegistry;
 import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
@@ -46,6 +47,11 @@ public final class PostgreSQLFrontendEngine implements 
DatabaseProtocolFrontendE
     private final DatabasePacketCodecEngine<PostgreSQLPacket> codecEngine = 
new PostgreSQLPacketCodecEngine();
     
     @Override
+    public void setDatabaseVersion(final String databaseVersion) {
+        PostgreSQLServerInfo.setServerVersion(databaseVersion);
+    }
+    
+    @Override
     public void release(final ConnectionSession connectionSession) {
         
PostgreSQLPreparedStatementRegistry.getInstance().unregister(connectionSession.getConnectionId());
         
PostgreSQLConnectionContextRegistry.getInstance().remove(connectionSession.getConnectionId());
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
index d3d95cd..f23205a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/spi/DatabaseProtocolFrontendEngine.java
@@ -30,6 +30,14 @@ import 
org.apache.shardingsphere.infra.database.type.DatabaseTypeAwareSPI;
 public interface DatabaseProtocolFrontendEngine extends DatabaseTypeAwareSPI {
     
     /**
+     * Set database version.
+     * 
+     * @param databaseVersion database version
+     */
+    default void setDatabaseVersion(String databaseVersion) {
+    }
+    
+    /**
      * Get frontend context.
      *
      * @return frontend context

Reply via email to