This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 18745612142 Refactor `ImportDatabaseConfigurationHandler` (#24220)
18745612142 is described below
commit 18745612142dc006b4e0e83ae3708b45420ef14c
Author: Zichao <[email protected]>
AuthorDate: Fri Feb 17 18:33:21 2023 +0800
Refactor `ImportDatabaseConfigurationHandler` (#24220)
---
.../distsql/ral/RALBackendHandlerFactory.java | 3 --
...ava => ImportDatabaseConfigurationUpdater.java} | 28 +++++++------
...ingsphere.distsql.handler.ral.update.RALUpdater | 1 +
...=> ImportDatabaseConfigurationUpdaterTest.java} | 47 +++++++++-------------
4 files changed, 36 insertions(+), 43 deletions(-)
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index a760e84e8de..0cfd7330358 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -26,7 +26,6 @@ import
org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableGlobalRuleRALStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.scaling.UpdatableScalingRALStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.RefreshDatabaseMetaDataStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.RefreshTableMetaDataStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.SetDistVariableStatement;
@@ -37,7 +36,6 @@ import
org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.hint.HintRALBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.migration.update.UpdatableScalingRALBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.AlterReadwriteSplittingStorageUnitStatusStatementHandler;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.ImportDatabaseConfigurationHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.RefreshDatabaseMetaDataHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.RefreshTableMetaDataHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.SetDistVariableHandler;
@@ -60,7 +58,6 @@ public final class RALBackendHandlerFactory {
HANDLERS.put(AlterReadwriteSplittingStorageUnitStatusStatement.class,
AlterReadwriteSplittingStorageUnitStatusStatementHandler.class);
HANDLERS.put(RefreshDatabaseMetaDataStatement.class,
RefreshDatabaseMetaDataHandler.class);
HANDLERS.put(RefreshTableMetaDataStatement.class,
RefreshTableMetaDataHandler.class);
- HANDLERS.put(ImportDatabaseConfigurationStatement.class,
ImportDatabaseConfigurationHandler.class);
}
/**
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdater.java
similarity index 93%
rename from
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
rename to
proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdater.java
index 9bea892b3e0..9977705be9e 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandler.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdater.java
@@ -25,6 +25,7 @@ import
org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRu
import
org.apache.shardingsphere.dbdiscovery.yaml.swapper.YamlDatabaseDiscoveryRuleConfigurationSwapper;
import org.apache.shardingsphere.distsql.handler.exception.DistSQLException;
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
+import org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater;
import
org.apache.shardingsphere.distsql.handler.validate.DataSourcePropertiesValidateHandler;
import
org.apache.shardingsphere.distsql.parser.statement.ral.updatable.ImportDatabaseConfigurationStatement;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
@@ -43,14 +44,12 @@ import
org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
import
org.apache.shardingsphere.mask.yaml.swapper.YamlMaskRuleConfigurationSwapper;
-import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration;
import
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
import
org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.FileIOException;
-import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.UpdatableRALBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.DatabaseDiscoveryRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.EncryptRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.MaskRuleConfigurationImportChecker;
@@ -81,9 +80,9 @@ import java.util.Map;
import java.util.Map.Entry;
/**
- * Import database configuration handler.
+ * Import database configuration updater.
*/
-public final class ImportDatabaseConfigurationHandler extends
UpdatableRALBackendHandler<ImportDatabaseConfigurationStatement> {
+public final class ImportDatabaseConfigurationUpdater implements
RALUpdater<ImportDatabaseConfigurationStatement> {
private final ShardingRuleConfigurationImportChecker
shardingRuleConfigurationImportChecker = new
ShardingRuleConfigurationImportChecker();
@@ -102,23 +101,23 @@ public final class ImportDatabaseConfigurationHandler
extends UpdatableRALBacken
private final DataSourcePropertiesValidateHandler validateHandler = new
DataSourcePropertiesValidateHandler();
@Override
- protected void update(final ContextManager contextManager) throws
SQLException {
- File file = new File(getSqlStatement().getFilePath());
+ public void executeUpdate(final String databaseName, final
ImportDatabaseConfigurationStatement sqlStatement) throws SQLException {
+ File file = new File(sqlStatement.getFilePath());
YamlProxyDatabaseConfiguration yamlConfig;
try {
yamlConfig = YamlEngine.unmarshal(file,
YamlProxyDatabaseConfiguration.class);
} catch (final IOException ex) {
throw new FileIOException(ex);
}
- String databaseName = yamlConfig.getDatabaseName();
- checkDatabase(databaseName, file);
+ String yamlDatabaseName = yamlConfig.getDatabaseName();
+ checkDatabase(yamlDatabaseName, file);
checkDataSource(yamlConfig.getDataSources(), file);
- addDatabase(databaseName);
- addResources(databaseName, yamlConfig.getDataSources());
+ addDatabase(yamlDatabaseName);
+ addResources(yamlDatabaseName, yamlConfig.getDataSources());
try {
- addRules(databaseName, yamlConfig.getRules());
+ addRules(yamlDatabaseName, yamlConfig.getRules());
} catch (final DistSQLException ex) {
- dropDatabase(databaseName);
+ dropDatabase(yamlDatabaseName);
throw ex;
}
}
@@ -199,4 +198,9 @@ public final class ImportDatabaseConfigurationHandler
extends UpdatableRALBacken
private void dropDatabase(final String databaseName) {
ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager().dropDatabase(databaseName);
}
+
+ @Override
+ public String getType() {
+ return ImportDatabaseConfigurationStatement.class.getName();
+ }
}
diff --git
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater
index e87449041a1..1a4bf1ab11a 100644
---
a/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater
+++
b/proxy/backend/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.ral.update.RALUpdater
@@ -19,3 +19,4 @@
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.AlterInven
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.LabelComputeNodeUpdater
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.UnlabelComputeNodeUpdater
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.SetInstanceStatusUpdater
+org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.ImportDatabaseConfigurationUpdater
diff --git
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandlerTest.java
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
similarity index 77%
rename from
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandlerTest.java
rename to
proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
index 591f5bccb4e..b0ea8958e91 100644
---
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationHandlerTest.java
+++
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
@@ -30,8 +30,6 @@ import
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.DatabaseDiscoveryRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ReadwriteSplittingRuleConfigurationImportChecker;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShardingRuleConfigurationImportChecker;
-import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.junit.Before;
@@ -50,21 +48,13 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class ImportDatabaseConfigurationHandlerTest extends
ProxyContextRestorer {
-
- private final String shardingFilePath =
"/conf/import/config-sharding.yaml";
-
- private final String readwriteSplittingFilePath =
"/conf/import/config-readwrite-splitting.yaml";
-
- private final String dbDiscoveryFilePath =
"/conf/import/config-database-discovery.yaml";
+public final class ImportDatabaseConfigurationUpdaterTest extends
ProxyContextRestorer {
private final String sharding = "sharding_db";
@@ -84,52 +74,53 @@ public final class ImportDatabaseConfigurationHandlerTest
extends ProxyContextRe
@Mock
private DatabaseDiscoveryRuleConfigurationImportChecker
databaseDiscoveryRuleConfigurationImportChecker;
- private ImportDatabaseConfigurationHandler
importDatabaseConfigurationHandler;
+ private ImportDatabaseConfigurationUpdater
importDatabaseConfigurationUpdater;
private final Map<String, String> featureMap = new HashMap<>(3, 1);
@Before
public void setup() {
- featureMap.put(sharding, shardingFilePath);
- featureMap.put(readwriteSplitting, readwriteSplittingFilePath);
- featureMap.put(databaseDiscovery, dbDiscoveryFilePath);
+ featureMap.put(sharding, "/conf/import/config-sharding.yaml");
+ featureMap.put(readwriteSplitting,
"/conf/import/config-readwrite-splitting.yaml");
+ featureMap.put(databaseDiscovery,
"/conf/import/config-database-discovery.yaml");
}
@Test(expected = IllegalStateException.class)
public void assertImportDatabaseExecutorForSharding() throws
ReflectiveOperationException, SQLException {
init(sharding);
-
Plugins.getMemberAccessor().set(importDatabaseConfigurationHandler.getClass().getDeclaredField("shardingRuleConfigurationImportChecker"),
- importDatabaseConfigurationHandler,
shardingRuleConfigurationImportChecker);
+
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("shardingRuleConfigurationImportChecker"),
+ importDatabaseConfigurationUpdater,
shardingRuleConfigurationImportChecker);
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(sharding));
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(sharding).getRuleMetaData().getConfigurations());
- assertThat(importDatabaseConfigurationHandler.execute(),
instanceOf(UpdateResponseHeader.class));
+ importDatabaseConfigurationUpdater.executeUpdate(sharding,
+ new
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(sharding))).getPath()));
}
@Test(expected = IllegalStateException.class)
public void assertImportDatabaseExecutorForReadwriteSplitting() throws
ReflectiveOperationException, SQLException {
init(readwriteSplitting);
-
Plugins.getMemberAccessor().set(importDatabaseConfigurationHandler.getClass().getDeclaredField("readwriteSplittingRuleConfigurationImportChecker"),
- importDatabaseConfigurationHandler,
readwriteSplittingRuleConfigurationImportChecker);
+
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("readwriteSplittingRuleConfigurationImportChecker"),
+ importDatabaseConfigurationUpdater,
readwriteSplittingRuleConfigurationImportChecker);
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(readwriteSplitting));
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(readwriteSplitting).getRuleMetaData().getConfigurations());
- assertThat(importDatabaseConfigurationHandler.execute(),
instanceOf(UpdateResponseHeader.class));
+ importDatabaseConfigurationUpdater.executeUpdate(readwriteSplitting,
+ new
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(readwriteSplitting))).getPath()));
}
@Test(expected = IllegalStateException.class)
public void assertImportDatabaseExecutorForDatabaseDiscovery() throws
ReflectiveOperationException, SQLException {
init(databaseDiscovery);
-
Plugins.getMemberAccessor().set(importDatabaseConfigurationHandler.getClass().getDeclaredField("databaseDiscoveryRuleConfigurationImportChecker"),
- importDatabaseConfigurationHandler,
databaseDiscoveryRuleConfigurationImportChecker);
+
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("databaseDiscoveryRuleConfigurationImportChecker"),
+ importDatabaseConfigurationUpdater,
databaseDiscoveryRuleConfigurationImportChecker);
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(databaseDiscovery));
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseDiscovery).getRuleMetaData().getConfigurations());
- assertThat(importDatabaseConfigurationHandler.execute(),
instanceOf(UpdateResponseHeader.class));
+ importDatabaseConfigurationUpdater.executeUpdate(databaseDiscovery,
+ new
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(databaseDiscovery))).getPath()));
}
private void init(final String feature) throws
ReflectiveOperationException {
- ImportDatabaseConfigurationHandler handler =
importDatabaseConfigurationHandler = new ImportDatabaseConfigurationHandler();
- handler.init(new
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationHandlerTest.class.getResource(featureMap.get(feature))).getPath()),
- mock(ConnectionSession.class));
-
Plugins.getMemberAccessor().set(importDatabaseConfigurationHandler.getClass().getDeclaredField("validateHandler"),
importDatabaseConfigurationHandler, validateHandler);
+ importDatabaseConfigurationUpdater = new
ImportDatabaseConfigurationUpdater();
+
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("validateHandler"),
importDatabaseConfigurationUpdater, validateHandler);
ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(new
ShardingSphereSchema(createTableMap(), Collections.emptyMap()));