]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Add command to sync fuzzy storage
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 22 Feb 2016 14:10:36 +0000 (14:10 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 22 Feb 2016 14:10:36 +0000 (14:10 +0000)
It should be as simple as `rspamadm control fuzzy_sync`

Issue: #533
Reported by: @moisseev

src/fuzzy_storage.c
src/libserver/rspamd_control.c
src/libserver/rspamd_control.h
src/rspamadm/control.c

index e4ce81d42486cd7a405e5c5c738673c3cd73a4de..b847d78654185efe44dbe514cf662c57682a4b1b 100644 (file)
@@ -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)) {
index 3111d93f07614bc43dc905643c227b6ae545b672..b24be095468d8d11307ddbe19b5de1fc147bd48d 100644 (file)
@@ -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) {
index 113658e9c35cc1eaf84db483f7f6574200560407..a0d5fba1b968b441486ae4382aceb336bddb6b09 100644 (file)
@@ -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;
 };
 
index e69a7fe002b9387408955c09852f2601ab900dde..de6e483466f819335b5eb576362e2afacf80e546 100644 (file)
@@ -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);