// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
cmpq $0x0, 0x60(%rdi)
je 0x5a305
callq 0x22843
movq %rax, %r14
subq 0x60(%rbx), %r14
jmp 0x5a308
xorl %r14d, %r14d
cmpq $0x0, 0x28(%rbx)
je 0x5a371
callq 0x22843
subq 0x40(%rbx), %rax
cmpq $0x3a99, %rax           # imm = 0x3A99
jl 0x5a371
cmpq $0x0, 0x60(%rbx)
je 0x5a371
callq 0x22843
movq %rax, %rcx
subq 0x60(%rbx), %rcx
movq 0x98(%rbx), %rax
pushq $0x2
popq %r15
cqto
idivq %r15
cmpq %rax, %rcx
jle 0x5a371
callq 0x22843
movq %rax, %rcx
subq 0x68(%rbx), %rcx
movq 0x98(%rbx), %rax
cqto
idivq %r15
cmpq %rax, %rcx
jle 0x5a371
movq 0x28(%rbx), %rsi
movq %rbx, %rdi
callq 0x5742b
cmpq $0x0, 0x30(%rbx)
je 0x5a3a6
callq 0x22843
subq 0x48(%rbx), %rax
cmpq $0x3a99, %rax           # imm = 0x3A99
jl 0x5a3a6
callq 0x22843
subq 0x50(%rbx), %rax
cmpq $0x1771, %rax           # imm = 0x1771
jl 0x5a3a6
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x5742b
cmpq 0x98(%rbx), %r14
jle 0x5a3e6
testb $0x10, (%rbx)
jne 0x5a43c
leaq 0x3b887(%rip), %rsi     # 0x95c46
leaq 0x3ac1c(%rip), %rcx     # 0x94fe2
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5678f
callq 0x22843
movq %rax, 0x88(%rbx)
movl (%rbx), %eax
orl $0x10, %eax
jmp 0x5a43a
movl (%rbx), %eax
testb $0x1, %al
je 0x5a414
cmpl $0x2, 0xa8(%rbx)
jne 0x5a414
callq 0x22843
subq 0xb0(%rbx), %rax
movl $0x4e20, %ecx           # imm = 0x4E20
addq 0x98(%rbx), %rcx
cmpq %rcx, %rax
jg 0x5a3af
movl (%rbx), %eax
testb $0x10, %al
je 0x5a43c
leaq 0x3b82e(%rip), %rsi     # 0x95c4d
leaq 0x3abbc(%rip), %rcx     # 0x94fe2
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5678f
movl $0xffffdfef, %eax       # imm = 0xFFFFDFEF
andl (%rbx), %eax
movl %eax, (%rbx)
popq %rbx
popq %r14
popq %r15
retq