// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x71dc2
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
leaq -0xf1ad(%rip), %rcx     # 0x62c20
cmpq %rcx, 0x8(%rax)
jne 0x71e05
movq -0x10(%rbp), %rax
movl 0x90(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x71df5
movq 0xb618c(%rip), %rax     # 0x127f78
movq %rax, -0x8(%rbp)
jmp 0x7224a
movq -0x10(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x58(%rbp)
jmp 0x71e38
leaq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x1, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x58(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x720f6
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x8c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl -0xb4(%rbp), %edx
leaq -0xc4(%rbp), %rcx
callq 0x3e6f0
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x720d6
movq -0xb0(%rbp), %rax
movq -0xc0(%rbp), %rcx
movslq -0xc8(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x67ee0
movq -0xe0(%rbp), %rdi
movl %eax, %esi
callq 0x69ec0
movl %eax, -0xd4(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x7202a
movq -0xd0(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x90(%rbp)
leaq 0xa1980(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movslq -0x90(%rbp), %rcx
movq 0x40028(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x71f9b
jmp 0x71fc6
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq 0x710d0(%rip), %rdx     # 0xe307a
leaq 0x73cf4(%rip), %rcx     # 0xe5ca5
movl $0x1627, %r8d           # imm = 0x1627
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x40(%rbp), %rax
cmpq 0xb5fa7(%rip), %rax     # 0x127f78
jne 0x71ffc
leaq 0xa191e(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movslq -0x90(%rbp), %rcx
cmpq $0x0, 0x28(%rax,%rcx,8)
je 0x71ffc
movl $0x1, -0x94(%rbp)
jmp 0x72028
leaq 0xa18f5(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movslq -0x90(%rbp), %rcx
cmpq $0x0, 0x20028(%rax,%rcx,8)
je 0x72026
movl $0x1, -0x98(%rbp)
jmp 0x72028
jmp 0x72082
movq -0x48(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x3b240
cmpl $0x0, %eax
jne 0x72080
movl -0x90(%rbp), %eax
cmpl -0xd4(%rbp), %eax
je 0x72077
movq -0xc0(%rbp), %rdi
callq 0x3e760
cmpq $0x0, -0x38(%rbp)
je 0x7206a
movq -0x38(%rbp), %rax
movl $0x1, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x7224a
movl $0x1, -0x4c(%rbp)
jmp 0x72080
jmp 0x72082
cmpl $0x0, -0x94(%rbp)
jne 0x72094
cmpl $0x0, -0x98(%rbp)
je 0x720c0
leaq 0xa185d(%rip), %rax     # 0x1138f8
movq 0x10(%rax), %rdi
movq -0xd0(%rbp), %rsi
callq 0x3bed0
cmpq $0x0, %rax
jne 0x720c0
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x720c2
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x71ee3
movq -0xc0(%rbp), %rdi
callq 0x3e760
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x71e42
cmpq $0x0, -0x40(%rbp)
jne 0x7210d
movq 0xb5e74(%rip), %rax     # 0x127f78
movq %rax, -0x8(%rbp)
jmp 0x7224a
cmpq $0x0, -0x30(%rbp)
je 0x72120
movl -0x90(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x94(%rbp)
je 0x72166
cmpl $0x0, -0x9c(%rbp)
je 0x72166
cmpq $0x0, -0x38(%rbp)
je 0x72143
movq -0x38(%rbp), %rax
movl $0x3, (%rax)
leaq 0xa17ae(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movslq -0x90(%rbp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, -0x8(%rbp)
jmp 0x7224a
cmpl $0x0, -0x98(%rbp)
je 0x721ce
movq -0x10(%rbp), %rax
movl 0x90(%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
jne 0x72194
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
je 0x721ce
cmpl $0x0, -0x4c(%rbp)
je 0x721c1
cmpl $0x0, -0x9c(%rbp)
je 0x721c1
cmpq $0x0, -0x38(%rbp)
je 0x721b4
movq -0x38(%rbp), %rax
movl $0x2, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x7224a
movq 0xb5db0(%rip), %rax     # 0x127f78
movq %rax, -0x8(%rbp)
jmp 0x7224a
movq -0x10(%rbp), %rax
movl 0x90(%rax), %eax
andl $0x20000, %eax          # imm = 0x20000
cmpl $0x0, %eax
je 0x72224
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x72224
movq 0xb5d80(%rip), %rax     # 0x127f78
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x72224
movq 0xb5d6e(%rip), %rax     # 0x127f78
movq 0x850(%rax), %rax
cmpq -0x40(%rbp), %rax
jne 0x72224
movq 0xb5d5a(%rip), %rax     # 0x127f78
movq %rax, -0x8(%rbp)
jmp 0x7224a
movq -0x40(%rbp), %rax
cmpq 0xb5d49(%rip), %rax     # 0x127f78
je 0x72242
cmpq $0x0, -0x38(%rbp)
je 0x72242
movq -0x38(%rbp), %rax
movl $0x4, (%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)