MySQL并行復(fù)制的深入淺出
1.DBA都應(yīng)該知道,MySQL的復(fù)制是基于binlog的。 2.MySQL復(fù)制包括兩部分,IO線程 和 SQL線程。 3.IO線程主要是用于拉取接收Master傳遞過(guò)來(lái)的binlog,并將其寫入到relay log 4.SQL線程主要負(fù)責(zé)解析relay log,并應(yīng)用到slave中 5.不管怎么說(shuō),IO和SQL線程都是單線程的,然后master卻是多線程的,所以難免會(huì)有延遲,為了解決這個(gè)問題,多線程應(yīng)運(yùn)而生了。 6.IO多線程? 6.1 IO沒必要多線程,因?yàn)镮O線程并不是瓶頸啊7.SQL多線程?7.1 沒錯(cuò),目前最新的5.6,5.7,8.0 都是在SQL線程上實(shí)現(xiàn)了多線程,來(lái)提升slave的并發(fā)度接下來(lái),我們就來(lái)一窺MySQL在并行復(fù)制上的努力和成果吧 二、重點(diǎn)
是否能夠并行,關(guān)鍵在于多事務(wù)之間是否有鎖沖突,這是關(guān)鍵。 下面的并行復(fù)制原理就是在看如何讓避免鎖沖突
三、MySQL5.6 基于schema的并行復(fù)制slave-parallel-type=DATABASE(不同庫(kù)的事務(wù),沒有鎖沖突)
之前說(shuō)過(guò),并行復(fù)制的目的就是要讓slave盡可能的多線程跑起來(lái),當(dāng)然基于庫(kù)級(jí)別的多線程也是一種方式(不同庫(kù)的事務(wù),沒有鎖沖突)
先說(shuō)說(shuō)優(yōu)點(diǎn): 實(shí)現(xiàn)相對(duì)來(lái)說(shuō)簡(jiǎn)單,對(duì)用戶來(lái)說(shuō)使用起來(lái)也簡(jiǎn)單
再說(shuō)說(shuō)缺點(diǎn): 由于是基于庫(kù)的,那么并行的粒度非常粗,現(xiàn)在很多公司的架構(gòu)是一庫(kù)一實(shí)例,針對(duì)這樣的架構(gòu),5.6的并行復(fù)制無(wú)能為力。當(dāng)然還有就是主從事務(wù)的先后順序,對(duì)于5.6也是個(gè)大問題
話不多說(shuō),來(lái)張圖好了
slave-parallel-type=LOGICAL_CLOCK : Commit-Parent-Based模式(同一組的事務(wù)[last-commit相同],沒有鎖沖突. 同一組,肯定沒有沖突,否則沒辦法成為同一組)
slave-parallel-type=LOGICAL_CLOCK : Lock-Based模式(即便不是同一組的事務(wù),只要事務(wù)之間沒有鎖沖突[prepare階段],就可以并發(fā)。 不在同一組,只要N個(gè)事務(wù)prepare階段可以重疊,說(shuō)明沒有鎖沖突)
group commit,之前的文章有詳細(xì)描述,這里不多解釋。MySQL5.7在組提交的時(shí)候,還為每一組的事務(wù)打上了標(biāo)記,現(xiàn)在想想就是為了方便進(jìn)行MTS吧。
我們先看一組binlog
last_committed=0 sequence_number=1last_committed=1 sequence_number=2last_committed=2 sequence_number=3last_committed=3 sequence_number=4last_committed=4 sequence_number=5last_committed=4 sequence_number=6last_committed=4 sequence_number=7last_committed=6 sequence_number=8last_committed=6 sequence_number=9last_committed=9 sequence_number=10 4.1 Commit-Parent-Based模式
基于主鍵的沖突檢測(cè)(binlog_transaction_depandency_tracking = COMMIT_ORDERE|WRITESET|WRITESET_SESSION, 修改的row的主鍵或非空唯一鍵沒有沖突,即可并行)
5.7.22 也支持了 write-set 機(jī)制
事務(wù)依賴關(guān)系:binlog_transaction_depandency_tracking = COMMIT_ORDERE|WRITESET|WRITESET_SESSIONCOMMIT_ORDERE: 繼續(xù)基于組提交方式WRITESET: 基于寫集合決定事務(wù)依賴WRITESET_SESSION: 基于寫集合,但是同一個(gè)session中的事務(wù)不會(huì)有相同的last_committed 事務(wù)檢測(cè)算法:transaction_write_set_extraction = OFF| XXHASH64 | MURMUR32
MySQL會(huì)有一個(gè)變量來(lái)存儲(chǔ)已經(jīng)提交的事務(wù)HASH值,所有已經(jīng)提交的事務(wù)所修改的主鍵(或唯一鍵)的值經(jīng)過(guò)hash后都會(huì)與那個(gè)變量的集合進(jìn)行對(duì)比,來(lái)判斷改行是否與其沖突,并以此來(lái)確定依賴關(guān)系
這里說(shuō)的變量,可以通過(guò)這個(gè)設(shè)置大小: binlog_transaction_dependency_history_size
這樣的粒度,就到了 row級(jí)別了,此時(shí)并行的粒度更加精細(xì),并行的速度會(huì)更快,某些情況下,說(shuō)slave的并行度超越master也不為過(guò)(master是單線程的寫,slave也可以并行回放)
六、如何讓slave的并行復(fù)制和master的事務(wù)執(zhí)行的順序一致呢5.7.19 之后,可以通過(guò)設(shè)置 slave_preserve_commit_order = 1
官方解釋: For multithreaded slaves, enabling this variable ensures that transactions are externalized on the slave in the same order as they appear in the slave’s relay log. Setting this variable has no effect on slaves for which multithreading is not enabled. All replication threads (for all replication channels if you are using multiple replication channels) must be stopped before changing this variable. --log-bin and --log-slave-updates must be enabled on the slave.In addition --slave-parallel-type must be set to LOGICAL_CLOCK.Once a multithreaded slave has been started, transactions can begin to execute in parallel. With slave_preserve_commit_order enabled, the executing thread waits until all previous transactions are committed before committing. While the slave thread is waiting for other workers to commit their transactions it reports its status as Waiting for preceding transaction to commit.大致實(shí)現(xiàn)原理就是:excecution階段可以并行執(zhí)行,binlog flush的時(shí)候,按順序進(jìn)行。 引擎層提交的時(shí)候,根據(jù)binlog_order_commit也是排隊(duì)順序完成 換句話說(shuō),如果設(shè)置了這個(gè)參數(shù),master是怎么并行的,slave就怎么辦并行
來(lái)自:http://keithlan.github.io/2018/07/31/mysql_mts_detail/
相關(guān)文章:
1. sqlite時(shí)間戳轉(zhuǎn)時(shí)間語(yǔ)句(時(shí)間轉(zhuǎn)時(shí)間戳)2. Oracle數(shù)據(jù)庫(kù)最優(yōu)化參數(shù)縮短反應(yīng)時(shí)間3. 關(guān)于Mysql中current_time/current_date()與now()區(qū)別4. oracle數(shù)據(jù)庫(kù) exp/imp命令詳解5. mssqlserver和sqlexpress的區(qū)別6. Access 使用總結(jié)一篇7. Mysql入門系列:建立MYSQL客戶機(jī)程序的一般過(guò)程8. 為SQLite3提供一個(gè)ANSI到UTF8的互轉(zhuǎn)函數(shù)9. MySQL不就是多表查詢嗎10. MySQL數(shù)據(jù)庫(kù)node使用詳解
