You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/12/11 12:49:02 UTC

[shardingsphere] branch master updated: Add configuration for frontend protocol of DatabaseType (#8580)

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

panjuan 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 e4a6a0d  Add configuration for frontend protocol of DatabaseType (#8580)
e4a6a0d is described below

commit e4a6a0d7771a6d7da118ae758e47a06371fa4722
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Dec 11 20:48:35 2020 +0800

    Add configuration for frontend protocol of DatabaseType (#8580)
    
    * Add FrontDatabaseType configuration
    
    * Fix test case
    
    * Add todo
---
 .../properties/ConfigurationPropertyKey.java       |  5 +++++
 .../frontend/netty/ServerHandlerInitializer.java   | 26 +++++++++++++++++-----
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
index a09db77..f687789 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
@@ -64,6 +64,11 @@ public enum ConfigurationPropertyKey implements TypedPropertyKey {
     QUERY_WITH_CIPHER_COLUMN("query-with-cipher-column", String.valueOf(Boolean.TRUE), boolean.class),
     
     /**
+     * Frontend database protocol type for ShardingSphere-Proxy.
+     */
+    PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE("proxy-frontend-database-protocol-type", "", String.class),
+    
+    /**
      * Flush threshold for every records from databases for ShardingSphere-Proxy.
      */
     PROXY_FRONTEND_FLUSH_THRESHOLD("proxy-frontend-flush-threshold", String.valueOf(128), int.class),
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
index 17f80c8..031042f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializer.java
@@ -22,12 +22,16 @@ import io.netty.channel.ChannelPipeline;
 import io.netty.channel.socket.SocketChannel;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.db.protocol.codec.PacketCodec;
+import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.frontend.protocol.DatabaseProtocolFrontendEngineFactory;
 import org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngine;
 
+import java.util.Optional;
+
 /**
  * Channel initializer.
  */
@@ -36,14 +40,26 @@ public final class ServerHandlerInitializer extends ChannelInitializer<SocketCha
     
     @Override
     protected void initChannel(final SocketChannel socketChannel) {
-        DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine = DatabaseProtocolFrontendEngineFactory.newInstance(getDatabaseType());
+        DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine = DatabaseProtocolFrontendEngineFactory.newInstance(getFrontDatabaseType());
         ChannelPipeline pipeline = socketChannel.pipeline();
         pipeline.addLast(new PacketCodec(databaseProtocolFrontendEngine.getCodecEngine()));
         pipeline.addLast(new FrontendChannelInboundHandler(databaseProtocolFrontendEngine));
     }
     
-    private DatabaseType getDatabaseType() {
-        // TODO Consider loading from configuration.
-        return ProxyContext.getInstance().getMetaDataContexts().getMetaDataMap().isEmpty() ? new MySQLDatabaseType() : ProxyContext.getInstance().getMetaDataContexts().getDatabaseType();
+    private DatabaseType getFrontDatabaseType() {
+        Optional<DatabaseType> configuredDatabaseType = findConfiguredDatabaseType();
+        if (configuredDatabaseType.isPresent()) {
+            return configuredDatabaseType.get();
+        }
+        if (ProxyContext.getInstance().getMetaDataContexts().getMetaDataMap().isEmpty()) {
+            throw new ShardingSphereConfigurationException("Can not find any configured data sources and database frontend protocol type.");
+        }
+        return ProxyContext.getInstance().getMetaDataContexts().getDatabaseType();
+    }
+    
+    // TODO check database type config error in ShardingSphereProxy class
+    private Optional<DatabaseType> findConfiguredDatabaseType() {
+        String configuredDatabaseType = ProxyContext.getInstance().getMetaDataContexts().getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
+        return configuredDatabaseType.isEmpty() ? Optional.empty() : Optional.of(DatabaseTypeRegistry.getTrunkDatabaseType(configuredDatabaseType));
     }
 }