Skip to content

Commit

Permalink
fix multi process core dump on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwaaaaaa committed Feb 21, 2024
1 parent a0eeedc commit 8f4177b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 56 deletions.
2 changes: 1 addition & 1 deletion conf/my.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#user nobody;
master_process off;
master_process on;
daemon off;

#error_log logs/error.log;
Expand Down
120 changes: 71 additions & 49 deletions modules/auxiliary/ngx_auxiliary_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,60 @@ static void ngx_pass_open_channel(ngx_cycle_t *cycle);
static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle);

static ngx_core_module_t auxiliary_module = {
ngx_string("auxiliary"),
NULL, // 解析配置文件之前执行
NULL // 解析配置文件之后执行
ngx_string("auxiliary"),
NULL, // 解析配置文件之前执行
NULL // 解析配置文件之后执行
};

ngx_module_t ngx_auxiliary_module = {
NGX_MODULE_V1,
&auxiliary_module,
NULL,
NGX_CORE_MODULE,
NULL, /* init master */
NULL, /* init module */
ngx_aux_init_master, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};

#define ngx_aux_get_main_conf_ptr(cf) (ngx_aux_proc_main_conf_t **) &(cf)->cycle->conf_ctx[ngx_auxiliary_module.index]

ngx_module_t ngx_auxiliary_module = {NGX_MODULE_V1,
&auxiliary_module,
NULL,
NGX_CORE_MODULE,
NULL, /* init master */
NULL, /* init module */
ngx_aux_init_master, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING};

#define ngx_aux_get_main_conf_ptr(cf) \
(ngx_aux_proc_main_conf_t **) &(cf) \
->cycle->conf_ctx[ngx_auxiliary_module.index]

ngx_int_t ngx_aux_add_proc(ngx_conf_t *cf, ngx_aux_proc_t *proc) {
ngx_aux_proc_main_conf_t **ptr, *mcf;
ngx_aux_proc_t **n_proc;
ngx_uint_t i;

ptr = ngx_aux_get_main_conf_ptr(cf);
if (*ptr == NULL) {
*ptr = ngx_palloc(cf->pool, sizeof(ngx_aux_proc_main_conf_t));
}

mcf = *ptr;
if (mcf == NULL) {
return NGX_ERROR;
}
if (ngx_array_init(&mcf->process, cf->pool, 4, sizeof(ngx_aux_proc_t *)) != NGX_OK) {
return NGX_ERROR;
mcf = ngx_palloc(cf->pool, sizeof(ngx_aux_proc_main_conf_t));
if (mcf == NULL) {
return NGX_ERROR;
}
if (ngx_array_init(&mcf->process, cf->pool, 4,
sizeof(ngx_aux_proc_t *)) != NGX_OK) {
return NGX_ERROR;
}
*ptr = mcf;
} else {
n_proc = mcf->process.elts;
for (i = 0; i < mcf->process.nelts; i++) {
if (n_proc[i]->name.len == proc->name.len &&
ngx_strncmp(n_proc[i]->name.data, proc->name.data,
proc->name.len) == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"process \"%V\" exists", &proc->name);
return NGX_ERROR;
}
}
}

ngx_aux_proc_t **n_proc = ngx_array_push(&mcf->process);
n_proc = ngx_array_push(&mcf->process);
if (n_proc == NULL) {
return NGX_ERROR;
}
Expand All @@ -60,9 +76,11 @@ void ngx_aux_start_auxiliary_processes(ngx_cycle_t *cycle, ngx_uint_t respawn) {
ngx_aux_proc_main_conf_t *mcf;
ngx_aux_proc_t **proc;
ngx_uint_t i, n;
size_t len;
char buf[256];

mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_auxiliary_module);
mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_auxiliary_module);
if (mcf == NULL) {
return;
}
Expand All @@ -71,16 +89,19 @@ void ngx_aux_start_auxiliary_processes(ngx_cycle_t *cycle, ngx_uint_t respawn) {
proc = mcf->process.elts;

for (i = 0; i < n; ++i) {
ngx_sprintf((u_char *) buf, "auxiliary: %V", &proc[i]->name);

ngx_spawn_process(cycle, ngx_aux_process_cycle, proc[i], buf,
respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN);
len = ngx_sprintf((u_char *) buf, "auxiliary: %V", &proc[i]->name) -
(u_char *) buf;
buf[len] = 0;
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
"start auxiliary processes %s", buf);

ngx_spawn_process(
cycle, ngx_aux_process_cycle, proc[i], buf,
respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN);
ngx_pass_open_channel(cycle);
}

}


