This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 cd9c3609ec4 Refactor RuleDefinitionBackendHandler to support old
metadata (#29865)
cd9c3609ec4 is described below
commit cd9c3609ec40af3855b70a562c990f7476ef48b1
Author: zhaojinchao <[email protected]>
AuthorDate: Mon Jan 29 11:41:50 2024 +0800
Refactor RuleDefinitionBackendHandler to support old metadata (#29865)
* Refactor RuleDefinitionBackendHandler to support old metadata
* Fix unit test
* Fix checkstyle
---
.../update/DropReadwriteSplittingRuleExecutor.java | 6 ++
.../rule/engine/RuleDefinitionExecuteEngine.java | 35 +++++++---
.../global/GlobalRuleDefinitionExecuteEngine.java | 3 +-
.../LegacyDatabaseRuleDefinitionExecuteEngine.java | 41 +++++-------
.../LegacyGlobalRuleDefinitionExecuteEngine.java | 10 +--
.../spi/database/DatabaseRuleDropExecutor.java | 11 ++++
.../distsql/rdl/RDLBackendHandlerFactory.java | 14 +---
.../LegacyRuleDefinitionBackendHandlerTest.java | 75 ----------------------
8 files changed, 68 insertions(+), 127 deletions(-)
diff --git
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java
index 75adc344c6a..bfd4160e507 100644
---
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java
+++
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.distsql.handler.checker.ReadwriteSplittingRuleStatementChecker;
@@ -137,6 +138,11 @@ public final class DropReadwriteSplittingRuleExecutor
implements DatabaseRuleDro
.map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toSet()),
sqlStatement.getNames()).isEmpty();
}
+ @Override
+ public void operate(final DropReadwriteSplittingRuleStatement
sqlStatement, final ShardingSphereDatabase database) {
+
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class).ifPresent(optional
-> sqlStatement.getNames().forEach(optional::cleanStorageNodeDataSource));
+ }
+
@Override
public Class<ReadwriteSplittingRuleConfiguration>
getRuleConfigurationClass() {
return ReadwriteSplittingRuleConfiguration.class;
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/RuleDefinitionExecuteEngine.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/RuleDefinitionExecuteEngine.java
index 99d2874b4e6..2fa9e87b6bb 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/RuleDefinitionExecuteEngine.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/RuleDefinitionExecuteEngine.java
@@ -20,6 +20,7 @@ package
org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine;
import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.database.DatabaseRuleDefinitionExecuteEngine;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.global.GlobalRuleDefinitionExecuteEngine;
+import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy.LegacyDatabaseRuleDefinitionExecuteEngine;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy.LegacyGlobalRuleDefinitionExecuteEngine;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
@@ -50,18 +51,36 @@ public abstract class RuleDefinitionExecuteEngine {
public void executeUpdate() {
Optional<DatabaseRuleDefinitionExecutor> databaseExecutor =
TypedSPILoader.findService(DatabaseRuleDefinitionExecutor.class,
sqlStatement.getClass());
if (databaseExecutor.isPresent()) {
+ executeDatabaseRule(databaseExecutor.get());
+ } else {
+
executeGlobalRule(TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class,
sqlStatement.getClass()));
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private void executeDatabaseRule(final DatabaseRuleDefinitionExecutor
databaseExecutor) {
+ if (isNormalRuleUpdater()) {
new DatabaseRuleDefinitionExecuteEngine(
- sqlStatement, contextManager,
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
currentDatabaseName)), databaseExecutor.get()).executeUpdate();
+ sqlStatement, contextManager,
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
currentDatabaseName)), databaseExecutor).executeUpdate();
+ } else {
+ new LegacyDatabaseRuleDefinitionExecuteEngine(
+ sqlStatement, contextManager,
getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
currentDatabaseName)), databaseExecutor).executeUpdate();
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private void executeGlobalRule(final GlobalRuleDefinitionExecutor
globalExecutor) {
+ if (isNormalRuleUpdater()) {
+ new GlobalRuleDefinitionExecuteEngine(sqlStatement,
contextManager, globalExecutor).executeUpdate();
} else {
- String modeType =
contextManager.getInstanceContext().getModeConfiguration().getType();
- GlobalRuleDefinitionExecutor globalExecutor =
TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class,
sqlStatement.getClass());
- if ("Cluster".equals(modeType) || "Standalone".equals(modeType)) {
- new GlobalRuleDefinitionExecuteEngine(sqlStatement,
contextManager, globalExecutor).executeUpdate();
- } else {
- new LegacyGlobalRuleDefinitionExecuteEngine(sqlStatement,
contextManager).executeUpdate();
- }
+ new LegacyGlobalRuleDefinitionExecuteEngine(sqlStatement,
contextManager, globalExecutor).executeUpdate();
}
}
+ private boolean isNormalRuleUpdater() {
+ String modeType =
contextManager.getInstanceContext().getModeConfiguration().getType();
+ return "Cluster".equals(modeType) || "Standalone".equals(modeType);
+ }
+
protected abstract ShardingSphereDatabase getDatabase(String databaseName);
}
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/global/GlobalRuleDefinitionExecuteEngine.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/global/GlobalRuleDefinitionExecuteEngine.java
index d42fff96761..bf06a5a3b29 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/global/GlobalRuleDefinitionExecuteEngine.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/global/GlobalRuleDefinitionExecuteEngine.java
@@ -44,9 +44,8 @@ public final class GlobalRuleDefinitionExecuteEngine {
*/
@SuppressWarnings("unchecked")
public void executeUpdate() {
- Class<? extends RuleConfiguration> ruleConfigClass =
executor.getRuleConfigurationClass();
Collection<RuleConfiguration> ruleConfigs =
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
- RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigs, ruleConfigClass);
+ RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigs, executor.getRuleConfigurationClass());
executor.checkBeforeUpdate(currentRuleConfig, sqlStatement);
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs,
sqlStatement, currentRuleConfig));
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
similarity index 79%
rename from
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java
rename to
infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
index 6559ab04e18..3faf0282909 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyDatabaseRuleDefinitionExecuteEngine.java
@@ -15,28 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule;
+package org.apache.shardingsphere.distsql.handler.type.rdl.rule.engine.legacy;
import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDefinitionExecutor;
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor;
-import org.apache.shardingsphere.distsql.handler.util.DatabaseNameUtils;
import
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator;
import
org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule;
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.DistSQLBackendHandler;
-import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
-import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import
org.apache.shardingsphere.readwritesplitting.distsql.handler.update.DropReadwriteSplittingRuleExecutor;
-import
org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement;
+import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
@@ -48,29 +40,33 @@ import java.util.stream.Collectors;
// TODO Remove when metadata structure adjustment completed. #25485
/**
- * Legacy rule definition backend handler.
+ * Legacy rule definition execute engine.
*/
@RequiredArgsConstructor
-public final class LegacyRuleDefinitionBackendHandler implements
DistSQLBackendHandler {
+public final class LegacyDatabaseRuleDefinitionExecuteEngine {
private final RuleDefinitionStatement sqlStatement;
- private final ConnectionSession connectionSession;
+ private final ContextManager contextManager;
- @SuppressWarnings({"rawtypes", "unchecked"})
- @Override
- public ResponseHeader execute() {
- ShardingSphereDatabase database =
ProxyContext.getInstance().getDatabase(DatabaseNameUtils.getDatabaseName(sqlStatement,
connectionSession.getDatabaseName()));
- DatabaseRuleDefinitionExecutor executor =
TypedSPILoader.getService(DatabaseRuleDefinitionExecutor.class,
sqlStatement.getClass());
+ private final ShardingSphereDatabase database;
+
+ @SuppressWarnings("rawtypes")
+ private final DatabaseRuleDefinitionExecutor executor;
+
+ /**
+ * Execute update.
+ */
+ @SuppressWarnings("unchecked")
+ public void executeUpdate() {
Class<? extends RuleConfiguration> ruleConfigClass =
executor.getRuleConfigurationClass();
RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null);
executor.setDatabase(database);
executor.checkBeforeUpdate(sqlStatement, currentRuleConfig);
if (getRefreshStatus(sqlStatement, currentRuleConfig, executor)) {
-
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager()
+ contextManager.getInstanceContext().getModeContextManager()
.alterRuleConfiguration(database.getName(),
processSQLStatement(database, sqlStatement, executor, currentRuleConfig));
}
- return new UpdateResponseHeader(sqlStatement);
}
private Optional<RuleConfiguration> findCurrentRuleConfiguration(final
ShardingSphereDatabase database, final Class<? extends RuleConfiguration>
ruleConfigClass) {
@@ -130,10 +126,7 @@ public final class LegacyRuleDefinitionBackendHandler
implements DistSQLBackendH
if (executor.updateCurrentRuleConfiguration(sqlStatement,
currentRuleConfig)) {
configs.remove(currentRuleConfig);
}
- if (executor instanceof DropReadwriteSplittingRuleExecutor) {
-
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class)
- .ifPresent(optional ->
((DropReadwriteSplittingRuleStatement)
sqlStatement).getNames().forEach(optional::cleanStorageNodeDataSource));
- }
+ executor.operate(sqlStatement, database);
}
@SuppressWarnings("unchecked")
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyGlobalRuleDefinitionExecuteEngine.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyGlobalRuleDefinitionExecuteEngine.java
index 8d5e07cab83..5e690bdbafb 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyGlobalRuleDefinitionExecuteEngine.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/legacy/LegacyGlobalRuleDefinitionExecuteEngine.java
@@ -22,7 +22,6 @@ import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredR
import
org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
import
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import java.util.Collection;
@@ -39,15 +38,16 @@ public final class LegacyGlobalRuleDefinitionExecuteEngine {
private final ContextManager contextManager;
+ @SuppressWarnings("rawtypes")
+ private final GlobalRuleDefinitionExecutor executor;
+
/**
* Execute update.
*/
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("unchecked")
public void executeUpdate() {
- GlobalRuleDefinitionExecutor executor =
TypedSPILoader.getService(GlobalRuleDefinitionExecutor.class,
sqlStatement.getClass());
- Class<? extends RuleConfiguration> ruleConfigClass =
executor.getRuleConfigurationClass();
Collection<RuleConfiguration> ruleConfigs =
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getConfigurations();
- RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigs, ruleConfigClass);
+ RuleConfiguration currentRuleConfig =
findCurrentRuleConfiguration(ruleConfigs, executor.getRuleConfigurationClass());
executor.checkBeforeUpdate(currentRuleConfig, sqlStatement);
contextManager.getInstanceContext().getModeContextManager().alterGlobalRuleConfiguration(processUpdate(ruleConfigs,
sqlStatement, executor, currentRuleConfig));
}
diff --git
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java
index f4068e1b8ea..7c700578ad2 100644
---
a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java
+++
b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDropExecutor.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
@@ -95,4 +96,14 @@ public interface DatabaseRuleDropExecutor<T extends
SQLStatement, R extends Rule
default Collection<String> getIdenticalData(final Collection<String>
currentRules, final Collection<String> toBeDroppedRules) {
return currentRules.stream().filter(each ->
toBeDroppedRules.stream().anyMatch(each::equalsIgnoreCase)).collect(Collectors.toSet());
}
+
+ // TODO Remove when metadata structure adjustment completed. #25485
+ /**
+ * Drop rule configuration operate.
+ *
+ * @param sqlStatement SQL statement
+ * @param database database
+ */
+ default void operate(final T sqlStatement, final ShardingSphereDatabase
database) {
+ }
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java
index 8edc4fd1620..288876c3a9e 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/RDLBackendHandlerFactory.java
@@ -22,11 +22,8 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.statement.rdl.RDLStatement;
import
org.apache.shardingsphere.distsql.statement.rdl.resource.ResourceDefinitionStatement;
import
org.apache.shardingsphere.distsql.statement.rdl.rule.RuleDefinitionStatement;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLUpdateBackendHandler;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.LegacyRuleDefinitionBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule.RuleDefinitionBackendHandler;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
@@ -47,15 +44,6 @@ public final class RDLBackendHandlerFactory {
if (sqlStatement instanceof ResourceDefinitionStatement) {
return new DistSQLUpdateBackendHandler(sqlStatement,
connectionSession);
}
- return getRuleBackendHandler((RuleDefinitionStatement) sqlStatement,
connectionSession);
- }
-
- private static DistSQLBackendHandler getRuleBackendHandler(final
RuleDefinitionStatement sqlStatement, final ConnectionSession
connectionSession) {
- // TODO Remove when metadata structure adjustment completed. #25485
- String modeType =
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeConfiguration().getType();
- if ("Cluster".equals(modeType) || "Standalone".equals(modeType)) {
- return new RuleDefinitionBackendHandler(sqlStatement,
connectionSession);
- }
- return new LegacyRuleDefinitionBackendHandler(sqlStatement,
connectionSession);
+ return new RuleDefinitionBackendHandler((RuleDefinitionStatement)
sqlStatement, connectionSession);
}
}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandlerTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandlerTest.java
deleted file mode 100644
index febe8bf989b..00000000000
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandlerTest.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.rule;
-
-import io.netty.util.DefaultAttributeMap;
-import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.fixture.CreateFixtureRuleStatement;
-import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
-import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
-import org.apache.shardingsphere.test.mock.AutoMockExtension;
-import org.apache.shardingsphere.test.mock.StaticMockSettings;
-import org.apache.shardingsphere.transaction.api.TransactionType;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-
-import java.util.Collections;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@ExtendWith(AutoMockExtension.class)
-@StaticMockSettings(ProxyContext.class)
-class LegacyRuleDefinitionBackendHandlerTest {
-
- @Test
- void assertExecute() {
- ConnectionSession connectionSession = new
ConnectionSession(mock(MySQLDatabaseType.class), TransactionType.LOCAL, new
DefaultAttributeMap());
- connectionSession.setCurrentDatabase("foo_db");
- ShardingSphereDatabase database = mockDatabase();
- ContextManager contextManager = mockContextManager(database);
-
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
-
when(ProxyContext.getInstance().getDatabase("foo_db")).thenReturn(database);
- ResponseHeader response = new LegacyRuleDefinitionBackendHandler(new
CreateFixtureRuleStatement(), connectionSession).execute();
- assertThat(response, instanceOf(UpdateResponseHeader.class));
-
assertThat(connectionSession.getTransactionStatus().getTransactionType(),
is(TransactionType.LOCAL));
- }
-
- private ContextManager mockContextManager(final ShardingSphereDatabase
database) {
- ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
-
when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database);
- return result;
- }
-
- private ShardingSphereDatabase mockDatabase() {
- ShardingSphereDatabase result = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- RuleMetaData ruleMetaData = mock(RuleMetaData.class,
RETURNS_DEEP_STUBS);
-
when(ruleMetaData.getConfigurations()).thenReturn(Collections.emptyList());
- when(result.getRuleMetaData()).thenReturn(ruleMetaData);
- return result;
- }
-}