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

Code before:

    /* Initialize stats */
    for (int i = 0; i < CLUSTERMSG_TYPE_COUNT; i++) {
        server.cluster->stats_bus_messages_sent[i] = 0;
        server.cluster->stats_bus_messages_received[i] = 0;
    }
    server.cluster->stats_pfail_nodes = 0;
    server.cluster->stat_cluster_links_buffer_limit_exceeded = 0;

    memset(server.cluster->slots, 0, sizeof(server.cluster->slots));
    clusterCloseAllSlots();

    memset(server.cluster->owner_not_claiming_slot, 0, sizeof(server.cluster->owner_not_claiming_slot));

    /* Lock the cluster config file to make sure every node uses
     * its own nodes.conf. */
    server.cluster_config_file_lock_fd = -1;
    if (clusterLockConfig(server.cluster_configfile) == C_ERR) exit(1);

    /* Load or create a new nodes configuration. */
    if (clusterLoadConfig(server.cluster_configfile) == C_ERR) {
        /* No configuration found. We will just use the random name provided
         * by the createClusterNode() function. */
        myself = server.cluster->myself = createClusterNode(NULL, CLUSTER_NODE_MYSELF | CLUSTER_NODE_PRIMARY);
        serverLog(LL_NOTICE, "No cluster configuration found, I'm %.40s", myself->name);
        myself->flags |= CLUSTER_NODE_LIGHT_HDR_SUPPORTED;
        clusterAddNode(myself);
        clusterAddNodeToShard(myself->shard_id, myself);
        saveconf = 1;
    }
    if (saveconf) clusterSaveConfigOrDie(1);

    /* Port sanity check II
     * The other handshake port check is triggered too late to stop
     * us from trying to use a too-high cluster port number. */
    int port = defaultClientPort();
    if (!server.cluster_port && port > (65535 - CLUSTER_PORT_INCR)) {
        serverLog(LL_WARNING,
                  "%s port number too high. "
                  "Cluster communication port is 10,000 port "
                  "numbers higher than your %s port. "
                  "Your %s port number must be 55535 or less.",
                  SERVER_TITLE, SERVER_TITLE, SERVER_TITLE);
        exit(1);
    }
    if (!server.bindaddr_count) {
        serverLog(LL_WARNING, "No bind address is configured, but it is required for the Cluster bus.");
        exit(1);
    }

    /* Register our own rdb aux fields */

Review comment (bug):
Why not call `clusterUpdateMyselfFlags()` here? (This is the case when loading nodes.conf failed.)

In the future we may want to init more flags and then we can keep that in one place.

Code after:
        changed = 1;
    else if (prev_ip && curr_ip && strcmp(prev_ip, curr_ip))
        changed = 1;

    if (changed) {
        if (prev_ip) zfree(prev_ip);
        prev_ip = curr_ip;

        if (curr_ip) {
            /* We always take a copy of the previous IP address, by
             * duplicating the string. This way later we can check if
             * the address really changed. */
            prev_ip = zstrdup(prev_ip);
            valkey_strlcpy(myself->ip, server.cluster_announce_ip, NET_IP_STR_LEN);
        } else {
            myself->ip[0] = '\0'; /* Force autodetection. */
        }
    }
}

static void updateSdsExtensionField(char **field, const char *value) {
    if (value != NULL && !strcmp(value, *field)) {
        return;
    } else if (value == NULL && sdslen(*field) == 0) {
        return;
    }

    if (value != NULL) {
        *field = sdscpy(*field, value);
    } else {
        sdsclear(*field);
    }
    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
}

/* Update the hostname for the specified node with the provided C string. */
static void updateAnnouncedHostname(clusterNode *node, char *value) {
    updateSdsExtensionField(&node->hostname, value);
}

static void updateAnnouncedHumanNodename(clusterNode *node, char *value) {
    updateSdsExtensionField(&node->human_nodename, value);
}

static void updateAnnouncedClientIpV4(clusterNode *node, char *value) {
    updateSdsExtensionField(&node->announce_client_ipv4, value);
}

static void updateAnnouncedClientIpV6(clusterNode *node, char *value) {
    updateSdsExtensionField(&node->announce_client_ipv6, value);
}