aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstat
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-09 21:24:41 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-09 21:24:41 +0000
commit2fcbb9ce8a6c1d2e6bc52e3e04c23b22dc34b42d (patch)
tree2698844236de004bc989977e573283ddecc8468b /src/libstat
parentc3e917ac341171ba51739a8c4c6a42b68be1f972 (diff)
downloadrspamd-2fcbb9ce8a6c1d2e6bc52e3e04c23b22dc34b42d.tar.gz
rspamd-2fcbb9ce8a6c1d2e6bc52e3e04c23b22dc34b42d.zip
Fix stats for redis backend
Diffstat (limited to 'src/libstat')
-rw-r--r--src/libstat/backends/redis_backend.c65
-rw-r--r--src/libstat/stat_config.c8
2 files changed, 47 insertions, 26 deletions
diff --git a/src/libstat/backends/redis_backend.c b/src/libstat/backends/redis_backend.c
index 21e441abc..1c1690fc3 100644
--- a/src/libstat/backends/redis_backend.c
+++ b/src/libstat/backends/redis_backend.c
@@ -392,8 +392,11 @@ rspamd_redis_async_cbdata_cleanup (struct rspamd_redis_stat_cbdata *cbdata)
cbdata->elt->async->enabled = TRUE;
/* Replace ucl object */
- if (cbdata->elt->stat && cbdata->cur) {
- ucl_object_unref (cbdata->elt->stat);
+ if (cbdata->cur) {
+ if (cbdata->elt->stat) {
+ ucl_object_unref (cbdata->elt->stat);
+ }
+
cbdata->elt->stat = cbdata->cur;
cbdata->cur = NULL;
}
@@ -414,7 +417,7 @@ rspamd_redis_stat_learns (redisAsyncContext *c, gpointer r, gpointer priv)
struct rspamd_redis_stat_cbdata *cbdata = priv;
redisReply *reply = r;
ucl_object_t *obj;
- gulong num;
+ gulong num = 0;
cbdata->inflight --;
@@ -426,22 +429,10 @@ rspamd_redis_stat_learns (redisAsyncContext *c, gpointer r, gpointer priv)
rspamd_strtoul (reply->str, reply->len, &num);
}
- obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "used");
- if (obj) {
- obj->value.iv += num;
- }
-
- obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "total");
+ obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "revision");
if (obj) {
obj->value.iv += num;
}
-
- obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "size");
- if (obj) {
- /* Size of key + size of int64_t */
- obj->value.iv += num * (sizeof (G_STRINGIFY (G_MAXINT64)) +
- sizeof (guint64) + sizeof (gpointer));
- }
}
if (cbdata->inflight == 0) {
@@ -456,7 +447,7 @@ rspamd_redis_stat_key (redisAsyncContext *c, gpointer r, gpointer priv)
struct rspamd_redis_stat_cbdata *cbdata = priv;
redisReply *reply = r;
ucl_object_t *obj;
- gulong num;
+ glong num = 0;
cbdata->inflight --;
@@ -465,13 +456,30 @@ rspamd_redis_stat_key (redisAsyncContext *c, gpointer r, gpointer priv)
num = reply->integer;
}
else if (reply->type == REDIS_REPLY_STRING) {
- rspamd_strtoul (reply->str, reply->len, &num);
+ rspamd_strtol (reply->str, reply->len, &num);
}
- obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "revision");
+ if (num < 0) {
+ msg_err ("bad learns count: %L", (gint64)num);
+ num = 0;
+ }
+
+ obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "used");
+ if (obj) {
+ obj->value.iv += num;
+ }
+
+ obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "total");
if (obj) {
obj->value.iv += num;
}
+
+ obj = (ucl_object_t *)ucl_object_find_key (cbdata->cur, "size");
+ if (obj) {
+ /* Size of key + size of int64_t */
+ obj->value.iv += num * (sizeof (G_STRINGIFY (G_MAXINT64)) +
+ sizeof (guint64) + sizeof (gpointer));
+ }
}
if (cbdata->inflight == 0) {
@@ -559,13 +567,15 @@ rspamd_redis_stat_keys (redisAsyncContext *c, gpointer r, gpointer priv)
static void
rspamd_redis_async_stat_cb (struct rspamd_stat_async_elt *elt, gpointer d)
{
- struct redis_stat_ctx *ctx = REDIS_CTX (d);
+ struct redis_stat_ctx *ctx;
struct rspamd_redis_stat_elt *redis_elt = elt->ud;
struct rspamd_redis_stat_cbdata *cbdata;
rspamd_inet_addr_t *addr;
g_assert (redis_elt != NULL);
+ ctx = redis_elt->ctx;
+
if (redis_elt->cbdata) {
/* We have some other process pending */
rspamd_redis_async_cbdata_cleanup (redis_elt->cbdata);
@@ -657,7 +667,7 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv)
struct redis_stat_runtime *rt = REDIS_RUNTIME (priv);
redisReply *reply = r;
struct rspamd_task *task;
- gulong val;
+ glong val = 0;
task = rt->task;
@@ -667,8 +677,7 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv)
rt->learned = reply->integer;
}
else if (reply->type == REDIS_REPLY_STRING) {
- rspamd_strtoul (reply->str, reply->len, &val);
- rt->learned = val;
+ rspamd_strtol (reply->str, reply->len, &val);
}
else {
if (reply->type != REDIS_REPLY_NIL) {
@@ -676,9 +685,17 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv)
rt->stcf->symbol, reply->type);
}
- rt->learned = 0;
+ val = 0;
+ }
+
+ if (val < 0) {
+ msg_warn_task ("invalid number of learns for %s: %L",
+ rt->stcf->symbol, val);
+ val = 0;
}
+ rt->learned = val;
+
rt->conn_state = RSPAMD_REDIS_CONNECTED;
msg_debug_task ("connected to redis server, tokens learned for %s: %d",
diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c
index 9cdf6af8e..1c6989508 100644
--- a/src/libstat/stat_config.c
+++ b/src/libstat/stat_config.c
@@ -372,8 +372,12 @@ rspamd_stat_ctx_register_async (rspamd_stat_async_handler handler,
event_set (&elt->timer_ev, -1, EV_TIMEOUT, rspamd_async_elt_on_timer, elt);
event_base_set (st_ctx->ev_base, &elt->timer_ev);
- jittered_time = rspamd_time_jitter (elt->timeout, 0);
- double_to_tv (jittered_time, &elt->tv);
+ /*
+ * First we set timeval to zero as we want cb to be executed as
+ * fast as possible
+ */
+ elt->tv.tv_sec = 0;
+ elt->tv.tv_usec = 0;
event_add (&elt->timer_ev, &elt->tv);
g_queue_push_tail (st_ctx->async_elts, elt);