convert sofia to use new core version of cache db handles

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@15600 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2009-11-21 18:57:15 +00:00
parent 506aae7cbe
commit af9e8eab35
5 changed files with 116 additions and 215 deletions

View File

@ -1988,8 +1988,9 @@ SWITCH_DECLARE(switch_status_t) _switch_cache_db_get_db_handle(switch_cache_db_h
switch_cache_db_handle_type_t type,
switch_cache_db_connection_options_t *connection_options,
const char *file, const char *func, int line);
#define switch_cache_db_get_db_handle(_a, _b) _switch_cache_db_get_db_handle(_a, _b, __FILE__, __SWITCH_FUNC__, __LINE__)
#define switch_cache_db_get_db_handle(_a, _b, _c) _switch_cache_db_get_db_handle(_a, _b, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
SWITCH_DECLARE(char *) switch_cache_db_execute_sql2str(switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err);
SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err);
SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql,
switch_core_db_callback_func_t callback, void *pdata, char **err);

View File

@ -476,7 +476,6 @@ struct sofia_profile {
sofia_gateway_t *gateways;
su_home_t *home;
switch_hash_t *chat_hash;
switch_hash_t *db_hash;
//switch_core_db_t *master_db;
switch_thread_rwlock_t *rwlock;
switch_mutex_t *flag_mutex;
@ -673,14 +672,6 @@ typedef struct {
char *route_uri;
} sofia_destination_t;
typedef struct {
switch_core_db_t *db;
switch_odbc_handle_t *odbc_dbh;
time_t last_used;
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
} sofia_cache_db_handle_t;
#define sofia_test_pflag(obj, flag) ((obj)->pflags[flag] ? 1 : 0)
#define sofia_set_pflag(obj, flag) (obj)->pflags[flag] = 1
#define sofia_set_pflag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
@ -785,6 +776,7 @@ void sofia_presence_handle_sip_i_subscribe(int status,
void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot);
void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
void sofia_sub_check_gateway(sofia_profile_t *profile, time_t now);

View File

@ -916,7 +916,7 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread
switch_mutex_lock(profile->ireg_mutex);
sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
//sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
char *sql = (char *) pop;
@ -941,8 +941,8 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread
}
//printf("TRANS:\n%s\n", sqlbuf);
sofia_glue_actually_execute_sql(profile, sqlbuf, NULL);
sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
sofia_glue_actually_execute_sql_trans(profile, sqlbuf, NULL);
//sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
switch_mutex_unlock(profile->ireg_mutex);
@ -963,7 +963,6 @@ void *SWITCH_THREAD_FUNC sofia_profile_worker_thread_run(switch_thread_t *thread
if (++ireg_loops >= IREG_SECONDS) {
time_t now = switch_epoch_time_now(NULL);
sofia_reg_check_expire(profile, now, 0);
sofia_glue_sql_close(profile, now);
ireg_loops = 0;
}
@ -1264,14 +1263,12 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
}
}
sofia_glue_sql_close(profile, 0);
su_home_unref(profile->home);
su_root_destroy(profile->s_root);
pool = profile->pool;
sofia_glue_del_profile(profile);
switch_core_hash_destroy(&profile->chat_hash);
switch_core_hash_destroy(&profile->db_hash);
switch_thread_rwlock_unlock(profile->rwlock);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name);
@ -2393,7 +2390,6 @@ switch_status_t config_sofia(int reload, char *profile_name)
profile->dbname = switch_core_strdup(profile->pool, url);
switch_core_hash_init(&profile->chat_hash, profile->pool);
switch_core_hash_init(&profile->db_hash, profile->pool);
switch_thread_rwlock_create(&profile->rwlock, profile->pool);
switch_mutex_init(&profile->flag_mutex, SWITCH_MUTEX_NESTED, profile->pool);
profile->dtmf_duration = 100;

View File

