设为首页 收藏本站
查看: 946|回复: 0

[经验分享] redis sentinel模式下,如何选举新的master

[复制链接]

尚未签到

发表于 2016-12-20 11:38:04 | 显示全部楼层 |阅读模式
  首先要清楚,sentinel是一个独立于redis之外的进程,不对外提供key/value服务。在redis的安装目录下名称叫redis-sentinel主要用来监控redis-server进程,进行master/slave管理,如果你的redis没有运行在master/slave模式下,不需要设置sentinel。
 
两个基本概念

  • S_DOWN:subjectively down,直接翻译的为"主观"失效,即当前sentinel实例认为某个redis服务为"不可用"状态.
  • O_DOWN:objectively down,直接翻译为"客观"失效,即多个sentinel实例都认为master处于"SDOWN"状态,那么此时master将处于ODOWN,ODOWN可以简单理解为master已经被集群确定为"不可用",将会开启failover.
redis2.8.7的选举有两个条件,首先是要下面的条件过滤掉一些节点

一、使用如下条件筛选备选node:
1、slave节点状态处于S_DOWN,O_DOWN,DISCONNECTED的除外
2、最近一次ping应答时间不超过5倍ping的间隔(假如ping的间隔为1秒,则最近一次应答延迟不应超过5秒,redis sentinel默认为1秒)
3、info_refresh应答不超过3倍info_refresh的间隔(原理同2,redis sentinel默认为10秒)
4、slave节点与master节点失去联系的时间不能超过( (now - master->s_down_since_time) + (master->down_after_period * 10))。总体意思是说,slave节点与master同步太不及时的(比如新启动的节点),不应该参与被选举。
5、Slave priority不等于0(这个是在配置文件中指定,默认配置为100)。
 
二、从备选node中,按照如下顺序选择新的master
1、较低的slave_priority(这个是在配置文件中指定,默认配置为100)
2、较大的replication offset(每个slave在与master同步后offset自动增加)
3、较小的runid(每个redis实例,都会有一个runid,通常是一个40位的随机字符串,在redis启动时设置,重复概率非常小)
4、如果以上条件都不足以区别出唯一的节点,则会看哪个slave节点处理之前master发送的command多,就选谁。
 
附原英文:
Select a suitable slave to promote. The current algorithm only uses
the following parameters:
 
1) None of the following conditions: S_DOWN, O_DOWN, DISCONNECTED.
2) Last time the slave replied to ping no more than 5 times the PING period.
3) info_refresh not older than 3 times the INFO refresh period.
4) master_link_down_time no more than:
    (now - master->s_down_since_time) + (master->down_after_period * 10).
   Basically since the master is down from our POV, the slave reports
   to be disconnected no more than 10 times the configured down-after-period.
   This is pretty much black magic but the idea is, the master was not
   available so the slave may be lagging, but not over a certain time.
   Anyway we'll select the best slave according to replication offset.
5) Slave priority can't be zero, otherwise the slave is discarded.
 
Among all the slaves matching the above conditions we select the slave
with, in order of sorting key:
 
- lower slave_priority
- bigger processed replication offset.
- lexicographically smaller runid.
 
Basically if runid is the same, the slave that processed more commands
from the master is selected.
 
runid
 Redis "Run ID", a SHA1-sized random number that identifies a
 given execution of Redis, so that if you are talking with an instance
  having run_id == A, and you reconnect and it has run_id == B, you can be
 sure that it is either a different instance or it was restarte
util.c
void getRandomHexChars(char *p, unsigned int len);
 
对这类话题感兴趣?欢迎发送邮件至donlianli@126.com
关于我:邯郸人,软件工程师
 请支持原创:
http://donlianli.iyunv.com/blog/2034830

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-316967-1-1.html 上篇帖子: 数据库学习——关于redis的一点笔记 下篇帖子: 高性能网站架构设计之缓存篇(1)- Redis的安装与使用(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表