Wait zero active threads condition before to fork() for BGSAVE or BGREWRITEAOF

This commit is contained in:
antirez 2010-01-13 13:38:30 -05:00
parent c7df85a484
commit 4ee9488d7e

17
redis.c
View File

@ -561,6 +561,7 @@ static void freeIOJob(iojob *j);
static void queueIOJob(iojob *j); static void queueIOJob(iojob *j);
static int vmWriteObjectOnSwap(robj *o, off_t page); static int vmWriteObjectOnSwap(robj *o, off_t page);
static robj *vmReadObjectFromSwap(off_t page, int type); static robj *vmReadObjectFromSwap(off_t page, int type);
static void waitZeroActiveThreads(void);
static void authCommand(redisClient *c); static void authCommand(redisClient *c);
static void pingCommand(redisClient *c); static void pingCommand(redisClient *c);
@ -3085,6 +3086,7 @@ static int rdbSaveBackground(char *filename) {
pid_t childpid; pid_t childpid;
if (server.bgsavechildpid != -1) return REDIS_ERR; if (server.bgsavechildpid != -1) return REDIS_ERR;
if (server.vm_enabled) waitZeroActiveThreads();
if ((childpid = fork()) == 0) { if ((childpid = fork()) == 0) {
/* Child */ /* Child */
close(server.fd); close(server.fd);
@ -6906,6 +6908,7 @@ static int rewriteAppendOnlyFileBackground(void) {
pid_t childpid; pid_t childpid;
if (server.bgrewritechildpid != -1) return REDIS_ERR; if (server.bgrewritechildpid != -1) return REDIS_ERR;
if (server.vm_enabled) waitZeroActiveThreads();
if ((childpid = fork()) == 0) { if ((childpid = fork()) == 0) {
/* Child */ /* Child */
char tmpfile[256]; char tmpfile[256];
@ -7669,6 +7672,20 @@ static void spawnIOThread(void) {
server.io_active_threads++; server.io_active_threads++;
} }
/* We need to wait for the last thread to exit before we are able to
* fork() in order to BGSAVE or BGREWRITEAOF. */
static void waitZeroActiveThreads(void) {
while(1) {
lockThreadedIO();
if (server.io_active_threads == 0) {
unlockThreadedIO();
return;
}
unlockThreadedIO();
usleep(10000); /* 10 milliseconds */
}
}
/* This function must be called while with threaded IO locked */ /* This function must be called while with threaded IO locked */
static void queueIOJob(iojob *j) { static void queueIOJob(iojob *j) {
redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n", redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",