// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: migrateCommand
void migrateCommand(redisClient *c) {
    int fd, copy, replace, j;
    long timeout;
    long dbid;
    long long ttl, expireat;
    robj *o;
    rio cmd, payload;
    int retry_num = 0;

try_again:
    /* Initialization */
    copy = 0;
    replace = 0;
    ttl = 0;

    /* Parse additional options */
    // 读入 COPY 或者 REPLACE 选项
    for (j = 6; j < c->argc; j++) {
        if (!strcasecmp(c->argv[j]->ptr,"copy")) {
            copy = 1;
        } else if (!strcasecmp(c->argv[j]->ptr,"replace")) {
            replace = 1;
        } else {
            addReply(c,shared.syntaxerr);
            return;
        }
    }

    /* Sanity check */
    // 检查输入参数的正确性
    if (getLongFromObjectOrReply(c,c->argv[5],&timeout,NULL) != REDIS_OK)
        return;
    if (getLongFromObjectOrReply(c,c->argv[4],&dbid,NULL) != REDIS_OK)
        return;
    if (timeout <= 0) timeout = 1000;

    /* Check if the key is here. If not we reply with success as there is
     * nothing to migrate (for instance the key expired in the meantime), but
     * we include such information in the reply string. */
    // 取出键的值对象
    if ((o = lookupKeyRead(c->db,c->argv[3])) == NULL) {
        addReplySds(c,sdsnew("+NOKEY\r\n"));
        return;
    }

    /* Connect */
    // 获取套接字连接
    fd = migrateGetSocket(c,c->argv[1],c->argv[2],timeout);
    if (fd == -1) return; /* error sent to the client by migrateGetSocket() */

    /* Create RESTORE payload and generate the protocol to call the command. */
    // 创建用于指定数据库的 SELECT 命令，以免键值对被还原到了错误的地方
    rioInitWithBuffer(&cmd,sdsempty());
    redisAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',2));
    redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"SELECT",6));
    redisAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,dbid));

    // 取出键的过期时间戳
    expireat = getExpire(c->db,c->argv[3]);
    if (expireat != -1) {
        ttl = expireat-mstime();
        if (ttl < 1) ttl = 1;
    }
    redisAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',replace ? 5 : 4));

    // 如果运行在集群模式下，那么发送的命令为 RESTORE-ASKING
    // 如果运行在非集群模式下，那么发送的命令为 RESTORE
    if (server.cluster_enabled)
        redisAssertWithInfo(c,NULL,
            rioWriteBulkString(&cmd,"RESTORE-ASKING",14));
    else
        redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"RESTORE",7));

    // 写入键名和过期时间
    redisAssertWithInfo(c,NULL,sdsEncodedObject(c->argv[3]));
    redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,c->argv[3]->ptr,
            sdslen(c->argv[3]->ptr)));
    redisAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,ttl));

    /* Emit the payload argument, that is the serialized object using
     * the DUMP format. */
    // 将值对象进行序列化
    createDumpPayload(&payload,o);
    // 写入序列化对象
    redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,payload.io.buffer.ptr,
                                sdslen(payload.io.buffer.ptr)));
    sdsfree(payload.io.buffer.ptr);

    /* Add the REPLACE option to the RESTORE command if it was specified
     * as a MIGRATE option. */
    // 是否设置了 REPLACE 命令？
    if (replace)
        // 写入 REPLACE 参数
        redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"REPLACE",7));

    /* Transfer the query to the other node in 64K chunks. */
    // 以 64 kb 每次的大小向对方发送数据
    errno = 0;
    {
        sds buf = cmd.io.buffer.ptr;
        size_t pos = 0, towrite;
        int nwritten = 0;

        while ((towrite = sdslen(buf)-pos) > 0) {
            towrite = (towrite > (64*1024) ? (64*1024) : towrite);
            nwritten = syncWrite(fd,buf+pos,towrite,timeout);
            if (nwritten != (signed)towrite) goto socket_wr_err;
            pos += nwritten;
        }
    }

    /* Read back the reply. */
    // 读取命令的回复
    {
        char buf1[1024];
        char buf2[1024];

        /* Read the two replies */
        if (syncReadLine(fd, buf1, sizeof(buf1), timeout) <= 0)
            goto socket_rd_err;
        if (syncReadLine(fd, buf2, sizeof(buf2), timeout) <= 0)
            goto socket_rd_err;

        // 检查 RESTORE 命令执行是否成功

        if (buf1[0] == '-' || buf2[0] == '-') {

            // 执行出错。。。

            addReplyErrorFormat(c,"Target instance replied with error: %s",
                (buf1[0] == '-') ? buf1+1 : buf2+1);
        } else {

            // 执行成功。。。

            robj *aux;

            // 如果没有指定 COPY 选项，那么删除本机数据库中的键
            if (!copy) {
                /* No COPY option: remove the local key, signal the change. */
                dbDelete(c->db,c->argv[3]);
                signalModifiedKey(c->db,c->argv[3]);
            }
            addReply(c,shared.ok);
            server.dirty++;

            /* Translate MIGRATE as DEL for replication/AOF. */
            // 如果键被删除了的话，向 AOF 文件和从服务器/节点发送一个 DEL 命令
            aux = createStringObject("DEL",3);
            rewriteClientCommandVector(c,2,aux,c->argv[3]);
            decrRefCount(aux);
        }
    }

    sdsfree(cmd.io.buffer.ptr);
    return;

socket_wr_err:
    sdsfree(cmd.io.buffer.ptr);
    migrateCloseSocket(c->argv[1],c->argv[2]);
    if (errno != ETIMEDOUT && retry_num++ == 0) goto try_again;
    addReplySds(c,
        sdsnew("-IOERR error or timeout writing to target instance\r\n"));
    return;

socket_rd_err:
    sdsfree(cmd.io.buffer.ptr);
    migrateCloseSocket(c->argv[1],c->argv[2]);
    if (errno != ETIMEDOUT && retry_num++ == 0) goto try_again;
    addReplySds(c,
        sdsnew("-IOERR error or timeout reading from target node\r\n"));
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8d8, %rsp            # imm = 0x8D8
movq %rdi, %r13
movb $0x1, %al
leaq 0x30(%rsp), %rbx
movq %rdi, 0x20(%rsp)
movl %eax, 0x1c(%rsp)
movslq 0x30(%r13), %rbp
movq 0x38(%r13), %r12
movb $0x1, 0xe(%rsp)
movb $0x1, %r15b
cmpq $0x7, %rbp
jl 0x556c2
xorl %r15d, %r15d
movl $0x6, %r13d
xorl %ebx, %ebx
movq (%r12,%r13,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x47a8a(%rip), %rsi     # 0x9d100
callq 0x1f2e0
testl %eax, %eax
je 0x5569c
movq %r14, %rdi
leaq 0x479c0(%rip), %rsi     # 0x9d049
callq 0x1f2e0
movl $0x1, %ebx
testl %eax, %eax
je 0x556a2
jmp 0x559fe
movl $0x1, %r15d
incq %r13
cmpq %r13, %rbp
jne 0x55664
testl %ebx, %ebx
sete 0xe(%rsp)
testl %r15d, %r15d
sete %r15b
movq 0x20(%rsp), %r13
leaq 0x30(%rsp), %rbx
movq 0x28(%r12), %rsi
movq %r13, %rdi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x55a16
movq 0x38(%r13), %rax
movq 0x20(%rax), %rsi
movq %r13, %rdi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x55a16
cmpq $0x0, 0x10(%rsp)
jg 0x5570e
movq $0x3e8, 0x10(%rsp)      # imm = 0x3E8
movq 0x8(%r13), %rdi
movq 0x38(%r13), %rax
movq 0x18(%rax), %rsi
callq 0x3163a
testq %rax, %rax
je 0x55a28
movq %rax, %r14
movq 0x38(%r13), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x10(%rsp), %rcx
movq %r13, %rdi
callq 0x55355
cmpl $-0x1, %eax
je 0x55a16
movl %eax, %ebp
callq 0x2845e
movq %rbx, %rdi
movq %rax, %rsi
callq 0x588b3
movq %rbx, %rdi
movl $0x2a, %esi
movl $0x2, %edx
callq 0x5891c
testq %rax, %rax
je 0x55b22
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x479a3(%rip), %rsi     # 0x9d12c
callq 0x589bc
testq %rax, %rax
je 0x55bd6
movq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x58ab3
testq %rax, %rax
je 0x55bb8
movq 0x8(%r13), %rdi
movq 0x38(%r13), %rax
movq 0x18(%rax), %rsi
callq 0x329dd
cmpq $-0x1, %rax
je 0x557de
movq %rax, %r12
callq 0x23461
subq %rax, %r12
cmpq $0x2, %r12
movl $0x1, %eax
cmovlq %rax, %r12
jmp 0x557e1
xorl %r12d, %r12d
movzbl 0xe(%rsp), %edx
xorl $0x5, %edx
movq %rbx, %rdi
movl $0x2a, %esi
callq 0x5891c
testq %rax, %rax
je 0x55b9a
leaq 0x747a2(%rip), %rax     # 0xc9fa8
cmpl $0x0, 0x7bc(%rax)
je 0x5582d
movl $0xe, %edx
movq %rbx, %rdi
leaq 0x47985(%rip), %rsi     # 0x9d1a3
callq 0x589bc
testq %rax, %rax
jne 0x5584a
jmp 0x55bf1
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x479a3(%rip), %rsi     # 0x9d1df
callq 0x589bc
testq %rax, %rax
je 0x55c42
movq 0x38(%r13), %rax
movq 0x18(%rax), %rax
movl (%rax), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x55c0c
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
movq %rbx, %rdi
callq 0x589bc
testq %rax, %rax
je 0x55b7c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x58ab3
testq %rax, %rax
je 0x55b5e
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x54ffc
movq 0xb8(%rsp), %rsi
movslq -0x8(%rsi), %rdx
movq %rbx, %rdi
callq 0x589bc
testq %rax, %rax
je 0x55b40
movb %r15b, 0xf(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x2850d
cmpb $0x0, 0xe(%rsp)
jne 0x558f0
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x479d9(%rip), %rsi     # 0x9d2bb
callq 0x589bc
testq %rax, %rax
je 0x55c27
callq 0x1f810
movq %rax, %r14
movl $0x0, (%rax)
movq 0x68(%rsp), %r15
movslq -0x8(%r15), %r12
testq %r12, %r12
je 0x55947
xorl %ebx, %ebx
subq %rbx, %r12
movl $0x10000, %eax          # imm = 0x10000
cmpq %rax, %r12
cmovaeq %rax, %r12
leaq (%r15,%rbx), %rsi
movq 0x10(%rsp), %rcx
movl %ebp, %edi
movq %r12, %rdx
callq 0x4f4d0
cmpl %r12d, %eax
jne 0x559bf
cltq
addq %rax, %rbx
movslq -0x8(%r15), %r12
cmpq %r12, %rbx
jne 0x5590e
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
leaq 0x4d0(%rsp), %rsi
callq 0x4f62f
testq %rax, %rax
jle 0x55987
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %ebp, %edi
leaq 0xd0(%rsp), %rsi
callq 0x4f62f
testq %rax, %rax
jg 0x55a4a
movq 0x68(%rsp), %rdi
callq 0x2850d
movq 0x38(%r13), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x55507
cmpl $0x6e, (%r14)
setne %al
testb %al, 0x1c(%rsp)
movl $0x0, %eax
leaq 0x30(%rsp), %rbx
jne 0x5563f
jmp 0x55a31
movq 0x68(%rsp), %rdi
callq 0x2850d
movq 0x38(%r13), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x55507
cmpl $0x6e, (%r14)
setne %al
testb %al, 0x1c(%rsp)
movl $0x0, %eax
leaq 0x30(%rsp), %rbx
jne 0x5563f
leaq 0x47913(%rip), %rdi     # 0x9d30f
jmp 0x55a38
leaq 0x74dfb(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq 0x20(%rsp), %rdi
callq 0x2d97c
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x476d6(%rip), %rdi     # 0x9d105
jmp 0x55a38
leaq 0x4790c(%rip), %rdi     # 0x9d344
callq 0x28486
movq %r13, %rdi
movq %rax, %rsi
callq 0x2db20
jmp 0x55a16
movb 0x4d0(%rsp), %al
cmpb $0x2d, %al
je 0x55ae9
cmpb $0x2d, 0xd0(%rsp)
je 0x55ae9
cmpb $0x0, 0xf(%rsp)
je 0x55a90
movq 0x8(%r13), %rdi
movq 0x38(%r13), %rax
movq 0x18(%rax), %rsi
callq 0x31a36
movq 0x8(%r13), %rdi
movq 0x38(%r13), %rax
movq 0x18(%rax), %rsi
callq 0x319b8
leaq 0x74d69(%rip), %rax     # 0xca800
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x2d97c
leaq 0x744fe(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
leaq 0x3f265(%rip), %rdi     # 0x94d1d
movl $0x3, %esi
callq 0x3068f
movq %rax, %r14
movq 0x38(%r13), %rax
movq 0x18(%rax), %rcx
movq %r13, %rdi
movl $0x2, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2f870
movq %r14, %rdi
callq 0x30b0c
jmp 0x55b13
cmpb $0x2d, %al
leaq 0xd0(%rsp), %rdx
leaq 0x4d0(%rsp), %rax
cmoveq %rax, %rdx
incq %rdx
leaq 0x477df(%rip), %rsi     # 0x9d2e8
movq %r13, %rdi
xorl %eax, %eax
callq 0x2dc8a
movq 0x68(%rsp), %rdi
callq 0x2850d
jmp 0x55a16
leaq 0x475e5(%rip), %rdx     # 0x9d10e
leaq 0x4618d(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1528, %r8d           # imm = 0x1528
jmp 0x55c5b
leaq 0x47726(%rip), %rdx     # 0x9d26d
leaq 0x4616f(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1548, %r8d           # imm = 0x1548
jmp 0x55c5b
leaq 0x476e9(%rip), %rdx     # 0x9d24e
leaq 0x46151(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1540, %r8d           # imm = 0x1540
jmp 0x55c5b
leaq 0x47689(%rip), %rdx     # 0x9d20c
leaq 0x46133(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x153f, %r8d           # imm = 0x153F
jmp 0x55c5b
leaq 0x475d6(%rip), %rdx     # 0x9d177
leaq 0x46115(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1532, %r8d           # imm = 0x1532
jmp 0x55c5b
leaq 0x47598(%rip), %rdx     # 0x9d157
leaq 0x460f7(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x152a, %r8d           # imm = 0x152A
jmp 0x55c5b
leaq 0x47556(%rip), %rdx     # 0x9d133
leaq 0x460d9(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1529, %r8d           # imm = 0x1529
jmp 0x55c5b
leaq 0x475ba(%rip), %rdx     # 0x9d1b2
leaq 0x460be(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1538, %r8d           # imm = 0x1538
jmp 0x55c5b
leaq 0x445a8(%rip), %rdx     # 0x9a1bb
leaq 0x460a3(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x153d, %r8d           # imm = 0x153D
jmp 0x55c5b
leaq 0x47695(%rip), %rdx     # 0x9d2c3
leaq 0x46088(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x1550, %r8d           # imm = 0x1550
jmp 0x55c5b
leaq 0x4759e(%rip), %rdx     # 0x9d1e7
leaq 0x4606d(%rip), %rcx     # 0x9bcbd
movq %r13, %rdi
xorl %esi, %esi
movl $0x153a, %r8d           # imm = 0x153A
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0