]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
indexer: Provide progress updates in messages counts rather than just percentage...
authorMarco Bettini <marco.bettini@open-xchange.com>
Wed, 17 May 2023 08:01:33 +0000 (08:01 +0000)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Mon, 22 May 2023 09:21:43 +0000 (09:21 +0000)
src/indexer/Makefile.am
src/indexer/indexer.h
src/indexer/master-connection.c
src/indexer/worker-connection.c
src/lib-fts/Makefile.am
src/lib-fts/fts-indexer-status.h [new file with mode: 0644]

index 22d2843275e91b0ac32905d2753d230351c1273a..5dd02c96b91b9dcc7399e284c50b1fa5de9f21e9 100644 (file)
@@ -10,6 +10,7 @@ AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib-mail \
        -I$(top_srcdir)/src/lib-index \
        -I$(top_srcdir)/src/lib-storage \
+       -I$(top_srcdir)/src/lib-fts \
        -DPKG_RUNDIR=\""$(rundir)"\" \
        $(BINARY_CFLAGS)
 
index 2234f2db70e94d57517c57e1c2872574f1db0de3..17234e50f5864cdee5d77f9bb075881544f85bb9 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef INDEXER_H
 #define INDEXER_H
 
+#include "fts-indexer-status.h"
+
 struct indexer_request;
 
 /* percentage: -1 = failed, 0..99 = indexing in progress, 100 = done */
index 829986e1d9cced55566ee2e8508dee3ae22ccf75..53c8a4571b46c18be344aa751cc46e3dcac1bdb1 100644 (file)
@@ -15,6 +15,7 @@
 #include "mail-storage-service.h"
 #include "mail-search-build.h"
 #include "master-connection.h"
+#include "indexer.h"
 
 #include <unistd.h>
 
@@ -77,7 +78,6 @@ index_mailbox_precache(struct master_connection *conn, struct mailbox *box)
        struct mail *mail;
        struct mailbox_metadata metadata;
        uint32_t seq, first_uid = 0, last_uid = 0;
-       char percentage_str[2+1+1];
        int ret = 0;
        struct event *index_event = event_create(box->event);
        event_add_category(index_event, &event_category_indexer_worker);
@@ -114,7 +114,7 @@ index_mailbox_precache(struct master_connection *conn, struct mailbox *box)
 
        unsigned int counter = 0;
        unsigned int percentage_sent = 0;
-       unsigned int max = status.messages + 1 - seq;
+       unsigned int goal = status.messages + 1 - seq;
        while (mailbox_search_next(ctx, &mail)) {
                if (first_uid == 0)
                        first_uid = mail->uid;
@@ -129,19 +129,17 @@ index_mailbox_precache(struct master_connection *conn, struct mailbox *box)
                        ret = -1;
                        break;
                }
-               unsigned int percentage = (++counter * 100) / max;
+               unsigned int percentage = (++counter * 100) / goal;
                if (percentage_sent < percentage) {
                        percentage_sent = percentage;
-                       if (percentage < 100) {
-                               if (i_snprintf(percentage_str,
-                                              sizeof(percentage_str), "%u\n",
-                                              percentage) < 0)
-                                       i_unreached();
-                               o_stream_nsend_str(conn->conn.output,
-                                                  percentage_str);
-                       }
+                       if (percentage < 100) T_BEGIN {
+                               const char *update = t_strdup_printf(
+                                       "%d\t%u\t%u\n", INDEXER_STATE_PROCESSING,
+                                       counter, goal);
+                               o_stream_nsend_str(conn->conn.output, update);
+                       } T_END;
                        indexer_worker_refresh_proctitle(username, box_vname,
-                                                        counter, max);
+                                                        counter, goal);
                }
        }
        if (mailbox_search_deinit(&ctx) < 0) {
@@ -314,7 +312,6 @@ master_connection_input_args(struct connection *_conn, const char *const *args)
 {
        struct master_connection *conn =
                container_of(_conn, struct master_connection, conn);
-       const char *str;
        unsigned int max_recent_msgs;
        int ret;
 
@@ -333,7 +330,8 @@ master_connection_input_args(struct connection *_conn, const char *const *args)
        ret = master_connection_cmd_index(conn, username, mailbox, session_id,
                                          max_recent_msgs, what);
 
-       str = ret < 0 ? "-1\n" : "100\n";
+       const char *str = t_strdup_printf("%d\n",
+               ret < 0 ? INDEXER_STATE_FAILED: INDEXER_STATE_COMPLETED);
        o_stream_nsend_str(conn->conn.output, str);
        return ret;
 }
index b77ab05c5e9e87aee29b607809e50ea3389336b1..6566edd8eb795b58d80f5f686396e0364a7f4646 100644 (file)
@@ -96,17 +96,42 @@ worker_connection_input_args(struct connection *conn, const char *const *args)
 {
        struct worker_connection *worker =
                container_of(conn, struct worker_connection, conn);
-       int percentage;
+       unsigned int progress = 0, total = 0;
+       int state;
        int ret = 1;
 
-       if (str_to_int(args[0], &percentage) < 0 ||
-           percentage < -1 || percentage > 100) {
-               e_error(conn->event, "Worker sent invalid progress '%s'", args[0]);
+       if (str_to_int(args[0], &state) < 0 ||
+           state < INDEXER_STATE_FAILED || state > INDEXER_STATE_COMPLETED) {
+               e_error(conn->event, "Worker sent invalid state '%s'", args[0]);
                return -1;
        }
 
-       if (percentage < 0)
+       if (state == INDEXER_STATE_FAILED)
                ret = -1;
+       else if (args[1] != NULL && args[2] != NULL) {
+               if (str_to_uint32(args[1], &progress) < 0) {
+                       e_error(conn->event, "Worker sent invalid progress '%s'", args[1]);
+                       return -1;
+               }
+
+               if (str_to_uint32(args[2], &total) < 0) {
+                       e_error(conn->event, "Worker sent invalid total '%s'", args[2]);
+                       return -1;
+               }
+       }
+
+       unsigned int percentage;
+       switch (state) {
+       case INDEXER_STATE_FAILED:
+               percentage = -1;
+               break;
+       case INDEXER_STATE_COMPLETED:
+               percentage = 100;
+               break;
+       case INDEXER_STATE_PROCESSING:
+               percentage = total == 0 ? 0 : progress * 100 / total;
+               break;
+       }
 
        worker_connection_call_callback(worker, percentage);
        if (worker->request == NULL) {
index bf89a49dfe66b1d886a2a640bca57aba6cfa75f9..6ddc35b2ca2b65b4c3b31d0a4cb79952ec414bb6 100644 (file)
@@ -105,7 +105,8 @@ headers = \
        fts-tokenizer.h \
        fts-tokenizer-common.h \
        fts-tokenizer-private.h \
-       fts-tokenizer-generic-private.h
+       fts-tokenizer-generic-private.h \
+       fts-indexer-status.h
 
 pkginc_libdir=$(pkgincludedir)
 pkginc_lib_HEADERS = $(headers)
diff --git a/src/lib-fts/fts-indexer-status.h b/src/lib-fts/fts-indexer-status.h
new file mode 100644 (file)
index 0000000..5c4d6c7
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef FTS_INDEXER_STATUS_H
+#define FTS_INDEXER_STATUS_H
+
+enum indexer_state {
+       INDEXER_STATE_PROCESSING =  0,
+       INDEXER_STATE_COMPLETED  =  1,
+       INDEXER_STATE_FAILED     = -1,
+};
+
+struct indexer_status {
+       enum indexer_state state;
+       unsigned int progress;
+       unsigned int total;
+};
+
+#endif