// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
testb $-0x60, (%rdi)
je 0x5fc3e
movq %rsi, %r15
movq %rdi, %r14
leaq 0x6a0d9(%rip), %rdi     # 0xc9bb0
xorl %esi, %esi
callq 0x21f97
movq %rax, %rbx
movq %r14, 0x8(%rsp)
movq 0xc0(%r14), %rdi
callq 0x228b5
movq %rax, %r12
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
je 0x5fb44
movl $0x1, %ebp
movq 0x8(%rax), %rax
movq 0x118(%rax), %rsi
testq %rsi, %rsi
je 0x5fb2f
movq 0x120(%rax), %rax
cmpq 0x7edc3(%rip), %rax     # 0xde8e8
jne 0x5fb2f
movq %rbx, %rdi
callq 0x5fa48
movq %r12, %rdi
callq 0x22913
incl %ebp
testq %rax, %rax
jne 0x5fb07
shrl %ebp
incl %ebp
jmp 0x5fb49
movl $0x1, %ebp
movq %r15, %r14
movq %r12, %rdi
callq 0x229b9
movq %rbx, %rdi
callq 0x228b5
movq %rax, %r13
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
je 0x5fb90
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
jbe 0x5fb81
movq (%rax), %r12
movq %rcx, %r15
movq %r13, %rdi
callq 0x22913
testq %rax, %rax
jne 0x5fb72
jmp 0x5fb96
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x229b9
leaq 0x6a403(%rip), %rdx     # 0xc9fa8
addq $0x58, %rdx
testq %r12, %r12
cmovneq %r12, %rdx
leaq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x5ef1f
movq %rax, %r13
testq %rax, %rax
je 0x5fbe9
cmpq %r14, 0x10(%rsp)
jne 0x5fbe9
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5fa48
cltq
cmpq %rax, %r15
cmovbq %r13, %r12
cmovbeq %rax, %r15
testq %r12, %r12
je 0x5fc04
cmpq %rbp, %r15
jb 0x5fc04
movq 0x8(%rsp), %rax
movslq 0xd0(%rax), %rax
cmpq %rax, %r15
jae 0x5fc07
xorl %r12d, %r12d
testq %r12, %r12
je 0x5fc19
movq %r12, %rdi
callq 0x281da
movq %rax, %r14
jmp 0x5fc1c
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x28204
movq %rbx, %rdi
callq 0x227ed
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4009e(%rip), %rdi     # 0x9fce3
leaq 0x3f168(%rip), %rsi     # 0x9edb4
movl $0xfdf, %edx            # imm = 0xFDF
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0