static void ngx_pass_open_channel(ngx_cycle_t *cycle) {
ngx_int_t i;
ngx_channel_t ch;
Expand All @@ -91,28 +112,25 @@ static void ngx_pass_open_channel(ngx_cycle_t *cycle) {
ch.fd = ngx_processes[ngx_process_slot].channel[0];

for (i = 0; i < ngx_last_process; i++) {

if (i == ngx_process_slot
|| ngx_processes[i].pid == -1
|| ngx_processes[i].channel[0] == -1) {
if (i == ngx_process_slot || ngx_processes[i].pid == -1 ||
ngx_processes[i].channel[0] == -1) {
continue;
}

ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
"pass channel s:%i pid:%P fd:%d to s:%i pid:%P fd:%d",
ch.slot, ch.pid, ch.fd,
i, ngx_processes[i].pid,
ch.slot, ch.pid, ch.fd, i, ngx_processes[i].pid,
ngx_processes[i].channel[0]);


ngx_write_channel(ngx_processes[i].channel[0],
&ch, sizeof(ngx_channel_t), cycle->log);
ngx_write_channel(ngx_processes[i].channel[0], &ch,
sizeof(ngx_channel_t), cycle->log);
}
}

static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) {
char buf[256];
ngx_int_t ret;
size_t len;
ngx_aux_proc_t *proc = data;
ngx_process = NGX_PROCESS_HELPER;

Expand All @@ -123,15 +141,19 @@ static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) {
cycle->connection_n = 512;
ngx_worker_aux_process_init(cycle);

ngx_sprintf((u_char *) buf, "auxiliary %V", &proc->name);
len = ngx_sprintf((u_char *) buf, "auxiliary %V", &proc->name) -
(u_char *) buf;
buf[len] = 0;
ngx_setproctitle(buf);

ret = proc->process(cycle, proc);
if (ret != NGX_OK) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, ret, "exiting auxiliary process: %s", buf);
ngx_log_error(NGX_LOG_NOTICE, cycle->log, ret,
"exiting auxiliary process: %s", buf);
exit(1);
}
for (;;) {

for (;;) {
if (ngx_terminate || ngx_quit) {
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
exit(0);
Expand All @@ -145,7 +167,6 @@ static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) {

ngx_process_events_and_timers(cycle);
}

}

static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle) {
Expand All @@ -157,7 +178,8 @@ static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle) {
return NGX_OK;
}

mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_auxiliary_module);
mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx,
ngx_auxiliary_module);
if (mcf == NULL) {
return NGX_ERROR;
}
Expand Down
6 changes: 0 additions & 6 deletions modules/nacos/ngx_nacos_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <ngx_event_connect.h>
#include <ngx_nacos.h>
#include <ngx_nacos_aux.h>
#include <ngx_nacos_data.h>
#include <ngx_nacos_grpc.h>
#include <ngx_nacos_http_parse.h>
#include <ngx_nacos_udp.h>

static ngx_int_t ngx_aux_nacos_proc_handler(ngx_cycle_t *cycle,
Expand All @@ -27,10 +25,6 @@ static ngx_aux_proc_t aux_proc = {ngx_string("nacos"), NULL,
ngx_aux_nacos_proc_handler};

ngx_int_t ngx_nacos_aux_init(ngx_conf_t *cf) {
if (aux_proc.data != NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ngx_nacos_aux_init inited");
return NGX_ERROR;
}
aux_ctx.ncf = ngx_nacos_get_main_conf(cf);
aux_proc.data = &aux_ctx;
return ngx_aux_add_proc(cf, &aux_proc);
Expand Down

0 comments on commit 8f4177b

Please sign in to comment.