【沃趣科技】MySQL高可用工具Orchestrator系列三:探測機制
沃趣科技作為國內領先的資料庫雲平臺解決方案提供商,一直致力於企業級資料庫雲平臺產品的研發,為使用者提供高效能、高可用、可擴充套件的的資料庫雲環境及不同業務場景需求的資料庫平臺,滿足客戶對極致效能、資料安全、容災備份、業務永續等需求。沃趣科技憑藉專業的團隊,優質的產品,前沿的技術,貼心的服務贏得了客戶的信任與尊重,也獲得了市場的認同。
——————————————————————————————————————
上篇文章講了orchestrator複製拓撲的發現方式。本篇文章我們繼續探索orchestrator的旅程,講一講orchestrator的探測機制。
故障檢測
orch使用了一種整體性的方法去探測主庫和中間主庫是否正常。
一種比較天真的方法,比如,監控工具探測到主庫無法連線或者查詢,就發出報警。這種方法容易受到網路故障而造成誤報。為了降低誤報,會透過執行n次每次間隔t時間的方式。在某些情況下,這減少了誤報的機會,但是增加了真正故障的響應時間。
orchestrator會利用複製拓撲。orch不僅會監測主庫,也會檢測從庫。比如,要診斷出主庫掛了的情況,orch必須滿足以下兩個條件:
-
聯絡不到主庫。
-
可以聯絡到主庫對應的從庫,並且這些從庫也連不上主庫。
orch沒有將錯誤按時間來進行分類,而是按複製拓撲伺服器(也就是所謂的multiple observers)本身進行分類。實際上,當所有的從庫都連不上主庫的時候,說明覆制拓撲實際上就被破壞了,有理由需要進行故障轉移。
orch的這種整體故障檢測方式在生產環境是非常可靠的。
檢測機制
orch會每隔InstancePollSeconds(預設5s)時間去被監控的例項上拉取例項狀態,並將這些狀態資訊存入orch的後設資料庫的orchestrator.database_instance表中,然後orch會每隔InstancePollSeconds秒從後設資料庫中獲取每個instance的狀態,展示在web介面上。
拉取例項狀態的語句如下:
show variables like 'maxscale%' show global status like 'Uptime' select @@global.hostname, ifnull(@@global.report_host, ''), @@global.server_id, @@global.version, @@global.version_comment, @@global.read_only, @@global.binlog_format, @@global.log_bin, @@global.log_slave_updates show master status show global status like 'rpl_semi_sync_%_status' select @@global.gtid_mode, @@global.server_uuid, @@global.gtid_executed, @@global.gtid_purged, @@global.master_info_repository = 'TABLE', @@global.binlog_row_image show slave status select count(*) > 0 and MAX(User_name) != '' from mysql.slave_master_info show slave hosts select substring_index(host, ':', 1) as slave_hostname from information_schema.processlist where command IN ('Binlog Dump', 'Binlog Dump GTID') SELECT SUBSTRING_INDEX(@@hostname, '.', 1)
拉取得到例項狀態之後,透過下面語句將狀態值存入到orch的後設資料庫中:
注:values後面的值就是上面拉取到的例項狀態值。
INSERT INTO database_instance (hostname, port, last_checked, last_attempted_check, last_check_partial_success, uptime, server_id, server_uuid, version, major_version, version_comment, binlog_server, read_only, binlog_format, binlog_row_image, log_bin, log_slave_updates, binary_log_file, binary_log_pos, master_host, master_port, slave_sql_running, slave_io_running, replication_sql_thread_state, replication_io_thread_state, has_replication_filters, supports_oracle_gtid, oracle_gtid, master_uuid, ancestry_uuid, executed_gtid_set, gtid_mode, gtid_purged, gtid_errant, mariadb_gtid, pseudo_gtid, master_log_file, read_master_log_pos, relay_master_log_file, exec_master_log_pos, relay_log_file, relay_log_pos, last_sql_error, last_io_error, seconds_behind_master, slave_lag_seconds, sql_delay, num_slave_hosts, slave_hosts, cluster_name, suggested_cluster_alias, data_center, region, physical_environment, replication_depth, is_co_master, replication_credentials_available, has_replication_credentials, allow_tls, semi_sync_enforced, semi_sync_master_enabled, semi_sync_replica_enabled, instance_alias, last_discovery_latency, last_seen) VALUES ('10.10.30.5', 3306, NOW(), NOW(), 1, 322504, 1521, 'e2685a0f-d8f8-11e9-a2c9-002590e95c3c', '5.7.22-log', '5.7', 'MySQL Community Server (GPL)', 0, 1, 'ROW', 'FULL', 1, 1, 'mysql-bin.000016', 129186924, '10.10.30.6', 3306, 1, 1, 1, 1, 0, 1, 1, '6bf30525-d8f8-11e9-808c-0cc47a74fca8', '6bf30525-d8f8-11e9-808c-0cc47a74fca8,e2685a0f-d8f8-11e9-a2c9-002590e95c3c', '6bf30525-d8f8-11e9-808c-0cc47a74fca8:1-1554568,\ne2685a0f-d8f8-11e9-a2c9-002590e95c3c:1-632541', 'ON', '', '', 0, 0, 'mysql-bin.000017', 150703414, 'mysql-bin.000017', 150703414, 'mysql-relay-bin.000052', 137056344, '', '', 0, 0, 0, 1, '[{\"Hostname\":\"10.10.30.6\",\"Port\":3306}]', '10.10.30.6:3306', 'qhp-6', '', '', '', 1, 1, 1, 1, 0, 0, 0, 0, '', 8083748, NOW()) ON DUPLICATE KEY UPDATE hostname=VALUES(hostname), port=VALUES(port), last_checked=VALUES(last_checked), last_attempted_check=VALUES(last_attempted_check), last_check_partial_success=VALUES(last_check_partial_success), uptime=VALUES(uptime), server_id=VALUES(server_id), server_uuid=VALUES(server_uuid), version=VALUES(version), major_version=VALUES(major_version), version_comment=VALUES(version_comment), binlog_server=VALUES(binlog_server), read_only=VALUES(read_only), binlog_format=VALUES(binlog_format), binlog_row_image=VALUES(binlog_row_image), log_bin=VALUES(log_bin), log_slave_updates=VALUES(log_slave_updates), binary_log_file=VALUES(binary_log_file), binary_log_pos=VALUES(binary_log_pos), master_host=VALUES(master_host), master_port=VALUES(master_port), slave_sql_running=VALUES(slave_sql_running), slave_io_running=VALUES(slave_io_running), replication_sql_thread_state=VALUES(replication_sql_thread_state), replication_io_thread_state=VALUES(replication_io_thread_state), has_replication_filters=VALUES(has_replication_filters), supports_oracle_gtid=VALUES(supports_oracle_gtid), oracle_gtid=VALUES(oracle_gtid), master_uuid=VALUES(master_uuid), ancestry_uuid=VALUES(ancestry_uuid), executed_gtid_set=VALUES(executed_gtid_set), gtid_mode=VALUES(gtid_mode), gtid_purged=VALUES(gtid_purged), gtid_errant=VALUES(gtid_errant), mariadb_gtid=VALUES(mariadb_gtid), pseudo_gtid=VALUES(pseudo_gtid), master_log_file=VALUES(master_log_file), read_master_log_pos=VALUES(read_master_log_pos), relay_master_log_file=VALUES(relay_master_log_file), exec_master_log_pos=VALUES(exec_master_log_pos), relay_log_file=VALUES(relay_log_file), relay_log_pos=VALUES(relay_log_pos), last_sql_error=VALUES(last_sql_error), last_io_error=VALUES(last_io_error), seconds_behind_master=VALUES(seconds_behind_master), slave_lag_seconds=VALUES(slave_lag_seconds), sql_delay=VALUES(sql_delay), num_slave_hosts=VALUES(num_slave_hosts), slave_hosts=VALUES(slave_hosts), cluster_name=VALUES(cluster_name), suggested_cluster_alias=VALUES(suggested_cluster_alias), data_center=VALUES(data_center), region=VALUES(region), physical_environment=VALUES(physical_environment), replication_depth=VALUES(replication_depth), is_co_master=VALUES(is_co_master), replication_credentials_available=VALUES(replication_credentials_available), has_replication_credentials=VALUES(has_replication_credentials), allow_tls=VALUES(allow_tls), semi_sync_enforced=VALUES(semi_sync_enforced), semi_sync_master_enabled=VALUES(semi_sync_master_enabled), semi_sync_replica_enabled=VALUES(semi_sync_replica_enabled), instance_alias=VALUES(instance_alias), last_discovery_latency=VALUES(last_discovery_latency), last_seen=VALUES(last_seen)
然後orch會每隔InstancePollSeconds秒從後設資料庫中獲取每個被監控例項的狀態,透過web端展示到頁面上。
探測例項失敗
如果某個instance掛了,orch每隔InstancePollSeconds時間拉取例項狀態失敗,無法獲取到最新的例項狀態,也就無法用上面這條insert將例項狀態存入到後設資料庫中,那麼orch會按下面的方式更新後設資料庫:
// 每隔InstancePollSeconds時間更新database_instance表的last_checked和last_check_partial_success欄位 update database_instance set last_checked = NOW(), last_check_partial_success = 0 where hostname = '10.10.30.170' and port = 3306 // 每隔InstancePollSeconds+1s時間更新database_instance表的last_attempted_check欄位 update database_instance set last_attempted_check = NOW() where hostname = '10.10.30.170' and port = 3306
這裡為什麼要引入last_attempted_check,摘兩處原始碼中的註釋。
// UpdateInstanceLastAttemptedCheck updates the last_attempted_check timestamp in the orchestrator backed database // for a given instance. // This is used as a failsafe mechanism in case access to the instance gets hung (it happens), in which case // the entire ReadTopology gets stuck (and no, connection timeout nor driver timeouts don't help. Don't look at me, // the world is a harsh place to live in). // And so we make sure to note down *before* we even attempt to access the instance; and this raises a red flag when we // wish to access the instance again: if last_attempted_check is *newer* than last_checked, that's bad news and means // we have a "hanging" issue. func UpdateInstanceLastAttemptedCheck(instanceKey *InstanceKey) error { writeFunc := func() error { _, err := db.ExecOrchestrator(` update database_instance set last_attempted_check = NOW() where hostname = ? and port = ?`, instanceKey.Hostname, instanceKey.Port, ) return log.Errore(err) } return ExecDBWriteFunc(writeFunc) } // ValidSecondsFromSeenToLastAttemptedCheck returns the maximum allowed elapsed time // between last_attempted_check to last_checked before we consider the instance as invalid. func ValidSecondsFromSeenToLastAttemptedCheck() uint { return config.Config.InstancePollSeconds + 1 }
判斷例項是否存活
被orch監控的例項是否正常,透過如下方式進行判斷:
// instance每隔InstancePollSeconds時間透過如下方式判斷某個instance是否正常 select ifnull(last_checked <= last_seen, 0) as is_last_check_valid from database_instance where hostname = '10.10.30.170' and port = 3306 order by hostname, port; // master instance是否存活的判斷比較複雜 // 首先,orch會每秒透過類似於如下方式判斷master instance是否正常 SELECT MIN(master_instance.last_checked <= master_instance.last_seen and master_instance.last_attempted_check <= master_instance.last_seen + interval 6 second) = 1 AS is_last_check_valid from database_instance master_instance GROUP BY master_instance.hostname, master_instance.port; // 如果is_last_check_valid為0,然後需要判斷master instance對應的從庫是否連得上,並且這些從庫是否也連不上主庫 // 整個sql如下: SELECT master_instance.hostname, master_instance.port, master_instance.read_only AS read_only, MIN(master_instance.data_center) AS data_center, MIN(master_instance.region) AS region, MIN(master_instance.physical_environment) AS physical_environment, MIN(master_instance.master_host) AS master_host, MIN(master_instance.master_port) AS master_port, MIN(master_instance.cluster_name) AS cluster_name, MIN(IFNULL(cluster_alias.alias, master_instance.cluster_name)) AS cluster_alias, MIN( master_instance.last_checked <= master_instance.last_seen and master_instance.last_attempted_check <= master_instance.last_seen + interval 6 second ) = 1 AS is_last_check_valid, MIN(master_instance.last_check_partial_success) as last_check_partial_success, MIN(master_instance.master_host IN ('' , '_') OR master_instance.master_port = 0 OR substr(master_instance.master_host, 1, 2) = '//') AS is_master, MIN(master_instance.is_co_master) AS is_co_master, MIN(CONCAT(master_instance.hostname, ':', master_instance.port) = master_instance.cluster_name) AS is_cluster_master, MIN(master_instance.gtid_mode) AS gtid_mode, COUNT(replica_instance.server_id) AS count_replicas, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen), 0) AS count_valid_slaves, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.slave_io_running != 0 AND replica_instance.slave_sql_running != 0), 0) AS count_valid_replicating_slaves, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.slave_io_running = 0 AND replica_instance.last_io_error like '%error %connecting to master%' AND replica_instance.slave_sql_running = 1), 0) AS count_replicas_failing_to_connect_to_master, MIN(master_instance.replication_depth) AS replication_depth, GROUP_CONCAT(concat(replica_instance.Hostname, ':', replica_instance.Port)) as slave_hosts, MIN( master_instance.slave_sql_running = 1 AND master_instance.slave_io_running = 0 AND master_instance.last_io_error like '%error %connecting to master%' ) AS is_failing_to_connect_to_master, MIN( master_downtime.downtime_active is not null and ifnull(master_downtime.end_timestamp, now()) > now() ) AS is_downtimed, MIN( IFNULL(master_downtime.end_timestamp, '') ) AS downtime_end_timestamp, MIN( IFNULL(unix_timestamp() - unix_timestamp(master_downtime.end_timestamp), 0) ) AS downtime_remaining_seconds, MIN( master_instance.binlog_server ) AS is_binlog_server, MIN( master_instance.pseudo_gtid ) AS is_pseudo_gtid, MIN( master_instance.supports_oracle_gtid ) AS supports_oracle_gtid, SUM( replica_instance.oracle_gtid ) AS count_oracle_gtid_slaves, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.oracle_gtid != 0), 0) AS count_valid_oracle_gtid_slaves, SUM( replica_instance.binlog_server ) AS count_binlog_server_slaves, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.binlog_server != 0), 0) AS count_valid_binlog_server_slaves, MIN( master_instance.mariadb_gtid ) AS is_mariadb_gtid, SUM( replica_instance.mariadb_gtid ) AS count_mariadb_gtid_slaves, IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.mariadb_gtid != 0), 0) AS count_valid_mariadb_gtid_slaves, IFNULL(SUM(replica_instance.log_bin AND replica_instance.log_slave_updates), 0) AS count_logging_replicas, IFNULL(SUM(replica_instance.log_bin AND replica_instance.log_slave_updates AND replica_instance.binlog_format = 'STATEMENT'), 0) AS count_statement_based_loggin_slaves, IFNULL(SUM(replica_instance.log_bin AND replica_instance.log_slave_updates AND replica_instance.binlog_format = 'MIXED'), 0) AS count_mixed_based_loggin_slaves, IFNULL(SUM(replica_instance.log_bin AND replica_instance.log_slave_updates AND replica_instance.binlog_format = 'ROW'), 0) AS count_row_based_loggin_slaves, IFNULL(SUM(replica_instance.sql_delay > 0), 0) AS count_delayed_replicas, IFNULL(SUM(replica_instance.slave_lag_seconds > 10), 0) AS count_lagging_replicas, IFNULL(MIN(replica_instance.gtid_mode), '') AS min_replica_gtid_mode, IFNULL(MAX(replica_instance.gtid_mode), '') AS max_replica_gtid_mode, IFNULL(MAX( case when replica_downtime.downtime_active is not null and ifnull(replica_downtime.end_timestamp, now()) > now() then '' else replica_instance.gtid_errant end ), '') AS max_replica_gtid_errant, IFNULL(SUM( replica_downtime.downtime_active is not null and ifnull(replica_downtime.end_timestamp, now()) > now()), 0) AS count_downtimed_replicas, COUNT(DISTINCT case when replica_instance.log_bin AND replica_instance.log_slave_updates then replica_instance.major_version else NULL end ) AS count_distinct_logging_major_versions FROM database_instance master_instance LEFT JOIN hostname_resolve ON (master_instance.hostname = hostname_resolve.hostname) LEFT JOIN database_instance replica_instance ON (COALESCE(hostname_resolve.resolved_hostname, master_instance.hostname) = replica_instance.master_host AND master_instance.port = replica_instance.master_port) LEFT JOIN database_instance_maintenance ON (master_instance.hostname = database_instance_maintenance.hostname AND master_instance.port = database_instance_maintenance.port AND database_instance_maintenance.maintenance_active = 1) LEFT JOIN database_instance_downtime as master_downtime ON (master_instance.hostname = master_downtime.hostname AND master_instance.port = master_downtime.port AND master_downtime.downtime_active = 1) LEFT JOIN database_instance_downtime as replica_downtime ON (replica_instance.hostname = replica_downtime.hostname AND replica_instance.port = replica_downtime.port AND replica_downtime.downtime_active = 1) LEFT JOIN cluster_alias ON (cluster_alias.cluster_name = master_instance.cluster_name) WHERE database_instance_maintenance.database_instance_maintenance_id IS NULL AND '' IN ('', master_instance.cluster_name) GROUP BY master_instance.hostname, master_instance.port HAVING (MIN( master_instance.last_checked <= master_instance.last_seen and master_instance.last_attempted_check <= master_instance.last_seen + interval 6 second ) = 1 ) = 0 OR (IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.slave_io_running = 0 AND replica_instance.last_io_error like '%error %connecting to master%' AND replica_instance.slave_sql_running = 1), 0) > 0) OR (IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen), 0) < COUNT(replica_instance.server_id) ) OR (IFNULL(SUM(replica_instance.last_checked <= replica_instance.last_seen AND replica_instance.slave_io_running != 0 AND replica_instance.slave_sql_running != 0), 0) < COUNT(replica_instance.server_id) ) OR (MIN( master_instance.slave_sql_running = 1 AND master_instance.slave_io_running = 0 AND master_instance.last_io_error like '%error %connecting to master%' ) ) OR (COUNT(replica_instance.server_id) > 0) ORDER BY is_master DESC , is_cluster_master DESC, count_replicas DESC\G
參考:
| 作者簡介
韓傑 沃趣科技高階資料庫工程師
專注MySQL資料庫三年,精通MySQL體系結構,資料庫最佳化,trouble shooting。服務過多家銀行客戶,熟悉銀行業務及系統下資料庫不同架構使用場景。熟悉MySQL主從複製原理,及應用的各種高可用場景。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28218939/viewspace-2664928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【沃趣科技】MySQL高可用工具Orchestrator系列四:拓撲恢復MySql
- 【沃趣科技】MySQL高可用工具Orchestrator系列五:raft多節點模式安裝MySqlRaft模式
- MySQL高可用工具Orchestrator系列一:單節點模式安裝MySql模式
- MySQL高可用工具Orchestrator系列二:複製拓撲的發現MySql
- 沃趣科技李春:MySQL併發複製探祕MySql
- 【DB寶40】MySQL高可用管理工具Orchestrator簡介及測試MySql
- 【沃趣科技】直方圖系列1直方圖
- Redis高可用——副本機制Redis
- 高可用系列文章之三 - NGINX 高可用實施方案Nginx
- 沃趣微講堂 | Oracle叢集技術(五):叢集中的三種心跳機制Oracle
- HDFS HA 高可用機制詳解
- 【沃趣科技】再述mysqldump時域問題MySql
- 2021DTCC沃趣科技專訪視訊
- KunlunDB的Fullsync高可用機制簡介
- HDFS 09 - HDFS NameNode 的高可用機制
- 官方工具|MySQL Router 高可用原理與實戰MySql
- MySql(三) MySql中的鎖機制MySql
- 版本管理工具Git(三)Gitlab高可用Gitlab
- Redis高可用之哨兵機制實現細節Redis
- 超全面Redis分散式高可用方案:哨兵機制Redis分散式
- 高可用高可靠系統設計中的重試機制
- MySQL高可用之MHA切換測試(switchover & failover)MySqlAI
- MySQL——MHA高可用群集部署及故障測試MySql
- MySQL高可用群集MHA部署及故障測試分析MySql
- 【DB寶19】MySQL高可用之MHA功能測試MySql
- 高可用 proxysql + mysql MGRMySql
- mysql高可用之keepalivedMySql
- Mysql 5.7 MHA 高可用MySql
- Go小工具系列——重試機制Go
- Mysql高可用架構方案MySql架構
- mysql高可用衡搭建(Keepalived)MySql
- MySQL高可用之GC-Galera Cluster for MySQLMySqlGC
- 重磅官宣!沃趣科技完成數千萬B+輪戰略融資
- MySQL探祕(四):InnoDB的磁碟檔案及落盤機制MySql
- OkHttp 原始碼剖析系列(三)——快取機制HTTP原始碼快取
- MySQL高可用架構對比MySql架構
- 基於 MHA 高可用的 MySQLMySql
- keepalived+MySQL實現高可用MySql