2
0
Эх сурвалжийг харах

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
Lucian Balaceanu 8 жил өмнө
parent
commit
fda5be331d

+ 54 - 0
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}
+};

+ 45 - 0
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

+ 30 - 0
src/modules/p_usrloc/doc/p_usrloc_admin.xml

@@ -345,6 +345,12 @@ modparam("p_usrloc", "risk_group_column", "rg")
 		    Default value is <quote>3600</quote>.
 		</emphasis>
 	    </para>
+		<para>
+			Can be set at runtime, e.g.:
+			<programlisting>
+			$ &sercmd; cfg.set_now_int p_usrloc expire_time 7200
+			</programlisting>
+		</para>
 	    <example>
 		<title>Set <varname>expire_time</varname> parameter</title>
 		<programlisting format="linespecific">
@@ -364,6 +370,12 @@ modparam("p_usrloc", "expire_time", "3600")
 		    Default value is <quote>50</quote>.
 		</emphasis>
 	    </para>
+		<para>
+			Can be set at runtime, e.g.:
+			<programlisting>
+			$ &sercmd; cfg.set_now_int p_usrloc db_err_threshold 20
+			</programlisting>
+		</para>
 	    <example>
 		<title>Set <varname>db_err_threshold</varname> parameter</title>
 		<programlisting format="linespecific">
@@ -392,6 +404,12 @@ modparam("p_usrloc", "db_err_threshold", "50")
 		    Default value is <quote>1</quote>.
 		</emphasis>
 	    </para>
+		<para>
+			Can be set at runtime, e.g.:
+			<programlisting>
+			$ &sercmd; cfg.set_now_int p_usrloc failover_level 2
+			</programlisting>
+		</para>
 	    <example>
 		<title>Set <varname>failover_level</varname> parameter</title>
 		<programlisting format="linespecific">
@@ -597,6 +615,12 @@ modparam("p_usrloc", "default_db_type", "cluster")
 		Default value is <quote>1</quote>.
 	</emphasis>
 	</para>
+	<para>
+		Can be set at runtime, e.g.:
+		<programlisting>
+		$ &sercmd; cfg.set_now_int p_usrloc db_ops_ruid 1
+		</programlisting>
+	</para>
 	<example>
 	<title>Set <varname>db_ops_ruid</varname> parameter</title>
 	<programlisting format="linespecific">
@@ -619,6 +643,12 @@ modparam("p_usrloc", "db_ops_ruid", 0)
              Default value is <quote>0</quote>.
         </emphasis>
 	</para>
+	<para>
+		Can be set at runtime, e.g.:
+		<programlisting>
+		$ &sercmd; cfg.set_now_int p_usrloc db_update_as_insert 1
+		</programlisting>
+	</para>
 	<example>
 	<title>Set <varname>db_update_as_insert</varname> parameter</title>
 <programlisting format="linespecific">

+ 10 - 9
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_hash_size;
 	ul_locks_no = ul_hash_size;
 
+	if(cfg_declare("p_usrloc", p_usrloc_cfg_def, &default_p_usrloc_cfg, cfg_sizeof(p_usrloc), &p_usrloc_cfg)){
+		LM_ERR("Fail to declare the configuration\n");
+	        return -1;
+	}
 	/* check matching mode */
 	switch (matching_mode) {
 		case CONTACT_ONLY:

+ 0 - 5
src/modules/p_usrloc/p_usrloc_mod.h

@@ -91,7 +91,6 @@ extern int desc_time_order;
 extern int cseq_delay;
 extern int ul_fetch_rows;
 extern int ul_hash_size;
-extern int ul_db_update_as_insert;
 
 
 extern str default_db_url;
@@ -101,7 +100,6 @@ extern str domain_db;
 extern int expire;
 
 extern int matching_mode;
-extern int ul_db_ops_ruid;
 
 extern str write_db_url;
 extern str read_db_url;
@@ -114,9 +112,6 @@ extern str failover_time_col;
 extern str spare_col;
 extern str error_col;
 extern str risk_group_col;
-extern int expire_time;
-extern int db_error_threshold;
-extern int failover_level;
 extern int retry_interval;
 extern int policy;
 extern int db_write;

+ 5 - 3
src/modules/p_usrloc/ucontact.c

@@ -40,6 +40,8 @@
 #include "ucontact.h"
 #include "ul_db_layer.h"
 #include "dlist.h"
+#include "config.h"
+
 
 /*!
  * \brief Create a new contact structure
@@ -957,7 +959,7 @@ int db_update_ucontact_ruid(ucontact_t* _c)
  */
 int db_update_ucontact(ucontact_t* _c)
 {
-   if(ul_db_ops_ruid==0)
+   if(cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid)==0)
        return db_update_ucontact_addr(_c);
    else
        return db_update_ucontact_ruid(_c);
@@ -1090,7 +1092,7 @@ int db_delete_ucontact_ruid(ucontact_t* _c)
  */
 int db_delete_ucontact(ucontact_t* _c)
 {
-   if(ul_db_ops_ruid==0)
+	if (cfg_get(p_usrloc, p_usrloc_cfg, db_ops_ruid) == 0)
        return db_delete_ucontact_addr(_c);
    else
        return db_delete_ucontact_ruid(_c);
@@ -1204,7 +1206,7 @@ int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci)
 		 * in the second DB will not work. Thus the expire mechanism don't work, it
 		 * takes too long until both DBs have the same number of entries again.
 		 */
-		if (ul_db_update_as_insert)
+		if (cfg_get(p_usrloc, p_usrloc_cfg, db_update_as_insert))
 		    res = db_insert_ucontact(_c);
         else
             res = db_update_ucontact(_c);

+ 4 - 3
src/modules/p_usrloc/ul_db.c

@@ -30,6 +30,7 @@
 #include "ul_db_del.h"
 #include "ul_db_query.h"
 #include "ul_check.h"
+#include "config.h"
 #include <unistd.h>
 
 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");

+ 3 - 2
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",

+ 7 - 3
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);

+ 2 - 1
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);