MyCat analysis 02

3. Key Look default c.execute (stmt, ServerParse.SELECT)

3.1 First check whether the connection status closed

3.2 Check whether the current state of affairs normal

3.3 whether checks have been selected after a DB, had no choice to return

3.4 compatible phpAdmin's, support for analog Mysql metadata returned, information_schema

3.5 compatible MysqlWorkbench processing

3.6 Get schema from the db MycatServer configuration example, and the check can not be empty schema

3.7 If the specified sql schema, sql from parsing schema, then use

3.8 performing routeEndExecuteSQL, obtaining routeResultset, from mycatServer route set configuration;

It contains semantic parsing SQL, SQL routing, SQL query optimization, SQL statement is rewritten, the global ID generation

3.9 MycatServer initialization time, will create a new RouteService, calling its route analytic method, MycatServer of routeCache used ehcache

 

1. If sqlType as SELECT, the first splice out cachekey, then get out rrs from sqlRouteCache, if there is value in the cache sqlRouteCache, check the data migration rules, call checkMigrateRule, but we sqlType as SELECT, it will return to direct, but also rrs is a direct route to return a result set

2. Call isHitSql get the length of sql hits, and filter out the space * Two characters, character prefix match mycat sign Hintsql: "/ ** mycat: * /", which is annotated hit mycat sql

3. If you hit Mycat annotated sql, sql to do the interception route analysis, first assembled into a hintMap, then intercepted a realSQL, call hintHandler.route () to route

In essence, it is called AbstractRouteStrategy method of route

4. The method starts route, if there are attributes checkSQLschema schema, the schema for showing characters removed.

5. Some previous routing logic processing, global sequence number is inserted, is inserted into the table Sons

6. Start SQL statements to intercept calls interceptSQL processing DefaultSQLInterceptor, if it is fdbParser, is called processEscape; If you open the global table consistency checking, SQL statement into the Insert, Update and DDL, but our query will return directly .

7. If the determination is Debug log level, and the loadData, rrs not the cache, the cache if so, will greatly reduce performance.

8. If DDL statements type, divided into several categories process create table, create index, drop table, drop index, alter table, truncate table

9. Check watched sheet, without fragmentation, and not show statements, the implementation of a single routing node, the first node as a direct access route, RouteResultsetNode [0]

10. If there is a plurality of slices, call routeNormalSqlWithAST, to find the best route by analyzing syntax tree AST

11. First, determine whether it is only mysql, and if so, use MysqlStatementParser, if not, the MycatStatementParser

12. calls parser.parseStatement () to resolve, the call of the underlying druid-1.0.26 SQLStatementParser.parseStatementList, depending on the type of token

Perform different parse operation, probably divided SELECT, UPDATE, CREATE, INSERT, DELETE, EXPLAIN, ALTER, DROP and other operations.

13. Upon completion of parsing the return statement, then check the type of sentence, if the sentence is replace, direct throw an exception, does not support

14. Create druidParser according to schema, statement and visitor

If the statement is determined 14.1 multiple data sources, call getDruidParserForMultiDB, create a parser corresponding to multiple data sources; statement parsed from the type table, where the table judgment of db: resolve directly to acquire a table corresponding to sql statement - string interception; and summary db type that may arise, including the oracle, db2, mysql, sqlServer, postgresql

If not, 14.2 multiple data sources, according to the type of sql statement, and returns Parser, comprising, DruidSelectParser, DruidInsertParser, DruidDeleteParser, DruidCreateTableParser, DruidUpdateParser, DruidAlterTableParser, DruidLockTableParser

15. Create a good start calling parse druidParser

Set to the original 15.1 sql sql

15.2 hit by analyzing sql, only a corresponding DruidLockTableParser fulfilling - Multi-scene process in a distributed table lock cumbersome, less scenarios, thus intercepts this lock table statement herein. Sql string parsing, and if there is a command "," according to the statement handle multiple table locks, and further validate the syntax of compliance with multi-table lock.

16. By statement parsing, covering all of the parser:

