1、 运行时必须指定配置文件
2、 替换默认端口,新的默认端口为26379
3、 替换为新的可接受的命令集,参照《命令集》部分,创建sentinel.master字典,用来存放监视的master,默认tilt模式为关闭。创建脚本执行队列sentinel.scripts_queue
4、 加载配置文件(支持相对路径,绝对路径)
除去共有的配置项,还有sentinel专有配置项
sentinel monitor <name> <host> <port> <quorum>
i. 为sentinel 创建master实例
sentinel down-after-milliseconds <name> <milliseconds>
i. 设置该master 的主观下线时间,server.down_after_period
sentinel failover-timeout <name> <milliseconds>
sentinel parallel-syncs <name> <milliseconds>
i. 多少个slave可以同时reconfigure
sentinel notification-script <name> <path>
i. 设置该master的通知脚本
sentinel client-reconfig-script <name> <path>
i. 设置该master的reconfig脚本
sentinel auth-pass <name> <password>
i. 设置认证密码
sentinel current-epoch <epoch>
i. 设置sentinel的 current_epoch
sentinel config-epoch <name> <epoch>
i. 设置master config_epoch
Sentinel leader-epoch <name> <epoch>
i. 设置master的leader_epoch
Sentinel known-slave <name> <ip> <port> [runid]
i. 设置master知道的slave,其中name为master名,ip:port为salve地址
known-sentinel <name> <ip> <port> [runid]
i. 设置master知道的sentinel,其中name为master名,ip:port为sentinel地址
Sentinel announce-ip <ip-address>
Sentinel announce-port <port>
5、 初始化服务器,创建侦听,创建时间事件(serverCron),文件事件等。对于时间事件,事件处理函数返回-1表示一次性时间事件,返回其他的值,该值为下一次触发时间间隔。
6、 aeMain 轮询事件
serverCron时间事件中的sentinelTimer有以下处理
i. 检验是否使得sentinel进入tilt模式,当上次检验和这次检验时间差为负值或者大于SENTINEL_TILT_TRIGGER(2000ms)阀值,sentinel进入tilt模式。时间差为负值可能是由于被修改了系统时间,大于SENTINEL_TILT_TRIGGER可能是遇到高负载操作,进入tilt模式,并向给订阅了+tilt的client发送信息
ii. 断开的master,尝试重连,
创建命令连接,是异步模式
a) 设置连接回调函数
b) 设置断开连接回调函数
c) 发送是auth认证,如果需要的话
d) 设置client名(sentinel+runnid前八位+cmd)
e) 异步发送ping消息
创建pub/sub连接,异步模式
a) 设置连接回调函数
b) 设置端口连接回调函数
c) 发送auth认证,如果需要的话
d) 设置client名(sentinel+runnid前八位+pubsub)
e) 订阅master的__sentinel__:hello频道
i. 异步处理订阅__sentinel__:hello的函数逻辑
格式:0=ip,1=port,2=runid,3=current_epoch,4=master_name, 5=master_ip,6=master_port,7=master_config_epoch
0,1,2是该master的sentinel 的ip、port和runnid
添加该master的sentinel到该sentinel 关注的实例当中
当参数3大于sentinel.current_epoch, 更新sentinel.current_epoch为参数3的值并发送消息给侦听了+new-epoch消息的客户端
当参数7大于当前master的config_epoch,更新为大的,