Redis的七个核心机制底层原理

1、S_DOWN和O_DOWN

  S_DOWN和O_DOWN两种宕机状态

(1)、S_DOWN是主观宕机,就一个哨兵若是本身以为一个master宕机了,那么就是主观宕机<br> sdown达成的条件很简单,若是一个哨兵ping一个master,超过了is-master-down-after-milliseconds指定的毫秒数以后,就主观认为master宕机<br>redis

# 语法: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).
# 意思是任何从节点或者哨兵在指定的时间内,不能ping通主机就被认为成S_DOWN

(2)、O_DOWN是客观宕机,若是quorum数量的哨兵都以为一个master宕机了,那么就是客观宕机算法

 注意:S_DOWN到O_DOWN转换的条件很简单,若是一个哨兵在指定时间内,收到了quorum指定数量的其余哨兵也认为那个master是S_DOWN了,那么就认为是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监视这个master,若是至少quorum 数量的哨兵赞成的话就变成了
# 客观宕机

2、哨兵集群的自动发现机制

一、哨兵互相之间的发现,是经过redis的pub/sub系统实现的,每一个哨兵都会往sentinel:hello这个channel里发送一个消息,这时候全部其余哨兵均可以消费到这个消息,并感知到其余的哨兵的存在。
二、每隔两秒钟,每一个哨兵都会往本身监控的某个master+slaves对应的sentinel:hello channel里发送一个消息,内容是本身的host、ip和runid还有对这个master的监控配置,每一个哨兵也会去监听本身监控的每一个master+slaves对应的sentinel:hello channel,而后去感知到一样在监听这个master+slaves的其余哨兵的存在。 
三、每一个哨兵还会跟其余哨兵交换对master的监控配置,互相进行监控配置的同步。ide

3、slave配置的自动纠正

  哨兵会负责自动纠正slave的一些配置,好比slave若是要成为潜在的master候选人,哨兵会确保slave在复制现有master的数据; 若是slave链接到了一个错误的master上,好比故障转移以后,那么哨兵会确保它们链接到正确的master上this

4、从机变主机的选举算法

若是一个master被认为O_DOWN了,并且majority哨兵都容许了主备切换,那么某个哨兵就会执行主备切换操做,此时首先要选举一个slave来会考虑slave的一些信息:  
(1)跟master断开链接的时长
(2)slave优先级
(3)复制offset
(4)run idcode

若是一个slave跟master断开链接已经超过了down-after-milliseconds的10倍,外加master宕机的时长,那么slave就被认为不适合选举为masterorm

(down-after-milliseconds * 10) + milliseconds_since_master_is_in_SDOWN_state

接下来会对slave进行排序排序

(1)按照slave优先级进行排序,replica-priority越低,优先级就越高,下面的英文就是这个的解释:three

# 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比较小的那个slaveip

5、quorum和majority

一、每次一个哨兵要作主备切换,首先须要quorum数量的哨兵认为O_DOWN,而后选举出一个哨兵来作切换,这个哨兵还得获得majority哨兵的受权,才能正式执行切换 
二、若是quorum < majority,好比5个哨兵,majority就是3,quorum设置为2,那么就3个哨兵受权就能够执行切换,可是若是quorum >= majority,那么必须quorum数量的哨兵都受权,好比5个哨兵,quorum是5,那么必须5个哨兵都赞成受权,才能执行切换ci

6、configuration epoch

  哨兵会对一套redis master+slave进行监控,有相应的监控的配置

一、执行切换的那个哨兵,会从要切换到的新master(salve->master)那里获得一个configuration epoch,这就是一个version号,每次切换的version号都必须是惟一的。
二、若是第一个选举出的哨兵切换失败了,那么其余哨兵,会等待failover-timeout时间,而后接替继续执行切换,此时会从新获取一个新的configuration epoch,做为新的version号。

七、configuraiton传播

一、哨兵完成切换以后,会在本身本地更新生成最新的master配置,而后同步给其余的哨兵,就是经过以前说的pub/sub消息机制  二、version号就很重要了,由于各类消息都是经过一个channel去发布和监听的,因此一个哨兵完成一次新的切换以后,新的master配置是跟着新的version号的三、其余的哨兵都是根据版本号的大小来更新本身的master配置的

相关文章
相关标签/搜索