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

Code before:
 *
 * The rdbver argument is the RDB version to use. It should be calculated based
 * on what the replicas reported using REPLCONF VERSION.
 *
 * Side effects, other than starting a BGSAVE:
 *
 * 1) Handle the replicas in WAIT_START state, by preparing them for a full
 *    sync if the BGSAVE was successfully started, or sending them an error
 *    and dropping them from the list of replicas.
 *
 * 2) Flush the Lua scripting script cache if the BGSAVE was actually
 *    started.
 *
 * Returns C_OK on success or C_ERR otherwise. */
int startBgsaveForReplication(int mincapa, int req, int rdbver) {
    int retval;
    int socket_target = 0;
    listIter li;
    listNode *ln;

    /* We use a socket target if replica can handle the EOF marker and we're
     * configured to do diskless syncs.
     *
     * Note that in case we're creating a "filtered" RDB (functions-only, for
     * example) or an older RDB version, we also force socket replication to
     * avoid overwriting the snapshot RDB file with filtered data. */
    socket_target = (mincapa & REPLICA_CAPA_EOF) && (server.repl_diskless_sync ||
                                                     (req & REPLICA_REQ_RDB_MASK) ||
                                                     rdbver != RDB_VERSION);
    /* `SYNC` should have failed with error if we don't support socket and require a filter, assert this here */
    serverAssert(socket_target || !(req & REPLICA_REQ_RDB_MASK));

    serverLog(LL_NOTICE, "Starting BGSAVE for SYNC with target: %s using: %s",
              socket_target ? "replicas sockets" : "disk",
              (req & REPLICA_REQ_RDB_CHANNEL) ? "dual-channel" : "normal sync");

    rdbSaveInfo rsi, *rsiptr;
    rsiptr = rdbPopulateSaveInfo(&rsi);
    /* Only do rdbSave* when rsiptr is not NULL,
     * otherwise replica will miss repl-stream-db. */
    if (rsiptr) {
        if (socket_target)
            retval = rdbSaveToReplicasSockets(req, rdbver, rsiptr);
        else {
            /* Keep the page cache since it'll get used soon */
            retval = rdbSaveBackground(req, server.rdb_filename, rsiptr, RDBFLAGS_REPLICATION | RDBFLAGS_KEEP_CACHE);
        }
        if (server.debug_pause_after_fork) debugPauseProcess();
    } else {
        serverLog(LL_WARNING, "BGSAVE for replication: replication information not available, can't generate the RDB "
                              "file right now. Try later.");

Review comment (suggestion):
small note: in case of different RDB version, disk based will also not work since we will use the same RDB for all the syncing replicas each might have a different version. this is another motivation to force diskless sync. suggest mentioning that 

Code after:

/* Start a BGSAVE for replication goals, which is, selecting the disk or
 * socket target depending on the configuration, and making sure that
 * the script cache is flushed before to start.
 *
 * The mincapa argument is the bitwise AND among all the replicas capabilities
 * of the replicas waiting for this BGSAVE, so represents the replica capabilities
 * all the replicas support. Can be tested via REPLICA_CAPA_* macros.
 *
 * The rdbver argument is the RDB version to use. It should be calculated based
 * on what the replicas reported using REPLCONF VERSION.
 *
 * Side effects, other than starting a BGSAVE:
 *
 * 1) Handle the replicas in WAIT_START state, by preparing them for a full
 *    sync if the BGSAVE was successfully started, or sending them an error
 *    and dropping them from the list of replicas.
 *
 * 2) Flush the Lua scripting script cache if the BGSAVE was actually
 *    started.
 *
 * Returns C_OK on success or C_ERR otherwise. */
int startBgsaveForReplication(int mincapa, int req, int rdbver) {
    int retval;
    int socket_target = 0;
    listIter li;
    listNode *ln;

    /* We use a socket target if replica can handle the EOF marker and we're
     * configured to do diskless syncs.
     *
     * Note that in case we're creating a "filtered" RDB (functions-only, for
     * example) or an older RDB version, we also force socket replication to
     * avoid overwriting the snapshot RDB file, which needs to be usable by
     * other replicas (not using filtered RDB or older versions) in disk-based
     * full sync. */
    socket_target = (mincapa & REPLICA_CAPA_EOF) && (server.repl_diskless_sync ||
                                                     (req & REPLICA_REQ_RDB_MASK) ||
                                                     rdbver != RDB_VERSION);
    /* `SYNC` should have failed with error if we don't support socket and require a filter, assert this here */
    serverAssert(socket_target || !(req & REPLICA_REQ_RDB_MASK));

    serverLog(LL_NOTICE, "Starting BGSAVE for SYNC with target: %s using: %s",
              socket_target ? "replicas sockets" : "disk",
              (req & REPLICA_REQ_RDB_CHANNEL) ? "dual-channel" : "normal sync");

    rdbSaveInfo rsi, *rsiptr;
    rsiptr = rdbPopulateSaveInfo(&rsi);
    /* Only do rdbSave* when rsiptr is not NULL,
     * otherwise replica will miss repl-stream-db. */
    if (rsiptr) {