// File: /workspace/github_c_makefile_star2_O3/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 0x3e364(%rip), %rdx     # 0x9c17d
leaq 0xc(%rsp), %r14
movl $0x1, %ecx
movq %r14, %r8
callq 0x29056
movq %rax, %rbx
cmpl $0x8, (%r14)
jne 0x5e07e
movq 0x20(%rbx), %rdi
callq 0x5c08f
testq %rax, %rax
je 0x5e07e
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 0x5bfdc
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 0x5df4c
movq (%rbx), %rsi
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movl %r15d, %edx
callq 0x5bf22
testl %eax, %eax
je 0x5def3
movq (%rbx), %r8
subq $0x8, %rsp
leaq 0x417db(%rip), %rsi     # 0x9f6ae
leaq 0x417e2(%rip), %rcx     # 0x9f6bc
movl $0x2, %edi
movq %r14, %rdx
movl %r15d, %r9d
xorl %eax, %eax
pushq 0x10(%rbx)
callq 0x5b0ac
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 0x5bb77
testq %rax, %rax
je 0x5df4c
movq %rax, %r15
leaq 0x417b8(%rip), %rsi     # 0x9f6d9
leaq 0x41146(%rip), %rcx     # 0x9f06e
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5b0ac
movq 0x10(%rbx), %rdi
callq 0x28486
movq %rax, 0x10(%r15)
callq 0x5c4d7
movq %r15, %r12
cmpq 0x80995(%rip), %rbp     # 0xde8e8
jbe 0x5df85
movq %rbp, 0x8098c(%rip)     # 0xde8e8
callq 0x5c4d7
movq 0x80980(%rip), %r8      # 0xde8e8
leaq 0x41774(%rip), %rsi     # 0x9f6e3
leaq 0x414be(%rip), %rcx     # 0x9f434
movl $0x3, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x5b0ac
cmpq %r13, 0x18(%r14)
jae 0x5e06f
movq %r13, 0x18(%r14)
movq 0x20(%r14), %rax
movq 0x10(%rsp), %rcx
cmpl 0x8(%rax), %ecx
jne 0x5dfb5
movq (%rax), %rdi
movq 0x28(%rbx), %rsi
callq 0x1f880
testl %eax, %eax
je 0x5e06f
leaq 0x41732(%rip), %rsi     # 0x9f6ee
leaq 0x410ab(%rip), %rcx     # 0x9f06e
movl $0x3, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x5b0ac
movq 0x8(%r14), %r8
movq 0x20(%r14), %rax
movq (%rax), %r9
movl 0x8(%rax), %r10d
subq $0x8, %rsp
leaq 0x41716(%rip), %rsi     # 0x9f702
leaq 0x4171e(%rip), %rcx     # 0x9f711
movl $0x3, %edi
movq %r14, %rdx
xorl %eax, %eax
movq 0x18(%rsp), %rbp
pushq %rbp
pushq 0x28(%rbx)
pushq %r10
callq 0x5b0ac
addq $0x20, %rsp
movq 0x20(%r14), %r13
movl $0x10, %edi
callq 0x299b5
movq %rax, %r15
movq (%r13), %rdi
callq 0x28486
movq %rax, (%r15)
movl 0x8(%r13), %eax
movl %eax, 0x8(%r15)
movq 0x28(%rbx), %rsi
movq %r14, %rdi
movl %ebp, %edx
callq 0x5c2ff
movq 0x20(%r14), %r8
leaq 0x41700(%rip), %rdx     # 0x9f74f
movq %r14, %rdi
movl $0x40000, %esi          # imm = 0x40000
movq %r15, %rcx
callq 0x5bad8
movq (%r15), %rdi
callq 0x2850d
movq %r15, %rdi
callq 0x29af7
testq %r12, %r12
je 0x5e07e
callq 0x23461
movq %rax, 0x78(%r12)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x2921e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq