From fda5be331d24342beb5b50e7171b4a3be9c5b4fc Mon Sep 17 00:00:00 2001 From: Lucian Balaceanu Date: Tue, 5 Sep 2017 14:21:40 +0300 Subject: [PATCH] p_usrloc: add config framework support for certain parameters The following params are now configurable via kamcmd cfg: expire_time, db_err_threshold, failover_level, db_ops_ruid, db_update_as_insert --- src/modules/p_usrloc/config.c | 54 +++++++++++++++++++++ src/modules/p_usrloc/config.h | 45 +++++++++++++++++ src/modules/p_usrloc/doc/p_usrloc_admin.xml | 30 ++++++++++++ src/modules/p_usrloc/p_usrloc_mod.c | 19 ++++---- src/modules/p_usrloc/p_usrloc_mod.h | 5 -- src/modules/p_usrloc/ucontact.c | 8 +-- src/modules/p_usrloc/ul_db.c | 7 +-- src/modules/p_usrloc/ul_db_failover.c | 5 +- src/modules/p_usrloc/ul_db_handle.c | 10 ++-- src/modules/p_usrloc/urecord.c | 3 +- 10 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/modules/p_usrloc/config.c create mode 100644 src/modules/p_usrloc/config.h diff --git a/src/modules/p_usrloc/config.c b/src/modules/p_usrloc/config.c new file mode 100644 index 00000000000..fd04ca63e50 --- /dev/null +++ b/src/modules/p_usrloc/config.c @@ -0,0 +1,54 @@ +/* + * $Id$ + * This file is part of SIP-router, a free SIP server. + * + * SIP-router is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * SIP-router is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/*! + * \file + * \brief P_USRLOC :: Configuration + * \ingroup usrloc + */ + + +//#include "../../core/parser/msg_parser.h" /* method types */ + +#include "config.h" + +struct cfg_group_p_usrloc default_p_usrloc_cfg = { + DEFAULT_EXPIRE, /* expire_time */ + DEFAULT_ERR_THRESHOLD, /* db_err_threshold */ + DEFAULT_FAILOVER_LEVEL, /* failover_level */ + 0, /* db_ops_ruid */ + 1 /* db_update_as_insert */ + }; + +void *p_usrloc_cfg = &default_p_usrloc_cfg; + +cfg_def_t p_usrloc_cfg_def[] = { + {"expire_time", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Contains number of second to expire if no expire hf or contact expire present" }, + {"db_err_threshold", CFG_VAR_INT | CFG_ATOMIC, 0, 100, 0, 0, + " Specifies the error value on which a database shall be turned off. "}, + {"failover_level", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Specifies the manner a failover is done (1 = turn off, 2 = find a spare) "}, + {"db_ops_ruid", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Set this if you want to update / delete from DB using ruid value "}, + {"db_update_as_insert", CFG_VAR_INT | CFG_ATOMIC, 0, 0, 0, 0, + "Set this parameter if you want to do INSERT DB operations instead of UPDATE DB operations. "}, + {0, 0, 0, 0, 0, 0} +}; diff --git a/src/modules/p_usrloc/config.h b/src/modules/p_usrloc/config.h new file mode 100644 index 00000000000..53944428ee7 --- /dev/null +++ b/src/modules/p_usrloc/config.h @@ -0,0 +1,45 @@ +/* + * $Id$ + * + * SIP-router is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/*! + * \file + * \brief P_USRLOC :: Configuration + * \ingroup usrloc + */ + + +#ifndef _P_USRLOC_CONFIG_H +#define _P_USRLOC_CONFIG_H + + +#include "../../core/cfg/cfg.h" +#include "../../core/str.h" +#include "p_usrloc_mod.h" + +struct cfg_group_p_usrloc { + unsigned int expire_time; + unsigned int db_err_threshold; + unsigned int failover_level; + unsigned int db_ops_ruid; + unsigned int db_update_as_insert; +}; + +extern struct cfg_group_p_usrloc default_p_usrloc_cfg; +extern void *p_usrloc_cfg; +extern cfg_def_t p_usrloc_cfg_def[]; + +extern void default_expires_stats_update(str*, str*); +extern void default_expires_range_update(str*, str*); +extern void max_expires_stats_update(str*, str*); + +#endif diff --git a/src/modules/p_usrloc/doc/p_usrloc_admin.xml b/src/modules/p_usrloc/doc/p_usrloc_admin.xml index d1cd9e5131d..9fb34df5340 100644 --- a/src/modules/p_usrloc/doc/p_usrloc_admin.xml +++ b/src/modules/p_usrloc/doc/p_usrloc_admin.xml @@ -345,6 +345,12 @@ modparam("p_usrloc", "risk_group_column", "rg") Default value is 3600. + + Can be set at runtime, e.g.: + + $ &sercmd; cfg.set_now_int p_usrloc expire_time 7200 + + Set <varname>expire_time</varname> parameter @@ -364,6 +370,12 @@ modparam("p_usrloc", "expire_time", "3600") Default value is 50. + + Can be set at runtime, e.g.: + + $ &sercmd; cfg.set_now_int p_usrloc db_err_threshold 20 + + Set <varname>db_err_threshold</varname> parameter @@ -392,6 +404,12 @@ modparam("p_usrloc", "db_err_threshold", "50") Default value is 1. + + Can be set at runtime, e.g.: + + $ &sercmd; cfg.set_now_int p_usrloc failover_level 2 + + Set <varname>failover_level</varname> parameter @@ -597,6 +615,12 @@ modparam("p_usrloc", "default_db_type", "cluster") Default value is 1. + + Can be set at runtime, e.g.: + + $ &sercmd; cfg.set_now_int p_usrloc db_ops_ruid 1 + + Set <varname>db_ops_ruid</varname> parameter @@ -619,6 +643,12 @@ modparam("p_usrloc", "db_ops_ruid", 0) Default value is 0. + + Can be set at runtime, e.g.: + + $ &sercmd; cfg.set_now_int p_usrloc db_update_as_insert 1 + + Set <varname>db_update_as_insert</varname> parameter diff --git a/src/modules/p_usrloc/p_usrloc_mod.c b/src/modules/p_usrloc/p_usrloc_mod.c index 6ee7ce0a96b..21d78785e97 100644 --- a/src/modules/p_usrloc/p_usrloc_mod.c +++ b/src/modules/p_usrloc/p_usrloc_mod.c @@ -58,6 +58,7 @@ #include "ul_db.h" #include "ul_db_layer.h" #include "dlist.h" +#include "config.h" MODULE_VERSION @@ -85,7 +86,6 @@ static void destroy(void); /*!< Module destroy function static int child_init(int rank); /*!< Per-child init function */ extern int bind_usrloc(usrloc_api_t* api); extern int ul_locks_no; -int ul_db_update_as_insert = 0; /* * Module parameters and their default values @@ -160,15 +160,11 @@ str failover_time_col = str_init(FAILOVER_T_COL); str spare_col = str_init(SPARE_COL); str error_col = str_init(ERROR_COL); str risk_group_col = str_init(RISK_GROUP_COL); -int expire_time = DEFAULT_EXPIRE; -int db_error_threshold = DEFAULT_ERR_THRESHOLD; -int failover_level = DEFAULT_FAILOVER_LEVEL; int retry_interval = DB_RETRY; int policy = DB_DEFAULT_POLICY; int db_write = 0; int db_master_write = 0; int alg_location = 0; -int ul_db_ops_ruid = 0; int db_use_transactions = 0; str db_transaction_level = str_init(DB_DEFAULT_TRANSACTION_LEVEL); @@ -240,9 +236,9 @@ static param_export_t params[] = { {"spare_flag_column", PARAM_STR, &spare_col }, {"error_column", PARAM_STR, &error_col }, {"risk_group_column", PARAM_STR, &risk_group_col }, - {"expire_time", INT_PARAM, &expire_time }, - {"db_err_threshold", INT_PARAM, &db_error_threshold }, - {"failover_level", INT_PARAM, &failover_level }, + {"expire_time", INT_PARAM, &default_p_usrloc_cfg.expire_time}, + {"db_err_threshold", INT_PARAM, &default_p_usrloc_cfg.db_err_threshold}, + {"failover_level", INT_PARAM, &default_p_usrloc_cfg.failover_level}, {"db_retry_interval", INT_PARAM, &retry_interval }, {"db_use_transactions", INT_PARAM, &db_use_transactions }, {"db_transaction_level", INT_PARAM, &db_transaction_level}, @@ -250,7 +246,8 @@ static param_export_t params[] = { {"write_on_master_db", INT_PARAM, &db_master_write }, {"connection_expires", INT_PARAM, &connection_expires }, {"alg_location", INT_PARAM, &alg_location }, - {"db_ops_ruid", INT_PARAM, &ul_db_ops_ruid }, + {"db_ops_ruid", INT_PARAM, &default_p_usrloc_cfg.db_ops_ruid}, + {"db_update_as_insert", INT_PARAM, &default_p_usrloc_cfg.db_update_as_insert}, {0, 0, 0} }; @@ -316,6 +313,10 @@ static int mod_init(void) ul_hash_size = 1< ul_db_handle_t dbh_tmp; @@ -163,7 +164,7 @@ int db_handle_error(ul_db_handle_t * handle, int no) { return -1; } - if (db->errors < db_error_threshold) { + if (db->errors < cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)) { if (sprintf(query, "UPDATE %.*s " "SET %.*s=%.*s+1 " "WHERE %.*s=%i " @@ -199,8 +200,8 @@ int db_handle_error(ul_db_handle_t * handle, int no) { refresh_handle(handle, &dbh_tmp, 0); LM_ERR("error on id %i, db %i, " "errors occurred: %i, threshold: %i\n", - handle->id, db->no, db->errors, db_error_threshold); - if(db->errors >= db_error_threshold) { + handle->id, db->no, db->errors, cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)); + if(db->errors >= cfg_get(p_usrloc, p_usrloc_cfg, db_err_threshold)) { LM_DBG("db_handle_error: now doing failover"); if((db_failover(&mdb.write.dbf, mdb.write.dbh, handle, no)) < 0) { LM_ERR("error in doing failover.\n"); diff --git a/src/modules/p_usrloc/ul_db_failover.c b/src/modules/p_usrloc/ul_db_failover.c index 3ac0ddfdb25..d2e6f18dac3 100644 --- a/src/modules/p_usrloc/ul_db_failover.c +++ b/src/modules/p_usrloc/ul_db_failover.c @@ -24,6 +24,7 @@ #include "ul_db_handle.h" #include "ul_db.h" #include "p_usrloc_mod.h" +#include "config.h" static ul_db_handle_t spare; @@ -38,7 +39,7 @@ static int ul_db_failover_switch(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_ static int ul_db_failover_normal(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no); int db_failover(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int no) { - if(failover_level & FAILOVER_MODE_NORMAL){ + if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) & FAILOVER_MODE_NORMAL){ if(ul_db_failover_normal(dbf, dbh, handle, no) < 0){ LM_ERR("could not switch to spare, try to " "turn off broken db id %i, db %i.\n", @@ -47,7 +48,7 @@ int db_failover(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle, int n return 0; } } - if(failover_level & (FAILOVER_MODE_NONE | FAILOVER_MODE_NORMAL)){ + if(cfg_get(p_usrloc, p_usrloc_cfg, failover_level) & (FAILOVER_MODE_NONE | FAILOVER_MODE_NORMAL)){ if(db_failover_deactivate(dbf, dbh, handle, no) < 0){ LM_ERR("could not deactivate " "id %i, db %i.\n", diff --git a/src/modules/p_usrloc/ul_db_handle.c b/src/modules/p_usrloc/ul_db_handle.c index d8274509168..3b8549be033 100644 --- a/src/modules/p_usrloc/ul_db_handle.c +++ b/src/modules/p_usrloc/ul_db_handle.c @@ -23,6 +23,7 @@ #include "p_usrloc_mod.h" #include "ul_db.h" #include "ul_db_watch.h" +#include "config.h" #include "../../core/crc.h" static ul_db_handle_list_t * db_handles = NULL; @@ -382,9 +383,12 @@ int check_handle(db_func_t * dbf, db1_con_t * dbh, ul_db_handle_t * handle){ handle->id, handle->db[i].no, handle->db[i].url.len, handle->db[i].url.s); } } else if((handle->db[i].status == DB_ON) && handle->db[i].dbh) { - if((handle->db[i].failover_time < (time(NULL) - expire_time)) && (handle->db[i].failover_time != UL_DB_ZERO_TIME)){ - LM_ERR("%s: failover_time: %ld, now: %ld, delta: %ld, now going to reset failover time\n", __FUNCTION__, - (long int)handle->db[i].failover_time, (long int)time(NULL), (long int)(time(NULL) - handle->db[i].failover_time)); + if ((handle->db[i].failover_time + < (time(NULL) + - cfg_get(p_usrloc, p_usrloc_cfg, expire_time))) + && (handle->db[i].failover_time != UL_DB_ZERO_TIME)) { + LM_ERR("%s: failover_time: %ld, now: %ld, delta: %ld, now going to reset failover time\n", __FUNCTION__, + (long int)handle->db[i].failover_time, (long int)time(NULL), (long int)(time(NULL) - handle->db[i].failover_time)); if(db_reset_failover_time(handle, handle->db[i].no) < 0) { LM_ERR("could not reset failover time for id %i, db %i.\n", handle->id, handle->db[i].no); diff --git a/src/modules/p_usrloc/urecord.c b/src/modules/p_usrloc/urecord.c index 16bd1084ffc..b517586d7a6 100644 --- a/src/modules/p_usrloc/urecord.c +++ b/src/modules/p_usrloc/urecord.c @@ -38,6 +38,7 @@ #include "../usrloc/ul_callback.h" #include "ul_db_layer.h" +#include "config.h" /*! contact matching mode */ @@ -348,7 +349,7 @@ static inline void wb_timer(urecord_t* _r) break; case 2: /* update */ - if (ul_db_update_as_insert) + if (cfg_get(p_usrloc,p_usrloc_cfg,db_update_as_insert)) res = db_insert_ucontact(ptr); else res = db_update_ucontact(ptr);