Skip to content

Commit

Permalink
OPS-9: Add support for NoSQL query diagnosis
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Apr 12, 2019
1 parent 07f45f2 commit 94d3d31
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 221 deletions.
22 changes: 15 additions & 7 deletions cachedb/cachedb.c
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 OpenSIPS Solutions
* Copyright (C) 2011-2019 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand All @@ -16,14 +16,8 @@
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* history:
* ---------
* 2011-09-xx created (vlad-paiu)
*/


#include "cachedb.h"
#include "cachedb_cap.h"
#include "../dprint.h"
Expand All @@ -36,12 +30,26 @@
#include <string.h>
#include <stdlib.h>

stat_var *cdb_total_queries;
stat_var *cdb_slow_queries;

struct cachedb_engine_t
{
cachedb_engine cde;
struct cachedb_engine_t* next;
};

int init_cdb_support(void)
{
if (register_stat("cdb", "cdb_total_queries", &cdb_total_queries, 0) ||
register_stat("cdb", "cdb_slow_queries", &cdb_slow_queries, 0)) {
LM_ERR("failed to register CacheDB stats\n");
return -1;
}

return 0;
}

int cachedb_store_url(struct cachedb_url **list,char *val)
{
struct cachedb_url *new,*it;
Expand Down
13 changes: 6 additions & 7 deletions cachedb/cachedb.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 OpenSIPS Solutions
* Copyright (C) 2011-2019 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand All @@ -16,14 +16,8 @@
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* history:
* ---------
* 2011-09-xx created (vlad-paiu)
*/


#ifndef _CACHEDB_H
#define _CACHEDB_H

Expand All @@ -48,6 +42,11 @@ typedef struct {
} val;
} cdb_raw_entry;

extern stat_var *cdb_total_queries;
extern stat_var *cdb_slow_queries;

int init_cdb_support(void);

int cachedb_store_url(struct cachedb_url **list,char *val);
void cachedb_free_url(struct cachedb_url *list);

Expand Down
9 changes: 8 additions & 1 deletion main.c
Expand Up @@ -139,6 +139,7 @@
#include "version.h"
#include "mi/mi_core.h"
#include "db/db_insertq.h"
#include "cachedb/cachedb.h"
#include "net/trans.h"

#include "test/unit_tests.h"
Expand Down Expand Up @@ -1398,7 +1399,13 @@ int main(int argc, char** argv)

/* init SQL DB support */
if (init_db_support() != 0) {
LM_ERR("failed to initialise SQL database support\n");
LM_ERR("failed to initialize SQL database support\n");
goto error;
}

/* init CacheDB support */
if (init_cdb_support() != 0) {
LM_ERR("failed to initialize CacheDB support\n");
goto error;
}

Expand Down
4 changes: 2 additions & 2 deletions modules/cachedb_cassandra/cachedb_cassandra_dbase.c
Expand Up @@ -299,8 +299,8 @@ const CassResult *execute_query(CassSession* session, CassStatement *statement,

start_expire_timer(start, cassandra_exec_threshold);
rc = cass_future_wait_timed(query_future, 1000*cassandra_query_timeout);
stop_expire_timer(start, cassandra_exec_threshold, op_name,
NULL, 0, 0);
_stop_expire_timer(start, cassandra_exec_threshold, op_name,
NULL, 0, 0, cdb_slow_queries, cdb_total_queries);
if (rc == 0) {
LM_ERR("Cassandra query took too long\n");
goto error;
Expand Down
125 changes: 75 additions & 50 deletions modules/cachedb_couchbase/cachedb_couchbase_dbase.c
Expand Up @@ -440,8 +440,9 @@ int couchbase_set(cachedb_con *connection,str *attr,
LM_ERR("Set request failed - %s\n", lcb_strerror(instance, oprc));
//Attempt reconnect
if(couchbase_conditional_reconnect(connection, oprc) != 1) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

Expand All @@ -451,15 +452,17 @@ int couchbase_set(cachedb_con *connection,str *attr,

if (oprc != LCB_SUCCESS) {
LM_ERR("Set command retry failed - %s\n", lcb_strerror(instance, oprc));
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}
LM_ERR("Set command successfully retried\n");
}
LM_DBG("Successfully stored\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase set",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return 1;
}

Expand All @@ -481,15 +484,17 @@ int couchbase_remove(cachedb_con *connection,str *attr)

if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}

LM_ERR("Failed to send the remove query - %s\n", lcb_strerror(instance, oprc));
if (couchbase_conditional_reconnect(connection, oprc) != 1) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
};

Expand All @@ -499,21 +504,24 @@ int couchbase_remove(cachedb_con *connection,str *attr)
if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
LM_ERR("Remove command successfully retried\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}
LM_ERR("Remove command retry failed - %s\n", lcb_strerror(instance, oprc));
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}
LM_ERR("Remove command successfully retried\n");
}

LM_DBG("Successfully removed\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase remove",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return 1;
}

Expand All @@ -537,15 +545,17 @@ int couchbase_get(cachedb_con *connection,str *attr,str *val)
if (oprc != LCB_SUCCESS) {
/* Key not present, record does not exist */
if (oprc == LCB_KEY_ENOENT) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}

//Attempt reconnect
if (couchbase_conditional_reconnect(connection, oprc) != 1) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

Expand All @@ -555,27 +565,31 @@ int couchbase_get(cachedb_con *connection,str *attr,str *val)
if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
LM_ERR("Get command successfully retried\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}
LM_ERR("Get command retry failed - %s\n", lcb_strerror(instance, oprc));
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}
LM_ERR("Get command successfully retried\n");
}

//Incase of malloc failure
if (!get_res.s) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
*val = get_res;
return 1;
}
Expand Down Expand Up @@ -604,15 +618,17 @@ int couchbase_add(cachedb_con *connection,str *attr,int val,int expires,int *new
if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
return -1;
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
}

LM_ERR("Failed to send the arithmetic query - %s\n", lcb_strerror(instance, oprc));
//Attempt reconnect
if (couchbase_conditional_reconnect(connection, oprc) != 1) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

Expand All @@ -623,13 +639,15 @@ int couchbase_add(cachedb_con *connection,str *attr,int val,int expires,int *new
if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
LM_ERR("Arithmetic command successfully retried\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}
LM_ERR("Arithmetic command retry failed - %s\n", lcb_strerror(instance, oprc));
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}
LM_ERR("Arithmetic command successfully retried\n");
Expand All @@ -638,8 +656,9 @@ int couchbase_add(cachedb_con *connection,str *attr,int val,int expires,int *new
if (new_val)
*new_val = arithmetic_res;

stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase add",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return 1;
}

Expand Down Expand Up @@ -668,15 +687,17 @@ int couchbase_get_counter(cachedb_con *connection,str *attr,int *val)
if (oprc != LCB_SUCCESS) {
/* Key not present, record does not exist */
if (oprc == LCB_KEY_ENOENT) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}

//Attempt reconnect
if (couchbase_conditional_reconnect(connection, oprc) != 1) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter ",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter ",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

Expand All @@ -686,27 +707,31 @@ int couchbase_get_counter(cachedb_con *connection,str *attr,int *val)
if (oprc != LCB_SUCCESS) {
if (oprc == LCB_KEY_ENOENT) {
LM_ERR("Get counter command successfully retried\n");
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -1;
}
LM_ERR("Get counter command retry failed - %s\n", lcb_strerror(instance, oprc));
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}
LM_ERR("Get command successfully retried\n");
}

//Incase of malloc failure
if (!get_res.s) {
stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);
return -2;
}

stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0);
_stop_expire_timer(start,couch_exec_threshold,
"cachedb_couchbase get counter",attr->s,attr->len,0,
cdb_slow_queries, cdb_total_queries);

if (str2sint((str *)&get_res,val)) {
LM_ERR("Failued to convert counter [%.*s] to int\n",get_res.len,get_res.s);
Expand Down

0 comments on commit 94d3d31

Please sign in to comment.