You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/12/03 06:33:28 UTC

[shardingsphere] branch master updated: Merge QueryBackendHandler and UnicastBackendHandler (#8487)

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

zhangyonglun 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 02c473e  Merge QueryBackendHandler and UnicastBackendHandler (#8487)
02c473e is described below

commit 02c473ee3b737999fcd28f28bcb71ad4ebe30673
Author: Liang Zhang <te...@163.com>
AuthorDate: Thu Dec 3 14:33:06 2020 +0800

    Merge QueryBackendHandler and UnicastBackendHandler (#8487)
    
    * Remove useless null judge for QueryBackendHandler
    
    * Merge QueryBackendHandler and UnicastBackendHandler
---
 .../text/TextProtocolBackendHandlerFactory.java    |   2 +-
 .../text/admin/DALBackendHandlerFactory.java       |   3 +-
 .../backend/text/admin/UnicastBackendHandler.java  |  78 --------------
 .../backend/text/query/QueryBackendHandler.java    |  19 +++-
 .../TextProtocolBackendHandlerFactoryTest.java     |   9 +-
 .../text/admin/DALBackendHandlerFactoryTest.java   |  11 +-
 .../text/admin/UnicastBackendHandlerTest.java      | 115 ---------------------
 7 files changed, 31 insertions(+), 206 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index 630f454..85a6daf 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -70,6 +70,6 @@ public final class TextProtocolBackendHandlerFactory {
         if (sqlStatement instanceof DALStatement) {
             return DALBackendHandlerFactory.newInstance((DALStatement) sqlStatement, sql, backendConnection);
         }
-        return new QueryBackendHandler(sqlStatement, sql, backendConnection);
+        return new QueryBackendHandler(sqlStatement, sql, backendConnection, true);
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactory.java
index cbcd35b..47e3daf 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactory.java
@@ -21,6 +21,7 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import org.apache.shardingsphere.proxy.backend.text.query.QueryBackendHandler;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
@@ -54,6 +55,6 @@ public final class DALBackendHandlerFactory {
         if (dalStatement instanceof SetStatement) {
             return new BroadcastBackendHandler(dalStatement, sql, backendConnection);
         }
-        return new UnicastBackendHandler(dalStatement, sql, backendConnection);
+        return new QueryBackendHandler(dalStatement, sql, backendConnection, false);
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandler.java
deleted file mode 100644
index e99585c..0000000
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandler.java
+++ /dev/null
@@ -1,78 +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.proxy.backend.text.admin;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
-import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
-import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
-import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistsException;
-import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
-import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Map;
-
-/**
- * Backend handler for unicast.
- */
-@RequiredArgsConstructor
-public final class UnicastBackendHandler implements TextProtocolBackendHandler {
-    
-    private final DatabaseCommunicationEngineFactory databaseCommunicationEngineFactory = DatabaseCommunicationEngineFactory.getInstance();
-    
-    private final SQLStatement sqlStatement;
-    
-    private final String sql;
-    
-    private final BackendConnection backendConnection;
-    
-    private DatabaseCommunicationEngine databaseCommunicationEngine;
-    
-    @Override
-    public ResponseHeader execute() throws SQLException {
-        if (null == backendConnection.getSchemaName()) {
-            Map<String, ShardingSphereMetaData> metaDataMap = ProxyContext.getInstance().getMetaDataContexts().getMetaDataMap();
-            if (metaDataMap.isEmpty()) {
-                throw new NoDatabaseSelectedException();
-            }
-            if (!metaDataMap.values().iterator().next().isComplete()) {
-                throw new RuleNotExistsException();
-            }
-            // TODO we should remove set default ShardingSphere schema after parser can recognize all DAL broadcast SQL.
-            backendConnection.setCurrentSchema(metaDataMap.keySet().iterator().next());
-        }
-        databaseCommunicationEngine = databaseCommunicationEngineFactory.newTextProtocolInstance(sqlStatement, sql, backendConnection);
-        return databaseCommunicationEngine.execute();
-    }
-    
-    @Override
-    public boolean next() throws SQLException {
-        return databaseCommunicationEngine.next();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() throws SQLException {
-        return databaseCommunicationEngine.getQueryResponseRow().getData();
-    }
-}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
index c2f0311..80a8664 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/query/QueryBackendHandler.java
@@ -18,10 +18,12 @@
 package org.apache.shardingsphere.proxy.backend.text.query;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
 import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistsException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
@@ -29,6 +31,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.Map;
 
 /**
  * Backend handler with query.
@@ -44,10 +47,16 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
     
     private final BackendConnection backendConnection;
     
+    private final boolean schemaSelectedRequired;
+    
     private DatabaseCommunicationEngine databaseCommunicationEngine;
     
     @Override
     public ResponseHeader execute() throws SQLException {
+        if (!schemaSelectedRequired && null == backendConnection.getSchemaName()) {
+            // TODO should remove set default ShardingSphere schema after parser can recognize all DAL broadcast SQL.
+            backendConnection.setCurrentSchema(getFirstSchemaName());
+        }
         if (!ProxyContext.getInstance().getMetaData(backendConnection.getSchemaName()).isComplete()) {
             throw new RuleNotExistsException();
         }
@@ -55,9 +64,17 @@ public final class QueryBackendHandler implements TextProtocolBackendHandler {
         return databaseCommunicationEngine.execute();
     }
     
+    private String getFirstSchemaName() {
+        Map<String, ShardingSphereMetaData> metaDataMap = ProxyContext.getInstance().getMetaDataContexts().getMetaDataMap();
+        if (metaDataMap.isEmpty()) {
+            throw new NoDatabaseSelectedException();
+        }
+        return metaDataMap.keySet().iterator().next();
+    }
+    
     @Override
     public boolean next() throws SQLException {
-        return null != databaseCommunicationEngine && databaseCommunicationEngine.next();
+        return databaseCommunicationEngine.next();
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
index f3fda34..e836251 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
@@ -26,7 +26,6 @@ import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
 import org.apache.shardingsphere.proxy.backend.text.admin.BroadcastBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.admin.ShowDatabasesBackendHandler;
-import org.apache.shardingsphere.proxy.backend.text.admin.UnicastBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.admin.UseDatabaseBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.query.QueryBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.sctl.set.ShardingCTLSetBackendHandler;
@@ -170,16 +169,16 @@ public final class TextProtocolBackendHandlerFactoryTest {
     public void assertNewInstanceWithShow() {
         String sql = "SHOW VARIABLES LIKE '%x%'";
         TextProtocolBackendHandler actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
-        assertThat(actual, instanceOf(UnicastBackendHandler.class));
+        assertThat(actual, instanceOf(QueryBackendHandler.class));
         sql = "SHOW VARIABLES WHERE Variable_name ='language'";
         actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
-        assertThat(actual, instanceOf(UnicastBackendHandler.class));
+        assertThat(actual, instanceOf(QueryBackendHandler.class));
         sql = "SHOW CHARACTER SET";
         actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
-        assertThat(actual, instanceOf(UnicastBackendHandler.class));
+        assertThat(actual, instanceOf(QueryBackendHandler.class));
         sql = "SHOW COLLATION";
         actual = TextProtocolBackendHandlerFactory.newInstance(databaseType, sql, backendConnection);
-        assertThat(actual, instanceOf(UnicastBackendHandler.class));
+        assertThat(actual, instanceOf(QueryBackendHandler.class));
     }
     
     @Test
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java
index a91b05a..438e57b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.text.admin;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import org.apache.shardingsphere.proxy.backend.text.query.QueryBackendHandler;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
@@ -84,11 +85,11 @@ public final class DALBackendHandlerFactoryTest {
         DALStatement dalStatement = mock(DALStatement.class);
         BackendConnection backendConnection = mock(BackendConnection.class);
         TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance(dalStatement, "", backendConnection);
-        assertThat(textProtocolBackendHandler, instanceOf(UnicastBackendHandler.class));
-        UnicastBackendHandler unicastBackendHandler = (UnicastBackendHandler) textProtocolBackendHandler;
-        assertFieldOfInstance(unicastBackendHandler, "sqlStatement", is(dalStatement));
-        assertFieldOfInstance(unicastBackendHandler, "sql", is(""));
-        assertFieldOfInstance(unicastBackendHandler, "backendConnection", is(backendConnection));
+        assertThat(textProtocolBackendHandler, instanceOf(QueryBackendHandler.class));
+        QueryBackendHandler backendHandler = (QueryBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(backendHandler, "sqlStatement", is(dalStatement));
+        assertFieldOfInstance(backendHandler, "sql", is(""));
+        assertFieldOfInstance(backendHandler, "backendConnection", is(backendConnection));
     }
     
     @SuppressWarnings("unchecked")
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandlerTest.java
deleted file mode 100644
index 62d4da5..0000000
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/UnicastBackendHandlerTest.java
+++ /dev/null
@@ -1,115 +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.proxy.backend.text.admin;
-
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.auth.Authentication;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
-import org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
-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;
-import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.transaction.core.TransactionType;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.Spy;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.Field;
-import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class UnicastBackendHandlerTest {
-    
-    private static final String SCHEMA_PATTERN = "schema_%s";
-    
-    @Spy
-    private BackendConnection backendConnection = new BackendConnection(TransactionType.LOCAL);
-    
-    @Mock
-    private DatabaseCommunicationEngineFactory databaseCommunicationEngineFactory;
-    
-    @Before
-    public void setUp() throws SQLException, IllegalAccessException, NoSuchFieldException {
-        Field metaDataContexts = ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
-        metaDataContexts.setAccessible(true);
-        metaDataContexts.set(ProxyContext.getInstance(), 
-                new StandardMetaDataContexts(getMetaDataMap(), mock(ExecutorEngine.class), new Authentication(), new ConfigurationProperties(new Properties()), new MySQLDatabaseType()));
-        setUnderlyingHandler(new UpdateResponseHeader(mock(SQLStatement.class)));
-    }
-    
-    private Map<String, ShardingSphereMetaData> getMetaDataMap() {
-        Map<String, ShardingSphereMetaData> result = new HashMap<>(10);
-        for (int i = 0; i < 10; i++) {
-            result.put(String.format(SCHEMA_PATTERN, i), mock(ShardingSphereMetaData.class));
-        }
-        return result;
-    }
-    
-    @Test
-    public void assertExecuteWhileSchemaIsNull() throws SQLException {
-        UnicastBackendHandler backendHandler = new UnicastBackendHandler(mock(SQLStatement.class), "show variable like %s", backendConnection);
-        backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 8));
-        setDatabaseCommunicationEngine(backendHandler);
-        ResponseHeader actual = backendHandler.execute();
-        assertThat(actual, instanceOf(UpdateResponseHeader.class));
-        backendHandler.execute();
-    }
-    
-    @Test
-    public void assertExecuteWhileSchemaNotNull() throws SQLException {
-        backendConnection.setCurrentSchema(String.format(SCHEMA_PATTERN, 0));
-        UnicastBackendHandler backendHandler = new UnicastBackendHandler(mock(SQLStatement.class), "show variable like %s", backendConnection);
-        setDatabaseCommunicationEngine(backendHandler);
-        ResponseHeader actual = backendHandler.execute();
-        assertThat(actual, instanceOf(UpdateResponseHeader.class));
-        backendHandler.execute();
-    }
-    
-    private void setUnderlyingHandler(final ResponseHeader responseHeader) throws SQLException {
-        DatabaseCommunicationEngine databaseCommunicationEngine = mock(DatabaseCommunicationEngine.class);
-        when(databaseCommunicationEngine.execute()).thenReturn(responseHeader);
-        when(databaseCommunicationEngineFactory.newTextProtocolInstance(any(), anyString(), any())).thenReturn(databaseCommunicationEngine);
-    }
-    
-    @SneakyThrows(ReflectiveOperationException.class)
-    private void setDatabaseCommunicationEngine(final UnicastBackendHandler unicastSchemaBackendHandler) {
-        Field field = unicastSchemaBackendHandler.getClass().getDeclaredField("databaseCommunicationEngineFactory");
-        field.setAccessible(true);
-        field.set(unicastSchemaBackendHandler, databaseCommunicationEngineFactory);
-    }
-}