介紹:

在遷移的過中, gh-ost 的狀態除了持續的複製原表和應用binlog外,還有另一個狀態是 throttling
throttled(限制) 的時候, gh-ost 會暫停所有寫操作,包含複製原表的資料和檢查binlog,但仍會做 low-volume changlog status writesheartbeat writes
gh-ost 被限制時,對主機的 Loading 將徹底消失,和基於 trigger 原理的工具相比這是 gh-ost 的優勢,因為 trigger 無法暫停會持續的寫入新資料,相反的 gh-ost 包含複製原表資料和寫入新資料(透過應用 binlog event)都是由 gh-ost 進行控制。
gh-ost 支持各種限制的方法,並且他能允許使用者動態的調整。

Throttle的參數和因素:

Replication lag:

推薦將 gh-ost 連結到 Slavegh-ost 會自行找出 Master 開始進行遷移的同時, gh-ost 有自己的心跳機制去檢查他所連接的 Slave 是否有 replication lag 的狀況。
或者你也可以自行指定要檢查 replication lag 的清單。
  • --throttle-control-replicas :明確列出需要 gh-ost 檢查 replication lagSlave 清單
    • 範例:--throttle-control-replicas=myhost1.com:3306,myhost2.com,myhost3.com:3307
  • --max-lag-millis:允許 replication lag 的最大值,任何在檢查清單中的 replication lag 超過此值都將導致 throttling,除非清單中的所有 replication lag 都小於此值才會恢復。
以上設定丟可以透過 Interaction commands 來隨時動態調整。

Status thresholds

  • --max-load :當指定的指標超過設定的閾值時,將導致 throttling 發生。
    • 範例:--max-load='Threads_running=100,Threads_connected=500'
      指標必須是有效且為數字的 status variables (可參考mysql 官網)

Throttle query

  • 當提供 --throttle-query 時,必須確保返回整數,每秒會執行該查詢(因此應確保此查詢為輕量的),當返回值 > 0gh-ost 將進入 throttling 狀態;反之當返回值 <= 0 時且沒有其他限制因素時,則 gh-ost 正常運作
    • 範例:--throttle-query="select hour(now()) between 8 and 17" 表示 8:00am 開始進入 throttling 狀態,直到 18:00pm 解除。

HTTP Throttle

The --throttle-http flag allows for throttling via HTTP. Every 100ms gh-ost issues a HEAD request to the provided URL. If the response status code is not 200 throttling will kick in until a 200 response status code is returned.
If no URL is provided or the URL provided doesn't contain the scheme then the HTTP check will be disabled. For example --throttle-http="http://1.2.3.4:6789/throttle" will enable the HTTP check/throttling, but --throttle-http="1.2.3.4:6789/throttle" will not.
The URL can be queried and updated dynamically via interactive interface.

Manual control

除了以上的自動控制,還可以隨時手動讓 gh-ost 進入 throttling 狀態。
  • --throttle-flag-file :當該文件存在時,進入 throttling 狀態。可以使用 touch 建立即可
  • --throttle-additional-flag-file :與上述類似,當該文件存在時,進入 throttling 狀態。
    • 默認值為: /tmp/gh-ost.throttle
      有兩種參數的原因是可以運行多個 gh-ost--throttle-flag-file 為單個 gh-ost 所使用,--throttle-additional-flag-file 則是同時控制所有 gh-ost ,因此我們可以透過 touch 不同的文件來限制單個或多個 gh-ost
  • 透過 Interactive commandsgh-ost 下達 throttle
    • 範例:

Throttle precedence

上述提到的任何因素都將導致 gh-ost 進入 throttling 狀態,因此一旦其中一個因素引發 throttling 你都無法強制繼續執行遷移。
gh-ost 會在不同的時間點獨立收集不同的指標,並異步檢查不同的指標是否有達到閾值,因此 gh-ost 只會返回第一個超過閾值的指標作為 throttling 原因。

Throttle status

throttle 狀態會作為 status message 定期的 print 出來:

可以 throttle 多久?

最長可以維持 throttle 狀態的時間依賴於 binlog 保留的時間,因為當進入 throttling 狀態時, gh-ost 將暫停讀取 binlog ,之後恢復會從同一個 binlog 開始。
binlog 保留時間通常透過 expire_logs_days 設定,或者是其他外部腳本,因此必須先確認機器上的部屬,另外雖然 binlog 保留多久 gh-ost 就能 throttle 多久,但是在此期間 gh-ost 必須一直運行,並且相應的當恢復之後需要應用相當大量的 binlog event
 

參考: