The reason why MySQL database binlog records time stamps are out of order

Source: Internet
Author: User

MySQL databaseDuring the operationBinlogDuring parsing, we found that the dump data had serious out-of-order situations. The time difference was almost 1 s, as shown below:

 
 
  1. SET TIMESTAMPE = 1 
  2.  
  3. SET TIMESTAMPE = 0 
  4.  
  5. SET TIMESTAMPE = 1 
  6.  
  7. …… 

When writing the binlog header:

 
 
  1. log_event.cc  
  2.  
  3. Log_event::write_header  
  4.  
  5. now= (ulong) get_time();     //query start time  
  6.  
  7. int4store(header, now);              // timestamp  
  8.  
  9. log_event.h  
  10.  
  11. inline time_t get_time()  
  12.  
  13. {  
  14.  
  15. THD *tmp_thd;  
  16.  
  17. if (when)  
  18.  
  19. return when;  
  20.  
  21. if (thd)  
  22.  
  23. return thd->start_time;  
  24.  
  25. if ((tmp_thd= current_thd))  
  26.  
  27. return tmp_thd->start_time;  
  28.  
  29. return my_time(0);  
  30.  

/// It is found that if when is always positive.

So follow up to the upper-layer binlog_query:

 
 
  1. case THD::STMT_QUERY_TYPE:  
  2.  
  3. /*  
  4.  
  5. The MYSQL_LOG::write() function will set the STMT_END_F flag and  
  6.  
  7. flush the pending rows event if necessary.  
  8.  
  9. */  
  10.  
  11. {  
  12.  
  13. Query_log_event qinfo(this, query_arg, query_len, is_trans, suppress_use,  
  14.  
  15.                       errcode); 

Here we will construct a binlog struct. After the build, its qinfo-> when has been assigned a value.

Continue to follow up. In the query_log_event constructor, The log_event constructor is called first.

 
 
  1. Log_event::Log_event(THD* thd_arg, uint16 flags_arg, bool using_trans)  
  2.  
  3. :log_pos(0), temp_buf(0), exec_time(0), flags(flags_arg), thd(thd_arg)  
  4.  
  5. {  
  6.  
  7. server_id= thd->server_id;  
  8.  
  9. when= thd->start_time;  
  10.  
  11. cache_stmt= using_trans;  
  12.  

Next let's take a look at which function will set the start_time value of thd. We will follow the steps in the dispatch_command function to print thd-> start_time:

 
 
  1. (gdb) p thd->start_time  
  2.  
  3. $52 = 1312428114 

It is found that this value is consistent with the start_time of thd in the previous command, indicating that the thread is used repeatedly and this field has not been initialized.

Continue execution: n

After thd-> set_time () is executed, it is found that the value of start_time has changed:

 
 
  1. (gdb) p thd->start_time  
  2.  
  3. $55 = 1312428349 

Set the breakpoint in Log_event: write_header and observe the value of when.

 
 
  1. Breakpoint 3, Log_event::write_header (this=0x4923d2f0, file=0xcaf1b0, event_data_length=8) at log_event.cc:890  
  2.  
  3. 890     bool Log_event::write_header(IO_CACHE* file, ulong event_data_length)  
  4.  
  5. (gdb) p when  
  6.  
  7. $58 = 1312428349 

The values are consistent. Use mysqlbinlog to view the values:

 
 
  1. # at 2586  
  2.  
  3. #110804 11:25:49 server id 1  end_log_pos 2700  Query   thread_id=1     exec_time=70    error_code=0 
  4.  
  5. SET TIMESTAMP=1312428349/*!*/;  
  6.  
  7. insert into test values(NULL,'asdd','ssssdsdsss')  
  8.  
  9. /*!*/;  
  10.  
  11. # at 2700  
  12.  
  13. #110804 11:25:49 server id 1  end_log_pos 2727  Xid = 14 
  14.  
  15. COMMIT/*!*/;  
  16.  
  17. DELIMITER ;  
  18.  
  19. # End of log file  
  20.  
  21. ROLLBACK /* added by mysqlbinlog */;  
  22.  
  23. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 

In summary, we can think that the timestamp recorded in the binlog is determined after the command is received and has not been executed, the value is recorded in the start_time field of thd. This makes it easy to understandTime stamp out of orderIn a high-concurrency mysql, the SQL statement may be submitted before execution, resulting in a time stamp out of order in the binlog.

This article introduces the unsorted timestamp recorded in the binlog of MySQL database. We hope this article will help you gain some benefits!

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.