In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Sharding-JDBC architecture and source code example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Sharding-jdbc architecture
The Sharding-jdbc system architecture is divided into five parts:
SQL parsing
SQL routing
SQL rewriting
SQL execution
Result set merging
Let's analyze Sharding-jdbc from the above five parts.
Function module SQL parses SQL routing SQL routing sequence diagram
Call chain ShardingPreparedStatement.execute
Execution method
Org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.execute ()
Method source code
@ Override public boolean execute () throws SQLException {try {/ / Local cache empty clearPrevious (); / * * routing * * / shard (); / / initialize preparedStatement initPreparedStatementExecutor () / execute sql return preparedStatementExecutor.execute ();} finally {clearBatch ();}} BaseShardingEngine.shardorg.apache.shardingsphere.core.BaseShardingEngine.shard (String, List) public SQLRouteResult shard (final String sql, final List parameters) {List clonedParameters = cloneParameters (parameters); / / Route SQLRouteResult result = executeRoute (sql, clonedParameters) according to SQL / / rewrite sql result.getRouteUnits () .addAll (HintManager.isDatabaseShardingOnly ()? Convert (sql, clonedParameters, result): rewriteAndConvert (sql, clonedParameters, result); / / print routed sql if (shardingProperties.getValue (ShardingPropertiesConstant.SQL_SHOW)) {boolean showSimple = shardingProperties.getValue (ShardingPropertiesConstant.SQL_SIMPLE); SQLLogger.logSQL (sql, showSimple, result.getShardingStatement (), result.getRouteUnits ());} return result;} BaseShardingEngine.executeRoute
Some routing-related hook is performed here.
Org.apache.shardingsphere.core.BaseShardingEngine.executeRoute (String, List) private SQLRouteResult executeRoute (final String sql, final List clonedParameters) {routingHook.start (sql); try {SQLRouteResult result = route (sql, clonedParameters); routingHook.finishSuccess (result, metaData.getTables ()); return result / / CHECKSTYLE:OFF} catch (final Exception ex) {/ / CHECKSTYLE:ON routingHook.finishFailure (ex); throw ex }} PreparedStatementRoutingEngine.routeorg.apache.shardingsphere.core.route.PreparedStatementRoutingEngine.route (List) public SQLRouteResult route (final List parameters) {if (null = = sqlStatement) {/ / parse SQL sqlStatement = shardingRouter.parse (logicSQL, true) } / * step 1: find the corresponding physical table name according to the above asynchronous parsed sqlStatement and the configured routing rules * step 2: here is the master-slave (read-write) route, and decide whether to use the master database or the slave database according to the type of sql (select, DML). * / return masterSlaveRouter.route (shardingRouter.route (logicSQL, parameters, sqlStatement);} SQLParseEngine.parse0org.apache.shardingsphere.core.parse.SQLParseEngine.parse0 (String, boolean) private SQLStatement parse0 (final String sql, final boolean useCache) {. / / create a parsing engine that matches according to the database to parse the sql. For example, mysql's sql creates mysql's data parsing engine. SQLStatement result = new SQLParseKernel (ParseRuleRegistry.getInstance (), databaseType, sql). Parse (); if (useCache) {cache.put (sql, result);} return result;} SQLParseKernel.parse
This is parsing sql. This method is no longer in depth.
Org.apache.shardingsphere.core.parse.core.SQLParseKernel.parse () public SQLStatement parse () {/ / parse sql SQLAST ast = parserEngine.parse (); / / extract sql fragments Collection sqlSegments = extractorEngine.extract (ast); Map parameterMarkerIndexes = ast.getParameterMarkerIndexes (); return fillerEngine.fill (sqlSegments, parameterMarkerIndexes.size (), ast.getSqlStatementRule ()) } ParsingSQLRouter.route (important) org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter.route (String, List, SQLStatement) public SQLRouteResult route (final String logicSQL, final List parameters, final SQLStatement sqlStatement) {/ * generate different optimization engines according to the sql type. For example, I use the select statement for debugging here, and the generation is the ShardingSelectOptimizeEngine instance. * optimize statements * / ShardingOptimizedStatement shardingStatement = ShardingOptimizeEngineFactory.newInstance (sqlStatement) .statements (shardingRule, metaData.getTables (), logicSQL, parameters, sqlStatement); boolean needMergeShardingValues = isNeedMergeShardingValues (shardingStatement); if (shardingStatement instanceof ShardingConditionOptimizedStatement & & needMergeShardingValues) {checkSubqueryShardingValues (shardingStatement, ((ShardingConditionOptimizedStatement) shardingStatement). GetShardingConditions ()); mergeShardingConditions ((ShardingConditionOptimizedStatement) shardingStatement). GetShardingConditions () } / * get a routing engine here, and there are various engines. The common ones are StandardRoutingEngine and ComplexRoutingEngine * this time you get a StandardRoutingEngine routing engine. (the number of shardingtable is 1, or all tables are bound) * then execute the StandardRoutingEngine.route method * / RoutingResult routingResult = RoutingEngineFactory.newInstance (shardingRule, metaData.getDataSources (), shardingStatement) .route () If (needMergeShardingValues) {Preconditions.checkState (1 = = routingResult.getRoutingUnits (). Size (), "Must have one sharding with subquery.");} / / distributed primary key insertion if (shardingStatement instanceof ShardingInsertOptimizedStatement) {setGeneratedValues ((ShardingInsertOptimizedStatement) shardingStatement) } / / encryption EncryptOptimizedStatement encryptStatement = EncryptOptimizeEngineFactory.newInstance (sqlStatement) .encryption (shardingRule.getEncryptRule (), metaData.getTables (), logicSQL, parameters, sqlStatement); SQLRouteResult result = new SQLRouteResult (shardingStatement, encryptStatement); result.setRoutingResult (routingResult); return result } StandardRoutingEngine.route (important) org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine.route () public RoutingResult route () {if (isDMLForModify (optimizedStatement.getSQLStatement ()) & &! optimizedStatement.getTables (). IsSingleTable ()) {throw new ShardingException ("Cannot support Multiple-Table for'% slots.", optimizedStatement.getSQLStatement ()) } / * 1. Get the sub-table rule * 2 according to the logical table name, and get the DataNode according to the sub-table rule (key is the name of the physical table). * 3. Encapsulate the above dataNode into RoutingResult * / return generateRoutingResult (getDataNodes (shardingRule.getTableRule (logicTableName);} SQL rewrites SQL to perform sample analysis of Sharding-JDBC architecture and source code. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.