// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
movq %rdi, %rbx
movzwl 0xe(%rdi), %r12d
movq 0x20(%rsi), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x51585
leaq 0x28(%rbx), %rdi
callq 0x4fddc
movq %rax, 0x8(%rsp)
testw %r12w, %r12w
je 0x517d9
rolw $0x8, %r12w
addq $0x8a0, %rbx            # imm = 0x8A0
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x10(%rsp)
callq 0x2845e
movq %rax, %rbp
movzwl 0x60(%rbx), %eax
movl %eax, %ecx
rolw $0x8, %cx
movzwl %cx, %r13d
testw %ax, %ax
je 0x51675
testb $0x10, %r13b
je 0x515e0
movq %rbp, %rdi
leaq 0x4ab89(%rip), %rsi     # 0x9c161
callq 0x286db
movq %rax, %rbp
testb $0x1, %r13b
je 0x515f8
movq %rbp, %rdi
leaq 0x4ab79(%rip), %rsi     # 0x9c169
callq 0x286db
movq %rax, %rbp
testb $0x2, %r13b
je 0x51610
movq %rbp, %rdi
leaq 0x4ab69(%rip), %rsi     # 0x9c171
callq 0x286db
movq %rax, %rbp
testb $0x4, %r13b
je 0x51628
movq %rbp, %rdi
leaq 0x4ab58(%rip), %rsi     # 0x9c178
callq 0x286db
movq %rax, %rbp
testb $0x8, %r13b
je 0x51640
movq %rbp, %rdi
leaq 0x4ab47(%rip), %rsi     # 0x9c17f
callq 0x286db
movq %rax, %rbp
testb $0x20, %r13b
je 0x51658
movq %rbp, %rdi
leaq 0x4ab35(%rip), %rsi     # 0x9c185
callq 0x286db
movq %rax, %rbp
testb $0x40, %r13b
je 0x51687
movq %rbp, %rdi
leaq 0x4ab28(%rip), %rsi     # 0x9c190
callq 0x286db
movq %rax, %rbp
movb $0x1, %r14b
jmp 0x5168a
movq %rbp, %rdi
leaq 0x4aad9(%rip), %rsi     # 0x9c158
callq 0x286db
movq %rax, %rbp
xorl %r14d, %r14d
movslq -0x8(%rbp), %rax
cmpb $0x2c, -0x1(%rax,%rbp)
jne 0x5169a
movb $0x20, -0x1(%rbp,%rax)
leaq 0x30(%rbx), %r15
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %r8d
xorl %edi, %edi
leaq 0x4aae5(%rip), %rsi     # 0x9c198
movq %rbx, %rdx
movq %r15, %rcx
movq %rbp, %r9
xorl %eax, %eax
callq 0x23223
movq %rbp, %rdi
callq 0x2850d
movq %rbx, %rdi
callq 0x4fddc
testq %rax, %rax
je 0x51740
movq %rax, %rbp
cmpq $0x0, 0x8(%rsp)
je 0x51791
movq 0x8(%rsp), %rax
testb $0x1, 0x30(%rax)
je 0x51791
cmpq 0x8cf2b(%rip), %rbp     # 0xde628
je 0x51791
movq %rbp, %rdi
testb $0xc, %r13b
je 0x51767
movq 0x8(%rsp), %rsi
callq 0x50c70
testl %eax, %eax
je 0x51736
leaq 0x8(%rbp), %rcx
movl $0x1, %edi
leaq 0x4aa84(%rip), %rsi     # 0x9c1ae
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x23223
movq %rbp, %rdi
callq 0x5116c
jmp 0x51791
cmpq $0x0, 0x8(%rsp)
sete %al
orb %r14b, %al
jne 0x517c8
movq %rbx, %rdi
callq 0x51123
testl %eax, %eax
jne 0x517c8
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
jmp 0x517c0
movq 0x8(%rsp), %rsi
callq 0x50d70
testl %eax, %eax
je 0x51791
leaq 0x8(%rbp), %rcx
movl $0x1, %edi
leaq 0x4aa5a(%rip), %rsi     # 0x9c1df
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x23223
testb $0xc, 0x30(%rbp)
je 0x517c8
movq %rbp, %rdi
addq $0x888, %rdi            # imm = 0x888
movq %r15, %rsi
callq 0x1f2e0
movzwl 0x5e(%rbx), %ecx
rolw $0x8, %cx
movzwl %cx, %esi
testl %eax, %eax
jne 0x517c0
cmpl %esi, 0x8b8(%rbp)
je 0x517c8
movq %r15, %rdi
callq 0x51428
decl %r12d
addq $0x68, %rbx
testw %r12w, %r12w
jne 0x515a9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq