Follow insert a statement into the TiDB source in the world (on)

TiDB is the open source implementation of google F1;

TiDB implemented based on optimistic locking mvcc, on-line table structure changes, based on the data consistency linear time stamp, and so on;

For reliability, TiDB and Oracle, as millions maintenance level of automation test cases, running over k8s build clusters;

TiDB which used the algorithm will write the test program verification, in order to prove the correctness of the algorithm;

Since google F1 start, the database is divided into two categories:

  One is the traditional sql, representatives such as Oracle, rely on high-speed network and disk arrays massive data expansion and data of high reliability, and high cost, most banks have used Oracle system;

  The other is NewSQL, such as F1 representatives and TiDB, relies on algorithms, databases so that services can run on a server cluster built from cheap, also ensures high reliability and massive data expansion and data;

Before looking at TiDB source, and then briefly review TiDB structure:

 

This article deals with the content of the above-mentioned orange part --- TiDB-Server code;

TiDB-Server is implemented go, go of code organization very friendly, by contrast, C ++ and Java scripts and compiled code relies on a variety of warehouse management is very complex and error-prone;

After compiling generation of TiDB-Server executable file is tidb-server, you can run alone, without tipd and tikv can start, in fact, the code inside of tipd and tikv achieved mock; you can be understood as a tidb-server mysql- server;

Because of space limitations, here only about the general process; let's have a simple understanding of the realization of the database;

Suppose we mysql client executed an insert statement --- "insert into t1 values ​​(" zhangshan ", 5000000);", is how this statement is executed TiDB-Server it? 

First look at the code mysql client access part:

 

After connecting up, mysql client reads data, parses according mysql client server protocol;

 

 Note: When we have the following code inside variables example, are used in this sql --- "insert into t1 values ​​(" zhangshan ", 5000000);"

 We look at the implementation of cc.dispatch

 

 

sql type of query processing inside the clientConn.handleQuery:

 

 After a series of calls came session.execute, we look at the implementation:

 

 session.execute which do a few things:

  1. The syntax and lexical analysis sql parsed into abstract syntax tree, thereby generating a InsertStmt structure;

    Parsing and lexical analysis using a go-language version of yacc and lex;

    tidb used a tool ebnf2y (EBNF grammar turn .y file) to the mysql sql BNF grammar into a parser.y, you can see github.com \ pingcap \ parser \ parser.y;

    It is worth mentioning that, pingcap the sql parser from tidb independent, it can easily use for other projects;

  2. Optimize sql, execution plan;

    

  3. Execute sql;

    For insert statements in the insert data into kv;

    tidb-server in mock kv direct leveldb the key to save the go-language version of;

 We look at the structure of the corresponding Insert statement:

Due to space limitations, temporarily stop here, next we went into session.executeStatement function;

Tidb see how the data is encoded into a column kv, is inserted into the go leveldb;

 

Guess you like

Origin www.cnblogs.com/lijingshanxi/p/10958729.html