溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Redis中sentinel故障轉(zhuǎn)移的示例分析

發(fā)布時(shí)間:2021-10-28 11:18:58 來(lái)源:億速云 閱讀:141 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫(kù)

這篇文章主要為大家展示了“Redis中sentinel故障轉(zhuǎn)移的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Redis中sentinel故障轉(zhuǎn)移的示例分析”這篇文章吧。

當(dāng)兩臺(tái)以上的Redis實(shí)例形成了主備關(guān)系,它們組成的集群就具備了一定的高可用性:當(dāng)master發(fā)生故障的時(shí)候,slave可以成為新的master對(duì)外提供讀寫(xiě)服務(wù),這種運(yùn)營(yíng)機(jī)制成為failover。

那么誰(shuí)來(lái)發(fā)現(xiàn)master的故障做failover決策?

一種方式是,保持一個(gè)daemo進(jìn)程,監(jiān)控著所有的master-slave節(jié)點(diǎn),如下圖所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

一個(gè)Redis集群里面有一個(gè)master和兩個(gè)slave,這個(gè)daemon進(jìn)程監(jiān)控著這三個(gè)節(jié)點(diǎn)。但daemon為單節(jié)點(diǎn),本身可用性無(wú)法保證。需要引入多daemon,如下圖所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

多個(gè)daemon解決了可用性問(wèn)題,但又出現(xiàn)了一致性問(wèn)題,如何就某個(gè)master是否可用達(dá)成一致?例如上圖兩個(gè)daemon1和和master網(wǎng)絡(luò)不通,daemon和master連接暢通,那此時(shí)mater節(jié)點(diǎn)是否需要failover那?

Redis的sentinel提供了一套多daemon間的交互機(jī)制,多個(gè)daemon間組成一個(gè)集群,成為sentinel集群,daemon節(jié)點(diǎn)也稱(chēng)為sentinel節(jié)點(diǎn)。如下圖所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

這些節(jié)點(diǎn)相互間通信、選舉、協(xié)商,在master節(jié)點(diǎn)的故障發(fā)現(xiàn)、failover決策上表現(xiàn)出一致性。

sentinel集群監(jiān)視任意多個(gè)master以及master下的slave,自動(dòng)將下線(xiàn)的master從其下的某個(gè)slave升級(jí)為新的master代替繼續(xù)處理命令請(qǐng)求。

啟動(dòng)并初始化Sentinel

啟動(dòng)一個(gè)Sentinel可以使用命令:

./redis-sentinel ../sentinel.conf

或者命令:

./redis-server ../sentinel.conf --sentinel

當(dāng)一個(gè)Sentinel啟動(dòng)時(shí),它需要執(zhí)行以下步驟:

初始化服務(wù)器

Sentinel本質(zhì)上是運(yùn)行在特殊模式下的Redis服務(wù)器,它和普通的Redis服務(wù)器執(zhí)行的工作不同,初始化過(guò)程也不完全相同。如普通的Redis服務(wù)器初始化會(huì)載入RDB或者AOF文件來(lái)恢復(fù)數(shù)據(jù),而Sentinel啟動(dòng)時(shí)不會(huì)載入,因?yàn)镾entinel并不使用數(shù)據(jù)庫(kù)。

將普通Redis服務(wù)器使用的代碼替換成Sentinel專(zhuān)用代碼

將一部分普通Redis服務(wù)器使用的代碼替換成Sentinel專(zhuān)用代碼。如普通Redis服務(wù)器使用server.c/redisCommandTable作為服務(wù)器的命令表:

truct redisCommand redisCommandTable[] = {
    {"module",moduleCommand,-2,"as",0,NULL,0,0,0,0,0},
    {"get",getCommand,2,"rF",0,NULL,1,1,1,0,0},
    {"set",setCommand,-3,"wm",0,NULL,1,1,1,0,0},
    {"setnx",setnxCommand,3,"wmF",0,NULL,1,1,1,0,0},
    {"setex",setexCommand,4,"wm",0,NULL,1,1,1,0,0},
    {"psetex",psetexCommand,4,"wm",0,NULL,1,1,1,0,0},
    {"append",appendCommand,3,"wm",0,NULL,1,1,1,0,0},
    .....
    {"del",delCommand,-2,"w",0,NULL,1,-1,1,0,0},
    {"unlink",unlinkCommand,-2,"wF",0,NULL,1,-1,1,0,0},
    {"exists",existsCommand,-2,"rF",0,NULL,1,-1,1,0,0},
    {"setbit",setbitCommand,4,"wm",0,NULL,1,1,1,0,0},
    {"getbit",getbitCommand,3,"rF",0,NULL,1,1,1,0,0},
    {"bitfield",bitfieldCommand,-2,"wm",0,NULL,1,1,1,0,0},
    {"setrange",setrangeCommand,4,"wm",0,NULL,1,1,1,0,0},
    {"getrange",getrangeCommand,4,"r",0,NULL,1,1,1,0,0},
    {"substr",getrangeCommand,4,"r",0,NULL,1,1,1,0,0},
    {"incr",incrCommand,2,"wmF",0,NULL,1,1,1,0,0},
    {"decr",decrCommand,2,"wmF",0,NULL,1,1,1,0,0},
    {"mget",mgetCommand,-2,"rF",0,NULL,1,-1,1,0,0},
    {"rpush",rpushCommand,-3,"wmF",0,NULL,1,1,1,0,0},
    {"lpush",lpushCommand,-3,"wmF",0,NULL,1,1,1,0,0}
    ......
    }

Sentinel使用sentinel.c/sentinelcmds作為服務(wù)器列表,如下所示:

struct redisCommand sentinelcmds[] = {
    {"ping",pingCommand,1,"",0,NULL,0,0,0,0,0},
    {"sentinel",sentinelCommand,-2,"",0,NULL,0,0,0,0,0},
    {"subscribe",subscribeCommand,-2,"",0,NULL,0,0,0,0,0},
    {"unsubscribe",unsubscribeCommand,-1,"",0,NULL,0,0,0,0,0},
    {"psubscribe",psubscribeCommand,-2,"",0,NULL,0,0,0,0,0},
    {"punsubscribe",punsubscribeCommand,-1,"",0,NULL,0,0,0,0,0},
    {"publish",sentinelPublishCommand,3,"",0,NULL,0,0,0,0,0},
    {"info",sentinelInfoCommand,-1,"",0,NULL,0,0,0,0,0},
    {"role",sentinelRoleCommand,1,"l",0,NULL,0,0,0,0,0},
    {"client",clientCommand,-2,"rs",0,NULL,0,0,0,0,0},
    {"shutdown",shutdownCommand,-1,"",0,NULL,0,0,0,0,0},
    {"auth",authCommand,2,"sltF",0,NULL,0,0,0,0,0}
}

初始化Sentinel狀態(tài)

服務(wù)器會(huì)初始化一個(gè)sentinel.c/sentinelState結(jié)構(gòu)(保存服務(wù)器中所有和Sentinel功能有關(guān)的狀態(tài))。

struct sentinelState {
 
    char myid[CONFIG_RUN_ID_SIZE+1]; /* This sentinel ID. */
    
    //當(dāng)前紀(jì)元,用于實(shí)現(xiàn)故障轉(zhuǎn)移
    uint64_t current_epoch;         /* Current epoch. */
    
    //監(jiān)視的主服務(wù)器
    //字典的鍵是主服務(wù)器的名字
    //字典的值則是一個(gè)指向sentinelRedisInstances結(jié)構(gòu)的指針
    dict *masters;      /* Dictionary of master sentinelRedisInstances.
                           Key is the instance name, value is the
                           sentinelRedisInstance structure pointer. */
    //是否進(jìn)入tilt模式
    int tilt;           /* Are we in TILT mode? */
    
    //目前正在執(zhí)行的腳本數(shù)量
    int running_scripts;    /* Number of scripts in execution right now. */
    
    //進(jìn)入tilt模式的時(shí)間
    mstime_t tilt_start_time;       /* When TITL started. */
    
    //最后一次執(zhí)行時(shí)間處理器的時(shí)間
    mstime_t previous_time;         /* Last time we ran the time handler. */
    
    // 一個(gè)FIFO隊(duì)列,包含了所有需要執(zhí)行的用戶(hù)腳本
    list *scripts_queue;            /* Queue of user scripts to execute. */
    
    char *announce_ip;  /* IP addr that is gossiped to other sentinels if
                           not NULL. */
    int announce_port;  /* Port that is gossiped to other sentinels if
                           non zero. */
    unsigned long simfailure_flags; /* Failures simulation. */
    int deny_scripts_reconfig; /* Allow SENTINEL SET ... to change script
                                  paths at runtime? */
}

