diff --git a/cmk/base/config.py b/cmk/base/config.py index 06ec7eecd8b..bd76c125052 100644 --- a/cmk/base/config.py +++ b/cmk/base/config.py @@ -2949,6 +2949,7 @@ def _impl() -> Mapping[str, object]: def labels(self, host_name: HostName) -> Labels: with contextlib.suppress(KeyError): + # TODO(ml): Also cached in `RulesetOptimizer.labels_of_host(HostName) -> Labels`. return self.__labels[host_name] return self.__labels.setdefault(host_name, self.ruleset_matcher.labels_of_host(host_name)) diff --git a/cmk/utils/rulesets/ruleset_matcher.py b/cmk/utils/rulesets/ruleset_matcher.py index 54a3d9836cd..e861f5b30cf 100644 --- a/cmk/utils/rulesets/ruleset_matcher.py +++ b/cmk/utils/rulesets/ruleset_matcher.py @@ -12,7 +12,6 @@ from typing_extensions import TypedDict -from cmk.utils.caching import instance_method_lru_cache from cmk.utils.hostaddress import HostAddress, HostName from cmk.utils.labels import BuiltinHostLabelsStore, DiscoveredHostLabelsStore, HostLabel, Labels from cmk.utils.parameters import boil_down_parameters @@ -449,6 +448,7 @@ def __init__( nodes_of: Mapping[HostName, Sequence[HostName]], ) -> None: super().__init__() + self.__labels_of_host: dict[HostName, Labels] = {} self._ruleset_matcher = ruleset_matcher self._labels = labels self._host_tags = {hn: set(tags_of_host.items()) for hn, tags_of_host in host_tags.items()} @@ -880,7 +880,6 @@ def _initialize_host_lookup(self) -> None: self._hosts_grouped_by_tags.setdefault(group_ref, set()).add(hostname) self._host_grouped_ref[hostname] = group_ref - @instance_method_lru_cache(maxsize=None) def labels_of_host(self, hostname: HostName) -> Labels: """Returns the effective set of host labels from all available sources @@ -891,12 +890,16 @@ def labels_of_host(self, hostname: HostName) -> Labels: Last one wins. """ + with contextlib.suppress(KeyError): + # Also cached in `ConfigCache.labels(HostName) -> Labels` + return self.__labels_of_host[hostname] + labels: dict[str, str] = {} labels.update(self._discovered_labels_of_host(hostname)) labels.update(self._ruleset_labels_of_host(hostname)) labels.update(self._labels.explicit_host_labels.get(hostname, {})) labels.update(self._builtin_labels_of_host(hostname)) - return labels + return self.__labels_of_host.setdefault(hostname, labels) def label_sources_of_host(self, hostname: HostName) -> LabelSources: """Returns the effective set of host label keys with their source