// File: /workspace/github_c_makefile_star2_O1/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 0x3dd83(%rip), %rdx     # 0x9c13d
leaq 0xc(%rsp), %r14
movl $0x1, %ecx
movq %r14, %r8
callq 0x28d54
movq %rax, %rbx
cmpl $0x8, (%r14)
jne 0x5e61f
movq 0x20(%rbx), %rdi
callq 0x5c679
testq %rax, %rax
je 0x5e61f
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 0x5c59e
movq %rax, %r12
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1fa00
movq %rax, %rbp
movq 0x38(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x1fa00
movq %rax, %r13
testq %r12, %r12
jne 0x5e4ed
movq (%rbx), %rsi
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movl %r15d, %edx
callq 0x5c4e4
testl %eax, %eax
je 0x5e494
movq (%rbx), %r8
subq $0x8, %rsp
leaq 0x411ca(%rip), %rsi     # 0x9f63e
leaq 0x411d1(%rip), %rcx     # 0x9f64c
movl $0x2, %edi
movq %r14, %rdx
movl %r15d, %r9d
xorl %eax, %eax
pushq 0x10(%rbx)
callq 0x5b66f
addq $0x10, %rsp
movq (%rbx), %rdx
movl 0xd0(%r14), %r8d
xorl %r12d, %r12d
xorl %edi, %edi
movl $0x4, %esi
movl %r15d, %ecx
movq %r14, %r9
callq 0x5c139
testq %rax, %rax
je 0x5e4ed
movq %rax, %r15
leaq 0x411a7(%rip), %rsi     # 0x9f669
leaq 0x40b35(%rip), %rcx     # 0x9effe
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5b66f
movq 0x10(%rbx), %rdi
callq 0x281da
movq %rax, 0x10(%r15)
callq 0x5cac1
movq %r15, %r12
cmpq 0x803f4(%rip), %rbp     # 0xde8e8
jbe 0x5e526
movq %rbp, 0x803eb(%rip)     # 0xde8e8
callq 0x5cac1
movq 0x803df(%rip), %r8      # 0xde8e8
leaq 0x41163(%rip), %rsi     # 0x9f673
leaq 0x40ead(%rip), %rcx     # 0x9f3c4
movl $0x3, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x5b66f
cmpq %r13, 0x18(%r14)
jae 0x5e610
movq %r13, 0x18(%r14)
movq 0x20(%r14), %rax
movq 0x10(%rsp), %rcx
cmpl 0x8(%rax), %ecx
jne 0x5e556
movq (%rax), %rdi
movq 0x28(%rbx), %rsi
callq 0x1f880
testl %eax, %eax
je 0x5e610
leaq 0x41121(%rip), %rsi     # 0x9f67e
leaq 0x40a9a(%rip), %rcx     # 0x9effe
movl $0x3, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x5b66f
movq 0x8(%r14), %r8
movq 0x20(%r14), %rax
movq (%rax), %r9
movl 0x8(%rax), %r10d
subq $0x8, %rsp
leaq 0x41105(%rip), %rsi     # 0x9f692
leaq 0x4110d(%rip), %rcx     # 0x9f6a1
movl $0x3, %edi
movq %r14, %rdx
xorl %eax, %eax
movq 0x18(%rsp), %rbp
pushq %rbp
pushq 0x28(%rbx)
pushq %r10
callq 0x5b66f
addq $0x20, %rsp
movq 0x20(%r14), %r13
movl $0x10, %edi
callq 0x29709
movq %rax, %r15
movq (%r13), %rdi
callq 0x281da
movq %rax, (%r15)
movl 0x8(%r13), %eax
movl %eax, 0x8(%r15)
movq 0x28(%rbx), %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0x5c8e9
movq 0x20(%r14), %r8
leaq 0x410ef(%rip), %rdx     # 0x9f6df
movq %r14, %rdi
movl $0x40000, %esi          # imm = 0x40000
movq %r15, %rcx
callq 0x5c09a
movq (%r15), %rdi
callq 0x28204
movq %r15, %rdi
callq 0x2984b
testq %r12, %r12
je 0x5e61f
callq 0x232a3
movq %rax, 0x78(%r12)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x28f0a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq