[MySQL5.6] GTID implementation, O & M changes, and existing bugs

Source: Internet
Author: User
[MySQL5.6] GTID implementation, O & M changes, and existing bugs the main purpose of this article is to write down the backtrace related to gtid for future troubleshooting. In addition, we will discuss the bugs that currently exist in MySQL 5.6.11.

What is GTID?

What is GTID? in short, it is the global transaction ID (global transaction identifier), which was initially implemented by google and was added to the official MySQL 5.6, the reason for this article is that 5.6 introduced a lot of gtid-related variables, which are deeply confused.

Last year, I also wrote a short blog, roughly introduced what is gtid, http://mysqllover.com /? P = 87. This article does not describe too much text, because there are already a large number of similar articles on the Internet.

The GTID format is similar:

7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 1

This is the gtid record generated on one of my servers. the Event types in binlog are as follows:

GTID_LOG_EVENT: indicates the GTID of the subsequent transaction.

There are two types of GTID events:

ANONYMOUS_GTID_LOG_EVENT: type of the anonymous GTID event (for the time being, no matter)

Previus_gtids_log_event: indicates the GTID set that has been executed before the current binlog file. it is recorded in the Binlog file header, for example:

# At 120 #130502 23:23:27 server id 119821 end_log_pos 231 CRC32 0x4f33bb48 Previous-GTIDs #10a27632-a909-11e2-8bc7-0010184e9e08: 1, # Region: 1-1129

This string, separated by ":", represents the server_uuid of the server. this is a 128-bit random string generated at the first startup (function generate_server_uuid ), the corresponding variables is the read-only variable server_uuid. It can ensure global uniqueness with a very high probability and coexist in the file DATA/auto. cnf. Therefore, do not delete or modify this file, otherwise it will be troublesome.

The second part is an incremental transaction ID number. The transaction ID number + server_uuid uniquely identifies a transaction.

In addition to separate GTID, there is also a concept of gtid set. The expression of a gtid set is similar:

7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 1-31

GTID_EXECUTED and GTID_PURGED are typical gtid set variables. in A Replication Topology, GTID_EXECUTED may contain several groups of data, for example:

Mysql> show global variables like '% gtid_executed %'/G

* *************************** 1. row ***************************

Variable_name: gtid_executed

Value: 10a27632-a909-11e2-8bc7-0010184e9e08: 1-4,

153c0406-a909-11e2-8bc7-0010184e9e08: 1-3,

7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 1-31,

F914fb74-a908-11e2-8bc6-0010184e9e08: 1

The content discussed in this article includes:

1. gtid generation and record on the master database

II. how to use GTID for replication in the standby database

III. Changes in active/standby O & M

4. bugs in MySQL5.6.11

1. Gtida. related variables on the master database

The Gtid of each transaction on the master database includes the changed part and the unchanged part. Before the discussion, we need to figure out the four variables maintained by GTID:

GTID_PURGED : The transaction of the deleted binlog, which is a subset of GTID_EXECUTED and cannot be set from MySQL5.6.9.

GTID_OWNED : Indicates the gtid of the transaction being executed and the corresponding thread ID.

For example:

Mysql> show global variables like '% gtid_owned %'/G

* *************************** 1. row ***************************

Variable_name: gtid_owned

Value: 7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 11560057 #67: 11560038 #89: 11560059 #7: 11560034 #32: 11560053 #56: 11560052 #112: 11560055 #128: 11560054 #65: 11559997 #96: 11560056 #90: 11560051 #85: 11560058 #39: 11560061 #12: 11560060 #125: 11560035 #62: 11560062 #5

1 row in set (0.01 sec)

GTID_EXECUTED Indicates the transaction that has been executed on the instance; executing reset master will leave the variable blank; we can also set GTID_NEXT to execute an empty transaction to affect GTID_EXECUTED

GTID_NEXT Is a SESSION-level variable, indicating the next GTID to be used

The gtid_state global object corresponding to GTID_PURGED, GTID_OWNED, and GTID_EXECUTED is also maintained in the memory.

Three sets are maintained in gtid_state. logged_gtids corresponds to GTID_EXECUTED, lost_gtids corresponds to GTID_PURGED, and owned_gtids corresponds to GTID_OWNED.

B. how to allocate and use GTID

Gtid involves the following parts during the execution of a transaction in the master database:

The transaction starts. when the first SQL statement is executed Before writing the first "BEGIN" query event, allocate a group (Group_cache: add_logged_group) to the Group_cache of binlog cache and write a Gtid_log_event, at this time, no transaction id is assigned to it. backtrace is as follows:

Handler: ha_write_row-> binlog_log_row-> write_locked_table_maps-> THD: handler-> binlog_cache_data: write_event-> Group_cache: add_logged_group

It is unclear when a transaction will have multiple group_cache gtid.

In the flush phase of binlog group commit :

Step 1: Call Group_cache: generate_automatic_gno to generate a gtid for the current thread, assign it to thd-> owned_gtid, and add it to owned_gtids. the backtrace is as follows:

MYSQL_BIN_LOG: MySQL-> MYSQL_BIN_LOG: flush_thread_caches-> tables: flush-> binlog_cache_data: flush-> tables-> Group_cache: tables-> Gtid_state :: acquire_ownership-> Owned_gtids: add_gtid_owner

That is to say, gtid will not be allocated to the binlog cache until the transaction is completed and the binlog is to be flushed to the binlog cache.

When the type of gtid_next is AUTOMATIC, call generate_automatic_gno to generate the transaction id (gno). the allocation process is as follows:

1. gtid_state-> lock_sidno (automatic_gtid.sidno), locks the current sidno, and the allocation process is mutually exclusive.

2. gtid_state-> get_automatic_gno (automatic_gtid.sidno); get the transaction ID

|-> The initialization candidate (candidate) gno is 1

|-> Scan logged_gtids [$ sidno] to obtain each gno interval (iv ):

|-> When candidate <iv-> start (or MAX_GNO, if iv is NULL), determine whether candidate is in use. if not, use this candidate, return from the function. otherwise, candidate ++ continues this step.

|-> Set candidate to iv-> end and iv to the next interval. continue step 2.

From this process, we can see that there are shards in the interval, and the possible gno allocation is not the largest gno in the world. however, if the master database does not manually set gtid_next, we can think that gno on the master database is always increasing progressively.

3. gtid_state-> acquire_ownership (thd, automatic_gtid );

|-> Add to the owned_gtids collection (owned_gtids.add_gtid_owner) and assign the value to thd-> owned_gtid = gtid

4. gtid_state-> unlock_sidno (automatic_gtid.sidno); unlock

Step 2: call Gtid_state: update_on_flush to add the grid of the current transaction to logged_gtids. the backtrace is as follows:

MYSQL_BIN_LOG: Prepare-> MYSQL_BIN_LOG: flush_thread_caches-> tables: flush-> MYSQL_BIN_LOG: write_cache-> Gtid_state: update_on_flush

In the commit phase of the bin log group commit

Call Gtid_state: update_owned_gtids_impl to remove the gtid of the current transaction from owned_gtids. the backtrace is as follows:

MYSQL_BIN_LOG: ordered_commit-> MYSQL_BIN_LOG: finish_commit-> Gtid_state: update_owned_gtids_impl

The preceding steps involve modifications to logged_gtids and owned_gtids. In addition to maintenance at startup, lost_gtids is maintained during the Purge operation.

For example, when we execute purge binary logs to 'MySQL-bin.000205 ', the mysql-bin.index is first updated, and then the previus_gtids_log_event event of the first binlog file is located based on the index file to update the lost_gtids collection, backtrace:

Purge_master_logs-> MYSQL_BIN_LOG: purge_logs-> MYSQL_BIN_LOG: Queue-> queue: add_to_set-> Gtid_set: Queue-> Gtid_set: add_gno_interval

For more information about binlog group commit, see the previous blog: http://mysqllover.com /? P = 581

C. how to persist GTID

After restarting MySQL, we can see that GTID_EXECUTED and GTID_PURGED are consistent with those before the restart.

Persistent GTID is managed by the global object gtid_state. Gtid_state calls the gtid_server_init function to allocate memory when the system starts. if binlog is enabled, further initialization will be performed:

Quoted code:

5419 if (mysql_bin_log.init_gtid_sets (

5420 const_cast (Gtid_state-> get_logged_gtids ()),

5421 const_cast (Gtid_state-> get_lost_gtids ()),

5422 opt_master_verify_checksum,

5423 true/* true = need lock */))

5424 unireg_abort (1 );

Gtid_state contains three gtid sets: logged_gtids, lost_gtids, and owned_gtids. The first two are gtid_set and the owned_gtids type is Owned_gtids.

MYSQL_BIN_LOG: init_gtid_sets is mainly used to initialize logged_gtids and lost_gtids. the logic of this function is described below:

1. scan the mysql-index file, collect binlog file names, and add them to filename_list.

2. read from the last file and call the read_gtids_from_binlog function in sequence:

|-> Open the binlog file. if the previus_gtids_log_event event is read

(1) In any case, add it to logged_gtids (prev_gtids_ev-> add_to_set (all_gtids ))

(2) If the file is the first binlog file, add it to lost_gtids (prev_gtids_ev-> add_to_set (prev_gtids.

|-> Get the GTID_LOG_EVENT event

(1) read the corresponding sidno, sidno = gtid_ev-> get_sidno (false );

This is a 32-bit integer. sidno is used to represent a server_uuid. it is calculated from 1, which is mainly for memory saving considerations. Maintained in the global object global_sid_map.

When sidno is not added to map, global_sid_map-> add_sid (sid) is called. sidno increments from 1.

(2) all_gtids-> ensure_sidno (sidno)

All_gtids is of the gtid_set type and can be understood as a set. ensure_sidno ensures that the set can contain at least sidno elements.

(3) all_gtids-> _ add_gtid (sidno, gtid_ev-> get_gno ()

Add the gtid recorded in this event to all_gtids [sidno] (the Gtid_set: add_gno_interval is finally called. here we actually add the range (gno, gno + 1) to it.

Operations such as interval merging and intersection)

When neither PREVIOUS_GTIDS_LOG_EVENT nor GTID_LOG_EVENT exists in the first file, continue to read the previous file.

If only the previus_gtids_log_event event exists, the function read_gtids_from_binlog returns got_previus_gtids

If the GTID_LOG_EVENT event still exists, GOT_GTIDS is returned.

There is obviously a problem here, that is, if we did not use gtid_mode before the restart, and a large number of binlogs were generated, after this restart, we may need to scan a large number of binlog files. This is a very obvious Bug, which will be discussed later.

3. if the second scan does not reach the first file, the scan starts from the first file. The process is similar to step 1. read the first previus_gtids_log_event event and add it to lost_gtids.

To put it simply, if we keep opening gtid_mode, we only need to read the first binlog file and the last binlog file to determine the two GTID sets: logged_gtids and lost_gtids.

II. GTIDa on the Slave Database. how to keep the master and slave GTID consistent

Since the GTID of each transaction is recorded in the binlog, the replication thread of the slave database can ensure the consistency between the GTID of the master database and the slave database by setting the thread level GTID_NEXT.

By default, thd-> variables. gtid_next.type on the master database is set to AUTOMATIC_GROUP, while the standby database is set to GTID_GROUP.

The standby database SQL thread gtid_next outputs:

(Gdb) p thd-> variables. gtid_next $2 = {type = GTID_GROUP, gtid = {sidno = 2, gno = 1127, static MAX_TEXT_LENGTH = 56}, static MAX_TEXT_LENGTH = 56}

These variables are assigned a value when Gtid_log_event is executed: Gtid_log_event: do_apply_event. the general process is as follows:

1. rpl_sidno sidno = get_sidno (true); get sidno

2. thd-> variables. gtid_next.set (sidno, spec. gtid. gno); set gtid_next

3. gtid_acquire_ownership_single (thd );

|-> Check whether the gtid is in the logged_gtids collection. if it is in the collection, return (gtid_pre_statement_checks ignores this transaction)

|-> If the gtid has been owned by other threads, wait (gtid_state-> wait_for_gtid (thd, gtid_next )), otherwise, set the current thread to owner (gtid_state-> acquire_ownership (thd, gtid_next ))

As mentioned above, it is possible that the GTID of the current transaction is already in logged_gtids. Therefore, gtid_pre_statement_checks will be called when the Rows_log_event: do_apply_event or mysql_execute_command function

This function also checks whether gtid is valid before each SQL statement is executed. The main process includes:

1. when enforce_gtid_consistency is enabled, check whether DDL execution is allowed (thd-> is_ddl_gtid_compatible (). If not, GTID_STATEMENT_CANCEL is returned.

2. check whether the current SQL statement will generate implicit commit and gtid_next is set (gtid_next-> type! = AUTOMATIC_GROUP). If yes, the error ER_CANT_DO_IMPLICIT_COMMIT_IN_TRX_WHEN_GTID_NEXT_IS_SET will be thrown and GTID_STATEMENT_CANCEL will be returned.

3. for BEGIN/COMMIT/ROLLBACK/(set option or SELECT) SQL statements that do not use stored procedures/these types, execution is always allowed and GTID_STATEMENT_EXECUTE is returned.

4. gtid_next-> type is UNDEFINED_GROUP, and an error ER_GTID_NEXT_TYPE_UNDEFINED_GROUP is thrown. GTID_STATEMENT_CANCEL is returned.

5. gtid_next-> type = GTID_GROUP and thd-> owned_gtid.sidno = 0, GTID_STATEMENT_SKIP is returned.

In step 5, special cases of the gtid_acquire_ownership_single function are processed.

B. How does the slave database initiate a DUMP request?

The biggest benefit of introducing GTID is that we can switch the master-slave topology as needed. In a normal replication structure, we can simply execute the following SQL statement in the slave database:

Change master to MASTER_USER = '$ username', MASTER_HOST = '', MASTER_PORT ='', MASTER_AUTO_POSITION = 1;

After GTID is opened, we do not need to specify the binlog file or location. MySQL will automatically perform these tasks for us. The key here is MASTER_AUTO_POSITION. IO threads can be connected to the master database in the following steps:

1. after establishing a TCP connection with the master database, the IO thread will obtain the uuid (get_master_uuid) of the master database, and then set a user variable @ slave_uuid (io_thread_init_commands) on the master database)

2. then, register SLAVE (register_slave_on_master) on the master database)

Call register_slave on the master database to register the Slave Database. record the host, user, password, port, server_id, and other information of the slave database to the slave_list hash.

3. call request_dump to request data from the master database. There are two scenarios:

When MASTER_AUTO_POSITION = 0, the command type sent to the master database is COM_BINLOG_DUMP, which is the traditional BINLOG request mode.

When MASTER_AUTO_POSITION = 1, the command type is COM_BINLOG_DUMP_GTID, which is the new method.

Here we will only discuss the second type. In the second case, the gtid set executed by the slave database is read first.

Quoted code in rpl_slave.cc:

2974 if (command = COM_BINLOG_DUMP_GTID)

2975 {

2976 // get set of GTIDs

2977 Sid_map sid_map (NULL/* no lock needed */);

2978 Gtid_set gtid_executed (& sid_map );

2979 global_sid_lock-> wrlock ();

2980 gtid_state-> dbug_print ();

2981 if (gtid_executed.add_gtid_set (mi-> rli-> get_gtid_set ())! = RETURN_STATUS_ OK |

2982 gtid_executed.add_gtid_set (gtid_state-> get_logged_gtids ())! =

2983 RETURN_STATUS_ OK)

After the package is built, it is sent to the master database.

After receiving the command on the master database, call the entry function com_binlog_dump_gtid. The process is as follows:

1. slave_gtid_executed.add_gtid_encoding (packet_position, data_size); read the gtid set sent from the slave database

2. read the uuid (get_slave_uuid) of the slave database and kill the zombie thread (kill_zombie_dump_threads) based on the uuid)

This is also the use of the slave io thread to execute SET @ SLAVE_UUID.

3. enter the mysql_binlog_send function:

|-> Call MYSQL_BIN_LOG: unknown to start scanning from the last Binlog and obtain the PREVIOUS_GTIDS_LOG_EVENT in the file header. if it is a subset of slave_gtid_executed, save the current binlog file name. Otherwise, continue scanning.

This step aims to find the last Binlog file executed by the slave database.

|-> Scanning starts from the header of this file. when GTID_EVENT is encountered, it determines whether the GTID is included in slave_gtid_executed:

Gtid_log_event gtid_ev (packet-> ptr () + ev_offset,

Packet-> length ()-checksum_size,

P_fdle );

Skip_group = slave_gtid_executed-> contains_gtid (gtid_ev.get_sidno (sid_map ),

Gtid_ev.get_gno ());

The master database uses GTID to determine whether transactions can be ignored and the starting position of execution.

Note: After MASTER_LOG_POSITION is used, do not specify the binlog location. Otherwise, an error is returned.

III. O & M operations a. how to ignore replication errors

When a replication error occurs in the standby database, the traditional method of skipping the error is to set SQL _slave_skip_counter and then START SLAVE.

However, if GTID is enabled, the setting fails:

Mysql> set global SQL _slave_skip_counter = 1;

ERROR 1858 (HY000): SQL _slave_skip_counter can not be set when the server is running with @ GLOBAL. GTID_MODE = ON. instead, for each transaction that you want to skip, generate an empty transaction with the same GTID as the transaction

The error message indicates that an empty transaction can be generated to skip the wrong transaction.

We manually generate a slave database copy error:

Last_ SQL _Error: Error 'unknown table 'test. t1 "on query. Default database: 'test'. Query: 'Drop TABLE 'T1'/* generated by server */'

Check the binlog. the GTID corresponding to this DDL is 7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 1131.

Run the following command on the slave database:

Mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

Mysql> set session GTID_NEXT = '7a07cd08-ac1b-11e2-9fcf-0010184e9e08: 8080 ′;

Query OK, 0 rows affected (0.00 sec)

Mysql> BEGIN; COMMIT;

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Mysql> set session GTID_NEXT = AUTOMATIC;

Query OK, 0 rows affected (0.00 sec)

Mysql> start slave;

Check the show slave status and you will find that the error transaction has been skipped. The principle of this method is very simple. the GTID generated by a null transaction is added to GTID_EXECUTED, which is equivalent to telling the slave database that the transaction corresponding to this GTID has been executed.

B. refer to the master database again

Use change master .... , MASTER_AUTO_POSITION = 1;

Note that gtid_mode must be enabled throughout the replication topology.

C. New until conditions

5.6 a new util condition is provided to determine the location where the standby database replication is executed based on GTID.

SQL _BEFORE_GTIDS: stops copying before the specified GTID.

SQL _AFTER_GTIDS: stops copying after the specified GTID.

The judgment function is Relay_log_info: is_until_satisfied.

For more information, see: http://dev.mysql.com/doc/refman/5.6/en/start-slave.html

D. reduce the size of the binlog file.

If GTID is enabled, theoretically it is best to reduce the maximum value of each binlog file to reduce the file scan time.

4. existing bugs

Bug #69097. , Even if gtid_mode is disabled, the binlog file will be scanned at startup.

If gtid_mode is not used before the restart, all binlog files may be scanned after the restart. if there are many Binlog files, this is obviously unacceptable.

Bug #69096. You cannot skip the copy error through GTID_NEXT_LIST because GTID_NEXT_LIST is not compiled by default.

TODO: The Logic of GTID_NEXT_LIST is not mentioned above. if you are free, check again.

Bug #69095. To set the replication mode of the standby database to STATEMENT/MIXED. The master database is set to row mode. executing DML will interrupt the replication of the slave database.

Last_ SQL _Error: Error executing row event: 'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT .'

Backtrace used to determine the error:

Handle_slave_worker-> slave_worker_exec_job-> Rows_log_event: do_apply_event-> open_and_lock_tables-> lock_tables-> THD: unknown

Solution: Set the replication mode of the slave database to 'row' to ensure consistency between the master and slave databases.

This bug is irrelevant to GTID.

Bug #69045. When the master database performs actions such as flush privileges, if gtid_mode is enabled for both the master and slave databases, the replication is interrupted.

Last_ SQL _Error: Error 'cannot execute statements with implicit commit inside a transaction when @ SESSION. GTID_NEXT! = AUTOMATIC or @ SESSION. GTID_NEXT_LIST! = NULL. 'On query. Default database: ". Query: 'Flush privileges'

It is also a very low-level bug. in MySQL5.6.11, gtid_next must be equivalent to AUTOMATIC if it may cause implicitly committed transactions. it is easy to interrupt the standby database replication thread, the judgment logic is in the gtid_pre_statement_checks function.

References

1. three blogs of Ali Changyuan (1, 2, 3)

2. MySQL5.6.11 source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.