Redis的幾個核心機制底層原理

  • 2019 年 10 月 18 日
  • 筆記

#### 1、S_DOWN和O_DOWN ######   S_DOWN和O_DOWN兩種宕機狀態  (1)、S_DOWN是主觀宕機,就一個哨兵如果自己覺得一個master宕機了,那麼就是主觀宕機
sdown達成的條件很簡單,如果一個哨兵ping一個master,超過了is-master-down-after-milliseconds指定的毫秒數之後,就主觀認為master宕機
“` # 語法:sentinel down-after-milliseconds # Number of milliseconds the master (or any attached replica or sentinel) should # be unreachable (as in, not acceptable reply to PING, continuously, for the # specified period) in order to consider it in S_DOWN state (Subjectively Down). # 意思是任何從節點或者哨兵在指定的時間內,不能ping通主機就被認為成S_DOWN “`  (2)、O_DOWN是客觀宕機,如果quorum數量的哨兵都覺得一個master宕機了,那麼就是客觀宕機 ######  注意:S_DOWN到O_DOWN轉換的條件很簡單,如果一個哨兵在指定時間內,收到了quorum指定數量的其他哨兵也認為那個master是S_DOWN了,那麼就認為是O_DOWN了 “` # 語法:sentinel monitor # Tells Sentinel to monitor this master, and to consider it in O_DOWN # (Objectively Down) state only if at least sentinels agree. # 意思是:告訴Sentinel監視這個master,如果至少quorum 數量的哨兵同意的話就變成了 # 客觀宕機 “` #### 2、哨兵集群的自動發現機制   1、哨兵互相之間的發現,是通過redis的pub/sub系統實現的,每個哨兵都會往__sentinel__:hello這個channel里發送一個消息,這時候所有其他哨兵都可以消費到這個消息,並感知到其他的哨兵的存在。
  2、每隔兩秒鐘,每個哨兵都會往自己監控的某個master+slaves對應的__sentinel__:hello channel里發送一個消息,內容是自己的host、ip和runid還有對這個master的監控配置,每個哨兵也會去監聽自己監控的每個master+slaves對應的__sentinel__:hello channel,然後去感知到同樣在監聽這個master+slaves的其他哨兵的存在。
  3、每個哨兵還會跟其他哨兵交換對master的監控配置,互相進行監控配置的同步。 #### 3、slave配置的自動糾正   哨兵會負責自動糾正slave的一些配置,比如slave如果要成為潛在的master候選人,哨兵會確保slave在複製現有master的數據; 如果slave連接到了一個錯誤的master上,比如故障轉移之後,那麼哨兵會確保它們連接到正確的master上 #### 4、從機變主機的選舉演算法   如果一個master被認為O_DOWN了,而且majority哨兵都允許了主備切換,那麼某個哨兵就會執行主備切換操作,此時首先要選舉一個slave來會考慮slave的一些資訊:
 (1)跟master斷開連接的時長
 (2)slave優先順序
 (3)複製offset
 (4)run id
  如果一個slave跟master斷開連接已經超過了down-after-milliseconds的10倍,外加master宕機的時長,那麼slave就被認為不適合選舉為master “` (down-after-milliseconds * 10) + milliseconds_since_master_is_in_SDOWN_state “`   接下來會對slave進行排序 (1)按照slave優先順序進行排序,replica-priority越低,優先順序就越高,下面的英文就是這個的解釋: “` # The replica priority is an integer number published by Redis in the INFO output. # It is used by Redis Sentinel in order to select a replica to promote into a # master if the master is no longer working correctly. # # A replica with a low priority number is considered better for promotion, so # for instance if there are three replicas with priority 10, 100, 25 Sentinel will # pick the one with priority 10, that is the lowest. # # However a special priority of 0 marks the replica as not able to perform the # role of master, so a replica with priority of 0 will never be selected by # Redis Sentinel for promotion. # # By default the priority is 100. replica-priority 100 “` (2)如果slave priority相同,那麼看replica offset,哪個slave複製了越多的數據,offset越靠後,優先順序就越高 (3)如果上面兩個條件都相同,那麼選擇一個run id比較小的那個slave #### 5、quorum和majority   1、每次一個哨兵要做主備切換,首先需要quorum數量的哨兵認為O_DOWN,然後選舉出一個哨兵來做切換,這個哨兵還得得到majority哨兵的授權,才能正式執行切換
  2、如果quorum < majority,比如5個哨兵,majority就是3,quorum設置為2,那麼就3個哨兵授權就可以執行切換,但是如果quorum >= majority,那麼必須quorum數量的哨兵都授權,比如5個哨兵,quorum是5,那麼必須5個哨兵都同意授權,才能執行切換 #### 6、configuration epoch ######   哨兵會對一套redis master+slave進行監控,有相應的監控的配置   1、執行切換的那個哨兵,會從要切換到的新master(salve->master)那裡得到一個configuration epoch,這就是一個version號,每次切換的version號都必須是唯一的。
  2、如果第一個選舉出的哨兵切換失敗了,那麼其他哨兵,會等待failover-timeout時間,然後接替繼續執行切換,此時會重新獲取一個新的configuration epoch,作為新的version號。 #### 7、configuraiton傳播   1、哨兵完成切換之後,會在自己本地更新生成最新的master配置,然後同步給其他的哨兵,就是通過之前說的pub/sub消息機制
  2、version號就很重要了,因為各種消息都是通過一個channel去發布和監聽的,所以一個哨兵完成一次新的切換之後,新的master配置是跟著新的version號的
  3、其他的哨兵都是根據版本號的大小來更新自己的master配置的 幫忙關注一下 微信公眾號一起學習 :chengxuyuan95(不一樣的程式設計師)![image](https://img2018.cnblogs.com/blog/1334966/201910/1334966-20191018201157042-1574244697.jpg)