// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/cluster.c
// Function: clusterHandleSlaveMigration
void clusterHandleSlaveMigration(int max_slaves) {
    int j, okslaves = 0;
    clusterNode *mymaster = myself->slaveof, *target = NULL, *candidate = NULL;
    dictIterator *di;
    dictEntry *de;

    /* Step 1: Don't migrate if the cluster state is not ok. */
    if (server.cluster->state != CLUSTER_OK) return;

    /* Step 2: Don't migrate if my master will not be left with at least
     *         'migration-barrier' slaves after my migration. */
    if (mymaster == NULL) return;
    for (j = 0; j < mymaster->numslaves; j++)
        if (!nodeFailed(mymaster->slaves[j]) &&
            !nodeTimedOut(mymaster->slaves[j])) okslaves++;
    if (okslaves <= server.cluster_migration_barrier) return;

    /* Step 3: Idenitfy a candidate for migration, and check if among the
     * masters with the greatest number of ok slaves, I'm the one with the
     * smallest node ID (the "candidate slave").
     *
     * Note: this means that eventually a replica migration will occurr
     * since slaves that are reachable again always have their FAIL flag
     * cleared, so eventually there must be a candidate. At the same time
     * this does not mean that there are no race conditions possible (two
     * slaves migrating at the same time), but this is unlikely to
     * happen, and harmless when happens. */
    candidate = myself;
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);
        int okslaves = 0, is_orphaned = 1;

        /* We want to migrate only if this master is working, orphaned, and
         * used to have slaves or if failed over a master that had slaves
         * (MIGRATE_TO flag). This way we only migrate to instances that were
         * supposed to have replicas. */
        if (nodeIsSlave(node) || nodeFailed(node)) is_orphaned = 0;
        if (!(node->flags & CLUSTER_NODE_MIGRATE_TO)) is_orphaned = 0;

        /* Check number of working slaves. */
        if (nodeIsMaster(node)) okslaves = clusterCountNonFailingSlaves(node);
        if (okslaves > 0) is_orphaned = 0;

        if (is_orphaned) {
            if (!target && node->numslots > 0) target = node;

            /* Track the starting time of the orphaned condition for this
             * master. */
            if (!node->orphaned_time) node->orphaned_time = mstime();
        } else {
            node->orphaned_time = 0;
        }

        /* Check if I'm the slave candidate for the migration: attached
         * to a master with the maximum number of slaves and with the smallest
         * node ID. */
        if (okslaves == max_slaves) {
            for (j = 0; j < node->numslaves; j++) {
                if (memcmp(node->slaves[j]->name,
                           candidate->name,
                           CLUSTER_NAMELEN) < 0)
                {
                    candidate = node->slaves[j];
                }
            }
        }
    }
    dictReleaseIterator(di);

    /* Step 4: perform the migration if there is a target, and if I'm the
     * candidate, but only if the master is continuously orphaned for a
     * couple of seconds, so that during failovers, we give some time to
     * the natural slaves of this instance to advertise their switch from
     * the old master to the new one. */
    if (target && candidate == myself &&
        (mstime()-target->orphaned_time) > CLUSTER_SLAVE_MIGRATION_DELAY)
    {
        serverLog(LL_WARNING,"Migrating to orphaned master %.40s",
            target->name);
        clusterSetMaster(target);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, 0x14(%rsp)
movq 0xade3d(%rip), %r12     # 0x11b1d0
movq 0x850(%r12), %rsi
leaq 0x994b6(%rip), %rcx     # 0x106858
movq 0x9a0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %dl
testq %rsi, %rsi
sete %dil
orb %dl, %dil
jne 0x6d596
movslq 0x844(%rsi), %rdx
testq %rdx, %rdx
jle 0x6d3f5
movq 0x848(%rsi), %rdi
xorl %r8d, %r8d
xorl %esi, %esi
movq (%rdi,%r8,8), %r9
movb 0x30(%r9), %r9b
andb $0xc, %r9b
cmpb $0x1, %r9b
adcl $0x0, %esi
incq %r8
cmpq %r8, %rdx
jne 0x6d3d8
jmp 0x6d3f7
xorl %esi, %esi
cmpl 0x9a8(%rcx), %esi
jle 0x6d596
movq 0x18(%rax), %rdi
callq 0x320e5
movq %rax, %r15
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
je 0x6d529
movq $0x0, 0x8(%rsp)
movq %r12, %r13
movq 0x8(%rax), %r12
movl 0x30(%r12), %eax
movl $0x0, %ebp
testb $0x1, %al
je 0x6d472
movslq 0x844(%r12), %rcx
testq %rcx, %rcx
jle 0x6d472
movq 0x848(%r12), %rdx
xorl %esi, %esi
xorl %ebp, %ebp
movq (%rdx,%rsi,8), %rdi
movb 0x30(%rdi), %dil
andb $0x8, %dil
cmpb $0x1, %dil
adcl $0x0, %ebp
incq %rsi
cmpq %rsi, %rcx
jne 0x6d457
andl $0x10a, %eax            # imm = 0x10A
cmpl $0x100, %eax            # imm = 0x100
jne 0x6d4bb
testl %ebp, %ebp
jg 0x6d4bb
cmpq $0x0, 0x8(%rsp)
jne 0x6d4a1
cmpl $0x0, 0x840(%r12)
movl $0x0, %eax
cmovgq %r12, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x880(%r12)
jne 0x6d4c7
callq 0x330c8
movq %rax, 0x880(%r12)
jmp 0x6d4c7
movq $0x0, 0x880(%r12)
cmpl 0x14(%rsp), %ebp
jne 0x6d510
movslq 0x844(%r12), %rbp
testq %rbp, %rbp
jle 0x6d510
movq 0x848(%r12), %rbx
xorl %r14d, %r14d
movq (%rbx,%r14,8), %r12
leaq 0x8(%r12), %rdi
leaq 0x8(%r13), %rsi
movl $0x28, %edx
callq 0x269c0
testl %eax, %eax
js 0x6d503
movq %r13, %r12
incq %r14
movq %r12, %r13
cmpq %r14, %rbp
jne 0x6d4e5
jmp 0x6d513
movq %r13, %r12
movq %r15, %rdi
callq 0x32116
movq %r12, %r13
testq %rax, %rax
jne 0x6d42c
jmp 0x6d532
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x321d3
cmpq $0x0, 0x8(%rsp)
je 0x6d596
cmpq 0xadc87(%rip), %r12     # 0x11b1d0
jne 0x6d596
callq 0x330c8
movq 0x8(%rsp), %rcx
subq 0x880(%rcx), %rax
cmpq $0x1389, %rax           # imm = 0x1389
jl 0x6d596
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %rdx
leaq 0x5deaa(%rip), %rsi     # 0xcb41e
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6b31e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq