FSCORE-668

This commit is contained in:
Anthony Minessale 2010-09-07 10:51:02 -05:00
parent e3eff8165e
commit 68d1c32ad1
1 changed files with 29 additions and 20 deletions

View File

@ -35,6 +35,8 @@
#include <switch.h> #include <switch.h>
#include "private/switch_core_pvt.h" #include "private/switch_core_pvt.h"
#define SQLLEN 32768
static struct { static struct {
switch_cache_db_handle_t *event_db; switch_cache_db_handle_t *event_db;
switch_queue_t *sql_queue[2]; switch_queue_t *sql_queue[2];
@ -537,7 +539,7 @@ SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_hand
switch (dbh->type) { switch (dbh->type) {
default: default:
{ {
status = switch_cache_db_execute_sql_chunked(dbh, (char *) sql, 32768, err); status = switch_cache_db_execute_sql_chunked(dbh, (char *) sql, SQLLEN, err);
} }
break; break;
} }
@ -845,7 +847,7 @@ SWITCH_DECLARE(switch_bool_t) switch_cache_db_test_reactive(switch_cache_db_hand
#define SQLLEN 1024 * 1024
static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread, void *obj) static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread, void *obj)
{ {
void *pop; void *pop;
@ -853,13 +855,14 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread,
uint8_t trans = 0, nothing_in_queue = 0; uint8_t trans = 0, nothing_in_queue = 0;
uint32_t target = 100000; uint32_t target = 100000;
switch_size_t len = 0, sql_len = SQLLEN; switch_size_t len = 0, sql_len = SQLLEN;
char *tmp, *sqlbuf = (char *) malloc(sql_len); char *sqlbuf = (char *) malloc(sql_len);
char *sql; char *sql = NULL;
switch_size_t newlen; switch_size_t newlen;
int lc = 0; int lc = 0;
uint32_t loops = 0, sec = 0; uint32_t loops = 0, sec = 0;
uint32_t l1 = 1000; uint32_t l1 = 1000;
uint32_t sanity = 120; uint32_t sanity = 120;
int item_remained = 0;
switch_assert(sqlbuf); switch_assert(sqlbuf);
@ -897,10 +900,17 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread,
continue; continue;
} }
if (switch_queue_trypop(sql_manager.sql_queue[0], &pop) == SWITCH_STATUS_SUCCESS || //printf("SIZE %d %d\n", switch_queue_size(sql_manager.sql_queue[0]), switch_queue_size(sql_manager.sql_queue[1]));
switch_queue_trypop(sql_manager.sql_queue[1], &pop) == SWITCH_STATUS_SUCCESS) {
sql = (char *) pop;
if (item_remained || switch_queue_trypop(sql_manager.sql_queue[0], &pop) == SWITCH_STATUS_SUCCESS ||
switch_queue_trypop(sql_manager.sql_queue[1], &pop) == SWITCH_STATUS_SUCCESS) {
if (item_remained) {
item_remained = 0;
} else {
sql = (char *) pop;
}
if (sql) { if (sql) {
newlen = strlen(sql) + 2; newlen = strlen(sql) + 2;
@ -911,20 +921,18 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread,
/* ignore abnormally large strings sql strings as potential buffer overflow */ /* ignore abnormally large strings sql strings as potential buffer overflow */
if (newlen < SQLLEN) { if (newlen < SQLLEN) {
itterations++; itterations++;
if (len + newlen > sql_len) {
sql_len = len + SQLLEN; if (len + newlen < sql_len) {
if (!(tmp = realloc(sqlbuf, sql_len))) { sprintf(sqlbuf + len, "%s;\n", sql);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL thread ending on mem err\n"); len += newlen;
abort(); } else {
break; item_remained = 1;
}
sqlbuf = tmp;
} }
sprintf(sqlbuf + len, "%s;\n", sql);
len += newlen;
} }
free(sql);
if (!item_remained) {
free(sql);
}
} else { } else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "SQL thread ending\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "SQL thread ending\n");
break; break;
@ -934,7 +942,8 @@ static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t *thread,
} }
if (trans && ((itterations == target) || (nothing_in_queue && ++lc >= 500))) { if ((item_remained || (trans && ((itterations == target) || (nothing_in_queue && ++lc >= 500)))) &&
(sql_manager.event_db->native_handle.core_db_dbh)) {
if (switch_cache_db_persistant_execute_trans(sql_manager.event_db, sqlbuf, 1) != SWITCH_STATUS_SUCCESS) { if (switch_cache_db_persistant_execute_trans(sql_manager.event_db, sqlbuf, 1) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL thread unable to commit transaction, records lost!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SQL thread unable to commit transaction, records lost!\n");
} }