Skip to content

Commit 68d5c5d

Browse files
committed
更新 cluster.c 和 cluster.h 的注释
1 parent 4579f74 commit 68d5c5d

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/cluster.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
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 。
4748
clusterNode *myself = NULL;
4849

4950
clusterNode *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 消息
30773079
void 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+
*/
35393547
void 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+
// 通常在初始化或者将主节点转为从节点时使用
40244036
void 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+
// 如果当前节点为主节点,那么将它转换为从节点
42354249
void 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+
// 生成节点的状态描述信息
42654280
sds 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.");

src/cluster.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,29 @@
3232
#define REDIS_CLUSTER_FAIL_UNDO_TIME_ADD 10 /* Some additional time. */
3333
// 在检查从节点数据是否有效时使用的乘法因子
3434
#define REDIS_CLUSTER_SLAVE_VALIDITY_MULT 10 /* Slave data validity. */
35+
// 在执行故障转移之前需要等待的秒数,似乎已经废弃
3536
#define REDIS_CLUSTER_FAILOVER_DELAY 5 /* Seconds */
37+
// 未使用,似乎已经废弃
3638
#define REDIS_CLUSTER_DEFAULT_MIGRATION_BARRIER 1
39+
// 在进行手动的故障转移之前,需要等待的超时时间
3740
#define REDIS_CLUSTER_MF_TIMEOUT 5000 /* Milliseconds to do a manual failover. */
41+
// 未使用,似乎已经废弃
3842
#define REDIS_CLUSTER_MF_PAUSE_MULT 2 /* Master pause manual failover mult. */
3943

4044
/* Redirection errors returned by getNodeByQuery(). */
45+
/* 由 getNodeByQuery() 函数返回的转向错误。 */
46+
// 节点可以处理这个命令
4147
#define REDIS_CLUSTER_REDIR_NONE 0 /* Node can serve the request. */
48+
// 键在其他槽
4249
#define REDIS_CLUSTER_REDIR_CROSS_SLOT 1 /* Keys in different slots. */
50+
// 键所处的槽正在进行 reshard
4351
#define REDIS_CLUSTER_REDIR_UNSTABLE 2 /* Keys in slot resharding. */
52+
// 需要进行 ASK 转向
4453
#define REDIS_CLUSTER_REDIR_ASK 3 /* -ASK redirection required. */
54+
// 需要进行 MOVED 转向
4555
#define REDIS_CLUSTER_REDIR_MOVED 4 /* -MOVED redirection required. */
4656

57+
// 前置定义,防止编译错误
4758
struct clusterNode;
4859

4960

@@ -91,6 +102,7 @@ typedef struct clusterLink {
91102
// 空名字(在节点为主节点时,用作消息中的 slaveof 属性的值)
92103
#define REDIS_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
93104

105+
// 用于判断节点身份和状态的一系列宏
94106
#define nodeIsMaster(n) ((n)->flags & REDIS_NODE_MASTER)
95107
#define nodeIsSlave(n) ((n)->flags & REDIS_NODE_SLAVE)
96108
#define nodeInHandshake(n) ((n)->flags & REDIS_NODE_HANDSHAKE)
@@ -186,9 +198,10 @@ struct clusterNode {
186198
typedef struct clusterNode clusterNode;
187199

188200

189-
// 集群状态,每个节点都保存着一个这样的状态,记录了它们眼中的集群的样子
190-
// 注意,结构中有一部分属性其实和节点有关的,不知道为什么被放在了这里
191-
// 比如 slots_to_keys 、failover_auth_count 等属性就是和本节点有关的
201+
// 集群状态,每个节点都保存着一个这样的状态,记录了它们眼中的集群的样子。
202+
// 另外,虽然这个结构主要用于记录集群的属性,但是为了节约资源,
203+
// 有些与节点有关的属性,比如 slots_to_keys 、 failover_auth_count
204+
// 也被放到了这个结构里面。
192205
typedef struct clusterState {
193206

194207
// 指向当前节点的指针
@@ -244,18 +257,31 @@ typedef struct clusterState {
244257
int failover_auth_sent; /* True if we already asked for votes. */
245258

246259
int failover_auth_rank; /* This slave rank for current auth request. */
260+
247261
uint64_t failover_auth_epoch; /* Epoch of the current election. */
262+
248263
/* Manual failover state in common. */
264+
/* 共用的手动故障转移状态 */
265+
266+
// 手动故障转移执行的时间限制
249267
mstime_t mf_end; /* Manual failover time limit (ms unixtime).
250268
It is zero if there is no MF in progress. */
251269
/* Manual failover state of master. */
270+
/* 主服务器的手动故障转移状态 */
252271
clusterNode *mf_slave; /* Slave performing the manual failover. */
253272
/* Manual failover state of slave. */
273+
/* 从服务器的手动故障转移状态 */
254274
long long mf_master_offset; /* Master offset the slave needs to start MF
255275
or zero if stil not received. */
276+
// 指示手动故障转移是否可以开始的标志值
277+
// 值为非 0 时表示各个主服务器可以开始投票
256278
int mf_can_start; /* If non-zero signal that the manual failover
257279
can start requesting masters vote. */
280+
258281
/* The followign fields are uesd by masters to take state on elections. */
282+
/* 以下这些域由主服务器使用,用于记录选举时的状态 */
283+
284+
// 集群最后一次进行投票的纪元
259285
uint64_t lastVoteEpoch; /* Epoch of the last vote granted. */
260286

261287
// 在进入下个事件循环之前要做的事情,以各个 flag 来记录
@@ -303,6 +329,7 @@ typedef struct clusterState {
303329
#define CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK 6 /* Yes, you have my vote */
304330
// 槽布局已经发生变化,消息发送者要求消息接收者进行相应的更新
305331
#define CLUSTERMSG_TYPE_UPDATE 7 /* Another node slots configuration */
332+
// 为了进行手动故障转移,暂停各个客户端
306333
#define CLUSTERMSG_TYPE_MFSTART 8 /* Pause clients for manual failover */
307334

308335
/* Initially we don't know our "name", but we'll find it once we connect
@@ -331,6 +358,7 @@ typedef struct {
331358
// 节点的标识值
332359
uint16_t flags;
333360

361+
// 对齐字节,不使用
334362
uint32_t notused; /* for 64 bit alignment */
335363

336364
} clusterMsgDataGossip;
@@ -446,6 +474,7 @@ typedef struct {
446474
// 消息发送者所处集群的状态
447475
unsigned char state; /* Cluster state from the POV of the sender */
448476

477+
// 消息标志
449478
unsigned char mflags[3]; /* Message flags: CLUSTERMSG_FLAG[012]_... */
450479

451480
// 消息的正文(或者说,内容)

0 commit comments

Comments
 (0)