This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 a0b83281ba6 Refactor ConnectionSizeAwareQueryableRALExecutor (#29714)
a0b83281ba6 is described below
commit a0b83281ba62c5d7edf21c514b9941c440675750
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 13 22:17:09 2024 +0800
Refactor ConnectionSizeAwareQueryableRALExecutor (#29714)
---
.../query/ConnectionSizeAwareQueryableRALExecutor.java | 14 ++++++--------
.../handler/distsql/ral/QueryableRALBackendHandler.java | 6 +++---
.../distsql/ral/queryable/ShowDistVariableExecutor.java | 17 ++++++++---------
.../ral/queryable/ShowDistVariablesExecutor.java | 9 ++++-----
.../ral/queryable/ShowDistVariableExecutorTest.java | 10 +---------
.../ral/queryable/ShowDistVariablesExecutorTest.java | 5 -----
6 files changed, 22 insertions(+), 39 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/executor/ConnectionSessionAwareQueryableRALExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/query/ConnectionSizeAwareQueryableRALExecutor.java
similarity index 62%
rename from
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/executor/ConnectionSessionAwareQueryableRALExecutor.java
rename to
infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/query/ConnectionSizeAwareQueryableRALExecutor.java
index 157ef430061..e6aa909a838 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/executor/ConnectionSessionAwareQueryableRALExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/ral/query/ConnectionSizeAwareQueryableRALExecutor.java
@@ -15,23 +15,21 @@
* limitations under the License.
*/
-package
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.executor;
+package org.apache.shardingsphere.distsql.handler.ral.query;
-import
org.apache.shardingsphere.distsql.handler.ral.query.QueryableRALExecutor;
import org.apache.shardingsphere.distsql.statement.ral.QueryableRALStatement;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
/**
- * Connection session aware queryable RAL executor.
+ * Connection size aware queryable RAL executor.
*
* @param <T> type of SQL statement
*/
-public interface ConnectionSessionAwareQueryableRALExecutor<T extends
QueryableRALStatement> extends QueryableRALExecutor<T> {
+public interface ConnectionSizeAwareQueryableRALExecutor<T extends
QueryableRALStatement> extends QueryableRALExecutor<T> {
/**
- * Set connection session.
+ * Set connection size.
*
- * @param connectionSession connection session
+ * @param connectionSize connection size
*/
- void setConnectionSession(ConnectionSession connectionSession);
+ void setConnectionSize(int connectionSize);
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandler.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandler.java
index 0bc9c4d48fb..1b8a0a7127b 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandler.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/QueryableRALBackendHandler.java
@@ -26,7 +26,7 @@ import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedRe
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.executor.ConnectionSessionAwareQueryableRALExecutor;
+import
org.apache.shardingsphere.distsql.handler.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
@@ -81,8 +81,8 @@ public final class QueryableRALBackendHandler<T extends
QueryableRALStatement> i
if (executor instanceof DatabaseAwareQueryableRALExecutor) {
((DatabaseAwareQueryableRALExecutor<T>)
executor).setCurrentDatabase(ProxyContext.getInstance().getDatabase(getDatabaseName(connectionSession,
sqlStatement)));
}
- if (executor instanceof ConnectionSessionAwareQueryableRALExecutor) {
- ((ConnectionSessionAwareQueryableRALExecutor<T>)
executor).setConnectionSession(connectionSession);
+ if (executor instanceof ConnectionSizeAwareQueryableRALExecutor) {
+ ((ConnectionSizeAwareQueryableRALExecutor<T>)
executor).setConnectionSize(connectionSession.getDatabaseConnectionManager().getConnectionSize());
}
return createMergedResult(executor.getRows(sqlStatement,
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData()));
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutor.java
index d61b90eb6bc..c6f5d709ab7 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutor.java
@@ -30,8 +30,7 @@ import
org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.util.LoggingUtils;
import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.enums.VariableEnum;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.executor.ConnectionSessionAwareQueryableRALExecutor;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import
org.apache.shardingsphere.distsql.handler.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import java.util.Arrays;
import java.util.Collection;
@@ -43,9 +42,9 @@ import java.util.Properties;
* Show dist variable executor.
*/
@Setter
-public final class ShowDistVariableExecutor implements
ConnectionSessionAwareQueryableRALExecutor<ShowDistVariableStatement> {
+public final class ShowDistVariableExecutor implements
ConnectionSizeAwareQueryableRALExecutor<ShowDistVariableStatement> {
- private ConnectionSession connectionSession;
+ private int connectionSize;
@Override
public Collection<String> getColumnNames() {
@@ -54,17 +53,17 @@ public final class ShowDistVariableExecutor implements
ConnectionSessionAwareQue
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShowDistVariableStatement sqlStatement, final ShardingSphereMetaData metaData) {
- return buildSpecifiedRow(metaData, connectionSession,
sqlStatement.getName());
+ return buildSpecifiedRow(metaData, sqlStatement.getName());
}
- private Collection<LocalDataQueryResultRow> buildSpecifiedRow(final
ShardingSphereMetaData metaData, final ConnectionSession connectionSession,
final String variableName) {
+ private Collection<LocalDataQueryResultRow> buildSpecifiedRow(final
ShardingSphereMetaData metaData, final String variableName) {
if (isConfigurationKey(variableName)) {
return Collections.singleton(new
LocalDataQueryResultRow(variableName.toLowerCase(),
getConfigurationValue(metaData, variableName)));
}
if (isTemporaryConfigurationKey(variableName)) {
return Collections.singleton(new
LocalDataQueryResultRow(variableName.toLowerCase(),
getTemporaryConfigurationValue(metaData, variableName)));
}
- return Collections.singleton(new
LocalDataQueryResultRow(variableName.toLowerCase(),
getSpecialValue(connectionSession, variableName)));
+ return Collections.singleton(new
LocalDataQueryResultRow(variableName.toLowerCase(),
getConnectionSize(variableName)));
}
private boolean isConfigurationKey(final String variableName) {
@@ -102,9 +101,9 @@ public final class ShowDistVariableExecutor implements
ConnectionSessionAwareQue
return
metaData.getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.valueOf(variableName)).toString();
}
- private String getSpecialValue(final ConnectionSession connectionSession,
final String variableName) {
+ private String getConnectionSize(final String variableName) {
ShardingSpherePreconditions.checkState(VariableEnum.CACHED_CONNECTIONS
== VariableEnum.getValueOf(variableName), () -> new
UnsupportedVariableException(variableName));
- return
String.valueOf(connectionSession.getDatabaseConnectionManager().getConnectionSize());
+ return String.valueOf(connectionSize);
}
private String getStringResult(final Object value) {
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutor.java
index 1ea2679c1f7..6f46f600448 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutor.java
@@ -28,8 +28,7 @@ import
org.apache.shardingsphere.logging.constant.LoggingConstants;
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.apache.shardingsphere.logging.util.LoggingUtils;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.enums.VariableEnum;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable.executor.ConnectionSessionAwareQueryableRALExecutor;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import
org.apache.shardingsphere.distsql.handler.ral.query.ConnectionSizeAwareQueryableRALExecutor;
import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
import java.util.Arrays;
@@ -44,9 +43,9 @@ import java.util.stream.Collectors;
* Show dist variables executor.
*/
@Setter
-public final class ShowDistVariablesExecutor implements
ConnectionSessionAwareQueryableRALExecutor<ShowDistVariablesStatement> {
+public final class ShowDistVariablesExecutor implements
ConnectionSizeAwareQueryableRALExecutor<ShowDistVariablesStatement> {
- private ConnectionSession connectionSession;
+ private int connectionSize;
@Override
public Collection<String> getColumnNames() {
@@ -61,7 +60,7 @@ public final class ShowDistVariablesExecutor implements
ConnectionSessionAwareQu
result.addAll(TemporaryConfigurationPropertyKey.getKeyNames().stream()
.map(each -> new LocalDataQueryResultRow(each.toLowerCase(),
getStringResult(metaData.getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.valueOf(each)))))
.collect(Collectors.toList()));
- result.add(new
LocalDataQueryResultRow(VariableEnum.CACHED_CONNECTIONS.name().toLowerCase(),
connectionSession.getDatabaseConnectionManager().getConnectionSize()));
+ result.add(new
LocalDataQueryResultRow(VariableEnum.CACHED_CONNECTIONS.name().toLowerCase(),
connectionSize));
addLoggingPropsRows(metaData, result);
if (sqlStatement.getLikePattern().isPresent()) {
String pattern =
SQLUtils.convertLikePatternToRegex(sqlStatement.getLikePattern().get());
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutorTest.java
index 0e07938b6a8..4a2862a4851 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutorTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariableExecutorTest.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigura
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
@@ -42,8 +41,6 @@ class ShowDistVariableExecutorTest {
private final ShardingSphereMetaData metaData =
mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
- private final ConnectionSession connectionSession =
mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
-
@Test
void assertGetColumns() {
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
@@ -56,9 +53,8 @@ class ShowDistVariableExecutorTest {
@Test
void assertShowCachedConnections() {
-
when(connectionSession.getDatabaseConnectionManager().getConnectionSize()).thenReturn(1);
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
- executor.setConnectionSession(connectionSession);
+ executor.setConnectionSize(1);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new
ShowDistVariableStatement("CACHED_CONNECTIONS"), metaData);
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
@@ -70,7 +66,6 @@ class ShowDistVariableExecutorTest {
void assertShowPropsVariable() {
when(metaData.getProps()).thenReturn(new
ConfigurationProperties(PropertiesBuilder.build(new Property("sql-show",
Boolean.TRUE.toString()))));
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
- executor.setConnectionSession(connectionSession);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new
ShowDistVariableStatement("SQL_SHOW"), metaData);
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
@@ -82,7 +77,6 @@ class ShowDistVariableExecutorTest {
void assertShowPropsVariableForTypedSPI() {
when(metaData.getProps()).thenReturn(new
ConfigurationProperties(PropertiesBuilder.build(new
Property("proxy-frontend-database-protocol-type", "MySQL"))));
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
- executor.setConnectionSession(connectionSession);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new
ShowDistVariableStatement("PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE"), metaData);
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
@@ -94,7 +88,6 @@ class ShowDistVariableExecutorTest {
void assertShowTemporaryPropsVariable() {
when(metaData.getTemporaryProps()).thenReturn(new
TemporaryConfigurationProperties(PropertiesBuilder.build(new
Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
- executor.setConnectionSession(connectionSession);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new
ShowDistVariableStatement("PROXY_META_DATA_COLLECTOR_ENABLED"), metaData);
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
@@ -105,7 +98,6 @@ class ShowDistVariableExecutorTest {
@Test
void assertExecuteWithInvalidVariableName() {
ShowDistVariableExecutor executor = new ShowDistVariableExecutor();
- executor.setConnectionSession(connectionSession);
assertThrows(UnsupportedVariableException.class, () ->
executor.getRows(new ShowDistVariableStatement("wrong_name"), metaData));
}
}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutorTest.java
index c491d7a295f..1e34ca23766 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutorTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ShowDistVariablesExecutorTest.java
@@ -25,7 +25,6 @@ import
org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.logging.rule.LoggingRule;
import
org.apache.shardingsphere.logging.rule.builder.DefaultLoggingRuleConfigurationBuilder;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
@@ -44,8 +43,6 @@ class ShowDistVariablesExecutorTest {
private final ShardingSphereMetaData metaData =
mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
- private final ConnectionSession connectionSession =
mock(ConnectionSession.class, RETURNS_DEEP_STUBS);
-
@Test
void assertGetColumns() {
ShowDistVariablesExecutor executor = new ShowDistVariablesExecutor();
@@ -61,7 +58,6 @@ class ShowDistVariablesExecutorTest {
when(metaData.getProps()).thenReturn(new
ConfigurationProperties(PropertiesBuilder.build(new
Property("system-log-level", "INFO"))));
when(metaData.getTemporaryProps()).thenReturn(new
TemporaryConfigurationProperties(PropertiesBuilder.build(new
Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
ShowDistVariablesExecutor executor = new ShowDistVariablesExecutor();
- executor.setConnectionSession(connectionSession);
Collection<LocalDataQueryResultRow> actual =
executor.getRows(mock(ShowDistVariablesStatement.class), metaData);
assertThat(actual.size(), is(21));
LocalDataQueryResultRow row = actual.iterator().next();
@@ -75,7 +71,6 @@ class ShowDistVariablesExecutorTest {
when(metaData.getTemporaryProps()).thenReturn(new
TemporaryConfigurationProperties(PropertiesBuilder.build(new
Property("proxy-meta-data-collector-enabled", Boolean.FALSE.toString()))));
when(metaData.getGlobalRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(new LoggingRule(new
DefaultLoggingRuleConfigurationBuilder().build()))));
ShowDistVariablesExecutor executor = new ShowDistVariablesExecutor();
- executor.setConnectionSession(connectionSession);
Collection<LocalDataQueryResultRow> actual = executor.getRows(new
ShowDistVariablesStatement("sql_%"), metaData);
assertThat(actual.size(), is(2));
Iterator<LocalDataQueryResultRow> iterator = actual.iterator();