Przeglądaj źródła

carrierroute: addind db_load_description parameter

- toggle on/off loading in memory the description column in the
  carrierroute/carrierfailureroute database tables. This reduces the
  shared memory used by the module
Lucian Balaceanu 9 lat temu
rodzic
commit
6a738a9123

+ 10 - 0
modules/carrierroute/carrierroute.c

@@ -50,6 +50,7 @@
 #include "cr_func.h"
 #include "db_carrierroute.h"
 #include "config.h"
+#include "cr_db.h"
 #include <sys/stat.h>
 
 #define AVP_CR_URIS "_cr_uris"
@@ -63,6 +64,7 @@ str subscriber_table = str_init("subscriber");
 static str subscriber_username_col = str_init("username");
 static str subscriber_domain_col = str_init("domain");
 static str cr_preferred_carrier_col = str_init("cr_preferred_carrier");
+static int cr_load_comments = 1;
 
 str * subscriber_columns[SUBSCRIBER_COLUMN_NUM] = {
 	&subscriber_username_col,
@@ -118,6 +120,7 @@ static param_export_t params[]= {
 	{"use_domain",                INT_PARAM, &default_carrierroute_cfg.use_domain },
 	{"fallback_default",          INT_PARAM, &default_carrierroute_cfg.fallback_default },
 	{"fetch_rows",                INT_PARAM, &default_carrierroute_cfg.fetch_rows },
+	{"db_load_description", 	  INT_PARAM, &cr_load_comments },
 	{"match_mode",                INT_PARAM, &cr_match_mode },
 	{"avoid_failed_destinations", INT_PARAM, &cr_avoid_failed_dests },
 	{0,0,0}
@@ -186,9 +189,16 @@ static int mod_init(void) {
 		return -1;
 	}
 
+	if (cr_load_comments != 0 && cr_load_comments != 1) {
+		LM_ERR("db_load_comments must be 0 or 1");
+		return -1;
+	}
+
 	if (strcmp(config_source, "db") == 0) {
 		mode = CARRIERROUTE_MODE_DB;
 
+		set_load_comments_params(cr_load_comments);
+
 		LM_INFO("use database as configuration source\n");
 		if(carrierroute_db_init() < 0){
 			return -1;

+ 34 - 11
modules/carrierroute/cr_db.c

@@ -39,6 +39,7 @@
 
 #define QUERY_LEN 2048
 
+static int columns_load_num, failure_columns_load_num, load_comments;
 static char query[QUERY_LEN];
 
 str * columns[COLUMN_NUM] = { &carrierroute_id_col, &carrierroute_carrier_col,
@@ -78,6 +79,15 @@ str * failure_columns[FAILURE_COLUMN_NUM] = {
 };
 
 
+void set_load_comments_params(int lc) {
+	load_comments = lc;
+	columns_load_num = lc ? COLUMN_NUM : COLUMN_NUM_NO_COMMENT;
+	failure_columns_load_num = lc ? FAILURE_COLUMN_NUM : FAILURE_COLUMN_NUM_NO_COMMENT;
+}
+
+
+
+
 static int load_carrier_map(struct route_data_t *rd) {
 	db1_res_t * res = NULL;
 	int i, count;
@@ -263,6 +273,7 @@ int load_route_data_db(struct route_data_t * rd) {
 	static str query_str;
 	str tmp_scan_prefix, tmp_rewrite_host, tmp_rewrite_prefix,
 		tmp_rewrite_suffix, tmp_host_name, tmp_reply_code, tmp_comment;
+	str *p_tmp_comment;
 
 	if( (strlen("SELECT DISTINCT  FROM  WHERE = ")
 			+ carrierroute_table.len + columns[COL_DOMAIN]->len
@@ -325,7 +336,7 @@ int load_route_data_db(struct route_data_t * rd) {
 
 	if (DB_CAPABILITY(carrierroute_dbf, DB_CAP_FETCH)) {
 		if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *) columns, 0,
-					COLUMN_NUM, NULL, NULL) < 0) {
+				columns_load_num, NULL, NULL) < 0) {
 			LM_ERR("Failed to query database to prepare fetch row.\n");
 			return -1;
 		}
@@ -335,7 +346,7 @@ int load_route_data_db(struct route_data_t * rd) {
 		}
 	} else {
 		if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *) columns, 0,
-				 COLUMN_NUM, NULL, &res) < 0) {
+				columns_load_num, NULL, &res) < 0) {
 			LM_ERR("Failed to query database.\n");
 			return -1;
 		}
@@ -349,17 +360,23 @@ int load_route_data_db(struct route_data_t * rd) {
 			tmp_rewrite_host.s=(char *)row->values[COL_REWRITE_HOST].val.string_val;
 			tmp_rewrite_prefix.s=(char *)row->values[COL_REWRITE_PREFIX].val.string_val;
 			tmp_rewrite_suffix.s=(char *)row->values[COL_REWRITE_SUFFIX].val.string_val;
-			tmp_comment.s=(char *)row->values[COL_COMMENT].val.string_val;
 			if (tmp_scan_prefix.s==NULL) tmp_scan_prefix.s="";
 			if (tmp_rewrite_host.s==NULL) tmp_rewrite_host.s="";
 			if (tmp_rewrite_prefix.s==NULL) tmp_rewrite_prefix.s="";
 			if (tmp_rewrite_suffix.s==NULL) tmp_rewrite_suffix.s="";
-			if (tmp_comment.s==NULL) tmp_comment.s="";
 			tmp_scan_prefix.len=strlen(tmp_scan_prefix.s);
 			tmp_rewrite_host.len=strlen(tmp_rewrite_host.s);
 			tmp_rewrite_prefix.len=strlen(tmp_rewrite_prefix.s);
 			tmp_rewrite_suffix.len=strlen(tmp_rewrite_suffix.s);
-			tmp_comment.len=strlen(tmp_comment.s);
+
+			p_tmp_comment = NULL;
+			if (load_comments) {
+				tmp_comment.s = (char *)row->values[COL_COMMENT].val.string_val;
+				if (tmp_comment.s==NULL) tmp_comment.s="";
+				tmp_comment.len=strlen(tmp_comment.s);
+				p_tmp_comment = &tmp_comment;
+			}
+
 			if (add_route(rd,
 					row->values[COL_CARRIER].val.int_val,
 					row->values[COL_DOMAIN].val.int_val,
@@ -376,7 +393,7 @@ int load_route_data_db(struct route_data_t * rd) {
 					0,
 					-1,
 					NULL,
-					&tmp_comment) == -1) {
+					p_tmp_comment) == -1) {
 				goto errout;
 			}
 		}
@@ -400,7 +417,7 @@ int load_route_data_db(struct route_data_t * rd) {
 		return -1;
 	}
 	if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *)failure_columns, 0,
-								FAILURE_COLUMN_NUM, NULL, &res) < 0) {
+			failure_columns_load_num, NULL, &res) < 0) {
 		LM_ERR("failed to query database.\n");
 		return -1;
 	}
@@ -409,15 +426,21 @@ int load_route_data_db(struct route_data_t * rd) {
 		tmp_scan_prefix.s=(char *)row->values[FCOL_SCAN_PREFIX].val.string_val;
 		tmp_host_name.s=(char *)row->values[FCOL_HOST_NAME].val.string_val;
 		tmp_reply_code.s=(char *)row->values[FCOL_REPLY_CODE].val.string_val;
-		tmp_comment.s=(char *)row->values[FCOL_COMMENT].val.string_val;
 		if (tmp_scan_prefix.s==NULL) tmp_scan_prefix.s="";
 		if (tmp_host_name.s==NULL) tmp_host_name.s="";
 		if (tmp_reply_code.s==NULL) tmp_reply_code.s="";
-		if (tmp_comment.s==NULL) tmp_comment.s="";
 		tmp_scan_prefix.len=strlen(tmp_scan_prefix.s);
 		tmp_host_name.len=strlen(tmp_host_name.s);
 		tmp_reply_code.len=strlen(tmp_reply_code.s);
-		tmp_comment.len=strlen(tmp_comment.s);
+		p_tmp_comment = NULL;
+
+		if (load_comments) {
+			tmp_comment.s = (char *)row->values[FCOL_COMMENT].val.string_val;
+			if (tmp_comment.s==NULL) tmp_comment.s="";
+			tmp_comment.len=strlen(tmp_comment.s);
+			p_tmp_comment = &tmp_comment;
+		}
+
 		if (add_failure_route(rd,
 				row->values[FCOL_CARRIER].val.int_val,
 				row->values[COL_DOMAIN].val.int_val,
@@ -427,7 +450,7 @@ int load_route_data_db(struct route_data_t * rd) {
 				row->values[FCOL_FLAGS].val.int_val,
 				row->values[FCOL_MASK].val.int_val,
 				row->values[FCOL_NEXT_DOMAIN].val.int_val,
-				&tmp_comment) == -1) {
+				p_tmp_comment) == -1) {
 			goto errout;
 		}
 	}

+ 5 - 3
modules/carrierroute/cr_db.h

@@ -34,8 +34,6 @@
 #include "db_carrierroute.h"
 #include "cr_data.h"
 
-
-#define COLUMN_NUM 12
 #define COL_ID             0
 #define COL_CARRIER        1
 #define COL_DOMAIN         2
@@ -48,8 +46,9 @@
 #define COL_REWRITE_PREFIX 9
 #define COL_REWRITE_SUFFIX 10
 #define COL_COMMENT        11
+#define COLUMN_NUM 		   12
+#define COLUMN_NUM_NO_COMMENT COL_COMMENT
 
-#define FAILURE_COLUMN_NUM 10
 #define FCOL_ID             0
 #define FCOL_CARRIER        1
 #define FCOL_DOMAIN         2
@@ -60,6 +59,8 @@
 #define FCOL_MASK           7
 #define FCOL_NEXT_DOMAIN    8
 #define FCOL_COMMENT        9
+#define FAILURE_COLUMN_NUM 10
+#define FAILURE_COLUMN_NUM_NO_COMMENT FCOL_COMMENT
 
 #define CARRIER_NAME_COLUMN_NUM 2
 #define CARRIER_NAME_ID_COL 0
@@ -73,6 +74,7 @@ extern str * columns[];
 extern str * carrier_columns[];
 extern str * failure_columns[];
 
+void set_load_comments_params(int load_comments);
 
 /**
  * Loads the routing data from the database given in global

+ 1 - 1
modules/carrierroute/cr_rule.c

@@ -95,7 +95,7 @@ int add_route_rule(struct route_flags *rf, const str * prefix,
 		goto mem_error;
 	}
 
-	if (shm_str_dup(&shm_rr->comment, comment) != 0) {
+	if (comment && shm_str_dup(&shm_rr->comment, comment) != 0) {
 		goto mem_error;
 	}
 

+ 21 - 1
modules/carrierroute/doc/carrierroute_admin.xml

@@ -311,7 +311,27 @@ modparam("carrierroute", "fetch_rows", 3000)
 </programlisting>
 		</example>
 	</section>
-
+<section>
+		<title><varname>db_load_description</varname> (integer)</title>
+		<para>
+		Toggle on/off loading in memory the description column in the 
+		carrierroute/carrierfailureroute database tables. This reduces the 
+		shared memory used by the module.
+		</para>
+		<para>
+		<emphasis>
+			Default value is <quote>1</quote>.
+		</emphasis>
+		</para>
+		<example>
+		<title>Unset <varname>db_load_description</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("carrierroute", "db_load_description", 0)
+...
+</programlisting>
+		</example>
+	</section>
 	<section>
 		<title><varname>match_mode</varname> (integer)</title>
 		<para>