From 9656df37e15b22894143a76d8d96f61ac4d974d5 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 22 Feb 2016 14:10:36 +0000 Subject: [PATCH] Add command to sync fuzzy storage It should be as simple as `rspamadm control fuzzy_sync` Issue: #533 Reported by: @moisseev --- src/fuzzy_storage.c | 41 ++++++++++++++++++++++++++++++++++ src/libserver/rspamd_control.c | 15 ++++++++++++- src/libserver/rspamd_control.h | 7 ++++++ src/rspamadm/control.c | 4 ++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c index e4ce81d42..b847d7865 100644 --- a/src/fuzzy_storage.c +++ b/src/fuzzy_storage.c @@ -807,6 +807,45 @@ sync_callback (gint fd, short what, void *arg) evtimer_add (&tev, &tmv); } +static gboolean +rspamd_fuzzy_storage_sync (struct rspamd_main *rspamd_main, + struct rspamd_worker *worker, gint fd, + struct rspamd_control_command *cmd, + gpointer ud) +{ + struct rspamd_fuzzy_storage_ctx *ctx = ud; + gdouble next_check; + guint64 old_expired, new_expired; + struct rspamd_control_reply rep; + + if (ctx->backend) { + rspamd_fuzzy_process_updates_queue (ctx); + /* Call backend sync */ + old_expired = rspamd_fuzzy_backend_expired (ctx->backend); + rspamd_fuzzy_backend_sync (ctx->backend, ctx->expire, TRUE); + new_expired = rspamd_fuzzy_backend_expired (ctx->backend); + + if (old_expired < new_expired) { + ctx->stat.fuzzy_hashes_expired += new_expired - old_expired; + } + } + + rep.reply.fuzzy_sync.status = 0; + + /* Handle next timer event */ + event_del (&tev); + next_check = rspamd_time_jitter (ctx->sync_timeout, 0); + double_to_tv (next_check, &tmv); + evtimer_add (&tev, &tmv); + + if (write (fd, &rep, sizeof (rep)) != sizeof (rep)) { + msg_err ("cannot write reply to the control socket: %s", + strerror (errno)); + } + + return TRUE; +} + static gboolean rspamd_fuzzy_storage_reload (struct rspamd_main *rspamd_main, struct rspamd_worker *worker, gint fd, @@ -1382,6 +1421,8 @@ start_fuzzy (struct rspamd_worker *worker) rspamd_fuzzy_storage_reload, ctx); rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_FUZZY_STAT, rspamd_fuzzy_storage_stat, ctx); + rspamd_control_worker_add_cmd_handler (worker, RSPAMD_CONTROL_FUZZY_SYNC, + rspamd_fuzzy_storage_sync, ctx); /* Create radix tree */ if (ctx->update_map != NULL) { if (!rspamd_map_is_map (ctx->update_map)) { diff --git a/src/libserver/rspamd_control.c b/src/libserver/rspamd_control.c index 3111d93f0..b24be0954 100644 --- a/src/libserver/rspamd_control.c +++ b/src/libserver/rspamd_control.c @@ -93,6 +93,13 @@ static const struct rspamd_control_cmd_match { }, .type = RSPAMD_CONTROL_FUZZY_STAT }, + { + .name = { + .begin = "/fuzzysync", + .len = sizeof ("/fuzzysync") - 1 + }, + .type = RSPAMD_CONTROL_FUZZY_SYNC + }, }; void @@ -177,7 +184,8 @@ rspamd_control_write_reply (struct rspamd_control_session *session) DL_FOREACH (session->replies, elt) { /* Skip incompatible worker for fuzzy_stat */ - if (session->cmd.type == RSPAMD_CONTROL_FUZZY_STAT && + if ((session->cmd.type == RSPAMD_CONTROL_FUZZY_STAT || + session->cmd.type == RSPAMD_CONTROL_FUZZY_SYNC) && elt->wrk->type != g_quark_from_static_string ("fuzzy")) { continue; } @@ -265,6 +273,10 @@ rspamd_control_write_reply (struct rspamd_control_session *session) false); } break; + case RSPAMD_CONTROL_FUZZY_SYNC: + ucl_object_insert_key (cur, ucl_object_fromint ( + elt->reply.reply.fuzzy_sync.status), "status", 0, false); + break; default: break; } @@ -501,6 +513,7 @@ rspamd_control_default_cmd_handler (gint fd, case RSPAMD_CONTROL_RECOMPILE: case RSPAMD_CONTROL_HYPERSCAN_LOADED: case RSPAMD_CONTROL_FUZZY_STAT: + case RSPAMD_CONTROL_FUZZY_SYNC: break; case RSPAMD_CONTROL_RERESOLVE: if (cd->worker->srv->cfg) { diff --git a/src/libserver/rspamd_control.h b/src/libserver/rspamd_control.h index 113658e9c..a0d5fba1b 100644 --- a/src/libserver/rspamd_control.h +++ b/src/libserver/rspamd_control.h @@ -30,6 +30,7 @@ enum rspamd_control_type { RSPAMD_CONTROL_RECOMPILE, RSPAMD_CONTROL_HYPERSCAN_LOADED, RSPAMD_CONTROL_FUZZY_STAT, + RSPAMD_CONTROL_FUZZY_SYNC, RSPAMD_CONTROL_MAX }; @@ -60,6 +61,9 @@ struct rspamd_control_command { struct { guint unused; } fuzzy_stat; + struct { + guint unused; + } fuzzy_sync; } cmd; }; @@ -89,6 +93,9 @@ struct rspamd_control_reply { guint status; gchar storage_id[MEMPOOL_UID_LEN]; } fuzzy_stat; + struct { + guint status; + } fuzzy_sync; } reply; }; diff --git a/src/rspamadm/control.c b/src/rspamadm/control.c index e69a7fe00..de6e48346 100644 --- a/src/rspamadm/control.c +++ b/src/rspamadm/control.c @@ -201,6 +201,10 @@ rspamadm_control (gint argc, gchar **argv) g_ascii_strcasecmp (cmd, "fuzzy_stat") == 0) { path = "/fuzzystat"; } + else if (g_ascii_strcasecmp (cmd, "fuzzysync") == 0 || + g_ascii_strcasecmp (cmd, "fuzzy_sync") == 0) { + path = "/fuzzysync"; + } else { rspamd_fprintf (stderr, "unknown command: %s\n", cmd); exit (1); -- 2.39.5