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 2021/01/11 11:14:20 UTC

[shardingsphere] branch master updated: format the result of 'show resources' (#8937)

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 bda94fb  format the result of 'show resources' (#8937)
bda94fb is described below

commit bda94fbb8f95ea04e430dd3a6b0cea82438c8c2e
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Mon Jan 11 19:13:46 2021 +0800

    format the result of 'show resources' (#8937)
    
    * format the result of 'show resources'
    
    * Update DataSourcesQueryBackendHandler.java
    
    * fix
    
    * fix
    
    * add unit test
    
    * fix
---
 .../rql/impl/DataSourcesQueryBackendHandler.java   | 45 +++++++++++++++++++---
 .../text/distsql/RDLBackendHandlerFactoryTest.java | 19 +++++++++
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DataSourcesQueryBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DataSourcesQueryBackendHandler.java
index 8862ac7..d25b160 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DataSourcesQueryBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DataSourcesQueryBackendHandler.java
@@ -17,10 +17,11 @@
 
 package org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl;
 
+import com.google.gson.Gson;
 import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
@@ -32,7 +33,10 @@ import org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
 import java.sql.Types;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -42,20 +46,36 @@ public final class DataSourcesQueryBackendHandler extends SchemaRequiredBackendH
     
     private Map<String, DataSourceParameter> dataSourceParameterMap;
     
+    private final String schema;
+    
     private Iterator<String> dataSourceNames;
     
     public DataSourcesQueryBackendHandler(final ShowResourcesStatement sqlStatement, final BackendConnection backendConnection) {
         super(sqlStatement, backendConnection);
+        if (sqlStatement.getSchema().isPresent()) {
+            schema = sqlStatement.getSchema().get().getIdentifier().getValue();
+        } else {
+            schema = backendConnection.getSchemaName();
+        }
     }
     
     @Override
     public ResponseHeader execute(final String schemaName, final ShowResourcesStatement sqlStatement) {
-        QueryHeader nameQueryHeader = new QueryHeader(schemaName, "", "name", "name", Types.CHAR, "CHAR", 255, 0, false, false, false, false);
-        QueryHeader contentQueryHeader = new QueryHeader(schemaName, "", "data source", "data source", Types.CHAR, "CHAR", 255, 0, false, false, false, false);
         dataSourceParameterMap = DataSourceParameterConverter.getDataSourceParameterMap(
-                DataSourceConverter.getDataSourceConfigurationMap(ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources()));
+                DataSourceConverter.getDataSourceConfigurationMap(ProxyContext.getInstance().getMetaData(schema).getResource().getDataSources()));
         dataSourceNames = dataSourceParameterMap.keySet().iterator();
-        return new QueryResponseHeader(Arrays.asList(nameQueryHeader, contentQueryHeader));
+        return new QueryResponseHeader(generateResponseHeader());
+    }
+    
+    private List<QueryHeader> generateResponseHeader() {
+        List<QueryHeader> result = new LinkedList();
+        result.add(new QueryHeader(schema, "", "name", "name", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+        result.add(new QueryHeader(schema, "", "type", "type", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+        result.add(new QueryHeader(schema, "", "host", "host", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+        result.add(new QueryHeader(schema, "", "port", "port", Types.BIGINT, "BIGINT", 255, 0, false, false, false, false));
+        result.add(new QueryHeader(schema, "", "db", "db", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+        result.add(new QueryHeader(schema, "", "attribute", "attribute", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+        return result;
     }
     
     @Override
@@ -66,6 +86,19 @@ public final class DataSourcesQueryBackendHandler extends SchemaRequiredBackendH
     @Override
     public Collection<Object> getRowData() {
         String dataSourceName = dataSourceNames.next();
-        return Arrays.asList(dataSourceName, YamlEngine.marshal(dataSourceParameterMap.get(dataSourceName)));
+        DataSourceMetaData dataSourceMetaData = ProxyContext.getInstance().getMetaData(schema).getResource().getDataSourcesMetaData().getDataSourceMetaData(dataSourceName);
+        Map<Object, Object> attributeMap = new HashMap();
+        attributeMap.put("connectionTimeoutMilliseconds", dataSourceParameterMap.get(dataSourceName).getConnectionTimeoutMilliseconds());
+        attributeMap.put("idleTimeoutMilliseconds", dataSourceParameterMap.get(dataSourceName).getIdleTimeoutMilliseconds());
+        attributeMap.put("maxLifetimeMilliseconds", dataSourceParameterMap.get(dataSourceName).getMaxLifetimeMilliseconds());
+        attributeMap.put("maxPoolSize", dataSourceParameterMap.get(dataSourceName).getMaxPoolSize());
+        attributeMap.put("minPoolSize", dataSourceParameterMap.get(dataSourceName).getMinPoolSize());
+        attributeMap.put("maintenanceIntervalMilliseconds", dataSourceParameterMap.get(dataSourceName).getMaintenanceIntervalMilliseconds());
+        attributeMap.put("readOnly", dataSourceParameterMap.get(dataSourceName).isReadOnly());
+        String type = ProxyContext.getInstance().getMetaData(schema).getResource().getDatabaseType().getName();
+        String host = dataSourceMetaData.getHostName();
+        int port = dataSourceMetaData.getPort();
+        String db = dataSourceMetaData.getCatalog();
+        return Arrays.asList(dataSourceName, type, host, port, db, (new Gson()).toJson(attributeMap));
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
index ba90c10..cfb4d40 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.Create
 import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
 import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -36,9 +37,11 @@ import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleNotExistedException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.RDLBackendHandlerFactory;
+import org.apache.shardingsphere.proxy.backend.text.distsql.rql.RQLBackendHandlerFactory;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateDatabaseStatement;
@@ -256,6 +259,22 @@ public final class RDLBackendHandlerFactoryTest {
         assertThat(response, instanceOf(UpdateResponseHeader.class));
     }
     
+    @Test(expected = ClassCastException.class)
+    public void assertExecuteShowResourceContext() throws SQLException {
+        BackendConnection connection = mock(BackendConnection.class);
+        when(connection.getSchemaName()).thenReturn("schema");
+        try {
+            RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(ShowResourcesStatement.class), connection);
+        } catch (final SQLException ex) {
+            assertThat(ex.getMessage(), is("No Registry center to execute `ShowResourcesStatement` SQL"));
+        }
+        setGovernanceMetaDataContexts(true);
+        Optional<TextProtocolBackendHandler> rdlBackendHandler = RQLBackendHandlerFactory.newInstance(mock(ShowResourcesStatement.class), connection);
+        assertTrue(rdlBackendHandler.isPresent());
+        ResponseHeader response = rdlBackendHandler.get().execute();
+        assertThat(response, instanceOf(QueryResponseHeader.class));
+    }
+    
     @SneakyThrows(ReflectiveOperationException.class)
     private void setGovernanceMetaDataContexts(final boolean isGovernance) {
         Field metaDataContexts = ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");