4444/* A global reference to myself is handy to make code more clear.
4545 * Myself always points to server.cluster->myself, that is, the clusterNode
4646 * that represents this node. */
47+ // 为了方便起见,维持一个 myself 全局变量,让它总是指向 cluster->myself 。
4748clusterNode * myself = NULL ;
4849
4950clusterNode * createClusterNode (char * nodename , int flags );
@@ -3074,6 +3075,7 @@ void clusterSendFailoverAuth(clusterNode *node) {
30743075}
30753076
30763077/* Send a MFSTART message to the specified node. */
3078+ // 向给定的节点发送一条 MFSTART 消息
30773079void clusterSendMFStart (clusterNode * node ) {
30783080 unsigned char buf [sizeof (clusterMsg )];
30793081 clusterMsg * hdr = (clusterMsg * ) buf ;
@@ -3411,6 +3413,7 @@ void clusterHandleSlaveFailover(void) {
34113413 clusterBroadcastPong (CLUSTER_BROADCAST_ALL );
34123414
34133415 /* 6) If there was a manual failover in progress, clear the state. */
3416+ // 如果有手动故障转移正在执行,那么清理和它有关的状态
34143417 resetManualFailover ();
34153418 }
34163419}
@@ -3534,8 +3537,13 @@ void clusterHandleSlaveMigration(int max_slaves) {
35343537/* Reset the manual failover state. This works for both masters and slavesa
35353538 * as all the state about manual failover is cleared.
35363539 *
3540+ * 重置与手动故障转移有关的状态,主节点和从节点都可以使用。
3541+ *
35373542 * The function can be used both to initialize the manual failover state at
3538- * startup or to abort a manual failover in progress. */
3543+ * startup or to abort a manual failover in progress.
3544+ * 这个函数既可以用于在启动集群时进行初始化,
3545+ * 又可以实际地应用在手动故障转移的情况。
3546+ */
35393547void resetManualFailover (void ) {
35403548 if (server .cluster -> mf_end && clientsArePaused ()) {
35413549 server .clients_pause_end_time = 0 ;
@@ -3797,6 +3805,8 @@ void clusterCron(void) {
37973805
37983806 /* If we are a master and one of the slaves requested a manual
37993807 * failover, ping it continuously. */
3808+ // 如果这是一个主节点,并且有一个从服务器请求进行手动故障转移
3809+ // 那么向从服务器发送 PING 。
38003810 if (server .cluster -> mf_end &&
38013811 nodeIsMaster (myself ) &&
38023812 server .cluster -> mf_slave == node &&
@@ -4021,6 +4031,8 @@ int clusterDelNodeSlots(clusterNode *node) {
40214031
40224032/* Clear the migrating / importing state for all the slots.
40234033 * This is useful at initialization and when turning a master into slave. */
4034+ // 清理所有槽的迁移和导入状态
4035+ // 通常在初始化或者将主节点转为从节点时使用
40244036void clusterCloseAllSlots (void ) {
40254037 memset (server .cluster -> migrating_slots_to ,0 ,
40264038 sizeof (server .cluster -> migrating_slots_to ));
@@ -4232,6 +4244,8 @@ int verifyClusterConfigWithData(void) {
42324244
42334245/* Set the specified node 'n' as master for this node.
42344246 * If this node is currently a master, it is turned into a slave. */
4247+ // 将节点 n 设置为当前节点的主节点
4248+ // 如果当前节点为主节点,那么将它转换为从节点
42354249void clusterSetMaster (clusterNode * n ) {
42364250 redisAssert (n != myself );
42374251 redisAssert (myself -> numslots == 0 );
@@ -4262,6 +4276,7 @@ void clusterSetMaster(clusterNode *n) {
42624276 * See clusterGenNodesDescription() top comment for more information.
42634277 *
42644278 * The function returns the string representation as an SDS string. */
4279+ // 生成节点的状态描述信息
42654280sds clusterGenNodeDescription (clusterNode * node ) {
42664281 int j , start ;
42674282 sds ci ;
@@ -4856,6 +4871,8 @@ void clusterCommand(redisClient *c) {
48564871 addReply (c ,shared .ok );
48574872 } else if (!strcasecmp (c -> argv [1 ]-> ptr ,"slaves" ) && c -> argc == 3 ) {
48584873 /* CLUSTER SLAVES <NODE ID> */
4874+ // 打印给定主节点的所有从节点的信息
4875+
48594876 clusterNode * n = clusterLookupNode (c -> argv [2 ]-> ptr );
48604877 int j ;
48614878
@@ -4880,6 +4897,8 @@ void clusterCommand(redisClient *c) {
48804897 (c -> argc == 2 || c -> argc == 3 ))
48814898 {
48824899 /* CLUSTER FAILOVER [FORCE] */
4900+ // 执行手动故障转移
4901+
48834902 int force = 0 ;
48844903
48854904 if (c -> argc == 3 ) {
@@ -4891,26 +4910,36 @@ void clusterCommand(redisClient *c) {
48914910 }
48924911 }
48934912
4913+ // 命令只能发送给从节点
48944914 if (nodeIsMaster (myself )) {
48954915 addReplyError (c ,"You should send CLUSTER FAILOVER to a slave" );
48964916 return ;
48974917 } else if (!force &&
48984918 (myself -> slaveof == NULL || nodeFailed (myself -> slaveof ) ||
48994919 myself -> slaveof -> link == NULL ))
49004920 {
4921+ // 如果主节点已下线或者处于失效状态
4922+ // 并且命令没有给定 force 参数,那么命令执行失败
49014923 addReplyError (c ,"Master is down or failed, "
49024924 "please use CLUSTER FAILOVER FORCE" );
49034925 return ;
49044926 }
4927+
4928+ // 重置手动故障转移的有关属性
49054929 resetManualFailover ();
4930+ // 设定手动故障转移的最大执行时限
49064931 server .cluster -> mf_end = mstime () + REDIS_CLUSTER_MF_TIMEOUT ;
49074932
49084933 /* If this is a forced failover, we don't need to talk with our master
49094934 * to agree about the offset. We just failover taking over it without
49104935 * coordination. */
4936+ // 如果这是强制的手动 failover ,那么直接开始 failover ,
4937+ // 无须向其他 master 沟通偏移量。
49114938 if (force ) {
4939+ // 如果这是强制的手动故障转移,那么直接开始执行故障转移操作
49124940 server .cluster -> mf_can_start = 1 ;
49134941 } else {
4942+ // 如果不是强制的话,那么需要和主节点比对相互的偏移量是否一致
49144943 clusterSendMFStart (myself -> slaveof );
49154944 }
49164945 redisLog (REDIS_WARNING ,"Manual failover user request accepted." );
0 commit comments