Skip to content

Commit

Permalink
Test rdb-connection race condition
Browse files Browse the repository at this point in the history
Test the case in which the replica establish psync connection after the
RDB was already loaded.
Added debug command in order to froce main process to be slower then the
bg child.
  • Loading branch information
naglera committed Jan 30, 2024
1 parent 1a6052f commit acd12a8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ void debugCommand(client *c) {
" Enable or disable the reply buffer resize cron job",
"CLUSTERLINK KILL <to|from|all> <node-id>",
" Kills the link based on the direction to/from (both) with the provided node." ,
"SLEEP-AFTER-FORK <seconds>",
" Stop the server's main process for <seconds> after forking.",
NULL
};
addReplyHelp(c, help);
Expand Down Expand Up @@ -1031,6 +1033,10 @@ NULL
addReplyErrorFormat(c, "Unknown direction %s", (char*) c->argv[3]->ptr);
}
addReply(c,shared.ok);
} else if(!strcasecmp(c->argv[1]->ptr,"SLEEP-AFTER-FORK") &&
c->argc == 3) {
server.debug_sleep_after_fork = atoi(c->argv[2]->ptr);
addReply(c,shared.ok);
} else {
addReplySubcommandSyntaxError(c);
return;
Expand Down
1 change: 1 addition & 0 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ int startBgsaveForReplication(int mincapa, int req) {
/* Keep the page cache since it'll get used soon */
retval = rdbSaveBackground(req,server.rdb_filename,rsiptr,RDBFLAGS_KEEP_CACHE);
}
if (server.debug_sleep_after_fork) sleep(server.debug_sleep_after_fork);
} else {
serverLog(LL_WARNING,"BGSAVE for replication: replication information not available, can't generate the RDB file right now. Try later.");
retval = C_ERR;
Expand Down
2 changes: 2 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,8 @@ struct redisServer {
* delay (start sooner if they all connect). */
int rdb_channel_enabled; /* Config used to determine if the replica should
* use rdb channel for full syncs. */
int debug_sleep_after_fork; /* Debug param that force the main connection to
* sleep for N seconds after fork() in repl. */
size_t repl_buffer_mem; /* The memory of replication buffer. */
list *repl_buffer_blocks; /* Replication buffers blocks list
* (serving replica clients and repl backlog) */
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/replication.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1789,4 +1789,38 @@ start_server {tags {"repl rdb-channel external:skip"}} {
}
}
}
}

start_server {tags {"repl rdb-channel external:skip"}} {
set replica [srv 0 client]
set replica_host [srv 0 host]
set replica_port [srv 0 port]
set replica_log [srv 0 stdout]
start_server {} {
set master [srv 0 client]
set master_host [srv 0 host]
set master_port [srv 0 port]
set loglines [count_log_lines -1]
# Create small enough db to be loaded before replica establish psync connection
$master set key1 val1

$master config set repl-diskless-sync yes
$master debug sleep-after-fork 5;# Stop master after fork

$replica config set repl-rdb-channel yes
$replica config set loglevel debug

test "Test rdb-channel psync established after rdb load" {
$replica slaveof $master_host $master_port

wait_for_value_to_propegate_to_replica $master $replica "key1"

verify_replica_online $master 0 500
set res [wait_for_log_messages -1 {"*MASTER <-> REPLICA sync: Finished with success*"} $loglines 2000 1]
# Confirm the occurrence of a race condition.
set res [wait_for_log_messages -1 {"*RDB channel sync - psync established after rdb load*"} $loglines 2000 1]
set loglines [lindex $res 1]
incr $loglines
}
}
}

0 comments on commit acd12a8

Please sign in to comment.