// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x7f800
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x60(%rcx), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x7f8a8
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x40(%rcx), %rax
cmpq $0x3a98, %rax           # imm = 0x3A98
jle 0x7f8a8
movq -0x8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x7f8a8
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x60(%rcx), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
cmpq %rcx, %rax
jle 0x7f8a8
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x68(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x20(%rbp), %rax
cmpq %rcx, %rax
jle 0x7f8a8
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x7aee0
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x7f8ee
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x48(%rcx), %rax
cmpq $0x3a98, %rax           # imm = 0x3A98
jle 0x7f8ee
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0x50(%rcx), %rax
cmpq $0x1770, %rax           # imm = 0x1770
jle 0x7f8ee
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x7aee0
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x98(%rcx), %rax
jg 0x7f945
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x7f991
movq -0x8(%rbp), %rax
cmpl $0x2, 0xa8(%rax)
jne 0x7f991
callq 0x252e0
movq -0x8(%rbp), %rcx
subq 0xb0(%rcx), %rax
movq -0x8(%rbp), %rcx
movq 0x98(%rcx), %rcx
addq $0x4e20, %rcx           # imm = 0x4E20
cmpq %rcx, %rax
jle 0x7f991
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x7f98f
movq -0x8(%rbp), %rdx
movl $0x3, %edi
leaq 0x6a333(%rip), %rsi     # 0xe9c96
leaq 0x696c8(%rip), %rcx     # 0xe9032
movb $0x0, %al
callq 0x79b10
callq 0x252e0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
orl $0x10, %ecx
movl %ecx, (%rax)
jmp 0x7f9cd
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x7f9cb
movq -0x8(%rbp), %rdx
movl $0x3, %edi
leaq 0x6a2ee(%rip), %rsi     # 0xe9c9d
leaq 0x6967c(%rip), %rcx     # 0xe9032
movb $0x0, %al
callq 0x79b10
movq -0x8(%rbp), %rax
movl (%rax), %ecx
andl $0xffffdfef, %ecx       # imm = 0xFFFFDFEF
movl %ecx, (%rax)
jmp 0x7f9cd
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)