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

Code before:
         * command must generate a SELECT statement. */
        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 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;

Review comment (suggestion):
"valid" here means that the save info contained replication ID and offset and could be successfully restored, right? Let's make the comment more explicit?

```suggestion
 * return 1 if replication ID and offset were restored from the rdbSaveInfo */
```

Code after:
         * command must generate a SELECT statement. */
        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;