You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/01/20 15:25:10 UTC

[shardingsphere] branch master updated: [DistSQL] Support for `show instance mode` syntax. (#14905)

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

jianglongtao 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 2e98584  [DistSQL] Support for `show instance mode` syntax. (#14905)
2e98584 is described below

commit 2e985840130924ad734c3843b1bd4968aad48498
Author: lanchengx <52...@users.noreply.github.com>
AuthorDate: Thu Jan 20 23:23:23 2022 +0800

    [DistSQL] Support for `show instance mode` syntax. (#14905)
    
    * Support for `show instance mode` syntax.
    
    * Adjust display.
    
    * Add unit test.
    
    * Adjust case.
    
    * Adjust unit tests, comment.
    
    * Remove blank line.
---
 .../src/main/antlr4/imports/Keyword.g4             |  4 +
 .../src/main/antlr4/imports/RALStatement.g4        |  4 +
 .../parser/autogen/CommonDistSQLStatement.g4       |  1 +
 .../core/common/CommonDistSQLStatementVisitor.java |  7 ++
 .../common/show/ShowInstanceModeStatement.java}    | 38 +++-------
 .../common/show/ShowStatementExecutorFactory.java  |  5 ++
 .../show/executor/ShowInstanceModeExecutor.java    | 87 ++++++++++++++++++++++
 .../common/show/ShowInstanceModeExecutorTest.java  | 86 +++++++++++++++++++++
 .../common/show/ShowTableMetadataExecutorTest.java |  2 +-
 9 files changed, 204 insertions(+), 30 deletions(-)

diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
index ce2de53..4df621f 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
@@ -218,6 +218,10 @@ CONCURRENCY_LEVEL
 TYPE
     : T Y P E
     ;
+    
+MODE
+    : M O D E
+    ;
 
 COUNT
     : C O U N T
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
index 786599b..35d905f 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
@@ -75,6 +75,10 @@ alterSQLParserRule
     : ALTER SQL_PARSER RULE sqlParserRuleDefinition
     ;
 
+showInstanceMode
+    : SHOW INSTANCE MODE
+    ;
+
 transactionRuleDefinition
     : LP DEFAULT EQ defaultType COMMA providerDefinition
     ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4
index 563a82b..d9cbd21 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4
@@ -31,6 +31,7 @@ execute
     | enableInstance
     | disableInstance
     | showInstance
+    | showInstanceMode
     | showSingleTable
     | showSingleTableRules
     | createDefaultSingleTableRule
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
index c873657..3c71f96 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
@@ -45,6 +45,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementPa
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowAllVariablesContext;
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowAuthorityRuleContext;
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowInstanceContext;
+import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowInstanceModeContext;
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowResourcesContext;
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowSQLParserRuleContext;
 import org.apache.shardingsphere.distsql.parser.autogen.CommonDistSQLStatementParser.ShowSingleTableContext;
@@ -65,6 +66,7 @@ import org.apache.shardingsphere.distsql.parser.statement.ral.common.set.SetInst
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.set.SetVariableStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowAllVariablesStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowAuthorityRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowInstanceModeStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowInstanceStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowSQLParserRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowTableMetadataStatement;
@@ -141,6 +143,11 @@ public final class CommonDistSQLStatementVisitor extends CommonDistSQLStatementB
     }
     
     @Override
+    public ASTNode visitShowInstanceMode(final ShowInstanceModeContext ctx) {
+        return new ShowInstanceModeStatement();
+    }
+    
+    @Override
     public ASTNode visitEnableInstance(final EnableInstanceContext ctx) {
         return buildSetInstanceStatusStatement(ctx.ENABLE().getText().toUpperCase(), ctx.instanceDefination(), ctx.instanceId());
     }
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/common/show/ShowInstanceModeStatement.java
similarity index 54%
copy from shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4
copy to shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/common/show/ShowInstanceModeStatement.java
index 563a82b..22e1275 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/CommonDistSQLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/common/show/ShowInstanceModeStatement.java
@@ -15,34 +15,14 @@
  * limitations under the License.
  */
 
-grammar CommonDistSQLStatement;
+package org.apache.shardingsphere.distsql.parser.statement.ral.common.show;
 
-import Symbol, RALStatement, RDLStatement, RQLStatement;
+import lombok.Getter;
+import org.apache.shardingsphere.distsql.parser.statement.ral.common.ShowDistSQLStatement;
 
-execute
-    : (addResource
-    | alterResource
-    | dropResource
-    | showResources
-    | setVariable
-    | showVariable
-    | showAllVariables
-    | clearHint
-    | enableInstance
-    | disableInstance
-    | showInstance
-    | showSingleTable
-    | showSingleTableRules
-    | createDefaultSingleTableRule
-    | alterDefaultSingleTableRule
-    | dropDefaultSingleTableRule
-    | refreshTableMetadata
-    | showTableMetadata
-    | showSQLParserRule
-    | alterSQLParserRule
-    | showAuthorityRule
-    | showTransactionRule
-    | alterTransactionRule
-    | countSchemaRules
-    ) SEMI?
-    ;
+/**
+ * Show instance mode statement.
+ */
+@Getter
+public final class ShowInstanceModeStatement extends ShowDistSQLStatement {
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowStatementExecutorFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowStatementExecutorFactory.java
index f209fd8..f2989c1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowStatementExecutorFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowStatementExecutorFactory.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.ShowDistSQLStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowAllVariablesStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowAuthorityRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowInstanceModeStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowInstanceStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowSQLParserRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.common.show.ShowTableMetadataStatement;
@@ -29,6 +30,7 @@ import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowAllVariablesExecutor;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowAuthorityRuleExecutor;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowInstanceExecutor;
+import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowInstanceModeExecutor;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowReadwriteSplittingReadResourcesExecutor;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowSQLParserRuleExecutor;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowTableMetadataExecutor;
@@ -76,6 +78,9 @@ public final class ShowStatementExecutorFactory {
         if (sqlStatement instanceof ShowTableMetadataStatement) {
             return new ShowTableMetadataExecutor((ShowTableMetadataStatement) sqlStatement, connectionSession);
         }
+        if (sqlStatement instanceof ShowInstanceModeStatement) {
+            return new ShowInstanceModeExecutor();
+        }
         throw new UnsupportedOperationException(sqlStatement.getClass().getCanonicalName());
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/executor/ShowInstanceModeExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/executor/ShowInstanceModeExecutor.java
new file mode 100644
index 0000000..26fddd0
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/executor/ShowInstanceModeExecutor.java
@@ -0,0 +1,87 @@
+/*
+ * 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.backend.text.distsql.ral.common.show.executor;
+
+import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.infra.properties.PropertiesConverter;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.persist.PersistRepository;
+import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader;
+import org.apache.shardingsphere.sharding.merge.dal.common.MultipleLocalDataMergedResult;
+
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Show instance mode executor.
+ */
+public final class ShowInstanceModeExecutor extends AbstractShowExecutor {
+    
+    private static final String ID = "id";
+    
+    private static final String TYPE = "type";
+    
+    private static final String REPOSITORY = "repository";
+    
+    private static final String PROPS = "props";
+    
+    @Override
+    protected List<QueryHeader> createQueryHeaders() {
+        return Arrays.asList(
+                new QueryHeader("", "", ID, ID, Types.VARCHAR, "VARCHAR", 64, 0, false, false, false, false),
+                new QueryHeader("", "", TYPE, TYPE, Types.VARCHAR, "VARCHAR", 64, 0, false, false, false, false),
+                new QueryHeader("", "", REPOSITORY, REPOSITORY, Types.VARCHAR, "VARCHAR", 64, 0, false, false, false, false),
+                new QueryHeader("", "", PROPS, PROPS, Types.VARCHAR, "VARCHAR", 1024, 0, false, false, false, false)
+        );
+    }
+    
+    @Override
+    protected MergedResult createMergedResult() {
+        ContextManager contextManager = ProxyContext.getInstance().getContextManager();
+        MetaDataPersistService persistService = contextManager.getMetaDataContexts().getMetaDataPersistService().orElse(null);
+        ComputeNodeInstance instance = contextManager.getInstanceContext().getInstance();
+        if (null == persistService || null == persistService.getRepository()) {
+            return new MultipleLocalDataMergedResult(Collections.emptyList());
+        }
+        return new MultipleLocalDataMergedResult(buildRows(instance, persistService.getRepository()));
+    }
+    
+    private Collection<List<Object>> buildRows(final ComputeNodeInstance instance, final PersistRepository persistService) {
+        // TODO Add display of overwrite after metadata save overwrite.
+        return Collections.singleton(Arrays.asList(instance.getInstanceDefinition().getInstanceId().getId(), getTypeName(persistService), persistService.getType(),
+                PropertiesConverter.convert(persistService.getProps())));
+    }
+    
+    private String getTypeName(final PersistRepository persistRepository) {
+        if (persistRepository instanceof ClusterPersistRepository) {
+            return "Cluster";
+        } else if (persistRepository instanceof StandalonePersistRepository) {
+            return "Standalone";
+        } else {
+            return "";
+        }
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowInstanceModeExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowInstanceModeExecutorTest.java
new file mode 100644
index 0000000..170609a
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowInstanceModeExecutorTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.backend.text.distsql.ral.common.show;
+
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.persist.PersistRepository;
+import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
+import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.show.executor.ShowInstanceModeExecutor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShowInstanceModeExecutorTest {
+    
+    @Test
+    public void assertExecutor() throws SQLException {
+        ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+        Optional<MetaDataPersistService> metaDataPersistService = Optional.of(createMetaDataPersistService());
+        when(contextManager.getMetaDataContexts().getMetaDataPersistService()).thenReturn(metaDataPersistService);
+        InstanceContext instanceContext = createInstanceContext();
+        when(contextManager.getInstanceContext()).thenReturn(instanceContext);
+        ShowInstanceModeExecutor executor = new ShowInstanceModeExecutor();
+        ProxyContext.getInstance().init(contextManager);
+        executor.execute();
+        executor.next();
+        QueryResponseRow queryResponseRow = executor.getQueryResponseRow();
+        ArrayList<Object> data = new ArrayList<>(queryResponseRow.getData());
+        assertThat(data.size(), is(4));
+        assertThat(data.get(0), is("127.0.0.1@3309"));
+        assertThat(data.get(1), is("Cluster"));
+        assertThat(data.get(2), is("ZooKeeper"));
+        assertThat(data.get(3), is("key=value"));
+    }
+    
+    private MetaDataPersistService createMetaDataPersistService() {
+        MetaDataPersistService result = mock(MetaDataPersistService.class);
+        PersistRepository repository = mock(ClusterPersistRepository.class, RETURNS_DEEP_STUBS);
+        when(result.getRepository()).thenReturn(repository);
+        when(repository.getType()).thenReturn("ZooKeeper");
+        when(repository.getProps()).thenReturn(createProperties("key", "value"));
+        return result;
+    }
+    
+    private InstanceContext createInstanceContext() {
+        InstanceContext result = mock(InstanceContext.class, RETURNS_DEEP_STUBS);
+        when(result.getInstance().getInstanceDefinition().getInstanceId().getId()).thenReturn("127.0.0.1@3309");
+        return result;
+    }
+    
+    private Properties createProperties(final String key, final String value) {
+        Properties result = new Properties();
+        result.put(key, value);
+        return result;
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowTableMetadataExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowTableMetadataExecutorTest.java
index 07b2c73..33101bc 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowTableMetadataExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/show/ShowTableMetadataExecutorTest.java
@@ -51,7 +51,7 @@ import static org.mockito.Mockito.when;
 public final class ShowTableMetadataExecutorTest {
     
     @Test
-    public void assertSetVariableExecutor() throws SQLException {
+    public void assertExecutor() throws SQLException {
         ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         when(contextManager.getMetaDataContexts().getAllSchemaNames()).thenReturn(Collections.singletonList("schema_name"));
         ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);