Skip to content

Commit

Permalink
Check tableRule and throw error information friendly (#3734)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion authored and terrymanu committed Dec 13, 2019
1 parent 9bb82d9 commit eb8b6ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Expand Up @@ -23,7 +23,9 @@
import lombok.ToString;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.core.config.ShardingConfigurationException;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.spi.algorithm.keygen.ShardingKeyGeneratorServiceLoader;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory;
Expand Down Expand Up @@ -107,6 +109,7 @@ public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDat
generateKeyColumn = getGenerateKeyColumn(tableRuleConfig.getKeyGeneratorConfig(), defaultGenerateKeyColumn);
shardingKeyGenerator = containsKeyGeneratorConfiguration(tableRuleConfig)
? new ShardingKeyGeneratorServiceLoader().newService(tableRuleConfig.getKeyGeneratorConfig().getType(), tableRuleConfig.getKeyGeneratorConfig().getProperties()) : null;
checkRule(dataNodes);
}

private void cacheActualDatasourcesAndTables() {
Expand Down Expand Up @@ -227,4 +230,10 @@ int findActualTableIndex(final String dataSourceName, final String actualTableNa
boolean isExisted(final String actualTableName) {
return actualTables.contains(actualTableName);
}

private void checkRule(final List<String> dataNodes) {
if (isEmptyDataNodes(dataNodes) && null != tableShardingStrategy && !(tableShardingStrategy instanceof NoneShardingStrategy)) {
throw new ShardingConfigurationException("ActualDataNodes must be configured if want to shard tables for logicTable [%s]", logicTable);
}
}
}
Expand Up @@ -21,7 +21,9 @@
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.core.config.ShardingConfigurationException;
import org.apache.shardingsphere.core.strategy.keygen.fixture.IncrementShardingKeyGenerator;
import org.junit.Test;

Expand Down Expand Up @@ -111,11 +113,18 @@ public void assertActualTableNameNotExisted() {
assertFalse(actual.isExisted("table_3"));
}

@Test(expected = ShardingConfigurationException.class)
public void assertActualDataNodesNotConfigured() {
TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("LOGIC_TABLE", "");
tableRuleConfiguration.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("shardingColumn", "table_${shardingColumn % 3}"));
TableRule actual = new TableRule(tableRuleConfiguration, createShardingDataSourceNames(), null);
}

@Test
public void assertToString() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
String actualString = "TableRule(logicTable=logic_table, actualDataNodes=[DataNode(dataSourceName=ds0, tableName=table_0), DataNode(dataSourceName=ds0, tableName=table_1), "
+ "DataNode(dataSourceName=ds0, tableName=table_2), DataNode(dataSourceName=ds1, tableName=table_0), DataNode(dataSourceName=ds1, tableName=table_1), "
String actualString = "TableRule(logicTable=logic_table, actualDataNodes=[DataNode(dataSourceName=ds0, tableName=table_0), DataNode(dataSourceName=ds0, tableName=table_1), "
+ "DataNode(dataSourceName=ds0, tableName=table_2), DataNode(dataSourceName=ds1, tableName=table_0), DataNode(dataSourceName=ds1, tableName=table_1), "
+ "DataNode(dataSourceName=ds1, tableName=table_2)], databaseShardingStrategy=null, tableShardingStrategy=null, generateKeyColumn=null, shardingKeyGenerator=null)";
assertThat(actual.toString(), is(actualString));
}
Expand Down

0 comments on commit eb8b6ac

Please sign in to comment.