Skip to content

Commit

Permalink
OPS-9: Add support for slow SQL query diagnosis
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Apr 12, 2019
1 parent b0fd150 commit 07f45f2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
27 changes: 27 additions & 0 deletions db/db.c
Expand Up @@ -51,6 +51,8 @@
#include "db_pool.h"
#include "db.h"

#include "db_insertq.h"

char *db_version_table = VERSION_TABLE;
char *db_default_url = NULL;
int db_max_async_connections = 10;
Expand All @@ -59,6 +61,31 @@ int db_max_async_connections = 10;
static unsigned int MAX_URL_LENGTH = 255;
#define COLUMN_OVERHEAD 256

stat_var *sql_total_queries;
stat_var *sql_slow_queries;

int init_db_support(void)
{
/* init query list now in shm
* so all processes that will be forked from now on
* will have access to it
*
* if it fails, give it a try and carry on */
if (init_ql_support() != 0) {
LM_ERR("failed to initialise buffering query list\n");
query_buffer_size = 0;
*query_list = NULL;
}

if (register_stat("sql", "sql_total_queries", &sql_total_queries, 0) ||
register_stat("sql", "sql_slow_queries", &sql_slow_queries, 0)) {
LM_ERR("failed to register SQL stats\n");
return -1;
}

return 0;
}

int estimate_available_rows(int payload_size, int column_count)
{
struct mem_info info;
Expand Down
6 changes: 6 additions & 0 deletions db/db.h
Expand Up @@ -48,6 +48,12 @@
#include "db_ps.h"
#include "../globals.h"

extern stat_var *sql_total_queries;
extern stat_var *sql_slow_queries;

/* to be called in the pre-fork phase */
int init_db_support(void);

/**
* \brief Specify table name that will be used for subsequent operations.
*
Expand Down
17 changes: 6 additions & 11 deletions main.c
Expand Up @@ -1396,6 +1396,12 @@ int main(int argc, char** argv)
goto error;
}

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

/* init modules */
if (init_modules() != 0) {
LM_ERR("error while initializing modules\n");
Expand All @@ -1420,17 +1426,6 @@ int main(int argc, char** argv)
goto error;
}

/* init query list now in shm
* so all processes that will be forked from now on
* will have access to it
*
* if it fails, give it a try and carry on */
if (init_ql_support() != 0) {
LM_ERR("failed to initialise buffering query list\n");
query_buffer_size = 0;
*query_list = NULL;
}

/* init multi processes support */
if (init_multi_proc_support()!=0) {
LM_ERR("failed to init multi-proc support\n");
Expand Down
13 changes: 9 additions & 4 deletions modules/db_mysql/dbase.c
Expand Up @@ -296,7 +296,9 @@ static int db_mysql_submit_query(const db_con_t* _h, const str* _s)
for (i=0; i<max_db_queries; i++) {
start_expire_timer(start,db_mysql_exec_query_threshold);
code = wrapper_single_mysql_real_query(_h, _s);
stop_expire_timer(start,db_mysql_exec_query_threshold,"mysql query",_s->s,_s->len,0);
_stop_expire_timer(start, db_mysql_exec_query_threshold, "mysql query",
_s->s, _s->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
if (code < 0) {
/* got disconnected during call */
switch_state_to_disconnected(_h);
Expand Down Expand Up @@ -718,7 +720,9 @@ static int db_mysql_do_prepared_query(const db_con_t* conn, const str *query,

start_expire_timer(start,db_mysql_exec_query_threshold);
code = wrapper_single_mysql_stmt_execute(conn, ctx->stmt);
stop_expire_timer(start,db_mysql_exec_query_threshold,"mysql prep stmt",query->s,query->len,0);
_stop_expire_timer(start, db_mysql_exec_query_threshold, "mysql prep stmt",
query->s, query->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
if (code < 0) {
/* got disconnected during call */
switch_state_to_disconnected(conn);
Expand Down Expand Up @@ -1161,8 +1165,9 @@ int db_mysql_async_raw_query(db_con_t *_h, const str *_s, void **_priv)
} else {
code = wrapper_single_mysql_real_query(_h, _s);
}
stop_expire_timer(start, db_mysql_exec_query_threshold,
"mysql async query", _s->s, _s->len, 0);
_stop_expire_timer(start, db_mysql_exec_query_threshold,
"mysql async query", _s->s, _s->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
if (code < 0) {
/* got disconnected during call */
switch_state_to_disconnected(_h);
Expand Down
13 changes: 9 additions & 4 deletions modules/db_postgres/dbase.c
Expand Up @@ -180,7 +180,9 @@ static int db_postgres_submit_query(const db_con_t* _con, const str* _s)
}
start_expire_timer(start,db_postgres_exec_query_threshold);
ret = PQsendQuery(CON_CONNECTION(_con), _s->s);
stop_expire_timer(start,db_postgres_exec_query_threshold,"pgsql query",_s->s,_s->len,0);
_stop_expire_timer(start, db_postgres_exec_query_threshold,
"pgsql query", _s->s, _s->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
/* exec the query */
if (ret) {
LM_DBG("%p PQsendQuery(%.*s)\n", _con, _s->len, _s->s);
Expand Down Expand Up @@ -274,7 +276,9 @@ static int db_postgres_submit_async_query(const db_con_t* _con, const str* _s)
}
start_expire_timer(start,db_postgres_exec_query_threshold);
ret = PQsendQuery(CON_CONNECTION(_con), _s->s);
stop_expire_timer(start,db_postgres_exec_query_threshold,"pgsql query",_s->s,_s->len,0);
_stop_expire_timer(start, db_postgres_exec_query_threshold,
"pgsql query", _s->s, _s->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
/* exec the query */
if (ret) {
LM_DBG("%p PQsendQuery(%.*s)\n", _con, _s->len, _s->s);
Expand Down Expand Up @@ -740,8 +744,9 @@ int db_postgres_async_raw_query(db_con_t *_h, const str *_s, void **_priv)
} else {
code = db_postgres_submit_query(_h, _s);
}
stop_expire_timer(start, db_postgres_exec_query_threshold,
"postgres async query", _s->s, _s->len, 0);
_stop_expire_timer(start, db_postgres_exec_query_threshold,
"postgres async query", _s->s, _s->len, 0, sql_slow_queries);
inc_stat(sql_total_queries);
if (code < 0) {
LM_ERR("failed to send postgres query %.*s",_s->len,_s->s);
goto out;
Expand Down

0 comments on commit 07f45f2

Please sign in to comment.