Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand sharding-jdbc 's ShardingMasterSlaveRouter

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to understand the ShardingMasterSlaveRouter of sharding-jdbc. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Mainly study the ShardingMasterSlaveRouterShardingMasterSlaveRouter of sharding-jdbc

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java

@ RequiredArgsConstructorpublic final class ShardingMasterSlaveRouter {private final Collection masterSlaveRules; / * Route Master slave after sharding. * * @ param sqlRouteResult SQL route result * @ return route result * / public SQLRouteResult route (final SQLRouteResult sqlRouteResult) {for (MasterSlaveRule each: masterSlaveRules) {route (each, sqlRouteResult);} return sqlRouteResult;} private void route (final MasterSlaveRule masterSlaveRule, final SQLRouteResult sqlRouteResult) {Collection toBeRemoved = new LinkedList (); Collection toBeAdded = new LinkedList () For (TableUnit each: sqlRouteResult.getRoutingResult (). GetTableUnits (). GetTableUnits ()) {if (! masterSlaveRule.getName (). EqualsIgnoreCase (each.getDataSourceName () {continue;} toBeRemoved.add (each); String actualDataSourceName; if (isMasterRoute (sqlRouteResult.getSqlStatement (). GetType () {MasterVisitedManager.setMasterVisited () ActualDataSourceName = masterSlaveRule.getMasterDataSourceName ();} else {actualDataSourceName = masterSlaveRule.getLoadBalanceAlgorithm (). GetDataSource (masterSlaveRule.getName (), masterSlaveRule.getMasterDataSourceName (), new ArrayList (masterSlaveRule.getSlaveDataSourceNames ();} toBeAdded.add (createNewTableUnit (actualDataSourceName, each));} sqlRouteResult.getRoutingResult (). GetTableUnits (). GetTableUnits (). RemoveAll (toBeRemoved) SqlRouteResult.getRoutingResult (). GetTableUnits (). GetTableUnits (). AddAll (toBeAdded);} private boolean isMasterRoute (final SQLType sqlType) {return SQLType.DQL! = sqlType | | MasterVisitedManager.isMasterVisited () | | HintManager.isMasterRouteOnly ();} private TableUnit createNewTableUnit (final String actualDataSourceName, final TableUnit originalTableUnit) {TableUnit result = new TableUnit (actualDataSourceName, originalTableUnit.getDataSourceName ()); result.getRoutingTables (). AddAll (originalTableUnit.getRoutingTables ()); return result }}

ShardingMasterSlaveRouter provides the route method, which returns sqlRouteResult. Here, the TableUnit collection of tableUnits of sqlRouteResult's routingResult has been modified.

SQLRouteResult

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java

RequiredArgsConstructor@Getter@Setterpublic final class SQLRouteResult {private final SQLStatement sqlStatement; private final GeneratedKey generatedKey; / / For multiple thread read cached sqlStatement, clone limit on SQLRouteResult, because limit will be modified after cache / / TODO need more good design here private Limit limit; private RoutingResult routingResult; private OptimizeResult optimizeResult; private final Collection routeUnits = new LinkedHashSet (); public SQLRouteResult (final SQLStatement sqlStatement) {this (sqlStatement, null);}}

SQLRouteResult has the RoutingResult property

RoutingResult

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingResult.java

@ Getterpublic class RoutingResult {private final TableUnits tableUnits = new TableUnits (); / * * Judge is route for single database and table only or not. * * @ return is route for single database and table only or not * / public boolean isSingleRouting () {return 1 = = tableUnits.getTableUnits () .size ();}

RoutingResult has the TableUnits property

TableUnits

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnits.java

@ Getter@ToStringpublic final class TableUnits {private final Collection tableUnits = new LinkedHashSet (); / * * Get all data source names. * * @ return all data source names * / public Collection getDataSourceNames () {Collection result = new HashSet (tableUnits.size (), 1); for (TableUnit each: tableUnits) {result.add (each.getDataSourceName ());} return result;} / * Get routing table via data source name and actual table name. * * @ param dataSourceName data source name * @ param actualTableName actual table name * @ return routing table * / public Optional getRoutingTable (final String dataSourceName, final String actualTableName) {for (TableUnit each: tableUnits) {Optional result = each.getRoutingTable (dataSourceName, actualTableName); if (result.isPresent ()) {return result;}} return Optional.absent () } / * Get actual tables group via data source name and logic tables' names. *

* Actual tables in same group are belong one logic name. *

* * @ param dataSourceName data source name * @ param logicTableNames logic tables' names * @ return actual tables group * / public List getActualTableNameGroups (final String dataSourceName, final Set logicTableNames) {List result = new ArrayList (); for (String logicTableName: logicTableNames) {Set actualTableNames = getActualTableNames (dataSourceName, logicTableName); if (! actualTableNames.isEmpty ()) {result.add (actualTableNames) }} return result;} private Set getActualTableNames (final String dataSourceName, final String logicTableName) {Set result = new HashSet (tableUnits.size (), 1); for (TableUnit each: tableUnits) {result.addAll (each.getActualTableNames (dataSourceName, logicTableName));} return result } / * Get map relationship between data source and logic tables via data sources' names. * * @ param dataSourceNames data sources' names * @ return map relationship between data source and logic tables * / public Map getDataSourceLogicTablesMap (final Collection dataSourceNames) {Map result = new HashMap (); for (String each: dataSourceNames) {Set logicTableNames = getLogicTableNames (each); if (! logicTableNames.isEmpty ()) {result.put (each, logicTableNames) } return result;} private Set getLogicTableNames (final String dataSourceName) {Set result = new HashSet (tableUnits.size (), 1); for (TableUnit each: tableUnits) {if (each.getDataSourceName (). EqualsIgnoreCase (dataSourceName)) {result.addAll (each.getLogicTableNames (dataSourceName));}} return result;}}

Inside TableUnits is a collection of TableUnit

TableUnit

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/TableUnit.java

@ RequiredArgsConstructor@Getter@EqualsAndHashCode@ToStringpublic final class TableUnit {private final String dataSourceName; private final String masterSlaveLogicDataSourceName; private final List routingTables = new LinkedList (); public TableUnit (final String dataSourceName) {this.dataSourceName = dataSourceName; masterSlaveLogicDataSourceName = dataSourceName;} / * * Get routing table via data source name and actual table name. * * @ param dataSourceName data source name * @ param actualTableName actual table name * @ return routing table * / public Optional getRoutingTable (final String dataSourceName, final String actualTableName) {for (RoutingTable each: routingTables) {if (dataSourceName.equalsIgnoreCase (masterSlaveLogicDataSourceName) & & each.getActualTableName (). EqualsIgnoreCase (actualTableName)) {return Optional.of (each);}} return Optional.absent () } / * Get actual tables' names via data source name. * * @ param dataSourceName data source name * @ param logicTableName logic table name * @ return actual tables' names * / public Set getActualTableNames (final String dataSourceName, final String logicTableName) {Set result = new HashSet (routingTables.size (), 1) For (RoutingTable each: routingTables) {if (dataSourceName.equalsIgnoreCase (this.dataSourceName) & & each.getLogicTableName (). EqualsIgnoreCase (logicTableName)) {result.add (each.getActualTableName ());}} return result;} / * Get logic tables' names via data source name. * * @ param dataSourceName data source name * @ return logic tables' names * / public Set getLogicTableNames (final String dataSourceName) {Set result = new HashSet (routingTables.size (), 1); for (RoutingTable each: routingTables) {if (dataSourceName.equalsIgnoreCase (this.dataSourceName)) {result.add (each.getLogicTableName ());}} return result;}}

TableUnit contains a collection of RoutingTable

RoutingTable

Incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/RoutingTable.java

@ RequiredArgsConstructor@Getter@EqualsAndHashCode@ToStringpublic final class RoutingTable {private final String logicTableName; private final String actualTableName;}

RoutingTable includes logicTableName and actualTableName

Summary

ShardingMasterSlaveRouter provides the route method, which returns sqlRouteResult. Here, the TableUnit collection of tableUnits of sqlRouteResult's routingResult has been modified.

The above is how to understand sharding-jdbc 's ShardingMasterSlaveRouter. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report