// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: masterTryPartialResynchronization
int masterTryPartialResynchronization(redisClient *c) {
    long long psync_offset, psync_len;
    char *master_runid = c->argv[1]->ptr;
    char buf[128];
    int buflen;

    /* Is the runid of this master the same advertised by the wannabe slave
     * via PSYNC? If runid changed this master is a different instance and
     * there is no way to continue. */
    // 检查 master id 是否和 runid 一致，只有一致的情况下才有 PSYNC 的可能
    if (strcasecmp(master_runid, server.runid)) {
        /* Run id "?" is used by slaves that want to force a full resync. */
        // 从服务器提供的 run id 和服务器的 run id 不一致
        if (master_runid[0] != '?') {
            redisLog(REDIS_NOTICE,"Partial resynchronization not accepted: "
                "Runid mismatch (Client asked for runid '%s', my runid is '%s')",
                master_runid, server.runid);
        // 从服务器提供的 run id 为 '?' ，表示强制 FULL RESYNC
        } else {
            redisLog(REDIS_NOTICE,"Full resync requested by slave.");
        }
        // 需要 full resync
        goto need_full_resync;
    }

    /* We still have the data our slave is asking for? */
    // 取出 psync_offset 参数
    if (getLongLongFromObjectOrReply(c,c->argv[2],&psync_offset,NULL) !=
       REDIS_OK) goto need_full_resync;

        // 如果没有 backlog
    if (!server.repl_backlog ||
        // 或者 psync_offset 小于 server.repl_backlog_off
        // （想要恢复的那部分数据已经被覆盖）
        psync_offset < server.repl_backlog_off ||
        // psync offset 大于 backlog 所保存的数据的偏移量
        psync_offset > (server.repl_backlog_off + server.repl_backlog_histlen))
    {
        // 执行 FULL RESYNC
        redisLog(REDIS_NOTICE,
            "Unable to partial resync with the slave for lack of backlog (Slave request was: %lld).", psync_offset);
        if (psync_offset > server.master_repl_offset) {
            redisLog(REDIS_WARNING,
                "Warning: slave tried to PSYNC with an offset that is greater than the master replication offset.");
        }
        goto need_full_resync;
    }

    /* If we reached this point, we are able to perform a partial resync:
     * 程序运行到这里，说明可以执行 partial resync
     *
     * 1) Set client state to make it a slave.
     *    将客户端状态设为 salve  
     *
     * 2) Inform the client we can continue with +CONTINUE
     *    向 slave 发送 +CONTINUE ，表示 partial resync 的请求被接受
     *
     * 3) Send the backlog data (from the offset to the end) to the slave. 
     *    发送 backlog 中，客户端所需要的数据
     */
    c->flags |= REDIS_SLAVE;
    c->replstate = REDIS_REPL_ONLINE;
    c->repl_ack_time = server.unixtime;
    listAddNodeTail(server.slaves,c);
    /* We can't use the connection buffers since they are used to accumulate
     * new commands at this stage. But we are sure the socket send buffer is
     * emtpy so this write will never fail actually. */
    // 向从服务器发送一个同步 +CONTINUE ，表示 PSYNC 可以执行
    buflen = snprintf(buf,sizeof(buf),"+CONTINUE\r\n");
    if (write(c->fd,buf,buflen) != buflen) {
        freeClientAsync(c);
        return REDIS_OK;
    }
    // 发送 backlog 中的内容（也即是从服务器缺失的那些内容）到从服务器
    psync_len = addReplyReplicationBacklog(c,psync_offset);
    redisLog(REDIS_NOTICE,
        "Partial resynchronization request accepted. Sending %lld bytes of backlog starting from offset %lld.", psync_len, psync_offset);
    /* Note that we don't need to set the selected DB at server.slaveseldb
     * to -1 to force the master to emit SELECT, since the slave already
     * has this state from the previous connection with the master. */

    // 刷新低延迟从服务器的数量
    refreshGoodSlavesCount();
    return REDIS_OK; /* The caller can return, no full resync needed. */

need_full_resync:
    /* We need a full resync for some reason... notify the client. */
    // 刷新 psync_offset
    psync_offset = server.master_repl_offset;
    /* Add 1 to psync_offset if it the replication backlog does not exists
     * as when it will be created later we'll increment the offset by one. */
    // 刷新 psync_offset
    if (server.repl_backlog == NULL) psync_offset++;
    /* Again, we can't use the connection buffers (see above). */
    // 发送 +FULLRESYNC ，表示需要完整重同步
    buflen = snprintf(buf,sizeof(buf),"+FULLRESYNC %s %lld\r\n",
                      server.runid,psync_offset);
    if (write(c->fd,buf,buflen) != buflen) {
        freeClientAsync(c);
        return REDIS_OK;
    }
    return REDIS_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %r13
movq 0x8(%r13), %rax
movq 0x8(%rax), %r15
leaq 0x953f5(%rip), %r12     # 0xc6fa8
leaq 0x58(%r12), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1f2e0
testl %eax, %eax
je 0x31be7
cmpb $0x3f, (%r15)
jne 0x31c57
leaq 0x5bda3(%rip), %rsi     # 0x8d97b
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
jmp 0x31c6e
movq 0x10(%r13), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f617
testl %eax, %eax
jne 0x31c6e
cmpq $0x0, 0x600(%r12)
movq 0x8(%rsp), %rdx
je 0x31c2c
movq 0x620(%r12), %rax
cmpq %rax, %rdx
jl 0x31c2c
addq 0x610(%r12), %rax
cmpq %rax, %rdx
jle 0x31ce4
leaq 0x5bd68(%rip), %rsi     # 0x8d99b
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0x8(%rsp), %rax
cmpq 0x5f0(%r12), %rax
jle 0x31c6e
leaq 0x5bd9f(%rip), %rsi     # 0x8d9f2
pushq $0x3
jmp 0x31bda
leaq 0x5bcb6(%rip), %rsi     # 0x8d914
pushq $0x2
popq %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x22613
movq 0x5f0(%r12), %r8
movq %r8, 0x8(%rsp)
cmpq $0x0, 0x600(%r12)
jne 0x31c8e
incq %r8
movq %r8, 0x8(%rsp)
leaq 0x5be2f(%rip), %rdx     # 0x8dac4
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movl $0x80, %esi
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x1f120
movl (%rbx), %edi
movslq %eax, %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x1fb30
cmpq %r14, %rax
jne 0x31cc8
pushq $-0x1
popq %rbp
jmp 0x31cd0
movq %rbx, %rdi
callq 0x2ced9
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb $0x1, 0x90(%rbx)
movl $0x9, 0x98(%rbx)
movq 0x798(%r12), %rax
movq %rax, 0xc8(%rbx)
movq 0x1c0(%r12), %rdi
movq %rbx, %rsi
callq 0x1fe4c
movabsq $0x554e49544e4f432b, %rax # imm = 0x554E49544E4F432B
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl $0xa0d45, 0x8(%rsi)     # imm = 0xA0D45
movl (%rbx), %edi
pushq $0xb
popq %rdx
callq 0x1fb30
cmpq $0xb, %rax
jne 0x31d6f
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x319f7
movq 0x8(%rsp), %rcx
leaq 0x5bd09(%rip), %rsi     # 0x8da5f
xorl %ebp, %ebp
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
callq 0x31d7e
jmp 0x31cd0
movq %rbx, %rdi
callq 0x2ced9
xorl %ebp, %ebp
jmp 0x31cd0