Skip to content

Don't let WAITAOF ack AOF data that has not been fsynced - #240

Open
madolson wants to merge 1 commit into
unstablefrom
fix-waitaof-everysec-unfsynced
Open

madolson wants to merge 1 commit into
unstablefrom
fix-waitaof-everysec-unfsynced

Conversation

@madolson

Copy link
Copy Markdown
Owner

Under appendfsync everysec, WAITAOF 1 0 can acknowledge a write up to a second before it is fsynced. When the AOF buffer is empty, flushAppendOnlyFile() publishes primary_repl_offset as fsynced (added in Redis #12622 so WAITAOF doesn't hang on NO_AOF module writes), but its only guard is a sync_in_progress flag that is usually never computed on that path. This only publishes the offset when every written byte has had an fsync requested and no AOF fsync job is still running.

AI generated details

Problem

src/aof.c:1189-1217. With an empty aof_buf, the first branch evaluates sync_in_progress = aofFsyncInProgress() only after aof_last_incr_fsync_offset != aof_last_incr_size and mstime - aof_last_fsync >= 1000 both hold. Otherwise sync_in_progress stays 0 and the else branch stores primary_repl_offset into fsynced_reploff_pending. Three ways that publishes data that is not on disk:

Case Base Fix
everysec, write lands less than 1 s after the last fsync (written, no fsync requested) acked on the next event loop waits for the next fsync. Automated test
everysec, background fsync requested but still running (aof_last_incr_fsync_offset advances at request time, aof.c:1394) acked before fdatasync returns waits for the BIO job, which publishes its own offset (bio.c:310). Manual run below
AOFRW just opened a new INCR file: offsets reset to 0/0 (aof.c:831), old file's BIO_CLOSE_AOF fsync still queued acked before the old file is fsynced waits for the close job. Manual run below
appendfsync always no change no change
RM_Call NO_AOF writes, nothing to fsync (#12622) released released (tests/unit/moduleapi/usercall.tcl passes)

The previous everysec tests missed it because WAITAOF local copy everysec asserts {1 0} with timeout 0, which the bug satisfies early. Introduced by redis/redis#12622 (c2a4b7849), so every Valkey release is affected.

Fix

Publish only when aof_last_incr_fsync_offset == aof_last_incr_size, no BIO_AOF_FSYNC job is pending, and no BIO_CLOSE_AOF job is pending. The old !sync_in_progress term is dropped: with the offset check it can only be 0 there.

Behavior change: WAITAOF 1 0 under everysec now waits for the next real fsync, up to about a second, instead of returning on the next event loop. The existing WAITAOF local copy everysec test goes from about 0 ms to about 800 ms.

Not changed here: when a background fsync fails, the job doesn't publish but still drains, so the next empty-buffer flush publishes anyway. That is the fdatasync EIO case in valkey-io#5571 / valkey-io#5572.

Alternative considered

Only the offset check (aof_last_incr_fsync_offset == aof_last_incr_size). It fixes the first row but not the other two. With fdatasync slowed to 300 ms via LD_PRELOAD, 30 x (SET; WAITAOF 1 0 5000) under everysec:

stock:            iterations=30 fdatasyncs=4  acked_without_covering_fsync=29
offset check:     iterations=30 fdatasyncs=33 acked_without_covering_fsync=29
this PR:          iterations=30 fdatasyncs=33 acked_without_covering_fsync=0

With the offset check, e.g. the fsync ran 364718 to 365019 and WAITAOF replied at 364820. For the AOFRW row (SET; BGREWRITEAOF; WAITAOF 1 0 5000), without the BIO_CLOSE_AOF term WAITAOF replied at 498586 while the old INCR file's fsync ran 498585 to 498904; with it the reply waits for the fsync.

Testing

The new test uses no-appendfsync-on-rewrite yes with a bgsave child, so no fsync can happen while it asserts WAITAOF 1 0 100 times out. Without the fix, 5 of 5 runs:

[err]: WAITAOF local copy everysec does not ack a write that was not fsynced in tests/unit/wait.tcl
Expected '1 0' to be equal to '0 0' (context: type eval line 15 cmd {assert_equal [$master waitaof 1 0 100] {0 0} } proc ::test)

The in-flight and AOFRW rows have no automated test: they need an fsync slow enough to observe, and there is no debug knob for that. Without the fix the test only fails if the write lands within a second of the preceding fsync, which a slow runner (valgrind) may miss; with the fix it cannot pass by luck.

This was generated by AI but verified, with love, by a human.

When the AOF buffer is empty, flushAppendOnlyFile() publishes
primary_repl_offset as fsynced so that WAITAOF doesn't hang on offsets
that only reached replicas (RM_Call with NO_AOF). It did that whenever no
fsync was visibly in progress, without checking that the AOF had been
fsynced at all. Under appendfsync everysec a write that lands between two
fsyncs was acknowledged by WAITAOF 1 0 on the next event loop iteration,
up to a second before it reached the disk. The same happened while a
background fsync was still running, since aof_last_incr_fsync_offset
advances when the fsync is requested, and while the fsync of the previous
INCR file queued by an AOF rewrite was still running.

Only publish the offset when everything written has had an fsync
requested and no AOF fsync job is pending. The jobs publish the offset
themselves when they complete.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson added the needs-review Staged work waiting on madolson's review label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review Staged work waiting on madolson's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant