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

Code before:
        if (old_style)
            serverLog(LL_NOTICE, "Reading RDB preamble from AOF file...");
        else
            serverLog(LL_NOTICE, "Reading RDB base file on AOF loading...");

        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);
                }
            }


Review comment (refactor):
This code is duplicated from `loadDataFromDisk()` where an RDB file is loaded. Let's avoid duplicated code by moving this common code to a function?

Code after:
        if (old_style)
            serverLog(LL_NOTICE, "Reading RDB preamble from AOF file...");
        else
            serverLog(LL_NOTICE, "Reading RDB base file on AOF loading...");

        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);