@ -4080,63 +4080,6 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
}
void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune)
{
switch_hash_index_t *hi;
const void *var;
void *val;
sofia_cache_db_handle_t *dbh = NULL;
int locked = 0;
char *key;
switch_mutex_lock(profile->ireg_mutex);
top:
locked = 0;
for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, &var, NULL, &val);
key = (char *) var;
if ((dbh = (sofia_cache_db_handle_t *) val)) {
time_t diff = 0;
if (prune > 0) {
if (prune > dbh->last_used) {
diff = (time_t) prune - dbh->last_used;
}
if (diff < SQL_CACHE_TIMEOUT) {
continue;
}
}
if (switch_mutex_trylock(dbh->mutex) == SWITCH_STATUS_SUCCESS) {
if (dbh->db) {
switch_core_db_close(dbh->db);
dbh->db = NULL;
} else if (switch_odbc_available() && dbh->odbc_dbh) {
switch_odbc_handle_destroy(&dbh->odbc_dbh);
}
switch_core_hash_delete(profile->db_hash, key);
switch_mutex_unlock(dbh->mutex);
switch_core_destroy_memory_pool(&dbh->pool);
goto top;
} else {
if (!prune) locked++;
continue;
}
}
}
if (locked) {
goto top;
}
switch_mutex_unlock(profile->ireg_mutex);
}
void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic)
{
switch_status_t status = SWITCH_STATUS_FALSE;
@ -4172,69 +4115,28 @@ void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t
}
void sofia_glue_release_db_handle(sofia_cache_db_handle_t **dbh)
switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
{
if (dbh && *dbh) {
switch_mutex_unlock((*dbh)->mutex);
*dbh = NULL;
}
}
sofia_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
{
switch_thread_id_t self = switch_thread_self();
char thread_str[256] = "";
sofia_cache_db_handle_t *dbh = NULL;
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
snprintf(thread_str, sizeof(thread_str) - 1, "%lu", (unsigned long)(intptr_t)self);
if (profile->odbc_dsn && profile->odbc_user && profile->odbc_pass) {
options.odbc_options.dsn = profile->odbc_dsn;
options.odbc_options.user = profile->odbc_user;
options.odbc_options.pass = profile->odbc_pass;
switch_mutex_lock(profile->ireg_mutex);
if (!(dbh = switch_core_hash_find(profile->db_hash, thread_str))) {
switch_memory_pool_t *pool = NULL;
switch_core_db_t *db = NULL;
switch_odbc_handle_t *odbc_dbh = NULL;
if (switch_odbc_available() && profile->odbc_dsn) {
if ((odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
switch_odbc_handle_destroy(&odbc_dbh);
}
}
} else {
db = switch_core_db_open_file(profile->dbname);
}
if (!db && !odbc_dbh) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure!\n");
goto end;
}
switch_core_new_memory_pool(&pool);
dbh = switch_core_alloc(pool, sizeof(*dbh));
dbh->pool = pool;
if (db) dbh->db = db; else dbh->odbc_dbh = odbc_dbh;
switch_mutex_init(&dbh->mutex, SWITCH_MUTEX_UNNESTED, dbh->pool);
switch_mutex_lock(dbh->mutex);
switch_core_hash_insert(profile->db_hash, thread_str, dbh);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
return dbh;
} else {
options.core_db_options.db_path = profile->dbname;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
return dbh;
}
end:
if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
switch_mutex_unlock(profile->ireg_mutex);
return dbh;
}
void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
{
sofia_cache_db_handle_t *dbh = NULL;
switch_cache_db_handle_t *dbh = NULL;
if (mutex) {
switch_mutex_lock(mutex);
@ -4245,34 +4147,35 @@ void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch
goto end;
}
if (switch_odbc_available() && dbh->odbc_dbh) {
switch_odbc_statement_handle_t stmt;
if (switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
char *err_str;
err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
if (!zstr(err_str)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, err_str);
}
switch_safe_free(err_str);
}
switch_odbc_statement_handle_free(&stmt);
} else if (profile->odbc_dsn) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
} else {
char *errmsg;
switch_core_db_exec(dbh->db, sql, NULL, NULL, &errmsg);
if (errmsg) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
switch_core_db_free(errmsg);
}
}
switch_cache_db_persistant_execute_trans(dbh, sql, 1);
end:
switch_cache_db_release_db_handle(&dbh);
sofia_glue_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
}
}
void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
{
switch_cache_db_handle_t *dbh = NULL;
if (mutex) {
switch_mutex_lock(mutex);
}
if (!(dbh = sofia_glue_get_db_handle(profile))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
switch_cache_db_execute_sql(dbh, sql, NULL);
end:
switch_cache_db_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
@ -4284,7 +4187,7 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
{
switch_bool_t ret = SWITCH_FALSE;
char *errmsg = NULL;
sofia_cache_db_handle_t *dbh = NULL;
switch_cache_db_handle_t *dbh = NULL;
if (mutex) {
switch_mutex_lock(mutex);
@ -4294,23 +4197,17 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
if (switch_odbc_available() && dbh->odbc_dbh) {
switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata, NULL);
} else if (profile->odbc_dsn) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
} else {
switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
if (errmsg) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
free(errmsg);
}
if (errmsg) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
free(errmsg);
}
end:
sofia_glue_release_db_handle(&dbh);
switch_cache_db_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
@ -4321,66 +4218,26 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
{
switch_core_db_stmt_t *stmt;
char *ret = NULL;
sofia_cache_db_handle_t *dbh = NULL;
switch_cache_db_handle_t *dbh = NULL;
if (!(dbh = sofia_glue_get_db_handle(profile))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
return NULL;
}
if (mutex) {
switch_mutex_lock(mutex);
}
if (switch_odbc_available() && dbh->odbc_dbh) {
if (switch_odbc_handle_exec_string(dbh->odbc_dbh, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
ret = resbuf;
}
goto end;
}
if (switch_core_db_prepare(dbh->db, sql, -1, &stmt, 0)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
goto end;
} else {
int running = 1;
int colcount;
while (running < 5000) {
int result = switch_core_db_step(stmt);
const unsigned char *txt;
if (result == SWITCH_CORE_DB_ROW) {
if ((colcount = switch_core_db_column_count(stmt)) > 0) {
if ((txt = switch_core_db_column_text(stmt, 0))) {
switch_copy_string(resbuf, (char *) txt, len);
ret = resbuf;
} else {
goto end;
}
}
break;
} else if (result == SWITCH_CORE_DB_BUSY) {
running++;
switch_cond_next();
continue;
}
break;
}
switch_core_db_finalize(stmt);
}
end:
ret = switch_cache_db_execute_sql2str(dbh, sql, resbuf, len, NULL);
if (mutex) {
switch_mutex_unlock(mutex);
}
sofia_glue_release_db_handle(&dbh);
switch_cache_db_release_db_handle(&dbh);
return ret;
}

View File

@ -314,8 +314,6 @@ static switch_status_t switch_cache_db_execute_sql_real(switch_cache_db_handle_t
if (err) *err = NULL;
switch (dbh->type) {
case SCDB_TYPE_ODBC:
{
@ -428,6 +426,63 @@ SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_hand
}
SWITCH_DECLARE(char *) switch_cache_db_execute_sql2str(switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err)
{
switch_status_t status = SWITCH_STATUS_FALSE;
switch (dbh->type) {
case SCDB_TYPE_CORE_DB:
{
switch_core_db_stmt_t *stmt;
if (switch_core_db_prepare(dbh->native_handle.core_db_dbh, sql, -1, &stmt, 0)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
return NULL;
} else {
int running = 1;
int colcount;
while (running < 5000) {
int result = switch_core_db_step(stmt);
const unsigned char *txt;
if (result == SWITCH_CORE_DB_ROW) {
if ((colcount = switch_core_db_column_count(stmt)) > 0) {
if ((txt = switch_core_db_column_text(stmt, 0))) {
switch_copy_string(str, (char *) txt, len);
status = SWITCH_STATUS_SUCCESS;
} else {
goto end;
}
}
break;
} else if (result == SWITCH_CORE_DB_BUSY) {
running++;
switch_cond_next();
continue;
}
break;
}
switch_core_db_finalize(stmt);
}
}
break;
case SCDB_TYPE_ODBC:
{
status = switch_odbc_handle_exec_string(dbh->native_handle.odbc_dbh, sql, str, len);
}
break;
}
end:
return status == SWITCH_STATUS_SUCCESS ? str : NULL;
}
SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, const char *sql, uint32_t retries)
{
char *errmsg = NULL;