Skip to content

Commit

Permalink
Refactor ConvertYamlConfigurationExecutor (#30627)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaigorJiang authored Mar 24, 2024
1 parent a4f1ae3 commit d4296b8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class BroadcastRuleConfigurationToDistSQLConverterTest {
void assertConvert() {
BroadcastRuleConfiguration ruleConfig = mock(BroadcastRuleConfiguration.class);
when(ruleConfig.getTables()).thenReturn(Arrays.asList("t_province", "t_city"));
BroadcastRuleConfigurationToDistSQLConverter converter = new BroadcastRuleConfigurationToDistSQLConverter();
String actual = converter.convert(ruleConfig);
String actual = new BroadcastRuleConfigurationToDistSQLConverter().convert(ruleConfig);
assertThat(actual, is("CREATE BROADCAST TABLE RULE t_province,t_city;"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DistSQLScriptConstants {

public static final String COMMA = ",";

public static final String SEMI = ";";

public static final String CREATE_DATABASE = "CREATE DATABASE %s;";

public static final String USE_DATABASE = "USE %s;";
Expand All @@ -42,7 +38,7 @@ public final class DistSQLScriptConstants {

public static final String KEY_PASSWORD = "password";

public static final String RESOURCE_DEFINITION = " %s ("
public static final String STORAGE_UNIT_DEFINITION = " %s ("
+ System.lineSeparator()
+ "URL='%s',"
+ System.lineSeparator()
Expand All @@ -54,7 +50,7 @@ public final class DistSQLScriptConstants {
+ System.lineSeparator()
+ ")";

public static final String RESOURCE_DEFINITION_WITHOUT_PASSWORD = " %s ("
public static final String STORAGE_UNIT_DEFINITION_WITHOUT_PASSWORD = " %s ("
+ System.lineSeparator()
+ "URL='%s',"
+ System.lineSeparator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,94 +74,94 @@ public Collection<LocalDataQueryResultRow> getRows(final ConvertYamlConfiguratio
}
Preconditions.checkNotNull(yamlConfig, "Invalid yaml file `%s`", file.getName());
Preconditions.checkNotNull(yamlConfig.getDatabaseName(), "`databaseName` in file `%s` is required.", file.getName());
return Collections.singleton(new LocalDataQueryResultRow(generateDistSQL(yamlConfig)));
return Collections.singleton(new LocalDataQueryResultRow(convertYamlConfigToDistSQL(yamlConfig)));
}

@SuppressWarnings("unchecked")
private String generateDistSQL(final YamlProxyDatabaseConfiguration yamlConfig) {
private String convertYamlConfigToDistSQL(final YamlProxyDatabaseConfiguration yamlConfig) {
StringBuilder result = new StringBuilder();
appendResourceDistSQL(yamlConfig, result);
for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
result.append(TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, each.getClass()).convert(each));
result.append(convertDatabase(yamlConfig.getDatabaseName()));
if (!yamlConfig.getDataSources().isEmpty()) {
result.append(System.lineSeparator()).append(System.lineSeparator());
result.append(convertDataSources(yamlConfig.getDataSources()));
}
return result.toString();
}

@SuppressWarnings({"rawtypes", "unchecked"})
private Map<Integer, RuleConfiguration> swapToRuleConfigs(final YamlProxyDatabaseConfiguration yamlConfig) {
Map<Integer, RuleConfiguration> result = new TreeMap<>(Comparator.reverseOrder());
for (YamlRuleConfiguration each : yamlConfig.getRules()) {
YamlRuleConfigurationSwapper swapper = OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class, Collections.singleton(each.getRuleConfigurationType()))
.get(each.getRuleConfigurationType());
result.put(swapper.getOrder(), (RuleConfiguration) swapper.swapToObject(each));
if (!yamlConfig.getRules().isEmpty()) {
result.append(System.lineSeparator()).append(System.lineSeparator());
for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
result.append(TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, each.getClass()).convert(each));
result.append(System.lineSeparator()).append(System.lineSeparator());
}
}
return result;
}

private void appendResourceDistSQL(final YamlProxyDatabaseConfiguration yamlConfig, final StringBuilder stringBuilder) {
appendDatabase(yamlConfig.getDatabaseName(), stringBuilder);
appendResources(yamlConfig.getDataSources(), stringBuilder);
return result.toString();
}

private void appendDatabase(final String databaseName, final StringBuilder stringBuilder) {
stringBuilder.append(String.format(DistSQLScriptConstants.CREATE_DATABASE, databaseName)).append(System.lineSeparator())
.append(String.format(DistSQLScriptConstants.USE_DATABASE, databaseName)).append(System.lineSeparator()).append(System.lineSeparator());
private String convertDatabase(final String databaseName) {
return String.format(DistSQLScriptConstants.CREATE_DATABASE, databaseName) + System.lineSeparator() + String.format(DistSQLScriptConstants.USE_DATABASE, databaseName);
}

private void appendResources(final Map<String, YamlProxyDataSourceConfiguration> dataSources, final StringBuilder stringBuilder) {
if (dataSources.isEmpty()) {
return;
}
stringBuilder.append(DistSQLScriptConstants.REGISTER_STORAGE_UNIT);
private String convertDataSources(final Map<String, YamlProxyDataSourceConfiguration> dataSources) {
StringBuilder result = new StringBuilder(DistSQLScriptConstants.REGISTER_STORAGE_UNIT);
Iterator<Entry<String, YamlProxyDataSourceConfiguration>> iterator = dataSources.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, YamlProxyDataSourceConfiguration> entry = iterator.next();
DataSourceConfiguration dataSourceConfig = dataSourceConfigSwapper.swap(entry.getValue());
DataSourcePoolProperties props = DataSourcePoolPropertiesCreator.create(dataSourceConfig);
appendResource(entry.getKey(), props, stringBuilder);
result.append(convertDataSource(entry.getKey(), props));
if (iterator.hasNext()) {
stringBuilder.append(DistSQLScriptConstants.COMMA);
result.append(",");
}
}
stringBuilder.append(DistSQLScriptConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
result.append(";");
return result.toString();
}

private void appendResource(final String resourceName, final DataSourcePoolProperties dataSourcePoolProps, final StringBuilder stringBuilder) {
private String convertDataSource(final String resourceName, final DataSourcePoolProperties dataSourcePoolProps) {
Map<String, Object> connectionProps = dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties();
String url = (String) connectionProps.get(DistSQLScriptConstants.KEY_URL);
String username = (String) connectionProps.get(DistSQLScriptConstants.KEY_USERNAME);
String password = (String) connectionProps.get(DistSQLScriptConstants.KEY_PASSWORD);
String props = getResourceProperties(dataSourcePoolProps.getPoolPropertySynonyms(), dataSourcePoolProps.getCustomProperties());
String props = getDataSourcePoolProps(dataSourcePoolProps.getPoolPropertySynonyms(), dataSourcePoolProps.getCustomProperties());
if (Strings.isNullOrEmpty(password)) {
stringBuilder.append(String.format(DistSQLScriptConstants.RESOURCE_DEFINITION_WITHOUT_PASSWORD, resourceName, url, username, props));
} else {
stringBuilder.append(String.format(DistSQLScriptConstants.RESOURCE_DEFINITION, resourceName, url, username, password, props));
return String.format(DistSQLScriptConstants.STORAGE_UNIT_DEFINITION_WITHOUT_PASSWORD, resourceName, url, username, props);
}
return String.format(DistSQLScriptConstants.STORAGE_UNIT_DEFINITION, resourceName, url, username, password, props);
}

private String getResourceProperties(final PoolPropertySynonyms poolPropertySynonyms, final CustomDataSourcePoolProperties customDataSourcePoolProps) {
private String getDataSourcePoolProps(final PoolPropertySynonyms poolPropertySynonyms, final CustomDataSourcePoolProperties customDataSourcePoolProps) {
StringBuilder result = new StringBuilder();
appendProperties(poolPropertySynonyms.getStandardProperties(), result);
result.append(getDataSourcePoolProps(poolPropertySynonyms.getStandardProperties()));
if (!customDataSourcePoolProps.getProperties().isEmpty()) {
result.append(DistSQLScriptConstants.COMMA);
appendProperties(customDataSourcePoolProps.getProperties(), result);
result.append(",");
result.append(getDataSourcePoolProps(customDataSourcePoolProps.getProperties()));
}
return result.toString();
}

private void appendProperties(final Map<String, Object> props, final StringBuilder stringBuilder) {
private String getDataSourcePoolProps(final Map<String, Object> props) {
StringBuilder result = new StringBuilder();
Iterator<Entry<String, Object>> iterator = props.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
if (null == entry.getValue()) {
continue;
}
stringBuilder.append(String.format(DistSQLScriptConstants.PROPERTY, entry.getKey(), entry.getValue()));
result.append(String.format(DistSQLScriptConstants.PROPERTY, entry.getKey(), entry.getValue()));
if (iterator.hasNext()) {
stringBuilder.append(DistSQLScriptConstants.COMMA).append(' ');
result.append(",");
}
}
return result.toString();
}

@SuppressWarnings({"rawtypes", "unchecked"})
private Map<Integer, RuleConfiguration> swapToRuleConfigs(final YamlProxyDatabaseConfiguration yamlConfig) {
Map<Integer, RuleConfiguration> result = new TreeMap<>(Comparator.reverseOrder());
for (YamlRuleConfiguration each : yamlConfig.getRules()) {
YamlRuleConfigurationSwapper swapper = OrderedSPILoader.getServicesByClass(YamlRuleConfigurationSwapper.class, Collections.singleton(each.getRuleConfigurationType()))
.get(each.getRuleConfigurationType());
result.put(swapper.getOrder(), (RuleConfiguration) swapper.swapToObject(each));
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ REGISTER STORAGE UNIT ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_encrypt_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_1 (
URL='jdbc:mysql://127.0.0.1:3306/demo_encrypt_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE ENCRYPT RULE t_encrypt (
Expand Down
12 changes: 6 additions & 6 deletions proxy/backend/core/src/test/resources/expected/convert-mix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ REGISTER STORAGE UNIT ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_1 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_2 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_2?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_3 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_3?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_4 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_4?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_5 (
URL='jdbc:mysql://127.0.0.1:3306/demo_ds_5?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE READWRITE_SPLITTING RULE readwrite_ds_0 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ REGISTER STORAGE UNIT write_ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_write_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), read_ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_read_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), read_ds_1 (
URL='jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE READWRITE_SPLITTING RULE readwrite_ds (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ REGISTER STORAGE UNIT ds (
URL='jdbc:mysql://127.0.0.1:3306/demo_shadow_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), shadow_ds (
URL='jdbc:mysql://127.0.0.1:3306/demo_shadow_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE SHADOW RULE shadowDataSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ REGISTER STORAGE UNIT ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_sharding_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_1 (
URL='jdbc:mysql://127.0.0.1:3306/demo_sharding_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE SHARDING TABLE RULE t_order (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ REGISTER STORAGE UNIT ds_0 (
URL='jdbc:mysql://127.0.0.1:3306/demo_sharding_ds_0?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
), ds_1 (
URL='jdbc:mysql://127.0.0.1:3306/demo_sharding_ds_1?useSSL=false',
USER='root',
PASSWORD='12345678',
PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifetimeMilliseconds'='1800000', 'idleTimeoutMilliseconds'='60000', 'maxPoolSize'='50')
PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetimeMilliseconds'='1800000','idleTimeoutMilliseconds'='60000','maxPoolSize'='50')
);

CREATE SHARDING TABLE RULE t_order (
Expand Down

0 comments on commit d4296b8

Please sign in to comment.