根據(jù)給定的配置文件,初始化Sentinel的監(jiān)視主服務(wù)器列表

對(duì)Sentinel狀態(tài)的初始化將引發(fā)對(duì)masters字典的初始化,而master字典的初始化是根據(jù)被載入的Sentinel配置文件來(lái)進(jìn)行的。

字典的key是監(jiān)視主服務(wù)器的名字,字典的值則是被監(jiān)控主服務(wù)器對(duì)應(yīng)的sentinel.c/sentinelRedisInstance結(jié)構(gòu)。

sentinelRedisInstance結(jié)構(gòu)部分屬性如下:

typedef struct sentinelRedisInstance {
    //標(biāo)識(shí)值,記錄了實(shí)例的類(lèi)型,以及該實(shí)例的當(dāng)前狀態(tài)
    int flags;      /* See SRI_... defines */
    
    //實(shí)例的名字
    //主服務(wù)器的名字由用戶(hù)在配置文件中設(shè)置
    //從服務(wù)器以及Sentinel的名字由Sentinel自動(dòng)設(shè)置
    //格式為ip:port,例如“127.0.0.1:26379”
    char *name;     /* Master name from the point of view of this sentinel. */
    
    //實(shí)例運(yùn)行的ID
    char *runid;    /* Run ID of this instance, or unique ID if is a Sentinel.*/
    
    //配置紀(jì)元,用于實(shí)現(xiàn)故障轉(zhuǎn)移
    uint64_t config_epoch;  /* Configuration epoch. */
    
    //實(shí)例的地址
    sentinelAddr *addr; /* Master host. */
    
    //sentinel down-after-milliseconds選項(xiàng)設(shè)定的值
    //實(shí)例無(wú)響應(yīng)多少毫秒之后才會(huì)被判斷為主觀(guān)下線(xiàn)(subjectively down)
    mstime_t down_after_period; /* Consider it down after that period. */
    
    //sentinel monitor <master-name> <ip> <redis-port> <quorum>選項(xiàng)中的quorum
    //判斷這個(gè)實(shí)例為客觀(guān)下線(xiàn)(objective down)所需的支持投票的數(shù)量
    unsigned int quorum;/* Number of sentinels that need to agree on failure. */  
    //sentinel parallel-syncs <master-name> <numreplicas> 選項(xiàng)的numreplicas值
    //在執(zhí)行故障轉(zhuǎn)移操作時(shí),可以同時(shí)對(duì)新的主服務(wù)器進(jìn)行同步的從服務(wù)器數(shù)量
    int parallel_syncs; /* How many slaves to reconfigure at same time. */
    
    //sentinel failover-timeout <master-name> <milliseconds>選項(xiàng)的值
    //刷新故障遷移狀態(tài)的最大時(shí)限
    mstime_t failover_timeout;      /* Max time to refresh failover state. */
}

例如啟動(dòng)Sentinel時(shí),配置了如下的配置文件:

# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor master1 127.0.0.1 6379 2

# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds master1 30000

# sentinel parallel-syncs <master-name> <numreplicas>
sentinel parallel-syncs master1 1

# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout master1 900000

則Sentinel則會(huì)為主服務(wù)器master1創(chuàng)建如下圖所示的實(shí)例結(jié)構(gòu):

Redis中sentinel故障轉(zhuǎn)移的示例分析

Sentinel狀態(tài)以及masters字典的機(jī)構(gòu)如下:

Redis中sentinel故障轉(zhuǎn)移的示例分析

創(chuàng)建連向主服務(wù)器的網(wǎng)絡(luò)連接

創(chuàng)建連向被監(jiān)視主服務(wù)器的網(wǎng)絡(luò)連接,Sentinel將成為主服務(wù)器的客戶(hù)端,向主服務(wù)器發(fā)送命令并從命令回復(fù)獲取信息。

Sentinel會(huì)創(chuàng)建兩個(gè)連向主服務(wù)器的異步網(wǎng)絡(luò)連接:

  • 命令連接,用于向主服務(wù)器發(fā)送命令并接收命令回復(fù)

  • 訂閱連接,訂閱主服務(wù)器的_sentinel_:hello頻道

Redis中sentinel故障轉(zhuǎn)移的示例分析

