// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterUpdateState
void clusterUpdateState(void) {
    int j, new_state;
    int unreachable_masters = 0;
    static mstime_t among_minority_time;
    static mstime_t first_call_time = 0;

    server.cluster->todo_before_sleep &= ~CLUSTER_TODO_UPDATE_STATE;

    /* If this is a master node, wait some time before turning the state
     * into OK, since it is not a good idea to rejoin the cluster as a writable
     * master, after a reboot, without giving the cluster a chance to
     * reconfigure this node. Note that the delay is calculated starting from
     * the first call to this function and not since the server start, in order
     * to don't count the DB loading time. */
    if (first_call_time == 0) first_call_time = mstime();
    if (nodeIsMaster(myself) &&
        mstime() - first_call_time < REDIS_CLUSTER_WRITABLE_DELAY) return;

    /* Start assuming the state is OK. We'll turn it into FAIL if there
     * are the right conditions. */

    // 先假设节点状态为 OK ，后面再检测节点是否真的下线
    new_state = REDIS_CLUSTER_OK;

    /* Check if all the slots are covered. */
    // 检查是否所有槽都已经有某个节点在处理
    for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) {
        if (server.cluster->slots[j] == NULL ||
            server.cluster->slots[j]->flags & (REDIS_NODE_FAIL))
        {
            new_state = REDIS_CLUSTER_FAIL;
            break;
        }
    }

    /* Compute the cluster size, that is the number of master nodes
     * serving at least a single slot.
     *
     * At the same time count the number of unreachable masters with
     * at least one node. */
    // 统计在线并且正在处理至少一个槽的 master 的数量，
    // 以及下线 master 的数量
    {
        dictIterator *di;
        dictEntry *de;

        server.cluster->size = 0;
        di = dictGetSafeIterator(server.cluster->nodes);
        while((de = dictNext(di)) != NULL) {
            clusterNode *node = dictGetVal(de);

            if (nodeIsMaster(node) && node->numslots) {
                server.cluster->size++;
                if (node->flags & (REDIS_NODE_FAIL|REDIS_NODE_PFAIL))
                    unreachable_masters++;
            }
        }
        dictReleaseIterator(di);
    }

    /* If we can't reach at least half the masters, change the cluster state
     * to FAIL, as we are not even able to mark nodes as FAIL in this side
     * of the netsplit because of lack of majority.
     *
     * 如果不能连接到半数以上节点，那么将我们自己的状态设置为 FAIL
     * 因为在少于半数节点的情况下，节点是无法将一个节点判断为 FAIL 的。
     */
    {
        int needed_quorum = (server.cluster->size / 2) + 1;

        if (unreachable_masters >= needed_quorum) {
            new_state = REDIS_CLUSTER_FAIL;
            among_minority_time = mstime();
        }
    }

    /* Log a state change */
    // 记录状态变更
    if (new_state != server.cluster->state) {
        mstime_t rejoin_delay = server.cluster_node_timeout;

        /* If the instance is a master and was partitioned away with the
         * minority, don't let it accept queries for some time after the
         * partition heals, to make sure there is enough time to receive
         * a configuration update. */
        if (rejoin_delay > REDIS_CLUSTER_MAX_REJOIN_DELAY)
            rejoin_delay = REDIS_CLUSTER_MAX_REJOIN_DELAY;
        if (rejoin_delay < REDIS_CLUSTER_MIN_REJOIN_DELAY)
            rejoin_delay = REDIS_CLUSTER_MIN_REJOIN_DELAY;

        if (new_state == REDIS_CLUSTER_OK &&
            nodeIsMaster(myself) &&
            mstime() - among_minority_time < rejoin_delay)
        {
            return;
        }

        /* Change the state and log the event. */
        redisLog(REDIS_WARNING,"Cluster state changed: %s",
            new_state == REDIS_CLUSTER_OK ? "ok" : "fail");

        // 设置新状态
        server.cluster->state = new_state;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq 0x77ebd(%rip), %r14     # 0xc6fa8
movq 0x7d0(%r14), %rax
andl $-0x3, 0x60078(%rax)
cmpq $0x0, 0x8c53f(%rip)     # 0xdb640
jne 0x4f10f
callq 0x22843
movq %rax, 0x8c531(%rip)     # 0xdb640
movq 0x8c512(%rip), %rax     # 0xdb628
testb $0x1, 0x30(%rax)
je 0x4f134
callq 0x22843
subq 0x8c518(%rip), %rax     # 0xdb640
cmpq $0x7d0, %rax            # imm = 0x7D0
jl 0x4f25c
movq 0x7d0(%r14), %rax
xorl %r15d, %r15d
pushq $0x1
popq %rcx
xorl %edx, %edx
cmpq $0x4000, %rdx           # imm = 0x4000
je 0x4f165
movq 0x40028(%rax,%rdx,8), %rsi
testq %rsi, %rsi
je 0x4f162
incq %rdx
testb $0x8, 0x30(%rsi)
je 0x4f143
movl %ecx, %r15d
andl $0x0, 0x14(%rax)
movq 0x18(%rax), %rdi
callq 0x21f20
movq %rax, %rbx
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x4f1ad
movq 0x8(%rax), %rcx
movl 0x30(%rcx), %eax
testb $0x1, %al
je 0x4f177
cmpl $0x0, 0x840(%rcx)
je 0x4f177
movq 0x7d0(%r14), %rcx
incl 0x14(%rcx)
andl $0xc, %eax
cmpl $0x1, %eax
sbbl $-0x1, %ebp
jmp 0x4f177
movq %rbx, %rdi
callq 0x21fe6
movq 0x7d0(%r14), %rcx
movl 0x14(%rcx), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
cmpl %eax, %ebp
jle 0x4f1e0
callq 0x22843
movq %rax, 0x8c463(%rip)     # 0xdb638
movq 0x7d0(%r14), %rcx
pushq $0x1
popq %r15
cmpl 0x10(%rcx), %r15d
je 0x4f25c
movq 0x7c0(%r14), %rax
movl $0x1388, %ecx           # imm = 0x1388
cmpq %rcx, %rax
cmovlq %rax, %rcx
cmpq $0x1f5, %rcx            # imm = 0x1F5
movl $0x1f4, %r12d           # imm = 0x1F4
cmovgeq %rcx, %r12
testl %r15d, %r15d
je 0x4f218
leaq 0x42a77(%rip), %rbx     # 0x91c8d
jmp 0x4f23d
movq 0x8c409(%rip), %rax     # 0xdb628
leaq 0x489d5(%rip), %rbx     # 0x97bfb
testb $0x1, 0x30(%rax)
je 0x4f23d
callq 0x22843
subq 0x8c400(%rip), %rax     # 0xdb638
cmpq %r12, %rax
jl 0x4f25c
leaq 0x43565(%rip), %rsi     # 0x927a9
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22613
movq 0x7d0(%r14), %rax
movl %r15d, 0x10(%rax)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq