Skip to content

Commit

Permalink
usrloc: Add support for "cache DB" URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Mar 12, 2018
1 parent 6760e45 commit 962228b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cachedb/cachedb.h
Expand Up @@ -132,6 +132,8 @@ typedef struct cachedb_engines {
for this particular cachedb engine */
} cachedb_engine;

#include "cachedb_cap.h"

int register_cachedb(cachedb_engine* cde_entry);

/* functions to be used from script */
Expand Down
2 changes: 2 additions & 0 deletions modules/usrloc/udomain.c
Expand Up @@ -43,6 +43,8 @@
#include "../../socket_info.h"
#include "../../ut.h"
#include "../../hash_func.h"
#include "../../cachedb/cachedb.h"

#include "ul_mod.h" /* usrloc module parameters */
#include "utime.h"
#include "ureplication.h"
Expand Down
58 changes: 56 additions & 2 deletions modules/usrloc/ul_mod.c
Expand Up @@ -127,7 +127,8 @@ str attr_col = str_init(ATTR_COL); /*!< Name of column containing additi
str sip_instance_col = str_init(SIP_INSTANCE_COL);
str contactid_col = str_init(CONTACTID_COL);

str db_url = {NULL, 0}; /*!< Database URL */
str db_url = STR_NULL; /*!< Database URL */
str cdb_url = STR_NULL; /*!< Cache Database URL */
int timer_interval = 60; /*!< Timer interval in seconds */
enum usrloc_modes db_mode = NOT_SET; /*!< XXX: DEPRECATED: DB sync scheme */
char *runtime_preset;
Expand Down Expand Up @@ -160,6 +161,9 @@ int ul_replication_cluster = 0;
db_con_t* ul_dbh = 0; /* Database connection handle */
db_func_t ul_dbf;

cachedb_funcs cdbf;
cachedb_con *cdbc;


/*! \brief
* Exported functions
Expand All @@ -185,6 +189,7 @@ static param_export_t params[] = {
{"flags_column", STR_PARAM, &flags_col.s },
{"cflags_column", STR_PARAM, &cflags_col.s },
{"db_url", STR_PARAM, &db_url.s },
{"cachedb_url", STR_PARAM, &cdb_url.s },
{"timer_interval", INT_PARAM, &timer_interval },

/* runtime behavior selection */
Expand Down Expand Up @@ -256,9 +261,12 @@ static module_dependency_t *get_deps_wmode_preset(param_export_t *param)
{
char *haystack = (char *)param->param_pointer;

if (l_memmem(haystack, "sql-", strlen(haystack), 4))
if (l_memmem(haystack, "sql-", strlen(haystack), strlen("sql-")))
return alloc_module_dep(MOD_TYPE_SQLDB, NULL, DEP_ABORT);

if (l_memmem(haystack, "cachedb", strlen(haystack), strlen("cachedb")))
return alloc_module_dep(MOD_TYPE_CACHEDB, NULL, DEP_ABORT);

return NULL;
}

Expand Down Expand Up @@ -362,6 +370,22 @@ static int mod_init(void)
return -1;
}

if (have_cdb_conns()) {
cdb_url.len = strlen(cdb_url.s);

if (cachedb_bind_mod(&cdb_url, &cdbf) < 0) {
LM_ERR("cannot bind functions for cachedb_url %.*s\n",
cdb_url.len, cdb_url.s);
return -1;
}

if (!CACHEDB_CAPABILITY(&cdbf, CACHEDB_CAP_COL_ORIENTED)) {
LM_ERR("not enough capabilities for cachedb_url %.*s\n",
cdb_url.len, cdb_url.s);
return -1;
}
}

if (have_mem_storage()) {
cid_keys = pkg_malloc(max_contact_delete *
(sizeof(db_key_t) * sizeof(db_val_t)));
Expand Down Expand Up @@ -486,9 +510,30 @@ static void ul_rpc_data_load(int sender_id, void *unsused)
}
}

int init_cachedb(void)
{
if (!cdbf.init) {
LM_ERR("cachedb functions not initialized\n");
return -1;
}

cdbc = cdbf.init(&cdb_url);
if (!cdbc) {
LM_ERR("cannot connect to cachedb_url %.*s\n", cdb_url.len, cdb_url.s);
return -1;
}

LM_DBG("Init'ed cachedb\n");
return 0;
}

static int child_init(int _rank)
{
if (have_cdb_conns() && init_cachedb() < 0) {
LM_ERR("cannot init cachedb feature\n");
return -1;
}

if (!have_db_conns())
return 0;

Expand Down Expand Up @@ -540,6 +585,10 @@ static int mi_child_init(void)
*/
static void destroy(void)
{
if (cdbc)
cdbf.destroy(cdbc);
cdbc = NULL;

/* we need to sync DB in order to flush the cache */
if (ul_dbh) {
ul_unlock_locks();
Expand Down Expand Up @@ -734,6 +783,11 @@ int check_runtime_config(void)
LM_ERR("'contact_replication_cluster' is not set!\n");
return -1;
}

if (ZSTR(cdb_url)) {
LM_ERR("no cache database URL defined! ('cdb_url')\n");
return -1;
}
}

if (cluster_mode == CM_SQL_ONLY) {
Expand Down
4 changes: 4 additions & 0 deletions modules/usrloc/ul_mod.h
Expand Up @@ -34,6 +34,7 @@

#include "../../db/db.h"
#include "../../str.h"
#include "../../cachedb/cachedb.h"

extern enum ul_cluster_mode cluster_mode;
extern enum ul_rr_persist rr_persist;
Expand Down Expand Up @@ -94,6 +95,9 @@ extern int ul_hash_size;
extern db_con_t* ul_dbh; /* Database connection handle */
extern db_func_t ul_dbf;

extern cachedb_funcs cdbf;
extern cachedb_con *cdbc;

/*
* Matching algorithms
*/
Expand Down
3 changes: 3 additions & 0 deletions modules/usrloc/usrloc.h
Expand Up @@ -115,6 +115,9 @@ static inline int load_ul_api(usrloc_api_t *ul)
#define have_db_conns() \
(cluster_mode == CM_SQL_ONLY || \
rr_persist == RRP_LOAD_FROM_SQL)
#define have_cdb_conns() \
(cluster_mode == CM_EDGE_CACHEDB_ONLY || \
cluster_mode == CM_CORE_CACHEDB_ONLY)
#define have_mem_storage() \
(cluster_mode == CM_NONE || \
cluster_mode == CM_EDGE || \
Expand Down

0 comments on commit 962228b

Please sign in to comment.