From 0694eeea8d69c3b48a63242c91904ca015d74a86 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Mon, 30 Nov 2026 08:11:07 +0000 Subject: [PATCH] Report the replica AOF fsync to the primary as soon as it happens 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 --- src/replication.c | 6 +++++- src/server.c | 8 ++++++++ src/server.h | 2 ++ tests/unit/wait.tcl | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/replication.c b/src/replication.c index cddc74042ba..fd8f91d19ae 100644 --- a/src/replication.c +++ b/src/replication.c @@ -1918,7 +1918,11 @@ void replconfCommand(client *c) { } else if (!strcasecmp(objectGetVal(c->argv[j]), "getack")) { /* REPLCONF GETACK is used in order to request an ACK ASAP * to the replica. */ - if (server.primary_host && server.primary) replicationSendAck(); + if (server.primary_host && server.primary) { + replicationSendAck(); + if (server.fsynced_reploff != -1 && server.fsynced_reploff < c->repl_data->reploff) + server.repl_fack_wait_offset = c->repl_data->reploff; + } return; } else if (!strcasecmp(objectGetVal(c->argv[j]), "rdb-only")) { /* REPLCONF RDB-ONLY is used to identify the client only wants diff --git a/src/server.c b/src/server.c index 9e37c72a1b8..74ed4cc1ae7 100644 --- a/src/server.c +++ b/src/server.c @@ -2079,6 +2079,13 @@ void beforeSleep(struct aeEventLoop *eventLoop) { if (listLength(server.clients_waiting_acks) && prev_fsynced_reploff != server.fsynced_reploff) dont_sleep = 1; } + /* A replica answers GETACK before it fsyncs what it acked, so report the fsync as soon as it + * happens instead of in the next periodic ACK. */ + if (server.repl_fack_wait_offset != -1 && server.fsynced_reploff >= server.repl_fack_wait_offset) { + server.repl_fack_wait_offset = -1; + replicationSendAck(); + } + /* Handle writes with pending output buffers. */ int client_writes = handleClientsWithPendingWrites(); if (client_writes > 0) server.el_iteration_active = true; @@ -3151,6 +3158,7 @@ void initServer(void) { server.pending_push_messages = listCreate(); server.clients_waiting_acks = listCreate(); server.get_ack_from_replicas = 0; + server.repl_fack_wait_offset = -1; server.paused_actions = 0; memset(server.client_pause_per_purpose, 0, sizeof(server.client_pause_per_purpose)); server.postponed_clients = listCreate(); diff --git a/src/server.h b/src/server.h index 6d86dcb4244..4ef9c5dbd68 100644 --- a/src/server.h +++ b/src/server.h @@ -2351,6 +2351,7 @@ struct valkeyServer { * when it receives an error on the replication stream */ int repl_ignore_disk_write_error; /* Configures whether replicas panic when unable to * persist writes to AOF. */ + long long repl_fack_wait_offset; /* Send an ACK once fsynced_reploff reaches this, -1 if none. */ int repl_compression_advertised; /* Compression algorithm advertised in the current upstream * handshake, or REPL_COMPRESSION_CAPA_UNKNOWN before REPLCONF capa. */ @@ -3466,6 +3467,7 @@ void freeReplicaReferencedReplBuffer(client *replica); void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc); void updateReplicasWaitingBgsave(int bgsaveerr, int type); void replicationCron(void); +void replicationSendAck(void); void replicationStartPendingFork(void); void replicationHandlePrimaryDisconnection(void); void replicationCachePrimary(client *c); diff --git a/tests/unit/wait.tcl b/tests/unit/wait.tcl index caea1de65df..892fef7d5a8 100644 --- a/tests/unit/wait.tcl +++ b/tests/unit/wait.tcl @@ -317,6 +317,26 @@ tags {"wait aof network external:skip"} { $replica config set appendfsync everysec } + test {WAITAOF replica copy appendfsync always does not wait for the replica cron} { + test_config_set $replica appendfsync always + $master incr foo + assert_equal [$master waitaof 0 1 0] {1 1} + + # Pipelined, so the replica gets the GETACK before it fsyncs the write. Its + # periodic ACK is once a second, so at most a few of these succeed without an + # ACK after the fsync. + set rd [valkey_deferring_client -1] + test_defer [list $rd close] + set timeout [expr {$::valgrind ? 1000 : 200}] + for {set i 0} {$i < 10} {incr i} { + $rd write [format_command incr foo] + $rd write [format_command waitaof 0 1 $timeout] + $rd flush + $rd read + assert_equal [$rd read] {1 1} + } + } + test {WAITAOF replica copy if replica is blocked} { pause_process $replica_pid $master incr foo