redis哨兵模式原理
2020-11-06 11:20:15 34 举报
redis sentinel模式图解
作者其他创作
大纲/内容
(3)新建连接
sentinel 4epoch 1
down-after-ms时间内没有正常回复
leader=s1
为了保证数据一致当slave断线重连时,slave会主动发送同步请求
leader
AskOther(force)
主从模式下命令执行过程master执行set命令后,立即响应client命令传播到slave是异步的如果命令传播未完成,访问slave时会出现不一致(3)
SDOWN
s3:m1
TILT
follower
DATA
slavem1(ip:port)
master1
(4)new config_epoch
pong
(4)TRUE
选出领头sentinel之后,由s1来执行选择最优从节点作为新的主节点,从节点必须是正常在线的,优先级高,复制偏移量大,runid小的优先,这里选择了slave 1
serverCron
ping获得响应后发送listen_portmaster保存从服务器信息
根据master状态执行操作:
epoch = 1
确认master下线
sentinel之间有没有publish命令?
sentinel 2
failover第二步:选择新master
代码:发布hello消息sentinelSendHello()处理hello消息sentinelProcessHelloMessage()
(2) is-master-down-by-addr
follower发现命令中的epoch高于自己和master会更新m1的epoch和leader返回信息表示已经投票给s1
(3)
(1) PUBLISH:s3:m1
sentinel 1
SET A 100(1)
WAIT_START
(2)更新信息
slave2
port
slave
让其他slave更新master
expired
sentinel3
AOF
(1)无效回复
redis
SAVE
only master
COMMAND
slave1
REPLCONF ACK
(1)UPDATE
获取Leader
m1 SDOWN(主观下线)m1ODOWN(客观下线)
backlog buffer
整体视图
new config_epoch
Client
connect
FailoverStateMachine
sentinel主要配置
master1 __sentinel__:hello
sentinel__:hello
sentinel 2epoch 0
附录:redis主从之间的交互
update
sentinel 3epoch 0
选择slave
代码:执行包括状态机在内的任务sentinelHandleRedisInstance( )主观下线检查sentinelCheckSubjectivelyDown( )发送is_master_down消息sentinelAskMasterStateToOtherSentinels( )收到is_master_down消息的响应sentinelReceiveIsMasterDownReply( )
leader epoch 1leader s1
代码:处理整个failover的状态机sentinelFailoverStateMachine执行包括状态机在内的任务sentinelHandleRedisInstance()统计票数sentinelGetLeader()投票sentinelVoteLeader()收到is_master_down命令的响应过程sentinelCommand()
sentinelTimer
SendPeriodicCommands
代码:sentinel处理收到的消息:sentinelProcessHelloMessage( )发现新master产生,发布消息sentinelForceHelloUpdateForMaster()
SEND_SLAVEOF_NOONE
PING
.....
sentinel
sentinel 3
ODOWN
(1)返回INFO
sentinelHandleRedisInstance
SELECT_SLAVE
s3: m1
(3) SLAVE OF slave1
master
RECONF_SLAVES
(3)connection
slave发送心跳消息 一秒一次m1发现offset不一致会补发命令及时发现故障
epoch+1
(2) MSG s3:m1
WAIT_PROMOTION
提拔slave
m1(e0)epoch 1id s1
(3)PING
发现新的redis实例
Scripts
实线表示命令连接
等slave变成master
SET A 100
StartFailoverIfNeeded
完整failover状态/* Failover machine different states. */#define SENTINEL_FAILOVER_STATE_NONE 0 /* No failover in progress. */#define SENTINEL_FAILOVER_STATE_WAIT_START 1 /* Wait for failover_start_time*/#define SENTINEL_FAILOVER_STATE_SELECT_SLAVE 2 /* Select slave to promote */#define SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE 3 /* Slave - Master */#define SENTINEL_FAILOVER_STATE_WAIT_PROMOTION 4 /* Wait slave to change role */#define SENTINEL_FAILOVER_STATE_RECONF_SLAVES 5 /* SLAVEOF newmaster */#define SENTINEL_FAILOVER_STATE_UPDATE_CONFIG 6 /* Monitor promoted slave. */
get log
PSYNC myid offset
(2)update
GET N(3)
bgsave
ping
异步执行
(5)下线QUORUM
__sentinel__:hello
TRUE
补发 commmands..
执行slave of m1 命令保存master信息&建立连接
(2)OK
(2) info master
m1 ip:ports1 ip:port+s2 ip:port
Reconnect
UPDATE
(1) SLAVE OF no one
m1(e1)epoch 1id s4
failover第一步:选举sentinel
AskOther
sentinel 1epoch 1
发现新的sentinel实例
检查offset
master2
0 条评论
回复 删除
下一页