// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterInit
void clusterInit(void) {
    int saveconf = 0;

    // 初始化配置
    server.cluster = zmalloc(sizeof(clusterState));
    server.cluster->myself = NULL;
    server.cluster->currentEpoch = 0;
    server.cluster->state = REDIS_CLUSTER_FAIL;
    server.cluster->size = 1;
    server.cluster->todo_before_sleep = 0;
    server.cluster->nodes = dictCreate(&clusterNodesDictType,NULL);
    server.cluster->nodes_black_list =
        dictCreate(&clusterNodesBlackListDictType,NULL);
    server.cluster->failover_auth_time = 0;
    server.cluster->failover_auth_count = 0;
    server.cluster->failover_auth_rank = 0;
    server.cluster->failover_auth_epoch = 0;
    server.cluster->lastVoteEpoch = 0;
    server.cluster->stats_bus_messages_sent = 0;
    server.cluster->stats_bus_messages_received = 0;
    memset(server.cluster->slots,0, sizeof(server.cluster->slots));
    clusterCloseAllSlots();

    /* Lock the cluster config file to make sure every node uses
     * its own nodes.conf. */
    if (clusterLockConfig(server.cluster_configfile) == REDIS_ERR)
        exit(1);

    /* Load or create a new nodes configuration. */
    if (clusterLoadConfig(server.cluster_configfile) == REDIS_ERR) {
        /* No configuration found. We will just use the random name provided
         * by the createClusterNode() function. */
        myself = server.cluster->myself =
            createClusterNode(NULL,REDIS_NODE_MYSELF|REDIS_NODE_MASTER);
        redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s",
            myself->name);
        clusterAddNode(myself);
        saveconf = 1;
    }

    // 保存 nodes.conf 文件
    if (saveconf) clusterSaveConfigOrDie(1);

    /* We need a listening TCP port for our cluster messaging needs. */
    // 监听 TCP 端口
    server.cfd_count = 0;

    /* Port sanity check II
     * The other handshake port check is triggered too late to stop
     * us from trying to use a too-high cluster port number. */
    if (server.port > (65535-REDIS_CLUSTER_PORT_INCR)) {
        redisLog(REDIS_WARNING, "Redis port number too high. "
                   "Cluster communication port is 10,000 port "
                   "numbers higher than your Redis port. "
                   "Your Redis port number must be "
                   "lower than 55535.");
        exit(1);
    }

    if (listenToPort(server.port+REDIS_CLUSTER_PORT_INCR,
        server.cfd,&server.cfd_count) == REDIS_ERR)
    {
        exit(1);
    } else {
        int j;

        for (j = 0; j < server.cfd_count; j++) {
            // 关联监听事件处理器
            if (aeCreateFileEvent(server.el, server.cfd[j], AE_READABLE,
                clusterAcceptHandler, NULL) == AE_ERR)
                    redisPanic("Unrecoverable error creating Redis Cluster "
                                "file event.");
        }
    }

    /* The slots -> keys map is a sorted set. Init it. */
    // slots -> keys 映射是一个有序集合
    server.cluster->slots_to_keys = zslCreate();
    resetManualFailover();
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movl $0x60090, %edi          # imm = 0x60090
callq 0x29709
leaq 0x79730(%rip), %r15     # 0xc9fa8
movq %rax, 0x7d0(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x10(%rax)
xorl %ebx, %ebx
movl %ebx, 0x60078(%rax)
leaq 0x7914e(%rip), %rdi     # 0xc99f0
xorl %esi, %esi
callq 0x21f97
movq 0x7d0(%r15), %rcx
movq %rax, 0x18(%rcx)
leaq 0x79165(%rip), %rdi     # 0xc9a20
xorl %esi, %esi
callq 0x21f97
movq 0x7d0(%r15), %r14
movq %rax, 0x20(%r14)
movq %rbx, 0x60030(%r14)
movl %ebx, 0x60038(%r14)
movl %ebx, 0x60040(%r14)
movq %rbx, 0x60048(%r14)
movq %rbx, 0x60070(%r14)
leaq 0x40028(%r14), %rdi
movl $0x20000, %edx          # imm = 0x20000
xorl %esi, %esi
callq 0x1f110
leaq 0x28(%r14), %rdi
movl $0x20000, %edx          # imm = 0x20000
xorl %esi, %esi
callq 0x1f110
xorps %xmm0, %xmm0
movups %xmm0, 0x60080(%r14)
movl $0x20028, %edi          # imm = 0x20028
addq 0x7d0(%r15), %rdi
movl $0x20000, %edx          # imm = 0x20000
xorl %esi, %esi
callq 0x1f110
movq 0x7c8(%r15), %rdi
callq 0x507b1
cmpl $-0x1, %eax
je 0x50a9e
movq 0x7c8(%r15), %rdi
callq 0x4fcd0
cmpl $-0x1, %eax
jne 0x509c8
xorl %edi, %edi
movl $0x11, %esi
callq 0x503df
movq 0x7d0(%r15), %rcx
movq %rax, (%rcx)
movq %rax, 0x8dcaf(%rip)     # 0xde628
leaq 0x8(%rax), %rdx
leaq 0x4b513(%rip), %rsi     # 0x9be97
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movq 0x8dc91(%rip), %rbx     # 0xde628
movq 0x7d0(%r15), %rax
movq 0x18(%rax), %r14
leaq 0x8(%rbx), %rdi
movl $0x28, %esi
callq 0x28148
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x222b6
movl $0x1, %edi
callq 0x50787
movl $0x0, 0x1ac(%r15)
movl 0x88(%r15), %edi
cmpl $0xd8f0, %edi           # imm = 0xD8F0
jge 0x50a8b
addl $0x2710, %edi           # imm = 0x2710
leaq 0x16c(%r15), %rsi
leaq 0x1ac(%r15), %rdx
callq 0x250db
cmpl $-0x1, %eax
je 0x50a9e
cmpl $0x0, 0x1ac(%r15)
jle 0x50a4c
xorl %r14d, %r14d
leaq 0xc4(%rip), %rbx        # 0x50ae0
movq 0x28(%r15), %rdi
movl 0x16c(%r15,%r14,4), %esi
movl $0x1, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x209d7
cmpl $-0x1, %eax
je 0x50a69
incq %r14
movslq 0x1ac(%r15), %rax
cmpq %rax, %r14
jl 0x50a1c
callq 0x3cfc2
movq 0x7d0(%r15), %rcx
movq %rax, 0x60028(%rcx)
popq %rbx
popq %r14
popq %r15
jmp 0x50bd9
leaq 0x4b4ed(%rip), %rdi     # 0x9bf5d
leaq 0x4b206(%rip), %rsi     # 0x9bc7d
movl $0x204, %edx            # imm = 0x204
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0
leaq 0x4b42f(%rip), %rsi     # 0x9bec1
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl $0x1, %edi
callq 0x1f2b0