您的位置:首页 > 数据库 > Redis

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

2019-11-17 22:19 2691 查看

一、S_DOWN和O_DOWN

  S_DOWN和O_DOWN两种宕机状态

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

# 语法: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 数量的哨兵同意的话就变成了
# 客观宕机

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

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的监控配置,互相进行监控配置的同步。

三、slave配置的自动纠正

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

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

如果一个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

五、quorum和majority

1、每次一个哨兵要做主备切换,首先需要quorum数量的哨兵认为O_DOWN,然后选举出一个哨兵来做切换,这个哨兵还得得到majority哨兵的授权,才能正式执行切换 
2、如果quorum < majority,比如5个哨兵,majority就是3,quorum设置为2,那么就3个哨兵授权就可以执行切换,但是如果quorum >= majority,那么必须quorum数量的哨兵都授权,比如5个哨兵,quorum是5,那么必须5个哨兵都同意授权,才能执行切换

六、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配置的

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: