// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0xd3f6a(%rip), %rsi     # 0x1138f8
addq $0x58, %rsi
callq 0x1f2f0
cmpl $0x0, %eax
je 0x3f9e4
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3f, %eax
je 0x3f9cc
movq -0x28(%rbp), %rdx
movl $0x2, %edi
leaq 0xa2064(%rip), %rsi     # 0xe1a1c
leaq 0xd3f39(%rip), %rcx     # 0x1138f8
addq $0x58, %rcx
movb $0x0, %al
callq 0x24fb0
jmp 0x3f9df
movl $0x2, %edi
leaq 0xa20ab(%rip), %rsi     # 0xe1a83
movb $0x0, %al
callq 0x24fb0
jmp 0x3fb80
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x18(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b8e0
cmpl $0x0, %eax
je 0x3fa0b
jmp 0x3fb80
leaq 0xd3ee6(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
je 0x3fa55
movq -0x18(%rbp), %rax
leaq 0xd3ed1(%rip), %rcx     # 0x1138f8
cmpq 0x620(%rcx), %rax
jl 0x3fa55
movq -0x18(%rbp), %rax
leaq 0xd3ebd(%rip), %rcx     # 0x1138f8
movq 0x620(%rcx), %rcx
leaq 0xd3eaf(%rip), %rdx     # 0x1138f8
addq 0x610(%rdx), %rcx
cmpq %rcx, %rax
jle 0x3fa98
movq -0x18(%rbp), %rdx
movl $0x2, %edi
leaq 0xa203e(%rip), %rsi     # 0xe1aa3
movb $0x0, %al
callq 0x24fb0
movq -0x18(%rbp), %rax
leaq 0xd3e81(%rip), %rcx     # 0x1138f8
cmpq 0x5f0(%rcx), %rax
jle 0x3fa93
movl $0x3, %edi
leaq 0xa206e(%rip), %rsi     # 0xe1afa
movb $0x0, %al
callq 0x24fb0
jmp 0x3fb80
movq -0x10(%rbp), %rax
movl 0x90(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x90(%rax)
movq -0x10(%rbp), %rax
movl $0x9, 0x98(%rax)
leaq 0xd3e38(%rip), %rax     # 0x1138f8
movq 0x798(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc8(%rax)
leaq 0xd3e1f(%rip), %rax     # 0x1138f8
movq 0x1c0(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x1ff90
leaq -0xb0(%rbp), %rdi
movl $0x80, %esi
leaq 0xa205f(%rip), %rdx     # 0xe1b5b
movb $0x0, %al
callq 0x1f120
movl %eax, -0xb4(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0xb0(%rbp), %rsi
movslq -0xb4(%rbp), %rdx
callq 0x1fb50
movslq -0xb4(%rbp), %rcx
cmpq %rcx, %rax
je 0x3fb43
movq -0x10(%rbp), %rdi
callq 0x37000
movl $0x0, -0x4(%rbp)
jmp 0x3fc1c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3f700
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x2, %edi
leaq 0xa1fff(%rip), %rsi     # 0xe1b67
movb $0x0, %al
callq 0x24fb0
callq 0x3fc30
movl $0x0, -0x4(%rbp)
jmp 0x3fc1c
leaq 0xd3d71(%rip), %rax     # 0x1138f8
movq 0x5f0(%rax), %rax
movq %rax, -0x18(%rbp)
leaq 0xd3d5f(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
jne 0x3fbaf
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
leaq -0xb0(%rbp), %rdi
movq -0x18(%rbp), %r8
movl $0x80, %esi
leaq 0xa2006(%rip), %rdx     # 0xe1bcc
leaq 0xd3d2b(%rip), %rcx     # 0x1138f8
addq $0x58, %rcx
movb $0x0, %al
callq 0x1f120
movl %eax, -0xb4(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0xb0(%rbp), %rsi
movslq -0xb4(%rbp), %rdx
callq 0x1fb50
movslq -0xb4(%rbp), %rcx
cmpq %rcx, %rax
je 0x3fc15
movq -0x10(%rbp), %rdi
callq 0x37000
movl $0x0, -0x4(%rbp)
jmp 0x3fc1c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)