version 4.0.0-RC2: key generated by SnowflakeShardingKeyGenerator mod 4 always is 0/1, expected 0/1/2/3
See original GitHub issueHi. I found a fact about the sharding key generated by built-in snowflake algorithm. The result of the key mod 4 always is 0/1, to me, it is wierd. What I expected is, the result should be 0/1/2/3, and the key should evenly distributed to all the sharded tables. I am new to ShardingSphere and the snowflake algorithm. Hope I can get some help here.
I found out there is no this problem when keys are generated in a same millisecond.
Thanks.
`public final class PreciseModuloShardingTableAlgorithm implements PreciseShardingAlgorithm<Long> {
@Override
public String doSharding(final Collection<String> tableNames, final PreciseShardingValue<Long> shardingValue) {
String tableName = (String) tableNames.toArray()[0];
if(tableName.startsWith("table_a")) {
for (String each : tableNames) {
if (each.endsWith(shardingValue.getValue() % 4 + "")) {
return each;
}
}
}
if (tableName.startsWith("table_b")) {
for (String each : tableNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
}
throw new UnsupportedOperationException();
}
}`
In database config class: ` DataSource getShardingDataSource() throws SQLException { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(new TableRuleConfiguration("table_a", "ds.table_a_$->{0..3}"));
shardingRuleConfig.getBindingTableGroups().add("table_a");
shardingRuleConfig.getTableRuleConfigs().add(new TableRuleConfiguration("table_b", "ds.admin_behaviour_track_$->{0..2}"));
shardingRuleConfig.getBindingTableGroups().add("table_b");
shardingRuleConfig.setDefaultKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "id"));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new PreciseModuloShardingTableAlgorithm()));
Properties properties = new Properties();
properties.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), "true");
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
}`
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

DISCUSS:
Writing the description and constraint of configuring the max vibration offset in the official document.
explanation Using the snowflake algorithm as the partition key strategy, the max vibration offset needs to be configured.
Constraints The max vibration offset is not recommended to take the number not 2 ^ n but less than 2 ^ 12 When the number of physical tables is 2 ^ n, the max vibration offset needs to be configured as 2 ^ n-1
Any Suggestions?
Me too, and hope you’ll get involved