// File: /workspace/github_c_makefile_star2_O2/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
movb $0x1, %cl
pushq $0x6
popq %rax
pushq $0x1
popq %rdx
movq %rdi, 0x8(%rsp)
movl %ecx, 0x1c(%rsp)
movslq 0x30(%rbx), %r12
movq 0x38(%rbx), %rbp
movq %rax, %rbx
xorl %r13d, %r13d
movl $0x0, 0x4(%rsp)
movl %edx, %r15d
cmpq %r12, %rbx
jge 0x512b2
movq (%rbp,%rbx,8), %rax
movq 0x8(%rax), %r14
movq %r14, %rdi
leaq 0x41e47(%rip), %rsi     # 0x930c0
callq 0x1f2e0
movl %r15d, %edx
movl %r15d, %ecx
testl %eax, %eax
je 0x512a9
movq %r14, %rdi
leaq 0x41d77(%rip), %rsi     # 0x93009
callq 0x1f2e0
movl %r15d, %edx
movl %r15d, %r13d
movl 0x4(%rsp), %ecx
testl %eax, %eax
jne 0x51601
incq %rbx
movl %ecx, 0x4(%rsp)
jmp 0x5125e
movq 0x28(%rbp), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
leaq 0x30(%rsp), %rbp
jne 0x51619
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rsi
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x51619
cmpq $0x0, 0x10(%rsp)
jg 0x51307
movq $0x3e8, 0x10(%rsp)      # imm = 0x3E8
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x2f89e
testq %rax, %rax
je 0x5162b
movq %rax, %r12
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %rdi
callq 0x50f71
cmpl $-0x1, %eax
je 0x51619
movl %eax, %r14d
callq 0x271bf
movq %rbp, %rdi
movq %rax, %rsi
callq 0x5421f
movq %rbp, %rdi
pushq $0x2a
popq %rsi
pushq $0x2
popq %rdx
callq 0x54281
testq %rax, %rax
je 0x517d6
movq %rbp, %rdi
leaq 0x41d72(%rip), %rsi     # 0x930ec
pushq $0x6
popq %rdx
callq 0x54345
testq %rax, %rax
je 0x517b8
movq %r12, 0x20(%rsp)
movq 0x28(%rsp), %rsi
movq %rbp, %rdi
callq 0x543ad
testq %rax, %rax
je 0x5179a
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x30ab1
cmpq $-0x1, %rax
je 0x513d5
movq %rax, %rbp
callq 0x22843
subq %rax, %rbp
cmpq $0x2, %rbp
pushq $0x1
popq %rax
cmovlq %rax, %rbp
jmp 0x513d7
xorl %ebp, %ebp
cmpl $0x1, %r13d
pushq $0x5
popq %rdx
sbbl $0x0, %edx
leaq 0x30(%rsp), %rdi
pushq $0x2a
popq %rsi
callq 0x54281
testq %rax, %rax
je 0x5177c
leaq 0x75baa(%rip), %rax     # 0xc6fa8
cmpl $0x0, 0x7bc(%rax)
je 0x51425
leaq 0x30(%rsp), %rdi
leaq 0x41d50(%rip), %rsi     # 0x93163
pushq $0xe
popq %rdx
callq 0x54345
testq %rax, %rax
jne 0x51442
jmp 0x517f1
leaq 0x30(%rsp), %rdi
leaq 0x41d6e(%rip), %rsi     # 0x9319f
pushq $0x7
popq %rdx
callq 0x54345
testq %rax, %rax
je 0x51844
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rax
movl (%rax), %ecx
shrl $0x4, %ecx
testb $0x7, %cl
jne 0x51829
movq 0x8(%rax), %rsi
movslq -0x8(%rsi), %rdx
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x54345
testq %rax, %rax
je 0x5175e
movq %r12, %rdi
movq %rbp, %rsi
callq 0x543ad
testq %rax, %rax
je 0x51740
leaq 0x80(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x50c30
movq 0xb8(%rsp), %rsi
movslq -0x8(%rsi), %rdx
movq %r12, %rdi
callq 0x54345
testq %rax, %rax
je 0x51722
movq 0xb8(%rsp), %rdi
callq 0x271f7
testl %r13d, %r13d
je 0x514e6
movq %r12, %rdi
leaq 0x41da6(%rip), %rsi     # 0x9327b
pushq $0x7
popq %rdx
callq 0x54345
testq %rax, %rax
je 0x5180c
callq 0x1f810
movq %rax, 0x20(%rsp)
andl $0x0, (%rax)
movq 0x68(%rsp), %rbx
xorl %r12d, %r12d
movl $0x10000, %r13d         # imm = 0x10000
movslq -0x8(%rbx), %rbp
subq %r12, %rbp
je 0x51534
cmpq %r13, %rbp
cmovaeq %r13, %rbp
leaq (%rbx,%r12), %rsi
movq 0x10(%rsp), %rcx
movl %r14d, %edi
movq %rbp, %rdx
callq 0x4b630
cmpl %ebp, %eax
jne 0x515b8
cltq
addq %rax, %r12
jmp 0x51501
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %r14d, %edi
leaq 0x4d0(%rsp), %rsi
callq 0x4b772
testq %rax, %rax
jle 0x51576
movq 0x10(%rsp), %rcx
movl $0x400, %edx            # imm = 0x400
movl %r14d, %edi
leaq 0xd0(%rsp), %rsi
callq 0x4b772
testq %rax, %rax
jg 0x5164d
movq 0x68(%rsp), %rdi
callq 0x271f7
movq 0x8(%rsp), %rbx
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x5111b
movq 0x20(%rsp), %rax
cmpl $0x6e, (%rax)
setne %al
testb %al, 0x1c(%rsp)
movl $0x0, %ecx
pushq $0x6
popq %rax
movl %r15d, %edx
jne 0x51244
jmp 0x51634
movq 0x68(%rsp), %rdi
callq 0x271f7
movq 0x8(%rsp), %rbx
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rax), %rsi
callq 0x5111b
movq 0x20(%rsp), %rax
cmpl $0x6e, (%rax)
setne %al
testb %al, 0x1c(%rsp)
movl $0x0, %ecx
pushq $0x6
popq %rax
movl %r15d, %edx
jne 0x51244
leaq 0x41cd0(%rip), %rdi     # 0x932cf
jmp 0x5163b
leaq 0x761f8(%rip), %rax     # 0xc7800
movq 0x80(%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x2c10d
addq $0x8d8, %rsp            # imm = 0x8D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x41a93(%rip), %rdi     # 0x930c5
jmp 0x5163b
leaq 0x41cc9(%rip), %rdi     # 0x93304
callq 0x271cd
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c20f
jmp 0x51619
movb 0x4d0(%rsp), %al
cmpb $0x2d, %al
movq 0x8(%rsp), %rbx
je 0x516e9
cmpb $0x2d, 0xd0(%rsp)
je 0x516e9
cmpl $0x0, 0x4(%rsp)
jne 0x51694
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x2fc3b
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
callq 0x2fbc0
leaq 0x76165(%rip), %rax     # 0xc7800
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x2c10d
leaq 0x758fa(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
leaq 0x39621(%rip), %rdi     # 0x8acdd
pushq $0x3
popq %rsi
callq 0x2ea81
movq %rax, %r14
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rcx
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2dd2e
movq %r14, %rdi
callq 0x2ee1f
jmp 0x51713
cmpb $0x2d, %al
leaq 0xd0(%rsp), %rdx
leaq 0x4d0(%rsp), %rax
cmoveq %rax, %rdx
incq %rdx
leaq 0x41b9f(%rip), %rsi     # 0x932a8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2c301
movq 0x68(%rsp), %rdi
callq 0x271f7
jmp 0x51619
leaq 0x41b04(%rip), %rdx     # 0x9322d
leaq 0x4054d(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1548, %r8d           # imm = 0x1548
jmp 0x5185d
leaq 0x41ac7(%rip), %rdx     # 0x9320e
leaq 0x4052f(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1540, %r8d           # imm = 0x1540
jmp 0x5185d
leaq 0x41a67(%rip), %rdx     # 0x931cc
leaq 0x40511(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153f, %r8d           # imm = 0x153F
jmp 0x5185d
leaq 0x419b4(%rip), %rdx     # 0x93137
leaq 0x404f3(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1532, %r8d           # imm = 0x1532
jmp 0x5185d
leaq 0x41976(%rip), %rdx     # 0x93117
leaq 0x404d5(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x152a, %r8d           # imm = 0x152A
jmp 0x5185d
leaq 0x41934(%rip), %rdx     # 0x930f3
leaq 0x404b7(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1529, %r8d           # imm = 0x1529
jmp 0x5185d
leaq 0x418f1(%rip), %rdx     # 0x930ce
leaq 0x40499(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1528, %r8d           # imm = 0x1528
jmp 0x5185d
leaq 0x4197a(%rip), %rdx     # 0x93172
leaq 0x4047e(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1538, %r8d           # imm = 0x1538
jmp 0x5185d
leaq 0x41a70(%rip), %rdx     # 0x93283
leaq 0x40463(%rip), %rcx     # 0x91c7d
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x1550, %r8d           # imm = 0x1550
jmp 0x5185d
leaq 0x3e94f(%rip), %rdx     # 0x9017f
leaq 0x40446(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153d, %r8d           # imm = 0x153D
jmp 0x5185d
leaq 0x4195c(%rip), %rdx     # 0x931a7
leaq 0x4042b(%rip), %rcx     # 0x91c7d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x153a, %r8d           # imm = 0x153A
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0