16.1 DruidAlterTableParser: stmt converted into the SQLAlterTableStatement, and then by truncating the acquired tableName string added after returning to the ctx

16.2 DruidCreateTableParser: stmt converted to the MysqlCreateTableStatement, acquiring character string taken tableName; if the schema contains tableName,

Corresponding to the load tableConfig, then acquires from the sub-table tableConfig the algorithm, if slotFunction, sql filled into the ctx

16.3 DruidDeleteParser: the conversion stmt MysqlDeleteStatement, parses the string interception tableName, provided to the ctx

16.4 DruidInsertParser: transforming into MySqlInsertStatement, parses tableName, provided to the CTX;

If the sub-table, regardless of the library, it is available directly from the schema node [0], then fill properties, and finally into RouteResultSet, route setting is completed

If the sub-table is a sub-library, it is necessary to obtain the correspondence tableName tableConfig, determines whether the child table, continue to acquire a child table tableConfig, then obtaining joinKey, and joinKey index, and volume can not insert the syndrome table; The joinKey the index acquisition joinKeyVal, call RouterUtil. routeByERParentKey obtain routing results;

The acquisition route set ER fragmentation rules: determine whether two sub-tables (no parent table has a parent table), configured in accordance with the parent table, the route calculation ruleCalculate call, acquires the config rule of the parent table, and acquires the corresponding routing algorithm ruleAlgorithm , is calculated call algorithm.calculate nodeIndex, then calculate nodeRange, a certain kind of a method according to the part table, for example PartitionByDate, finally obtaining a list of the node added to routeNodeSet, return result

If not, the child table, it acquires from the configuration table partitionColumn, i.e. slice row; if bulk inserted, obtaining slice row in the column index of bulk inserts; from the batch insert statements acquired insert list of values, and from points obtaining routing algorithm routes sheet, and then the cycle is inserted a list of values, in accordance with the record index fragment column to obtain the value of the slice recorded. Then calculate the corresponding nodeIndex, and added to nodeValuesMap, the last cycle was added to the result set route RouteResultSetNode;

If a single insert: The same method as above, but also need to calculate the corresponding routing Node, and then returns a corresponding result set route.

16.5 DruidSelectParser: transforming into SQLSelectStatement, then turn into MysqlSelectQueryBlock, parseOrderAggGroupMysql call resolution, essentially calling parseAggGroupCommon

SelectList is acquired, the cycle, if a calculated total expr, i.e. sum and COUNT, setting various attributes added to RouteResultSet; supra process for groupBy

If the judgment is forUpdate or lock mode, rrs set to run in a read only db

16.6 DruidUpdateParser: restriction fragment update can only support a table, over throwing an exception; stmt of the conversion into the reference MysqlUpdateStatement, and then by truncating the string tableName acquired, then according to the schema obtained from tableName TableConfig

1. If the entire library or sub-schema is not, regardless of the table, is acquired from the schema dataNode, and then returned into RouteResultSet.

2. If the part table, the part table to obtain column partitionColumn; if the global table, then return the entire route set rrs DataNodes; if not, it returns the corresponding DataNodes route set as a result.

+++++++++++++++++++++++++++++++++++++++++++++++

17. changeSql: rewriting sql, involves only DruidSelectParser

17.1 calls tryRoute (), to determine if the route is over, return directly;

17.2 If there is no table, select statement routed directly to any node

17.3 ctx cycle in each route calculation unit then calculates the route rrsTmp, and the cycle all the node is added to rrsTmp nodeSet. If the last nodeSet is empty, direct throw an exception. FinishRoute last set to true, returns the result

17.4 mysql sentence is determined, according to the statement, the initialization mysqlSelectQuery, increase limit determined if necessary, is set in the mysqlSelectQuery

17.5 acquiring offset and count objects from the above limit, it is determined if the number of nodes rrs is greater than 1, it is necessary changeLimit, provided rowCount = limitStart + limitSize

 

Published 386 original articles · won praise 2 · Views 9844

Guess you like

Origin blog.csdn.net/kuaipao19950507/article/details/105014935
Recommended