You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "DragonZhang123 (via GitHub)" <gi...@apache.org> on 2025/08/01 13:03:05 UTC
[I] ShardingSphere fails to apply multiple custom sharding algorithms correctly, always uses the first defined algorithm in configuration [shardingsphere]
DragonZhang123 opened a new issue, #36147:
URL: https://github.com/apache/shardingsphere/issues/36147
Two distinct sharding algorithms (YearTableShardingAlgorithm and MonthTableShardingAlgorithm) are defined in the ShardingSphere configuration file. However, at runtime, all tables use the first algorithm listed in the configuration, leading to incorrect sharding logic.
Which version of ShardingSphere did you use?
5.5.1
Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
ShardingSphere-JDBC
Expected behavior
Table t_year_table should use YearTableShardingAlgorithm (sharding by year).
Table t_month_table should use MonthTableShardingAlgorithm (sharding by month).
Actual behavior
Both t_year_table and t_month_table use YearTableShardingAlgorithm (the first algorithm defined in the configuration).
Reason analyze (If you can)
Sharding algorithm instances in the configuration may be overwritten (e.g., singleton pattern causes duplicate registration).
ShardingSphere fails to parse the algorithm order in the configuration, prioritizing the first instance.
Custom algorithms are not correctly bound to their shardingColumn or shardingAlgorithmName.
rules:
- !SHARDING
tables:
t_year_table:
actualDataNodes: ds0.t_year_table_$->{2020..2025}
tableStrategy:
standard:
shardingColumn: year_date
shardingAlgorithmName: year_algorithm
t_month_table:
actualDataNodes: ds0.t_month_table_$->{202001..202512}
tableStrategy:
standard:
shardingColumn: month_date
shardingAlgorithmName: month_algorithm
shardingAlgorithms:
year_algorithm:
type: CLASS_BASED
props:
strategy: STANDARD
algorithmClassName: com.example.YearTableShardingAlgorithm
month_algorithm:
type: CLASS_BASED
props:
strategy: STANDARD
algorithmClassName: com.example.MonthTableShardingAlgorithm
// YearTableShardingAlgorithm.java
public class YearTableShardingAlgorithm implements StandardShardingAlgorithm<Date> {
private static final String TABLE_PATTERN = "t_year_table_%04d";
// Implement year-based sharding logic
}
// MonthTableShardingAlgorithm.java
public class MonthTableShardingAlgorithm implements StandardShardingAlgorithm<Date> {
private static final String TABLE_PATTERN = "t_month_table_%04d%02d";
// Implement month-based sharding logic
}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ShardingSphere fails to apply multiple custom sharding algorithms correctly, always uses the first defined algorithm in configuration [shardingsphere]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #36147:
URL: https://github.com/apache/shardingsphere/issues/36147#issuecomment-3697754713
There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ShardingSphere fails to apply multiple custom sharding algorithms correctly, always uses the first defined algorithm in configuration [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu commented on issue #36147:
URL: https://github.com/apache/shardingsphere/issues/36147#issuecomment-3677712258
## Problem Understanding:
- On ShardingSphere-JDBC 5.5.1 you configured two CLASS_BASED custom StandardShardingAlgorithms (year and month). Each logical table should use its own algorithm, but at runtime both tables appear to use the first algorithm.
## Root Cause
- Framework code does not favor the first entry: algorithms are bound by shardingAlgorithmName via a map, not by order. CLASS_BASED creates a separate instance per config, so the likely causes are config not actually loaded/overridden, or custom algorithm logic/static state making both behave the same.
## Problem Analysis
- ShardingRule loads shardingAlgorithms into a map by name (features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java:120-126); routing looks up by name, not by order.
- CLASS_BASED builds independent instances with their own props (features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java:68-89 and Factory); there’s no logic to overwrite the second with
the first.
- If both tables act the same, common causes are: the effective YAML is not the intended one (e.g., not loaded or overridden); a typo/structure issue causes both tables to reference the same shardingAlgorithmName; or custom algorithm code uses static/shared state or returns the same result so it looks like one algorithm.
## Problem Conclusion
- With current info it’s not yet a confirmed ShardingSphere bug. Please share: (1) the full effective config file (datasource + rules) used at runtime; (2) full code of both custom algorithms, especially init and routing logic, and whether static state exists; (3) the SQL that reproduces the issue with expected vs actual routed targets; (4) startup or debug logs showing algorithm registration/routing (enable sharding-sphere-sql logs). This will allow deeper pinpointing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ShardingSphere fails to apply multiple custom sharding algorithms correctly, always uses the first defined algorithm in configuration [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu closed issue #36147: ShardingSphere fails to apply multiple custom sharding algorithms correctly, always uses the first defined algorithm in configuration
URL: https://github.com/apache/shardingsphere/issues/36147
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org