// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: slaveTryPartialResynchronization
int slaveTryPartialResynchronization(int fd) {
    char *psync_runid;
    char psync_offset[32];
    sds reply;

    /* Initially set repl_master_initial_offset to -1 to mark the current
     * master run_id and offset as not valid. Later if we'll be able to do
     * a FULL resync using the PSYNC command we'll set the offset at the
     * right value, so that this information will be propagated to the
     * client structure representing the master into server.master. */
    server.repl_master_initial_offset = -1;

    if (server.cached_master) {
        // 缓存存在，尝试部分重同步
        // 命令为 "PSYNC <master_run_id> <repl_offset>"
        psync_runid = server.cached_master->replrunid;
        snprintf(psync_offset,sizeof(psync_offset),"%lld", server.cached_master->reploff+1);
        redisLog(REDIS_NOTICE,"Trying a partial resynchronization (request %s:%s).", psync_runid, psync_offset);
    } else {
        // 缓存不存在
        // 发送 "PSYNC ? -1" ，要求完整重同步
        redisLog(REDIS_NOTICE,"Partial resynchronization not possible (no cached master)");
        psync_runid = "?";
        memcpy(psync_offset,"-1",3);
    }

    /* Issue the PSYNC command */
    // 向主服务器发送 PSYNC 命令
    reply = sendSynchronousCommand(fd,"PSYNC",psync_runid,psync_offset,NULL);

    // 接收到 FULLRESYNC ，进行 full-resync
    if (!strncmp(reply,"+FULLRESYNC",11)) {
        char *runid = NULL, *offset = NULL;

        /* FULL RESYNC, parse the reply in order to extract the run id
         * and the replication offset. */
        // 分析并记录主服务器的 run id
        runid = strchr(reply,' ');
        if (runid) {
            runid++;
            offset = strchr(runid,' ');
            if (offset) offset++;
        }
        // 检查 run id 的合法性
        if (!runid || !offset || (offset-runid-1) != REDIS_RUN_ID_SIZE) {
            redisLog(REDIS_WARNING,
                "Master replied with wrong +FULLRESYNC syntax.");
            /* This is an unexpected condition, actually the +FULLRESYNC
             * reply means that the master supports PSYNC, but the reply
             * format seems wrong. To stay safe we blank the master
             * runid to make sure next PSYNCs will fail. */
            // 主服务器支持 PSYNC ，但是却发来了异常的 run id
            // 只好将 run id 设为 0 ，让下次 PSYNC 时失败
            memset(server.repl_master_runid,0,REDIS_RUN_ID_SIZE+1);
        } else {
            // 保存 run id
            memcpy(server.repl_master_runid, runid, offset-runid-1);
            server.repl_master_runid[REDIS_RUN_ID_SIZE] = '\0';
            // 以及 initial offset
            server.repl_master_initial_offset = strtoll(offset,NULL,10);
            // 打印日志，这是一个 FULL resync
            redisLog(REDIS_NOTICE,"Full resync from master: %s:%lld",
                server.repl_master_runid,
                server.repl_master_initial_offset);
        }
        /* We are going to full resync, discard the cached master structure. */
        // 要开始完整重同步，缓存中的 master 已经没用了，清除它
        replicationDiscardCachedMaster();
        sdsfree(reply);
        
        // 返回状态
        return PSYNC_FULLRESYNC;
    }

    // 接收到 CONTINUE ，进行 partial resync
    if (!strncmp(reply,"+CONTINUE",9)) {
        /* Partial resync was accepted, set the replication state accordingly */
        redisLog(REDIS_NOTICE,
            "Successful partial resynchronization with master.");
        sdsfree(reply);
        // 将缓存中的 master 设为当前 master
        replicationResurrectCachedMaster(fd);

        // 返回状态
        return PSYNC_CONTINUE;
    }

    /* If we reach this point we receied either an error since the master does
     * not understand PSYNC, or an unexpected reply from the master.
     * Return PSYNC_NOT_SUPPORTED to the caller in both cases. */

    // 接收到错误？
    if (strncmp(reply,"-ERR",4)) {
        /* If it's not an error, log the unexpected event. */
        redisLog(REDIS_WARNING,
            "Unexpected reply to PSYNC from master: %s", reply);
    } else {
        redisLog(REDIS_NOTICE,
            "Master does not support PSYNC or is in "
            "error state (reply: %s)", reply);
    }
    sdsfree(reply);
    replicationDiscardCachedMaster();

    // 主服务器不支持 PSYNC
    return PSYNC_NOT_SUPPORTED;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %edi, %ebp
leaq 0x955df(%rip), %r15     # 0xc9fa8
movq $-0x1, 0x6f0(%r15)
movq 0x668(%r15), %rbx
testq %rbx, %rbx
je 0x34a25
movq 0xb8(%rbx), %rcx
addq $0xd0, %rbx
incq %rcx
leaq 0x684dc(%rip), %rdx     # 0x9ced4
movq %rsp, %r14
movl $0x20, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1f120
leaq 0x6370f(%rip), %rsi     # 0x98120
movl $0x2, %edi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x23064
jmp 0x34a4a
leaq 0x63728(%rip), %rsi     # 0x98154
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movb $0x0, 0x2(%rsp)
movw $0x312d, (%rsp)         # imm = 0x312D
leaq 0x6d278(%rip), %rbx     # 0xa1cc2
leaq 0x6373d(%rip), %rsi     # 0x9818e
movq %rsp, %rcx
movl %ebp, %edi
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x347fc
movq %rax, %rbx
leaq 0x63727(%rip), %rsi     # 0x98194
movl $0xb, %edx
movq %rax, %rdi
callq 0x1f340
testl %eax, %eax
je 0x34abc
leaq 0x6376a(%rip), %rsi     # 0x981ef
movl $0x9, %edx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x34aee
leaq 0x6378e(%rip), %rsi     # 0x9822b
movl $0x4, %edx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x34b18
leaq 0x6377b(%rip), %rsi     # 0x98230
movl $0x3, %edi
jmp 0x34b24
movq %rbx, %rdi
movl $0x20, %esi
callq 0x1f790
testq %rax, %rax
je 0x34b46
movq %rax, %r14
incq %r14
movq %r14, %rdi
movl $0x20, %esi
callq 0x1f790
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %rax, %rdi
jmp 0x34b4b
leaq 0x63704(%rip), %rsi     # 0x981f9
xorl %r14d, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movq %rbx, %rdi
callq 0x28204
movl %ebp, %edi
callq 0x34c56
jmp 0x34c03
leaq 0x6373b(%rip), %rsi     # 0x9825a
movl $0x2, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x23064
movq %rbx, %rdi
callq 0x28204
callq 0x34c11
movl $0x2, %r14d
jmp 0x34c03
xorl %r14d, %r14d
xorl %edi, %edi
testq %r14, %r14
je 0x34bc2
testq %rdi, %rdi
je 0x34bc2
movq %r14, %rax
subq %rdi, %rax
cmpq $-0x29, %rax
jne 0x34bc2
movq 0x20(%r14), %rax
movq %rax, 0x6e0(%r15)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x6d0(%r15)
movups %xmm0, 0x6c0(%r15)
leaq 0x6c0(%r15), %r14
movb $0x0, 0x6e8(%r15)
xorl %esi, %esi
movl $0xa, %edx
callq 0x1fa70
movq %rax, 0x6f0(%r15)
leaq 0x63620(%rip), %rsi     # 0x981ce
movl $0x2, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23064
jmp 0x34bf0
leaq 0x635d7(%rip), %rsi     # 0x981a0
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
xorps %xmm0, %xmm0
movups %xmm0, 0x6d9(%r15)
movups %xmm0, 0x6d0(%r15)
movups %xmm0, 0x6c0(%r15)
callq 0x34c11
movq %rbx, %rdi
callq 0x28204
movl $0x1, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq