-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/plugin registry cache adjustments #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
graepaul
wants to merge
6
commits into
development
Choose a base branch
from
feature/plugin_registry_cache_adjustments
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d6e21f
Quick self-review and adjustments
graepaul 890cfb0
Adding tests
graepaul 95662cd
Updating to work with 3.9
graepaul 290f842
Update Tests
graepaul 3403976
Merge branch 'development' into feature/plugin_registry_cache_adjustm…
alexandraBara 718b988
Merge branch 'development' into feature/plugin_registry_cache_adjustm…
graepaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,105 +4,153 @@ | |||||
| # | ||||||
| # Copyright (c) 2025 Advanced Micro Devices, Inc. | ||||||
| # | ||||||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
| # of this software and associated documentation files (the "Software"), to deal | ||||||
| # in the Software without restriction, including without limitation the rights | ||||||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
| # copies of the Software, and to permit persons to whom the Software is | ||||||
| # furnished to do so, subject to the following conditions: | ||||||
| # | ||||||
| # The above copyright notice and this permission notice shall be included in all | ||||||
| # copies or substantial portions of the Software. | ||||||
| # | ||||||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||
| # SOFTWARE. | ||||||
| # | ||||||
| ############################################################################### | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| from typing import Iterable | ||||||
|
|
||||||
|
|
||||||
| def registered_plugin_names() -> tuple[str, ...]: | ||||||
| """Return all plugin names known to :class:`~nodescraper.pluginregistry.PluginRegistry`. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted registered plugin names. | ||||||
| """ | ||||||
| from nodescraper.pluginregistry import PluginRegistry | ||||||
|
|
||||||
| return tuple(sorted(PluginRegistry().plugins)) | ||||||
|
|
||||||
|
|
||||||
| def plugin_names_matching(names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Return plugin names from ``names`` that are registered at runtime. | ||||||
|
|
||||||
| Args: | ||||||
| names (Iterable[str]): Candidate plugin names to filter. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted subset of ``names`` present in the plugin registry. | ||||||
| """ | ||||||
| available = set(registered_plugin_names()) | ||||||
| return tuple(sorted(name for name in names if name in available)) | ||||||
|
|
||||||
| from __future__ import annotations | ||||||
|
|
||||||
| def load_plugin_class(plugin_name: str) -> type | None: | ||||||
| """Return a registered plugin class by name. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_name (str): Registered plugin name. | ||||||
|
|
||||||
| Returns: | ||||||
| type | None: Plugin class, or ``None`` if the name is not registered. | ||||||
| """ | ||||||
| from nodescraper.pluginregistry import PluginRegistry | ||||||
|
|
||||||
| return PluginRegistry().plugins.get(plugin_name) | ||||||
|
|
||||||
|
|
||||||
| def plugin_has_collector(plugin_name: str) -> bool: | ||||||
| """Return whether the plugin exposes a collector task. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_name (str): Registered plugin name. | ||||||
| import threading | ||||||
| from typing import TYPE_CHECKING, Iterable | ||||||
|
|
||||||
| Returns: | ||||||
| bool: ``True`` when the plugin class defines ``COLLECTOR``. | ||||||
| """ | ||||||
| plugin_class = load_plugin_class(plugin_name) | ||||||
| if plugin_class is None: | ||||||
| return False | ||||||
| collectors = getattr(plugin_class, "get_collector_classes", None) | ||||||
| if callable(collectors): | ||||||
| return bool(collectors()) | ||||||
| collector = getattr(plugin_class, "COLLECTOR", None) | ||||||
| if collector is None: | ||||||
| return False | ||||||
| if isinstance(collector, (tuple, list)): | ||||||
| return len(collector) > 0 | ||||||
| return True | ||||||
|
|
||||||
|
|
||||||
| def plugin_has_analyzer(plugin_name: str) -> bool: | ||||||
| """Return whether the plugin exposes an analyzer task. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_name (str): Registered plugin name. | ||||||
|
|
||||||
| Returns: | ||||||
| bool: ``True`` when the plugin class defines ``ANALYZER``. | ||||||
| """ | ||||||
| plugin_class = load_plugin_class(plugin_name) | ||||||
| return plugin_class is not None and getattr(plugin_class, "ANALYZER", None) is not None | ||||||
| from nodescraper.pluginregistry import PluginRegistry | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| from nodescraper.interfaces import PluginInterface | ||||||
|
|
||||||
| def plugins_with_collector(plugin_names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Filter plugin names to those that define a collector. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_names (Iterable[str]): Candidate plugin names. | ||||||
| class PluginDiscovery: | ||||||
| """Allows for the discovery of plugins and their capabilities. These external plugins must be | ||||||
| registered with the :class:`~nodescraper.pluginregistry.PluginRegistry` before they can be discovered. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted plugin names with a ``COLLECTOR`` implementation. | ||||||
| This class can use a cache to avoid repeated PluginRegistry lookups, which can be expensive. | ||||||
| If use_cache is False, it will query the PluginRegistry each time. | ||||||
| """ | ||||||
| return tuple(sorted(name for name in plugin_names if plugin_has_collector(name))) | ||||||
|
|
||||||
|
|
||||||
| def plugins_with_analyzer(plugin_names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Filter plugin names to those that define an analyzer. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_names (Iterable[str]): Candidate plugin names. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted plugin names with an ``ANALYZER`` implementation. | ||||||
| """ | ||||||
| return tuple(sorted(name for name in plugin_names if plugin_has_analyzer(name))) | ||||||
| _plugin_cache: dict[str, type[PluginInterface]] | None = None | ||||||
| _cache_lock = threading.Lock() | ||||||
| COLLECTOR_ATTRIBUTE = "COLLECTOR" | ||||||
| ANALYZER_ATTRIBUTE = "ANALYZER" | ||||||
|
|
||||||
| def __init__(self, use_cache: bool = True) -> None: | ||||||
| """Initialize the PluginDiscovery instance. | ||||||
|
|
||||||
| Args: | ||||||
| use_cache: If True, cache plugin lookups to improve performance. Defaults to True. | ||||||
| """ | ||||||
| self._use_cache = use_cache | ||||||
|
|
||||||
| def load_plugin_class(self, plugin_name: str) -> type | None: | ||||||
| if not self._use_cache: | ||||||
| return PluginRegistry().plugins.get(plugin_name) | ||||||
|
|
||||||
| if PluginDiscovery._plugin_cache is None: | ||||||
| with PluginDiscovery._cache_lock: | ||||||
| if PluginDiscovery._plugin_cache is None: | ||||||
| PluginDiscovery._plugin_cache = PluginRegistry().plugins | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied this change, also moved access inside of lock. @@ -60,12 +60,10 @@ class PluginDiscovery:
if not self._use_cache:
return PluginRegistry().plugins.get(plugin_name)
- if PluginDiscovery._plugin_cache is None:
- with PluginDiscovery._cache_lock:
- if PluginDiscovery._plugin_cache is None:
- PluginDiscovery._plugin_cache = PluginRegistry().plugins
-
- return PluginDiscovery._plugin_cache.get(plugin_name)
+ with PluginDiscovery._cache_lock:
+ if PluginDiscovery._plugin_cache is None:
+ PluginDiscovery._plugin_cache = PluginRegistry().plugins.copy()
+ return PluginDiscovery._plugin_cache.get(plugin_name) |
||||||
|
|
||||||
| return PluginDiscovery._plugin_cache.get(plugin_name) | ||||||
|
|
||||||
| def plugin_has_collector(self, plugin_name: str) -> bool: | ||||||
| """Check if a plugin has a COLLECTOR attribute. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_name: The name of the plugin to check. | ||||||
|
|
||||||
| Returns: | ||||||
| True if the plugin exists and has a COLLECTOR attribute, False otherwise. | ||||||
| """ | ||||||
| plugin_class = self.load_plugin_class(plugin_name) | ||||||
| return ( | ||||||
| plugin_class is not None | ||||||
| and getattr(plugin_class, self.COLLECTOR_ATTRIBUTE, None) is not None | ||||||
| ) | ||||||
|
|
||||||
| def plugin_has_analyzer(self, plugin_name: str) -> bool: | ||||||
| """Check if a plugin has an ANALYZER attribute. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_name: The name of the plugin to check. | ||||||
|
|
||||||
| Returns: | ||||||
| True if the plugin exists and has an ANALYZER attribute, False otherwise. | ||||||
| """ | ||||||
| plugin_class = self.load_plugin_class(plugin_name) | ||||||
| return ( | ||||||
| plugin_class is not None | ||||||
| and getattr(plugin_class, self.ANALYZER_ATTRIBUTE, None) is not None | ||||||
| ) | ||||||
|
|
||||||
| def plugins_with_collector(self, plugin_names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Filter a list of plugin names to those that have a COLLECTOR attribute. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_names: An iterable of plugin names to filter. | ||||||
|
|
||||||
| Returns: | ||||||
| A sorted tuple of plugin names that have a COLLECTOR attribute. | ||||||
| """ | ||||||
| return tuple(sorted(name for name in plugin_names if self.plugin_has_collector(name))) | ||||||
|
|
||||||
| def plugins_with_analyzer(self, plugin_names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Filter a list of plugin names to those that have an ANALYZER attribute. | ||||||
|
|
||||||
| Args: | ||||||
| plugin_names: An iterable of plugin names to filter. | ||||||
|
|
||||||
| Returns: | ||||||
| A sorted tuple of plugin names that have an ANALYZER attribute. | ||||||
| """ | ||||||
| return tuple(sorted(name for name in plugin_names if self.plugin_has_analyzer(name))) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def clear_cache() -> None: | ||||||
| """Clears the plugin cache, forcing future lookups to query the PluginRegistry again. | ||||||
|
|
||||||
| Thread-safe: Acquires the cache lock to ensure no other thread is accessing the cache. | ||||||
| """ | ||||||
| with PluginDiscovery._cache_lock: | ||||||
| PluginDiscovery._plugin_cache = None | ||||||
|
|
||||||
| def registered_plugin_names(self) -> tuple[str, ...]: | ||||||
| """Return all plugin names known to :class:`~nodescraper.pluginregistry.PluginRegistry`. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted registered plugin names. | ||||||
| """ | ||||||
| if not self._use_cache: | ||||||
| return tuple(sorted(PluginRegistry().plugins.keys())) | ||||||
|
|
||||||
| if PluginDiscovery._plugin_cache is None: | ||||||
| with PluginDiscovery._cache_lock: | ||||||
| if PluginDiscovery._plugin_cache is None: | ||||||
| PluginDiscovery._plugin_cache = PluginRegistry().plugins | ||||||
| return tuple(sorted(PluginDiscovery._plugin_cache.keys())) | ||||||
|
|
||||||
| def plugin_names_matching(self, names: Iterable[str]) -> tuple[str, ...]: | ||||||
| """Return plugin names from ``names`` that are registered at runtime. | ||||||
|
|
||||||
| Args: | ||||||
| names (Iterable[str]): Candidate plugin names to filter. | ||||||
|
|
||||||
| Returns: | ||||||
| tuple[str, ...]: Sorted subset of ``names`` present in the plugin registry. | ||||||
| """ | ||||||
| available = set(self.registered_plugin_names()) | ||||||
| return tuple(sorted(name for name in names if name in available)) | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this read should be inside the lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is now