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

Code before:
        rsi->repl_stream_db = server.replicas_eldb == -1 ? 0 : server.replicas_eldb;
        return rsi;
    }

    /* If the instance is a replica we need a connected primary
     * in order to fetch the currently selected DB. */
    if (server.primary) {
        rsi->repl_stream_db = server.primary->db->id;
        return rsi;
    }

    /* If we have a cached primary we can use it in order to populate the
     * replication selected DB info inside the RDB file: the replica can
     * increment the primary_repl_offset only from data arriving from the
     * primary, so if we are disconnected the offset in the cached primary
     * is valid. */
    if (server.cached_primary) {
        rsi->repl_stream_db = server.cached_primary->db->id;
        return rsi;
    }
    return NULL;
}

/* Restore the replication ID / offset from the RDB file
 * return 1 if rdbSaveInfo is valid */
int replicationRestoreOffsetFromSaveInfo(rdbSaveInfo *rsi, bool is_aof_preamble) {
    int rsi_is_valid = 0;
    if (rsi == NULL) return rsi_is_valid;
    if (rsi->repl_id_is_set && rsi->repl_offset != -1 && rsi->repl_stream_db != -1) {
        /* Note that older implementations may save a repl_stream_db
         * of -1 inside the RDB file in a wrong way, see more
         * information in function rdbPopulateSaveInfo. */
        rsi_is_valid = 1;
        if (!iAmPrimary()) {
            memcpy(server.replid, rsi->repl_id, sizeof(server.replid));
            server.primary_repl_offset = rsi->repl_offset;
            if (!is_aof_preamble || (!server.primary && !server.cached_primary)) {
                /* If this is a replica, create a cached primary from this
                 * information, in order to allow partial resynchronizations
                 * with primaries. For AOF, only cache the primary if replica
                 * has not synced to its primary node yet. */
                replicationCachePrimaryUsingMyself();
                selectDb(server.cached_primary, rsi->repl_stream_db);
            }
        } else {
            /* If this is a primary, we can save the replication info
             * as secondary ID and offset, in order to allow replicas
             * to partial resynchronizations with primaries. */
            memcpy(server.replid2, rsi->repl_id, sizeof(server.replid));
            server.second_replid_offset = rsi->repl_offset + 1;
            /* Rebase primary_repl_offset from rsi.repl_offset. */

Review comment (suggestion):
With the prefix "replication" I was expecting it to be in `replication.c`.

If you think it better belongs in `rdb.c`, then it should use "rdb" prefix instead.

I'm not sure what's better. My thinking was that `rdb.c` is lower level library and it shouldn't access globals like the `server` object, while `replication.c` is more tightly coupled with the server state.

Code after:
        rsi->repl_stream_db = server.replicas_eldb == -1 ? 0 : server.replicas_eldb;
        return rsi;
    }

    /* If the instance is a replica we need a connected primary
     * in order to fetch the currently selected DB. */
    if (server.primary) {
        rsi->repl_stream_db = server.primary->db->id;
        return rsi;
    }

    /* If we have a cached primary we can use it in order to populate the
     * replication selected DB info inside the RDB file: the replica can
     * increment the primary_repl_offset only from data arriving from the
     * primary, so if we are disconnected the offset in the cached primary
     * is valid. */
    if (server.cached_primary) {
        rsi->repl_stream_db = server.cached_primary->db->id;
        return rsi;
    }
    return NULL;
}

/* Restore the replication ID / offset from the RDB file
 * return 1 if replication ID and offset were restored from the rdbSaveInfo */
int rdbRestoreOffsetFromSaveInfo(rdbSaveInfo *rsi, bool is_aof_preamble) {
    int rsi_is_valid = 0;
    serverAssert(rsi != NULL);
    if (rsi->repl_id_is_set && rsi->repl_offset != -1 && rsi->repl_stream_db != -1) {
        /* Note that older implementations may save a repl_stream_db
         * of -1 inside the RDB file in a wrong way, see more
         * information in function rdbPopulateSaveInfo. */
        rsi_is_valid = 1;
        if (!iAmPrimary()) {
            memcpy(server.replid, rsi->repl_id, sizeof(server.replid));
            server.primary_repl_offset = rsi->repl_offset;
            if (!is_aof_preamble || (!server.primary && !server.cached_primary)) {
                /* If this is a replica, create a cached primary from this
                 * information, in order to allow partial resynchronizations
                 * with primaries. For AOF, only cache the primary if replica
                 * has not synced to its primary node yet. */
                replicationCachePrimaryUsingMyself();
                selectDb(server.cached_primary, rsi->repl_stream_db);
            }
        } else {
            /* If this is a primary, we can save the replication info
             * as secondary ID and offset, in order to allow replicas
             * to partial resynchronizations with primaries. */
            memcpy(server.replid2, rsi->repl_id, sizeof(server.replid));
            server.second_replid_offset = rsi->repl_offset + 1;
            /* Rebase primary_repl_offset from rsi.repl_offset. */