From 4dc69497f509956d298455492fe5ab98a9738cb6 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 21 Jan 2019 11:28:44 +0100 Subject: [PATCH] Refactoring: always kill AOF/RDB child via helper functions. --- src/aof.c | 2 +- src/db.c | 7 +------ src/rdb.c | 10 ++++++++++ src/replication.c | 5 +---- src/server.c | 5 ++--- src/server.h | 2 ++ 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/aof.c b/src/aof.c index 9497813b2..46ae58324 100644 --- a/src/aof.c +++ b/src/aof.c @@ -204,7 +204,7 @@ void aof_background_fsync(int fd) { } /* Kills an AOFRW child process if exists */ -static void killAppendOnlyChild(void) { +void killAppendOnlyChild(void) { int statloc; /* No AOFRW child? return. */ if (server.aof_child_pid == -1) return; diff --git a/src/db.c b/src/db.c index 6e7b46ac5..f59ae8406 100644 --- a/src/db.c +++ b/src/db.c @@ -448,12 +448,7 @@ void flushallCommand(client *c) { signalFlushedDb(-1); server.dirty += emptyDb(-1,flags,NULL); addReply(c,shared.ok); - if (server.rdb_child_pid != -1) { - kill(server.rdb_child_pid,SIGUSR1); - rdbRemoveTempFile(server.rdb_child_pid); - closeChildInfoPipe(); - updateDictResizePolicy(); - } + if (server.rdb_child_pid != -1) killRDBChild(); if (server.saveparamslen > 0) { /* Normally rdbSave() will reset dirty, but we don't want this here * as otherwise FLUSHALL will not be replicated nor put into the AOF. */ diff --git a/src/rdb.c b/src/rdb.c index 47555101e..351b90774 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -2225,6 +2225,16 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) { } } +/* Kill the RDB saving child using SIGUSR1 (so that the parent will know + * the child did not exit for an error, but because we wanted), and performs + * the cleanup needed. */ +void killRDBChild(void) { + kill(server.rdb_child_pid,SIGUSR1); + rdbRemoveTempFile(server.rdb_child_pid); + closeChildInfoPipe(); + updateDictResizePolicy(); +} + /* Spawn an RDB child that writes the RDB to the sockets of the slaves * that are currently in SLAVE_STATE_WAIT_BGSAVE_START state. */ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) { diff --git a/src/replication.c b/src/replication.c index a10b10133..a34257546 100644 --- a/src/replication.c +++ b/src/replication.c @@ -1254,10 +1254,7 @@ void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) { "Killing process %ld and removing its temp file to avoid " "any race", (long) server.rdb_child_pid); - kill(server.rdb_child_pid,SIGUSR1); - rdbRemoveTempFile(server.rdb_child_pid); - closeChildInfoPipe(); - updateDictResizePolicy(); + killRDBChild(); } if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) { diff --git a/src/server.c b/src/server.c index 41d6f454c..fd4d9adb8 100644 --- a/src/server.c +++ b/src/server.c @@ -2798,8 +2798,7 @@ int prepareForShutdown(int flags) { overwrite the synchronous saving did by SHUTDOWN. */ if (server.rdb_child_pid != -1) { serverLog(LL_WARNING,"There is a child saving an .rdb. Killing it!"); - kill(server.rdb_child_pid,SIGUSR1); - rdbRemoveTempFile(server.rdb_child_pid); + killRDBChild(); } if (server.aof_state != AOF_OFF) { @@ -2814,7 +2813,7 @@ int prepareForShutdown(int flags) { } serverLog(LL_WARNING, "There is a child rewriting the AOF. Killing it!"); - kill(server.aof_child_pid,SIGUSR1); + killAppendOnlyChild(); } /* Append only file: flush buffers and fsync() the AOF at exit */ serverLog(LL_NOTICE,"Calling fsync() on the AOF file."); diff --git a/src/server.h b/src/server.h index 956494aa3..9e129b62c 100644 --- a/src/server.h +++ b/src/server.h @@ -1674,6 +1674,7 @@ int writeCommandsDeniedByDiskError(void); /* RDB persistence */ #include "rdb.h" int rdbSaveRio(rio *rdb, int *error, int flags, rdbSaveInfo *rsi); +void killRDBChild(void); /* AOF persistence */ void flushAppendOnlyFile(int force); @@ -1687,6 +1688,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal); void aofRewriteBufferReset(void); unsigned long aofRewriteBufferSize(void); ssize_t aofReadDiffFromParent(void); +void killAppendOnlyChild(void); /* Child info */ void openChildInfoPipe(void);