// File: /workspace/github_c_makefile_star2_O3/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 0x9519b(%rip), %r15     # 0xc9fa8
movq $-0x1, 0x6f0(%r15)
movq 0x668(%r15), %rbx
testq %rbx, %rbx
je 0x34e69
movq 0xb8(%rbx), %rcx
addq $0xd0, %rbx
incq %rcx
leaq 0x680d8(%rip), %rdx     # 0x9cf14
movq %rsp, %r14
movl $0x20, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x1f120
leaq 0x6333b(%rip), %rsi     # 0x98190
movl $0x2, %edi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x23223
jmp 0x34e8e
leaq 0x63354(%rip), %rsi     # 0x981c4
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movb $0x0, 0x2(%rsp)
movw $0x312d, (%rsp)         # imm = 0x312D
leaq 0x6cec4(%rip), %rbx     # 0xa1d52
leaq 0x63369(%rip), %rsi     # 0x981fe
movq %rsp, %rcx
movl %ebp, %edi
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x34c40
movq %rax, %rbx
leaq 0x63353(%rip), %rsi     # 0x98204
movl $0xb, %edx
movq %rax, %rdi
callq 0x1f340
testl %eax, %eax
je 0x34f0b
leaq 0x63396(%rip), %rsi     # 0x9825f
movl $0x9, %edx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x34ff1
leaq 0x633b6(%rip), %rsi     # 0x9829b
movl $0x4, %edx
movq %rbx, %rdi
callq 0x1f340
testl %eax, %eax
je 0x35018
leaq 0x6339f(%rip), %rsi     # 0x982a0
movl $0x3, %edi
jmp 0x35024
movq %rbx, %rdi
movl $0x20, %esi
callq 0x1f790
testq %rax, %rax
je 0x34fae
movq %rax, %r14
incq %r14
movq %r14, %rdi
movl $0x20, %esi
callq 0x1f790
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmovneq %rdi, %rax
je 0x34fae
movq %r14, %rcx
subq %rax, %rcx
cmpq $-0x29, %rcx
jne 0x34fae
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 0x632a4(%rip), %rsi     # 0x9823e
movl $0x2, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x23223
jmp 0x34fdc
leaq 0x6325b(%rip), %rsi     # 0x98210
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
xorps %xmm0, %xmm0
movups %xmm0, 0x6d9(%r15)
movups %xmm0, 0x6d0(%r15)
movups %xmm0, 0x6c0(%r15)
callq 0x3504f
movq %rbx, %rdi
callq 0x2850d
movl $0x1, %r14d
jmp 0x35041
leaq 0x63271(%rip), %rsi     # 0x98269
xorl %r14d, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq %rbx, %rdi
callq 0x2850d
movl %ebp, %edi
callq 0x35094
jmp 0x35041
leaq 0x632ab(%rip), %rsi     # 0x982ca
movl $0x2, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x23223
movq %rbx, %rdi
callq 0x2850d
callq 0x3504f
movl $0x2, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq