亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

一文看懂Redis的七個(gè)核心機(jī)制、底層原理

 

一、S_DOWN和O_DOWN

S_DOWN和O_DOWN兩種宕機(jī)狀態(tài)

(1)、S_DOWN是主觀宕機(jī),就一個(gè)哨兵如果自己覺得一個(gè)master宕機(jī)了,那么就是主觀宕機(jī)

sdown達(dá)成的條件很簡(jiǎn)單,如果一個(gè)哨兵ping一個(gè)master,超過了is-master-down-after-milliseconds指定的毫秒數(shù)之后,就主觀認(rèn)為master宕機(jī)

# 語法:sentinel down-after-milliseconds <master-name> <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).
# 意思是任何從節(jié)點(diǎn)或者哨兵在指定的時(shí)間內(nèi),不能ping通主機(jī)就被認(rèn)為成S_DOWN

(2)、O_DOWN是客觀宕機(jī),如果quorum數(shù)量的哨兵都覺得一個(gè)master宕機(jī)了,那么就是客觀宕機(jī)

注意:S_DOWN到O_DOWN轉(zhuǎn)換的條件很簡(jiǎn)單,如果一個(gè)哨兵在指定時(shí)間內(nèi),收到了quorum指定數(shù)量的其他哨兵也認(rèn)為那個(gè)master是S_DOWN了,那么就認(rèn)為是O_DOWN了

# 語法:sentinel monitor <master-name> <ip> <redis-port> <quorum>
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
# 意思是:告訴Sentinel監(jiān)視這個(gè)master,如果至少quorum 數(shù)量的哨兵同意的話就變成了
# 客觀宕機(jī)

二、哨兵集群的自動(dòng)發(fā)現(xiàn)機(jī)制

1、哨兵互相之間的發(fā)現(xiàn),是通過redis的pub/sub系統(tǒng)實(shí)現(xiàn)的,每個(gè)哨兵都會(huì)往sentinel:hello這個(gè)channel里發(fā)送一個(gè)消息,這時(shí)候所有其他哨兵都可以消費(fèi)到這個(gè)消息,并感知到其他的哨兵的存在。

2、每隔兩秒鐘,每個(gè)哨兵都會(huì)往自己監(jiān)控的某個(gè)master+slaves對(duì)應(yīng)的sentinel:hello channel里發(fā)送一個(gè)消息,內(nèi)容是自己的host、ip和runid還有對(duì)這個(gè)master的監(jiān)控配置,每個(gè)哨兵也會(huì)去監(jiān)聽自己監(jiān)控的每個(gè)master+slaves對(duì)應(yīng)的sentinel:hello channel,然后去感知到同樣在監(jiān)聽這個(gè)master+slaves的其他哨兵的存在。 

3、每個(gè)哨兵還會(huì)跟其他哨兵交換對(duì)master的監(jiān)控配置,互相進(jìn)行監(jiān)控配置的同步。

三、slave配置的自動(dòng)糾正

哨兵會(huì)負(fù)責(zé)自動(dòng)糾正slave的一些配置,比如slave如果要成為潛在的master候選人,哨兵會(huì)確保slave在復(fù)制現(xiàn)有master的數(shù)據(jù); 如果slave連接到了一個(gè)錯(cuò)誤的master上,比如故障轉(zhuǎn)移之后,那么哨兵會(huì)確保它們連接到正確的master上

四、從機(jī)變主機(jī)的選舉算法

如果一個(gè)master被認(rèn)為O_DOWN了,而且majority哨兵都允許了主備切換,那么某個(gè)哨兵就會(huì)執(zhí)行主備切換操作,此時(shí)首先要選舉一個(gè)slave來會(huì)考慮slave的一些信息:

(1)跟master斷開連接的時(shí)長(zhǎng)

(2)slave優(yōu)先級(jí)

(3)復(fù)制offset

(4)run id

如果一個(gè)slave跟master斷開連接已經(jīng)超過了down-after-milliseconds的10倍,外加master宕機(jī)的時(shí)長(zhǎng),那么slave就被認(rèn)為不適合選舉為master

(down-after-milliseconds * 10) + milliseconds_since_master_is_in_SDOWN_state

接下來會(huì)對(duì)slave進(jìn)行排序

(1)按照slave優(yōu)先級(jí)進(jìn)行排序,replica-priority越低,優(yōu)先級(jí)就越高,下面的英文就是這個(gè)的解釋:

# 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,哪個(gè)slave復(fù)制了越多的數(shù)據(jù),offset越靠后,優(yōu)先級(jí)就越高

(3)如果上面兩個(gè)條件都相同,那么選擇一個(gè)run id比較小的那個(gè)slave

五、quorum和majority

1、每次一個(gè)哨兵要做主備切換,首先需要quorum數(shù)量的哨兵認(rèn)為O_DOWN,然后選舉出一個(gè)哨兵來做切換,這個(gè)哨兵還得得到majority哨兵的授權(quán),才能正式執(zhí)行切換 

2、如果quorum < majority,比如5個(gè)哨兵,majority就是3,quorum設(shè)置為2,那么就3個(gè)哨兵授權(quán)就可以執(zhí)行切換,但是如果quorum >= majority,那么必須quorum數(shù)量的哨兵都授權(quán),比如5個(gè)哨兵,quorum是5,那么必須5個(gè)哨兵都同意授權(quán),才能執(zhí)行切換

六、configuration epoch

哨兵會(huì)對(duì)一套redis master+slave進(jìn)行監(jiān)控,有相應(yīng)的監(jiān)控的配置

1、執(zhí)行切換的那個(gè)哨兵,會(huì)從要切換到的新master(salve->master)那里得到一個(gè)configuration epoch,這就是一個(gè)version號(hào),每次切換的version號(hào)都必須是唯一的。

2、如果第一個(gè)選舉出的哨兵切換失敗了,那么其他哨兵,會(huì)等待failover-timeout時(shí)間,然后接替繼續(xù)執(zhí)行切換,此時(shí)會(huì)重新獲取一個(gè)新的configuration epoch,作為新的version號(hào)。

7、configuraiton傳播

1、哨兵完成切換之后,會(huì)在自己本地更新生成最新的master配置,然后同步給其他的哨兵,就是通過之前說的pub/sub消息機(jī)制

2、version號(hào)就很重要了,因?yàn)楦鞣N消息都是通過一個(gè)channel去發(fā)布和監(jiān)聽的,所以一個(gè)哨兵完成一次新的切換之后,新的master配置是跟著新的version號(hào)的

3、其他的哨兵都是根據(jù)版本號(hào)的大小來更新自己的master配置的

分享到:
標(biāo)簽:Redis
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定