question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

version 4.0.0-RC2: key generated by SnowflakeShardingKeyGenerator mod 4 always is 0/1, expected 0/1/2/3

See original GitHub issue

Hi. 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:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
haetaocommented, Nov 7, 2019

DISCUSS:

Writing the description and constraint of configuring the max vibration offset in the official document.

  1. explanation Using the snowflake algorithm as the partition key strategy, the max vibration offset needs to be configured.

  2. 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?

0reactions
geomonlincommented, Nov 5, 2019

Me too, and hope you’ll get involved

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found