// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelGetLeader
char *sentinelGetLeader(sentinelRedisInstance *master, uint64_t epoch) {
    dict *counters;
    dictIterator *di;
    dictEntry *de;
    unsigned int voters = 0, voters_quorum;
    char *myvote;
    char *winner = NULL;
    uint64_t leader_epoch;
    uint64_t max_votes = 0;

    redisAssert(master->flags & (SRI_O_DOWN|SRI_FAILOVER_IN_PROGRESS));

    // 统计器
    counters = dictCreate(&leaderVotesDictType,NULL);

    /* Count other sentinels votes */
    // 统计其他 sentinel 的主观 leader 投票
    di = dictGetIterator(master->sentinels);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *ri = dictGetVal(de);

        // 为目标 Sentinel 选出的领头 Sentinel 增加一票
        if (ri->leader != NULL && ri->leader_epoch == sentinel.current_epoch)
            sentinelLeaderIncr(counters,ri->leader);

        // 统计投票数量
        voters++;
    }
    dictReleaseIterator(di);

    /* Check what's the winner. For the winner to win, it needs two conditions:
     *
     * 选出领头 leader ，它必须满足以下两个条件：
     *
     * 1) Absolute majority between voters (50% + 1).
     *    有多于一般的 Sentinel 支持
     * 2) And anyway at least master->quorum votes. 
     *    投票数至少要有 master->quorum 那么多
     */
    di = dictGetIterator(counters);
    while((de = dictNext(di)) != NULL) {

        // 取出票数
        uint64_t votes = dictGetUnsignedIntegerVal(de);

        // 选出票数最大的人
        if (votes > max_votes) {
            max_votes = votes;
            winner = dictGetKey(de);
        }
    }
    dictReleaseIterator(di);

    /* Count this Sentinel vote:
     * if this Sentinel did not voted yet, either vote for the most
     * common voted sentinel, or for itself if no vote exists at all. */
    // 本 Sentinel 进行投票
    // 如果 Sentinel 之前还没有进行投票，那么有两种选择：
    // 1）如果选出了 winner （最多票数支持的 Sentinel ），那么这个 Sentinel 也投 winner 一票
    // 2）如果没有选出 winner ，那么 Sentinel 投自己一票
    if (winner)
        myvote = sentinelVoteLeader(master,epoch,winner,&leader_epoch);
    else
        myvote = sentinelVoteLeader(master,epoch,server.runid,&leader_epoch);

    // 领头 Sentinel 已选出，并且领头的纪元和给定的纪元一样
    if (myvote && leader_epoch == epoch) {

        // 为领头 Sentinel 增加一票（这一票来自本 Sentinel ）
        uint64_t votes = sentinelLeaderIncr(counters,myvote);

        // 如果投票之后的票数比最大票数要大，那么更换领头 Sentinel
        if (votes > max_votes) {
            max_votes = votes;
            winner = myvote;
        }
    }
    voters++; /* Anyway, count me as one of the voters. */

    // 如果支持领头的投票数量不超过半数
    // 并且支持票数不超过 master 配置指定的投票数量
    // 那么这次领头选举无效
    voters_quorum = voters/2+1;
    if (winner && (max_votes < voters_quorum || max_votes < master->quorum))
        winner = NULL;

    // 返回领头 Sentinel ，或者 NULL
    winner = winner ? sdsnew(winner) : NULL;
    sdsfree(myvote);
    dictRelease(counters);
    return winner;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testb $-0x60, (%rdi)
je 0x5f6b7
movq %rsi, %r15
movq %rdi, %r14
leaq 0x6a699(%rip), %rdi     # 0xc9bb0
xorl %esi, %esi
callq 0x2217b
movq %rax, %rbx
movq 0xc0(%r14), %rdi
callq 0x22a71
movq %rax, %r12
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x5f57f
movl $0x1, %ebp
movq 0x8(%rax), %rax
movq 0x118(%rax), %rsi
testq %rsi, %rsi
je 0x5f56a
movq 0x120(%rax), %rax
cmpq 0x7f388(%rip), %rax     # 0xde8e8
jne 0x5f56a
movq %rbx, %rdi
callq 0x5f488
movq %r12, %rdi
callq 0x22acf
incl %ebp
testq %rax, %rax
jne 0x5f542
shrl %ebp
incl %ebp
jmp 0x5f584
movl $0x1, %ebp
movq %r15, 0x18(%rsp)
movq %r12, %rdi
callq 0x22b86
movq %rbx, %rdi
callq 0x22a71
movq %rax, %r13
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
movq %r14, 0x10(%rsp)
je 0x5f5ea
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
jbe 0x5f5c3
movq (%rax), %r12
movq %rcx, %r15
movq %r13, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x5f5b4
movq %r13, %rdi
callq 0x22b86
testq %r12, %r12
je 0x5f608
movl $0x0, 0xc(%rsp)
movq %r12, %r13
jmp 0x5f61c
movq %r13, %rdi
callq 0x22b86
leaq 0x6a9af(%rip), %r12     # 0xc9fa8
addq $0x58, %r12
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r15d, %r15d
jmp 0x5f619
leaq 0x6a999(%rip), %r12     # 0xc9fa8
addq $0x58, %r12
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
leaq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x5e9a3
movq %rax, %r12
testq %rax, %rax
je 0x5f668
cmpq %r14, 0x20(%rsp)
jne 0x5f668
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5f488
cltq
cmpq %rax, %r15
cmovbeq %rax, %r15
setae %al
cmovbq %r12, %r13
testb %al, 0xc(%rsp)
je 0x5f66f
jmp 0x5f685
cmpb $0x0, 0xc(%rsp)
jne 0x5f685
cmpq %rbp, %r15
jb 0x5f685
movq 0x10(%rsp), %rax
movslq 0xd0(%rax), %rax
cmpq %rax, %r15
jae 0x5f6aa
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x2850d
movq %rbx, %rdi
callq 0x229a9
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x28486
movq %rax, %r14
jmp 0x5f688
leaq 0x40695(%rip), %rdi     # 0x9fd53
leaq 0x3f75f(%rip), %rsi     # 0x9ee24
movl $0xfdf, %edx            # imm = 0xFDF
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0