16.1 启动并初始化Sentinel

初始化服务器

Sentinel本质上只是运行在特殊模式下的Redis服务器,启动第一步就是初始化一个普通的Redis服务器

使用Sentinel专用代码

使用redis.h/REDIS_SERVERPORT常量值作为服务器端口

使用redis.h/redisCommandTable作为服务器的命令表

// 服务器在 sentinel 模式下可执行的命令
struct redisCommand sentinelcmds[] = {
{"ping",pingCommand,1,"",0,NULL,0,0,0,0,0},
{"sentinel",sentinelCommand,-2,"",0,NULL,0,0,0,0,0},
{"subscribe",subscribeCommand,-2,"",0,NULL,0,0,0,0,0},
{"unsubscribe",unsubscribeCommand,-1,"",0,NULL,0,0,0,0,0},
{"psubscribe",psubscribeCommand,-2,"",0,NULL,0,0,0,0,0},
{"punsubscribe",punsubscribeCommand,-1,"",0,NULL,0,0,0,0,0},
{"publish",sentinelPublishCommand,3,"",0,NULL,0,0,0,0,0},
{"info",sentinelInfoCommand,-1,"",0,NULL,0,0,0,0,0},
{"shutdown",shutdownCommand,-1,"",0,NULL,0,0,0,0,0}
}; struct redisCommand { // 命令名字
char *name; // 实现函数
redisCommandProc *proc; // 参数个数
int arity; // 字符串表示的 FLAG
char *sflags; /* Flags as string representation, one char per flag. */ // 实际 FLAG
int flags; /* The actual flags, obtained from the 'sflags' field. */ /* Use a function to determine keys arguments in a command line.
* Used for Redis Cluster redirect. */
// 从命令中判断命令的键参数。在 Redis 集群转向时使用。
redisGetKeysProc *getkeys_proc; /* What keys should be loaded in background when calling this command? */
// 指定哪些参数是 key
int firstkey; /* The first argument that's a key (0 = no keys) */
int lastkey; /* The last argument that's a key */
int keystep; /* The step between first and last key */ // 统计信息
// microseconds 记录了命令执行耗费的总毫微秒数
// calls 是命令被执行的总次数
long long microseconds, calls;
};

初始化Sentinel状态

/* Sentinel 的状态结构 */
struct sentinelState { // 当前纪元
uint64_t current_epoch; /* Current epoch. */ // 保存了所有被这个 sentinel 监视的主服务器
// 字典的键是主服务器的名字
// 字典的值则是一个指向 sentinelRedisInstance 结构的指针
dict *masters; /* Dictionary of master sentinelRedisInstances.
Key is the instance name, value is the
sentinelRedisInstance structure pointer. */ // 是否进入了 TILT 模式?
int tilt; /* Are we in TILT mode? */ // 目前正在执行的脚本的数量
int running_scripts; /* Number of scripts in execution right now. */ // 进入 TILT 模式的时间
mstime_t tilt_start_time; /* When TITL started. */ // 最后一次执行时间处理器的时间
mstime_t previous_time; /* Last time we ran the time handler. */ // 一个 FIFO 队列,包含了所有需要执行的用户脚本
list *scripts_queue; /* Queue of user scripts to execute. */ } sentinel;

初始化Sentinel状态的masters属性

Sentinel状态中的master字典记录了所有被Sentinel监视的主服务器的相关信息其中:

  • 字典的键是被监视主服务器的名字
  • 而字典的值是被监视主服务器对应的sentinel.h/sentinelRedisInstance结构
/* A Sentinel Redis Instance object is monitoring. */
/* 每个被监视的 Redis 实例都会创建一个 sentinelRedisInstance 结构
* 而每个结构的 flags 值会是以下常量的一个或多个的并 */
// 实例是一个主服务器
#define SRI_MASTER (1<<0)
// 实例是一个从服务器
#define SRI_SLAVE (1<<1)
// 实例是一个 Sentinel
#define SRI_SENTINEL (1<<2)
// 实例已断线
#define SRI_DISCONNECTED (1<<3)
// 实例已处于 SDOWN 状态
#define SRI_S_DOWN (1<<4) /* Subjectively down (no quorum). */
// 实例已处于 ODOWN 状态
#define SRI_O_DOWN (1<<5) /* Objectively down (confirmed by others). */
// Sentinel 认为主服务器已下线
#define SRI_MASTER_DOWN (1<<6) /* A Sentinel with this flag set thinks that
its master is down. */
// 正在对主服务器进行故障迁移
#define SRI_FAILOVER_IN_PROGRESS (1<<7) /* Failover is in progress for
this master. */
// 实例是被选中的新主服务器(目前仍是从服务器)
#define SRI_PROMOTED (1<<8) /* Slave selected for promotion. */
// 向从服务器发送 SLAVEOF 命令,让它们转向复制新主服务器
#define SRI_RECONF_SENT (1<<9) /* SLAVEOF <newmaster> sent. */
// 从服务器正在与新主服务器进行同步
#define SRI_RECONF_INPROG (1<<10) /* Slave synchronization in progress. */
// 从服务器与新主服务器同步完毕,开始复制新主服务器
#define SRI_RECONF_DONE (1<<11) /* Slave synchronized with new master. */
// 对主服务器强制执行故障迁移操作
#define SRI_FORCE_FAILOVER (1<<12) /* Force failover with master up. */
// 已经对返回 -BUSY 的服务器发送 SCRIPT KILL 命令
#define SRI_SCRIPT_KILL_SENT (1<<13) /* SCRIPT KILL already sent on -BUSY */ // Sentinel 会为每个被监视的 Redis 实例创建相应的 sentinelRedisInstance 实例
// (被监视的实例可以是主服务器、从服务器、或者其他 Sentinel )
typedef struct sentinelRedisInstance { // 位图,标识值,记录了实例的类型,以及该实例的当前状态
int flags; /* See SRI_... defines */ // 实例的名字
// 主服务器的名字由用户在配置文件中设置
// 从服务器以及 Sentinel 的名字由 Sentinel 自动设置
// 格式为 ip:port ,例如 "127.0.0.1:26379"
char *name; /* Master name from the point of view of this sentinel. */ // 实例的运行 ID
char *runid; /* run ID of this instance. */ // 配置纪元,用于实现故障转移
uint64_t config_epoch; /* Configuration epoch. */ // 实例的地址
sentinelAddr *addr; /* Master host. */ // 用于发送命令的异步连接
redisAsyncContext *cc; /* Hiredis context for commands. */ // 用于执行 SUBSCRIBE 命令、接收频道信息的异步连接
// 仅在实例为主服务器时使用
redisAsyncContext *pc; /* Hiredis context for Pub / Sub. */ // 已发送但尚未回复的命令数量
int pending_commands; /* Number of commands sent waiting for a reply. */ // cc 连接的创建时间
mstime_t cc_conn_time; /* cc connection time. */ // pc 连接的创建时间
mstime_t pc_conn_time; /* pc connection time. */ // 最后一次从这个实例接收信息的时间
mstime_t pc_last_activity; /* Last time we received any message. */ // 实例最后一次返回正确的 PING 命令回复的时间
mstime_t last_avail_time; /* Last time the instance replied to ping with
a reply we consider valid. */
// 实例最后一次发送 PING 命令的时间
mstime_t last_ping_time; /* Last time a pending ping was sent in the
context of the current command connection
with the instance. 0 if still not sent or
if pong already received. */
// 实例最后一次返回 PING 命令的时间,无论内容正确与否
mstime_t last_pong_time; /* Last time the instance replied to ping,
whatever the reply was. That's used to check
if the link is idle and must be reconnected. */ // 最后一次向频道发送问候信息的时间
// 只在当前实例为 sentinel 时使用
mstime_t last_pub_time; /* Last time we sent hello via Pub/Sub. */ // 最后一次接收到这个 sentinel 发来的问候信息的时间
// 只在当前实例为 sentinel 时使用
mstime_t last_hello_time; /* Only used if SRI_SENTINEL is set. Last time
we received a hello from this Sentinel
via Pub/Sub. */ // 最后一次回复 SENTINEL is-master-down-by-addr 命令的时间
// 只在当前实例为 sentinel 时使用
mstime_t last_master_down_reply_time; /* Time of last reply to
SENTINEL is-master-down command. */ // 实例被判断为 SDOWN 状态的时间
mstime_t s_down_since_time; /* Subjectively down since time. */ // 实例被判断为 ODOWN 状态的时间
mstime_t o_down_since_time; /* Objectively down since time. */ // SENTINEL down-after-milliseconds 选项所设定的值
// 实例无响应多少毫秒之后才会被判断为主观下线(subjectively down)
mstime_t down_after_period; /* Consider it down after that period. */ // 从实例获取 INFO 命令的回复的时间
mstime_t info_refresh; /* Time at which we received INFO output from it. */ /* Role and the first time we observed it.
* This is useful in order to delay replacing what the instance reports
* with our own configuration. We need to always wait some time in order
* to give a chance to the leader to report the new configuration before
* we do silly things. */
// 实例的角色
int role_reported;
// 角色的更新时间
mstime_t role_reported_time; // 最后一次从服务器的主服务器地址变更的时间
mstime_t slave_conf_change_time; /* Last time slave master addr changed. */ /* Master specific. */
/* 主服务器实例特有的属性 -------------------------------------------------------------*/ // 其他同样监控这个主服务器的所有 sentinel
dict *sentinels; /* Other sentinels monitoring the same master. */ // 如果这个实例代表的是一个主服务器
// 那么这个字典保存着主服务器属下的从服务器
// 字典的键是从服务器的名字,字典的值是从服务器对应的 sentinelRedisInstance 结构
dict *slaves; /* Slaves for this master instance. */ // SENTINEL monitor <master-name> <IP> <port> <quorum> 选项中的 quorum 参数
// 判断这个实例为客观下线(objectively down)所需的支持投票数量
int quorum; /* Number of sentinels that need to agree on failure. */ // SENTINEL parallel-syncs <master-name> <number> 选项的值
// 在执行故障转移操作时,可以同时对新的主服务器进行同步的从服务器数量
int parallel_syncs; /* How many slaves to reconfigure at same time. */ // 连接主服务器和从服务器所需的密码
char *auth_pass; /* Password to use for AUTH against master & slaves. */ /* Slave specific. */
/* 从服务器实例特有的属性 -------------------------------------------------------------*/ // 主从服务器连接断开的时间
mstime_t master_link_down_time; /* Slave replication link down time. */ // 从服务器优先级
int slave_priority; /* Slave priority according to its INFO output. */ // 执行故障转移操作时,从服务器发送 SLAVEOF <new-master> 命令的时间
mstime_t slave_reconf_sent_time; /* Time at which we sent SLAVE OF <new> */ // 主服务器的实例(在本实例为从服务器时使用)
struct sentinelRedisInstance *master; /* Master instance if it's slave. */ // INFO 命令的回复中记录的主服务器 IP
char *slave_master_host; /* Master host as reported by INFO */ // INFO 命令的回复中记录的主服务器端口号
int slave_master_port; /* Master port as reported by INFO */ // INFO 命令的回复中记录的主从服务器连接状态
int slave_master_link_status; /* Master link status as reported by INFO */ // 从服务器的复制偏移量
unsigned long long slave_repl_offset; /* Slave replication offset. */ /* Failover */
/* 故障转移相关属性 -------------------------------------------------------------------*/ // 如果这是一个主服务器实例,那么 leader 将是负责进行故障转移的 Sentinel 的运行 ID 。
// 如果这是一个 Sentinel 实例,那么 leader 就是被选举出来的领头 Sentinel 。
// 这个域只在 Sentinel 实例的 flags 属性的 SRI_MASTER_DOWN 标志处于打开状态时才有效。
char *leader; /* If this is a master instance, this is the runid of
the Sentinel that should perform the failover. If
this is a Sentinel, this is the runid of the Sentinel
that this Sentinel voted as leader. */
// 领头的纪元
uint64_t leader_epoch; /* Epoch of the 'leader' field. */
// 当前执行中的故障转移的纪元
uint64_t failover_epoch; /* Epoch of the currently started failover. */
// 故障转移操作的当前状态
int failover_state; /* See SENTINEL_FAILOVER_STATE_* defines. */ // 状态改变的时间
mstime_t failover_state_change_time; // 最后一次进行故障迁移的时间
mstime_t failover_start_time; /* Last failover attempt start time. */ // SENTINEL failover-timeout <master-name> <ms> 选项的值
// 刷新故障迁移状态的最大时限
mstime_t failover_timeout; /* Max time to refresh failover state. */ mstime_t failover_delay_logged; /* For what failover_start_time value we
logged the failover delay. */
// 指向被提升为新主服务器的从服务器的指针
struct sentinelRedisInstance *promoted_slave; /* Promoted slave instance. */ /* Scripts executed to notify admin or reconfigure clients: when they
* are set to NULL no script is executed. */
// 一个文件路径,保存着 WARNING 级别的事件发生时执行的,
// 用于通知管理员的脚本的地址
char *notification_script; // 一个文件路径,保存着故障转移执行之前、之后、或者被中止时,
// 需要执行的脚本的地址
char *client_reconfig_script; } sentinelRedisInstance;

16.2 获取服务器信息

Sentinel默认会以每十秒一次的频率,通过命令连接向被监视的主服务器发送INFO命令,并通过分析INFO命令的回复来获取主服务器的当前信息

通过分析主服务器回复的信息,填充主服务器实例结构中的slaves字典,而字典的键为从服务器的名字(addr:port), 值为从服务器的实例结构,如果从服务器的值已经存在,那么更新从服务器实例结构

16.3 获取从服务器信息

在创建命令连接之后,Sentinel默认情况下会以每十秒一次的频率通过命令连接向从服务器发送INFO命令,并获得类似以下内容的回复:

16.4 向主服务器和从服务器发送信息

在默认情况下,Sentinel会以每2秒一次的频率,通过命令连接向所有被监视的主服务器和从服务器发送以下格式的命令:

PUBLISH __sentinel__:hello "<s_ip>, <s_port>, <s_runid>, <s_epoch>, <m_name>, <m_ip>, <m_prot>, <m_epoch>"

int sentinelSendHello(sentinelRedisInstance *ri) {
char ip[REDIS_IP_STR_LEN];
char payload[REDIS_IP_STR_LEN+1024];
int retval; // 如果实例是主服务器,那么使用此实例的信息
// 如果实例是从服务器,那么使用这个从服务器的主服务器的信息
sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ? ri : ri->master; // 获取地址信息
sentinelAddr *master_addr = sentinelGetCurrentMasterAddress(master); /* Try to obtain our own IP address. */
// 获取实例自身的地址
if (anetSockName(ri->cc->c.fd,ip,sizeof(ip),NULL) == -1) return REDIS_ERR;
if (ri->flags & SRI_DISCONNECTED) return REDIS_ERR; /* Format and send the Hello message. */
// 格式化信息
snprintf(payload,sizeof(payload),
"%s,%d,%s,%llu," /* Info about this sentinel. */
"%s,%s,%d,%llu", /* Info about current master. */
ip, server.port, server.runid,
(unsigned long long) sentinel.current_epoch,
/* --- */
master->name,master_addr->ip,master_addr->port,
(unsigned long long) master->config_epoch); // 发送信息
retval = redisAsyncCommand(ri->cc,
sentinelPublishReplyCallback, NULL, "PUBLISH %s %s",
SENTINEL_HELLO_CHANNEL,payload); if (retval != REDIS_OK) return REDIS_ERR; ri->pending_commands++; return REDIS_OK;
}

16.5 接收来自主服务器和从服务器的频道消息

当一个Sentinel从__sentinel__:hello频道收到一条信息时,Sentinel会对这条信息进行分析,提取出信息中的八个参数,根据这些参数对主服务器的实例结构进行更新

更新sentinals字典

最新文章

  1. 将程序部署到weblogic出现乱码问题
  2. L# ForUnity Helloworld的更新方法
  3. I535卡刷土豆修改4.1.2版本ROMV4過程
  4. HDU3996 Gold Mine(最大权闭合子图)
  5. BZOJ 3564 信号增幅仪
  6. create table xxx as select 与 create table xxx like
  7. 常用的js函数
  8. hackyviewpager有什么用
  9. Linux学习笔记(一):常用命令(2)
  10. LAMP优化
  11. 网络协议 5 - ICMP 与 ping:投石问路的侦察兵
  12. CSS居中的几种方式总结
  13. Idea设置行注释不显示在行首
  14. outlook2016用Exchange轻松绑定腾讯企业邮箱
  15. django 存在则忽略, 不存在则创 TagSheet.objects.get_or_create(tag=&#39;test&#39;)
  16. php常量 const 和 define
  17. 【模板】 ST表
  18. Django使用多个数据库
  19. 告诉你什么是javascript的回调函数
  20. FineUI 选中多行获取行ID

热门文章

  1. Spring学习过程中遇到的No bean named &#39;beanId&#39; is defined报错
  2. Gateway网关
  3. Oracle数据库配置监听程序
  4. django 内置“信号”机制和自定义方法
  5. 后端程序员之路 50、Go语言开发环境
  6. 后端程序员之路 26、CAP理论
  7. URL 地址解析
  8. AOP(面向切面编程)大概了解一下
  9. 如何自学成 Python 大神?这里有些建议
  10. 给出镜像FreeBSD 基本要求