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

Code before:
        /* Check if the node supports extensions. */
        if (linkSupportsExtension(link)) {
            sender->flags |= CLUSTER_NODE_EXTENSIONS_SUPPORTED;
        }

        /* Check if the node supports light publish message hdr */
        if (flags & CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED) {
            sender->flags |= CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED;
        } else {
            sender->flags &= ~CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED;
        }

        /* Check if the node supports light module message hdr */
        if (flags & CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED) {
            sender->flags |= CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
        } else {
            sender->flags &= ~CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
        }
    }

    /* Update the last time we saw any data from this node. We
     * use this in order to avoid detecting a timeout from a node that
     * 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 && server.cluster->failover_auth_sent &&
                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);

Review comment (suggestion):
We don't really need this code, because CLUSTER_NODE_MULTI_MEET_SUPPORT == CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED and we already have the same code for CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED. We rely on them being equal for the compatibility logic (e.g. when running a mixed cluster with 8.1.0 and 8.1.5).

Code after:
    if (sender) {
        /* Check if the node supports extensions. */
        if (linkSupportsExtension(link)) {
            sender->flags |= CLUSTER_NODE_EXTENSIONS_SUPPORTED;
        }

        /* Check if the node supports light publish message hdr */
        if (flags & CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED) {
            sender->flags |= CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED;
        } else {
            sender->flags &= ~CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED;
        }

        /* Check if the node supports light module message hdr */
        if (flags & CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED) {
            sender->flags |= CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
        } else {
            sender->flags &= ~CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
        }

        /* Check if the node can handle multi meet packet. */
        if (flags & CLUSTER_NODE_MULTI_MEET_SUPPORTED) {
            sender->flags |= CLUSTER_NODE_MULTI_MEET_SUPPORTED;
        } else {
            sender->flags &= ~CLUSTER_NODE_MULTI_MEET_SUPPORTED;
        }
    }

    /* Update the last time we saw any data from this node. We
     * use this in order to avoid detecting a timeout from a node that
     * 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 && server.cluster->failover_auth_sent &&
                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. */