]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
conf: add adaptive lmdb readers setting based on current configuration
authorDaniel Salzman <daniel.salzman@nic.cz>
Thu, 22 Jul 2021 13:28:24 +0000 (15:28 +0200)
committerDaniel Salzman <daniel.salzman@nic.cz>
Fri, 23 Jul 2021 05:48:47 +0000 (07:48 +0200)
src/knot/conf/base.h
src/knot/conf/conf.c
src/knot/conf/conf.h
src/knot/conf/schema.c
src/knot/journal/knot_lmdb.c
src/knot/server/server.c

index d87ed12238cb698f114328d81cb1779298c76057..9cb1a4c7cfd1cd61617ca8cf4a521d7d77ab13cb 100644 (file)
 #define CONF_DEFAULT_DBDIR     (STORAGE_DIR "/confdb")
 /*! Maximum depth of nested transactions. */
 #define CONF_MAX_TXN_DEPTH     5
+
+/*! Maximum number of UDP workers. */
+#define CONF_MAX_UDP_WORKERS   256
+/*! Maximum number of TCP workers. */
+#define CONF_MAX_TCP_WORKERS   256
+/*! Maximum number of background workers. */
+#define CONF_MAX_BG_WORKERS    512
 /*! Maximum number of concurrent DB readers. */
-#define CONF_MAX_DB_READERS    630
+#define CONF_MAX_DB_READERS    (CONF_MAX_UDP_WORKERS + CONF_MAX_TCP_WORKERS + \
+                                CONF_MAX_BG_WORKERS + 128 /* XDP workers */)
 
 /*! Configuration specific logging. */
 #define CONF_LOG(severity, msg, ...) do { \
index fb626d28ce41af67db2a07726ba380a841c4cf04..35595bb4b3b54dedaf986665811cb329c8655d8e 100644 (file)
@@ -40,8 +40,8 @@
 
 #define DBG_LOG(err) CONF_LOG(LOG_DEBUG, "%s (%s)", __func__, knot_strerror((err)));
 
