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 1917f16 Add setFetchSize logic for JDBC adaptor when use stream query (#14833) 1917f16 is described below commit 1917f1666fb5e945cbc43b80ea62dc6a43cc1237 Author: Zhengqiang Duan <duanzhengqi...@apache.org> AuthorDate: Tue Jan 18 10:08:54 2022 +0800 Add setFetchSize logic for JDBC adaptor when use stream query (#14833) * Revert "Revert #14780 changes in Proxy backend (#14801)" This reverts commit 83f22b494f1a89429cc5ced57ec5944a7ac1484d. * Add setFetchSize logic for JDBC adaptor when use stream query * Revert "Revert "Revert #14780 changes in Proxy backend (#14801)"" This reverts commit 493cbc5d4ceb197a3412005f51d2171587e43c7a. * fix unit test --- .../StatementMemoryStrictlyFetchSizeSetter.java | 6 ++-- ...ySQLStatementMemoryStrictlyFetchSizeSetter.java | 10 +++--- ...aussStatementMemoryStrictlyFetchSizeSetter.java | 9 ++--- ...eSQLStatementMemoryStrictlyFetchSizeSetter.java | 10 +++--- ...tatement.StatementMemoryStrictlyFetchSizeSetter | 6 ++-- ...StatementMemoryStrictlyFetchSizeSetterTest.java | 39 +++------------------- ...StatementMemoryStrictlyFetchSizeSetterTest.java | 39 +++------------------- ...StatementMemoryStrictlyFetchSizeSetterTest.java | 39 +++------------------- .../jdbc/core/connection/ConnectionManager.java | 31 +++++++++++++++-- .../core/connection/ConnectionManagerTest.java | 19 +++++++++++ .../driver/state/DriverStateContextTest.java | 21 +++++++++++- .../jdbc/connection/JDBCBackendConnection.java | 6 ++-- 12 files changed, 109 insertions(+), 126 deletions(-) diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java similarity index 80% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java index bf73682..e55932c 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/StatementMemoryStrictlyFetchSizeSetter.java @@ -15,8 +15,9 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.spi.singleton.SingletonSPI; import org.apache.shardingsphere.spi.typed.TypedSPI; @@ -32,7 +33,8 @@ public interface StatementMemoryStrictlyFetchSizeSetter extends TypedSPI, Single * Set fetch size. * * @param statement statement to be set + * @param props configuration properties * @throws SQLException SQL exception */ - void setFetchSize(Statement statement) throws SQLException; + void setFetchSize(Statement statement, ConfigurationProperties props) throws SQLException; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java similarity index 72% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java index ed41ea4..91a2f38 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetter.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; +import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; import java.sql.SQLException; import java.sql.Statement; @@ -30,8 +30,8 @@ import java.sql.Statement; public final class MySQLStatementMemoryStrictlyFetchSizeSetter implements StatementMemoryStrictlyFetchSizeSetter { @Override - public void setFetchSize(final Statement statement) throws SQLException { - int configuredFetchSize = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE); + public void setFetchSize(final Statement statement, final ConfigurationProperties props) throws SQLException { + int configuredFetchSize = props.<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE); statement.setFetchSize(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE.getDefaultValue().equals(String.valueOf(configuredFetchSize)) ? Integer.MIN_VALUE : configuredFetchSize); } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java similarity index 73% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java index 3717a8e..3fb64fa 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetter.java @@ -15,9 +15,10 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; -import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; import java.sql.SQLException; import java.sql.Statement; @@ -30,8 +31,8 @@ public final class OpenGaussStatementMemoryStrictlyFetchSizeSetter implements St private final PostgreSQLStatementMemoryStrictlyFetchSizeSetter delegated = new PostgreSQLStatementMemoryStrictlyFetchSizeSetter(); @Override - public void setFetchSize(final Statement statement) throws SQLException { - delegated.setFetchSize(statement); + public void setFetchSize(final Statement statement, final ConfigurationProperties props) throws SQLException { + delegated.setFetchSize(statement, props); } @Override diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java similarity index 72% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java index 46e23ac..1ca18fd 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetter.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; +import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; import java.sql.SQLException; import java.sql.Statement; @@ -30,8 +30,8 @@ import java.sql.Statement; public final class PostgreSQLStatementMemoryStrictlyFetchSizeSetter implements StatementMemoryStrictlyFetchSizeSetter { @Override - public void setFetchSize(final Statement statement) throws SQLException { - int configuredFetchSize = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE); + public void setFetchSize(final Statement statement, final ConfigurationProperties props) throws SQLException { + int configuredFetchSize = props.<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE); statement.setFetchSize(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE.getDefaultValue().equals(String.valueOf(configuredFetchSize)) ? 1 : configuredFetchSize); } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter b/shardingsphere-infra/shardingsphere-infra-executor/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter similarity index 66% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter rename to shardingsphere-infra/shardingsphere-infra-executor/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter index 077e29e..4b7b9ae 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter @@ -15,6 +15,6 @@ # limitations under the License. # -org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl.MySQLStatementMemoryStrictlyFetchSizeSetter -org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl.PostgreSQLStatementMemoryStrictlyFetchSizeSetter -org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl.OpenGaussStatementMemoryStrictlyFetchSizeSetter +org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl.MySQLStatementMemoryStrictlyFetchSizeSetter +org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl.PostgreSQLStatementMemoryStrictlyFetchSizeSetter +org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl.OpenGaussStatementMemoryStrictlyFetchSizeSetter diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java similarity index 52% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java index 3f88e2f..b9fe115 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/MySQLStatementMemoryStrictlyFetchSizeSetterTest.java @@ -15,44 +15,29 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; -import lombok.SneakyThrows; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; -import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Statement; 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.verify; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) public final class MySQLStatementMemoryStrictlyFetchSizeSetterTest { - private static ContextManager originContextManager; - - @BeforeClass - public static void setup() { - originContextManager = swapContextManager(mock(ContextManager.class, RETURNS_DEEP_STUBS)); - } - @Test public void assertSetFetchSize() throws SQLException { - when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); Statement statement = mock(Statement.class); - new MySQLStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement); + ConfigurationProperties props = mock(ConfigurationProperties.class); + when(props.getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); + new MySQLStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement, props); verify(statement).setFetchSize(Integer.MIN_VALUE); } @@ -60,18 +45,4 @@ public final class MySQLStatementMemoryStrictlyFetchSizeSetterTest { public void assertGetType() { assertThat(new MySQLStatementMemoryStrictlyFetchSizeSetter().getType(), is("MySQL")); } - - @AfterClass - public static void tearDown() { - swapContextManager(originContextManager); - } - - @SneakyThrows - private static ContextManager swapContextManager(final ContextManager newContextManager) { - Field contextManagerField = ProxyContext.class.getDeclaredField("contextManager"); - contextManagerField.setAccessible(true); - ContextManager result = (ContextManager) contextManagerField.get(ProxyContext.getInstance()); - contextManagerField.set(ProxyContext.getInstance(), newContextManager); - return result; - } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java similarity index 52% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java index d8f760f..73df420 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/OpenGaussStatementMemoryStrictlyFetchSizeSetterTest.java @@ -15,44 +15,29 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; -import lombok.SneakyThrows; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; -import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Statement; 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.verify; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) public final class OpenGaussStatementMemoryStrictlyFetchSizeSetterTest { - private static ContextManager originContextManager; - - @BeforeClass - public static void setup() { - originContextManager = swapContextManager(mock(ContextManager.class, RETURNS_DEEP_STUBS)); - } - @Test public void assertSetFetchSize() throws SQLException { - when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); Statement statement = mock(Statement.class); - new OpenGaussStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement); + ConfigurationProperties props = mock(ConfigurationProperties.class); + when(props.getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); + new OpenGaussStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement, props); verify(statement).setFetchSize(1); } @@ -60,18 +45,4 @@ public final class OpenGaussStatementMemoryStrictlyFetchSizeSetterTest { public void assertGetType() { assertThat(new OpenGaussStatementMemoryStrictlyFetchSizeSetter().getType(), is("openGauss")); } - - @AfterClass - public static void tearDown() { - swapContextManager(originContextManager); - } - - @SneakyThrows - private static ContextManager swapContextManager(final ContextManager newContextManager) { - Field contextManagerField = ProxyContext.class.getDeclaredField("contextManager"); - contextManagerField.setAccessible(true); - ContextManager result = (ContextManager) contextManagerField.get(ProxyContext.getInstance()); - contextManagerField.set(ProxyContext.getInstance(), newContextManager); - return result; - } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java similarity index 52% rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java rename to shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java index bd581d2..14c2df6 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java +++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/engine/driver/jdbc/statement/impl/PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest.java @@ -15,44 +15,29 @@ * limitations under the License. */ -package org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.impl; +package org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.impl; -import lombok.SneakyThrows; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; -import java.lang.reflect.Field; import java.sql.SQLException; import java.sql.Statement; 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.verify; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) public final class PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest { - private static ContextManager originContextManager; - - @BeforeClass - public static void setup() { - originContextManager = swapContextManager(mock(ContextManager.class, RETURNS_DEEP_STUBS)); - } - @Test public void assertSetFetchSize() throws SQLException { - when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps().<Integer>getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); Statement statement = mock(Statement.class); - new PostgreSQLStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement); + ConfigurationProperties props = mock(ConfigurationProperties.class); + when(props.getValue(ConfigurationPropertyKey.PROXY_BACKEND_QUERY_FETCH_SIZE)).thenReturn(-1); + new PostgreSQLStatementMemoryStrictlyFetchSizeSetter().setFetchSize(statement, props); verify(statement).setFetchSize(1); } @@ -60,18 +45,4 @@ public final class PostgreSQLStatementMemoryStrictlyFetchSizeSetterTest { public void assertGetType() { assertThat(new PostgreSQLStatementMemoryStrictlyFetchSizeSetter().getType(), is("PostgreSQL")); } - - @AfterClass - public static void tearDown() { - swapContextManager(originContextManager); - } - - @SneakyThrows - private static ContextManager swapContextManager(final ContextManager newContextManager) { - Field contextManagerField = ProxyContext.class.getDeclaredField("contextManager"); - contextManagerField.setAccessible(true); - ContextManager result = (ContextManager) contextManagerField.get(ProxyContext.getInstance()); - contextManagerField.set(ProxyContext.getInstance(), newContextManager); - return result; - } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java index f952d1c..64befc7 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java @@ -27,9 +27,12 @@ import org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteTempla import org.apache.shardingsphere.driver.jdbc.adapter.invocation.MethodInvocationRecorder; import org.apache.shardingsphere.infra.config.datasource.pool.creator.DataSourcePoolCreator; import org.apache.shardingsphere.infra.config.datasource.props.DataSourceProperties; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; +import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.ExecutorJDBCManager; import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; @@ -38,6 +41,7 @@ import org.apache.shardingsphere.infra.instance.definition.InstanceType; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService; +import org.apache.shardingsphere.spi.singleton.SingletonSPIRegistry; import org.apache.shardingsphere.traffic.rule.TrafficRule; import org.apache.shardingsphere.transaction.ConnectionTransaction; import org.apache.shardingsphere.transaction.core.TransactionType; @@ -79,11 +83,20 @@ public final class ConnectionManager implements ExecutorJDBCManager, AutoCloseab private final Random random = new SecureRandom(); + private final Map<String, StatementMemoryStrictlyFetchSizeSetter> fetchSizeSetters; + + private final DatabaseType databaseType; + + private final ConfigurationProperties props; + public ConnectionManager(final String schema, final ContextManager contextManager) { dataSourceMap.putAll(contextManager.getDataSourceMap(schema)); dataSourceMap.putAll(getTrafficDataSourceMap(schema, contextManager)); physicalDataSourceMap.putAll(contextManager.getDataSourceMap(schema)); connectionTransaction = createConnectionTransaction(schema, contextManager); + fetchSizeSetters = SingletonSPIRegistry.getTypedSingletonInstancesMap(StatementMemoryStrictlyFetchSizeSetter.class); + databaseType = contextManager.getMetaDataContexts().getMetaData(schema).getResource().getDatabaseType(); + props = contextManager.getMetaDataContexts().getProps(); } private Map<String, DataSource> getTrafficDataSourceMap(final String schema, final ContextManager contextManager) { @@ -312,15 +325,29 @@ public final class ConnectionManager implements ExecutorJDBCManager, AutoCloseab @SuppressWarnings("MagicConstant") @Override public Statement createStorageResource(final Connection connection, final ConnectionMode connectionMode, final StatementOption option) throws SQLException { - return connection.createStatement(option.getResultSetType(), option.getResultSetConcurrency(), option.getResultSetHoldability()); + Statement result = connection.createStatement(option.getResultSetType(), option.getResultSetConcurrency(), option.getResultSetHoldability()); + if (ConnectionMode.MEMORY_STRICTLY == connectionMode) { + setFetchSize(result); + } + return result; } @SuppressWarnings("MagicConstant") @Override public PreparedStatement createStorageResource(final String sql, final List<Object> parameters, final Connection connection, final ConnectionMode connectionMode, final StatementOption option) throws SQLException { - return option.isReturnGeneratedKeys() ? connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) + PreparedStatement result = option.isReturnGeneratedKeys() ? connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS) : connection.prepareStatement(sql, option.getResultSetType(), option.getResultSetConcurrency(), option.getResultSetHoldability()); + if (ConnectionMode.MEMORY_STRICTLY == connectionMode) { + setFetchSize(result); + } + return result; + } + + private void setFetchSize(final Statement statement) throws SQLException { + if (fetchSizeSetters.containsKey(databaseType.getName())) { + fetchSizeSetters.get(databaseType.getName()).setFetchSize(statement, props); + } } @Override diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java index 312311e..532809b 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManagerTest.java @@ -21,7 +21,9 @@ import com.google.common.collect.Sets; import com.zaxxer.hikari.HikariDataSource; import org.apache.shardingsphere.infra.config.datasource.pool.creator.DataSourcePoolCreator; import org.apache.shardingsphere.infra.config.datasource.props.DataSourceProperties; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.DefaultSchema; +import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition; @@ -38,8 +40,11 @@ import org.junit.Test; import org.mockito.MockedStatic; import javax.sql.DataSource; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Statement; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -47,6 +52,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -55,6 +61,8 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public final class ConnectionManagerTest { @@ -83,6 +91,8 @@ public final class ConnectionManagerTest { when(result.getMetaDataContexts().getMetaDataPersistService()).thenReturn(Optional.of(metaDataPersistService)); when(result.getMetaDataContexts().getGlobalRuleMetaData().findSingleRule(TransactionRule.class)).thenReturn(Optional.empty()); when(result.getMetaDataContexts().getGlobalRuleMetaData().findSingleRule(TrafficRule.class)).thenReturn(Optional.of(trafficRule)); + when(result.getMetaDataContexts().getMetaData(DefaultSchema.LOGIC_NAME).getResource().getDatabaseType()).thenReturn(new MySQLDatabaseType()); + when(result.getMetaDataContexts().getProps()).thenReturn(new ConfigurationProperties(new Properties())); dataSourcePoolCreator = mockStatic(DataSourcePoolCreator.class); Map<String, DataSource> trafficDataSourceMap = mockTrafficDataSourceMap(); when(DataSourcePoolCreator.create((Map) any())).thenReturn(trafficDataSourceMap); @@ -214,4 +224,13 @@ public final class ConnectionManagerTest { assertThat(ex.getMessage(), is("Can not get 3 connections one time, partition succeed connection(0) have released!")); } } + + @Test + public void assertSetFetchSizeAsExpected() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, SQLException { + Statement statement = mock(Statement.class); + Method setFetchSizeMethod = ConnectionManager.class.getDeclaredMethod("setFetchSize", Statement.class); + setFetchSizeMethod.setAccessible(true); + setFetchSizeMethod.invoke(connectionManager, statement); + verify(statement, times(1)).setFetchSize(Integer.MIN_VALUE); + } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java index 864a096..ddea681 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java @@ -18,7 +18,13 @@ package org.apache.shardingsphere.driver.state; import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.DefaultSchema; +import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType; +import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine; +import org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData; import org.apache.shardingsphere.infra.state.StateContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; @@ -31,6 +37,8 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.sql.Connection; +import java.util.LinkedHashMap; +import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; @@ -45,8 +53,19 @@ public final class DriverStateContextTest { @Before public void setUp() { - when(contextManager.getMetaDataContexts()).thenReturn(new MetaDataContexts(mock(MetaDataPersistService.class))); + Map<String, ShardingSphereMetaData> metaDataMap = mockMetaDataMap(); + when(contextManager.getMetaDataContexts()).thenReturn(new MetaDataContexts(mock(MetaDataPersistService.class), metaDataMap, + mock(ShardingSphereRuleMetaData.class), mock(ExecutorEngine.class), mock(ConfigurationProperties.class), mock(OptimizerContext.class))); when(contextManager.getInstanceContext().getState()).thenReturn(new StateContext()); + when(contextManager.getMetaDataContexts().getMetaData(DefaultSchema.LOGIC_NAME).getResource().getDatabaseType()).thenReturn(new MySQLDatabaseType()); + } + + private Map<String, ShardingSphereMetaData> mockMetaDataMap() { + Map<String, ShardingSphereMetaData> result = new LinkedHashMap<>(); + ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, Answers.RETURNS_DEEP_STUBS); + when(metaData.getResource().getDatabaseType()).thenReturn(new MySQLDatabaseType()); + result.put(DefaultSchema.LOGIC_NAME, metaData); + return result; } @Test diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java index a9bba20..bba9061 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java @@ -23,6 +23,7 @@ import com.google.common.collect.Multimap; import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.db.protocol.parameter.TypeUnspecifiedSQLParameter; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.ExecutorJDBCManager; @@ -30,7 +31,7 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.Statemen import org.apache.shardingsphere.infra.federation.executor.FederationExecutor; import org.apache.shardingsphere.proxy.backend.communication.BackendConnection; import org.apache.shardingsphere.proxy.backend.communication.jdbc.JDBCDatabaseCommunicationEngine; -import org.apache.shardingsphere.proxy.backend.communication.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; +import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.statement.StatementMemoryStrictlyFetchSizeSetter; import org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.JDBCBackendTransactionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.exception.BackendConnectionException; @@ -155,7 +156,8 @@ public final class JDBCBackendConnection implements BackendConnection<Void>, Exe private void setFetchSize(final Statement statement) throws SQLException { DatabaseType databaseType = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData(connectionSession.getSchemaName()).getResource().getDatabaseType(); if (fetchSizeSetters.containsKey(databaseType.getName())) { - fetchSizeSetters.get(databaseType.getName()).setFetchSize(statement); + ConfigurationProperties props = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps(); + fetchSizeSetters.get(databaseType.getName()).setFetchSize(statement, props); } }