Comments (1)
The business supports global data centers, so routing enforcement based on the user's country is implemented in AOP aspects. However, some issues have been identified. For instance, Data Center A is configured with read-write separation. Forced routing might direct traffic to a secondary node, but if the current operation is a write operation, ShardingSphere may execute the write operation on the secondary node despite the enforced routing. Therefore, considering read-write separation, compatibility with read-write operations should be ensured. Currently, compatibility with ReadwriteSplittingSQLRouter has been implemented. The code is as follows:
public final class ShardingReadwriteSplittingSQLRouter implements SQLRouter {
@Override
public RouteContext createRouteContext(QueryContext queryContext, RuleMetaData ruleMetaData, ShardingSphereDatabase shardingSphereDatabase, ReadwriteSplittingRule rule, ConfigurationProperties configurationProperties, ConnectionContext connectionContext) {
RouteContext result = new RouteContext();
ReadwriteSplittingDataSourceRule singleDataSourceRule = rule.getSingleDataSourceRule();
String dataSourceName = (new ReadwriteSplittingDataSourceRouter(singleDataSourceRule, connectionContext)).route(queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
result.getRouteUnits().add(new RouteUnit(new RouteMapper(singleDataSourceRule.getName(), dataSourceName), Collections.emptyList()));
return result;
}
@Override
public void decorateRouteContext(RouteContext routeContext, QueryContext queryContext, ShardingSphereDatabase database, ReadwriteSplittingRule rule, ConfigurationProperties props, ConnectionContext connectionContext) {
Collection<RouteUnit> toBeRemoved = new LinkedList<>();
Collection<RouteUnit> toBeAdded = new LinkedList<>();
for (RouteUnit each : routeContext.getRouteUnits()) {
String dataSourceName = each.getDataSourceMapper().getLogicName();
Optional<ReadwriteSplittingDataSourceRule> dataSourceRule = rule.findDataSourceRule(dataSourceName);
if (dataSourceRule.isEmpty()) {
Map<String, ReadwriteSplittingDataSourceRule> dataSourceMappers = rule.getDataSourceRules();
for (Map.Entry<String, ReadwriteSplittingDataSourceRule> dataSourceMapper : dataSourceMappers.entrySet()) {
if (dataSourceMapper.getValue().getDisabledDataSourceNames().contains(dataSourceName)) {
dataSourceRule = rule.findDataSourceRule(dataSourceMapper.getKey());
if (dataSourceRule.isPresent()) {
toBeRemoved.add(each);
String actualDataSourceName = (new ReadwriteSplittingDataSourceRouter(dataSourceRule.get(), connectionContext)).route(queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), actualDataSourceName), each.getTableMappers()));
}
}
}
} else {
if (dataSourceRule.get().getName().equalsIgnoreCase(each.getDataSourceMapper().getActualName())) {
toBeRemoved.add(each);
String actualDataSourceName = (new ReadwriteSplittingDataSourceRouter(dataSourceRule.get(), connectionContext)).route(queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), actualDataSourceName), each.getTableMappers()));
}
}
}
routeContext.getRouteUnits().removeAll(toBeRemoved);
routeContext.getRouteUnits().addAll(toBeAdded);
}
@Override
public int getOrder() {
return ReadwriteSplittingOrder.ORDER + 2;
}
@Override
public Class<ReadwriteSplittingRule> getTypeClass() {
return ReadwriteSplittingRule.class;
}
from shardingsphere.
Related Issues (20)
- shardingsphere jdbc 5.2.1 custom standard sharding not available
- CREATE SHARDING TABLE REFERENCE RULE throws invalid sharding table reference. HOT 1
- Malformed packet when select from information_schema.tables
- why the xa transaction does not work?
- Link redirection error:shardingsphere/docs/community/content/involved /_index.cn.md and shardingsphere/docs/community/content/involved /_index.en.md
- Could not find org.apache.shardingsphere:shardingsphere-test-util:5.5.0 HOT 3
- Caused by: java.lang.NoClassDefFoundError: org/apache/shardingsphere/globalclock/core/yaml/config/YamlGlobalClockRuleConfiguration HOT 2
- Migrate the JDK used by nightly CI from Temurin to Zulu HOT 1
- Include the 5.5.0 tag in the final generated document HOT 1
- There seems to be a bug regarding the documentation HOT 4
- Support for Extending New Data Types based on Trunk Database Type
- Optimize sharding table index name rewriting rules and remove unnecessary suffix rewriting
- Is kingbase8 considered supporting? HOT 1
- How to perform dynamic desensitization?
- Encounter error "org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException: Table or view '%s' does not exist" with Read-Write Splitting HOT 5
- 5.5.0 start failed
- How to configure default data sources in version 5.* HOT 1
- shardingsphere-jdbc-5.5.0 causes spring boot to return JSON data as XML HOT 5
- About sharing-jdbc cannot build HikariCP sharded data source HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shardingsphere.