aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstat
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-06-19 15:25:10 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-06-19 15:25:10 +0100
commit45c538da92ef6482a6e276be8064321c0636aff4 (patch)
tree34f0fb94e604da018dd8fcfb68d119e91c59a881 /src/libstat
parent37117c9d9884e2293373aea028e33eaaedf4b6a5 (diff)
downloadrspamd-45c538da92ef6482a6e276be8064321c0636aff4.tar.gz
rspamd-45c538da92ef6482a6e276be8064321c0636aff4.zip
Implement statistics closing.
Diffstat (limited to 'src/libstat')
-rw-r--r--src/libstat/backends/backends.h4
-rw-r--r--src/libstat/backends/mmaped_file.c43
-rw-r--r--src/libstat/backends/sqlite3_backend.c31
-rw-r--r--src/libstat/stat_api.h5
-rw-r--r--src/libstat/stat_config.c18
5 files changed, 79 insertions, 22 deletions
diff --git a/src/libstat/backends/backends.h b/src/libstat/backends/backends.h
index 8180e1baf..18f4fce26 100644
--- a/src/libstat/backends/backends.h
+++ b/src/libstat/backends/backends.h
@@ -54,6 +54,7 @@ struct rspamd_stat_backend {
gulong (*inc_learns)(struct rspamd_statfile_runtime *runtime, gpointer ctx);
gulong (*dec_learns)(struct rspamd_statfile_runtime *runtime, gpointer ctx);
ucl_object_t* (*get_stat)(struct rspamd_statfile_runtime *runtime, gpointer ctx);
+ void (*close)(gpointer ctx);
gpointer ctx;
};
@@ -79,7 +80,8 @@ struct rspamd_stat_backend {
gulong rspamd_##name##_learns (struct rspamd_statfile_runtime *runtime, \
gpointer ctx); \
ucl_object_t * rspamd_##name##_get_stat (struct rspamd_statfile_runtime *runtime, \
- gpointer ctx)
+ gpointer ctx); \
+ void rspamd_##name##_close (gpointer ctx)
RSPAMD_STAT_BACKEND_DEF(mmaped_file);
RSPAMD_STAT_BACKEND_DEF(redis);
diff --git a/src/libstat/backends/mmaped_file.c b/src/libstat/backends/mmaped_file.c
index 70452378f..b4d60ad0d 100644
--- a/src/libstat/backends/mmaped_file.c
+++ b/src/libstat/backends/mmaped_file.c
@@ -638,7 +638,7 @@ rspamd_mmaped_file_open (rspamd_mmaped_file_ctx * pool,
}
gint
-rspamd_mmaped_file_close (rspamd_mmaped_file_ctx * pool,
+rspamd_mmaped_file_close_file (rspamd_mmaped_file_ctx * pool,
rspamd_mmaped_file_t * file)
{
rspamd_mmaped_file_t *pos;
@@ -797,23 +797,6 @@ rspamd_mmaped_file_create (rspamd_mmaped_file_ctx * pool, const gchar *filename,
return 0;
}
-void
-rspamd_mmaped_file_destroy (rspamd_mmaped_file_ctx * pool)
-{
- GHashTableIter it;
- gpointer k, v;
- rspamd_mmaped_file_t *f;
-
- g_hash_table_iter_init (&it, pool->files);
- while (g_hash_table_iter_next (&it, &k, &v)) {
- f = (rspamd_mmaped_file_t *)v;
- rspamd_mmaped_file_close (pool, f);
- }
-
- g_hash_table_destroy (pool->files);
- rspamd_mempool_delete (pool->pool);
-}
-
gpointer
rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg)
{
@@ -883,6 +866,30 @@ rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg)
return (gpointer)new;
}
+void
+rspamd_mmaped_file_close (gpointer p)
+{
+ rspamd_mmaped_file_ctx *ctx = (rspamd_mmaped_file_ctx *)p;
+ GHashTableIter it;
+ gpointer k, v;
+ rspamd_mmaped_file_t *mf;
+
+ g_assert (ctx != NULL);
+
+ rspamd_mempool_lock_mutex (ctx->lock);
+ g_hash_table_iter_init (&it, ctx->files);
+
+ while (g_hash_table_iter_next (&it, &k, &v)) {
+ mf = v;
+ rspamd_mmaped_file_close_file (ctx, mf);
+ }
+
+ g_hash_table_unref (ctx->files);
+ rspamd_mempool_unlock_mutex (ctx->lock);
+
+ rspamd_mempool_delete (ctx->pool);
+}
+
gpointer
rspamd_mmaped_file_runtime (struct rspamd_task *task,
struct rspamd_statfile_config *stcf,
diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c
index 43c52483f..e5471b436 100644
--- a/src/libstat/backends/sqlite3_backend.c
+++ b/src/libstat/backends/sqlite3_backend.c
@@ -63,7 +63,7 @@ static const char *create_tables_sql =
");"
"CREATE UNIQUE INDEX IF NOT EXISTS un ON users(name);"
"CREATE UNIQUE INDEX IF NOT EXISTS ln ON languages(name);"
- "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION
+ "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION ";"
"INSERT INTO users(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);"
"INSERT INTO languages(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);"
"COMMIT;";
@@ -302,7 +302,7 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx,
while (curst) {
stf = curst->data;
- if (strcmp (stf->backend, SQLITE3_BACKEND_TYPE) == 0) {
+ if (stf->backend && strcmp (stf->backend, SQLITE3_BACKEND_TYPE) == 0) {
/*
* Check configuration sanity
*/
@@ -349,6 +349,33 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx,
return (gpointer)new;
}
+void
+rspamd_sqlite3_close (gpointer p)
+{
+ struct rspamd_stat_sqlite3_ctx *ctx = p;
+ struct rspamd_stat_sqlite3_db *bk;
+ GHashTableIter it;
+ gpointer k, v;
+
+ g_hash_table_iter_init (&it, ctx->files);
+
+ while (g_hash_table_iter_next (&it, &k, &v)) {
+ bk = v;
+
+ if (bk->sqlite) {
+ if (bk->in_transaction) {
+ rspamd_sqlite3_run_prstmt (bk, RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT);
+ }
+
+ sqlite3_close (bk->sqlite);
+ rspamd_sqlite3_close_prstmt (bk->prstmt);
+ g_slice_free1 (sizeof (*bk), bk);
+ }
+ }
+
+ g_hash_table_destroy (ctx->files);
+}
+
gpointer
rspamd_sqlite3_runtime (struct rspamd_task *task,
struct rspamd_statfile_config *stcf, gboolean learn, gpointer p)
diff --git a/src/libstat/stat_api.h b/src/libstat/stat_api.h
index 48c7c53c6..94734adef 100644
--- a/src/libstat/stat_api.h
+++ b/src/libstat/stat_api.h
@@ -51,6 +51,11 @@ typedef enum rspamd_stat_result_e {
void rspamd_stat_init (struct rspamd_config *cfg);
/**
+ * Finalize statistics
+ */
+void rspamd_stat_close (void);
+
+/**
* Classify the task specified and insert symbols if needed
* @param task
* @return TRUE if task has been classified
diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c
index 91cca76c7..c75b02fe0 100644
--- a/src/libstat/stat_config.c
+++ b/src/libstat/stat_config.c
@@ -65,7 +65,8 @@ static struct rspamd_stat_tokenizer stat_tokenizers[] = {
.total_learns = rspamd_##eltn##_total_learns, \
.inc_learns = rspamd_##eltn##_inc_learns, \
.dec_learns = rspamd_##eltn##_dec_learns, \
- .get_stat = rspamd_##eltn##_get_stat \
+ .get_stat = rspamd_##eltn##_get_stat, \
+ .close = rspamd_##eltn##_close \
}
static struct rspamd_stat_backend stat_backends[] = {
@@ -113,6 +114,21 @@ rspamd_stat_init (struct rspamd_config *cfg)
}
}
+void
+rspamd_stat_close (void)
+{
+ guint i;
+
+ g_assert (stat_ctx != NULL);
+
+ for (i = 0; i < stat_ctx->backends_count; i ++) {
+ if (stat_ctx->backends[i].close != NULL) {
+ stat_ctx->backends[i].close (stat_ctx->backends[i].ctx);
+ msg_debug ("closed backend %s", stat_ctx->backends[i].name);
+ }
+ }
+}
+
struct rspamd_stat_ctx *
rspamd_stat_get_ctx (void)
{