-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to config actual-data-nodes while my actual-data-nodes tables is dynamic。 #16725
Comments
|
The actualDatanode is dynamic ,for example, today's ActualDatanode is [table_name_20220401..table_name_20220411], and become [table_name_20220402..table_name_20220412] tomorrow,should i use a Scheduling to set ActualDatanode everyday ? but i can't find how to do this. |
actual-data-nodes: ds0.$->{com.lingh.LocalShardingDatabasesAndTablesUtil.getActualDataNodes()} public class LocalShardingDatabasesAndTablesUtil {
public static List<String> getActualDataNodes() {
LocalDate startTime = LocalDate.now().minusDays(10);
LocalDate endTime = LocalDate.now();
return LongStream.range(0, ChronoUnit.DAYS.between(startTime, endTime))
.mapToObj(startTime::plusDays)
.map(localDate -> "table_name_" + localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")))
.collect(Collectors.toList());
}
}
|
@linghengqian |
There is also some experiment in ShardingSphere 4.x to implement |
I try to refresh it by ContextManager.alterRuleConfiguration(final String schemaName, final Collection ruleConfigs),It seems to be working properly. |
package com.lingh.ao;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.LinkedList;
@Component
public class InitActualDataNodesAO {
@Resource
private ShardingSphereDataSource shardingSphereDataSource;
private final String logicTableNameForTOrder = "t_order";
private final String schemaNameForTOrder = "sharding_db";
public void testSharding() {
// generate actualDataNodes
String actualDataNodes = "ds-0.t_order_$->{[1..8]}";
this.updateShardRuleActualDataNodes(shardingSphereDataSource, schemaNameForTOrder, logicTableNameForTOrder, actualDataNodes);
}
private void updateShardRuleActualDataNodes(ShardingSphereDataSource dataSource, String schemaName, String logicTableName, String newActualDataNodes) {
// Context manager.
org.apache.shardingsphere.mode.manager.ContextManager contextManager = dataSource.getContextManager();
// Rule configuration.
Collection<RuleConfiguration> newRuleConfigList = new LinkedList<>();
Collection<RuleConfiguration> oldRuleConfigList = dataSource.getContextManager()
.getMetaDataContexts()
.getMetaData(schemaName)
.getRuleMetaData()
.getConfigurations();
for (RuleConfiguration oldRuleConfig : oldRuleConfigList) {
if (oldRuleConfig instanceof AlgorithmProvidedShardingRuleConfiguration) {
// Algorithm provided sharding rule configuration
AlgorithmProvidedShardingRuleConfiguration oldAlgorithmConfig = (AlgorithmProvidedShardingRuleConfiguration) oldRuleConfig;
AlgorithmProvidedShardingRuleConfiguration newAlgorithmConfig = new AlgorithmProvidedShardingRuleConfiguration();
// Sharding table rule configuration Collection
Collection<ShardingTableRuleConfiguration> newTableRuleConfigList = new LinkedList<>();
Collection<ShardingTableRuleConfiguration> oldTableRuleConfigList = oldAlgorithmConfig.getTables();
oldTableRuleConfigList.forEach(oldTableRuleConfig -> {
if (logicTableName.equals(oldTableRuleConfig.getLogicTable())) {
ShardingTableRuleConfiguration newTableRuleConfig = new ShardingTableRuleConfiguration(oldTableRuleConfig.getLogicTable(), newActualDataNodes);
newTableRuleConfig.setTableShardingStrategy(oldTableRuleConfig.getTableShardingStrategy());
newTableRuleConfig.setDatabaseShardingStrategy(oldTableRuleConfig.getDatabaseShardingStrategy());
newTableRuleConfig.setKeyGenerateStrategy(oldTableRuleConfig.getKeyGenerateStrategy());
newTableRuleConfigList.add(newTableRuleConfig);
} else {
newTableRuleConfigList.add(oldTableRuleConfig);
}
});
newAlgorithmConfig.setTables(newTableRuleConfigList);
newAlgorithmConfig.setAutoTables(oldAlgorithmConfig.getAutoTables());
newAlgorithmConfig.setBindingTableGroups(oldAlgorithmConfig.getBindingTableGroups());
newAlgorithmConfig.setBroadcastTables(oldAlgorithmConfig.getBroadcastTables());
newAlgorithmConfig.setDefaultDatabaseShardingStrategy(oldAlgorithmConfig.getDefaultDatabaseShardingStrategy());
newAlgorithmConfig.setDefaultTableShardingStrategy(oldAlgorithmConfig.getDefaultTableShardingStrategy());
newAlgorithmConfig.setDefaultKeyGenerateStrategy(oldAlgorithmConfig.getDefaultKeyGenerateStrategy());
newAlgorithmConfig.setDefaultShardingColumn(oldAlgorithmConfig.getDefaultShardingColumn());
newAlgorithmConfig.setShardingAlgorithms(oldAlgorithmConfig.getShardingAlgorithms());
newAlgorithmConfig.setKeyGenerators(oldAlgorithmConfig.getKeyGenerators());
newRuleConfigList.add(newAlgorithmConfig);
}
}
// update context
contextManager.alterRuleConfiguration(schemaName, newRuleConfigList);
}
} |
@linghengqian Thank you for your remind, I will take a look at this way. |
@strongduanmu @linghengqian |
@xieyongchao2 Replying to the closed issue is an inexplicable thing for the owner of this issue. I think it would be better if you open a new issue and provide a minimal replica demo, and then point out the association with this issue. |
@linghengqian In 5.1.2 version, ShardingSphereDataSource doesn't have getContextManager method. Is this solution needs to update? |
|
@linghengqian Understood, thanks. |
I found a simpler code actual-data-nodes: ds0.$->{com.lingh.LocalShardingDatabasesAndTablesUtil.getActualDataNodes()} public class LocalShardingDatabasesAndTablesUtil {
public static List<String> getActualDataNodes() {
LocalDate startTime = LocalDate.now().minusDays(10);
LocalDate endTime = LocalDate.now();
return LongStream.range(0, ChronoUnit.DAYS.between(startTime, endTime))
.mapToObj(startTime::plusDays)
.map(localDate -> "table_name_" + localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")))
.collect(Collectors.toList());
}
} public class InitActualDataNodesAO {
@Resource
private ShardingSphereDataSource shardingSphereDataSource;
private final String schemaNameForTOrder = "sharding_db";
public void testSharding() {
this.reloadShardRuleActualDataNodes(shardingSphereDataSource, schemaNameForTOrder);
}
private void reloadShardRuleActualDataNodes(ShardingSphereDataSource dataSource, String schemaName) {
// Context manager.
org.apache.shardingsphere.mode.manager.ContextManager contextManager = dataSource.getContextManager();
// Rule configuration.
Collection<RuleConfiguration> ruleConfigList = dataSource.getContextManager()
.getMetaDataContexts()
.getMetaData(schemaName)
.getRuleMetaData()
.getConfigurations();
// update context
contextManager.alterRuleConfiguration(schemaName, ruleConfigList);
}
} It's just need to execute |
hello , i Use custom table splitting strategy ,Successfully dynamically adding nodes
|
|
|
My shard-table will be created everyday。 It can't be configured by
<sharding:table-rule logic-table="t_mt" table-strategy-ref="self_hint_shard" actual-data-nodes="——"/>
my maven dependency is
org.apache.shardingsphere
shardingsphere-jdbc-core-spring-namespace
5.1.0
The text was updated successfully, but these errors were encountered: