// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelProcessHelloMessage
void sentinelProcessHelloMessage(char *hello, int hello_len) {
    /* Format is composed of 8 tokens:
     * 0=ip,1=port,2=runid,3=current_epoch,4=master_name,
     * 5=master_ip,6=master_port,7=master_config_epoch. */
    int numtokens, port, removed, master_port;
    uint64_t current_epoch, master_config_epoch;
    char **token = sdssplitlen(hello, hello_len, ",", 1, &numtokens);
    sentinelRedisInstance *si, *master;

    if (numtokens == 8) {
        /* Obtain a reference to the master this hello message is about */
        // 获取主服务器的名字，并丢弃和未知主服务器相关的消息。
        master = sentinelGetMasterByName(token[4]);
        if (!master) goto cleanup; /* Unknown master, skip the message. */

        /* First, try to see if we already have this sentinel. */
        // 看这个 Sentinel 是否已经认识发送消息的 Sentinel
        port = atoi(token[1]);
        master_port = atoi(token[6]);
        si = getSentinelRedisInstanceByAddrAndRunID(
                        master->sentinels,token[0],port,token[2]);
        current_epoch = strtoull(token[3],NULL,10);
        master_config_epoch = strtoull(token[7],NULL,10);

        if (!si) {

            // 这个 Sentinel 不认识发送消息的 Sentinel 
            // 将对方加入到 Sentinel 列表中

            /* If not, remove all the sentinels that have the same runid
             * OR the same ip/port, because it's either a restart or a
             * network topology change. */
            removed = removeMatchingSentinelsFromMaster(master,token[0],port,
                            token[2]);
            if (removed) {
                sentinelEvent(REDIS_NOTICE,"-dup-sentinel",master,
                    "%@ #duplicate of %s:%d or %s",
                    token[0],port,token[2]);
            }

            /* Add the new sentinel. */
            si = createSentinelRedisInstance(NULL,SRI_SENTINEL,
                            token[0],port,master->quorum,master);
            if (si) {
                sentinelEvent(REDIS_NOTICE,"+sentinel",si,"%@");
                /* The runid is NULL after a new instance creation and
                 * for Sentinels we don't have a later chance to fill it,
                 * so do it now. */
                si->runid = sdsnew(token[2]);
                sentinelFlushConfig();
            }
        }

        /* Update local current_epoch if received current_epoch is greater.*/
        // 如果消息中记录的纪元比 Sentinel 当前的纪元要高，那么更新纪元
        if (current_epoch > sentinel.current_epoch) {
            sentinel.current_epoch = current_epoch;
            sentinelFlushConfig();
            sentinelEvent(REDIS_WARNING,"+new-epoch",master,"%llu",
                (unsigned long long) sentinel.current_epoch);
        }

        /* Update master info if received configuration is newer. */
        // 如果消息中记录的配置信息更新，那么对主服务器的信息进行更新
        if (master->config_epoch < master_config_epoch) {
            master->config_epoch = master_config_epoch;
            if (master_port != master->addr->port ||
                strcmp(master->addr->ip, token[5]))
            {
                sentinelAddr *old_addr;

                sentinelEvent(REDIS_WARNING,"+config-update-from",si,"%@");
                sentinelEvent(REDIS_WARNING,"+switch-master",
                    master,"%s %s %d %s %d",
                    master->name,
                    master->addr->ip, master->addr->port,
                    token[5], master_port);

                old_addr = dupSentinelAddr(master->addr);
                sentinelResetMasterAndChangeAddress(master, token[5], master_port);
                sentinelCallClientReconfScript(master,
                    SENTINEL_OBSERVER,"start",
                    old_addr,master->addr);
                releaseSentinelAddr(old_addr);
            }
        }

        /* Update the state of the Sentinel. */
        // 更新我方 Sentinel 记录的对方 Sentinel 的信息。
        if (si) si->last_hello_time = mstime();
    }

cleanup:
    sdsfreesplitres(token,numtokens);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x38f98(%rip), %rdx     # 0x9213d
pushq $0x1
popq %rcx
leaq 0xc(%rsp), %r14
movq %r14, %r8
callq 0x27b60
movq %rax, %rbx
cmpl $0x8, (%r14)
jne 0x593dc
movq 0x20(%rbx), %rdi
callq 0x5764f
testq %rax, %rax
je 0x593dc
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x1f710
movl %eax, %r15d
movq 0x30(%rbx), %rdi
callq 0x1f710
movq %rax, 0x10(%rsp)
movq 0xc0(%r14), %rdi
movq (%rbx), %rsi
movq 0x10(%rbx), %rcx
movl %r15d, %edx
callq 0x575a1
movq %rax, %r12
movq 0x18(%rbx), %rdi
pushq $0xa
popq %r13
xorl %esi, %esi
movl %r13d, %edx
callq 0x1fa00
movq %rax, %rbp
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movl %r13d, %edx
callq 0x1fa00
movq %rax, %r13
testq %r12, %r12
jne 0x592ce
movq (%rbx), %rsi
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movl %r15d, %edx
callq 0x574ee
testl %eax, %eax
je 0x59279
movq (%rbx), %r8
subq $0x8, %rsp
leaq 0x3c3c5(%rip), %rsi     # 0x95622
leaq 0x3c3cc(%rip), %rcx     # 0x95630
pushq $0x2
popq %rdi
movq %r14, %rdx
movl %r15d, %r9d
xorl %eax, %eax
pushq 0x10(%rbx)
callq 0x5678f
popq %rax
popq %rcx
movq (%rbx), %rdx
movl 0xd0(%r14), %r8d
xorl %r12d, %r12d
pushq $0x4
popq %rsi
xorl %edi, %edi
movl %r15d, %ecx
movq %r14, %r9
callq 0x5715c
testq %rax, %rax
je 0x592ce
movq %rax, %r15
leaq 0x3c3a8(%rip), %rsi     # 0x9564d
leaq 0x3bd36(%rip), %rcx     # 0x94fe2
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5678f
movq 0x10(%rbx), %rdi
callq 0x271cd
movq %rax, 0x10(%r15)
callq 0x57a25
movq %r15, %r12
cmpq 0x82613(%rip), %rbp     # 0xdb8e8
jbe 0x59305
movq %rbp, 0x8260a(%rip)     # 0xdb8e8
callq 0x57a25
movq 0x825fe(%rip), %r8      # 0xdb8e8
leaq 0x3c366(%rip), %rsi     # 0x95657
leaq 0x3c0b0(%rip), %rcx     # 0x953a8
pushq $0x3
popq %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x5678f
cmpq %r13, 0x18(%r14)
jae 0x593cd
movq %r13, 0x18(%r14)
movq 0x20(%r14), %rax
movq 0x10(%rsp), %rcx
cmpl 0x8(%rax), %ecx
jne 0x59335
movq (%rax), %rdi
movq 0x28(%rbx), %rsi
callq 0x1f880
testl %eax, %eax
je 0x593cd
leaq 0x3c326(%rip), %rsi     # 0x95662
leaq 0x3bc9f(%rip), %rcx     # 0x94fe2
pushq $0x3
popq %rbp
movl %ebp, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x5678f
movq 0x8(%r14), %r8
movq 0x20(%r14), %rax
movq (%rax), %r9
movl 0x8(%rax), %r10d
subq $0x8, %rsp
leaq 0x3c30a(%rip), %rsi     # 0x95676
leaq 0x3c312(%rip), %rcx     # 0x95685
movl %ebp, %edi
movq %r14, %rdx
xorl %eax, %eax
movq 0x18(%rsp), %r13
pushq %r13
pushq 0x28(%rbx)
pushq %r10
callq 0x5678f
addq $0x20, %rsp
movq 0x20(%r14), %rdi
callq 0x56727
movq %rax, %r15
movq 0x28(%rbx), %rsi
movq %r14, %rdi
movl %r13d, %edx
callq 0x5789f
movq 0x20(%r14), %r8
leaq 0x3c30e(%rip), %rdx     # 0x956c3
movq %r14, %rdi
movl $0x40000, %esi          # imm = 0x40000
movq %r15, %rcx
callq 0x570b0
movq %r15, %rdi
callq 0x56756
testq %r12, %r12
je 0x593dc
callq 0x22843
movq %rax, 0x78(%r12)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x27d1e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq