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

Code before:
        if (fseek(fp, 0, SEEK_SET) == -1) goto readerr;
        rioInitWithFile(&rdb, fp);
        rdbSaveInfo rsi = RDB_SAVE_INFO_INIT;
        int rdb_flags = RDBFLAGS_AOF_PREAMBLE;
        int rsi_is_valid = 0;
        if (iAmPrimary()) {
            if (server.repl_backlog == NULL) createReplicationBacklog();
            rdb_flags |= RDBFLAGS_FEED_REPL;
        }
        if (rdbLoadRio(&rdb, rdb_flags, &rsi) != C_OK) {
            if (old_style)
                serverLog(LL_WARNING, "Error reading the RDB preamble of the AOF file %s, AOF loading aborted",
                          filename);
            else
                serverLog(LL_WARNING, "Error reading the RDB base file %s, AOF loading aborted", filename);

            ret = AOF_FAILED;
            goto cleanup;
        } else {
            /* Restore the replication ID / offset from the RDB file. */
            if (rsi.repl_id_is_set && rsi.repl_offset != -1 && rsi.repl_stream_db != -1) {
                rsi_is_valid = 1;
                if (!iAmPrimary()) {
                    memcpy(server.replid, rsi.repl_id, sizeof(server.replid));
                    server.primary_repl_offset = rsi.repl_offset;
                    if (!server.primary && !server.cached_primary) {
                        /* only cache myself primary if replica did not synced to its primary node yet */
                        replicationCachePrimaryUsingMyself();
                        selectDb(server.cached_primary, rsi.repl_stream_db);
                    }
                    serverLog(LL_NOTICE, "Loading preamble rdb changed replication info, replid: %s, primary_repl_offset: %lld",
                              server.replid, server.primary_repl_offset);
                } else {
                    memcpy(server.replid2, rsi.repl_id, sizeof(server.replid));
                    server.second_replid_offset = rsi.repl_offset + 1;
                    server.primary_repl_offset += rsi.repl_offset;
                    serverAssert(server.repl_backlog);
                    server.repl_backlog->offset = server.primary_repl_offset - server.repl_backlog->histlen + 1;
                    rebaseReplicationBuffer(rsi.repl_offset);
                    server.repl_no_replicas_since = time(NULL);
                    serverLog(LL_NOTICE, "Loading preamble rdb changed replication info, replid2: %s, secondary_repl_offset: "
                                         "%lld, primary_repl_offset: %lld",
                              server.replid2, server.second_replid_offset, server.primary_repl_offset);
                }
            }

            loadingAbsProgress(ftello(fp));
            last_progress_report_size = ftello(fp);
            if (old_style) serverLog(LL_NOTICE, "Reading the remaining AOF tail...");
        }
        if (!rsi_is_valid && server.repl_backlog && listLength(server.replicas) == 0) freeReplicationBacklog();

Review comment (bug):
I did not merge the code cause we have a little difference here.

This condition is a workaround for `debug loadaof`, if we call this after the instance becomes a replica, then we should not call `replicationCachePrimaryUsingMyself` here.

Code after:
        if (fseek(fp, 0, SEEK_SET) == -1) goto readerr;
        rioInitWithFile(&rdb, fp);
        rdbSaveInfo rsi = RDB_SAVE_INFO_INIT;
        int rdb_flags = RDBFLAGS_AOF_PREAMBLE;
        int rsi_is_valid = 0;
        if (iAmPrimary()) {
            if (server.repl_backlog == NULL) createReplicationBacklog();
            rdb_flags |= RDBFLAGS_FEED_REPL;
        }
        if (rdbLoadRio(&rdb, rdb_flags, &rsi) != C_OK) {
            if (old_style)
                serverLog(LL_WARNING, "Error reading the RDB preamble of the AOF file %s, AOF loading aborted",
                          filename);
            else
                serverLog(LL_WARNING, "Error reading the RDB base file %s, AOF loading aborted", filename);

            ret = AOF_FAILED;
            goto cleanup;
        } else {
            /* Restore the replication ID / offset from the RDB file. */
            rsi_is_valid = rdbRestoreOffsetFromSaveInfo(&rsi, true);
            loadingAbsProgress(ftello(fp));
            last_progress_report_size = ftello(fp);
            if (old_style) serverLog(LL_NOTICE, "Reading the remaining AOF tail...");
        }
        /* If the AOF didn't contain replication info, it's not possible to
         * support partial resync, so we can free the backlog to save memory. */
        if (!rsi_is_valid && server.repl_backlog && listLength(server.replicas) == 0) freeReplicationBacklog();
    }

    /* Read the actual AOF file, in REPL format, command by command. */
    while (1) {
        int argc, j;
        unsigned long len;
        robj **argv;
        char buf[AOF_ANNOTATION_LINE_MAX_LEN];
        sds argsds;
        struct serverCommand *cmd;

        /* Serve the clients from time to time */
        if (!(loops++ % 1024)) {
            off_t progress_delta = ftello(fp) - last_progress_report_size;
            loadingIncrProgress(progress_delta);
            last_progress_report_size += progress_delta;
            processEventsWhileBlocked();
            processModuleLoadingProgressEvent(1);
        }
        if (fgets(buf, sizeof(buf), fp) == NULL) {
            if (feof(fp)) {
                break;
            } else {