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

Code before:
        }

        if (type == CLUSTERMSG_TYPE_MEET) {
            if (!sender) {
                if (!link->node) {
                    char ip[NET_IP_STR_LEN] = {0};
                    if (nodeIp2String(ip, link, hdr->myip) != C_OK) {
                        /* Unable to retrieve the node's IP address from the connection. Without a
                         * valid IP, the node becomes unusable in the cluster. This failure might be
                         * due to the connection being closed. */
                        serverLog(LL_NOTICE, "Closing cluster link due to failure to retrieve IP from the connection, "
                                             "possibly caused by a closed connection.");
                        freeClusterLink(link);
                        return 0;
                    }

                    /* Add this node if it is new for us and the msg type is MEET.
                     * In this stage we don't try to add the node with the right
                     * flags, replicaof pointer, and so forth, as this details will be
                     * resolved when we'll receive PONGs from the node. The exception
                     * to this is the flag that indicates extensions are supported, as
                     * we want to send extensions right away in the return PONG in order
                     * to reduce the amount of time needed to stabilize the shard ID. */
                    clusterNode *node = createClusterNode(NULL, CLUSTER_NODE_HANDSHAKE);
                    memcpy(node->ip, ip, sizeof(ip));
                    getClientPortFromClusterMsg(hdr, &node->tls_port, &node->tcp_port);
                    node->cport = ntohs(hdr->cport);
                    setClusterNodeToInboundClusterLink(node, link);
                    clusterAddNode(node);
                    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
                } else {
                    /* A second MEET packet was received on an existing link during the handshake
                     * process. This happens when the other node detects no inbound link, and
                     * re-sends a MEET packet before this node can respond with a PING.
                     * This MEET is a no-op.
                     *
                     * Note: Nodes in HANDSHAKE state are not fully "known" (random names), so the
                     * sender remains unidentified at this point. The MEET packet might be re-sent
                     * if the inbound connection is still unestablished by the next cron cycle. */
                    debugServerAssert(link->inbound && nodeInHandshake(link->node));
                }

                /* If this is a MEET packet from an unknown node, we still process
                 * the gossip section here since we have to trust the sender because
                 * of the message type. */
                clusterProcessGossipSection(hdr, link);
            } else if (sender->link && nodeExceedsHandshakeTimeout(sender, now)) {
                /* The MEET packet is from a known node, after the handshake timeout, so the sender
                 * thinks that I do not know it.
                 * Free my outbound link to that node, triggering a reconnect and a PING over the
                 * new link.

Review comment (refactor):
This trick was used to propagate extension support as part of the node flags in the gossip section. By moving the abstraction to link level, we would need direct message between two nodes to be aware about the extension support.

So, I think we should leave it as is.

Code after:
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
            }
        }

        if (type == CLUSTERMSG_TYPE_MEET) {
            if (!sender) {
                if (!link->node) {
                    char ip[NET_IP_STR_LEN] = {0};
                    if (nodeIp2String(ip, link, hdr->myip) != C_OK) {
                        /* Unable to retrieve the node's IP address from the connection. Without a
                         * valid IP, the node becomes unusable in the cluster. This failure might be
                         * due to the connection being closed. */
                        serverLog(LL_NOTICE, "Closing cluster link due to failure to retrieve IP from the connection, "
                                             "possibly caused by a closed connection.");
                        freeClusterLink(link);
                        return 0;
                    }

                    /* Add this node if it is new for us and the msg type is MEET.
                     * In this stage we don't try to add the node with the right
                     * flags, replicaof pointer, and so forth, as this details will be
                     * resolved when we'll receive PONGs from the node. The exception
                     * to this is the flag that indicates extensions are supported, as
                     * we want to send extensions right away in the return PONG in order
                     * to reduce the amount of time needed to stabilize the shard ID. */
                    clusterNode *node = createClusterNode(NULL, CLUSTER_NODE_HANDSHAKE);
                    memcpy(node->ip, ip, sizeof(ip));
                    getClientPortFromClusterMsg(hdr, &node->tls_port, &node->tcp_port);
                    node->cport = ntohs(hdr->cport);
                    if (hdr->mflags[0] & CLUSTERMSG_FLAG0_EXT_DATA) {
                        node->flags |= CLUSTER_NODE_EXTENSIONS_SUPPORTED;
                    }
                    setClusterNodeToInboundClusterLink(node, link);
                    clusterAddNode(node);
                    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
                } else {
                    /* A second MEET packet was received on an existing link during the handshake
                     * process. This happens when the other node detects no inbound link, and
                     * re-sends a MEET packet before this node can respond with a PING.
                     * This MEET is a no-op.
                     *
                     * Note: Nodes in HANDSHAKE state are not fully "known" (random names), so the
                     * sender remains unidentified at this point. The MEET packet might be re-sent
                     * if the inbound connection is still unestablished by the next cron cycle. */
                    debugServerAssert(link->inbound && nodeInHandshake(link->node));
                }

                /* If this is a MEET packet from an unknown node, we still process
                 * the gossip section here since we have to trust the sender because
                 * of the message type. */
                clusterProcessGossipSection(hdr, link);