Skip to content

Commit

Permalink
Adjust algorithm parameters in RQL type DistSQL to json format (#29104)
Browse files Browse the repository at this point in the history
* Adjust algorithm parameters in RQL type DistSQL to json format

* Fix checkstyle error

* Fix e2e test error
  • Loading branch information
jiangML authored Nov 21, 2023
1 parent 6231220 commit 46fc668
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
Expand All @@ -46,7 +47,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (data.hasNext()) {
Entry<String, AlgorithmConfiguration> entry = data.next();
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), entry.getValue().getProps()));
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps())));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGeneratorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
Expand Down Expand Up @@ -48,7 +49,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (data.hasNext()) {
Entry<String, AlgorithmConfiguration> entry = data.next();
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), entry.getValue().getProps().toString()));
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps())));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowUnusedShardingAuditorsStatement;
Expand Down Expand Up @@ -50,7 +51,7 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
for (Entry<String, AlgorithmConfiguration> entry : shardingRuleConfig.getAuditors().entrySet()) {
if (!inUsedAuditors.contains(entry.getKey())) {
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), entry.getValue().getProps().toString()));
result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps())));
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingAuditorsExecutor;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -54,7 +55,7 @@ void assertGetRowData() {
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("sharding_key_required_auditor"));
assertThat(row.getCell(2), is("DML_SHARDING_CONDITIONS"));
assertThat(row.getCell(3).toString(), is("{}"));
assertThat(row.getCell(3).toString(), is("{\"key\":\"value\"}"));
}

@Test
Expand All @@ -70,7 +71,7 @@ void assertGetColumnNames() {

private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", PropertiesBuilder.build(new Property("key", "value"))));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGeneratorExecutor;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGeneratorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -55,7 +56,7 @@ void assertGetRowData() {
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("snowflake"));
assertThat(row.getCell(2), is("SNOWFLAKE"));
assertThat(row.getCell(3), is("{}"));
assertThat(row.getCell(3), is("{\"key\":\"value\"}"));
}

@Test
Expand All @@ -72,7 +73,7 @@ void assertGetColumnNames() {

private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", PropertiesBuilder.build(new Property("key", "value"))));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.shardingsphere.sharding.distsql.handler.query.ShowUnusedShardingAuditorsExecutor;
import org.apache.shardingsphere.sharding.distsql.statement.ShowUnusedShardingAuditorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collection;
Expand All @@ -53,7 +55,7 @@ void assertGetRowData() {
LocalDataQueryResultRow row = iterator.next();
assertThat(row.getCell(1), is("fixture"));
assertThat(row.getCell(2), is("FIXTURE"));
assertThat(row.getCell(3), is("{}"));
assertThat(row.getCell(3), is("{\"key\":\"value\"}"));
}

@Test
Expand All @@ -78,7 +80,7 @@ private ShardingSphereDatabase mockDatabase() {
private RuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
result.getAuditors().put("fixture", new AlgorithmConfiguration("FIXTURE", null));
result.getAuditors().put("fixture", new AlgorithmConfiguration("FIXTURE", PropertiesBuilder.build(new Property("key", "value"))));
result.getAutoTables().add(createShardingAutoTableRuleConfiguration());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public final class PropertiesConverter {
* @return converted string content
*/
public static String convert(final Properties props) {
if (null == props) {
return "";
}
Map<Object, Object> sortedProps = new LinkedHashMap<>();
props.keySet().stream().map(Object::toString).sorted().forEach(each -> sortedProps.put(each, props.get(each)));
return sortedProps.isEmpty() ? "" : JsonUtils.toJsonString(sortedProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.globalclock.distsql.statement.queryable.ShowGlobalClockRuleStatement;
import org.apache.shardingsphere.infra.props.PropertiesConverter;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -36,7 +37,8 @@ public final class ShowGlobalClockRuleExecutor implements MetaDataRequiredQuerya
@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereMetaData metaData, final ShowGlobalClockRuleStatement sqlStatement) {
GlobalClockRuleConfiguration ruleConfig = metaData.getGlobalRuleMetaData().getSingleRule(GlobalClockRule.class).getConfiguration();
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getType(), ruleConfig.getProvider(), String.valueOf(ruleConfig.isEnabled()), ruleConfig.getProps().toString()));
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getType(), ruleConfig.getProvider(),
String.valueOf(ruleConfig.isEnabled()), PropertiesConverter.convert(ruleConfig.getProps())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

package org.apache.shardingsphere.globalclock.distsql.handler.query;

import org.apache.shardingsphere.globalclock.api.config.GlobalClockRuleConfiguration;
import org.apache.shardingsphere.globalclock.core.rule.GlobalClockRule;
import org.apache.shardingsphere.globalclock.core.rule.builder.DefaultGlobalClockRuleConfigurationBuilder;
import org.apache.shardingsphere.globalclock.distsql.statement.queryable.ShowGlobalClockRuleStatement;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.globalclock.distsql.statement.queryable.ShowGlobalClockRuleStatement;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collection;
Expand All @@ -51,7 +53,7 @@ void assertGlobalClockRule() {
assertThat(row.getCell(1), is("TSO"));
assertThat(row.getCell(2), is("local"));
assertThat(row.getCell(3), is("false"));
assertThat(row.getCell(4), is("{}"));
assertThat(row.getCell(4), is("{\"key\":\"value\"}"));
}

@Test
Expand All @@ -68,7 +70,8 @@ void assertGetColumnNames() {

private ShardingSphereMetaData mockMetaData() {
GlobalClockRule sqlParserRule = mock(GlobalClockRule.class);
when(sqlParserRule.getConfiguration()).thenReturn(new DefaultGlobalClockRuleConfigurationBuilder().build());
GlobalClockRuleConfiguration globalClockRuleConfig = new GlobalClockRuleConfiguration("TSO", "local", false, PropertiesBuilder.build(new Property("key", "value")));
when(sqlParserRule.getConfiguration()).thenReturn(globalClockRuleConfig);
return new ShardingSphereMetaData(new LinkedHashMap<>(), mock(ResourceMetaData.class),
new RuleMetaData(Collections.singleton(sqlParserRule)), new ConfigurationProperties(new Properties()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auditor_constant| IT.AUDITOR.FIXTURE| {}" />
<row values="auditor_constant| IT.AUDITOR.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| {}" />
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auditor_constant| IT.AUDITOR.FIXTURE| {}" />
<row values="auditor_constant| IT.AUDITOR.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| {}" />
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auditor_constant| IT.AUDITOR.FIXTURE| {}" />
<row values="auditor_constant| IT.AUDITOR.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="constant| IT.FIXTURE| {}" />
<row values="constant| IT.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auditor_constant| IT.AUDITOR.FIXTURE| {}" />
<row values="auditor_constant| IT.AUDITOR.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="constant| IT.FIXTURE| {}" />
<row values="constant| IT.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auditor_constant| IT.AUDITOR.FIXTURE| {}" />
<row values="auditor_constant| IT.AUDITOR.FIXTURE| " />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
<column name="type" />
<column name="props" />
</metadata>
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| {}" />
<row values="auto_increment| IT.AUTO_INCREMENT.FIXTURE| " />
</dataset>

0 comments on commit 46fc668

Please sign in to comment.