Skip to content

Commit

Permalink
Optimize the rollback strategy for import database config failed (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang authored Oct 14, 2024
1 parent d1b8239 commit 10a270b
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.shardingsphere.infra.util.SphereEx.Type;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration;
Expand All @@ -63,6 +64,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand All @@ -88,11 +90,21 @@ public void importDatabaseConfiguration(final YamlProxyDatabaseConfiguration yam
importDataSources(databaseName, yamlConfig.getDataSources());
importRules(databaseName, yamlConfig.getRules());
} catch (final ShardingSphereSQLException ex) {
dropDatabase(databaseName);
dropDatabaseUntilSuccessful(databaseName);
throw ex;
}
}

private void dropDatabaseUntilSuccessful(final String databaseName) {
do {
dropDatabase(databaseName);
try {
TimeUnit.MILLISECONDS.sleep(200);
} catch (final InterruptedException ignored) {
}
} while (ProxyContext.getInstance().getContextManager().getMetaDataContexts().getPersistService().getRepository().isExisted(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)));
}

private void checkDataSources(final String databaseName, final Map<String, YamlProxyDataSourceConfiguration> dataSources) {
ShardingSpherePreconditions.checkState(!dataSources.isEmpty(), () -> new EmptyStorageUnitException(databaseName));
}
Expand Down

0 comments on commit 10a270b

Please sign in to comment.