]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: datamodel: workers moved to top-level node
authorAleš Mrázek <ales.mrazek@nic.cz>
Wed, 4 May 2022 08:52:16 +0000 (10:52 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Wed, 4 May 2022 08:52:16 +0000 (10:52 +0200)
manager/etc/knot-resolver/config.dev.yml
manager/knot_resolver_manager/datamodel/config_schema.py
manager/knot_resolver_manager/datamodel/server_schema.py
manager/knot_resolver_manager/kres_manager.py

index e51dada996a1c1ab38c627dacb84cb3aad83d70f..715f2209a38c1f24485abe53ff7a0d16d70306d2 100644 (file)
@@ -1,7 +1,7 @@
 id: dev
 rundir: etc/knot-resolver/runtime
+workers: 1
 server:
-  workers: 1
   management:
     interface: 127.0.0.1@5000
 cache:
index 742a6e15f2bc89f3aa2cc2f5d69e8b99f3743630..dc43038619820fe2c29d65daf6bd96cd5e330957 100644 (file)
@@ -1,7 +1,8 @@
+import logging
 import os
 import socket
 import sys
-from typing import Dict, Optional, Union
+from typing import Any, Dict, Optional, Union
 
 from jinja2 import Environment, FileSystemLoader, Template
 from typing_extensions import Literal
@@ -22,10 +23,13 @@ from knot_resolver_manager.datamodel.static_hints_schema import StaticHintsSchem
 from knot_resolver_manager.datamodel.stub_zone_schema import StubZoneSchema
 from knot_resolver_manager.datamodel.supervisor_schema import SupervisorSchema
 from knot_resolver_manager.datamodel.types import DomainName
-from knot_resolver_manager.datamodel.types.types import IDPattern, UncheckedPath
+from knot_resolver_manager.datamodel.types.types import IDPattern, IntPositive, UncheckedPath
 from knot_resolver_manager.datamodel.view_schema import ViewSchema
+from knot_resolver_manager.exceptions import DataException
 from knot_resolver_manager.utils import SchemaNode
 
+logger = logging.getLogger(__name__)
+
 
 def _get_templates_dir() -> str:
     module = sys.modules["knot_resolver_manager.datamodel"].__file__
@@ -56,6 +60,24 @@ def _import_lua_template() -> Template:
 _MAIN_TEMPLATE = _import_lua_template()
 
 
+def _cpu_count() -> int:
+    try:
+        return len(os.sched_getaffinity(0))
+    except (NotImplementedError, AttributeError):
+        logger.warning(
+            "The number of usable CPUs could not be determined using 'os.sched_getaffinity()'."
+            "Attempting to get the number of system CPUs using 'os.cpu_count()'"
+        )
+        cpus = os.cpu_count()
+        if cpus is None:
+            raise DataException(
+                "The number of available CPUs to automatically set the number of running"
+                "'kresd' workers could not be determined."
+                "The number can be specified manually in 'server:instances' configuration option."
+            )
+        return cpus
+
+
 class KresConfig(SchemaNode):
     class Raw(SchemaNode):
         """
@@ -63,8 +85,9 @@ class KresConfig(SchemaNode):
 
         ---
         id: System-wide unique identifier of this instance. Used for grouping logs and tagging workers.
-        hostname: Internal DNS resolver hostname. Default is machine hostname.
         rundir: Directory where the resolver can create files and which will be it's cwd.
+        hostname: Internal DNS resolver hostname. Default is machine hostname.
+        workers: The number of running kresd (Knot Resolver daemon) workers. If set to 'auto', it is equal to number of CPUs available.
         server: DNS server control and management configuration.
         supervisor: Proceses supervisor configuration.
         options: Fine-tuning global parameters of DNS resolver operation.
@@ -84,8 +107,9 @@ class KresConfig(SchemaNode):
         """
 
         id: IDPattern
-        hostname: Optional[str] = None
         rundir: UncheckedPath = UncheckedPath(".")
+        hostname: Optional[str] = None
+        workers: Union[Literal["auto"], IntPositive] = IntPositive(1)
         server: ServerSchema = ServerSchema()
         supervisor: SupervisorSchema = SupervisorSchema()
         options: OptionsSchema = OptionsSchema()
@@ -106,8 +130,9 @@ class KresConfig(SchemaNode):
     _PREVIOUS_SCHEMA = Raw
 
     id: IDPattern
-    hostname: str
     rundir: UncheckedPath
+    hostname: str
+    workers: IntPositive
     server: ServerSchema
     supervisor: SupervisorSchema
     options: OptionsSchema
@@ -125,21 +150,35 @@ class KresConfig(SchemaNode):
     monitoring: MonitoringSchema
     lua: LuaSchema
 
-    def _hostname(self, obj: Raw) -> str:
+    def _hostname(self, obj: Raw) -> Any:
         if obj.hostname is None:
             return socket.gethostname()
         return obj.hostname
 
-    def _dnssec(self, obj: Raw) -> Union[Literal[False], DnssecSchema]:
+    def _workers(self, obj: Raw) -> Any:
+        if obj.workers == "auto":
+            return IntPositive(_cpu_count())
+        return obj.workers
+
+    def _dnssec(self, obj: Raw) -> Any:
         if obj.dnssec is True:
             return DnssecSchema()
         return obj.dnssec
 
-    def _dns64(self, obj: Raw) -> Union[Literal[False], Dns64Schema]:
+    def _dns64(self, obj: Raw) -> Any:
         if obj.dns64 is True:
             return Dns64Schema()
         return obj.dns64
 
+    def _validate(self) -> None:
+        try:
+            cpu_count = _cpu_count()
+            if int(self.workers) > 10 * cpu_count:
+                raise ValueError("refusing to run with more then instances 10 instances per cpu core")
+        except DataException:
+            # sometimes, we won't be able to get information about the cpu count
+            pass
+
     def render_lua(self) -> str:
         # FIXME the `cwd` argument is used only for configuring control socket path
         # it should be removed and relative path used instead as soon as issue
index ef6ea5a0d64c7e90c09b80b05bd977e78cd2eb32..59937c8c41df8f1f723d9f2a403d47482517f323 100644 (file)
@@ -1,33 +1,8 @@
-import logging
-import os
-from typing import Any, Optional, Union
+from typing import Optional
 
-from typing_extensions import Literal
-
-from knot_resolver_manager.datamodel.types import CheckedPath, InterfacePort, IntPositive, IPAddressPort
-from knot_resolver_manager.exceptions import DataException
+from knot_resolver_manager.datamodel.types import CheckedPath, InterfacePort, IPAddressPort
 from knot_resolver_manager.utils import SchemaNode
 
-logger = logging.getLogger(__name__)
-
-
-def _cpu_count() -> int:
-    try:
-        return len(os.sched_getaffinity(0))
-    except (NotImplementedError, AttributeError):
-        logger.warning(
-            "The number of usable CPUs could not be determined using 'os.sched_getaffinity()'."
-            "Attempting to get the number of system CPUs using 'os.cpu_count()'"
-        )
-        cpus = os.cpu_count()
-        if cpus is None:
-            raise DataException(
-                "The number of available CPUs to automatically set the number of running"
-                "'kresd' workers could not be determined."
-                "The number can be specified manually in 'server:instances' configuration option."
-            )
-        return cpus
-
 
 class ManagementSchema(SchemaNode):
     """
@@ -75,31 +50,14 @@ class ServerSchema(SchemaNode):
         DNS server control and management configuration.
 
         ---
-        workers: The number of running kresd (Knot Resolver daemon) workers. If set to 'auto', it is equal to number of CPUs available.
         management: Configuration of management HTTP API.
         webmgmt: Configuration of legacy web management endpoint.
         """
 
-        workers: Union[Literal["auto"], IntPositive] = IntPositive(1)
         management: ManagementSchema = ManagementSchema({"unix-socket": "./manager.sock"})
         webmgmt: Optional[WebmgmtSchema] = None
 
     _PREVIOUS_SCHEMA = Raw
 
-    workers: IntPositive
     management: ManagementSchema
     webmgmt: Optional[WebmgmtSchema]
-
-    def _workers(self, obj: Raw) -> Any:
-        if obj.workers == "auto":
-            return IntPositive(_cpu_count())
-        return obj.workers
-
-    def _validate(self) -> None:
-        try:
-            cpu_count = _cpu_count()
-            if int(self.workers) > 10 * cpu_count:
-                raise ValueError("refusing to run with more then instances 10 instances per cpu core")
-        except DataException:
-            # sometimes, we won't be able to get information about the cpu count
-            pass
index f770b6522f153572bb0e3363a6e3696b10b5e36e..040a6033f324da6c2ad335f98f740b460abceb0f 100644 (file)
@@ -182,7 +182,7 @@ class KresManager:  # pylint: disable=too-many-instance-attributes
         try:
             async with self._manager_lock:
                 logger.debug("Applying config to all workers")
-                await self._ensure_number_of_children(config, int(config.server.workers))
+                await self._ensure_number_of_children(config, int(config.workers))
                 await self._rolling_restart(config)
 
                 if self._is_gc_running() != config.cache.garbage_collector: