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

Code before:
    }
    if (retval == C_ERR) {
        latencyStartMonitor(latency);
        unlink(tmpfilename);
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("cluster-config-unlink", latency);
        latencyTraceIfNeeded(cluster, cluster_config_unlink, latency);
    }
    sdsfree(tmpfilename);
    sdsfree(ci);
    return retval;
}

/* Save the cluster configuration file. If the save fails, exit the process. */
void clusterSaveConfigOrDie(int do_fsync) {
    if (clusterSaveConfig(do_fsync) == C_ERR) {
        serverLog(LL_WARNING, "Fatal: can't update cluster config file.");
        exit(1);
    }
}

/* Save the cluster configuration file. If the save fails, print the log. */
void clusterSaveConfigOrLog(int do_fsync) {
    if (clusterSaveConfig(do_fsync) == C_ERR) {
        serverLog(LL_WARNING, "Cluster config file is applying a change even though "
                              "it is unable to write to disk.");
    }
}

/* Lock the cluster config using flock(), and retain the file descriptor used to
 * acquire the lock so that the file will be locked as long as the process is up.
 *
 * This works because we always update nodes.conf with a new version
 * in-place, reopening the file, and writing to it in place (later adjusting
 * the length with ftruncate()).
 *
 * On success C_OK is returned, otherwise an error is logged and
 * the function returns C_ERR to signal a lock was not acquired. */
int clusterLockConfig(char *filename) {
/* flock() does not exist on Solaris
 * and a fcntl-based solution won't help, as we constantly re-open that file,
 * which will release _all_ locks anyway
 */
#if !defined(__sun)
    /* To lock it, we need to open the file in a way it is created if
     * it does not exist, otherwise there is a race condition with other
     * processes. */
    int fd = open(filename, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
    if (fd == -1) {
        serverLog(LL_WARNING, "Can't open %s in order to acquire a lock: %s", filename, strerror(errno));
        return C_ERR;

Review comment (bug):
Slightly strange English. Maybe better:

```suggestion
        serverLog(LL_WARNING, "Cluster config updated even though writing "
                              "the cluster config file to disk failed.");
```

Code after:
            goto cleanup;
        }
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("cluster-config-dir-fsync", latency);
        latencyTraceIfNeeded(cluster, cluster_config_dir_fsync, latency);
    }
    retval = C_OK; /* If we reached this point, everything is fine. */

cleanup:
    if (fd != -1) {
        latencyStartMonitor(latency);
        close(fd);
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("cluster-config-close", latency);
        latencyTraceIfNeeded(cluster, cluster_config_close, latency);
    }
    if (retval == C_ERR) {
        latencyStartMonitor(latency);
        unlink(tmpfilename);
        latencyEndMonitor(latency);
        latencyAddSampleIfNeeded("cluster-config-unlink", latency);
        latencyTraceIfNeeded(cluster, cluster_config_unlink, latency);
    }
    sdsfree(tmpfilename);
    sdsfree(ci);
    return retval;
}

/* Save the cluster configuration file. If the save fails, exit the process. */
void clusterSaveConfigOrDie(int do_fsync) {
    if (clusterSaveConfig(do_fsync) == C_ERR) {
        serverLog(LL_WARNING, "Fatal: can't update cluster config file.");
        exit(1);
    }
}

/* Save the cluster configuration file. If the save fails, print the log. */
#define CONFIG_SAVE_LOG_ERROR_RATE 30 /* Seconds between errors logging. */
void clusterSaveConfigOrLog(int do_fsync) {
    if (clusterSaveConfig(do_fsync) == C_ERR) {
        static time_t last_save_error_log = 0;
        /* Limit logging rate to 1 line per CONFIG_SAVE_LOG_ERROR_RATE seconds. */
        if ((server.unixtime - last_save_error_log) > CONFIG_SAVE_LOG_ERROR_RATE) {
            serverLog(LL_WARNING, "Cluster config updated even though writing "
                                  "the cluster config file to disk failed.");
            last_save_error_log = server.unixtime;
        }
    }
}

/* Lock the cluster config using flock(), and retain the file descriptor used to