// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterProcessGossipSection
void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) {

    // 记录这条消息中包含了多少个节点的信息
    uint16_t count = ntohs(hdr->count);

    // 指向第一个节点的信息
    clusterMsgDataGossip *g = (clusterMsgDataGossip*) hdr->data.ping.gossip;

    // 取出发送者
    clusterNode *sender = link->node ? link->node : clusterLookupNode(hdr->sender);

    // 遍历所有节点的信息
    while(count--) {
        sds ci = sdsempty();

        // 分析节点的 flag
        uint16_t flags = ntohs(g->flags);

        // 信息节点
        clusterNode *node;

        // 取出节点的 flag
        if (flags == 0) ci = sdscat(ci,"noflags,");
        if (flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,");
        if (flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,");
        if (flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,");
        if (flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,");
        if (flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,");
        if (flags & REDIS_NODE_HANDSHAKE) ci = sdscat(ci,"handshake,");
        if (flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,");
        if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' ';

        redisLog(REDIS_DEBUG,"GOSSIP %.40s %s:%d %s",
            g->nodename,
            g->ip,
            ntohs(g->port),
            ci);
        sdsfree(ci);

        /* Update our state accordingly to the gossip sections */
        // 使用消息中的信息对节点进行更新
        node = clusterLookupNode(g->nodename);
        // 节点已经存在于当前节点
        if (node) {
            /* We already know this node.
               Handle failure reports, only when the sender is a master. */
            // 如果 sender 是一个主节点，那么我们需要处理下线报告
            if (sender && nodeIsMaster(sender) && node != myself) {
                // 节点处于 FAIL 或者 PFAIL 状态
                if (flags & (REDIS_NODE_FAIL|REDIS_NODE_PFAIL)) {

                    // 添加 sender 对 node 的下线报告
                    if (clusterNodeAddFailureReport(node,sender)) {
                        redisLog(REDIS_VERBOSE,
                            "Node %.40s reported node %.40s as not reachable.",
                            sender->name, node->name);
                    }

                    // 尝试将 node 标记为 FAIL
                    markNodeAsFailingIfNeeded(node);

                // 节点处于正常状态
                } else {

                    // 如果 sender 曾经发送过对 node 的下线报告
                    // 那么清除该报告
                    if (clusterNodeDelFailureReport(node,sender)) {
                        redisLog(REDIS_VERBOSE,
                            "Node %.40s reported node %.40s is back online.",
                            sender->name, node->name);
                    }
                }
            }

            /* If we already know this node, but it is not reachable, and
             * we see a different address in the gossip section, start an
             * handshake with the (possibly) new address: this will result
             * into a node address update if the handshake will be
             * successful. */
            // 如果节点之前处于 PFAIL 或者 FAIL 状态
            // 并且该节点的 IP 或者端口号已经发生变化
            // 那么可能是节点换了新地址，尝试对它进行握手
            if (node->flags & (REDIS_NODE_FAIL|REDIS_NODE_PFAIL) &&
                (strcasecmp(node->ip,g->ip) || node->port != ntohs(g->port)))
            {
                clusterStartHandshake(g->ip,ntohs(g->port));
            }

        // 当前节点不认识 node
        } else {
            /* If it's not in NOADDR state and we don't have it, we
             * start a handshake process against this IP/PORT pairs.
             *
             * 如果 node 不在 NOADDR 状态，并且当前节点不认识 node 
             * 那么向 node 发送 HANDSHAKE 消息。
             *
             * Note that we require that the sender of this gossip message
             * is a well known node in our cluster, otherwise we risk
             * joining another cluster.
             *
             * 注意，当前节点必须保证 sender 是本集群的节点，
             * 否则我们将有加入了另一个集群的风险。
             */
            if (sender &&
                !(flags & REDIS_NODE_NOADDR) &&
                !clusterBlacklistExists(g->nodename))
            {
                clusterStartHandshake(g->ip,ntohs(g->port));
            }
        }

        /* Next node */
        // 处理下个节点的信息
        g++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movzwl 0xe(%rdi), %r13d
movq 0x20(%rsi), %r14
testq %r14, %r14
jne 0x51b2d
leaq 0x28(%rbx), %rdi
callq 0x50394
movq %rax, %r14
testw %r13w, %r13w
je 0x51d5d
rolw $0x8, %r13w
addq $0x8a0, %rbx            # imm = 0x8A0
leaq 0x8(%r14), %rax
movq %rax, (%rsp)
callq 0x281b2
movq %rax, %r12
movzwl 0x60(%rbx), %ebp
testw %bp, %bp
jne 0x51b6e
movq %r12, %rdi
leaq 0x4a5b2(%rip), %rsi     # 0x9c118
callq 0x283d7
movq %rax, %r12
rolw $0x8, %bp
movzwl %bp, %ebp
testb $0x10, %bpl
je 0x51b8d
movq %r12, %rdi
leaq 0x4a59c(%rip), %rsi     # 0x9c121
callq 0x283d7
movq %rax, %r12
testb $0x1, %bpl
je 0x51ba5
movq %r12, %rdi
leaq 0x4a58c(%rip), %rsi     # 0x9c129
callq 0x283d7
movq %rax, %r12
testb $0x2, %bpl
je 0x51bbd
movq %r12, %rdi
leaq 0x4a57c(%rip), %rsi     # 0x9c131
callq 0x283d7
movq %rax, %r12
testb $0x4, %bpl
je 0x51bd5
movq %r12, %rdi
leaq 0x4a56b(%rip), %rsi     # 0x9c138
callq 0x283d7
movq %rax, %r12
testb $0x8, %bpl
je 0x51bed
movq %r12, %rdi
leaq 0x4a55a(%rip), %rsi     # 0x9c13f
callq 0x283d7
movq %rax, %r12
testb $0x20, %bpl
je 0x51c05
movq %r12, %rdi
leaq 0x4a548(%rip), %rsi     # 0x9c145
callq 0x283d7
movq %rax, %r12
testb $0x40, %bpl
je 0x51c1d
movq %r12, %rdi
leaq 0x4a53b(%rip), %rsi     # 0x9c150
callq 0x283d7
movq %rax, %r12
movslq -0x8(%r12), %rax
cmpb $0x2c, -0x1(%rax,%r12)
jne 0x51c30
movb $0x20, -0x1(%r12,%rax)
leaq 0x30(%rbx), %r15
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %r8d
xorl %edi, %edi
leaq 0x4a50f(%rip), %rsi     # 0x9c158
movq %rbx, %rdx
movq %r15, %rcx
movq %r12, %r9
xorl %eax, %eax
callq 0x23064
movq %r12, %rdi
callq 0x28204
movq %rbx, %rdi
callq 0x50394
testq %rax, %rax
je 0x51cc9
movq %rax, %r12
testq %r14, %r14
je 0x51d06
testb $0x1, 0x30(%r14)
je 0x51d06
cmpq 0x8c99c(%rip), %r12     # 0xde628
je 0x51d06
movq %r12, %rdi
movq %r14, %rsi
testb $0xc, %bpl
je 0x51ce1
callq 0x5122f
testl %eax, %eax
je 0x51cbf
leaq 0x8(%r12), %rcx
movl $0x1, %edi
leaq 0x4a4ba(%rip), %rsi     # 0x9c16e
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x23064
movq %r12, %rdi
callq 0x5172b
jmp 0x51d06
testq %r14, %r14
je 0x51d4c
andl $0x40, %ebp
jne 0x51d4c
movq %rbx, %rdi
callq 0x516e2
testl %eax, %eax
jne 0x51d4c
jmp 0x51d39
callq 0x5132f
testl %eax, %eax
je 0x51d06
leaq 0x8(%r12), %rcx
movl $0x1, %edi
leaq 0x4a4a4(%rip), %rsi     # 0x9c19f
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x23064
testb $0xc, 0x30(%r12)
je 0x51d4c
movq %r12, %rdi
addq $0x888, %rdi            # imm = 0x888
movq %r15, %rsi
callq 0x1f2e0
testl %eax, %eax
jne 0x51d39
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl %eax, 0x8b8(%r12)
je 0x51d4c
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %r15, %rdi
callq 0x519de
decl %r13d
addq $0x68, %rbx
testw %r13w, %r13w
jne 0x51b4b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq