// File: /workspace/github_c_makefile_star2_O3/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 0x96269(%rip), %r12     # 0xc9fa8
leaq 0x58(%r12), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x1f2e0
testl %eax, %eax
je 0x33d75
cmpb $0x3f, (%r15)
jne 0x33ded
leaq 0x63c53(%rip), %rsi     # 0x979b7
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
jmp 0x33e06
movq 0x10(%r13), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x31355
testl %eax, %eax
jne 0x33e06
cmpq $0x0, 0x600(%r12)
movq 0x8(%rsp), %rdx
je 0x33dba
movq 0x620(%r12), %rax
cmpq %rax, %rdx
jl 0x33dba
addq 0x610(%r12), %rax
cmpq %rax, %rdx
jle 0x33e7c
leaq 0x63c16(%rip), %rsi     # 0x979d7
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq 0x8(%rsp), %rax
cmpq 0x5f0(%r12), %rax
jle 0x33e06
leaq 0x63c4b(%rip), %rsi     # 0x97a2e
movl $0x3, %edi
jmp 0x33d69
leaq 0x63b5c(%rip), %rsi     # 0x97950
movl $0x2, %edi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x23223
movq 0x5f0(%r12), %r8
movq %r8, 0x8(%rsp)
cmpq $0x0, 0x600(%r12)
jne 0x33e26
incq %r8
movq %r8, 0x8(%rsp)
leaq 0x63cd3(%rip), %rdx     # 0x97b00
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
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %r14, %rax
je 0x33e68
movq %rbx, %rdi
callq 0x2e90b
xorl %ebp, %ebp
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 0x204cb
movabsq $0x554e49544e4f432b, %rax # imm = 0x554E49544E4F432B
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl $0xa0d45, 0x8(%rsi)     # imm = 0xA0D45
movl (%rbx), %edi
movl $0xb, %edx
callq 0x1fb30
cmpq $0xb, %rax
jne 0x33e5e
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x33b7c
movq 0x8(%rsp), %rcx
leaq 0x63bab(%rip), %rsi     # 0x97a9b
xorl %ebp, %ebp
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
callq 0x33f0b
jmp 0x33e68