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

Code before:
     * is just sending a lot of data in the cluster bus, for instance
     * because of Pub/Sub. */
    if (sender) sender->data_received = now;

    if (sender && !nodeInHandshake(sender)) {
        /* Update our currentEpoch if we see a newer epoch in the cluster. */
        sender_claimed_current_epoch = ntohu64(hdr->currentEpoch);
        sender_claimed_config_epoch = ntohu64(hdr->configEpoch);
        if (sender_claimed_current_epoch > server.cluster->currentEpoch)
            server.cluster->currentEpoch = sender_claimed_current_epoch;
        /* Update the sender configEpoch if it is a primary publishing a newer one. */
        if (sender_claims_to_be_primary && sender_claimed_config_epoch > sender->configEpoch) {
            sender->configEpoch = sender_claimed_config_epoch;
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_FSYNC_CONFIG);

            if (server.cluster->failover_auth_time && sender->configEpoch >= server.cluster->failover_auth_epoch) {
                /* Another node has claimed an epoch greater than or equal to ours.
                 * If we have an ongoing election, reset it because we cannot win
                 * with an epoch smaller than or equal to the incoming claim. This
                 * allows us to start a new election as soon as possible. */
                server.cluster->failover_auth_time = 0;
                serverLog(LL_WARNING, "Failover election in progress for epoch %llu, but received a claim from "
                                      "node %.40s (%s) with an equal or higher epoch %llu. Resetting the election "
                                      "since we cannot win an election in the past.",
                                      (unsigned long long)server.cluster->failover_auth_epoch,
                                      sender->name, sender->human_nodename, (unsigned long long)sender->configEpoch);
                /* Maybe we could start a new election, set a flag here to make sure
                 * we check as soon as possible, instead of waiting for a cron. */
                clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_FAILOVER);
            }
        }
        /* Update the replication offset info for this node. */
        sender->repl_offset = ntohu64(hdr->offset);
        sender->repl_offset_time = now;
        /* If we are a replica performing a manual failover and our primary
         * sent its offset while already paused, populate the MF state. */
        if (server.cluster->mf_end && nodeIsReplica(myself) && myself->replicaof == sender &&
            hdr->mflags[0] & CLUSTERMSG_FLAG0_PAUSED && server.cluster->mf_primary_offset == -1) {
            server.cluster->mf_primary_offset = sender->repl_offset;
            clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_MANUALFAILOVER);
            serverLog(LL_NOTICE,
                      "Received replication offset for paused "
                      "primary manual failover: %lld",
                      server.cluster->mf_primary_offset);
        }
    }

    /* Initial processing of PING and MEET requests replying with a PONG. */
    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) {
        /* We use incoming MEET messages in order to set the address
         * for 'myself', since only other cluster nodes will send us

Review comment (bug):
Clang-format complained about indentation here. The args need to be aligned under LL_WARNING. Maybe like this?

```suggestion
                serverLog(LL_WARNING,
                          "Failover election in progress for epoch %llu, but received a claim from "
                          "node %.40s (%s) with an equal or higher epoch %llu. Resetting the election "
                          "since we cannot win an election in the past.",
                          (unsigned long long)server.cluster->failover_auth_epoch,
                          sender->name, sender->human_nodename,
                          (unsigned long long)sender->configEpoch);
```

Code after:
     * is just sending a lot of data in the cluster bus, for instance
     * because of Pub/Sub. */
    if (sender) sender->data_received = now;

    if (sender && !nodeInHandshake(sender)) {
        /* Update our currentEpoch if we see a newer epoch in the cluster. */
        sender_claimed_current_epoch = ntohu64(hdr->currentEpoch);
        sender_claimed_config_epoch = ntohu64(hdr->configEpoch);
        if (sender_claimed_current_epoch > server.cluster->currentEpoch)
            server.cluster->currentEpoch = sender_claimed_current_epoch;
        /* Update the sender configEpoch if it is a primary publishing a newer one. */
        if (sender_claims_to_be_primary && sender_claimed_config_epoch > sender->configEpoch) {
            sender->configEpoch = sender_claimed_config_epoch;
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_FSYNC_CONFIG);

            if (server.cluster->failover_auth_time && sender->configEpoch >= server.cluster->failover_auth_epoch) {
                /* Another node has claimed an epoch greater than or equal to ours.
                 * If we have an ongoing election, reset it because we cannot win
                 * with an epoch smaller than or equal to the incoming claim. This
                 * allows us to start a new election as soon as possible. */
                server.cluster->failover_auth_time = 0;
                serverLog(LL_WARNING,
                          "Failover election in progress for epoch %llu, but received a claim from "
                          "node %.40s (%s) with an equal or higher epoch %llu. Resetting the election "
                          "since we cannot win an election in the past.",
                          (unsigned long long)server.cluster->failover_auth_epoch,
                          sender->name, sender->human_nodename,
                          (unsigned long long)sender->configEpoch);
                /* Maybe we could start a new election, set a flag here to make sure
                 * we check as soon as possible, instead of waiting for a cron. */
                clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_FAILOVER);
            }
        }
        /* Update the replication offset info for this node. */
        sender->repl_offset = ntohu64(hdr->offset);
        sender->repl_offset_time = now;
        /* If we are a replica performing a manual failover and our primary
         * sent its offset while already paused, populate the MF state. */
        if (server.cluster->mf_end && nodeIsReplica(myself) && myself->replicaof == sender &&
            hdr->mflags[0] & CLUSTERMSG_FLAG0_PAUSED && server.cluster->mf_primary_offset == -1) {
            server.cluster->mf_primary_offset = sender->repl_offset;
            clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_MANUALFAILOVER);
            serverLog(LL_NOTICE,
                      "Received replication offset for paused "
                      "primary manual failover: %lld",
                      server.cluster->mf_primary_offset);
        }
    }

    /* Initial processing of PING and MEET requests replying with a PONG. */
    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) {