Redis Sentinel 源码:Redis的高可用模型分析

**摘要:**本文通过对Redis Sentinel源码的理解,详细说明Sentinel的代码实现方式。

Redis Sentinel 是Redis提供的高可用模型解决方案。Sentinel可以自动监测一个或多个Redis主备实例,并在主实例宕机的情况下自动实行主备倒换。本文通过对Redis Sentinel源码的理解,详细说明Sentinel的代码实现方式。

Sentinel使用Redis内核相同的事件驱动代码框架, 但Sentinel有自己独特的初始化步骤。在这篇文章里,会从Sentinel的初始化、Sentinel主时间事件函数、Sentinel 网络连接和Tilt模式三部分进行讲解。

Sentinel初始化

我们可以通过redis-sentinel <path-to-configfile> 或者 redis-server <path-to-configfile> --sentinel 这两种方式启动并运行Sentinel实例,这两种方式是等价的。在Redis server.c 的main函数中,我们会看到Redis如何判断用户指定以Sentinel方式运行的逻辑:

1int main(int argc, char **argv) { 2 .......... 3 server.sentinel_mode = checkForSentinelMode(argc,argv); 4 .......... 5}

其中checkForSentinelMode函数会监测以下两种条件:

  1. 程序使用redis-sentinel可执行文件执行。
  2. 程序参数列表中有--sentinel 标志。

以上任何一种条件成立则Redis会使用Sentinel的方式运行。

1/* Returns 1 if there is --sentinel among the arguments or if 2 3 * argv[0] contains "redis-sentinel". */ 4 5int checkForSentinelMode(int argc, char **argv) { 6 7 int j; 8 9 10 11 if (strstr(argv[0],"redis-sentinel") != NULL) return 1; 12 13 for (j = 1; j < argc; j++) 14 15 if (!strcmp(argv[j],"--sentinel")) return 1; 16 17 return 0; 18 19}

在Redis 判断是否以Sentinel的方式运行以后,我们会看到如下代码段:

1int main(int argc, char **argv) { 2 struct timeval tv; 3 int j; 4 5 ............ 6/* We need to init sentinel right now as parsing the configuration file 7 * in sentinel mode will have the effect of populating the sentinel 8 * data structures with master nodes to monitor. */ 9 if (server.sentinel_mode) { 10 initSentinelConfig(); 11 initSentinel(); 12 } 13 ............

在initSentinelConfig函数中,会使用Sentinel特定的端口(默认为26379)来替代Redis的默认端口(6379)。另外,在Sentinel模式下,需要禁用服务器运行保护模式。

1/* This function overwrites a few normal Redis config default with Sentinel 2 * specific defaults. */ 3void initSentinelConfig(void) { 4 server.port = REDIS_SENTINEL_PORT; 5 server.protected_mode = 0; /* Sentinel must be exposed. */ 6}

与此同时,initSentinel函数会做如下操作:

1/* Perform the Sentinel mode initialization. */ 2void initSentinel(void) { 3 unsigned int j; 4 5 /* Remove usual Redis commands from the command table, then just add 6 * the SENTINEL command. */ 7 dictEmpty(server.commands,NULL); 8 for (j = 0; j < sizeof(sentinelcmds)/sizeof(sentinelcmds[0]); j++) { 9 int retval; 10 struct redisCommand *cmd = sentinelcmds+j; 11 12 retval = dictAdd(server.commands, sdsnew(cmd->name), cmd); 13 serverAssert(retval == DICT_OK); 14 15 /* Translate the command string flags description into an actual 16 * set of flags. */ 17 if (populateCommandTableParseFlags(cmd,cmd->sflags) == C_ERR) 18 serverPanic("Unsupported command flag"); 19 } 20 21 /* Initialize various data structures. */ 22 sentinel.current_epoch = 0; 23 sentinel.masters = dictCreate(&instancesDictType,NULL); 24 sentinel.tilt = 0; 25 sentinel.tilt_start_time = 0; 26 sentinel.previous_time = mstime(); 27 ............. 28}

1、使用Sentinel自带的命令表去替代Redis服务器原生的命令. Sentinel 支持的命令表如下:

1struct redisCommand sentinelcmds[] = { 2 {"ping",pingCommand,1,"",0,NULL,0,0,0,0,0}, 3 {"sentinel",sentinelCommand,-2,"",0,NULL,0,0,0,0,0}, 4 {"subscribe",subscribeCommand,-2,"",0,NULL,0,0,0,0,0}, 5 {"unsubscribe",unsubscribeCommand,-1,"",0,NULL,0,0,0,0,0}, 6 {"psubscribe",psubscribeCommand,-2,"",0,NULL,0,0,0,0,0}, 7 {"punsubscribe",punsubscribeCommand,-1,"",0,NULL,0,0,0,0,0}, 8 {"publish",sentinelPublishCommand,3,"",0,NULL,0,0,0,0,0}, 9 {"info",sentinelInfoCommand,-1,"",0,NULL,0,0,0,0,0}, 10 {"role",sentinelRoleCommand,1,"ok-loading",0,NULL,0,0,0,0,0}, 11 {"client",clientCommand,-2,"read-only no-script",0,NULL,0,0,0,0,0}, 12 {"shutdown",shutdownCommand,-1,"",0,NULL,0,0,0,0,0}, 13 {"auth",authCommand,2,"no-auth no-script ok-loading ok-stale fast",0,NULL,0,0,0,0,0}, 14 {"hello",helloCommand,-2,"no-auth no-script fast",0,NULL,0,0,0,0,0} 15};

2、初始化Sentinel主状态结构,Sentinel主状态的定义及注释如下。

1/* Main state. */ 2 3struct sentinelState { 4 5 char myid[CONFIG_RUN_ID_SIZE+1]; /* This sentinel ID. */ 6 7 uint64_t current_epoch; /* Current epoch. */ 8 9 dict *masters; /* Dictionary of master sentinelRedisInstances. 10 11 Key is the instance name, value is the 12 13 sentinelRedisInstance structure pointer. */ 14 15 int tilt; /* Are we in TILT mode? */ 16 17 int running_scripts; /* Number of scripts in execution right now. */ 18 19 mstime_t tilt_start_time; /* When TITL started. */ 20 21 mstime_t previous_time; /* Last time we ran the time handler. */ 22 23 list *scripts_queue; /* Queue of user scripts to execute. */ 24 25 char *announce_ip; /* IP addr that is gossiped to other sentinels if 26 27 not NULL. */ 28 29 int announce_port; /* Port that is gossiped to other sentinels if 30 31 non zero. */ 32 33 unsigned long simfailure_flags; /* Failures simulation. */ 34 35 int deny_scripts_reconfig; /* Allow SENTINEL SET ... to change script 36 37 paths at runtime? */ 38 39} sentinel;

其中masters字典指针中的每个值都对应着一个Sentinel检测的主实例。

在读取配置信息后,Redis服务器主函数会调用sentinelIsRunning函数, 做以下几个工作:

  1. 检查配置文件是否被设置,并且检查程序对配置文件是否有写权限,因为如果Sentinel状态改变的话,会不断将自己当前状态记录在配置文件中。

  2. 如果在配置文件中指定运行ID,Sentinel 会使用这个ID作为运行ID,相反地,如果没有指定运行ID,Sentinel会生成一个ID用来作为Sentinel的运行ID。

  3. 对所有的Sentinel监测实例产生初始监测事件。

    /* This function gets called when the server is in Sentinel mode, started,

    • loaded the configuration, and is ready for normal operations. */
      void sentinelIsRunning(void) {
      int j;

      if (server.configfile == NULL) {
      serverLog(LL_WARNING,
      "Sentinel started without a config file. Exiting...");
      exit(1);
      } else if (access(server.configfile,W_OK) == -1) {
      serverLog(LL_WARNING,
      "Sentinel config file %s is not writable: %s. Exiting...",
      server.configfile,strerror(errno));
      exit(1);
      }

      /* If this Sentinel has yet no ID set in the configuration file, we

      • pick a random one and persist the config on disk. From now on this
      • will be this Sentinel ID across restarts. */
        for (j = 0; j < CONFIG_RUN_ID_SIZE; j++)
        if (sentinel.myid[j] != 0) break;

      if (j == CONFIG_RUN_ID_SIZE) {
      /* Pick ID and persist the config. */
      getRandomHexChars(sentinel.myid,CONFIG_RUN_ID_SIZE);
      sentinelFlushConfig();
      }

      /* Log its ID to make debugging of issues simpler. */
      serverLog(LL_WARNING,"Sentinel ID is %s", sentinel.myid);

      /* We want to generate a +monitor event for every configured master

      • at startup. */
        sentinelGenerateInitialMonitorEvents();
        }

Sentinel的主时间事件函数

Sentinel 使用和Redis服务器相同的事件处理机制:分为文件事件和时间事件。文件事件处理机制使用I/O 多路复用来处理服务器端的网络I/O 请求,例如客户端连接,读写等操作。时间处理机制则在主循环中周期性调用时间函数来处理定时操作,例如服务器端的维护,定时更新,删除等操作。Redis服务器主时间函数是在server.c中定义的serverCron函数,在默认情况下,serverCron会每100ms被调用一次。在这个函数中,我们看到如下代码:

1int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { 2 3 int j; 4 5 UNUSED(eventLoop); 6 7 UNUSED(id); 8 9 UNUSED(clientData); 10 11 12 ........... 13 14 /* Run the Sentinel timer if we are in sentinel mode. */ 15 16 if (server.sentinel_mode) sentinelTimer(); 17 18 ........... 19 20}

其中当服务器以sentinel模式运行的时候,serverCron会调用sentinelTimer函数,来运行Sentinel中的主逻辑,sentinelTimer函数在sentinel.c中的定义如下:

1void sentinelTimer(void) { 2 sentinelCheckTiltCondition(); 3 sentinelHandleDictOfRedisInstances(sentinel.masters); 4 sentinelRunPendingScripts(); 5 sentinelCollectTerminatedScripts(); 6 sentinelKillTimedoutScripts(); 7 8 /* We continuously change the frequency of the Redis "timer interrupt" 9 * in order to desynchronize every Sentinel from every other. 10 * This non-determinism avoids that Sentinels started at the same time 11 * exactly continue to stay synchronized asking to be voted at the 12 * same time again and again (resulting in nobody likely winning the 13 * election because of split brain voting). */ 14 server.hz = CONFIG_DEFAULT_HZ + rand() % CONFIG_DEFAULT_HZ; 15}

Sentinel Timer函数会做如下几个操作:

  1. 检查Sentinel当前是否在Tilt 模式(Tilt模式将会在稍后章节介绍)。
  2. 检查Sentinel与其监控主备实例,以及其他Sentinel实例的连接,更新当前状态,并在主实例下线的时候自动做主备倒换操作。
  3. 检查回调脚本状态,并做相应操作。
  4. 更新服务器频率(调用serverCron函数的频率),加上一个随机因子,作用是防止监控相同主节点的Sentinel在选举Leader的时候时间冲突,导致选举无法产生绝对多的票数。

其中SentinelHandleDictOfRedisInstances函数的定义如下:

1/* Perform scheduled operations for all the instances in the dictionary. 2 * Recursively call the function against dictionaries of slaves. */ 3void sentinelHandleDictOfRedisInstances(dict *instances) { 4 dictIterator *di; 5 dictEntry *de; 6 sentinelRedisInstance *switch_to_promoted = NULL; 7 8 /* There are a number of things we need to perform against every master. */ 9 di = dictGetIterator(instances); 10 while((de = dictNext(di)) != NULL) { 11 sentinelRedisInstance *ri = dictGetVal(de); 12 13 sentinelHandleRedisInstance(ri); 14 if (ri->flags & SRI_MASTER) { 15 sentinelHandleDictOfRedisInstances(ri->slaves); 16 sentinelHandleDictOfRedisInstances(ri->sentinels); 17 if (ri->failover_state == SENTINEL_FAILOVER_STATE_UPDATE_CONFIG) { 18 switch_to_promoted = ri; 19 } 20 } 21 } 22 if (switch_to_promoted) 23 sentinelFailoverSwitchToPromotedSlave(switch_to_promoted); 24 dictReleaseIterator(di); 25}

SentinelHandleDictOfRedisInstances函数主要做的工作是:

调用sentinelHandleDictOfRedisInstance函数处理Sentinel与其它特定实例连接,状态更 新,以及主备倒换工作。

  1. 如果当前处理实例为主实例,递归调用SentinelHandleDictOfRedisInstances函数处理其下属的从实例以及其他监控这个主实例的Sentinel。
  2. 在主备倒换成功的情况下,更新主实例为升级为主实例的从实例。

其中在sentinelHandleRedisInstance的定义如下:

1/* Perform scheduled operations for the specified Redis instance. */ 2void sentinelHandleRedisInstance(sentinelRedisInstance *ri) { 3 /* ========== MONITORING HALF ============ */ 4 /* Every kind of instance */ 5 sentinelReconnectInstance(ri); 6 sentinelSendPeriodicCommands(ri); 7 8 /* ============== ACTING HALF ============= */ 9 /* We don't proceed with the acting half if we are in TILT mode. 10 * TILT happens when we find something odd with the time, like a 11 * sudden change in the clock. */ 12 if (sentinel.tilt) { 13 if (mstime()-sentinel.tilt_start_time < SENTINEL_TILT_PERIOD) return; 14 sentinel.tilt = 0; 15 sentinelEvent(LL_WARNING,"-tilt",NULL,"#tilt mode exited"); 16 } 17 18 /* Every kind of instance */ 19 sentinelCheckSubjectivelyDown(ri); 20 21 /* Masters and slaves */ 22 if (ri->flags & (SRI_MASTER|SRI_SLAVE)) { 23 /* Nothing so far. */ 24 } 25 26 /* Only masters */ 27 if (ri->flags & SRI_MASTER) { 28 sentinelCheckObjectivelyDown(ri); 29 if (sentinelStartFailoverIfNeeded(ri)) 30 sentinelAskMasterStateToOtherSentinels(ri,SENTINEL_ASK_FORCED); 31 sentinelFailoverStateMachine(ri); 32 sentinelAskMasterStateToOtherSentinels(ri,SENTINEL_NO_FLAGS); 33 } 34}

这个函数会做以下两部分操作:

1、检查Sentinel和其他实例(主备实例以及其他Sentinel)的连接,如果连接没有设置或已经断开连接,Sentinel会重试相对应的连接,并定时发送响应命令。 需要注意的是:Sentinel和每个主备实例都有两个连接,命令连接和发布订阅连接。但是与其他监听相同主备实例的Sentinel只保留命令连接,这部分细节会在网络章节单独介绍。

2、第二部分操作主要做的是监测主备及其他Sentinel实例,并监测其是否在主观下线状态,对于主实例来说,还要检测是否在客观下线状态,并进行相应的主备倒换操作。

需要注意的是第二部分操作如果Sentinel在Tilt模式下是忽略的,下面我们来看一下这个函数第二部分的的具体实现细节。

sentinelCheckSubjectivelyDown 函数会监测特定的Redis实例(主备实例以及其他Sentinel)是否处于主观下线状态,这部分函数代码如下:

1/* Is this instance down from our point of view? */ 2void sentinelCheckSubjectivelyDown(sentinelRedisInstance *ri) { 3 mstime_t elapsed = 0; 4 5 if (ri->link->act_ping_time) 6 elapsed = mstime() - ri->link->act_ping_time; 7 else if (ri->link->disconnected) 8 elapsed = mstime() - ri->link->last_avail_time; 9 10 ....... 11 12 /* Update the SDOWN flag. We believe the instance is SDOWN if: 13 * 14 * 1) It is not replying. 15 * 2) We believe it is a master, it reports to be a slave for enough time 16 * to meet the down_after_period, plus enough time to get two times 17 * INFO report from the instance. */ 18 if (elapsed > ri->down_after_period || 19 (ri->flags & SRI_MASTER && 20 ri->role_reported == SRI_SLAVE && 21 mstime() - ri->role_reported_time > 22 (ri->down_after_period+SENTINEL_INFO_PERIOD*2))) 23 { 24 /* Is subjectively down */ 25 if ((ri->flags & SRI_S_DOWN) == 0) { 26 sentinelEvent(LL_WARNING,"+sdown",ri,"%@"); 27 ri->s_down_since_time = mstime(); 28 ri->flags |= SRI_S_DOWN; 29 } 30 } else { 31 /* Is subjectively up */ 32 if (ri->flags & SRI_S_DOWN) { 33 sentinelEvent(LL_WARNING,"-sdown",ri,"%@"); 34 ri->flags &= ~(SRI_S_DOWN|SRI_SCRIPT_KILL_SENT); 35 } 36 } 37}

主观下线状态意味着特定的Redis实例满足以下条件之一:

  1. 在实例配置的down_after_milliseconds时间内没有收到Ping的回复。
  2. Sentinel认为实例是主实例,但收到实例为从实例的回复,并且上次实例角色回复时间大于在实例配置的down_after_millisecon时间加上2倍INFO命令间隔。

如果任何一个条件满足,Sentinel会打开实例的S_DOWN标志并认为实例进入主观下线状态。

主观下线状态意味着Sentinel主观认为实例下线,但此时Sentinel并没有询问其他监控此实例的其他Sentinel此实例的在线状态。

sentinelCheckObjectivelyDown 函数会检查实例是否为客观下线状态,这个操作仅仅对主实例进行。sentinelCheckObjectivelyDown函数定义如下:

1/* Is this instance down according to the configured quorum? 2 * 3 * Note that ODOWN is a weak quorum, it only means that enough Sentinels 4 * reported in a given time range that the instance was not reachable. 5 * However messages can be delayed so there are no strong guarantees about 6 * N instances agreeing at the same time about the down state. */ 7void sentinelCheckObjectivelyDown(sentinelRedisInstance *master) { 8 dictIterator *di; 9 dictEntry *de; 10 unsigned int quorum = 0, odown = 0; 11 12 if (master->flags & SRI_S_DOWN) { 13 /* Is down for enough sentinels? */ 14 quorum = 1; /* the current sentinel. */ 15 /* Count all the other sentinels. */ 16 di = dictGetIterator(master->sentinels); 17 while((de = dictNext(di)) != NULL) { 18 sentinelRedisInstance *ri = dictGetVal(de); 19 20 if (ri->flags & SRI_MASTER_DOWN) quorum++; 21 } 22 dictReleaseIterator(di); 23 if (quorum >= master->quorum) odown = 1; 24 } 25 26 /* Set the flag accordingly to the outcome. */ 27 if (odown) { 28 if ((master->flags & SRI_O_DOWN) == 0) { 29 sentinelEvent(LL_WARNING,"+odown",master,"%@ #quorum %d/%d", 30 quorum, master->quorum); 31 master->flags |= SRI_O_DOWN; 32 master->o_down_since_time = mstime(); 33 } 34 } else { 35 if (master->flags & SRI_O_DOWN) { 36 sentinelEvent(LL_WARNING,"-odown",master,"%@"); 37 master->flags &= ~SRI_O_DOWN; 38 } 39 } 40}

这个函数主要进行的操作是循环查看监控此主实例的其他Sentinel SRI_MASTER_DOWN 标志是否打开,如果打开则意味着其他特定的Sentinel认为主实例处于下线状态,并统计认为主实例处于下线状态的票数,如果票数大于等于主实例配置的quorum值,则Sentinel会把主实例的SRI_O_DOWN标志打开,并认为主实例处于客观下线状态。

sentinelStartFailoverIfNeeded函数首先会检查实例是否处于客观下线状态(SRI_O_DOWN标志是否打开),并且在2倍主实例配置的主备倒换超时时间内没有进行主备倒换工作,Sentinel会打开SRI_FAILOVER_IN_PROGRESS标志并设置倒换状态为SENTINEL_FAILOVER_STATE_WAIT_START。并开始进行主备倒换工作。主备倒换的细节将在主备倒换的章节里介绍。

1int sentinelStartFailoverIfNeeded(sentinelRedisInstance *master) { 2 /* We can't failover if the master is not in O_DOWN state. */ 3 if (!(master->flags & SRI_O_DOWN)) return 0; 4 5 /* Failover already in progress? */ 6 if (master->flags & SRI_FAILOVER_IN_PROGRESS) return 0; 7 8 /* Last failover attempt started too little time ago? */ 9 if (mstime() - master->failover_start_time < 10 master->failover_timeout*2) 11 { 12 if (master->failover_delay_logged != master->failover_start_time) { 13 time_t clock = (master->failover_start_time + 14 master->failover_timeout*2) / 1000; 15 char ctimebuf[26]; 16 17 ctime_r(&clock,ctimebuf); 18 ctimebuf[24] = '\0'; /* Remove newline. */ 19 master->failover_delay_logged = master->failover_start_time; 20 serverLog(LL_WARNING, 21 "Next failover delay: I will not start a failover before %s", 22 ctimebuf); 23 } 24 return 0; 25 } 26 27 sentinelStartFailover(master); 28 return 1; 29}

Sentinel的网络连接

上文提到每个Sentinel实例会维护与所监测的主从实例的两个连接,分别是命令连接(Command Connection)和发布订阅连接(Pub/Sub Connection)。但是需要注意的是,Sentinel和其他Sentinel之间只有一个命令连接。下面将分别介绍命令连接和发布订阅连接的作用。

命令连接

Sentinel维护命令连接是为了与其他主从实例以及Sentinel实例通过发送接收命令的方式进行通信,例如:

  1. Sentinel会默认以每1s间隔发送PING 命令给其他实例以主观判断其他实例是否下线。
  2. Sentinel会通过Sentinel和主实例之间的命令连接每隔10s发送INFO命令给主从实例以得到主实例和从实例的最新信息。
  3. 在主实例下线的情况下,Sentinel会通过Sentinel和从实例的命令连接发送SLAVEOF NO ONE命令给选定的从实例从而使从实例提升为新的主节点。
  4. Sentinel会默认每隔1s发送is-master-down-by-addr命令以询问其他Sentinel节点关于监控的主节点是否下线。

在sentinel.c中的sentinelReconnectInstance函数中,命令连接的初始化如下:

1/* Commands connection. */ 2 if (link->cc == NULL) { 3 link->cc = redisAsyncConnectBind(ri->addr->ip,ri->addr->port,NET_FIRST_BIND_ADDR); 4 if (!link->cc->err && server.tls_replication && 5 (instanceLinkNegotiateTLS(link->cc) == C_ERR)) { 6 sentinelEvent(LL_DEBUG,"-cmd-link-reconnection",ri,"%@ #Failed to initialize TLS"); 7 instanceLinkCloseConnection(link,link->cc); 8 } else if (link->cc->err) { 9 sentinelEvent(LL_DEBUG,"-cmd-link-reconnection",ri,"%@ #%s", 10 link->cc->errstr); 11 instanceLinkCloseConnection(link,link->cc); 12 } else { 13 link->pending_commands = 0; 14 link->cc_conn_time = mstime(); 15 link->cc->data = link; 16 redisAeAttach(server.el,link->cc); 17 redisAsyncSetConnectCallback(link->cc, 18 sentinelLinkEstablishedCallback); 19 redisAsyncSetDisconnectCallback(link->cc, 20 sentinelDisconnectCallback); 21 sentinelSendAuthIfNeeded(ri,link->cc); 22 sentinelSetClientName(ri,link->cc,"cmd"); 23 24 /* Send a PING ASAP when reconnecting. */ 25 sentinelSendPing(ri); 26 } 27}

发布订阅连接

Sentinel维护和其他主从节点的发布订阅连接作用是为了获知其他监控相同主从实例的Sentinel实例的存在,并且从其他Sentinel实例中更新对所监控的主从实例以及发送的Sentinel实例的认知。例如在主备倒换完成后,其他Sentinel通过读取领头的Sentinel的频道消息来更新新的主节点的相关信息(地址,端口号等)。

Sentinel在默认每隔2秒钟会发送Hello消息包到其对应的主从实例的__sentinel__:hello频道中。Hello消息格式如下:

__sentinel_:hello <sentinel地址> <sentinel端口号> <sentinel运行id> <sentinel配置纪元> <主节点名字 > <主节点地址> <主节点端口号> <主节点配置纪元>

当Sentinel通过订阅连接收到其他Sentinel发送的的Hello包时,会更新对主从节点以及S发送Sentinel的认知,如果收到自己发送的Hello包,则简单的丢弃不做任何处理。这部分代码逻辑是在sentinel.c中的sentinelProcessHelloMessage函数中定义的,由于篇幅原因在这里不做详细介绍。

在sentinel.c中的sentinelReconnectInstance函数中,发布订阅连接初始化如下:

1/* Pub / Sub */ 2 if ((ri->flags & (SRI_MASTER|SRI_SLAVE)) && link->pc == NULL) { 3 link->pc = redisAsyncConnectBind(ri->addr->ip,ri->addr->port,NET_FIRST_BIND_ADDR); 4 if (!link->pc->err && server.tls_replication && 5 (instanceLinkNegotiateTLS(link->pc) == C_ERR)) { 6 sentinelEvent(LL_DEBUG,"-pubsub-link-reconnection",ri,"%@ #Failed to initialize TLS"); 7 } else if (link->pc->err) { 8 sentinelEvent(LL_DEBUG,"-pubsub-link-reconnection",ri,"%@ #%s", 9 link->pc->errstr); 10 instanceLinkCloseConnection(link,link->pc); 11 } else { 12 int retval; 13 14 link->pc_conn_time = mstime(); 15 link->pc->data = link; 16 redisAeAttach(server.el,link->pc); 17 redisAsyncSetConnectCallback(link->pc, 18 sentinelLinkEstablishedCallback); 19 redisAsyncSetDisconnectCallback(link->pc, 20 sentinelDisconnectCallback); 21 sentinelSendAuthIfNeeded(ri,link->pc); 22 sentinelSetClientName(ri,link->pc,"pubsub"); 23 /* Now we subscribe to the Sentinels "Hello" channel. */ 24 retval = redisAsyncCommand(link->pc, 25 sentinelReceiveHelloMessages, ri, "%s %s", 26 sentinelInstanceMapCommand(ri,"SUBSCRIBE"), 27 SENTINEL_HELLO_CHANNEL); 28 if (retval != C_OK) { 29 /* If we can't subscribe, the Pub/Sub connection is useless 30 * and we can simply disconnect it and try again. */ 31 instanceLinkCloseConnection(link,link->pc); 32 return; 33 } 34 } 35 }

is-master-down-by-addr 命令

Sentinel会默认每隔1s通过命令连接发送is-master-down-by-addr命令以询问其他Sentinel节点关于监控的主节点是否下线。另外,在主实例下线的情况下,Sentinel之间也通过is-master-down-by-addr命令来获得投票并选举领头Sentinel。is-master-down-by-addr格式如下:

is-master-down-by-addr: <主实例地址> <主实例端口号> <当前配置纪元> <运行ID>

如果不是在选举领头Sentinel过程中, <runid>项总为*,相反地,如果在Sentinel向其他Sentinel发送投票请求情况下,<runid>项为自己的运行id。这部分代码如下:

1 if ((master->flags & SRI_S_DOWN) == 0) continue; 2 if (ri->link->disconnected) continue; 3 if (!(flags & SENTINEL_ASK_FORCED) && 4 mstime() - ri->last_master_down_reply_time < SENTINEL_ASK_PERIOD) 5 continue; 6 7 /* Ask */ 8 ll2string(port,sizeof(port),master->addr->port); 9 retval = redisAsyncCommand(ri->link->cc, 10 sentinelReceiveIsMasterDownReply, ri, 11 "%s is-master-down-by-addr %s %s %llu %s", 12 sentinelInstanceMapCommand(ri,"SENTINEL"), 13 master->addr->ip, port, 14 sentinel.current_epoch, 15 (master->failover_state > SENTINEL_FAILOVER_STATE_NONE) ? 16 sentinel.myid : "*"); 17 if (retval == C_OK) ri->link->pending_commands++;

is-master-down-by-addr的命令回复格式如下:

  • <主节点下线状态>
  • <领头Sentinel运行ID >
  • <领头Sentinel配置纪元>

Sentinel在收到其他Sentinel命令回复后,会记录其他Sentinel回复的主实例在线状态信息,以及在选举领头Sentinel过程中的投票情况,这部分的代码逻辑定义在sentinel.c中的sentinelReceiveIsMasterDownByReply函数:

1/* Ignore every error or unexpected reply. 2 * Note that if the command returns an error for any reason we'll 3 * end clearing the SRI_MASTER_DOWN flag for timeout anyway. */ 4if (r->type == REDIS_REPLY_ARRAY && r->elements == 3 && 5 r->element[0]->type == REDIS_REPLY_INTEGER && 6 r->element[1]->type == REDIS_REPLY_STRING && 7 r->element[2]->type == REDIS_REPLY_INTEGER) 8{ 9 ri->last_master_down_reply_time = mstime(); 10 if (r->element[0]->integer == 1) { 11 ri->flags |= SRI_MASTER_DOWN; 12 } else { 13 ri->flags &= ~SRI_MASTER_DOWN; 14 } 15 16 if (strcmp(r->element[1]->str,"*")) { 17 /* If the runid in the reply is not "*" the Sentinel actually 18 * replied with a vote. */ 19 sdsfree(ri->leader); 20 21 if ((long long)ri->leader_epoch != r->element[2]->integer) { 22 serverLog(LL_WARNING, "%s voted for %s %llu", ri->name, r->element[1]->str, (unsigned long long) r->element[2]->integer); 23 } 24 ri->leader = sdsnew(r->element[1]->str); 25 ri->leader_epoch = r->element[2]->integer; 26 } 27}

Tilt模式

Sentinel的Tilt模式会在以下两种情况下开启:

  1. Sentinel进程被阻塞超过SENTINEL_TILT_TRIGGER时间(默认为2s),可能因为进程或系统I/O(内存,网络,存储)请求过多。
  2. 系统时钟调整到之前某个时间值。

Tilt模式是一种保护机制,处于该模式下Sentinel除了发送必要的PING及INFO命令外,不会主动做其他操作,例如主备倒换,标志主观、客观下线等。但可以通过INFO 命令及发布订阅连接的HELLO消息包来获取外界信息并对自身结构进行更新,直到SENTINEL_TILT_PERIOD时长(默认为30s)结束为止,我们可以认为Tilt模式是Sentinel的被动模式。

判断Tilt模式的代码逻辑定义如下:

1void sentinelCheckTiltCondition(void) { 2 mstime_t now = mstime(); 3 mstime_t delta = now - sentinel.previous_time; 4 5 if (delta < 0 || delta > SENTINEL_TILT_TRIGGER) { 6 sentinel.tilt = 1; 7 sentinel.tilt_start_time = mstime(); 8 sentinelEvent(LL_WARNING,"+tilt",NULL,"#tilt mode entered"); 9 } 10 sentinel.previous_time = mstime(); 11}

参考资料:

本文分享自华为云社区《Redis Sentinel 源码分析》,原文作者:中间件小哥。

点击关注,第一时间了解华为云新鲜技术~

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )