// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: getNodeByQuery
clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *error_code) {

    // 初始化为 NULL ，
    // 如果输入命令是无参数命令，那么 n 就会继续为 NULL
    clusterNode *n = NULL;

    robj *firstkey = NULL;
    int multiple_keys = 0;
    multiState *ms, _ms;
    multiCmd mc;
    int i, slot = 0, migrating_slot = 0, importing_slot = 0, missing_keys = 0;

    /* Set error code optimistically for the base case. */
    if (error_code) *error_code = REDIS_CLUSTER_REDIR_NONE;

    /* We handle all the cases as if they were EXEC commands, so we have
     * a common code path for everything */
    // 集群可以执行事务，
    // 但必须确保事务中的所有命令都是针对某个相同的键进行的
    // 这个 if 和接下来的 for 进行的就是这一合法性检测
    if (cmd->proc == execCommand) {
        /* If REDIS_MULTI flag is not set EXEC is just going to return an
         * error. */
        if (!(c->flags & REDIS_MULTI)) return myself;
        ms = &c->mstate;
    } else {
        /* In order to have a single codepath create a fake Multi State
         * structure if the client is not in MULTI/EXEC state, this way
         * we have a single codepath below. */
        ms = &_ms;
        _ms.commands = &mc;
        _ms.count = 1;
        mc.argv = argv;
        mc.argc = argc;
        mc.cmd = cmd;
    }

    /* Check that all the keys are in the same hash slot, and obtain this
     * slot and the node associated. */
    for (i = 0; i < ms->count; i++) {
        struct redisCommand *mcmd;
        robj **margv;
        int margc, *keyindex, numkeys, j;

        mcmd = ms->commands[i].cmd;
        margc = ms->commands[i].argc;
        margv = ms->commands[i].argv;

        // 定位命令的键位置
        keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys);
        // 遍历命令中的所有键
        for (j = 0; j < numkeys; j++) {
            robj *thiskey = margv[keyindex[j]];
            int thisslot = keyHashSlot((char*)thiskey->ptr,
                                       sdslen(thiskey->ptr));

            if (firstkey == NULL) {
                // 这是事务中第一个被处理的键
                // 获取该键的槽和负责处理该槽的节点
                /* This is the first key we see. Check what is the slot
                 * and node. */
                firstkey = thiskey;
                slot = thisslot;
                n = server.cluster->slots[slot];
                redisAssertWithInfo(c,firstkey,n != NULL);
                /* If we are migrating or importing this slot, we need to check
                 * if we have all the keys in the request (the only way we
                 * can safely serve the request, otherwise we return a TRYAGAIN
                 * error). To do so we set the importing/migrating state and
                 * increment a counter for every missing key. */
                if (n == myself &&
                    server.cluster->migrating_slots_to[slot] != NULL)
                {
                    migrating_slot = 1;
                } else if (server.cluster->importing_slots_from[slot] != NULL) {
                    importing_slot = 1;
                }
            } else {
                /* If it is not the first key, make sure it is exactly
                 * the same key as the first we saw. */
                if (!equalStringObjects(firstkey,thiskey)) {
                    if (slot != thisslot) {
                        /* Error: multiple keys from different slots. */
                        getKeysFreeResult(keyindex);
                        if (error_code)
                            *error_code = REDIS_CLUSTER_REDIR_CROSS_SLOT;
                        return NULL;
                    } else {
                        /* Flag this request as one with multiple different
                         * keys. */
                        multiple_keys = 1;
                    }
                }
            }

            /* Migarting / Improrting slot? Count keys we don't have. */
            if ((migrating_slot || importing_slot) &&
                lookupKeyRead(&server.db[0],thiskey) == NULL)
            {
                missing_keys++;
            }
        }
        getKeysFreeResult(keyindex);
    }

    /* No key at all in command? then we can serve the request
     * without redirections or errors. */
    if (n == NULL) return myself;

    /* Return the hashslot by reference. */
    if (hashslot) *hashslot = slot;

    /* This request is about a slot we are migrating into another instance?
     * Then if we have all the keys. */

    /* If we don't have all the keys and we are migrating the slot, send
     * an ASK redirection. */
    if (migrating_slot && missing_keys) {
        if (error_code) *error_code = REDIS_CLUSTER_REDIR_ASK;
        return server.cluster->migrating_slots_to[slot];
    }

    /* If we are receiving the slot, and the client correctly flagged the
     * request as "ASKING", we can serve the request. However if the request
     * involves multiple keys and we don't have them all, the only option is
     * to send a TRYAGAIN error. */
    if (importing_slot &&
        (c->flags & REDIS_ASKING || cmd->flags & REDIS_CMD_ASKING))
    {
        if (multiple_keys && missing_keys) {
            if (error_code) *error_code = REDIS_CLUSTER_REDIR_UNSTABLE;
            return NULL;
        } else {
            return myself;
        }
    }

    /* Handle the read-only client case reading from a slave: if this
     * node is a slave and the request is about an hash slot our master
     * is serving, we can reply without redirection. */
    if (c->flags & REDIS_READONLY &&
        cmd->flags & REDIS_CMD_READONLY &&
        nodeIsSlave(myself) &&
        myself->slaveof == n)
    {
        return myself;
    }

    /* Base case: just return the right node. However if this node is not
     * myself, set error_code to MOVED since we need to issue a rediretion. */
    if (n != myself && error_code) *error_code = REDIS_CLUSTER_REDIR_MOVED;

    // 返回负责处理槽 slot 的节点 n
    return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
