]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
worker: migrate worker.id to string
authorPetr Špaček <petr.spacek@nic.cz>
Fri, 23 Oct 2020 15:48:41 +0000 (17:48 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Tue, 27 Oct 2020 10:25:54 +0000 (11:25 +0100)
It now contains either content of SYSTEMD_INSTANCE environment variable
or stringified version of getpid() value.

Main motivation is that the old worker.id was broken on systemd,
i.e. the default installation.

Related: #631

daemon/bindings/worker.rst
daemon/main.c
daemon/worker.c
daemon/worker.h
doc/upgrading.rst

index e530c845ca08f8735a78997c2fdcce376ec6dbdd..9dfcbe83a9b83a34a8c05a02a795fca3f702f046 100644 (file)
@@ -7,11 +7,15 @@ Worker is a service over event loop that tracks and schedules outstanding querie
 you can see the statistics or schedule new queries. It also contains information about
 specified worker count and process rank.
 
+.. envvar:: worker.id
+
+   Value from environment variable ``SYSTEMD_INSTANCE``,
+   or if it is not set, :envvar:`PID <worker.pid>` (string).
+
 .. envvar:: worker.pid
 
    Current worker process PID (number).
 
-
 .. function:: worker.stats()
 
    Return table of statistics.  See member descriptions in :c:type:`worker_stats`.
index cd83a976dfee2ca5dc3ee41b0f371cf4b4b15eef..28c998a750cae1e8275dc008a6cf65626e4ac175 100644 (file)
@@ -496,7 +496,7 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
        /* Initialize the worker */
-       ret = worker_init(&engine, fork_id, the_args->forks);
+       ret = worker_init(&engine, the_args->forks);
        if (ret != 0) {
                kr_log_error("[system] failed to initialize worker: %s\n", kr_strerror(ret));
                return EXIT_FAILURE;
index e4e1cdc38bbaec0ba1a8c8c51347e424384bcde9..c420350dd34659a6c40b500462f03999ecf60232 100644 (file)
@@ -10,6 +10,7 @@
 #include <lauxlib.h>
 #include <libknot/packet/pkt.h>
 #include <libknot/descriptor.h>
+#include <contrib/cleanup.h>
 #include <contrib/ucw/lib.h>
 #include <contrib/ucw/mempool.h>
 #include <contrib/wire.h>
@@ -2046,7 +2047,7 @@ void worker_deinit(void)
        the_worker = NULL;
 }
 
-int worker_init(struct engine *engine, int worker_id, int worker_count)
+int worker_init(struct engine *engine, int worker_count)
 {
        assert(engine && engine->L);
        assert(the_worker == NULL);
@@ -2060,7 +2061,6 @@ int worker_init(struct engine *engine, int worker_id, int worker_count)
        uv_loop_t *loop = uv_default_loop();
        worker->loop = loop;
 
-       worker->id = worker_id;
        worker->count = worker_count;
 
        /* Register table for worker per-request variables */
@@ -2080,9 +2080,20 @@ int worker_init(struct engine *engine, int worker_id, int worker_count)
 
        /* Set some worker.* fields in Lua */
        lua_getglobal(engine->L, "worker");
-       lua_pushnumber(engine->L, worker_id);
+       pid_t pid = getpid();
+
+       auto_free char *pid_str = NULL;
+       const char *inst_name = getenv("SYSTEMD_INSTANCE");
+       if (inst_name) {
+               lua_pushstring(engine->L, inst_name);
+       } else {
+               ret = asprintf(&pid_str, "%ld", (long)pid);
+               assert(ret > 0);
+               lua_pushstring(engine->L, pid_str);
+       }
        lua_setfield(engine->L, -2, "id");
-       lua_pushnumber(engine->L, getpid());
+
+       lua_pushnumber(engine->L, pid);
        lua_setfield(engine->L, -2, "pid");
        lua_pushnumber(engine->L, worker_count);
        lua_setfield(engine->L, -2, "count");
index 0d7f3f82edec8c250708c27405ed9d209b0abbcb..2e93617df3019e57f76de67412ff4e18d567cdbb 100644 (file)
@@ -23,7 +23,7 @@ KR_EXPORT extern struct worker_ctx *the_worker;
 
 /** Create and initialize the worker.
  * \return error code (ENOMEM) */
-int worker_init(struct engine *engine, int worker_id, int worker_count);
+int worker_init(struct engine *engine, int worker_count);
 
 /** Destroy the worker (free memory). */
 void worker_deinit(void);
@@ -152,8 +152,7 @@ typedef array_t(struct qr_task *) qr_tasklist_t;
 struct worker_ctx {
        struct engine *engine;
        uv_loop_t *loop;
-       int id;
-       int count;
+       int count;  /** unreliable, does not count systemd instance, do not use */
        int vars_table_ref;
        unsigned tcp_pipeline_max;
 
index 2c6d14e14b56b3c02fe7700cbf4a00f45847429c..4f11fc1bcab81979593873717832fa595abf7607 100644 (file)
@@ -36,12 +36,19 @@ newer versions when they are released.
 ==========
 
 Users
-~~~~~
+-----
 
 * Users of :ref:`control-sockets` API need to terminate each command sent to resolver with newline
   character (ASCII ``\n``). Correct usage: ``cache.stats()\n``.
   Newline terminated commands are accepted by all resolver versions >= 1.0.0.
 
+Configuration file
+------------------
+
+* Lua variable :envvar:`worker.id` is now a string with either Systemd instance name or PID
+  (instead of number). If your custom configuration uses :envvar:`worker.id` value please
+  check your scripts.
+
 Module changes
 --------------
 * Reply packet :c:type:`kr_request.answer`