Skip to content

Commit

Permalink
Regain some control on labels of host cache
Browse files Browse the repository at this point in the history
CMK-14467

Change-Id: I1d59e8d80fee13b6b2ea881af1da4f31d1aad061
  • Loading branch information
Synss committed Oct 23, 2023
1 parent a2e4b09 commit 5a70827
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmk/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions cmk/utils/rulesets/ruleset_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5a70827

Please sign in to comment.