testq %r9, %r9
je 0x55d04
movl $0x0, (%r9)
movq 0x8(%rsi), %rax
cmpq 0x701e1(%rip), %rax     # 0xc5ef0
je 0x55f88
movq %r8, 0x40(%rsp)
movq %rdi, 0x20(%rsp)
movq %r9, 0x28(%rsp)
leaq 0x80(%rsp), %rax
leaq 0x78(%rsp), %rdi
movq %rdi, 0x50(%rsp)
movq %rax, (%rdi)
leaq 0x70(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movl $0x1, (%rdi)
movq %rdx, (%rax)
movl %ecx, 0x8(%rax)
movq %rsi, 0x18(%rsp)
movq %rsi, 0x10(%rax)
xorl %ecx, %ecx
movq $0x0, 0x58(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq %rcx, 0x60(%rsp)
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdi
movl 0x8(%rax,%rcx,8), %edx
movq (%rax,%rcx,8), %rsi
movq %rsi, 0x68(%rsp)
leaq 0x14(%rsp), %rcx
callq 0x330a8
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x14(%rsp)
jle 0x55eda
xorl %r14d, %r14d
movl %r13d, %r15d
movl %ebx, %ebp
movq 0x30(%rsp), %rax
movslq (%rax,%r14,4), %rax
movq 0x68(%rsp), %rcx
movq (%rcx,%rax,8), %r12
movq 0x8(%r12), %rdi
movl -0x8(%rdi), %esi
callq 0x50c03
movl %eax, %ebx
movq 0x38(%rsp), %r13
testq %r13, %r13
je 0x55e0c
movq %r13, %rdi
movq %r12, %rsi
callq 0x30f08
testl %eax, %eax
je 0x55e5d
movl %ebp, %ebx
jmp 0x55e6f
leaq 0x74195(%rip), %rax     # 0xc9fa8
movq 0x7d0(%rax), %rax
movl %ebx, %ecx
movq 0x40028(%rax,%rcx,8), %rdx
testq %rdx, %rdx
je 0x5605b
movq %rdx, 0x58(%rsp)
cmpq 0x887ef(%rip), %rdx     # 0xde628
je 0x55ec1
cmpq $0x0, 0x20028(%rax,%rcx,8)
movl 0x8(%rsp), %eax
movl $0x1, %ecx
cmovnel %ecx, %eax
movl %eax, 0x8(%rsp)
movq %r12, %r13
jmp 0x55e6f
movl $0x1, 0xc(%rsp)
cmpl %ebx, %ebp
movl %ebp, %ebx
jne 0x55f67
movq %r13, 0x38(%rsp)
xorl %r13d, %r13d
movl %r15d, %eax
orl 0x8(%rsp), %eax
je 0x55ea1
leaq 0x74121(%rip), %rax     # 0xc9fa8
movq 0x10(%rax), %rdi
movq %r12, %rsi
callq 0x3163a
cmpq $0x1, %rax
adcl $0x0, 0x10(%rsp)
movl %r15d, %r13d
jmp 0x55eac
movl $0x0, 0x8(%rsp)
movl %r13d, %r15d
incq %r14
movslq 0x14(%rsp), %rax
movl %ebx, %ebp
cmpq %rax, %r14
jl 0x55dce
jmp 0x55eda
cmpq $0x0, 0x28(%rax,%rcx,8)
je 0x55e3f
movq %r12, 0x38(%rsp)
movl $0x1, %r15d
jmp 0x55e80
movq 0x30(%rsp), %rdi
callq 0x330b7
movq 0x60(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %rax
movslq (%rax), %rax
cmpq %rax, %rcx
jl 0x55d89
testl %r13d, %r13d
setne %dl
cmpl $0x0, 0x10(%rsp)
setne %al
cmpl $0x0, 0xc(%rsp)
setne %cl
movq 0x58(%rsp), %r8
testq %r8, %r8
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %r9
je 0x5600c
andb %al, %dl
testq %r9, %r9
je 0x55f3a
movl %ebx, (%r9)
testb %dl, %dl
je 0x55fcb
testq %rsi, %rsi
je 0x55f4d
movl $0x3, (%rsi)
leaq 0x74054(%rip), %rax     # 0xc9fa8
movq 0x7d0(%rax), %rax
movl %ebx, %ecx
movq 0x28(%rax,%rcx,8), %rax
jmp 0x56013
movq 0x30(%rsp), %rdi
callq 0x330b7
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x55f81
movl $0x1, (%rax)
xorl %eax, %eax
jmp 0x56013
testb $0x8, 0x90(%rdi)
je 0x5600c
movq %rsi, 0x18(%rsp)
cmpl $0x0, 0x108(%rdi)
jle 0x5600c
movq %r8, 0x40(%rsp)
movq %r9, 0x28(%rsp)
leaq 0x100(%rdi), %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0x20(%rsp)
leaq 0x108(%rdi), %rax
movq %rax, 0x48(%rsp)
jmp 0x55d58
movl 0x90(%rdi), %edx
cmpl $0x0, 0x8(%rsp)
je 0x55fe9
btl $0x9, %edx
jb 0x55ff8
movq 0x18(%rsp), %rdi
testb $0x10, 0x21(%rdi)
jne 0x55ff8
btl $0x11, %edx
jb 0x56025
movq 0x88632(%rip), %rax     # 0xde628
jmp 0x56046
andb %al, %cl
je 0x5600c
testq %rsi, %rsi
je 0x55f81
movl $0x2, (%rsi)
jmp 0x55f81
movq 0x88615(%rip), %rax     # 0xde628
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
testb $0x2, 0x20(%rax)
movq 0x885f3(%rip), %rax     # 0xde628
je 0x56046
testb $0x2, 0x30(%rax)
je 0x56046
cmpq %r8, 0x850(%rax)
je 0x56013
testq %rsi, %rsi
je 0x56056
cmpq %rax, %r8
je 0x56056
movl $0x4, (%rsi)
movq %r8, %rax
jmp 0x56013
leaq 0x42f95(%rip), %rdx     # 0x98ff7
leaq 0x45c54(%rip), %rcx     # 0x9bcbd
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movl $0x1627, %r8d           # imm = 0x1627
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0
nop