Skip to content

Commit

Permalink
Fix aof base suffix when modifying aof-use-rdb-preamble during rewrite (
Browse files Browse the repository at this point in the history
#886)

If we modify aof-use-rdb-preamble in the middle of rewrite,
we may get a wrong aof base suffix. This is because the suffix
is concatenated by the main process afterwards, and it may be
different from the beginning.

We cache this value when we start the rewrite.

Signed-off-by: Binbin <binloveplay1314@qq.com>
  • Loading branch information
enjoy-binbin authored Sep 7, 2024
1 parent 9b51949 commit 6478526
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/aof.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ void aofManifestFreeAndUpdate(aofManifest *am) {
* appendonly.aof.1.base.aof (server.aof_use_rdb_preamble is no)
* appendonly.aof.1.base.rdb (server.aof_use_rdb_preamble is yes)
*/
sds getNewBaseFileNameAndMarkPreAsHistory(aofManifest *am) {
sds getNewBaseFileNameAndMarkPreAsHistory(aofManifest *am, int aof_use_rdb_preamble) {
serverAssert(am != NULL);
if (am->base_aof_info) {
serverAssert(am->base_aof_info->file_type == AOF_FILE_TYPE_BASE);
am->base_aof_info->file_type = AOF_FILE_TYPE_HIST;
listAddNodeHead(am->history_aof_list, am->base_aof_info);
}

char *format_suffix = server.aof_use_rdb_preamble ? RDB_FORMAT_SUFFIX : AOF_FORMAT_SUFFIX;
char *format_suffix = aof_use_rdb_preamble ? RDB_FORMAT_SUFFIX : AOF_FORMAT_SUFFIX;

aofInfo *ai = aofInfoCreate();
ai->file_name = sdscatprintf(sdsempty(), "%s.%lld%s%s", server.aof_filename, ++am->curr_base_file_seq,
Expand Down Expand Up @@ -712,7 +712,7 @@ void aofOpenIfNeededOnServerStart(void) {
/* If we start with an empty dataset, we will force create a BASE file. */
size_t incr_aof_len = listLength(server.aof_manifest->incr_aof_list);
if (!server.aof_manifest->base_aof_info && !incr_aof_len) {
sds base_name = getNewBaseFileNameAndMarkPreAsHistory(server.aof_manifest);
sds base_name = getNewBaseFileNameAndMarkPreAsHistory(server.aof_manifest, server.aof_use_rdb_preamble);
sds base_filepath = makePath(server.aof_dirname, base_name);
if (rewriteAppendOnlyFile(base_filepath) != C_OK) {
exit(1);
Expand Down Expand Up @@ -2445,6 +2445,7 @@ int rewriteAppendOnlyFileBackground(void) {
serverLog(LL_NOTICE, "Background append only file rewriting started by pid %ld", (long)childpid);
server.aof_rewrite_scheduled = 0;
server.aof_rewrite_time_start = time(NULL);
server.aof_rewrite_use_rdb_preamble = server.aof_use_rdb_preamble;
return C_OK;
}
return C_OK; /* unreached */
Expand Down Expand Up @@ -2557,7 +2558,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {

/* Get a new BASE file name and mark the previous (if we have)
* as the HISTORY type. */
sds new_base_filename = getNewBaseFileNameAndMarkPreAsHistory(temp_am);
sds new_base_filename = getNewBaseFileNameAndMarkPreAsHistory(temp_am, server.aof_rewrite_use_rdb_preamble);
serverAssert(new_base_filename != NULL);
new_base_filepath = makePath(server.aof_dirname, new_base_filename);

Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,7 @@ struct valkeyServer {
int aof_last_write_errno; /* Valid if aof write/fsync status is ERR */
int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
int aof_use_rdb_preamble; /* Specify base AOF to use RDB encoding on AOF rewrites. */
int aof_rewrite_use_rdb_preamble; /* Base AOF to use RDB encoding on AOF rewrites start. */
_Atomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
_Atomic int aof_bio_fsync_errno; /* Errno of AOF fsync in bio job. */
aofManifest *aof_manifest; /* Used to track AOFs. */
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/aof-multi-part.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,38 @@ tags {"external:skip"} {
clean_aof_persistence $aof_dirpath
}

test {Changing aof-use-rdb-preamble during rewrite process} {
start_server_aof [list dir $server_path aof-use-rdb-preamble no] {
wait_done_loading r

assert_equal 1 [check_file_exist $aof_dirpath "${aof_basename}.1${::base_aof_sufix}${::aof_format_suffix}"]

assert_aof_manifest_content $aof_manifest_file {
{file appendonly.aof.1.base.aof seq 1 type b}
{file appendonly.aof.1.incr.aof seq 1 type i}
}

r set foo bar
r config set rdb-key-save-delay 1000000
r bgrewriteaof
wait_for_condition 100 10 {
[s aof_rewrite_in_progress] eq 1
} else {
fail "aof rewrite did not start in time"
}
r config set aof-use-rdb-preamble yes
r config set rdb-key-save-delay 0
waitForBgrewriteaof r

assert_aof_manifest_content $aof_manifest_file {
{file appendonly.aof.2.base.aof seq 2 type b}
{file appendonly.aof.2.incr.aof seq 2 type i}
}
}

clean_aof_persistence $aof_dirpath
}

# Test Part 2
#
# To test whether the AOFRW behaves as expected during the server run.
Expand Down

0 comments on commit 6478526

Please sign in to comment.