Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
                  "slot %d epoch (%llu) > reqEpoch (%llu)",
                  node->name, node->human_nodename, j, (unsigned long long)server.cluster->slots[j]->configEpoch,
                  (unsigned long long)requestConfigEpoch);
        return;
    }

    /* We can vote for this replica. */
    server.cluster->lastVoteEpoch = server.cluster->currentEpoch;
    node->replicaof->voted_time = mstime();
    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_FSYNC_CONFIG);
    clusterSendFailoverAuth(node);
    serverLog(LL_NOTICE, "Failover auth granted to %.40s (%s) for epoch %llu", node->name, node->human_nodename,
              (unsigned long long)server.cluster->currentEpoch);
}

/* This function returns the "rank" of this instance, a replica, in the context
 * of its primary-replicas ring. The rank of the replica is given by the number of
 * other replicas for the same primary that have a better replication offset
 * compared to the local one (better means, greater, so they claim more data).
 *
 * A replica with rank 0 is the one with the greatest (most up to date)
 * replication offset, and so forth. Note that because how the rank is computed
 * multiple replicas may have the same rank, in case they have the same offset.
 *
 * If the replication offsets are the same, the one with the lexicographically
 * smaller Node id  * will have a better ranking to avoid simultaneous elections
 * of replicas.
 *
 * The replica rank is used to add a delay to start an election in order to
 * get voted and replace a failing primary. Replicas with better replication
 * offsets are more likely to win. */
int clusterGetReplicaRank(void) {
    long long myoffset;
    int j, rank = 0;
    clusterNode *primary;

    serverAssert(nodeIsReplica(myself));
    primary = myself->replicaof;
    if (primary == NULL) return 0; /* Never called by replicas without primary. */

    myoffset = replicationGetReplicaOffset();
    for (j = 0; j < primary->num_replicas; j++) {
        if (primary->replicas[j] == myself) continue;
        if (nodeCantFailover(primary->replicas[j])) continue;

        if (primary->replicas[j]->repl_offset > myoffset) {
            rank++;
        } else if (primary->replicas[j]->repl_offset == myoffset &&
                   memcmp(primary->replicas[j]->name, myself->name, CLUSTER_NAMELEN) < 0) {
            rank++;
        }

Review comment (suggestion):
nit - extra `*`
```suggestion
 * smaller node id will have a lower rank to avoid simultaneous elections
```

Code after:
                  "slot %d epoch (%llu) > reqEpoch (%llu)",
                  node->name, node->human_nodename, j, (unsigned long long)server.cluster->slots[j]->configEpoch,
                  (unsigned long long)requestConfigEpoch);
        return;
    }

    /* We can vote for this replica. */
    server.cluster->lastVoteEpoch = server.cluster->currentEpoch;
    node->replicaof->voted_time = mstime();
    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_FSYNC_CONFIG);
    clusterSendFailoverAuth(node);
    serverLog(LL_NOTICE, "Failover auth granted to %.40s (%s) for epoch %llu", node->name, node->human_nodename,
              (unsigned long long)server.cluster->currentEpoch);
}

/* This function returns the "rank" of this instance, a replica, in the context
 * of its primary-replicas ring. The rank of the replica is given by the number of
 * other replicas for the same primary that have a better replication offset
 * compared to the local one (better means, greater, so they claim more data).
 *
 * A replica with rank 0 is the one with the greatest (most up to date)
 * replication offset, and so forth. Note that because how the rank is computed
 * multiple replicas may have the same rank, in case they have the same offset.
 *
 * If the replication offsets are the same, the one with the lexicographically
 * smaller node id will have a lower rank to avoid simultaneous elections
 * of replicas.
 *
 * The replica rank is used to add a delay to start an election in order to
 * get voted and replace a failing primary. Replicas with better replication
 * offsets are more likely to win. */
int clusterGetReplicaRank(void) {
    long long myoffset;
    int j, rank = 0;
    clusterNode *primary;

    serverAssert(nodeIsReplica(myself));
    primary = myself->replicaof;
    if (primary == NULL) return 0; /* Never called by replicas without primary. */

    myoffset = replicationGetReplicaOffset();
    for (j = 0; j < primary->num_replicas; j++) {
        if (primary->replicas[j] == myself) continue;
        if (nodeCantFailover(primary->replicas[j])) continue;

        if (primary->replicas[j]->repl_offset > myoffset) {
            rank++;
        } else if (primary->replicas[j]->repl_offset == myoffset &&
                   memcmp(primary->replicas[j]->name, myself->name, CLUSTER_NAMELEN) < 0) {
            rank++;
        }