Sentinel發(fā)送信息和獲取信息

  • Sentinel默認(rèn)會(huì)以每十秒一次的頻率,通過(guò)命令連接向被監(jiān)視的master和slave發(fā)送INFO命令。

    通過(guò)master的回復(fù)可獲取master本身信息,包括run_id域記錄的服務(wù)器運(yùn)行ID,以及role域記錄的服務(wù)器角色。另外還會(huì)獲取到master下的所有的從服務(wù)器信息,包括slave的ip地址和port端口號(hào)。Sentinel無(wú)需用戶(hù)提供從服務(wù)器的地址信息,由master返回的slave的ip地址和port端口號(hào),可以自動(dòng)發(fā)現(xiàn)slave。

    當(dāng)Sentinel發(fā)現(xiàn)master有新的slave出現(xiàn)時(shí),Sentinel會(huì)為這個(gè)新的slave創(chuàng)建相應(yīng)的實(shí)例外,Sentinel還會(huì)創(chuàng)建到slave的命令連接和訂閱連接。

    根據(jù)slave的INFO命令的回復(fù),Sentinel會(huì)提取如下信息:

    1.slave的運(yùn)行ID run_id

    2.slave的角色role

    3.master的ip地址和port端口

    4.master和slave的連接狀態(tài)master_link_status

    5.slave的優(yōu)先級(jí)slave_priority

    6.slave的復(fù)制偏移量slave_repl_offset

  • Sentinel在默認(rèn)情況下會(huì)以每?jī)擅胍淮蔚念l率,通過(guò)命令連接向所有被監(jiān)視的master和slave的_sentinel_:hello頻道發(fā)送一條信息

    發(fā)送以下格式的命令:

     PUBLISH _sentinel_:hello   "<s_ip>,<s_port>,<s_runid>,<s_epoch>,<m_name>,<m_ip>,<m_port>,<m_epoch>"

以上命令相關(guān)參數(shù)意義:

參數(shù)意義
s_ipSentinel的ip地址
s_portSentinel的端口號(hào)
s_runidSentinel的運(yùn)行ID
s_runidSentinel的運(yùn)行ID
m_name主服務(wù)器的名字
m_ip主服務(wù)器的IP地址
m_port主服務(wù)器的端口號(hào)
m_epoch主服務(wù)器當(dāng)前的配置紀(jì)元
  • Sentinel與master或者slave建立訂閱連接之后,Sentinel就會(huì)通過(guò)訂閱連接發(fā)送對(duì)_sentinel_:hello頻道的訂閱,訂閱會(huì)持續(xù)到Sentinel與服務(wù)器的連接斷開(kāi)為止

命令如下所示:

SUBSCRIBE sentinel:hello

Redis中sentinel故障轉(zhuǎn)移的示例分析

如上圖所示,對(duì)于每個(gè)與Sentinel連接的服務(wù)器 ,Sentinel既可以通過(guò)命令連接向服務(wù)器頻道_sentinel_:hello頻道發(fā)送信息,又通過(guò)訂閱連接從服務(wù)器的_sentinel_:hello頻道接收信息。

  • sentinel間會(huì)相互感知,新加入的sentinel會(huì)向master的_sentinel_:hello頻道發(fā)布一條消息,包括自己的消息,其它該頻道訂閱者sentinel會(huì)發(fā)現(xiàn)新的sentinel。隨后新的sentinel和其它sentinel會(huì)創(chuàng)建長(zhǎng)連接。

相互連接的各個(gè)Sentinel可以進(jìn)行信息交換。Sentinel為master創(chuàng)建的實(shí)例結(jié)構(gòu)中的sentinels字典保存了除Sentinel本身之外,所有同樣監(jiān)視這個(gè)主服務(wù)器的其它Sentinel信息。

前面也講到sentinel會(huì)為slave創(chuàng)建實(shí)例(在master實(shí)例的slaves字典中)?,F(xiàn)在我們也知道通過(guò)sentinel相互信息交換,也創(chuàng)建了其它sentinel的實(shí)例(在master實(shí)例的sentinels字典中)。我們將一個(gè)sentinel中保存的實(shí)例結(jié)構(gòu)大概情況理一下,如下圖所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

從上圖可以看到slave和sentinel字典的鍵由其ip地址和port端口組成,格式為ip:port,其字典的值為其對(duì)應(yīng)的sentinelRedisInstance實(shí)例。

master的故障發(fā)現(xiàn)

主觀(guān)不可用

默認(rèn)情況下Sentinel會(huì)以每秒一次的頻率向所有與它創(chuàng)建了命令連接的master(包括master、slave、其它Sentinel)發(fā)送PING命令,并通過(guò)實(shí)例返回的PING命令回復(fù)來(lái)判斷實(shí)例是否在線(xiàn)。

PING命令回復(fù)分為下面兩種情況:

  • 有效回復(fù):實(shí)例返回 +PONG、-LOADING、-MASTERDOWN三種回復(fù)的一種

  • 無(wú)效回復(fù):除上面有效回復(fù)外的其它回復(fù)或者在指定時(shí)限內(nèi)沒(méi)有任何返回

Sentinel配置文件中的設(shè)置down-after-milliseconds毫秒時(shí)效內(nèi)(各個(gè)sentinel可能配置的不相同),連續(xù)向Sentinel返回?zé)o效回復(fù),那么sentinel將此實(shí)例置為主觀(guān)下線(xiàn)狀態(tài),在sentinel中維護(hù)的該實(shí)例flags屬性中打開(kāi)SRI_S_DOWN標(biāo)識(shí),例如master如下所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

客觀(guān)不可用

在sentinel發(fā)現(xiàn)主觀(guān)不可用狀態(tài)后,它會(huì)將“主觀(guān)不可用狀態(tài)”發(fā)給其它sentinel進(jìn)行確認(rèn),當(dāng)確認(rèn)的sentinel節(jié)點(diǎn)數(shù)>=quorum,則判定該master為客觀(guān)不可用,隨后進(jìn)入failover流程。

上面說(shuō)到將主觀(guān)不可用狀態(tài)發(fā)給其它sentinel使用如下命令:

SENTINEL is-master-down-by-addr <ip> <port> <current_epoch> <runid>

各個(gè)參數(shù)的意義如下:

  • ip:被sentinel判斷為主觀(guān)下線(xiàn)的主服務(wù)器的ip地址

  • port: 被sentinel判斷為主觀(guān)下線(xiàn)的主服務(wù)器的port地址

  • current_epoch:sentinel的配置紀(jì)元,用于選舉領(lǐng)頭Sentinel

  • runid:可以為*號(hào)或者Sentinel的運(yùn)行ID,*號(hào)代表檢測(cè)主服務(wù)器客觀(guān)下線(xiàn)狀態(tài)。Sentinel的運(yùn)行ID用于選舉領(lǐng)頭Sentinel

接受到以上命令的sentinel會(huì)反回一條包含三個(gè)參數(shù)的Multi Bulk回復(fù)

1)<down_state> 目標(biāo)sentinel對(duì)該master檢查結(jié)果,1:master已下線(xiàn) 2:master未下線(xiàn)

2)<leader_runid> 兩種情況,*表示僅用于檢測(cè)master下線(xiàn)狀態(tài) ,否則表示局部領(lǐng)頭Sentinel的運(yùn)行ID(選舉領(lǐng)頭Sentinel)

3)<leader_epoch> 當(dāng)leader_runid為時(shí),leader_epoch始終為0。不為時(shí)則表示目標(biāo)Sentinel的局部領(lǐng)頭Sentinel的配置紀(jì)元(用于選舉領(lǐng)頭Sentinel)

其中節(jié)點(diǎn)數(shù)量限制quorum為sentinel配置文件中配置的

sentinel monitor <master-name> <ip> <redis-port> <quorum>

quorum選項(xiàng),不同的sentinel配置的可能不相同。

當(dāng)sentinel認(rèn)為master為客觀(guān)下線(xiàn)狀態(tài),則會(huì)將master屬性中的flags的SRI_O_DOWN標(biāo)識(shí)打開(kāi),例如master如下圖所示:

Redis中sentinel故障轉(zhuǎn)移的示例分析

選舉Sentinel Leader

當(dāng)一臺(tái)master宕機(jī)時(shí),可能多個(gè)sentinel節(jié)點(diǎn)同時(shí)發(fā)現(xiàn)并通過(guò)交互確認(rèn)相互的“主觀(guān)不可用狀態(tài)”,同時(shí)達(dá)到“客觀(guān)不可用狀態(tài)”,同時(shí)打算發(fā)起failover。但最終只能有一個(gè)sentinel節(jié)點(diǎn)作為failover發(fā)起者,那么就需要選舉出Sentinel Leader,需要開(kāi)始一個(gè)Sentinel Leader選舉過(guò)程。

Redis的Sentinel機(jī)制采用類(lèi)似于Raft協(xié)議實(shí)現(xiàn)這個(gè)選舉算法:

1.sentinelState的epoch變量類(lèi)似于raft協(xié)議中的term(選舉回合)。

2.每一個(gè)確認(rèn)了master“客觀(guān)不可用”的sentinel節(jié)點(diǎn)都會(huì)向周?chē)鷱V播自己的參選請(qǐng)求(SENTINEL is-master-down-by-addr <ip> <port> <current_epoch> <run_id> ,current_epoch為自己的配置紀(jì)元,run_id為自己的運(yùn)行ID)

3.每一個(gè)接收到參選請(qǐng)求的sentinel節(jié)點(diǎn)如果還沒(méi)接收到其它參選請(qǐng)求,它就將本回合的意向置為首個(gè)參選sentinel并回復(fù)它(先到先得);如果已經(jīng)在本回合表過(guò)意向了,則拒絕其它參選,并將已有意向回復(fù)(如上所介紹的三個(gè)參數(shù)的Multi Bulk回復(fù),down_state為1,leader_runid為首次接收到的發(fā)起參選請(qǐng)求的源sentinel的運(yùn)行ID,leader_epoch為首次接收到的發(fā)起參選請(qǐng)求的源sentinel的配置紀(jì)元)

4.每個(gè)發(fā)起參選請(qǐng)求的sentinel節(jié)點(diǎn)如果收到超過(guò)一半的意向同意某個(gè)參選sentinel(可能是自己),則確定該sentinel為leader。如果本回合持續(xù)了足夠長(zhǎng)時(shí)間未選出leader,則開(kāi)啟下一個(gè)回合

leader sentinel 確定之后,leader sentinel從master所有的slave中依據(jù)一定規(guī)則選取一個(gè)作為新的master。

故障轉(zhuǎn)移failover

在選舉出Sentinel Leader之后,sentinel leader對(duì)已下線(xiàn)master執(zhí)行故障轉(zhuǎn)移:

  • sentinel leader對(duì)已下線(xiàn)的master的所有slave中,選出一個(gè)狀態(tài)良好、數(shù)據(jù)完整的slave,然后向這個(gè)slave發(fā)送:SLAVEOF no one 命令,將這個(gè)slave轉(zhuǎn)換為master。

    我們來(lái)看下新的master是怎么挑選出來(lái)的?Sentinel leader會(huì)將已下線(xiàn)的所有slave保存到一個(gè)列表,然后按照以下規(guī)則過(guò)濾篩選:

  • 優(yōu)先級(jí)最高的slave,redis.conf配置中replica-priority選項(xiàng)來(lái)標(biāo)識(shí),默認(rèn)為100,replica-priority較低的優(yōu)先級(jí)越高。0為特殊優(yōu)先級(jí),標(biāo)志為不能升級(jí)為master。

  • 如果存在多個(gè)優(yōu)先級(jí)相等的slave,則會(huì)選擇復(fù)制偏移量(offset)最大的slave(數(shù)據(jù)更加完整)

  • 如果存在多個(gè)優(yōu)先級(jí)相等,最大復(fù)制偏移量最大的slave,則選擇運(yùn)行ID最小的slave

選出需要升級(jí)為新的master的slave后,Sentinel Leader會(huì)向這個(gè)slave發(fā)送SLAVEOF no one 命令。之后Sentinel會(huì)以每秒一次頻率(平時(shí)是十秒一次)向被升級(jí)slave發(fā)送INFO,當(dāng)回復(fù)的role由slave變?yōu)閙aster時(shí)Sentinel Leader就會(huì)知道已升級(jí)為master。

  • sentinel leader 向已下線(xiàn)的master屬下的slave發(fā)送SLAVEOF命令(SLAVEOF <new_master_ip> <new_master_port>),去復(fù)制新的master。

  • 將舊的master設(shè)置為新的master的slave,并繼續(xù)對(duì)其監(jiān)視,當(dāng)其重新上線(xiàn)時(shí)Sentinel會(huì)執(zhí)行命令讓其成為新的master的slave。

以上是“Redis中sentinel故障轉(zhuǎn)移的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI