Skip to content

Commit

Permalink
Use pause process instead of brute force sleep
Browse files Browse the repository at this point in the history
Signed-off-by: naglera <[email protected]>
  • Loading branch information
naglera committed Jul 21, 2024
1 parent d95dbd4 commit abea73c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 71 deletions.
19 changes: 10 additions & 9 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ void debugCommand(client *c) {
" In case RESET is provided the peak reset time will be restored to the default value",
"REPLYBUFFER RESIZING <0|1>",
" Enable or disable the reply buffer resize cron job",
"SLEEP-AFTER-FORK-SECONDS <seconds>",
" Stop the server's main process for <seconds> after forking.",
"PAUSE-AFTER-FORK <0|1>",
" Stop the server's main process after fork.",
"DELAY-RDB-CLIENT-FREE-SECOND <seconds>",
" Grace period in seconds for replica main channel to establish psync.",
"DICT-RESIZING <0|1>",
Expand Down Expand Up @@ -995,13 +995,8 @@ void debugCommand(client *c) {
return;
}
addReply(c, shared.ok);
} else if (!strcasecmp(c->argv[1]->ptr, "sleep-after-fork-seconds") && c->argc == 3) {
double sleep_after_fork_seconds;
if (getDoubleFromObjectOrReply(c, c->argv[2], &sleep_after_fork_seconds, NULL) != C_OK) {
addReply(c, shared.err);
return;
}
server.debug_sleep_after_fork_us = (int)(sleep_after_fork_seconds * 1e6);
} else if (!strcasecmp(c->argv[1]->ptr, "pause-after-fork") && c->argc == 3) {
server.debug_pause_after_fork = atoi(c->argv[2]->ptr);
addReply(c, shared.ok);
} else if (!strcasecmp(c->argv[1]->ptr, "delay-rdb-client-free-seconds") && c->argc == 3) {
server.wait_before_rdb_client_free = atoi(c->argv[2]->ptr);
Expand Down Expand Up @@ -2306,6 +2301,12 @@ void applyWatchdogPeriod(void) {
}
}

void debugPauseProcess(void) {
serverLog(LL_DEBUG, "Process is about to stop.");
raise(SIGSTOP);
serverLog(LL_DEBUG, "Process has been continued.");
}

/* Positive input is sleep time in microseconds. Negative input is fractions
* of microseconds, i.e. -10 means 100 nanoseconds. */
void debugDelay(int usec) {
Expand Down
4 changes: 2 additions & 2 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,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_REPLICATION | RDBFLAGS_KEEP_CACHE);
}
if (server.debug_sleep_after_fork_us) usleep(server.debug_sleep_after_fork_us);
if (server.debug_pause_after_fork) debugPauseProcess();
} else {
serverLog(LL_WARNING, "BGSAVE for replication: replication information not available, can't generate the RDB "
"file right now. Try later.");
Expand Down Expand Up @@ -3181,7 +3181,7 @@ void setupMainConnForPsync(connection *conn) {
}

if (server.repl_state == REPL_STATE_SEND_PSYNC) {
if (server.debug_sleep_after_fork_us) usleep(server.debug_sleep_after_fork_us);
if (server.debug_pause_after_fork) debugPauseProcess();
if (replicaTryPartialResynchronization(conn, 0) == PSYNC_WRITE_ERROR) {
serverLog(LL_WARNING, "Aborting dual channel sync. Write error.");
cancelReplicationHandshake(1);
Expand Down
5 changes: 3 additions & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2005,8 +2005,8 @@ struct valkeyServer {
* use dual channel replication for full syncs. */
int wait_before_rdb_client_free; /* Grace period in seconds for replica main channel
* to establish psync. */
int debug_sleep_after_fork_us; /* Debug param that force the main process to
* sleep for N microseconds after fork() in repl. */
int debug_pause_after_fork; /* Debug param that pause the main process
* after a replication fork() (for bgsave). */
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 Expand Up @@ -3980,6 +3980,7 @@ void killThreads(void);
void makeThreadKillable(void);
void swapMainDbWithTempDb(serverDb *tempDb);
sds getVersion(void);
void debugPauseProcess(void);

/* Use macro for checking log level to avoid evaluating arguments in cases log
* should be ignored due to low level. */
Expand Down
10 changes: 0 additions & 10 deletions tests/helpers/bg_server_sleep.tcl

This file was deleted.

Loading

0 comments on commit abea73c

Please sign in to comment.