-#define DFLT_TCP_WORKERS_MIN           10
-#define DFLT_BG_WORKERS_MAX            10
+#define DFLT_MIN_TCP_WORKERS           10
+#define DFLT_MAX_BG_WORKERS            10
 #define FALLBACK_MAX_TCP_CLIENTS       100
 
 bool conf_db_exists(
@@ -1142,8 +1142,9 @@ size_t conf_udp_threads_txn(
 {
        conf_val_t val = conf_get_txn(conf, txn, C_SRV, C_UDP_WORKERS);
        int64_t workers = conf_int(&val);
+       assert(workers <= CONF_MAX_UDP_WORKERS);
        if (workers == YP_NIL) {
-               return dt_optimal_size();
+               return MIN(dt_optimal_size(), CONF_MAX_UDP_WORKERS);
        }
 
        return workers;
@@ -1155,8 +1156,10 @@ size_t conf_tcp_threads_txn(
 {
        conf_val_t val = conf_get_txn(conf, txn, C_SRV, C_TCP_WORKERS);
        int64_t workers = conf_int(&val);
+       assert(workers <= CONF_MAX_TCP_WORKERS);
        if (workers == YP_NIL) {
-               return MAX(dt_optimal_size(), DFLT_TCP_WORKERS_MIN);
+               size_t optimal = MAX(dt_optimal_size(), DFLT_MIN_TCP_WORKERS);
+               return MIN(optimal, CONF_MAX_TCP_WORKERS);
        }
 
        return workers;
@@ -1191,8 +1194,10 @@ size_t conf_bg_threads_txn(
 {
        conf_val_t val = conf_get_txn(conf, txn, C_SRV, C_BG_WORKERS);
        int64_t workers = conf_int(&val);
+       assert(workers <= CONF_MAX_BG_WORKERS);
        if (workers == YP_NIL) {
-               return MIN(dt_optimal_size(), DFLT_BG_WORKERS_MAX);
+               assert(DFLT_MAX_BG_WORKERS <= CONF_MAX_BG_WORKERS);
+               return MIN(dt_optimal_size(), DFLT_MAX_BG_WORKERS);
        }
 
        return workers;
index 9f257e6fd1a1163b492049626251e2eb5c088e32..68463a10c6a1343fa7e7699706bd13643d078849 100644 (file)
@@ -749,6 +749,28 @@ static inline size_t conf_bg_threads(
        return conf_bg_threads_txn(conf, &conf->read_txn);
 }
 
+/*!
+ * Gets the required LMDB readers limit based on the current configuration.
+ *
+ * \note The resulting value is a common limit to journal, kasp, timers,
+ *       and catalog databases. So it's over-estimated for simpicity reasons.
+ *
+ * \note This function cannot be used for the configuration database setting :-/
+ *
+ * \param[in] conf  Configuration.
+ *
+ * \return Number of readers.
+ */
+static inline size_t conf_lmdb_readers(
+       conf_t *conf)
+{
+       if (conf == NULL) { // Return default in tests.
+               return 126;
+       }
+       return conf_udp_threads(conf) + conf_tcp_threads(conf) +
+              conf_bg_threads(conf) + conf_xdp_threads(conf);
+}
+
 /*!
  * Gets the configured maximum number of TCP clients.
  *
index b26ef0a70ecd84df9dac887603c5eee277f15872..d87548b0de4f2dce4e7a3484b9425bf6fb3b3283 100644 (file)
@@ -184,9 +184,9 @@ static const yp_item_t desc_server[] = {
        { C_RUNDIR,               YP_TSTR,  YP_VSTR = { RUN_DIR } },
        { C_USER,                 YP_TSTR,  YP_VNONE },
        { C_PIDFILE,              YP_TSTR,  YP_VSTR = { "knot.pid" } },
-       { C_UDP_WORKERS,          YP_TINT,  YP_VINT = { 1, 255, YP_NIL } },
-       { C_TCP_WORKERS,          YP_TINT,  YP_VINT = { 1, 255, YP_NIL } },
-       { C_BG_WORKERS,           YP_TINT,  YP_VINT = { 1, 255, YP_NIL } },
+       { C_UDP_WORKERS,          YP_TINT,  YP_VINT = { 1, CONF_MAX_UDP_WORKERS, YP_NIL } },
+       { C_TCP_WORKERS,          YP_TINT,  YP_VINT = { 1, CONF_MAX_TCP_WORKERS, YP_NIL } },
+       { C_BG_WORKERS,           YP_TINT,  YP_VINT = { 1, CONF_MAX_BG_WORKERS, YP_NIL } },
        { C_ASYNC_START,          YP_TBOOL, YP_VNONE },
        { C_TCP_IDLE_TIMEOUT,     YP_TINT,  YP_VINT = { 1, INT32_MAX, 10, YP_STIME } },
        { C_TCP_IO_TIMEOUT,       YP_TINT,  YP_VINT = { 0, INT32_MAX, 500 } },
index 0fcc8850eef4fbe6ac808d9c9da68b144e9dd5c5..4d3e49366fccc65c418bd9df98d4e25f2426fc61 100644 (file)
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-#include "knot/journal/knot_lmdb.h"
-
 #include <stdarg.h>
 #include <stdio.h> // snprintf
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "knot/journal/knot_lmdb.h"
+
+#include "knot/conf/conf.h"
 #include "contrib/files.h"
 #include "contrib/wire_ctx.h"
 #include "libknot/dname.h"
@@ -79,7 +80,7 @@ void knot_lmdb_init(knot_lmdb_db_t *db, const char *path, size_t mapsize, unsign
        db->dbname = dbname;
        pthread_mutex_init(&db->opening_mutex, NULL);
        db->maxdbs = 2;
-       db->maxreaders = 1278;
+       db->maxreaders = conf_lmdb_readers(conf());
 }
 
 static bool lmdb_stat(const char *lmdb_path, struct stat *st)
index d3d031fa455fed65ffa03a048e7d474c575c2e10..90973edf8e4256a076a521df1f9b85c549a196ea 100644 (file)
@@ -1167,6 +1167,10 @@ int server_reconfigure(conf_t *conf, server_t *server)
                if ((ret = configure_sockets(conf, server)) != KNOT_EOK) {
                        return ret;
                }
+
+               if (conf_lmdb_readers(conf) > CONF_MAX_DB_READERS) {
+                       log_warning("config, exceeded number of database readers");
+               }
        }
 
        /* Reconfigure journal DB. */