Giter Site home page Giter Site logo

Comments (1)

wu979 avatar wu979 commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.