// File: /workspace/github_c_makefile_star2_O2/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
leaq 0x94395(%rip), %r15     # 0xc6fa8
orq $-0x1, 0x6f0(%r15)
movl %edi, %ebp
movq 0x668(%r15), %rbx
testq %rbx, %rbx
je 0x32c6a
movq 0xb8(%rbx), %rcx
addq $0xd0, %rbx
incq %rcx
leaq 0x60293(%rip), %rdx     # 0x92ed4
movq %rsp, %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1f120
leaq 0x5b4fc(%rip), %rsi     # 0x8e154
pushq $0x2
popq %rdi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x22613
jmp 0x32c8d
leaq 0x5b517(%rip), %rsi     # 0x8e188
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movb $0x0, 0x2(%rsp)
movw $0x312d, (%rsp)         # imm = 0x312D
leaq 0x65055(%rip), %rbx     # 0x97ce2
leaq 0x5b52e(%rip), %rsi     # 0x8e1c2
movq %rsp, %rcx
movl %ebp, %edi
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x32a48
movq %rax, %rbx
leaq 0x5b518(%rip), %rsi     # 0x8e1c8
pushq $0xb
popq %rdx
movq %rax, %rdi
callq 0x1f340
testl %eax, %eax
je 0x32cf6
leaq 0x5b55d(%rip), %rsi     # 0x8e223
pushq $0x9
popq %rdx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x32d24
leaq 0x5b583(%rip), %rsi     # 0x8e25f
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x32d4c
leaq 0x5b572(%rip), %rsi     # 0x8e264
pushq $0x3
jmp 0x32d55
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x1f790
testq %rax, %rax
je 0x32d76
movq %rax, %r14
incq %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x1f790
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %rax, %rdi
jmp 0x32d7b
leaq 0x5b502(%rip), %rsi     # 0x8e22d
xorl %r14d, %r14d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq %rbx, %rdi
callq 0x271f7
movl %ebp, %edi
callq 0x32e79
jmp 0x32e2b
leaq 0x5b53b(%rip), %rsi     # 0x8e28e
pushq $0x2
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22613
pushq $0x2
popq %r14
movq %rbx, %rdi
callq 0x271f7
callq 0x32e39
jmp 0x32e2b
xorl %r14d, %r14d
xorl %edi, %edi
testq %r14, %r14
je 0x32dee
testq %rdi, %rdi
je 0x32dee
movq %r14, %rax
subq %rdi, %rax
cmpq $-0x29, %rax
jne 0x32dee
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)
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x1fa70
movq %rax, 0x6f0(%r15)
leaq 0x5b426(%rip), %rsi     # 0x8e202
pushq $0x2
popq %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x22613
jmp 0x32e1a
leaq 0x5b3df(%rip), %rsi     # 0x8e1d4
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
xorps %xmm0, %xmm0
movups %xmm0, 0x6d9(%r15)
movups %xmm0, 0x6d0(%r15)
movups %xmm0, 0x6c0(%r15)
callq 0x32e39
movq %rbx, %rdi
callq 0x271f7
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq