Repository navigation
Conversation
A replica answers REPLCONF GETACK while it processes the replication stream, before beforeSleep() writes and fsyncs its AOF. When the write and the GETACK arrive together, which is the normal case for a client that pipelines a write with WAITAOF, the FACK in that reply is the offset from before the write. The primary then learns about the fsync only from the replica's next periodic ACK in replicationCron(), so WAITAOF with numreplicas >= 1 takes up to a second even with appendfsync always on the replica. When the FACK sent for a GETACK is behind the offset being acked, remember that offset and send another ACK from beforeSleep() once fsynced_reploff reaches it. Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WAITAOFwithnumreplicas >= 1takes about a second when the client pipelines the write with the WAITAOF, even withappendfsync alwayson the replica. The replica answers the primary'sREPLCONF GETACKwhile it is still processing the replication stream, beforebeforeSleep()fsyncs the write, so the FACK it returns is stale and the primary only hears about the fsync from the once-a-second ACK inreplicationCron(). This makes the replica send one more ACK right after the fsync when the GETACK reply was behind.AI generated details
Problem
Primary: the blocked WAITAOF sets
get_ack_from_replicas, andbeforeSleep()appendsREPLCONF GETACKto the stream right behind the write (server.c:2033). Replica: both arrive in one read.replconfCommandanswers the GETACK immediately (replication.c:1918-1921) withFACK server.fsynced_reploff, which still excludes the write, because the AOF is written and fsynced later in the samebeforeSleep()(server.c:2063-2076). Nothing sends another ACK untilreplicationCron()(replication.c:6006), once a second.If the client waits for the write's reply before sending WAITAOF, the replica has usually fsynced before the GETACK arrives, so the bug is invisible in the non-pipelined case. The WAITAOF tests in
tests/unit/wait.tclsend the write and the WAITAOF as separate round trips with timeout 0, so a one-second ack passes them.WAITAOFand FACK came in with Redis 7.2 (redis/redis#11713), so every Valkey release is affected. I found no matching issue or PR in valkey-io/valkey.Fix
In the GETACK handler, if the FACK just sent is behind the acked offset, record that offset in
server.repl_fack_wait_offset. InbeforeSleep(), right afterfsynced_reploffis refreshed, send one ACK once it reaches the recorded offset and clear it. The ACK is queued beforehandleClientsWithPendingWrites(), so it leaves in the same iteration as the fsync. Withappendfsync everysecthe extra ACK goes out when the background fsync completes, instead of up to a second later. Withappendfsync nothe offset is never reached and only the periodic ACK is sent, as before. An extra ACK is always safe; the primary only movesrepl_ack_offandrepl_aof_offforward.Alternative considered
Send the GETACK reply after the fsync instead of from
replconfCommand(defer it tobeforeSleep()). That changes when every GETACK is answered, including plainWAIT, and delays the replica-offset ACK behind the replica's disk. Answering immediately and following up once keepsWAITunchanged.Testing
Built from 6c11c7f with and without this patch. Primary and replica on loopback, both
appendonly yes,appendfsync always, 50 x (SET k vandWAITAOF 1 1 1000in one write):Not pipelined (wait for the SET reply first), before and after are the same: p50 5.6 / 5.7 ms.
The new test pipelines
INCRandWAITAOF 0 1 200ten times and requires every one to be acked. Without the fix, 5 of 5 runs:Under valgrind the timeout is 1000 ms, which the periodic ACK can meet, so only non-valgrind builds catch a regression.
This was generated by AI but verified, with love, by a human.