From c4ff2804366d8cfdea3f7d90074c994b9504cd91 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 1 Nov 2023 18:44:59 -0400 Subject: [PATCH] remove dbt.flags.LOG_CACHE_EVENTS usage in dbt/adapters (#8933) --- .../unreleased/Under the Hood-20231101-173124.yaml | 6 ++++++ core/dbt/adapters/base/impl.py | 2 +- core/dbt/adapters/cache.py | 14 ++++++-------- core/dbt/adapters/contracts/connection.py | 1 + core/dbt/config/profile.py | 4 ++++ core/dbt/config/runtime.py | 2 ++ 6 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 .changes/unreleased/Under the Hood-20231101-173124.yaml diff --git a/.changes/unreleased/Under the Hood-20231101-173124.yaml b/.changes/unreleased/Under the Hood-20231101-173124.yaml new file mode 100644 index 00000000000..5a4656645f4 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20231101-173124.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: 'Remove usage of dbt.flags.LOG_CACHE_EVENTS in dbt/adapters' +time: 2023-11-01T17:31:24.974093-04:00 +custom: + Author: michelleark + Issue: "8969" diff --git a/core/dbt/adapters/base/impl.py b/core/dbt/adapters/base/impl.py index bc39d1f9dcc..e9befd599a0 100644 --- a/core/dbt/adapters/base/impl.py +++ b/core/dbt/adapters/base/impl.py @@ -244,7 +244,7 @@ class BaseAdapter(metaclass=AdapterMeta): def __init__(self, config, mp_context: SpawnContext) -> None: self.config = config - self.cache = RelationsCache() + self.cache = RelationsCache(log_cache_events=config.log_cache_events) self.connections = self.ConnectionManager(config, mp_context) self._macro_manifest_lazy: Optional[MacroManifest] = None diff --git a/core/dbt/adapters/cache.py b/core/dbt/adapters/cache.py index 4e8336dd21e..750a75cb7ad 100644 --- a/core/dbt/adapters/cache.py +++ b/core/dbt/adapters/cache.py @@ -16,7 +16,6 @@ ) from dbt.common.events.functions import fire_event, fire_event_if from dbt.common.events.types import CacheAction, CacheDumpGraph -from dbt.flags import get_flags from dbt.utils import lowercase @@ -165,10 +164,11 @@ class RelationsCache: :attr Set[str] schemas: The set of known/cached schemas, all lowercased. """ - def __init__(self) -> None: + def __init__(self, log_cache_events: bool = False) -> None: self.relations: Dict[_ReferenceKey, _CachedRelation] = {} self.lock = threading.RLock() self.schemas: Set[Tuple[Optional[str], Optional[str]]] = set() + self.log_cache_events = log_cache_events def add_schema( self, @@ -318,10 +318,9 @@ def add(self, relation): :param BaseRelation relation: The underlying relation. """ - flags = get_flags() cached = _CachedRelation(relation) fire_event_if( - flags.LOG_CACHE_EVENTS, + self.log_cache_events, lambda: CacheDumpGraph(before_after="before", action="adding", dump=self.dump_graph()), ) fire_event(CacheAction(action="add_relation", ref_key=_make_ref_key_dict(cached))) @@ -329,7 +328,7 @@ def add(self, relation): with self.lock: self._setdefault(cached) fire_event_if( - flags.LOG_CACHE_EVENTS, + self.log_cache_events, lambda: CacheDumpGraph(before_after="after", action="adding", dump=self.dump_graph()), ) @@ -454,9 +453,8 @@ def rename(self, old, new): ref_key_2=new_key._asdict(), ) ) - flags = get_flags() fire_event_if( - flags.LOG_CACHE_EVENTS, + self.log_cache_events, lambda: CacheDumpGraph(before_after="before", action="rename", dump=self.dump_graph()), ) @@ -467,7 +465,7 @@ def rename(self, old, new): self._setdefault(_CachedRelation(new)) fire_event_if( - flags.LOG_CACHE_EVENTS, + self.log_cache_events, lambda: CacheDumpGraph(before_after="after", action="rename", dump=self.dump_graph()), ) diff --git a/core/dbt/adapters/contracts/connection.py b/core/dbt/adapters/contracts/connection.py index 1d614989690..9a55a6d6780 100644 --- a/core/dbt/adapters/contracts/connection.py +++ b/core/dbt/adapters/contracts/connection.py @@ -235,3 +235,4 @@ class AdapterRequiredConfig(HasCredentials, Protocol): query_comment: QueryComment cli_vars: Dict[str, Any] target_path: str + log_cache_events: bool diff --git a/core/dbt/config/profile.py b/core/dbt/config/profile.py index 80d2850e69c..d96b0d80063 100644 --- a/core/dbt/config/profile.py +++ b/core/dbt/config/profile.py @@ -75,6 +75,7 @@ class Profile(HasCredentials): threads: int credentials: Credentials profile_env_vars: Dict[str, Any] + log_cache_events: bool def __init__( self, @@ -93,6 +94,9 @@ def __init__( self.threads = threads self.credentials = credentials self.profile_env_vars = {} # never available on init + self.log_cache_events = ( + get_flags().LOG_CACHE_EVENTS + ) # never available on init, set for adapter instantiation via AdapterRequiredConfig def to_profile_info(self, serialize_credentials: bool = False) -> Dict[str, Any]: """Unlike to_project_config, this dict is not a mirror of any existing diff --git a/core/dbt/config/runtime.py b/core/dbt/config/runtime.py index 5c88b447b26..82200e33e49 100644 --- a/core/dbt/config/runtime.py +++ b/core/dbt/config/runtime.py @@ -134,6 +134,7 @@ def from_parts( ).to_dict(omit_none=True) cli_vars: Dict[str, Any] = getattr(args, "vars", {}) + log_cache_events: bool = getattr(args, "log_cache_events", profile.log_cache_events) return cls( project_name=project.project_name, @@ -183,6 +184,7 @@ def from_parts( credentials=profile.credentials, args=args, cli_vars=cli_vars, + log_cache_events=log_cache_events, dependencies=dependencies, dbt_cloud=project.dbt_cloud, )