[Mycat 1.6 routing and distribution process]

1. Mycat's routing and distribution process

In principle, mycat can be regarded as a sql transponder. Mycat receives the sql sent by the front end, and then forwards it to the mysql server in the background for execution. But there are many mysql nodes (such as dn1, dn2, dn3) behind, which nodes should be forwarded to? That's what route resolution is supposed to do. Routing ensures that sql is forwarded to the correct node. The range of forwarding is just right, not too much and not too much. There are two problems with multiple occurrences: wasting performance and not finding the table. For example, in a statement select * from orders where pro='wuhan', only the dn1 node can find the data. If the statement is forwarded to the three nodes dn1, dn2, and dn3 at the same time, such a range will be sent more frequently, and the performance will be the same. kind of waste. If a new node dn4 is added, but the datanode range of orders is only dn1, dn2, dn3, if it is forwarded to dn1, dn2, dn3, and dn4 at the same time, it will return table orders not exists when sent to dn4 for execution. If it is sent less, the result set will be incomplete. For example, if select * from orders is only forwarded to dn1, only the result set on dn1 will be returned, and the result set on dn2 and dn3 will not be available.

 

Second, the parser fdb parser VS druidparser 

Route parsing uses the strategy pattern, and each parser implements a routing strategy. You can also continue to expand, such as Druid parsing and subdividing Mysql,

Implementation strategies such as postgresql, oracle, etc.

The parser refers to the sql parser. The parser used before mycat 1.3 was fdb parser (FoundationDB SQL Parser). From 1.3

The druid parser was introduced, and the fdbparser was removed from 1.4, and only the druidparser method was retained.

 

Two ways of druid route resolution

Druid parsing has two ways: vistor way and statement way.

 

Three, the core elements of routing calculation

1. Table name contained in sql

2. Conditions (Conditons) contained in sql, each Condition is a 3-tuple of <table name, field name, field value>.

3. The schema corresponding to the table.

4. Is the table fragmented? If fragmented, what is the fragmentation field? What is the fragmentation algorithm? The information in point 4 can be calculated according to point 3.

With some of the above data, the route can be calculated, so the route calculation needs to solve the following problems:

Extract the table name and condition (field, table to which the field belongs, field value) from the sql statement. With the table name and conditions, and then according to the sharding rules of the table

The exact route can be calculated.

 

Fourth, the routing calculation process

1) Single table routing calculation process



 

 

2) Multi-table routing calculation process

 


3) Route calculation of global table

Global table insert, update statements: routed to all nodes.

Global table select statement: route to any node.

 

 

4) or statement problem solution idea - equivalent replacement

The basic idea of ​​routing to solve an or statement is equivalent substitution.

1. Use the union statement to split the equivalent replacement of the or statement

This equivalent replacement should be known to everyone

Select * from travelrecord where id = 1 or id = 5000001 is equivalent to the following statement:

Select * from travelrecord where id = 1 unioin Select * from travelrecord where id = 5000001

2. The union of the result set of the Union statement is equivalent to the union of the routes

There is no clear theoretical basis for this equivalence, but we can prove by contradiction:

If the route sets are different, the result sets must be different, so the result sets are the same, and the route sets must be the same.

Select * from travelrecord where id = 1 or id = 5000001 Route collection

Route collection equivalent to Select * from travelrecord where id = 1 and Select * from travelrecord where id =

The union of 5000001's set of routes.

Eventually evolved into pairs Select * from travelrecord where id = 1 and Select * from travelrecord where id =

5000001 The two statements are routed separately, and then the union is taken.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326954745&siteId=291194637