]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
datamodel: stabilize cache schema
authorAleš Mrázek <ales.mrazek@nic.cz>
Tue, 20 May 2025 13:12:21 +0000 (15:12 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Thu, 28 Aug 2025 10:48:54 +0000 (12:48 +0200)
NEWS
doc/_static/config.schema.json
python/knot_resolver/controller/supervisord/config_file.py
python/knot_resolver/datamodel/cache_schema.py
python/knot_resolver/manager/manager.py

diff --git a/NEWS b/NEWS
index 93b4b73c2077b12a188c2af9e54ee5be8c034247..a2ab490989c961d8aea92113461832c94023615f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,7 @@ Incompatible changes
   - /network/tls/auto-discovery
   - /webmgmt
 - Renamed/moved options in the declarative configuration model (YAML).
+  - /cache/garbage-collector -> /cache/garbage-collector/enabled
   - /dns64: true|false -> /dns64/enabled: true|false
   - /dns64/rev-ttl -> /dns64/reverse-ttl
   - /dnssec: true|false -> /dnssec/enabled: true|false
index 9d354a05a47404b86fc9b23197025736f1fc8826..fccd20f7a4e8b04c59d20ed6cd298fea5fe3af96 100644 (file)
                     "default": "100M"
                 },
                 "garbage-collector": {
-                    "anyOf": [
-                        {
-                            "description": "Configuration options of the cache garbage collector (kres-cache-gc).",
-                            "type": "object",
-                            "properties": {
-                                "interval": {
-                                    "type": "string",
-                                    "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
-                                    "description": "Time interval how often the garbage collector will be run.",
-                                    "default": "1s"
-                                },
-                                "threshold": {
-                                    "type": "integer",
-                                    "minimum": 0,
-                                    "maximum": 100,
-                                    "description": "Cache usage in percent that triggers the garbage collector.",
-                                    "default": 80
-                                },
-                                "release": {
-                                    "type": "integer",
-                                    "minimum": 0,
-                                    "maximum": 100,
-                                    "description": "Percent of used cache to be freed by the garbage collector.",
-                                    "default": 10
-                                },
-                                "temp-keys-space": {
-                                    "type": "string",
-                                    "pattern": "^(\\d+)(B|K|M|G)$",
-                                    "description": "Maximum amount of temporary memory for copied keys (0 = unlimited).",
-                                    "default": "0M"
-                                },
-                                "rw-deletes": {
-                                    "type": "integer",
-                                    "minimum": 0,
-                                    "description": "Maximum number of deleted records per read-write transaction (0 = unlimited).",
-                                    "default": 100
-                                },
-                                "rw-reads": {
-                                    "type": "integer",
-                                    "minimum": 0,
-                                    "description": "Maximum number of readed records per read-write transaction (0 = unlimited).",
-                                    "default": 200
-                                },
-                                "rw-duration": {
-                                    "type": "string",
-                                    "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
-                                    "description": "Maximum duration of read-write transaction (0 = unlimited).",
-                                    "default": "0us"
-                                },
-                                "rw-delay": {
-                                    "type": "string",
-                                    "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
-                                    "description": "Wait time between two read-write transactions.",
-                                    "default": "0us"
-                                },
-                                "dry-run": {
-                                    "type": "boolean",
-                                    "description": "Run the garbage collector in dry-run mode.",
-                                    "default": false
-                                }
-                            }
+                    "description": "Use the garbage collector (kres-cache-gc) to periodically clear cache.",
+                    "type": "object",
+                    "properties": {
+                        "enabled": {
+                            "type": "boolean",
+                            "description": "Enable/disable cache garbage collector.",
+                            "default": true
                         },
-                        {
+                        "interval": {
                             "type": "string",
-                            "enum": [
-                                false
-                            ]
+                            "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
+                            "description": "Time interval how often the garbage collector will be run.",
+                            "default": "1s"
+                        },
+                        "threshold": {
+                            "type": "integer",
+                            "minimum": 0,
+                            "maximum": 100,
+                            "description": "Cache usage in percent that triggers the garbage collector.",
+                            "default": 80
+                        },
+                        "release": {
+                            "type": "integer",
+                            "minimum": 0,
+                            "maximum": 100,
+                            "description": "Percent of used cache to be freed by the garbage collector.",
+                            "default": 10
+                        },
+                        "temp-keys-space": {
+                            "type": "string",
+                            "pattern": "^(\\d+)(B|K|M|G)$",
+                            "description": "Maximum amount of temporary memory for copied keys (0 = unlimited).",
+                            "default": "0M"
+                        },
+                        "rw-deletes": {
+                            "type": "integer",
+                            "minimum": 0,
+                            "description": "Maximum number of deleted records per read-write transaction (0 = unlimited).",
+                            "default": 100
+                        },
+                        "rw-reads": {
+                            "type": "integer",
+                            "minimum": 0,
+                            "description": "Maximum number of readed records per read-write transaction (0 = unlimited).",
+                            "default": 200
+                        },
+                        "rw-duration": {
+                            "type": "string",
+                            "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
+                            "description": "Maximum duration of read-write transaction (0 = unlimited).",
+                            "default": "0us"
+                        },
+                        "rw-delay": {
+                            "type": "string",
+                            "pattern": "^(\\d+)(us|ms|s|m|h|d)$",
+                            "description": "Wait time between two read-write transactions.",
+                            "default": "0us"
+                        },
+                        "dry-run": {
+                            "type": "boolean",
+                            "description": "Run the garbage collector in dry-run mode.",
+                            "default": false
                         }
-                    ],
-                    "description": "Use the garbage collector (kres-cache-gc) to periodically clear cache.",
+                    },
                     "default": {
+                        "enabled": true,
                         "interval": "1s",
                         "threshold": 80,
                         "release": 10,
                 "storage": "/var/cache/knot-resolver",
                 "size_max": "100M",
                 "garbage_collector": {
+                    "enabled": true,
                     "interval": "1s",
                     "threshold": 80,
                     "release": 10,
index 828bcbac69900690576df1e80c0b0ae1c560ecbd..004c81aa007b0f4b5647309b154b829f73d94a08 100644 (file)
@@ -58,21 +58,19 @@ def kres_cache_gc_args(config: KresConfig) -> str:
         args += " -v"
 
     gc_config = config.cache.garbage_collector
-    if gc_config:
-        args += (
-            f" -d {gc_config.interval.millis()}"
-            f" -u {gc_config.threshold}"
-            f" -f {gc_config.release}"
-            f" -l {gc_config.rw_deletes}"
-            f" -L {gc_config.rw_reads}"
-            f" -t {gc_config.temp_keys_space.mbytes()}"
-            f" -m {gc_config.rw_duration.micros()}"
-            f" -w {gc_config.rw_delay.micros()}"
-        )
-        if gc_config.dry_run:
-            args += " -n"
-        return args
-    raise ValueError("missing configuration for the cache garbage collector")
+    args += (
+        f" -d {gc_config.interval.millis()}"
+        f" -u {gc_config.threshold}"
+        f" -f {gc_config.release}"
+        f" -l {gc_config.rw_deletes}"
+        f" -L {gc_config.rw_reads}"
+        f" -t {gc_config.temp_keys_space.mbytes()}"
+        f" -m {gc_config.rw_duration.micros()}"
+        f" -w {gc_config.rw_delay.micros()}"
+    )
+    if gc_config.dry_run:
+        args += " -n"
+    return args
 
 
 @dataclass
index d40ee2a0f3cc129995c9f2672904d68f56690c3a..c616a3d78ea0bd56fbb1475d468731c4f505bd6c 100644 (file)
@@ -1,4 +1,4 @@
-from typing import List, Literal, Optional, Union
+from typing import List, Optional
 
 from knot_resolver.constants import CACHE_DIR
 from knot_resolver.datamodel.templates import template_from_str
@@ -62,6 +62,7 @@ class GarbageCollectorSchema(ConfigSchema):
     Configuration options of the cache garbage collector (kres-cache-gc).
 
     ---
+    enabled: Enable/disable cache garbage collector.
     interval: Time interval how often the garbage collector will be run.
     threshold: Cache usage in percent that triggers the garbage collector.
     release: Percent of used cache to be freed by the garbage collector.
@@ -73,6 +74,7 @@ class GarbageCollectorSchema(ConfigSchema):
     dry_run: Run the garbage collector in dry-run mode.
     """
 
+    enabled: bool = True
     interval: TimeUnit = TimeUnit("1s")
     threshold: Percent = Percent(80)
     release: Percent = Percent(10)
@@ -126,7 +128,7 @@ class CacheSchema(ConfigSchema):
 
     storage: WritableDir = lazy_default(WritableDir, str(CACHE_DIR))
     size_max: SizeUnit = SizeUnit("100M")
-    garbage_collector: Union[GarbageCollectorSchema, Literal[False]] = GarbageCollectorSchema()
+    garbage_collector: GarbageCollectorSchema = GarbageCollectorSchema()
     ttl_min: TimeUnit = TimeUnit("5s")
     ttl_max: TimeUnit = TimeUnit("1d")
     ns_timeout: TimeUnit = TimeUnit("1000ms")
index ac6539c4272554a30c73692673116cdb83b3c3ee..3acdd3d4c61b37fdfe473808c826a18fea083d7e 100644 (file)
@@ -291,8 +291,8 @@ class KresManager:  # pylint: disable=too-many-instance-attributes
                 await self._rolling_restart(config)
                 await self._ensure_number_of_children(config, int(config.workers))
 
-                if self._is_gc_running() != bool(config.cache.garbage_collector):
-                    if config.cache.garbage_collector:
+                if self._is_gc_running() != config.cache.garbage_collector.enabled:
+                    if config.cache.garbage_collector.enabled:
                         logger.debug("Starting cache GC")
                         await self._start_gc(config)
                     else: