// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq $0x0, 0x20(%rsp)
leaq 0x30(%rsp), %r14
movq %rdi, 0x18(%rsp)
movslq 0x30(%rbx), %r12
movb $0x1, %bpl
movb $0x1, %r15b
cmpq $0x7, %r12
jl 0x55c57
movq 0x38(%rbx), %r14
xorl %r13d, %r13d
movl $0x6, %ebp
xorl %ebx, %ebx
movq (%r14,%rbp,8), %rax
movq 0x8(%rax), %r15
movq %r15, %rdi
leaq 0x474b4(%rip), %rsi     # 0x9d0c0
callq 0x1f2e0
testl %eax, %eax
je 0x55c32
movq %r15, %rdi
leaq 0x473ea(%rip), %rsi     # 0x9d009
callq 0x1f2e0
movl $0x1, %ebx
testl %eax, %eax
je 0x55c38
jmp 0x560b3
movl $0x1, %r13d
incq %rbp
cmpq %rbp, %r12
jne 0x55bfa
testl %ebx, %ebx
sete %bpl
testl %r13d, %r13d
sete %r15b
movq 0x18(%rsp), %rbx
leaq 0x30(%rsp), %r14
movq 0x38(%rbx), %rax
movq 0x28(%rax), %rsi
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x560cb
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rsi
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x560cb
cmpq $0x0, 0x10(%rsp)
jg 0x55ca6
movq $0x3e8, 0x10(%rsp)      # imm = 0x3E8
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x3136a
testq %rax, %rax
je 0x560ed
movq %rax, %r12
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %rdi
callq 0x558eb
cmpl $-0x1, %eax
je 0x560cb
movl %eax, %r13d
callq 0x281b2
movq %r14, %rdi
movq %rax, %rsi
callq 0x58e45
movq %r14, %rdi
movl $0x2a, %esi
movl $0x2, %edx
callq 0x58eae
testq %rax, %rax
je 0x56118
movl $0x6, %edx
movq %r14, %rdi
leaq 0x473ca(%rip), %rsi     # 0x9d0ec
callq 0x58f52
testq %rax, %rax
je 0x561cc
movb %r15b, 0xf(%rsp)
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x59069
testq %rax, %rax
je 0x561ae
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x326b1
cmpq $-0x1, %rax
je 0x55d7c
movq %rax, %r15
callq 0x232a3
subq %rax, %r15
cmpq $0x2, %r15
movl $0x1, %eax
cmovlq %rax, %r15
jmp 0x55d7f
xorl %r15d, %r15d
movzbl %bpl, %edx
xorl $0x5, %edx
movq %r14, %rdi
movl $0x2a, %esi
callq 0x58eae
testq %rax, %rax
je 0x56190
leaq 0x74205(%rip), %rax     # 0xc9fa8
cmpl $0x0, 0x7bc(%rax)
je 0x55dca
movl $0xe, %edx
movq %r14, %rdi
leaq 0x473a8(%rip), %rsi     # 0x9d163
callq 0x58f52
testq %rax, %rax
jne 0x55de7
jmp 0x561e7
movl $0x7, %edx
movq %r14, %rdi
leaq 0x473c6(%rip), %rsi     # 0x9d19f
callq 0x58f52
testq %rax, %rax
je 0x5623a
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rax
movl (%rax), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x56202
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
movq %r14, %rdi
callq 0x58f52
testq %rax, %rax
je 0x56172
movq %r14, %rdi
movq %r15, %rsi
callq 0x59069
testq %rax, %rax
je 0x56154
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x55592
movq 0xb8(%rsp), %rsi
movslq -0x8(%rsi), %rdx
movq %r14, %rdi
callq 0x58f52
testq %rax, %rax
je 0x56136
movq 0xb8(%rsp), %rdi
callq 0x28204
testb %bpl, %bpl
jne 0x55e86
movl $0x7, %edx
movq %r14, %rdi
leaq 0x47403(%rip), %rsi     # 0x9d27b
callq 0x58f52
testq %rax, %rax
je 0x5621d
callq 0x1f810
movq %rax, %r12
movl $0x0, (%rax)
movq 0x68(%rsp), %r15
movl -0x8(%r15), %ebp
testl %ebp, %ebp
sete %r14b
je 0x55f0b
movl $0x10000, %eax          # imm = 0x10000
cmpl %eax, %ebp
cmovael %eax, %ebp
movq 0x10(%rsp), %rcx
movl %r13d, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4fa20
cmpl %eax, %ebp
jne 0x55f02
xorl %ebx, %ebx
movq %rbx, %rcx
movslq %eax, %rbx
addq %rcx, %rbx
movslq -0x8(%r15), %rbp
subq %rbx, %rbp
sete %r14b
je 0x55f0b
movl $0x10000, %eax          # imm = 0x10000
cmpq %rax, %rbp
cmovaeq %rax, %rbp
leaq (%r15,%rbx), %rsi
movq 0x10(%rsp), %rcx
movl %r13d, %edi
movq %rbp, %rdx
callq 0x4fa20
cmpl %ebp, %eax
je 0x55ec8
testb %r14b, %r14b
je 0x5600d
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %r13d, %edi
leaq 0x4d0(%rsp), %rsi
callq 0x4fbd5
movl $0x9, %ebp
testq %rax, %rax
jle 0x56001
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %r13d, %edi
leaq 0xd0(%rsp), %rsi
callq 0x4fbd5
testq %rax, %rax
movq 0x18(%rsp), %rbx
leaq 0x30(%rsp), %r14
jle 0x56077
movb 0x4d0(%rsp), %al
cmpb $0x2d, %al
je 0x5604b
cmpb $0x2d, 0xd0(%rsp)
je 0x5604b
cmpb $0x0, 0xf(%rsp)
je 0x55fa6
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x3174b
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x316b7
leaq 0x74853(%rip), %rax     # 0xca800
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x2d6f0
leaq 0x73fe8(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
movl $0x3, %esi
leaq 0x3ecfa(%rip), %rdi     # 0x94ccd
callq 0x303b7
movq %rax, %r15
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rcx
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x2, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2f581
movq %r15, %rdi
callq 0x30834
jmp 0x56077
movq 0x18(%rsp), %rbx
leaq 0x30(%rsp), %r14
jmp 0x56077
movq 0x68(%rsp), %rdi
callq 0x28204
movq 0x18(%rsp), %rbx
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x55a9d
cmpl $0x6e, (%r12)
movq 0x20(%rsp), %rax
leaq 0x30(%rsp), %r14
je 0x560ff
testl %eax, %eax
je 0x560a7
jmp 0x560ff
cmpb $0x2d, %al
leaq 0xd0(%rsp), %rdx
leaq 0x4d0(%rsp), %rax
cmoveq %rax, %rdx
incq %rdx
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0x47238(%rip), %rsi     # 0x9d2a8
xorl %eax, %eax
callq 0x2d9fe
cmpl $0x9, %ebp
jne 0x560dd
movq 0x68(%rsp), %rdi
callq 0x28204
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x55a9d
cmpl $0x6e, (%r12)
movq 0x20(%rsp), %rax
je 0x560f6
testl %eax, %eax
jne 0x560f6
incl %eax
movq %rax, 0x20(%rsp)
jmp 0x55bdc
leaq 0x74746(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq 0x18(%rsp), %rdi
callq 0x2d6f0
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ebp, %ebp
jne 0x560cb
movq 0x68(%rsp), %rdi
callq 0x28204
jmp 0x560cb
leaq 0x46fd1(%rip), %rdi     # 0x9d0c5
jmp 0x56106
leaq 0x47207(%rip), %rdi     # 0x9d304
jmp 0x56106
leaq 0x471c9(%rip), %rdi     # 0x9d2cf
callq 0x281da
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2d894
jmp 0x560cb
leaq 0x46faf(%rip), %rdx     # 0x9d0ce
leaq 0x45b57(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1528, %r8d           # imm = 0x1528
jmp 0x56253
leaq 0x470f0(%rip), %rdx     # 0x9d22d
leaq 0x45b39(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1548, %r8d           # imm = 0x1548
jmp 0x56253
leaq 0x470b3(%rip), %rdx     # 0x9d20e
leaq 0x45b1b(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1540, %r8d           # imm = 0x1540
jmp 0x56253
leaq 0x47053(%rip), %rdx     # 0x9d1cc
leaq 0x45afd(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153f, %r8d           # imm = 0x153F
jmp 0x56253
leaq 0x46fa0(%rip), %rdx     # 0x9d137
leaq 0x45adf(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1532, %r8d           # imm = 0x1532
jmp 0x56253
leaq 0x46f62(%rip), %rdx     # 0x9d117
leaq 0x45ac1(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x152a, %r8d           # imm = 0x152A
jmp 0x56253
leaq 0x46f20(%rip), %rdx     # 0x9d0f3
leaq 0x45aa3(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1529, %r8d           # imm = 0x1529
jmp 0x56253
leaq 0x46f84(%rip), %rdx     # 0x9d172
leaq 0x45a88(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1538, %r8d           # imm = 0x1538
jmp 0x56253
leaq 0x43f6a(%rip), %rdx     # 0x9a173
leaq 0x45a6d(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153d, %r8d           # imm = 0x153D
jmp 0x56253
leaq 0x4705f(%rip), %rdx     # 0x9d283
leaq 0x45a52(%rip), %rcx     # 0x9bc7d
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x1550, %r8d           # imm = 0x1550
jmp 0x56253
leaq 0x46f66(%rip), %rdx     # 0x9d1a7
leaq 0x45a35(%rip), %rcx     # 0x9bc7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153a, %r8d           # imm = 0x153A
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0