// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelCheckSubjectivelyDown
void sentinelCheckSubjectivelyDown(sentinelRedisInstance *ri) {

    mstime_t elapsed = 0;

    if (ri->last_ping_time)
        elapsed = mstime() - ri->last_ping_time;

    /* Check if we are in need for a reconnection of one of the 
     * links, because we are detecting low activity.
     *
     * 如果检测到连接的活跃度（activity）很低，那么考虑重断开连接，并进行重连
     *
     * 1) Check if the command link seems connected, was connected not less
     *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have a
     *    pending ping for more than half the timeout. */
    // 考虑断开实例的 cc 连接
    if (ri->cc &&
        (mstime() - ri->cc_conn_time) > SENTINEL_MIN_LINK_RECONNECT_PERIOD &&
        ri->last_ping_time != 0 && /* Ther is a pending ping... */
        /* The pending ping is delayed, and we did not received
         * error replies as well. */
        (mstime() - ri->last_ping_time) > (ri->down_after_period/2) &&
        (mstime() - ri->last_pong_time) > (ri->down_after_period/2))
    {
        sentinelKillLink(ri,ri->cc);
    }

    /* 2) Check if the pubsub link seems connected, was connected not less
     *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have no
     *    activity in the Pub/Sub channel for more than
     *    SENTINEL_PUBLISH_PERIOD * 3.
     */
    // 考虑断开实例的 pc 连接
    if (ri->pc &&
        (mstime() - ri->pc_conn_time) > SENTINEL_MIN_LINK_RECONNECT_PERIOD &&
        (mstime() - ri->pc_last_activity) > (SENTINEL_PUBLISH_PERIOD*3))
    {
        sentinelKillLink(ri,ri->pc);
    }

    /* Update the SDOWN flag. We believe the instance is SDOWN if:
     *
     * 更新 SDOWN 标识。如果以下条件被满足，那么 Sentinel 认为实例已下线：
     *
     * 1) It is not replying.
     *    它没有回应命令
     * 2) We believe it is a master, it reports to be a slave for enough time
     *    to meet the down_after_period, plus enough time to get two times
     *    INFO report from the instance. 
     *    Sentinel 认为实例是主服务器，这个服务器向 Sentinel 报告它将成为从服务器，
     *    但在超过给定时限之后，服务器仍然没有完成这一角色转换。
     */
    if (elapsed > ri->down_after_period ||
        (ri->flags & SRI_MASTER &&
         ri->role_reported == SRI_SLAVE &&
         mstime() - ri->role_reported_time >
          (ri->down_after_period+SENTINEL_INFO_PERIOD*2)))
    {
        /* Is subjectively down */
        if ((ri->flags & SRI_S_DOWN) == 0) {
            // 发送事件
            sentinelEvent(REDIS_WARNING,"+sdown",ri,"%@");
            // 记录进入 SDOWN 状态的时间
            ri->s_down_since_time = mstime();
            // 打开 SDOWN 标志
            ri->flags |= SRI_S_DOWN;
        }
    } else {
        // 移除（可能有的） SDOWN 状态
        /* Is subjectively up */
        if (ri->flags & SRI_S_DOWN) {
            // 发送事件
            sentinelEvent(REDIS_WARNING,"-sdown",ri,"%@");
            // 移除相关标志
            ri->flags &= ~(SRI_S_DOWN|SRI_SCRIPT_KILL_SENT);
        }
    }
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpq $0x0, 0x60(%rdi)
je 0x5f054
callq 0x23461
movq %rax, %r14
subq 0x60(%rbx), %r14
jmp 0x5f057
xorl %r14d, %r14d
cmpq $0x0, 0x28(%rbx)
je 0x5f0c6
callq 0x23461
subq 0x40(%rbx), %rax
cmpq $0x3a99, %rax           # imm = 0x3A99
jl 0x5f0c6
cmpq $0x0, 0x60(%rbx)
je 0x5f0c6
callq 0x23461
subq 0x60(%rbx), %rax
movq 0x98(%rbx), %rcx
movq %rcx, %rdx
shrq $0x3f, %rdx
addq %rcx, %rdx
sarq %rdx
cmpq %rdx, %rax
jle 0x5f0c6
callq 0x23461
subq 0x68(%rbx), %rax
movq 0x98(%rbx), %rcx
movq %rcx, %rdx
shrq $0x3f, %rdx
addq %rcx, %rdx
sarq %rdx
cmpq %rdx, %rax
jle 0x5f0c6
movq 0x28(%rbx), %rsi
movq %rbx, %rdi
callq 0x5be4f
cmpq $0x0, 0x30(%rbx)
je 0x5f0fb
callq 0x23461
subq 0x48(%rbx), %rax
cmpq $0x3a99, %rax           # imm = 0x3A99
jl 0x5f0fb
callq 0x23461
subq 0x50(%rbx), %rax
cmpq $0x1771, %rax           # imm = 0x1771
jl 0x5f0fb
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x5be4f
cmpq 0x98(%rbx), %r14
jle 0x5f13d
testb $0x10, (%rbx)
jne 0x5f195
leaq 0x40bbe(%rip), %rsi     # 0x9fcd2
leaq 0x3ff53(%rip), %rcx     # 0x9f06e
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5b0ac
callq 0x23461
movq %rax, 0x88(%rbx)
movl (%rbx), %eax
orl $0x10, %eax
jmp 0x5f193
movl (%rbx), %eax
testb $0x1, %al
je 0x5f16b
cmpl $0x2, 0xa8(%rbx)
jne 0x5f16b
callq 0x23461
subq 0xb0(%rbx), %rax
movl $0x4e20, %ecx           # imm = 0x4E20
addq 0x98(%rbx), %rcx
cmpq %rcx, %rax
jg 0x5f104
movl (%rbx), %eax
testb $0x10, %al
je 0x5f195
leaq 0x40b63(%rip), %rsi     # 0x9fcd9
leaq 0x3fef1(%rip), %rcx     # 0x9f06e
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5b0ac
movl $0xffffdfef, %eax       # imm = 0xFFFFDFEF
andl (%rbx), %eax
movl %eax, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
retq