Seven core mechanism underlying principle of Redis

One, S_DOWN sum O_DOWN

  S_DOWN and O_DOWN two kinds downed state

(1), S_DOWN downtime is subjective, it is a sentinel if they feel that a master is down, so is the subjective condition reached down <br> sdown is simple, if a sentry ping a master, than is-master- after down-after-milliseconds specified number of milliseconds, down to subjective view 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 objective downtime, if the quorum number of sentinel feel a master is down, so is the objective of downtime

 Note: S_DOWN to O_DOWN conversion condition is very simple, if a sentinel within the specified time, received a specified number of quorum other guards also believes that the master is S_DOWN, then it is considered to be a 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 数量的哨兵同意的话就变成了
# 客观宕机

Second, the automatic discovery mechanism sentry Clusters

1, Sentinel found each other, is achieved by redis the pub / sub systems, each Sentinel will go Sentinel : the Hello this channel. Send a message, this time can be consumed all the other guards to the news, and perceived There are other sentinels.
2, every two seconds, each in his own guards will monitor a master + slaves corresponding Sentinel : the Hello Channel in sending a message that says your host, ip and runid also monitor the master configuration, each sentinel surveillance will go to monitor their own master + slaves each corresponding Sentinel : the Hello Channel, then go to the same perception in listening to this master + slaves of the existence of other sentinel. 
3, each sentinel surveillance will be exchanged with other configuration of the master of sentinel surveillance configuration for synchronization with each other.

Three, slave configuration is automatically corrected

  Sentinel will be responsible for automatically correct some slave configuration, such as slave if you want to become potential candidates for master, slave Sentinel will ensure that the existing master copy of the data; if after the slave connected to a wrong master, such as failover, the Sentinel It will ensure that they are connected to the correct master

Fourth, the slave becomes master election algorithm

If a master is considered O_DOWN, and it allows the majority sentry standby switch, then a sentry will perform the switchover operation, this time to elect a slave to first consider some of the information slave of:  
(1) with the master long disconnected
(2) slave priority
(3) copy offset
(. 4) RUN ID

If a slave with master disconnect the connection has more than 10-fold down-after-milliseconds, plus the length of master goes down, then the slave was not considered suitable for the master election

(down-after-milliseconds * 10) + milliseconds_since_master_is_in_SDOWN_state

Next will be sorted slave

(1) sorted by priority slave, the lower the replica-priority, the higher the priority, the following is the explanation of English:

# 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) If the slave priority are the same, look replica offset, which is more of a slave to copy data, after offset by the more, the higher the priority
(3) If the above two conditions are identical, then select a relatively small run id that slave

Five, quorum and majority

1, each time a sentinel to do the switchover, first quorum required number of Sentinel think O_DOWN, then elected to do a sentinel switch, the scout had to be authorized by majority Sentinel in order to perform the official switch 
2, if the quorum <majority, example 5 sentinel, Majority is. 3, quorum is set to 2, then the three sentinel authorization handover can be executed, but if quorum> = majority, must be quorum number sentinels are authorized, such as 5 sentinel, quorum is 5, it must have agreed to authorize five guards to perform switching

六、configuration epoch

  Sentinel have a redis master + slave monitor, a corresponding configuration of the monitor

1, the sentinel performing handover will get from switching to a new master (salve-> master) a configuration epoch, which is a number version, version number every time you switch must be unique.
2. If the first elected Sentinel switch fails, then the other sentinel, waits for failover-timeout period, then proceed to take over the switch at this time will retrieve a new configuration epoch, as the new version number.

7, configuraiton spread

1, after the handover is completed Sentinel will update the latest generation in its own local master configuration, and then sync to other Sentinel is said before by the pub / sub messaging mechanism  
2, version number is very important, because all kinds of messages are after passing through a channel to publish and monitor, so a sentry to complete a new switch, the new master configuration is followed by a new version number
3, the other guards to update their master is configured according to the size of the version number

Guess you like

Origin blog.51cto.com/14230003/2451073