Skip to content

Commit

Permalink
Fix test error
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangML committed Dec 24, 2024
1 parent ab2aead commit 9dac2f8
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.stream.Collectors;

/**
* Cluster export metadata generator.
Expand Down Expand Up @@ -68,15 +67,12 @@ public String generateJsonFormat() {
}

private Map<String, String> generatorDatabasesExportData() {
Collection<String> databaseNames = contextManager.getMetaDataContexts().getMetaData().getAllDatabases().stream()
.map(ShardingSphereDatabase::getName).collect(Collectors.toList());
Map<String, String> result = new LinkedHashMap<>(databaseNames.size(), 1F);
for (String each : databaseNames) {
ShardingSphereDatabase database = contextManager.getDatabase(each);
if (database.getResourceMetaData().getAllInstanceDataSourceNames().isEmpty()) {
Map<String, String> result = new LinkedHashMap<>(contextManager.getMetaDataContexts().getMetaData().getAllDatabases().size(), 1F);
for (ShardingSphereDatabase each : contextManager.getMetaDataContexts().getMetaData().getAllDatabases()) {
if (each.getResourceMetaData().getAllInstanceDataSourceNames().isEmpty()) {
continue;
}
result.put(each, new DatabaseExportMetaDataGenerator(database).generateYAMLFormat());
result.put(each.getName(), new DatabaseExportMetaDataGenerator(each).generateYAMLFormat());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public final class MetaDataImportExecutor {

private final YamlRuleConfigurationSwapperEngine ruleConfigSwapperEngine = new YamlRuleConfigurationSwapperEngine();

private final YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor();
private final YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor;

private final ContextManager contextManager;

public MetaDataImportExecutor(final ContextManager contextManager) {
this.contextManager = contextManager;
this.databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor(contextManager);
}

/**
* Import cluster configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.proxy.backend.util;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationCheckEngine;
Expand Down Expand Up @@ -47,7 +48,6 @@
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 java.sql.SQLException;
import java.util.Collection;
Expand All @@ -62,12 +62,15 @@
/**
* Yaml database configuration import executor.
*/
@RequiredArgsConstructor
public final class YamlDatabaseConfigurationImportExecutor {

private final YamlProxyDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlProxyDataSourceConfigurationSwapper();

private final DistSQLDataSourcePoolPropertiesValidator validateHandler = new DistSQLDataSourcePoolPropertiesValidator();

private final ContextManager contextManager;

/**
* Import proxy database from yaml configuration.
*
Expand All @@ -94,11 +97,11 @@ private void checkDataSources(final String databaseName, final Map<String, YamlP

private void checkDatabase(final String databaseName) {
ShardingSpherePreconditions.checkNotEmpty(databaseName, MissingRequiredDatabaseException::new);
ShardingSpherePreconditions.checkState(!ProxyContext.getInstance().databaseExists(databaseName), () -> new DatabaseCreateExistsException(databaseName));
ShardingSpherePreconditions.checkNotEmpty(databaseName, MissingRequiredDatabaseException::new);
ShardingSpherePreconditions.checkState(!contextManager.getMetaDataContexts().getMetaData().containsDatabase(databaseName), () -> new DatabaseCreateExistsException(databaseName));
}

private void addDatabase(final String databaseName) throws SQLException {
ContextManager contextManager = ProxyContext.getInstance().getContextManager();
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().createDatabase(databaseName);
DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(Collections.emptyMap(), contextManager.getMetaDataContexts().getMetaData().getProps());
contextManager.getMetaDataContexts().getMetaData().addDatabase(databaseName, protocolType, contextManager.getMetaDataContexts().getMetaData().getProps());
Expand All @@ -112,12 +115,11 @@ private void importDataSources(final String databaseName, final Map<String, Yaml
}
validateHandler.validate(propsMap);
try {
ProxyContext.getInstance().getContextManager().getPersistServiceFacade().getMetaDataManagerPersistService().registerStorageUnits(databaseName, propsMap);
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().registerStorageUnits(databaseName, propsMap);
} catch (final SQLException ex) {
throw new StorageUnitsOperateException("import", propsMap.keySet(), ex);
}
Map<String, StorageUnit> storageUnits = ProxyContext.getInstance().getContextManager()
.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits();
Map<String, StorageUnit> storageUnits = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits();
Map<String, StorageNode> toBeAddedStorageNode = StorageUnitNodeMapCreator.create(propsMap);
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
storageUnits.put(entry.getKey(), new StorageUnit(toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue())));
Expand All @@ -129,10 +131,10 @@ private void importRules(final String databaseName, final Collection<YamlRuleCon
return;
}
Collection<RuleConfiguration> ruleConfigs = new LinkedList<>();
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
MetaDataContexts metaDataContexts = contextManager.getMetaDataContexts();
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
swapToRuleConfigs(yamlRuleConfigs).values().forEach(each -> addRule(ruleConfigs, each, database));
ProxyContext.getInstance().getContextManager().getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService()
contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService()
.persist(metaDataContexts.getMetaData().getDatabase(databaseName).getName(), ruleConfigs);
}

Expand All @@ -145,7 +147,7 @@ private void addRule(final Collection<RuleConfiguration> ruleConfigs, final Rule
@SuppressWarnings({"unchecked", "rawtypes"})
private ShardingSphereRule buildRule(final RuleConfiguration ruleConfig, final ShardingSphereDatabase database) {
DatabaseRuleBuilder ruleBuilder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig);
ComputeNodeInstanceContext computeNodeInstanceContext = ProxyContext.getInstance().getContextManager().getComputeNodeInstanceContext();
ComputeNodeInstanceContext computeNodeInstanceContext = contextManager.getComputeNodeInstanceContext();
return ruleBuilder.build(ruleConfig, database.getName(), database.getProtocolType(), database.getResourceMetaData(), database.getRuleMetaData().getRules(), computeNodeInstanceContext);
}

Expand All @@ -161,6 +163,6 @@ private Map<Integer, RuleConfiguration> swapToRuleConfigs(final Collection<YamlR
}

private void dropDatabase(final String databaseName) throws SQLException {
ProxyContext.getInstance().getContextManager().getPersistServiceFacade().getMetaDataManagerPersistService().dropDatabase(databaseName);
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().dropDatabase(databaseName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@

class ExportDatabaseConfigurationExecutorTest {

private final ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);

@Test
void assertExecute() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("normal_db");
Map<String, StorageUnit> storageUnits = createStorageUnits();
when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits);
Expand All @@ -91,6 +90,7 @@ private Map<String, StorageUnit> createStorageUnits() {

@Test
void assertExecuteWithEmptyDatabase() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("empty_db");
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap());
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList());
Expand Down
Loading

0 comments on commit 9dac2f8

Please sign in to comment.