[asterisk-commits] juggie: branch group/NoLossCDR-Redux2 r104130 - in /team/group/NoLossCDR-Redu...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Feb 26 09:34:06 CST 2008
Author: juggie
Date: Tue Feb 26 09:34:06 2008
New Revision: 104130
URL: http://svn.digium.com/view/asterisk?view=rev&rev=104130
Log:
initial bring me back to life commit
Modified:
team/group/NoLossCDR-Redux2/cdr/cdr_adaptive_odbc.c
team/group/NoLossCDR-Redux2/cdr/cdr_csv.c
team/group/NoLossCDR-Redux2/cdr/cdr_custom.c
team/group/NoLossCDR-Redux2/cdr/cdr_manager.c
team/group/NoLossCDR-Redux2/cdr/cdr_odbc.c
team/group/NoLossCDR-Redux2/cdr/cdr_pgsql.c
team/group/NoLossCDR-Redux2/cdr/cdr_sqlite3_custom.c
team/group/NoLossCDR-Redux2/include/asterisk/cdr.h
team/group/NoLossCDR-Redux2/main/cdr.c
team/group/NoLossCDR-Redux2/res/res_config_sqlite.c
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_adaptive_odbc.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_adaptive_odbc.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_adaptive_odbc.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_adaptive_odbc.c Tue Feb 26 09:34:06 2008
@@ -323,7 +323,7 @@
} \
} while (0)
-static int odbc_log(struct ast_cdr *cdr)
+static int odbc_log(struct ast_cdr *cdr, void *data)
{
struct tables *tableptr;
struct columns *entry;
@@ -627,10 +627,10 @@
static int unload_module(void)
{
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
usleep(1);
if (AST_RWLIST_WRLOCK(&odbc_tables)) {
- ast_cdr_register(name, ast_module_info->description, odbc_log);
+ ast_cdr_register(name, NULL, ast_module_info->description, odbc_log, NULL, NULL);
ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
return -1;
}
@@ -649,7 +649,7 @@
load_config();
AST_RWLIST_UNLOCK(&odbc_tables);
- ast_cdr_register(name, ast_module_info->description, odbc_log);
+ ast_cdr_register(name, NULL, ast_module_info->description, odbc_log, NULL, NULL);
return 0;
}
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_csv.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_csv.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_csv.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_csv.c Tue Feb 26 09:34:06 2008
@@ -275,7 +275,7 @@
}
-static int csv_log(struct ast_cdr *cdr)
+static int csv_log(struct ast_cdr *cdr, void *data)
{
FILE *mf = NULL;
/* Make sure we have a big enough buf */
@@ -315,7 +315,7 @@
static int unload_module(void)
{
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
return 0;
}
@@ -326,7 +326,7 @@
if(!load_config(0))
return AST_MODULE_LOAD_DECLINE;
- if ((res = ast_cdr_register(name, ast_module_info->description, csv_log)))
+ if ((res = ast_cdr_register(name, NULL, ast_module_info->description, csv_log, NULL, NULL)))
ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
return res;
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_custom.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_custom.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_custom.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_custom.c Tue Feb 26 09:34:06 2008
@@ -102,7 +102,7 @@
-static int custom_log(struct ast_cdr *cdr)
+static int custom_log(struct ast_cdr *cdr, void *data)
{
/* Make sure we have a big enough buf */
char buf[2048];
@@ -137,7 +137,7 @@
{
if (mf)
fclose(mf);
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
return 0;
}
@@ -146,7 +146,7 @@
int res = 0;
if (!load_config(0)) {
- res = ast_cdr_register(name, ast_module_info->description, custom_log);
+ res = ast_cdr_register(name, NULL, ast_module_info->description, custom_log, NULL, NULL);
if (res)
ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
if (mf)
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_manager.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_manager.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_manager.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_manager.c Tue Feb 26 09:34:06 2008
@@ -48,7 +48,7 @@
static int enablecdr = 0;
struct ast_str *customfields;
-static int manager_log(struct ast_cdr *cdr);
+static int manager_log(struct ast_cdr *cdr, void *data);
static int load_config(int reload)
{
@@ -71,7 +71,7 @@
/* Standard configuration */
ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
if (enablecdr)
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
enablecdr = 0;
return 0;
}
@@ -107,15 +107,15 @@
ast_config_destroy(cfg);
if (enablecdr && !newenablecdr)
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
else if (!enablecdr && newenablecdr)
- ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
+ ast_cdr_register(name, NULL, "Asterisk Manager Interface CDR Backend", manager_log, NULL, NULL);
enablecdr = newenablecdr;
return 1;
}
-static int manager_log(struct ast_cdr *cdr)
+static int manager_log(struct ast_cdr *cdr, void *data)
{
struct ast_tm timeresult;
char strStartTime[80] = "";
@@ -176,7 +176,7 @@
static int unload_module(void)
{
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
if (customfields)
ast_free(customfields);
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_odbc.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_odbc.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_odbc.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_odbc.c Tue Feb 26 09:34:06 2008
@@ -124,7 +124,7 @@
}
-static int odbc_log(struct ast_cdr *cdr)
+static int odbc_log(struct ast_cdr *cdr, void *data)
{
struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
SQLHSTMT stmt;
@@ -220,7 +220,7 @@
ast_verb(3, "cdr_odbc: dsn is %s\n", dsn);
ast_verb(3, "cdr_odbc: table is %s\n", table);
- res = ast_cdr_register(name, ast_module_info->description, odbc_log);
+ res = ast_cdr_register(name, NULL, ast_module_info->description, odbc_log, NULL, NULL);
if (res) {
ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
}
@@ -238,7 +238,7 @@
static int unload_module(void)
{
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
if (dsn) {
ast_verb(11, "cdr_odbc: free dsn\n");
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_pgsql.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_pgsql.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_pgsql.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_pgsql.c Tue Feb 26 09:34:06 2008
@@ -104,7 +104,7 @@
} \
} while (0)
-static int pgsql_log(struct ast_cdr *cdr)
+static int pgsql_log(struct ast_cdr *cdr, void *data)
{
struct ast_tm tm;
char *pgerror;
@@ -308,7 +308,7 @@
static int unload_module(void)
{
struct columns *cur;
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
/* Give all threads time to finish */
usleep(1);
@@ -486,7 +486,7 @@
ast_config_destroy(cfg);
- return ast_cdr_register(name, ast_module_info->description, pgsql_log);
+ return ast_cdr_register(name, NULL, ast_module_info->description, pgsql_log, NULL, NULL);
}
static int load_module(void)
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_sqlite3_custom.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_sqlite3_custom.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_sqlite3_custom.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_sqlite3_custom.c Tue Feb 26 09:34:06 2008
@@ -238,7 +238,7 @@
return 0;
}
-static int sqlite3_log(struct ast_cdr *cdr)
+static int sqlite3_log(struct ast_cdr *cdr, void *data)
{
int res = 0;
char *error = NULL;
@@ -294,7 +294,7 @@
{
free_config();
- ast_cdr_unregister(name);
+ ast_cdr_unregister(name, NULL);
return 0;
}
@@ -307,7 +307,7 @@
char *sql;
if (!load_config(0)) {
- res = ast_cdr_register(name, desc, sqlite3_log);
+ res = ast_cdr_register(name, NULL, desc, sqlite3_log, NULL, NULL);
if (res) {
ast_log(LOG_ERROR, "Unable to register custom SQLite3 CDR handling\n");
free_config();
Modified: team/group/NoLossCDR-Redux2/include/asterisk/cdr.h
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/include/asterisk/cdr.h?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/include/asterisk/cdr.h (original)
+++ team/group/NoLossCDR-Redux2/include/asterisk/cdr.h Tue Feb 26 09:34:06 2008
@@ -48,6 +48,11 @@
#define AST_MAX_USER_FIELD 256
#define AST_MAX_ACCOUNT_CODE 20
+
+/* CDR Return Flags */
+#define AST_CDR_POST_OK 0
+#define AST_CDR_POST_FAILED -1
+#define AST_CDR_POST_DISABLED -2
/* Include channel.h after relevant declarations it will need */
#include "asterisk/channel.h"
@@ -97,6 +102,11 @@
/*! A linked list for variables */
struct varshead varshead;
+ /* count where i'm used in memory */
+ int usecount;
+ /* no more cdr stack struct ast_cdr *next; */
+ AST_RWLIST_ENTRY(ast_cdr) list;
+
struct ast_cdr *next;
};
@@ -107,7 +117,8 @@
int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr);
int ast_cdr_log_unanswered(void);
-typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
+typedef int (*ast_cdrbe)(struct ast_cdr *cdr, void *data);
+typedef void (*ast_cdrbe_cleanup)(void *data);
/*! \brief Return TRUE if CDR subsystem is enabled */
int check_cdr_enabled(void);
@@ -163,18 +174,21 @@
* \param name name associated with the particular CDR handler
* \param desc description of the CDR handler
* \param be function pointer to a CDR handler
+ * \param cleanup function pointer to a CDR sink cleanup handler
* Used to register a Call Detail Record handler.
* \retval 0 on success.
* \retval -1 on error
*/
-int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be);
+int ast_cdr_register(const char *name, const char *name_detail, const char *desc, ast_cdrbe be, ast_cdrbe_cleanup cleanup, void *be_data);
/*!
* \brief Unregister a CDR handling engine
* \param name name of CDR handler to unregister
+ * \param name_detail name of CDR handler sink to unregister, or
+ * NULL for all sinks for the specfied handler
* Unregisters a CDR by it's name
*/
-void ast_cdr_unregister(const char *name);
+void ast_cdr_unregister(const char *name, const char *name_detail);
/*!
* \brief Start a call
Modified: team/group/NoLossCDR-Redux2/main/cdr.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/main/cdr.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/main/cdr.c (original)
+++ team/group/NoLossCDR-Redux2/main/cdr.c Tue Feb 26 09:34:06 2008
@@ -49,6 +49,9 @@
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/stringfields.h"
+#include "asterisk/module.h"
+
+static void *do_cdr(void *data);
/*! Default AMA flag for billing records (CDR's) */
int ast_default_amaflags = AST_CDR_DOCUMENTATION;
@@ -56,46 +59,32 @@
struct ast_cdr_beitem {
char name[20];
+ char name_detail[20];
char desc[80];
ast_cdrbe be;
+ ast_cdrbe_cleanup cleanup;
+ void *be_data;
+ int cancel_thread;
+ int waiting_cdrs;
+ pthread_t cdr_thread;
+ ast_cond_t cdr_pending_cond;
+ ast_cond_t cdr_retry_cond;
+ ast_mutex_t retry_poll_lock;
+ AST_LIST_HEAD(, ast_cdr) cdr_queue;
AST_RWLIST_ENTRY(ast_cdr_beitem) list;
};
static AST_RWLIST_HEAD_STATIC(be_list, ast_cdr_beitem);
-struct ast_cdr_batch_item {
- struct ast_cdr *cdr;
- struct ast_cdr_batch_item *next;
-};
-
-static struct ast_cdr_batch {
- int size;
- struct ast_cdr_batch_item *head;
- struct ast_cdr_batch_item *tail;
-} *batch = NULL;
-
-static struct sched_context *sched;
-static int cdr_sched = -1;
-static pthread_t cdr_thread = AST_PTHREADT_NULL;
-
-#define BATCH_SIZE_DEFAULT 100
-#define BATCH_TIME_DEFAULT 300
-#define BATCH_SCHEDULER_ONLY_DEFAULT 0
-#define BATCH_SAFE_SHUTDOWN_DEFAULT 1
+#define SAFE_SHUTDOWN_DEFAULT 1
+#define MAX_QUEUE_SIZE_DEFAULT 1000
+#define RETRY_DELAY_DEFAULT 30
static int enabled; /*! Is the CDR subsystem enabled ? */
+static int safeshutdown;
+static int queuesize;
+static int retrydelay;
static int unanswered;
-static int batchmode;
-static int batchsize;
-static int batchtime;
-static int batchscheduleronly;
-static int batchsafeshutdown;
-
-AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
-
-/* these are used to wake up the CDR thread when there's work to do */
-AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
-static ast_cond_t cdr_pending_cond;
int check_cdr_enabled()
{
@@ -110,11 +99,11 @@
/*! Register a CDR driver. Each registered CDR driver generates a CDR
\return 0 on success, -1 on failure
*/
-int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
+int ast_cdr_register(const char *name, const char *name_detail, const char *desc, ast_cdrbe be, ast_cdrbe_cleanup cleanup, void *be_data)
{
struct ast_cdr_beitem *i = NULL;
- if (!name)
+ if (!name || !desc)
return -1;
if (!be) {
@@ -124,20 +113,32 @@
AST_RWLIST_WRLOCK(&be_list);
AST_RWLIST_TRAVERSE(&be_list, i, list) {
- if (!strcasecmp(name, i->name)) {
- ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
+ if (!strcasecmp(name, i->name) && !strcasecmp(name_detail, i->name_detail)) {
+ ast_log(LOG_WARNING, "Already have a CDR backend called '%s - %s'\n", name, name_detail);
AST_RWLIST_UNLOCK(&be_list);
return -1;
}
}
- if (!(i = ast_calloc(1, sizeof(*i))))
+ if (!(i = ast_calloc(1, sizeof(struct ast_cdr_beitem))))
return -1;
i->be = be;
+ i->cleanup = cleanup;
+ i->cancel_thread = 0;
+ i->waiting_cdrs = 0;
ast_copy_string(i->name, name, sizeof(i->name));
+ if (name_detail)
+ ast_copy_string(i->name_detail, name_detail, sizeof(i->name_detail));
+ else
+ ast_copy_string(i->name_detail, "", sizeof(i->name_detail));
+
ast_copy_string(i->desc, desc, sizeof(i->desc));
-
+ i->be_data = be_data;
+ ast_cond_init(&i->cdr_pending_cond, NULL);
+ ast_cond_init(&i->cdr_retry_cond, NULL);
+ if (ast_pthread_create_background(&i->cdr_thread, NULL, do_cdr, i) < 0)
+ ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
AST_RWLIST_INSERT_HEAD(&be_list, i, list);
AST_RWLIST_UNLOCK(&be_list);
@@ -145,17 +146,29 @@
}
/*! unregister a CDR driver */
-void ast_cdr_unregister(const char *name)
-{
- struct ast_cdr_beitem *i = NULL;
+void ast_cdr_unregister(const char *name, const char *name_detail)
+{
+ struct ast_cdr_beitem *item = NULL;
AST_RWLIST_WRLOCK(&be_list);
- AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
- if (!strcasecmp(name, i->name)) {
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, item, list) {
+ if (!strcasecmp(name, item->name) && (!name_detail || !strcasecmp(name_detail, item->name_detail))) {
+ item->cancel_thread = 1;
+ /* signal the thread so it can exit */
+ ast_cond_signal(&item->cdr_pending_cond);
+ /* wait for thread to exit so we can clean up */
+ pthread_join(item->cdr_thread, NULL);
+ item->cdr_thread = AST_PTHREADT_NULL;
+ ast_cond_destroy(&item->cdr_pending_cond);
+ ast_cond_destroy(&item->cdr_retry_cond);
AST_RWLIST_REMOVE_CURRENT(list);
- ast_verb(2, "Unregistered '%s' CDR backend\n", name);
- ast_free(i);
- break;
+ if (option_verbose > 1)
+ ast_verbose(VERBOSE_PREFIX_2 "Unregistered '%s - %s' CDR backend\n", item->name, item->name_detail);
+ if (item->cleanup)
+ item->cleanup(item->be_data);
+ ast_free(item);
+ if (name_detail)
+ break;
}
}
AST_RWLIST_TRAVERSE_SAFE_END;
@@ -744,7 +757,6 @@
/* everywhere ast_cdr_disposition is called, it will call ast_cdr_failed()
if ast_cdr_disposition returns a non-zero value */
-
int ast_cdr_disposition(struct ast_cdr *cdr, int cause)
{
int res = 0;
@@ -826,6 +838,8 @@
for ( ; cdr ; cdr = cdr->next) {
if (!ast_test_flag(cdr, AST_CDR_FLAG_LOCKED)) {
chan = S_OR(cdr->channel, "<unknown>");
+ if (!ast_strlen_zero(cdr->channel))
+ ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan);
ast_copy_string(cdr->channel, c->name, sizeof(cdr->channel));
set_one_cid(cdr, c);
@@ -911,7 +925,7 @@
int ast_cdr_setamaflags(struct ast_channel *chan, const char *flag)
{
- struct ast_cdr *cdr;
+ struct ast_cdr *cdr = chan->cdr;
int newflag = ast_cdr_amaflags2int(flag);
if (newflag) {
for (cdr = chan->cdr; cdr; cdr = cdr->next) {
@@ -998,11 +1012,28 @@
ast_set_flag(cdr, AST_CDR_FLAG_POSTED);
if (ast_test_flag(cdr, AST_CDR_FLAG_POST_DISABLED))
continue;
- AST_RWLIST_RDLOCK(&be_list);
+ /* add a ref to make sure we dont have a race condition */
+ ast_atomic_fetchadd_int(&cdr->usecount, +1);
+ AST_RWLIST_RDLOCK(&be_list);
AST_RWLIST_TRAVERSE(&be_list, i, list) {
- i->be(cdr);
+ AST_LIST_LOCK(&i->cdr_queue);
+ ast_debug(1, "cdr inserted into queue/list for: %s\n", i->name);
+ /* increment usecount of this cdr record */
+ ast_atomic_fetchadd_int(&cdr->usecount, +1);
+ /* increment cdr counter for this queue */
+ ast_atomic_fetchadd_int(&i->waiting_cdrs, +1);
+ /* if we hit our max number of records, delete the oldest one */
+ if (i->waiting_cdrs > queuesize)
+ AST_LIST_REMOVE_HEAD(&i->cdr_queue, list);
+ AST_LIST_INSERT_TAIL(&i->cdr_queue, cdr, list);
+ ast_cond_signal(&i->cdr_pending_cond);
+ AST_LIST_UNLOCK(&i->cdr_queue);
}
AST_RWLIST_UNLOCK(&be_list);
+ if (ast_atomic_fetchadd_int(&cdr->usecount, -1) == 1) {
+ ast_debug(1, "cdr record no longer in any queue, so lets free it\n");
+ ast_cdr_free(cdr);
+ }
}
}
@@ -1060,99 +1091,8 @@
return ret;
}
-/*! \note Don't call without cdr_batch_lock */
-static void reset_batch(void)
-{
- batch->size = 0;
- batch->head = NULL;
- batch->tail = NULL;
-}
-
-/*! \note Don't call without cdr_batch_lock */
-static int init_batch(void)
-{
- /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
- if (!(batch = ast_malloc(sizeof(*batch))))
- return -1;
-
- reset_batch();
-
- return 0;
-}
-
-static void *do_batch_backend_process(void *data)
-{
- struct ast_cdr_batch_item *processeditem;
- struct ast_cdr_batch_item *batchitem = data;
-
- /* Push each CDR into storage mechanism(s) and free all the memory */
- while (batchitem) {
- post_cdr(batchitem->cdr);
- ast_cdr_free(batchitem->cdr);
- processeditem = batchitem;
- batchitem = batchitem->next;
- ast_free(processeditem);
- }
-
- return NULL;
-}
-
-void ast_cdr_submit_batch(int shutdown)
-{
- struct ast_cdr_batch_item *oldbatchitems = NULL;
- pthread_t batch_post_thread = AST_PTHREADT_NULL;
-
- /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
- if (!batch || !batch->head)
- return;
-
- /* move the old CDRs aside, and prepare a new CDR batch */
- ast_mutex_lock(&cdr_batch_lock);
- oldbatchitems = batch->head;
- reset_batch();
- ast_mutex_unlock(&cdr_batch_lock);
-
- /* if configured, spawn a new thread to post these CDRs,
- also try to save as much as possible if we are shutting down safely */
- if (batchscheduleronly || shutdown) {
- ast_debug(1, "CDR single-threaded batch processing begins now\n");
- do_batch_backend_process(oldbatchitems);
- } else {
- if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
- ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
- do_batch_backend_process(oldbatchitems);
- } else {
- ast_debug(1, "CDR multi-threaded batch processing begins now\n");
- }
- }
-}
-
-static int submit_scheduled_batch(const void *data)
-{
- ast_cdr_submit_batch(0);
- /* manually reschedule from this point in time */
- cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
- /* returning zero so the scheduler does not automatically reschedule */
- return 0;
-}
-
-static void submit_unscheduled_batch(void)
-{
- /* this is okay since we are not being called from within the scheduler */
- AST_SCHED_DEL(sched, cdr_sched);
- /* schedule the submission to occur ASAP (1 ms) */
- cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
- /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
- ast_mutex_lock(&cdr_pending_lock);
- ast_cond_signal(&cdr_pending_cond);
- ast_mutex_unlock(&cdr_pending_lock);
-}
-
void ast_cdr_detach(struct ast_cdr *cdr)
{
- struct ast_cdr_batch_item *newtail;
- int curr;
-
if (!cdr)
return;
@@ -1164,75 +1104,79 @@
return;
}
- /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
- if (!batchmode) {
- post_cdr(cdr);
- ast_cdr_free(cdr);
- return;
- }
-
- /* otherwise, each CDR gets put into a batch list (at the end) */
- ast_debug(1, "CDR detaching from this thread\n");
-
- /* we'll need a new tail for every CDR */
- if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
- post_cdr(cdr);
- ast_cdr_free(cdr);
- return;
- }
-
- /* don't traverse a whole list (just keep track of the tail) */
- ast_mutex_lock(&cdr_batch_lock);
- if (!batch)
- init_batch();
- if (!batch->head) {
- /* new batch is empty, so point the head at the new tail */
- batch->head = newtail;
- } else {
- /* already got a batch with something in it, so just append a new tail */
- batch->tail->next = newtail;
- }
- newtail->cdr = cdr;
- batch->tail = newtail;
- curr = batch->size++;
- ast_mutex_unlock(&cdr_batch_lock);
-
- /* if we have enough stuff to post, then do it */
- if (curr >= (batchsize - 1))
- submit_unscheduled_batch();
+ post_cdr(cdr);
}
static void *do_cdr(void *data)
{
- struct timespec timeout;
- int schedms;
- int numevents = 0;
-
+ struct ast_cdr_beitem *i = data;
+ struct ast_cdr *next = NULL, *cdr = NULL;
+ struct timespec ts;
+ struct timeval tv;
+ int be_res = 0;
+
+ ast_debug(1, "CDR Worker thread for %s started\n", i->name);
for (;;) {
- struct timeval now;
- schedms = ast_sched_wait(sched);
- /* this shouldn't happen, but provide a 1 second default just in case */
- if (schedms <= 0)
- schedms = 1000;
- now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
- timeout.tv_sec = now.tv_sec;
- timeout.tv_nsec = now.tv_usec * 1000;
- /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
- ast_mutex_lock(&cdr_pending_lock);
- ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
- numevents = ast_sched_runq(sched);
- ast_mutex_unlock(&cdr_pending_lock);
- ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
- }
-
+ ast_debug(1, "CDR Worker thread for %s: top of loop\n", i->name);
+ /* We lock the cdr queue and see if one exists
+ if not we wait to be signaled*/
+ AST_LIST_LOCK(&i->cdr_queue);
+ ast_debug(1, "CDR Worker thread for %s: got the list lock\n", i->name);
+ if (AST_LIST_EMPTY(&i->cdr_queue) && !i->cancel_thread) {
+ ast_debug(1, "I guess the cdr queue for %s is empty, lets wait\n", i->name);
+ ast_cond_wait(&i->cdr_pending_cond, &i->cdr_queue.lock);
+ } else if (AST_LIST_EMPTY(&i->cdr_queue) && i->cancel_thread){
+ AST_LIST_UNLOCK(&i->cdr_queue);
+ break;
+ }
+ next = AST_LIST_FIRST(&i->cdr_queue);
+ AST_LIST_HEAD_INIT_NOLOCK(&i->cdr_queue);
+ AST_LIST_UNLOCK(&i->cdr_queue);
+
+ while ((cdr = next)) {
+ /* Get the next cdr now so that we can
+ free our current structure later */
+ next = AST_LIST_NEXT(cdr, list);
+ ast_debug(1, "writing cdr to %s\n", i->name);
+ for (;;)
+ {
+ be_res = i->be(cdr, i->be_data);
+ if (be_res == AST_CDR_POST_OK) {
+ ast_debug(1, "cdr insert into %s ok!\n", i->name);
+ break;
+ } else if (be_res == AST_CDR_POST_FAILED) {
+ ast_debug(1, "cdr insert into %s failed, retrying\n", i->name);
+ tv = ast_tvadd(ast_tvnow(), ast_samp2tv(retrydelay * 1000, 1000));
+ ts.tv_sec = tv.tv_sec;
+ ts.tv_nsec = tv.tv_usec * 1000;
+ ast_mutex_lock(&i->retry_poll_lock);
+ ast_cond_timedwait(&i->cdr_retry_cond, &i->retry_poll_lock, &ts);
+ ast_mutex_unlock(&i->retry_poll_lock);
+ } else if (be_res == AST_CDR_POST_DISABLED) {
+ ast_debug(1, "cdr backend %s is registered, but disabled\n", i->name);
+ break;
+ }
+ }
+ /* depracate cdr counter for this queue */
+ ast_atomic_fetchadd_int(&i->waiting_cdrs, -1);
+ /* depracate uses of this cdr record
+ free if no one is using it*/
+ /* there is a race condition here, if one thread finishes working on the cdr before
+ it is posted to all the backends, then it will delete the cdr before we are actually
+ finished with it and an explosion occurs */
+ if (ast_atomic_fetchadd_int(&cdr->usecount, -1) == 1) {
+ ast_debug(1, "cdr record no longer in any queue, so lets free it\n");
+ ast_cdr_free(cdr);
+ }
+ }
+ }
+ ast_debug(1, "CDR Worker thread for %s stopped\n", i->name);
return NULL;
}
static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
struct ast_cdr_beitem *beitem=NULL;
- int cnt=0;
- long nextbatchtime=0;
switch (cmd) {
case CLI_INIT:
@@ -1248,30 +1192,31 @@
if (a->argc > 3)
return CLI_SHOWUSAGE;
- ast_cli(a->fd, "CDR logging: %s\n", enabled ? "enabled" : "disabled");
- ast_cli(a->fd, "CDR mode: %s\n", batchmode ? "batch" : "simple");
+
+ /* What if there are no backends? */
+ ast_cli(a->fd, "\nCall Detail Recording (CDR) Status\n");
+ ast_cli(a->fd, "----------------------------------\n");
+ ast_cli(a->fd, " Engine status: %s\n", enabled ? "Enabled" : "Disabled");
+
+ /* Since we unregister the CLI command when enabled=no, we will never get when !enabled */
if (enabled) {
- ast_cli(a->fd, "CDR output unanswered calls: %s\n", unanswered ? "yes" : "no");
- if (batchmode) {
- if (batch)
- cnt = batch->size;
- if (cdr_sched > -1)
- nextbatchtime = ast_sched_when(sched, cdr_sched);
- ast_cli(a->fd, "CDR safe shut down: %s\n", batchsafeshutdown ? "enabled" : "disabled");
- ast_cli(a->fd, "CDR batch threading model: %s\n", batchscheduleronly ? "scheduler only" : "scheduler plus separate threads");
- ast_cli(a->fd, "CDR current batch size: %d record%s\n", cnt, ESS(cnt));
- ast_cli(a->fd, "CDR maximum batch size: %d record%s\n", batchsize, ESS(batchsize));
- ast_cli(a->fd, "CDR maximum batch time: %d second%s\n", batchtime, ESS(batchtime));
- ast_cli(a->fd, "CDR next scheduled batch processing time: %ld second%s\n", nextbatchtime, ESS(nextbatchtime));
- }
+ ast_cli(a->fd, " Safe shutdown: %s\n\n", safeshutdown ? "Enabled" : "Disabled");
+ ast_cli(a->fd, " CDR output unanswered calls: %s\n\n", unanswered ? "yes" : "no");
+ ast_cli(a->fd, " Registered Backends:\n");
+ ast_cli(a->fd, " --------------------\n");
AST_RWLIST_RDLOCK(&be_list);
AST_RWLIST_TRAVERSE(&be_list, beitem, list) {
- ast_cli(a->fd, "CDR registered backend: %s\n", beitem->name);
- }
- AST_RWLIST_UNLOCK(&be_list);
- }
-
- return CLI_SUCCESS;
+ if (ast_strlen_zero(beitem->name_detail)) {
+ ast_cli(a->fd, " %s\n", beitem->desc);
+ } else {
+ ast_cli(a->fd, " %s (%s)\n", beitem->desc, beitem->name_detail);
+ }
+ }
+ AST_RWLIST_UNLOCK(&be_list);
+ }
+ ast_cli(a->fd, "\n");
+
+ return CLI_SUCCESS;
}
static char *handle_cli_status_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
@@ -1282,28 +1227,6 @@
return res;
}
-static char *handle_cli_submit(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
- switch (cmd) {
- case CLI_INIT:
- e->command = "cdr submit";
- e->usage =
- "Usage: cdr submit\n"
- " Posts all pending batched CDR data to the configured CDR backend engine modules.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- if (a->argc > 2)
- return CLI_SHOWUSAGE;
-
- submit_unscheduled_batch();
- ast_cli(a->fd, "Submitted CDRs to backend engines for processing. This may take a while.\n");
-
- return CLI_SUCCESS;
-}
-
-static struct ast_cli_entry cli_submit = AST_CLI_DEFINE(handle_cli_submit, "Posts all pending batched CDR data");
static struct ast_cli_entry cli_status_deprecated = AST_CLI_DEFINE(handle_cli_status_deprecated, "Display the CDR status");
static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CDR status", .deprecate_cmd = &cli_status_deprecated);
@@ -1311,116 +1234,74 @@
{
struct ast_config *config;
const char *enabled_value;
+ const char *safeshutdown_value;
+ const char *queuesize_value;
+ const char *retrydelay_value;
const char *unanswered_value;
- const char *batched_value;
- const char *scheduleronly_value;
- const char *batchsafeshutdown_value;
- const char *size_value;
- const char *time_value;
const char *end_before_h_value;
- int cfg_size;
- int cfg_time;
+ int cfg_queuesize;
+ int cfg_retrydelay;
int was_enabled;
- int was_batchmode;
int res=0;
+
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
if ((config = ast_config_load("cdr.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
return 0;
- ast_mutex_lock(&cdr_batch_lock);
-
- batchsize = BATCH_SIZE_DEFAULT;
- batchtime = BATCH_TIME_DEFAULT;
- batchscheduleronly = BATCH_SCHEDULER_ONLY_DEFAULT;
- batchsafeshutdown = BATCH_SAFE_SHUTDOWN_DEFAULT;
+
+ safeshutdown = SAFE_SHUTDOWN_DEFAULT;
+ queuesize = MAX_QUEUE_SIZE_DEFAULT;
+ retrydelay = RETRY_DELAY_DEFAULT;
was_enabled = enabled;
- was_batchmode = batchmode;
enabled = 1;
- batchmode = 0;
-
- /* don't run the next scheduled CDR posting while reloading */
- AST_SCHED_DEL(sched, cdr_sched);
if (config) {
if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
enabled = ast_true(enabled_value);
}
+ if ((safeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
+ safeshutdown = ast_true(safeshutdown_value);
+ }
if ((unanswered_value = ast_variable_retrieve(config, "general", "unanswered"))) {
unanswered = ast_true(unanswered_value);
}
- if ((batched_value = ast_variable_retrieve(config, "general", "batch"))) {
- batchmode = ast_true(batched_value);
- }
- if ((scheduleronly_value = ast_variable_retrieve(config, "general", "scheduleronly"))) {
- batchscheduleronly = ast_true(scheduleronly_value);
- }
- if ((batchsafeshutdown_value = ast_variable_retrieve(config, "general", "safeshutdown"))) {
- batchsafeshutdown = ast_true(batchsafeshutdown_value);
- }
- if ((size_value = ast_variable_retrieve(config, "general", "size"))) {
- if (sscanf(size_value, "%d", &cfg_size) < 1)
- ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", size_value);
- else if (size_value < 0)
- ast_log(LOG_WARNING, "Invalid maximum batch size '%d' specified, using default\n", cfg_size);
+ if ((queuesize_value = ast_variable_retrieve(config, "general", "size"))) {
+ if (sscanf(queuesize_value, "%d", &cfg_queuesize) < 1)
+ ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", queuesize_value);
+ else if (queuesize_value < 0)
+ ast_log(LOG_WARNING, "Invalid maximum queue size '%d' specified, using default\n", cfg_queuesize);
else
- batchsize = cfg_size;
- }
- if ((time_value = ast_variable_retrieve(config, "general", "time"))) {
- if (sscanf(time_value, "%d", &cfg_time) < 1)
- ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", time_value);
- else if (time_value < 0)
- ast_log(LOG_WARNING, "Invalid maximum batch time '%d' specified, using default\n", cfg_time);
- else
- batchtime = cfg_time;
- }
+ queuesize = cfg_queuesize;
+ }
+ if ((retrydelay_value = ast_variable_retrieve(config, "general", "retrydelay"))) {
+ if (sscanf(retrydelay_value, "%d", &cfg_retrydelay) < 1)
+ ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", retrydelay_value);
+ else if (retrydelay_value < 0)
+ ast_log(LOG_WARNING, "Invalid maximum queue size '%d' specified, using default\n", cfg_retrydelay);
+ else
+ retrydelay = cfg_retrydelay;
+ }
+
if ((end_before_h_value = ast_variable_retrieve(config, "general", "endbeforehexten")))
ast_set2_flag(&ast_options, ast_true(end_before_h_value), AST_OPT_FLAG_END_CDR_BEFORE_H_EXTEN);
}
- if (enabled && !batchmode) {
+ if (enabled) {
ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
- } else if (enabled && batchmode) {
- cdr_sched = ast_sched_add(sched, batchtime * 1000, submit_scheduled_batch, NULL);
- ast_log(LOG_NOTICE, "CDR batch mode logging enabled, first of either size %d or time %d seconds.\n", batchsize, batchtime);
} else {
- ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
- }
-
- /* if this reload enabled the CDR batch mode, create the background thread
- if it does not exist */
- if (enabled && batchmode && (!was_enabled || !was_batchmode) && (cdr_thread == AST_PTHREADT_NULL)) {
- ast_cond_init(&cdr_pending_cond, NULL);
- if (ast_pthread_create_background(&cdr_thread, NULL, do_cdr, NULL) < 0) {
- ast_log(LOG_ERROR, "Unable to start CDR thread.\n");
- AST_SCHED_DEL(sched, cdr_sched);
- } else {
- ast_cli_register(&cli_submit);
- ast_register_atexit(ast_cdr_engine_term);
- res = 0;
- }
- /* if this reload disabled the CDR and/or batch mode and there is a background thread,
- kill it */
- } else if (((!enabled && was_enabled) || (!batchmode && was_batchmode)) && (cdr_thread != AST_PTHREADT_NULL)) {
- /* wake up the thread so it will exit */
- pthread_cancel(cdr_thread);
- pthread_kill(cdr_thread, SIGURG);
- pthread_join(cdr_thread, NULL);
- cdr_thread = AST_PTHREADT_NULL;
- ast_cond_destroy(&cdr_pending_cond);
- ast_cli_unregister(&cli_submit);
+ ast_log(LOG_NOTICE, "CDR logging disabled, no data will be stored.\n");
+ }
+
+
+ if (was_enabled && !enabled) {
ast_unregister_atexit(ast_cdr_engine_term);
- res = 0;
- /* if leaving batch mode, then post the CDRs in the batch,
- and don't reschedule, since we are stopping CDR logging */
- if (!batchmode && was_batchmode) {
- ast_cdr_engine_term();
- }
- } else {
- res = 0;
- }
-
- ast_mutex_unlock(&cdr_batch_lock);
+ ast_cli_unregister(&cli_status);
+ } else if (!was_enabled && enabled) {
+ ast_register_atexit(ast_cdr_engine_term);
+ ast_cli_register(&cli_status);
+ }
+
ast_config_destroy(config);
manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: CDR\r\nMessage: CDR subsystem reload requested\r\n");
@@ -1429,35 +1310,44 @@
int ast_cdr_engine_init(void)
{
- int res;
-
- sched = sched_context_create();
- if (!sched) {
- ast_log(LOG_ERROR, "Unable to create schedule context.\n");
- return -1;
- }
-
- ast_cli_register(&cli_status);
-
- res = do_reload(0);
- if (res) {
- ast_mutex_lock(&cdr_batch_lock);
- res = init_batch();
- ast_mutex_unlock(&cdr_batch_lock);
- }
-
- return res;
+ return do_reload(0);
}
/* \note This actually gets called a couple of times at shutdown. Once, before we start
hanging up channels, and then again, after the channel hangup timeout expires */
void ast_cdr_engine_term(void)
{
- ast_cdr_submit_batch(batchsafeshutdown);
+ struct ast_cdr_beitem *i = NULL;
+
+ if (safeshutdown) {
+ if (option_verbose > 1)
+ ast_verbose(VERBOSE_PREFIX_2 "Waiting for CDR Backends to shutdown..\n");
+ AST_RWLIST_WRLOCK(&be_list);
+ AST_RWLIST_TRAVERSE_SAFE_BEGIN(&be_list, i, list) {
+ i->cancel_thread = 1;
+ /* signal the thread so it can exit */
+ ast_cond_signal(&i->cdr_pending_cond);
+ ast_cond_signal(&i->cdr_retry_cond);
+ /* wait for thread to exit so we can clean up */
+ ast_verbose(VERBOSE_PREFIX_2 "joining thread\n");
+ pthread_join(i->cdr_thread, NULL);
+ ast_verbose(VERBOSE_PREFIX_2 "=ast\n");
+ i->cdr_thread = AST_PTHREADT_NULL;
+ ast_verbose(VERBOSE_PREFIX_2 "destroy 1\n");
+ ast_cond_destroy(&i->cdr_pending_cond);
+ ast_verbose(VERBOSE_PREFIX_2 "destroy 2\n");
+ ast_cond_destroy(&i->cdr_retry_cond);
+ ast_verbose(VERBOSE_PREFIX_2 "remove\n");
+ AST_RWLIST_REMOVE_CURRENT(list);
+ ast_verbose(VERBOSE_PREFIX_2 "ast_free\n");
+ ast_free(i);
+ }
+ AST_RWLIST_TRAVERSE_SAFE_END;
+ AST_RWLIST_UNLOCK(&be_list);
+ }
}
int ast_cdr_engine_reload(void)
{
return do_reload(1);
}
-
Modified: team/group/NoLossCDR-Redux2/res/res_config_sqlite.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/res/res_config_sqlite.c?view=diff&rev=104130&r1=104129&r2=104130
==============================================================================
--- team/group/NoLossCDR-Redux2/res/res_config_sqlite.c (original)
+++ team/group/NoLossCDR-Redux2/res/res_config_sqlite.c Tue Feb 26 09:34:06 2008
@@ -239,7 +239,7 @@
* \retval 0 on success
* \retval 1 if an error occurred
*/
-static int cdr_handler(struct ast_cdr *cdr);
+static int cdr_handler(struct ast_cdr *cdr, void *data);
/*!
* \brief SQLite callback function for static configuration.
@@ -1409,7 +1409,7 @@
ast_cli_unregister_multiple(cli_status, sizeof(cli_status) / sizeof(struct ast_cli_entry));
if (cdr_registered)
- ast_cdr_unregister(RES_CONFIG_SQLITE_NAME);
+ ast_cdr_unregister(RES_CONFIG_SQLITE_NAME, NULL);
ast_config_engine_deregister(&sqlite_engine);
@@ -1506,7 +1506,7 @@
}
}
- error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler);
+ error = ast_cdr_register(RES_CONFIG_SQLITE_NAME, NULL, RES_CONFIG_SQLITE_DESCRIPTION, cdr_handler, NULL, NULL);
if (error) {
unload_module();
More information about the asterisk-commits
mailing list