sdown達成的條件很簡單,如果一個哨兵ping一個master,超過了is-master-down-after-milliseconds指定的毫秒數之后,就主觀認為master宕機
``` # 語法:sentinel down-after-milliseconds
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(不一樣的程序員)