mirror of
https://github.com/fluencelabs/redis
synced 2025-07-31 16:31:58 +00:00
Module API for Forking
* create module API for forking child processes. * refactor duplicate code around creating and tracking forks by AOF and RDB. * child processes listen to SIGUSR1 and dies exitFromChild in order to eliminate a valgrind warning of unhandled signal. * note that BGSAVE error reply has changed. valgrind error is: Process terminating with default action of signal 10 (SIGUSR1)
This commit is contained in:
@@ -13,12 +13,16 @@ endif
|
||||
|
||||
.SUFFIXES: .c .so .xo .o
|
||||
|
||||
all: commandfilter.so
|
||||
all: commandfilter.so fork.so
|
||||
|
||||
.c.xo:
|
||||
$(CC) -I../../src $(CFLAGS) $(SHOBJ_CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
commandfilter.xo: ../../src/redismodule.h
|
||||
fork.xo: ../../src/redismodule.h
|
||||
|
||||
commandfilter.so: commandfilter.xo
|
||||
$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc
|
||||
|
||||
fork.so: fork.xo
|
||||
$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc
|
||||
|
84
tests/modules/fork.c
Normal file
84
tests/modules/fork.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#define REDISMODULE_EXPERIMENTAL_API
|
||||
#include "redismodule.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNUSED(V) ((void) V)
|
||||
|
||||
int child_pid = -1;
|
||||
int exitted_with_code = -1;
|
||||
|
||||
void done_handler(int exitcode, int bysignal, void *user_data) {
|
||||
child_pid = -1;
|
||||
exitted_with_code = exitcode;
|
||||
assert(user_data==(void*)0xdeadbeef);
|
||||
UNUSED(bysignal);
|
||||
}
|
||||
|
||||
int fork_create(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
||||
{
|
||||
long long code_to_exit_with;
|
||||
if (argc != 2) {
|
||||
RedisModule_WrongArity(ctx);
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
RedisModule_StringToLongLong(argv[1], &code_to_exit_with);
|
||||
exitted_with_code = -1;
|
||||
child_pid = RedisModule_Fork(done_handler, (void*)0xdeadbeef);
|
||||
if (child_pid < 0) {
|
||||
RedisModule_ReplyWithError(ctx, "Fork failed");
|
||||
return REDISMODULE_OK;
|
||||
} else if (child_pid > 0) {
|
||||
/* parent */
|
||||
RedisModule_ReplyWithLongLong(ctx, child_pid);
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
||||
/* child */
|
||||
RedisModule_Log(ctx, "notice", "fork child started");
|
||||
usleep(200000);
|
||||
RedisModule_Log(ctx, "notice", "fork child exiting");
|
||||
RedisModule_ExitFromChild(code_to_exit_with);
|
||||
/* unreachable */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fork_exitcode(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
||||
{
|
||||
UNUSED(argv);
|
||||
UNUSED(argc);
|
||||
RedisModule_ReplyWithLongLong(ctx, exitted_with_code);
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
||||
int fork_kill(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
||||
{
|
||||
UNUSED(argv);
|
||||
UNUSED(argc);
|
||||
if (RedisModule_KillForkChild(child_pid) != REDISMODULE_OK)
|
||||
RedisModule_ReplyWithError(ctx, "KillForkChild failed");
|
||||
else
|
||||
RedisModule_ReplyWithLongLong(ctx, 1);
|
||||
child_pid = -1;
|
||||
return REDISMODULE_OK;
|
||||
}
|
||||
|
||||
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
||||
UNUSED(argv);
|
||||
UNUSED(argc);
|
||||
if (RedisModule_Init(ctx,"fork",1,REDISMODULE_APIVER_1)== REDISMODULE_ERR)
|
||||
return REDISMODULE_ERR;
|
||||
|
||||
if (RedisModule_CreateCommand(ctx,"fork.create", fork_create,"",0,0,0) == REDISMODULE_ERR)
|
||||
return REDISMODULE_ERR;
|
||||
|
||||
if (RedisModule_CreateCommand(ctx,"fork.exitcode", fork_exitcode,"",0,0,0) == REDISMODULE_ERR)
|
||||
return REDISMODULE_ERR;
|
||||
|
||||
if (RedisModule_CreateCommand(ctx,"fork.kill", fork_kill,"",0,0,0) == REDISMODULE_ERR)
|
||||
return REDISMODULE_ERR;
|
||||
|
||||
return REDISMODULE_OK;
|
||||
}
|
32
tests/unit/moduleapi/fork.tcl
Normal file
32
tests/unit/moduleapi/fork.tcl
Normal file
@@ -0,0 +1,32 @@
|
||||
set testmodule [file normalize tests/modules/fork.so]
|
||||
|
||||
proc count_log_message {pattern} {
|
||||
set result [exec grep -c $pattern < [srv 0 stdout]]
|
||||
}
|
||||
|
||||
start_server {tags {"modules"}} {
|
||||
r module load $testmodule
|
||||
|
||||
test {Module fork} {
|
||||
# the argument to fork.create is the exitcode on termination
|
||||
r fork.create 3
|
||||
wait_for_condition 20 100 {
|
||||
[r fork.exitcode] != -1
|
||||
} else {
|
||||
fail "fork didn't terminate"
|
||||
}
|
||||
r fork.exitcode
|
||||
} {3}
|
||||
|
||||
test {Module fork kill} {
|
||||
r fork.create 3
|
||||
after 20
|
||||
r fork.kill
|
||||
after 100
|
||||
|
||||
assert {[count_log_message "fork child started"] eq "2"}
|
||||
assert {[count_log_message "Received SIGUSR1 in child"] eq "1"}
|
||||
assert {[count_log_message "fork child exiting"] eq "1"}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user