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 2022/11/16 10:27:16 UTC
[shardingsphere] branch master updated: Optimize sqlFederationExecutor init logic when sqlFederationType modify dynamically (#22209)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 753c0cee8ee Optimize sqlFederationExecutor init logic when sqlFederationType modify dynamically (#22209)
753c0cee8ee is described below
commit 753c0cee8ee6fd3db00536da55b64bc5198a3758
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Wed Nov 16 18:27:09 2022 +0800
Optimize sqlFederationExecutor init logic when sqlFederationType modify dynamically (#22209)
* Optimize sqlFederationExecutor init logic when sqlFederationType modify dynamically
* update logic
* fix unit test exception
---
.../jdbc/adapter/PreparedStatementAdapterTest.java | 3 ++
.../driver/jdbc/adapter/StatementAdapterTest.java | 6 +++-
.../UnsupportedOperationPreparedStatementTest.java | 3 ++
.../UnsupportedOperationStatementTest.java | 3 ++
.../sqlfederation/enums/SQLFederationTypeEnum.java | 29 +++++++-----------
.../sqlfederation/rule/SQLFederationRule.java | 13 ++++++--
.../enums/SQLFederationTypeEnumTest.java | 35 ++++++++--------------
.../advanced/AdvancedSQLFederationExecutor.java | 5 ++++
.../distsql/ral/common/enums/VariableEnum.java | 1 +
9 files changed, 54 insertions(+), 44 deletions(-)
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/PreparedStatementAdapterTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/PreparedStatementAdapterTest.java
index 37439490f6f..f1ae0f8f0f1 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/PreparedStatementAdapterTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/PreparedStatementAdapterTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.driver.jdbc.adapter;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -47,6 +48,7 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.Collections;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -72,6 +74,7 @@ public final class PreparedStatementAdapterTest {
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(globalRuleMetaData.getSingleRule(SQLParserRule.class)).thenReturn(new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build()));
+ when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(connection.getDatabaseName()).getProtocolType()).thenReturn(new MySQLDatabaseType());
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(connection.getDatabaseName()).getResourceMetaData().getStorageTypes())
.thenReturn(Collections.singletonMap("ds_0", new MySQLDatabaseType()));
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/StatementAdapterTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/StatementAdapterTest.java
index 53dc61fd3e3..6048011b853 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/StatementAdapterTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/StatementAdapterTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.driver.jdbc.adapter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -36,11 +37,12 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -227,6 +229,7 @@ public final class StatementAdapterTest {
ShardingSphereConnection connection = mock(ShardingSphereConnection.class, RETURNS_DEEP_STUBS);
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(connection.getDatabaseName()).thenReturn("db");
when(globalRuleMetaData.getSingleRule(TrafficRule.class)).thenReturn(new TrafficRule(new DefaultTrafficRuleConfigurationBuilder().build()));
when(globalRuleMetaData.getSingleRule(SQLFederationRule.class)).thenReturn(new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build()));
@@ -246,6 +249,7 @@ public final class StatementAdapterTest {
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(connection.getDatabaseName()).thenReturn("db");
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(globalRuleMetaData.getSingleRule(TrafficRule.class)).thenReturn(trafficRule);
when(globalRuleMetaData.getSingleRule(SQLFederationRule.class)).thenReturn(sqlFederationRule);
ShardingSphereStatement result = new ShardingSphereStatement(connection);
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
index fa6614ece33..ca5426fed44 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationPreparedStatementTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.driver.jdbc.unsupported;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -35,6 +36,7 @@ import java.io.StringReader;
import java.sql.NClob;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
+import java.util.Properties;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -57,6 +59,7 @@ public final class UnsupportedOperationPreparedStatementTest {
ShardingSphereRuleMetaData globalRuleMetaData = mock(ShardingSphereRuleMetaData.class);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(connection.getDatabaseName()).getProtocolType()).thenReturn(new MySQLDatabaseType());
+ when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(globalRuleMetaData.getSingleRule(SQLParserRule.class)).thenReturn(sqlParserRule);
when(globalRuleMetaData.getSingleRule(TrafficRule.class)).thenReturn(trafficRule);
when(globalRuleMetaData.getSingleRule(SQLFederationRule.class)).thenReturn(sqlFederationRule);
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationStatementTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationStatementTest.java
index 61e4c620a4a..680dbd16bbe 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationStatementTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationStatementTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.driver.jdbc.unsupported;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
import org.apache.shardingsphere.sqlfederation.rule.builder.DefaultSQLFederationRuleConfigurationBuilder;
@@ -29,6 +30,7 @@ import org.junit.Test;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
+import java.util.Properties;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -46,6 +48,7 @@ public final class UnsupportedOperationStatementTest {
SQLFederationRule sqlFederationRule = new SQLFederationRule(new DefaultSQLFederationRuleConfigurationBuilder().build());
when(connection.getDatabaseName()).thenReturn("db");
when(connection.getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ when(connection.getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
when(globalRuleMetaData.getSingleRule(TrafficRule.class)).thenReturn(trafficRule);
when(globalRuleMetaData.getSingleRule(SQLFederationRule.class)).thenReturn(sqlFederationRule);
shardingSphereStatement = new ShardingSphereStatement(connection);
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnum.java
similarity index 54%
copy from proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
copy to kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnum.java
index 8e1ac961987..733d8e2b287 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
+++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnum.java
@@ -15,31 +15,24 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.enums;
+package org.apache.shardingsphere.sqlfederation.enums;
-import org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
+import java.util.Arrays;
/**
- * Variable enum.
+ * SQL federation type enum.
*/
-public enum VariableEnum {
+public enum SQLFederationTypeEnum {
- AGENT_PLUGINS_ENABLED,
-
- CACHED_CONNECTIONS,
-
- TRANSACTION_TYPE;
+ NONE, ORIGINAL, ADVANCED;
/**
- * Returns the variable constant of the specified variable name.
- * @param variableName variable name
- * @return variable constant
+ * Judge whether is valid sql federation type or not.
+ *
+ * @param sqlFederationType sql federation type
+ * @return whether is valid sql federation type or not
*/
- public static VariableEnum getValueOf(final String variableName) {
- try {
- return valueOf(variableName.toUpperCase());
- } catch (IllegalArgumentException ex) {
- throw new UnsupportedVariableException(variableName);
- }
+ public static boolean isValidSQLFederationType(final String sqlFederationType) {
+ return Arrays.stream(values()).anyMatch(each -> each.name().equals(sqlFederationType));
}
}
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
index 38dd7f4b13a..a5e3f98fe4a 100644
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
+++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
@@ -17,13 +17,16 @@
package org.apache.shardingsphere.sqlfederation.rule;
+import com.google.common.base.Preconditions;
import lombok.Getter;
+import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.data.ShardingSphereData;
import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.sqlfederation.api.config.SQLFederationRuleConfiguration;
+import org.apache.shardingsphere.sqlfederation.enums.SQLFederationTypeEnum;
import org.apache.shardingsphere.sqlfederation.factory.SQLFederationExecutorFactory;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationExecutor;
@@ -35,11 +38,11 @@ public final class SQLFederationRule implements GlobalRule {
@Getter
private final SQLFederationRuleConfiguration configuration;
- private final SQLFederationExecutor sqlFederationExecutor;
+ private SQLFederationExecutor sqlFederationExecutor;
public SQLFederationRule(final SQLFederationRuleConfiguration ruleConfig) {
configuration = ruleConfig;
- sqlFederationExecutor = SQLFederationExecutorFactory.getInstance(ruleConfig.getSqlFederationType());
+ sqlFederationExecutor = SQLFederationExecutorFactory.getInstance(configuration.getSqlFederationType());
}
/**
@@ -55,6 +58,12 @@ public final class SQLFederationRule implements GlobalRule {
*/
public SQLFederationExecutor getSQLFederationExecutor(final String databaseName, final String schemaName, final ShardingSphereMetaData metaData, final ShardingSphereData shardingSphereData,
final JDBCExecutor jdbcExecutor, final EventBusContext eventBusContext) {
+ String sqlFederationType = metaData.getProps().getValue(ConfigurationPropertyKey.SQL_FEDERATION_TYPE);
+ Preconditions.checkArgument(SQLFederationTypeEnum.isValidSQLFederationType(sqlFederationType), "%s is not a valid sqlFederationType.", sqlFederationType);
+ if (!configuration.getSqlFederationType().equals(sqlFederationType)) {
+ configuration.setSqlFederationType(sqlFederationType);
+ sqlFederationExecutor = SQLFederationExecutorFactory.getInstance(configuration.getSqlFederationType());
+ }
sqlFederationExecutor.init(databaseName, schemaName, metaData, shardingSphereData, jdbcExecutor, eventBusContext);
return sqlFederationExecutor;
}
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnumTest.java
similarity index 52%
copy from proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
copy to kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnumTest.java
index 8e1ac961987..b21d59aa442 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
+++ b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/enums/SQLFederationTypeEnumTest.java
@@ -15,31 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.enums;
+package org.apache.shardingsphere.sqlfederation.enums;
-import org.apache.shardingsphere.proxy.backend.exception.UnsupportedVariableException;
+import org.junit.Test;
-/**
- * Variable enum.
- */
-public enum VariableEnum {
-
- AGENT_PLUGINS_ENABLED,
-
- CACHED_CONNECTIONS,
-
- TRANSACTION_TYPE;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public final class SQLFederationTypeEnumTest {
- /**
- * Returns the variable constant of the specified variable name.
- * @param variableName variable name
- * @return variable constant
- */
- public static VariableEnum getValueOf(final String variableName) {
- try {
- return valueOf(variableName.toUpperCase());
- } catch (IllegalArgumentException ex) {
- throw new UnsupportedVariableException(variableName);
- }
+ @Test
+ public void assertIsValidSQLFederationType() {
+ assertTrue(SQLFederationTypeEnum.isValidSQLFederationType("NONE"));
+ assertTrue(SQLFederationTypeEnum.isValidSQLFederationType("ORIGINAL"));
+ assertTrue(SQLFederationTypeEnum.isValidSQLFederationType("ADVANCED"));
+ assertFalse(SQLFederationTypeEnum.isValidSQLFederationType("XXX"));
}
}
diff --git a/kernel/sql-federation/executor/advanced/src/main/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutor.java b/kernel/sql-federation/executor/advanced/src/main/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutor.java
index baa02b06704..8e17f3baba8 100644
--- a/kernel/sql-federation/executor/advanced/src/main/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutor.java
+++ b/kernel/sql-federation/executor/advanced/src/main/java/org/apache/shardingsphere/sqlfederation/advanced/AdvancedSQLFederationExecutor.java
@@ -166,6 +166,11 @@ public final class AdvancedSQLFederationExecutor implements SQLFederationExecuto
}
}
+ @Override
+ public boolean isDefault() {
+ return true;
+ }
+
@Override
public String getType() {
return "ADVANCED";
diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
index 8e1ac961987..cf908f73bc1 100644
--- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
+++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/enums/VariableEnum.java
@@ -32,6 +32,7 @@ public enum VariableEnum {
/**
* Returns the variable constant of the specified variable name.
+ *
* @param variableName variable name
* @return variable constant
*/