// File: /workspace/github_c_makefile_star2_O2/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
movq %rdi, 0x8(%rsp)
testb $-0x60, (%rdi)
je 0x5a8e6
movq %rsi, %r14
leaq 0x6c410(%rip), %rdi     # 0xc6bb0
xorl %esi, %esi
callq 0x216f0
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0xc0(%rax), %rdi
callq 0x21ef6
movq %rax, %r12
pushq $0x1
popq %rbp
movq %r12, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5a7fa
movq 0x8(%rax), %rax
movq 0x118(%rax), %rsi
testq %rsi, %rsi
je 0x5a7f6
movq 0x120(%rax), %rax
cmpq 0x810fc(%rip), %rax     # 0xdb8e8
jne 0x5a7f6
movq %rbx, %rdi
callq 0x5a713
incl %ebp
jmp 0x5a7c1
movq %r12, %rdi
callq 0x21fe6
movq %rbx, %rdi
callq 0x21ef6
movq %rax, %r13
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5a831
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
jbe 0x5a813
movq (%rax), %r12
movq %rcx, %r15
jmp 0x5a813
movq %r13, %rdi
callq 0x21fe6
leaq 0x6c768(%rip), %rdx     # 0xc6fa8
addq $0x58, %rdx
testq %r12, %r12
cmovneq %r12, %rdx
leaq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x59cc8
movq %rax, %r13
testq %rax, %rax
je 0x5a895
cmpq %r14, 0x10(%rsp)
jne 0x5a895
testq %r12, %r12
setne %r14b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x5a713
cltq
cmpq %rax, %r15
cmovbeq %rax, %r15
setb %al
cmovbq %r13, %r12
orb %r14b, %al
jne 0x5a89a
jmp 0x5a8b4
testq %r12, %r12
je 0x5a8b4
shrl %ebp
incl %ebp
cmpq %rbp, %r15
jb 0x5a8b4
movq 0x8(%rsp), %rax
movslq 0xd0(%rax), %rax
cmpq %rax, %r15
jae 0x5a8d9
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x271f7
movq %rbx, %rdi
callq 0x21e31
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x271cd
movq %rax, %r14
jmp 0x5a8b7
leaq 0x3b3da(%rip), %rdi     # 0x95cc7
leaq 0x3a4a4(%rip), %rsi     # 0x94d98
movl $0xfdf, %edx            # imm = 0xFDF
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0