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