// File: /workspace/github_c_makefile_star2_O2/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
testq %rax, %rax
jne 0x4d37f
leaq 0x28(%rbx), %rdi
callq 0x4be56
movq %rax, 0x10(%rsp)
rolw $0x8, %r12w
addq $0x8a0, %rbx            # imm = 0x8A0
addq $0x8, %rax
movq %rax, 0x8(%rsp)
subw $0x1, %r12w
jb 0x4d5bb
callq 0x271bf
movq %rax, %rbp
movzwl 0x60(%rbx), %eax
movl %eax, %ecx
rolw $0x8, %cx
movzwl %cx, %r13d
testw %ax, %ax
je 0x4d470
testb $0x10, %r13b
je 0x4d3db
movq %rbp, %rdi
leaq 0x44d4e(%rip), %rsi     # 0x92121
callq 0x273bf
movq %rax, %rbp
testb $0x1, %r13b
je 0x4d3f3
movq %rbp, %rdi
leaq 0x44d3e(%rip), %rsi     # 0x92129
callq 0x273bf
movq %rax, %rbp
testb $0x2, %r13b
je 0x4d40b
movq %rbp, %rdi
leaq 0x44d2e(%rip), %rsi     # 0x92131
callq 0x273bf
movq %rax, %rbp
testb $0x4, %r13b
je 0x4d423
movq %rbp, %rdi
leaq 0x44d1d(%rip), %rsi     # 0x92138
callq 0x273bf
movq %rax, %rbp
testb $0x8, %r13b
je 0x4d43b
movq %rbp, %rdi
leaq 0x44d0c(%rip), %rsi     # 0x9213f
callq 0x273bf
movq %rax, %rbp
testb $0x20, %r13b
je 0x4d453
movq %rbp, %rdi
leaq 0x44cfa(%rip), %rsi     # 0x92145
callq 0x273bf
movq %rax, %rbp
testb $0x40, %r13b
je 0x4d482
movq %rbp, %rdi
leaq 0x44ced(%rip), %rsi     # 0x92150
callq 0x273bf
movq %rax, %rbp
movb $0x1, %r14b
jmp 0x4d485
movq %rbp, %rdi
leaq 0x44c9e(%rip), %rsi     # 0x92118
callq 0x273bf
movq %rax, %rbp
xorl %r14d, %r14d
movslq -0x8(%rbp), %rax
cmpb $0x2c, -0x1(%rax,%rbp)
jne 0x4d495
movb $0x20, -0x1(%rbp,%rax)
leaq 0x30(%rbx), %r15
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %r8d
xorl %edi, %edi
leaq 0x44caa(%rip), %rsi     # 0x92158
movq %rbx, %rdx
movq %r15, %rcx
movq %rbp, %r9
xorl %eax, %eax
callq 0x22613
movq %rbp, %rdi
callq 0x271f7
movq %rbx, %rdi
callq 0x4be56
testq %rax, %rax
je 0x4d531
movq %rax, %rbp
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x4d57b
testb $0x1, 0x30(%rsi)
je 0x4d57b
cmpq 0x8e133(%rip), %rbp     # 0xdb628
je 0x4d57b
movq %rbp, %rdi
testb $0xc, %r13b
je 0x4d558
callq 0x4cb96
testl %eax, %eax
je 0x4d527
leaq 0x8(%rbp), %rcx
pushq $0x1
popq %rdi
leaq 0x44c53(%rip), %rsi     # 0x9216e
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x22613
movq %rbp, %rdi
callq 0x4d000
jmp 0x4d57b
cmpq $0x0, 0x10(%rsp)
sete %al
orb %r14b, %al
jne 0x4d5b2
movq %rbx, %rdi
callq 0x4cfb9
testl %eax, %eax
jne 0x4d5b2
movzwl 0x5e(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
jmp 0x4d5aa
callq 0x4cc82
testl %eax, %eax
je 0x4d57b
leaq 0x8(%rbp), %rcx
pushq $0x1
popq %rdi
leaq 0x44c30(%rip), %rsi     # 0x9219f
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x22613
testb $0xc, 0x30(%rbp)
je 0x4d5b2
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 0x4d5aa
cmpl %esi, 0x8b8(%rbp)
je 0x4d5b2
movq %r15, %rdi
callq 0x4d25c
addq $0x68, %rbx
jmp 0x4d399
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq