Skip to content

Commit

Permalink
The main process (attendant) is no longer running the child_init()
Browse files Browse the repository at this point in the history
The main proc must stay free of any db connections, so it can later fork and child_init() other procs (due auto_scaling).
For the modules which were keeping a DB conn open in main proc for later usage, during module destroy, the new approach is to open the conn, on the spot, in the destroy handler.

Related to #2003

(cherry picked from commit 3a8f6f1)
  • Loading branch information
bogdan-iancu committed Jun 22, 2020
1 parent 66c2c4d commit 54e380d
Show file tree
Hide file tree
Showing 43 changed files with 89 additions and 189 deletions.
8 changes: 1 addition & 7 deletions main.c
Expand Up @@ -825,7 +825,7 @@ static int main_loop(void)

if (testing_framework) {
if (init_child(1) < 0) {
LM_ERR("error in init_child for PROC_MAIN\n");
LM_ERR("error in init_child for First Worker\n");
report_failure_status();
goto error;
}
Expand All @@ -834,12 +834,6 @@ static int main_loop(void)
shutdown_opensips(rc);
}

if (init_child(PROC_MAIN) < 0) {
LM_ERR("error in init_child for PROC_MAIN\n");
report_failure_status();
goto error;
}

report_conditional_status( (!no_daemon_mode), 0);

for(;;){
Expand Down
4 changes: 0 additions & 4 deletions modules/alias_db/alias_db.c
Expand Up @@ -254,9 +254,5 @@ static int mod_init(void)
*/
static void destroy(void)
{
if (db_handle) {
adbf.close(db_handle);
db_handle = 0;
}
}

4 changes: 0 additions & 4 deletions modules/auth_db/authdb_mod.c
Expand Up @@ -231,10 +231,6 @@ static int mod_init(void)

static void destroy(void)
{
if (auth_db_handle) {
auth_dbf.close(auth_db_handle);
auth_db_handle = 0;
}
if (credentials) {
free_aaa_avp_list(credentials);
credentials = 0;
Expand Down
3 changes: 1 addition & 2 deletions modules/avpops/avpops.c
Expand Up @@ -310,8 +310,7 @@ static int avpops_init(void)

static int avpops_child_init(int rank)
{
/* skip main process and TCP manager process */
if (!need_db || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
if (!need_db)
return 0;
/* init DB connection */
return avpops_db_init(&db_table, db_columns);
Expand Down
10 changes: 7 additions & 3 deletions modules/b2b_entities/b2b_entities.c
Expand Up @@ -351,10 +351,14 @@ static int child_init(int rank)
/** Module destroy function */
static void mod_destroy(void)
{
if(b2be_db ) {
if(b2be_db_mode==WRITE_BACK)
if (b2be_dbf.init && b2be_db_mode==WRITE_BACK) {
b2be_db = b2be_dbf.init(&db_url);
if(!b2be_db) {
LM_ERR("connecting to database failed, unable to flush\n");
} else {
b2b_entities_dump(1);
b2be_dbf.close(b2be_db);
b2be_dbf.close(b2be_db);
}
}
destroy_b2b_htables();
}
Expand Down
13 changes: 9 additions & 4 deletions modules/b2b_logic/b2b_logic.c
Expand Up @@ -750,11 +750,16 @@ static void mod_destroy(void)

b2b_scenario_t* scenario, *next;

if(b2bl_db)
{
if(b2bl_db_mode==WRITE_BACK)
if (b2bl_db_mode==WRITE_BACK && b2bl_dbf.init) {

b2bl_db = b2bl_dbf.init(&db_url);
if(!b2bl_db)
{
LM_ERR("connecting to database failed\n");
} else {
b2b_logic_dump(1);
b2bl_dbf.close(b2bl_db);
b2bl_dbf.close(b2bl_db);
}
}

scenario = extern_scenarios;
Expand Down
5 changes: 1 addition & 4 deletions modules/b2b_sca/b2b_sca.c
Expand Up @@ -346,7 +346,7 @@ static int mod_init(void)

static int child_init(int rank)
{
if (sca_db_mode==DB_MODE_REALTIME && (rank>PROC_MAIN || rank==PROC_MODULE)) {
if (sca_db_mode==DB_MODE_REALTIME && (rank>=1 || rank==PROC_MODULE)) {
if (connect_sca_db(&db_url)) {
LM_ERR("failed to connect to database (rank=%d)\n",rank);
return -1;
Expand All @@ -359,9 +359,6 @@ static int child_init(int rank)

static void mod_destroy(void)
{
//if (sca_db_mode != DB_MODE_NONE) {
// sca_update_db();
//}
destroy_b2b_sca_handlers();
destroy_b2b_sca_htable();

Expand Down
4 changes: 0 additions & 4 deletions modules/cachedb_couchbase/cachedb_couchbase.c
Expand Up @@ -125,10 +125,6 @@ static int child_init(int rank)
struct cachedb_url *it;
cachedb_con *con;

if(rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
return 0;
}

for (it = couchbase_script_urls;it;it=it->next) {
LM_DBG("iterating through couchbase conns - [%.*s]\n",it->url.len,it->url.s);
con = couchbase_init(&it->url);
Expand Down
4 changes: 0 additions & 4 deletions modules/cachedb_memcached/cachedb_memcached.c
Expand Up @@ -480,10 +480,6 @@ static int child_init(int rank)
struct cachedb_url *it;
cachedb_con *con;

if(rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
return 0;
}

for (it = memcached_script_urls;it;it=it->next) {
con = memcached_init(&it->url);
if (con == NULL) {
Expand Down
4 changes: 0 additions & 4 deletions modules/cachedb_mongodb/cachedb_mongodb.c
Expand Up @@ -147,10 +147,6 @@ static int child_init(int rank)
struct cachedb_url *it;
cachedb_con *con;

if(rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
return 0;
}

for (it = mongodb_script_urls;it;it=it->next) {
LM_DBG("iterating through conns - [%.*s]\n",it->url.len,it->url.s);
con = mongo_con_init(&it->url);
Expand Down
4 changes: 0 additions & 4 deletions modules/cachedb_redis/cachedb_redis.c
Expand Up @@ -122,10 +122,6 @@ static int child_init(int rank)
struct cachedb_url *it;
cachedb_con *con;

if(rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
return 0;
}

for (it = redis_script_urls;it;it=it->next) {
LM_DBG("iterating through conns - [%.*s]\n",it->url.len,it->url.s);
con = redis_init(&it->url);
Expand Down
2 changes: 1 addition & 1 deletion modules/call_center/call_center.c
Expand Up @@ -308,7 +308,7 @@ static int mod_init(void)
static int child_init( int rank )
{
/* init DB connection */
if ( rank<PROC_MAIN)
if ( rank<1 )
return 0;
if ( cc_connect_db(&db_url)!=0 ) {
LM_CRIT("cannot initialize database connection\n");
Expand Down
8 changes: 6 additions & 2 deletions modules/cgrates/cgrates.c
Expand Up @@ -282,8 +282,12 @@ static int child_init(int rank)
struct cgr_engine *e;
struct cgr_conn *c;

/* connect to all servers */
/* go through each server and initialize a single connection */
/* external procs don't have a reactor, so they won't be able
* to run any commands received by CGRateS, nor they will generate cmds */
if (rank == PROC_MODULE)
return 0;

/* go through each server and initialize a default connection */
list_for_each(l, &cgrates_engines) {
e = list_entry(l, struct cgr_engine, list);
/* start a connection for everybody */
Expand Down
6 changes: 0 additions & 6 deletions modules/clusterer/clusterer_mod.c
Expand Up @@ -1176,12 +1176,6 @@ int cmd_check_addr(struct sip_msg *msg, char *param_cluster, char *param_ip,

static void destroy(void)
{
if (db_hdl) {
/* close DB connection */
dr_dbf.close(db_hdl);
db_hdl = NULL;
}

/* destroy data */
if (cluster_list) {
if (*cluster_list)
Expand Down
17 changes: 9 additions & 8 deletions modules/dialog/dialog.c
Expand Up @@ -1022,12 +1022,9 @@ static void rpc_load_dlg_db(int sender, void *param)

static int child_init(int rank)
{
if (
(dlg_db_mode==DB_MODE_REALTIME && (rank>=PROC_MAIN||rank==PROC_MODULE)) ||
(dlg_db_mode==DB_MODE_SHUTDOWN && (rank==PROC_MAIN||rank==PROC_MODULE)) ||
(dlg_db_mode==DB_MODE_DELAYED && (rank>=PROC_MAIN||rank==PROC_MODULE))
){
if ( dlg_connect_db(&db_url) ) {
if ( (dlg_db_mode==DB_MODE_REALTIME || dlg_db_mode==DB_MODE_DELAYED ) &&
(rank>=1 || rank==PROC_MODULE) ) {
if ( dlg_connect_db(&db_url)<0 ) {
LM_ERR("failed to connect to database (rank=%d)\n",rank);
return -1;
}
Expand All @@ -1050,8 +1047,12 @@ static void mod_destroy(void)
struct dlg_sharing_tag *tag, *tag_tmp;

if (dlg_db_mode != DB_MODE_NONE) {
dialog_update_db(0, 0/*do not do locking*/);
destroy_dlg_db();
if ( dlg_connect_db(&db_url)<0 ) {
LM_ERR("failed to connect to database\n");
} else {
dialog_update_db(0, 0/*do not do locking*/);
destroy_dlg_db();
}
}

if (shtags_list) {
Expand Down
22 changes: 15 additions & 7 deletions modules/dispatcher/dispatcher.c
Expand Up @@ -914,11 +914,10 @@ static int mod_init(void)
#include "../../pt.h"
static int ds_child_init(int rank)
{
/* we need DB connection from the worker procs (for the flushing)
* and from the main proc (for final flush on shutdown) */
if ( rank>=PROC_MAIN ) {
ds_partition_t *partition_it;

ds_partition_t *partition_it;
/* we need DB connection from the worker procs (for the flushing) */
if ( rank>=1 ) {

for (partition_it = partitions; partition_it;
partition_it = partition_it->next){
Expand Down Expand Up @@ -955,13 +954,22 @@ static int mi_child_init(void)
*/
static void destroy(void)
{
ds_partition_t *part_it = partitions, *aux;

LM_DBG("destroying module ...\n");

/* flush the state of the destinations */
if (ds_persistent_state)
ds_flusher_routine(0, NULL);
if (ds_persistent_state) {
/* open the DB conns*/
for (part_it = partitions; part_it; part_it = part_it->next) {
if (part_it->db_url.s)
if (ds_connect_db(part_it) != 0) {
LM_ERR("failed to do DB connect\n");
}
}

ds_partition_t *part_it = partitions, *aux;
ds_flusher_routine(0, NULL);
}

while (part_it) {
ds_destroy_data(part_it);
Expand Down
2 changes: 0 additions & 2 deletions modules/dns_cache/dns_cache.c
Expand Up @@ -144,8 +144,6 @@ static int child_init(int rank)
static void destroy(void)
{
LM_NOTICE("destroy module dns_cache ...\n");
if (cdbc)
cdbf.destroy(cdbc);
}

static int rdata_struct_len=sizeof(struct rdata)-sizeof(void *) -
Expand Down
6 changes: 1 addition & 5 deletions modules/domain/domain_mod.c
Expand Up @@ -286,7 +286,7 @@ static int mod_init(void)
static int child_init(int rank)
{
/* Check if database is needed by worker processes only */
if ( db_mode==0 && (rank>PROC_MAIN) ) {
if ( db_mode==0 && (rank>=1) ) {
if (domain_db_init(&db_url)<0) {
LM_ERR("Unable to connect to the database\n");
return -1;
Expand All @@ -304,10 +304,6 @@ static int mi_child_init(void)

static void destroy(void)
{
/* Destroy is called from the main process only,
* there is no need to close database here because
* it is closed in mod_init already
*/
if (hash_table) {
shm_free(hash_table);
hash_table = 0;
Expand Down
9 changes: 3 additions & 6 deletions modules/domainpolicy/domainpolicy_mod.c
Expand Up @@ -233,12 +233,9 @@ static int child_init(int rank)
{
LM_DBG("initializing\n");

/* Check if database is needed by child */
if (rank!=PROC_MAIN && rank!=PROC_TCP_MAIN) {
if (domainpolicy_db_init(&db_url)<0) {
LM_ERR("unable to connect to the database\n");
return -1;
}
if (domainpolicy_db_init(&db_url)<0) {
LM_ERR("unable to connect to the database\n");
return -1;
}
return 0;
}
Expand Down
13 changes: 2 additions & 11 deletions modules/drouting/drouting.c
Expand Up @@ -1779,15 +1779,8 @@ static int dr_child_init(int rank)
{
struct head_db *head_db_it = head_db_start;

/* We need DB connection from:
* - attendant - for shutdown, flushing state
* - timer - may trigger routes with dr group
* - workers - execute routes with dr group
* - module's proc - ??? */
if (rank==PROC_TCP_MAIN)
return 0;

LM_DBG("Child initialization on rank %d \n",rank);

while( head_db_it!=NULL ) {
db_load_head( head_db_it );
head_db_it = head_db_it->next;
Expand All @@ -1811,11 +1804,9 @@ static int dr_exit(void)
while( it!=NULL ) {
to_clean = it;
it = it->next;
if (dr_persistent_state && to_clean->db_con && *(to_clean->db_con))
if (dr_persistent_state && to_clean->db_con && *(to_clean->db_con)) {
dr_state_flusher(to_clean);

/* close DB connection */
if (to_clean->db_con && *(to_clean->db_con)) {
(to_clean->db_funcs).close(*(to_clean->db_con));
*(to_clean->db_con) = 0;
pkg_free(to_clean->db_con);
Expand Down
5 changes: 1 addition & 4 deletions modules/emergency/emergency_methods.c
Expand Up @@ -369,7 +369,7 @@ static int mod_init(void) {
static int child_init(int rank) {
LM_DBG("Initializing child\n");

if (db_url.s && rank>PROC_MAIN) {
if (db_url.s && rank>=1) {
/* open a test connection */

if ((db_con = db_funcs.init(&db_url)) == 0) {
Expand All @@ -396,9 +396,6 @@ static int child_init(int rank) {
static void mod_destroy(void) {
curl_global_cleanup();

if (db_con != NULL && db_funcs.close != 0)
db_funcs.close(db_con);

if(ref_lock){
lock_destroy_rw( ref_lock );
ref_lock = NULL;
Expand Down
3 changes: 2 additions & 1 deletion modules/freeswitch_scripting/fss_mod.c
Expand Up @@ -165,12 +165,13 @@ static int mod_init(void)

free_shm_str_dlist(&startup_fs_subs);

fss_db_close();

return 0;
}

static void mod_destroy(void)
{
fss_db_close();
}

static int fixup_fs_esl(void **param, int param_no)
Expand Down
2 changes: 0 additions & 2 deletions modules/group/group_mod.c
Expand Up @@ -296,8 +296,6 @@ static int mod_init(void)

static void destroy(void)
{
if (!db_url.s)
group_db_close();
}


Expand Down
6 changes: 0 additions & 6 deletions modules/h350/h350_mod.c
Expand Up @@ -138,12 +138,6 @@ struct module_exports exports = {

static int child_init(int rank)
{

/* don't do anything for non-worker process */
if (rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
return 0;
}

h350_search_scope_int = ldap_api.ldap_str2scope(h350_search_scope.s);

/*
Expand Down

0 comments on commit 54e380d

Please sign in to comment.