Skip to content
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

Cache plugin modules #9031

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20231107-191546.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Cache dbt plugin modules to improve integration test performance
time: 2023-11-07T19:15:46.170151-05:00
custom:
Author: peterallenwebb
Issue: "9029"
21 changes: 15 additions & 6 deletions core/dbt/plugins/manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import functools
import importlib
import pkgutil
from typing import Dict, List, Callable
from types import ModuleType
from typing import Dict, List, Callable, Mapping

from dbt.contracts.graph.manifest import Manifest
from dbt.exceptions import DbtRuntimeError
Expand Down Expand Up @@ -63,6 +65,17 @@ def get_manifest_artifacts(self, manifest: Manifest) -> PluginArtifacts:
raise NotImplementedError(f"get_manifest_artifacts hook not implemented for {self.name}")


@functools.lru_cache(maxsize=None)
def _get_dbt_modules() -> Mapping[str, ModuleType]:
# This is an expensive function, especially in the context of testing, when
# it is called repeatedly, so we break it out and cache the result globally.
return {
name: importlib.import_module(name)
for _, name, _ in pkgutil.iter_modules()
if name.startswith(PluginManager.PLUGIN_MODULE_PREFIX)
}


class PluginManager:
PLUGIN_MODULE_PREFIX = "dbt_"
PLUGIN_ATTR_NAME = "plugins"
Expand Down Expand Up @@ -91,11 +104,7 @@ def __init__(self, plugins: List[dbtPlugin]) -> None:

@classmethod
def from_modules(cls, project_name: str) -> "PluginManager":
discovered_dbt_modules = {
name: importlib.import_module(name)
for _, name, _ in pkgutil.iter_modules()
if name.startswith(cls.PLUGIN_MODULE_PREFIX)
}
discovered_dbt_modules = _get_dbt_modules()

plugins = []
for name, module in discovered_dbt_modules.items():
Expand Down