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

Code before:
        }

        /* Update our info about the node */
        if (!link->inbound && type == CLUSTERMSG_TYPE_PONG) {
            link->node->pong_received = now;
            link->node->ping_sent = 0;

            /* The PFAIL condition can be reversed without external
             * help if it is momentary (that is, if it does not
             * turn into a FAIL state).
             *
             * The FAIL condition is also reversible under specific
             * conditions detected by clearNodeFailureIfNeeded(). */
            if (nodeTimedOut(link->node)) {
                link->node->flags &= ~CLUSTER_NODE_PFAIL;
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE);
            } else if (nodeFailed(link->node)) {
                clearNodeFailureIfNeeded(link->node);
            }
        }

        /* Check for role switch: replica -> primary or primary -> replica. */
        if (sender) {
            if (sender_claims_to_be_primary) {
                /* Node is a primary. */
                if (nodeIsReplica(sender)) {
                    serverLog(LL_DEBUG, "node %.40s (%s) announces that it is a %s in shard %.40s", sender->name,
                              sender->human_nodename, sender_claims_to_be_primary ? "primary" : "replica", sender->shard_id);
                    clusterSetNodeAsPrimary(sender);
                }
            } else {
                /* Node is a replica. */
                clusterNode *sender_claimed_primary = clusterLookupNode(hdr->replicaof, CLUSTER_NAMELEN);

                if (sender_last_reported_as_primary) {
                    serverLog(LL_DEBUG, "node %.40s (%s) announces that it is a %s in shard %.40s", sender->name,
                              sender->human_nodename, sender_claims_to_be_primary ? "primary" : "replica", sender->shard_id);

                    /* Primary turned into a replica! Reconfigure the node. */
                    if (sender_claimed_primary && areInSameShard(sender_claimed_primary, sender)) {
                        /* `sender` was a primary and was in the same shard as its new primary */
                        if (nodeEpoch(sender_claimed_primary) > sender_claimed_config_epoch) {
                            serverLog(LL_NOTICE,
                                      "Ignore stale message from %.40s (%s) in shard %.40s;"
                                      " gossip config epoch: %llu, current config epoch: %llu",
                                      sender->name, sender->human_nodename, sender->shard_id,
                                      (unsigned long long)sender_claimed_config_epoch,
                                      (unsigned long long)nodeEpoch(sender_claimed_primary));
                            /* This packet is stale so we avoid processing it anymore. Otherwise
                             * this may cause a primary-replica chain issue. */
                            return 1;

Review comment (suggestion):
use `sender_last_reported_as_primary` so it is more symmetric to the other branch?
```suggestion
                if (sender_last_reported_as_replica) {
```

Code after:
        }

        /* Update our info about the node */
        if (!link->inbound && type == CLUSTERMSG_TYPE_PONG) {
            link->node->pong_received = now;
            link->node->ping_sent = 0;

            /* The PFAIL condition can be reversed without external
             * help if it is momentary (that is, if it does not
             * turn into a FAIL state).
             *
             * The FAIL condition is also reversible under specific
             * conditions detected by clearNodeFailureIfNeeded(). */
            if (nodeTimedOut(link->node)) {
                link->node->flags &= ~CLUSTER_NODE_PFAIL;
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE);
            } else if (nodeFailed(link->node)) {
                clearNodeFailureIfNeeded(link->node);
            }
        }

        /* Check for role switch: replica -> primary or primary -> replica. */
        if (sender) {
            if (sender_claims_to_be_primary) {
                /* Node is a primary. */
                if (sender_last_reported_as_replica) {
                    serverLog(LL_DEBUG, "node %.40s (%s) announces that it is a %s in shard %.40s", sender->name,
                              sender->human_nodename, sender_claims_to_be_primary ? "primary" : "replica", sender->shard_id);
                    clusterSetNodeAsPrimary(sender);
                }
            } else {
                /* Node is a replica. */
                clusterNode *sender_claimed_primary = clusterLookupNode(hdr->replicaof, CLUSTER_NAMELEN);

                if (sender_last_reported_as_primary) {
                    serverLog(LL_DEBUG, "node %.40s (%s) announces that it is a %s in shard %.40s", sender->name,
                              sender->human_nodename, sender_claims_to_be_primary ? "primary" : "replica", sender->shard_id);

                    /* Primary turned into a replica! Reconfigure the node. */
                    if (sender_claimed_primary && areInSameShard(sender_claimed_primary, sender)) {
                        /* `sender` was a primary and was in the same shard as its new primary */
                        if (nodeEpoch(sender_claimed_primary) > sender_claimed_config_epoch) {
                            serverLog(LL_NOTICE,
                                      "Ignore stale message from %.40s (%s) in shard %.40s;"
                                      " gossip config epoch: %llu, current config epoch: %llu",
                                      sender->name, sender->human_nodename, sender->shard_id,
                                      (unsigned long long)sender_claimed_config_epoch,
                                      (unsigned long long)nodeEpoch(sender_claimed_primary));
                            /* This packet is stale so we avoid processing it anymore. Otherwise
                             * this may cause a primary-replica chain issue. */
                            return 1;