[sr-dev] git:master: modules/usrloc Added module parameter to work with db_cassandra

Anca Vamanu anca.vamanu at 1and1.ro
Tue Jan 17 10:35:39 CET 2012


Module: sip-router
Branch: master
Commit: d66b99030debbca3f52cc57c82bedc99e823e51c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d66b99030debbca3f52cc57c82bedc99e823e51c

Author: Anca Vamanu <anca.vamanu at 1and1.ro>
Committer: Anca Vamanu <anca.vamanu at 1and1.ro>
Date:   Tue Jan 17 11:09:11 2012 +0200

modules/usrloc Added module parameter to work with db_cassandra

	New module parameter db_update_as_insert. If set to 1 it will
	replace update operations with insert operations as required by the
	db_cassandra module.

---

 modules_k/usrloc/README               |   17 ++++++++++
 modules_k/usrloc/doc/usrloc_admin.xml |   22 +++++++++++++
 modules_k/usrloc/ucontact.c           |    9 +++++-
 modules_k/usrloc/ul_mod.c             |   53 +++++++++++++++++---------------
 modules_k/usrloc/ul_mod.h             |    1 +
 modules_k/usrloc/urecord.c            |    7 ++++-
 6 files changed, 82 insertions(+), 27 deletions(-)

diff --git a/modules_k/usrloc/README b/modules_k/usrloc/README
index abd7ef1..de6c441 100644
--- a/modules_k/usrloc/README
+++ b/modules_k/usrloc/README
@@ -61,6 +61,7 @@ Bogdan-Andrei Iancu
               3.23. fetch_rows (integer)
               3.24. hash_size (integer)
               3.25. preload (string)
+              3.26. db_update_as_insert (string)
 
         4. Functions
         5. MI Commands
@@ -130,6 +131,7 @@ Bogdan-Andrei Iancu
    1.23. Set fetch_rows parameter
    1.24. Set hash_size parameter
    1.25. Set preload parameter
+   1.26. Set db_update_as_insert parameter
 
 Chapter 1. Admin Guide
 
@@ -171,6 +173,7 @@ Chapter 1. Admin Guide
         3.23. fetch_rows (integer)
         3.24. hash_size (integer)
         3.25. preload (string)
+        3.26. db_update_as_insert (string)
 
    4. Functions
    5. MI Commands
@@ -272,6 +275,7 @@ Chapter 1. Admin Guide
    3.23. fetch_rows (integer)
    3.24. hash_size (integer)
    3.25. preload (string)
+   3.26. db_update_as_insert (string)
 
 3.1. nat_bflag (integer)
 
@@ -616,6 +620,19 @@ modparam("usrloc", "hash_size", 10)
 modparam("usrloc", "preload", "location")
 ...
 
+3.26. db_update_as_insert (string)
+
+   Set this parameter if you want to do INSERT DB operations instead of
+   UPDATE DB operations. It is recommended to set this parameter if you
+   use Cassandra as a DB backend.
+
+   Default value is “0”.
+
+   Example 1.26. Set db_update_as_insert parameter
+...
+modparam("usrloc", "db_update_as_insert", 1)
+...
+
 4. Functions
 
    There are no exported functions that could be used in scripts.
diff --git a/modules_k/usrloc/doc/usrloc_admin.xml b/modules_k/usrloc/doc/usrloc_admin.xml
index 7edf15b..2a6f640 100644
--- a/modules_k/usrloc/doc/usrloc_admin.xml
+++ b/modules_k/usrloc/doc/usrloc_admin.xml
@@ -713,6 +713,28 @@ modparam("usrloc", "preload", "location")
 		</example>
 	</section>
 
+	<section id="db_update_as_insert">
+		<title><varname>db_update_as_insert</varname> (string)</title>
+		<para>
+			Set this parameter if you want to do INSERT DB operations
+			instead of UPDATE DB operations. It is recommended to set this parameter
+			if you use Cassandra as a DB backend.
+		</para>
+		<para>
+		<emphasis>
+			Default value is <quote>0</quote>.
+		</emphasis>
+		</para>
+		<example>
+		<title>Set <varname>db_update_as_insert</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("usrloc", "db_update_as_insert", 1)
+...
+</programlisting>
+		</example>
+	</section>
+
 	</section>
 
 	<section>
diff --git a/modules_k/usrloc/ucontact.c b/modules_k/usrloc/ucontact.c
index 5e88a95..8e25d3f 100644
--- a/modules_k/usrloc/ucontact.c
+++ b/modules_k/usrloc/ucontact.c
@@ -792,6 +792,8 @@ static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c)
  */
 int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci)
 {
+	int res;
+
 	/* we have to update memory in any case, but database directly
 	 * only in db_mode 1 */
 	if (mem_update_ucontact( _c, _ci) < 0) {
@@ -812,7 +814,12 @@ int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci)
 	st_update_ucontact(_c);
 
 	if (db_mode == WRITE_THROUGH || db_mode==DB_ONLY) {
-		if (db_update_ucontact(_c) < 0) {
+		if (ul_db_update_as_insert)
+			res = db_insert_ucontact(_c);
+		else
+			res = db_update_ucontact(_c);
+
+		if (res < 0) {
 			LM_ERR("failed to update database\n");
 			return -1;
 		} else {
diff --git a/modules_k/usrloc/ul_mod.c b/modules_k/usrloc/ul_mod.c
index 17526f9..20fa67b 100644
--- a/modules_k/usrloc/ul_mod.c
+++ b/modules_k/usrloc/ul_mod.c
@@ -99,6 +99,8 @@ static int ul_preload_param(modparam_t type, void* val);
 
 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
  */
@@ -149,31 +151,32 @@ static cmd_export_t cmds[] = {
  * Exported parameters 
  */
 static param_export_t params[] = {
-	{"user_column",       STR_PARAM, &user_col.s      },
-	{"domain_column",     STR_PARAM, &domain_col.s    },
-	{"contact_column",    STR_PARAM, &contact_col.s   },
-	{"expires_column",    STR_PARAM, &expires_col.s   },
-	{"q_column",          STR_PARAM, &q_col.s         },
-	{"callid_column",     STR_PARAM, &callid_col.s    },
-	{"cseq_column",       STR_PARAM, &cseq_col.s      },
-	{"flags_column",      STR_PARAM, &flags_col.s     },
-	{"cflags_column",     STR_PARAM, &cflags_col.s    },
-	{"db_url",            STR_PARAM, &db_url.s        },
-	{"timer_interval",    INT_PARAM, &timer_interval  },
-	{"db_mode",           INT_PARAM, &db_mode         },
-	{"use_domain",        INT_PARAM, &use_domain      },
-	{"desc_time_order",   INT_PARAM, &desc_time_order },
-	{"user_agent_column", STR_PARAM, &user_agent_col.s},
-	{"received_column",   STR_PARAM, &received_col.s  },
-	{"path_column",       STR_PARAM, &path_col.s      },
-	{"socket_column",     STR_PARAM, &sock_col.s      },
-	{"methods_column",    STR_PARAM, &methods_col.s   },
-	{"matching_mode",     INT_PARAM, &matching_mode   },
-	{"cseq_delay",        INT_PARAM, &cseq_delay      },
-	{"fetch_rows",        INT_PARAM, &ul_fetch_rows   },
-	{"hash_size",         INT_PARAM, &ul_hash_size    },
-	{"nat_bflag",         INT_PARAM, &nat_bflag       },
-	{"preload",           STR_PARAM|USE_FUNC_PARAM, (void*)ul_preload_param},
+	{"user_column",         STR_PARAM, &user_col.s      },
+	{"domain_column",       STR_PARAM, &domain_col.s    },
+	{"contact_column",      STR_PARAM, &contact_col.s   },
+	{"expires_column",      STR_PARAM, &expires_col.s   },
+	{"q_column",            STR_PARAM, &q_col.s         },
+	{"callid_column",       STR_PARAM, &callid_col.s    },
+	{"cseq_column",         STR_PARAM, &cseq_col.s      },
+	{"flags_column",        STR_PARAM, &flags_col.s     },
+	{"cflags_column",       STR_PARAM, &cflags_col.s    },
+	{"db_url",              STR_PARAM, &db_url.s        },
+	{"timer_interval",      INT_PARAM, &timer_interval  },
+	{"db_mode",             INT_PARAM, &db_mode         },
+	{"use_domain",          INT_PARAM, &use_domain      },
+	{"desc_time_order",     INT_PARAM, &desc_time_order },
+	{"user_agent_column",   STR_PARAM, &user_agent_col.s},
+	{"received_column",     STR_PARAM, &received_col.s  },
+	{"path_column",         STR_PARAM, &path_col.s      },
+	{"socket_column",       STR_PARAM, &sock_col.s      },
+	{"methods_column",      STR_PARAM, &methods_col.s   },
+	{"matching_mode",       INT_PARAM, &matching_mode   },
+	{"cseq_delay",          INT_PARAM, &cseq_delay      },
+	{"fetch_rows",          INT_PARAM, &ul_fetch_rows   },
+	{"hash_size",           INT_PARAM, &ul_hash_size    },
+	{"nat_bflag",           INT_PARAM, &nat_bflag       },
+	{"preload",             STR_PARAM|USE_FUNC_PARAM, (void*)ul_preload_param},
+	{"db_update_as_insert", INT_PARAM, &ul_db_update_as_insert},
 	{0, 0, 0}
 };
 
diff --git a/modules_k/usrloc/ul_mod.h b/modules_k/usrloc/ul_mod.h
index 6ed97ca..85e4715 100644
--- a/modules_k/usrloc/ul_mod.h
+++ b/modules_k/usrloc/ul_mod.h
@@ -69,6 +69,7 @@ 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 db1_con_t* ul_dbh;   /* Database connection handle */
 extern db_func_t ul_dbf;
diff --git a/modules_k/usrloc/urecord.c b/modules_k/usrloc/urecord.c
index 270d17f..accd0b6 100644
--- a/modules_k/usrloc/urecord.c
+++ b/modules_k/usrloc/urecord.c
@@ -311,6 +311,7 @@ static inline void wb_timer(urecord_t* _r)
 	ucontact_t* ptr, *t;
 	cstate_t old_state;
 	int op;
+	int res;
 
 	ptr = _r->contacts;
 
@@ -354,7 +355,11 @@ static inline void wb_timer(urecord_t* _r)
 				break;
 
 			case 2: /* update */
-				if (db_update_ucontact(ptr) < 0) {
+				if (ul_db_update_as_insert)
+					res = db_insert_ucontact(ptr);
+				else
+					res = db_update_ucontact(ptr);
+				if (res < 0) {
 					LM_ERR("updating contact in db failed\n");
 					ptr->state = old_state;
 				}




More information about the sr-dev mailing list