Browse code

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 authored on 16/11/2015 16:59:05
Showing 5 changed files
... ...
@@ -50,6 +50,7 @@
50 50
 #include "cr_func.h"
51 51
 #include "db_carrierroute.h"
52 52
 #include "config.h"
53
+#include "cr_db.h"
53 54
 #include <sys/stat.h>
54 55
 
55 56
 #define AVP_CR_URIS "_cr_uris"
... ...
@@ -63,6 +64,7 @@ str subscriber_table = str_init("subscriber");
63 63
 static str subscriber_username_col = str_init("username");
64 64
 static str subscriber_domain_col = str_init("domain");
65 65
 static str cr_preferred_carrier_col = str_init("cr_preferred_carrier");
66
+static int cr_load_comments = 1;
66 67
 
67 68
 str * subscriber_columns[SUBSCRIBER_COLUMN_NUM] = {
68 69
 	&subscriber_username_col,
... ...
@@ -118,6 +120,7 @@ static param_export_t params[]= {
118 118
 	{"use_domain",                INT_PARAM, &default_carrierroute_cfg.use_domain },
119 119
 	{"fallback_default",          INT_PARAM, &default_carrierroute_cfg.fallback_default },
120 120
 	{"fetch_rows",                INT_PARAM, &default_carrierroute_cfg.fetch_rows },
121
+	{"db_load_description", 	  INT_PARAM, &cr_load_comments },
121 122
 	{"match_mode",                INT_PARAM, &cr_match_mode },
122 123
 	{"avoid_failed_destinations", INT_PARAM, &cr_avoid_failed_dests },
123 124
 	{0,0,0}
... ...
@@ -186,9 +189,16 @@ static int mod_init(void) {
186 186
 		return -1;
187 187
 	}
188 188
 
189
+	if (cr_load_comments != 0 && cr_load_comments != 1) {
190
+		LM_ERR("db_load_comments must be 0 or 1");
191
+		return -1;
192
+	}
193
+
189 194
 	if (strcmp(config_source, "db") == 0) {
190 195
 		mode = CARRIERROUTE_MODE_DB;
191 196
 
197
+		set_load_comments_params(cr_load_comments);
198
+
192 199
 		LM_INFO("use database as configuration source\n");
193 200
 		if(carrierroute_db_init() < 0){
194 201
 			return -1;
... ...
@@ -39,6 +39,7 @@
39 39
 
40 40
 #define QUERY_LEN 2048
41 41
 
42
+static int columns_load_num, failure_columns_load_num, load_comments;
42 43
 static char query[QUERY_LEN];
43 44
 
44 45
 str * columns[COLUMN_NUM] = { &carrierroute_id_col, &carrierroute_carrier_col,
... ...
@@ -78,6 +79,15 @@ str * failure_columns[FAILURE_COLUMN_NUM] = {
78 78
 };
79 79
 
80 80
 
81
+void set_load_comments_params(int lc) {
82
+	load_comments = lc;
83
+	columns_load_num = lc ? COLUMN_NUM : COLUMN_NUM_NO_COMMENT;
84
+	failure_columns_load_num = lc ? FAILURE_COLUMN_NUM : FAILURE_COLUMN_NUM_NO_COMMENT;
85
+}
86
+
87
+
88
+
89
+
81 90
 static int load_carrier_map(struct route_data_t *rd) {
82 91
 	db1_res_t * res = NULL;
83 92
 	int i, count;
... ...
@@ -263,6 +273,7 @@ int load_route_data_db(struct route_data_t * rd) {
263 263
 	static str query_str;
264 264
 	str tmp_scan_prefix, tmp_rewrite_host, tmp_rewrite_prefix,
265 265
 		tmp_rewrite_suffix, tmp_host_name, tmp_reply_code, tmp_comment;
266
+	str *p_tmp_comment;
266 267
 
267 268
 	if( (strlen("SELECT DISTINCT  FROM  WHERE = ")
268 269
 			+ carrierroute_table.len + columns[COL_DOMAIN]->len
... ...
@@ -325,7 +336,7 @@ int load_route_data_db(struct route_data_t * rd) {
325 325
 
326 326
 	if (DB_CAPABILITY(carrierroute_dbf, DB_CAP_FETCH)) {
327 327
 		if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *) columns, 0,
328
-					COLUMN_NUM, NULL, NULL) < 0) {
328
+				columns_load_num, NULL, NULL) < 0) {
329 329
 			LM_ERR("Failed to query database to prepare fetch row.\n");
330 330
 			return -1;
331 331
 		}
... ...
@@ -335,7 +346,7 @@ int load_route_data_db(struct route_data_t * rd) {
335 335
 		}
336 336
 	} else {
337 337
 		if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *) columns, 0,
338
-				 COLUMN_NUM, NULL, &res) < 0) {
338
+				columns_load_num, NULL, &res) < 0) {
339 339
 			LM_ERR("Failed to query database.\n");
340 340
 			return -1;
341 341
 		}
... ...
@@ -349,17 +360,23 @@ int load_route_data_db(struct route_data_t * rd) {
349 349
 			tmp_rewrite_host.s=(char *)row->values[COL_REWRITE_HOST].val.string_val;
350 350
 			tmp_rewrite_prefix.s=(char *)row->values[COL_REWRITE_PREFIX].val.string_val;
351 351
 			tmp_rewrite_suffix.s=(char *)row->values[COL_REWRITE_SUFFIX].val.string_val;
352
-			tmp_comment.s=(char *)row->values[COL_COMMENT].val.string_val;
353 352
 			if (tmp_scan_prefix.s==NULL) tmp_scan_prefix.s="";
354 353
 			if (tmp_rewrite_host.s==NULL) tmp_rewrite_host.s="";
355 354
 			if (tmp_rewrite_prefix.s==NULL) tmp_rewrite_prefix.s="";
356 355
 			if (tmp_rewrite_suffix.s==NULL) tmp_rewrite_suffix.s="";
357
-			if (tmp_comment.s==NULL) tmp_comment.s="";
358 356
 			tmp_scan_prefix.len=strlen(tmp_scan_prefix.s);
359 357
 			tmp_rewrite_host.len=strlen(tmp_rewrite_host.s);
360 358
 			tmp_rewrite_prefix.len=strlen(tmp_rewrite_prefix.s);
361 359
 			tmp_rewrite_suffix.len=strlen(tmp_rewrite_suffix.s);
362
-			tmp_comment.len=strlen(tmp_comment.s);
360
+
361
+			p_tmp_comment = NULL;
362
+			if (load_comments) {
363
+				tmp_comment.s = (char *)row->values[COL_COMMENT].val.string_val;
364
+				if (tmp_comment.s==NULL) tmp_comment.s="";
365
+				tmp_comment.len=strlen(tmp_comment.s);
366
+				p_tmp_comment = &tmp_comment;
367
+			}
368
+
363 369
 			if (add_route(rd,
364 370
 					row->values[COL_CARRIER].val.int_val,
365 371
 					row->values[COL_DOMAIN].val.int_val,
... ...
@@ -376,7 +393,7 @@ int load_route_data_db(struct route_data_t * rd) {
376 376
 					0,
377 377
 					-1,
378 378
 					NULL,
379
-					&tmp_comment) == -1) {
379
+					p_tmp_comment) == -1) {
380 380
 				goto errout;
381 381
 			}
382 382
 		}
... ...
@@ -400,7 +417,7 @@ int load_route_data_db(struct route_data_t * rd) {
400 400
 		return -1;
401 401
 	}
402 402
 	if (carrierroute_dbf.query(carrierroute_dbh, NULL, NULL, NULL, (db_key_t *)failure_columns, 0,
403
-								FAILURE_COLUMN_NUM, NULL, &res) < 0) {
403
+			failure_columns_load_num, NULL, &res) < 0) {
404 404
 		LM_ERR("failed to query database.\n");
405 405
 		return -1;
406 406
 	}
... ...
@@ -409,15 +426,21 @@ int load_route_data_db(struct route_data_t * rd) {
409 409
 		tmp_scan_prefix.s=(char *)row->values[FCOL_SCAN_PREFIX].val.string_val;
410 410
 		tmp_host_name.s=(char *)row->values[FCOL_HOST_NAME].val.string_val;
411 411
 		tmp_reply_code.s=(char *)row->values[FCOL_REPLY_CODE].val.string_val;
412
-		tmp_comment.s=(char *)row->values[FCOL_COMMENT].val.string_val;
413 412
 		if (tmp_scan_prefix.s==NULL) tmp_scan_prefix.s="";
414 413
 		if (tmp_host_name.s==NULL) tmp_host_name.s="";
415 414
 		if (tmp_reply_code.s==NULL) tmp_reply_code.s="";
416
-		if (tmp_comment.s==NULL) tmp_comment.s="";
417 415
 		tmp_scan_prefix.len=strlen(tmp_scan_prefix.s);
418 416
 		tmp_host_name.len=strlen(tmp_host_name.s);
419 417
 		tmp_reply_code.len=strlen(tmp_reply_code.s);
420
-		tmp_comment.len=strlen(tmp_comment.s);
418
+		p_tmp_comment = NULL;
419
+
420
+		if (load_comments) {
421
+			tmp_comment.s = (char *)row->values[FCOL_COMMENT].val.string_val;
422
+			if (tmp_comment.s==NULL) tmp_comment.s="";
423
+			tmp_comment.len=strlen(tmp_comment.s);
424
+			p_tmp_comment = &tmp_comment;
425
+		}
426
+
421 427
 		if (add_failure_route(rd,
422 428
 				row->values[FCOL_CARRIER].val.int_val,
423 429
 				row->values[COL_DOMAIN].val.int_val,
... ...
@@ -427,7 +450,7 @@ int load_route_data_db(struct route_data_t * rd) {
427 427
 				row->values[FCOL_FLAGS].val.int_val,
428 428
 				row->values[FCOL_MASK].val.int_val,
429 429
 				row->values[FCOL_NEXT_DOMAIN].val.int_val,
430
-				&tmp_comment) == -1) {
430
+				p_tmp_comment) == -1) {
431 431
 			goto errout;
432 432
 		}
433 433
 	}
... ...
@@ -34,8 +34,6 @@
34 34
 #include "db_carrierroute.h"
35 35
 #include "cr_data.h"
36 36
 
37
-
38
-#define COLUMN_NUM 12
39 37
 #define COL_ID             0
40 38
 #define COL_CARRIER        1
41 39
 #define COL_DOMAIN         2
... ...
@@ -48,8 +46,9 @@
48 48
 #define COL_REWRITE_PREFIX 9
49 49
 #define COL_REWRITE_SUFFIX 10
50 50
 #define COL_COMMENT        11
51
+#define COLUMN_NUM 		   12
52
+#define COLUMN_NUM_NO_COMMENT COL_COMMENT
51 53
 
52
-#define FAILURE_COLUMN_NUM 10
53 54
 #define FCOL_ID             0
54 55
 #define FCOL_CARRIER        1
55 56
 #define FCOL_DOMAIN         2
... ...
@@ -60,6 +59,8 @@
60 60
 #define FCOL_MASK           7
61 61
 #define FCOL_NEXT_DOMAIN    8
62 62
 #define FCOL_COMMENT        9
63
+#define FAILURE_COLUMN_NUM 10
64
+#define FAILURE_COLUMN_NUM_NO_COMMENT FCOL_COMMENT
63 65
 
64 66
 #define CARRIER_NAME_COLUMN_NUM 2
65 67
 #define CARRIER_NAME_ID_COL 0
... ...
@@ -73,6 +74,7 @@ extern str * columns[];
73 73
 extern str * carrier_columns[];
74 74
 extern str * failure_columns[];
75 75
 
76
+void set_load_comments_params(int load_comments);
76 77
 
77 78
 /**
78 79
  * Loads the routing data from the database given in global
... ...
@@ -95,7 +95,7 @@ int add_route_rule(struct route_flags *rf, const str * prefix,
95 95
 		goto mem_error;
96 96
 	}
97 97
 
98
-	if (shm_str_dup(&shm_rr->comment, comment) != 0) {
98
+	if (comment && shm_str_dup(&shm_rr->comment, comment) != 0) {
99 99
 		goto mem_error;
100 100
 	}
101 101
 
... ...
@@ -311,7 +311,27 @@ modparam("carrierroute", "fetch_rows", 3000)
311 311
 </programlisting>
312 312
 		</example>
313 313
 	</section>
314
-
314
+<section>
315
+		<title><varname>db_load_description</varname> (integer)</title>
316
+		<para>
317
+		Toggle on/off loading in memory the description column in the 
318
+		carrierroute/carrierfailureroute database tables. This reduces the 
319
+		shared memory used by the module.
320
+		</para>
321
+		<para>
322
+		<emphasis>
323
+			Default value is <quote>1</quote>.
324
+		</emphasis>
325
+		</para>
326
+		<example>
327
+		<title>Unset <varname>db_load_description</varname> parameter</title>
328
+		<programlisting format="linespecific">
329
+...
330
+modparam("carrierroute", "db_load_description", 0)
331
+...
332
+</programlisting>
333
+		</example>
334
+	</section>
315 335
 	<section>
316 336
 		<title><varname>match_mode</varname> (integer)</title>
317 337
 		<para>