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 2023/02/02 06:39:30 UTC

[shardingsphere] branch master updated: Replace `TransactionRuleResultSet` with `ShowTransactionRuleExecutor` (#23917)

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 0f0cd098fe3 Replace `TransactionRuleResultSet` with `ShowTransactionRuleExecutor` (#23917)
0f0cd098fe3 is described below

commit 0f0cd098fe3a3004c5d7e83e5e41737bb55282b1
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Thu Feb 2 19:39:22 2023 +1300

    Replace `TransactionRuleResultSet` with `ShowTransactionRuleExecutor` (#23917)
    
    * Replace `TransactionRuleResultSet` with `ShowTransactionRuleExecutor`
    
    * Replace `TransactionRuleResultSet` with `ShowTransactionRuleExecutor`
---
 .../handler/query/ShowTransactionRuleExecutor.java | 52 +++++++++++++++
 .../handler/query/TransactionRuleResultSet.java    | 75 ----------------------
 ...distsql.handler.ral.query.QueryableRALExecutor} |  2 +-
 ...t.java => ShowTransactionRuleExecutorTest.java} | 51 ++++++++++-----
 .../distsql/ral/RALBackendHandlerFactory.java      |  8 +--
 .../handler/ProxyBackendHandlerFactoryTest.java    |  3 +-
 6 files changed, 93 insertions(+), 98 deletions(-)

diff --git a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutor.java b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutor.java
new file mode 100644
index 00000000000..a2f69b9654a
--- /dev/null
+++ b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutor.java
@@ -0,0 +1,52 @@
+/*
+ * 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.transaction.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.ral.query.MetaDataRequiredQueryableRALExecutor;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
+import org.apache.shardingsphere.transaction.distsql.parser.statement.queryable.ShowTransactionRuleStatement;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Show transaction rule executor.
+ */
+public final class ShowTransactionRuleExecutor implements MetaDataRequiredQueryableRALExecutor<ShowTransactionRuleStatement> {
+    
+    @Override
+    public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereMetaData metaData, final ShowTransactionRuleStatement sqlStatement) {
+        TransactionRule rule = metaData.getGlobalRuleMetaData().getSingleRule(TransactionRule.class);
+        return Collections.singleton(new LocalDataQueryResultRow(rule.getDefaultType().name(), null != rule.getProviderType() ? rule.getProviderType() : "",
+                !rule.getProps().isEmpty() ? PropertiesConverter.convert(rule.getProps()) : ""));
+    }
+    
+    @Override
+    public Collection<String> getColumnNames() {
+        return Arrays.asList("default_type", "provider_type", "props");
+    }
+    
+    @Override
+    public String getType() {
+        return ShowTransactionRuleStatement.class.getName();
+    }
+}
diff --git a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSet.java b/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSet.java
deleted file mode 100644
index e9e229c85cb..00000000000
--- a/kernel/transaction/distsql/handler/src/main/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSet.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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.transaction.distsql.handler.query;
-
-import org.apache.shardingsphere.distsql.handler.resultset.GlobalRuleDistSQLResultSet;
-import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
-import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.transaction.distsql.parser.statement.queryable.ShowTransactionRuleStatement;
-import org.apache.shardingsphere.transaction.rule.TransactionRule;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-
-/**
- * Result set for transaction rule.
- */
-public final class TransactionRuleResultSet implements GlobalRuleDistSQLResultSet {
-    
-    private static final String DEFAULT_TYPE = "default_type";
-    
-    private static final String PROVIDER_TYPE = "provider_type";
-    
-    private static final String PROPS = "props";
-    
-    private Iterator<Collection<Object>> data;
-    
-    @Override
-    public void init(final ShardingSphereRuleMetaData globalRuleMetaData, final SQLStatement sqlStatement) {
-        TransactionRule rule = globalRuleMetaData.getSingleRule(TransactionRule.class);
-        data = buildData(rule).iterator();
-    }
-    
-    private Collection<Collection<Object>> buildData(final TransactionRule rule) {
-        return Collections.singleton(Arrays.asList(
-                rule.getDefaultType().name(), null != rule.getProviderType() ? rule.getProviderType() : "", !rule.getProps().isEmpty() ? PropertiesConverter.convert(rule.getProps()) : ""));
-    }
-    
-    @Override
-    public Collection<String> getColumnNames() {
-        return Arrays.asList(DEFAULT_TYPE, PROVIDER_TYPE, PROPS);
-    }
-    
-    @Override
-    public boolean next() {
-        return data.hasNext();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() {
-        return data.next();
-    }
-    
-    @Override
-    public String getType() {
-        return ShowTransactionRuleStatement.class.getName();
-    }
-}
diff --git a/kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet b/kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor
similarity index 89%
rename from kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
rename to kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor
index d86ddd03302..6afbbd147bc 100644
--- a/kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ b/kernel/transaction/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.transaction.distsql.handler.query.TransactionRuleResultSet
+org.apache.shardingsphere.transaction.distsql.handler.query.ShowTransactionRuleExecutor
diff --git a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSetTest.java b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
similarity index 57%
rename from kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSetTest.java
rename to kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
index df943fa208c..e5955920246 100644
--- a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/TransactionRuleResultSetTest.java
+++ b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/query/ShowTransactionRuleExecutorTest.java
@@ -17,6 +17,9 @@
 
 package org.apache.shardingsphere.transaction.distsql.handler.query;
 
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
@@ -28,6 +31,7 @@ import org.junit.Test;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -35,32 +39,47 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
-public final class TransactionRuleResultSetTest {
+public final class ShowTransactionRuleExecutorTest {
     
     @Test
     public void assertExecuteWithXA() {
-        TransactionRuleResultSet resultSet = new TransactionRuleResultSet();
+        ShowTransactionRuleExecutor executor = new ShowTransactionRuleExecutor();
         ShardingSphereRuleMetaData ruleMetaData = mockGlobalRuleMetaData("XA", "Atomikos", PropertiesBuilder.build(new Property("host", "127.0.0.1"), new Property("databaseName", "jbossts")));
-        resultSet.init(ruleMetaData, mock(ShowTransactionRuleStatement.class));
-        Collection<Object> actual = resultSet.getRowData();
-        Iterator<Object> rowData = actual.iterator();
-        assertThat(actual.size(), is(3));
-        assertThat(rowData.next(), is("XA"));
-        assertThat(rowData.next(), is("Atomikos"));
-        String props = (String) rowData.next();
-        assertTrue(props.contains("host=127.0.0.1"));
+        ShardingSphereMetaData metaData = new ShardingSphereMetaData(new LinkedHashMap<>(), ruleMetaData, new ConfigurationProperties(new Properties()));
+        Collection<LocalDataQueryResultRow> actual = executor.getRows(metaData, mock(ShowTransactionRuleStatement.class));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("XA"));
+        assertThat(row.getCell(2), is("Atomikos"));
+        String props = (String) row.getCell(3);
         assertTrue(props.contains("databaseName=jbossts"));
+        assertTrue(props.contains("host=127.0.0.1"));
     }
     
     @Test
     public void assertExecuteWithLocal() {
-        TransactionRuleResultSet resultSet = new TransactionRuleResultSet();
+        ShowTransactionRuleExecutor executor = new ShowTransactionRuleExecutor();
         ShardingSphereRuleMetaData ruleMetaData = mockGlobalRuleMetaData("LOCAL", null, new Properties());
-        resultSet.init(ruleMetaData, mock(ShowTransactionRuleStatement.class));
-        Collection<Object> actual = resultSet.getRowData();
-        assertThat(actual.size(), is(3));
-        assertTrue(actual.contains("LOCAL"));
-        assertTrue(actual.contains(""));
+        ShardingSphereMetaData metaData = new ShardingSphereMetaData(new LinkedHashMap<>(), ruleMetaData, new ConfigurationProperties(new Properties()));
+        Collection<LocalDataQueryResultRow> actual = executor.getRows(metaData, mock(ShowTransactionRuleStatement.class));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("LOCAL"));
+        assertThat(row.getCell(2), is(""));
+        assertThat(row.getCell(3), is(""));
+    }
+    
+    @Test
+    public void assertGetColumnNames() {
+        ShowTransactionRuleExecutor executor = new ShowTransactionRuleExecutor();
+        Collection<String> columns = executor.getColumnNames();
+        assertThat(columns.size(), is(3));
+        Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("default_type"));
+        assertThat(iterator.next(), is("provider_type"));
+        assertThat(iterator.next(), is("props"));
     }
     
     private ShardingSphereRuleMetaData mockGlobalRuleMetaData(final String defaultType, final String providerType, final Properties props) {
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index 099376d1535..4a57e6fc9ad 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -103,6 +103,10 @@ public final class RALBackendHandlerFactory {
      * @return created instance
      */
     public static ProxyBackendHandler newInstance(final RALStatement sqlStatement, final ConnectionSession connectionSession) {
+        // TODO delete other if branches after replacing all query handlers with QueryableRALBackendHandler
+        if (TypedSPILoader.contains(QueryableRALExecutor.class, sqlStatement.getClass().getName())) {
+            return new QueryableRALBackendHandler<>(sqlStatement, connectionSession);
+        }
         if (sqlStatement instanceof HintRALStatement) {
             return new HintRALBackendHandler((HintRALStatement) sqlStatement, connectionSession);
         }
@@ -120,10 +124,6 @@ public final class RALBackendHandlerFactory {
         if (sqlStatement instanceof UpdatableGlobalRuleRALStatement) {
             return new UpdatableGlobalRuleRALBackendHandler(sqlStatement, TypedSPILoader.getService(GlobalRuleRALUpdater.class, sqlStatement.getClass().getName()));
         }
-        // TODO delete other if branches after replacing all query handlers with QueryableRALBackendHandler
-        if (TypedSPILoader.contains(QueryableRALExecutor.class, sqlStatement.getClass().getName())) {
-            return new QueryableRALBackendHandler<>(sqlStatement, connectionSession);
-        }
         return createRALBackendHandler(sqlStatement, connectionSession);
     }
     
diff --git a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
index b0eb9332308..c8fc5f31f2d 100644
--- a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
+++ b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.handler.admin.DatabaseAdminQueryBackendHandler;
 import org.apache.shardingsphere.proxy.backend.handler.admin.DatabaseAdminUpdateBackendHandler;
 import org.apache.shardingsphere.proxy.backend.handler.data.impl.UnicastDatabaseBackendHandler;
-import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.QueryableGlobalRuleRALBackendHandler;
 import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.QueryableRALBackendHandler;
 import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.hint.HintRALBackendHandler;
 import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.SetDistVariableHandler;
@@ -250,7 +249,7 @@ public final class ProxyBackendHandlerFactoryTest extends ProxyContextRestorer {
         when(connectionSession.getTransactionStatus().isInTransaction()).thenReturn(true);
         String sql = "SHOW TRANSACTION RULE;";
         ProxyBackendHandler actual = ProxyBackendHandlerFactory.newInstance(databaseType, sql, connectionSession);
-        assertThat(actual, instanceOf(QueryableGlobalRuleRALBackendHandler.class));
+        assertThat(actual, instanceOf(QueryableRALBackendHandler.class));
     }
     
     @Test