Skip to content

Commit daf12bc

Browse files
committed
Removed some of the old cache logic
1 parent 620f331 commit daf12bc

4 files changed

Lines changed: 21 additions & 186 deletions

File tree

lib/dl_core/dl_core/data_processing/cache/utils.py

Lines changed: 15 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
from __future__ import annotations
22

3+
import abc
34
import logging
45
from typing import (
56
TYPE_CHECKING,
67
Collection,
7-
Optional,
88
)
99

1010
import attr
11-
from sqlalchemy.exc import DatabaseError
1211

13-
from dl_core.data_processing.cache.exc import CachePreparationFailed
1412
from dl_core.data_processing.cache.primitives import (
1513
BIQueryCacheOptions,
1614
CacheTTLConfig,
1715
CacheTTLInfo,
1816
DataKeyPart,
1917
LocalKeyRepresentation,
2018
)
21-
from dl_core.query.bi_query import QueryAndResultInfo
2219
from dl_core.serialization import hashable_dumps
2320
from dl_core.us_connection_base import (
2421
ConnectionBase,
@@ -27,23 +24,17 @@
2724

2825

2926
if TYPE_CHECKING:
30-
from sqlalchemy.engine.default import DefaultDialect
31-
from sqlalchemy.sql import Select
32-
33-
from dl_constants.enums import UserDataType
3427
from dl_constants.types import TJSONExt
3528
from dl_core.data_processing.prepared_components.primitives import PreparedFromInfo
3629
from dl_core.data_source.base import DataSource
37-
from dl_core.us_manager.local_cache import USEntryBuffer
3830

3931

4032
LOGGER = logging.getLogger(__name__)
4133

4234

4335
@attr.s
44-
class CacheOptionsBuilderBase:
36+
class CacheOptionsBuilderBase(abc.ABC):
4537
default_ttl_config: CacheTTLConfig = attr.ib(factory=CacheTTLConfig)
46-
_is_bleeding_edge_user: bool = attr.ib(default=False)
4738

4839
def get_actual_ttl_config(
4940
self,
@@ -58,28 +49,6 @@ def get_actual_ttl_config(
5849

5950
return ctc
6051

61-
@staticmethod
62-
def get_query_str_for_cache(query: Select, dialect: DefaultDialect) -> str:
63-
try:
64-
compiled_query = query.compile(dialect=dialect)
65-
except DatabaseError as err:
66-
raise CachePreparationFailed from err
67-
68-
if isinstance(compiled_query.params, dict):
69-
ordered_params = sorted(
70-
compiled_query.params.items(),
71-
key=lambda item: item[0],
72-
)
73-
else:
74-
ordered_params = compiled_query.params
75-
76-
return ";".join(
77-
(
78-
str(compiled_query),
79-
str(ordered_params),
80-
)
81-
)
82-
8352
@staticmethod
8453
def config_to_ttl_info(ttl_config: CacheTTLConfig) -> CacheTTLInfo:
8554
return CacheTTLInfo(
@@ -97,133 +66,43 @@ def get_cache_ttl_info(self, data_source_list: Collection[DataSource]) -> CacheT
9766
)
9867
return self.config_to_ttl_info(ttl_config=ttl_config)
9968

100-
def get_data_key(
101-
self,
102-
*,
103-
query_res_info: QueryAndResultInfo,
104-
from_info: Optional[PreparedFromInfo] = None,
105-
base_key: LocalKeyRepresentation = LocalKeyRepresentation(), # noqa: B008
106-
) -> Optional[LocalKeyRepresentation]:
107-
return base_key
108-
10969

11070
@attr.s
11171
class DatasetOptionsBuilder(CacheOptionsBuilderBase):
112-
cache_enabled: bool = attr.ib(kw_only=True, default=True)
113-
114-
def get_cache_options(
115-
self,
116-
joint_dsrc_info: PreparedFromInfo,
117-
data_key: LocalKeyRepresentation,
118-
) -> BIQueryCacheOptions:
72+
@abc.abstractmethod
73+
def get_cache_enabled(self, joint_dsrc_info: PreparedFromInfo) -> bool:
11974
raise NotImplementedError
12075

121-
122-
@attr.s
123-
class CompengOptionsBuilder(DatasetOptionsBuilder): # TODO: Move to compeng package
124-
cache_enabled: bool = attr.ib(kw_only=True, default=True)
125-
12676
def get_cache_options(
12777
self,
12878
joint_dsrc_info: PreparedFromInfo,
12979
data_key: LocalKeyRepresentation,
13080
) -> BIQueryCacheOptions:
13181
ttl_info = self.get_cache_ttl_info(data_source_list=joint_dsrc_info.data_source_list) # type: ignore # 2024-01-24 # TODO: Argument "data_source_list" to "get_cache_ttl_info" of "CacheOptionsBuilderBase" has incompatible type "tuple[DataSource, ...] | None"; expected "Collection[DataSource]" [arg-type]
82+
cache_enabled = self.get_cache_enabled(joint_dsrc_info=joint_dsrc_info)
13283
return BIQueryCacheOptions(
133-
cache_enabled=self.cache_enabled,
134-
key=data_key,
84+
cache_enabled=cache_enabled,
85+
key=data_key if cache_enabled else None,
13586
ttl_sec=ttl_info.ttl_sec,
13687
refresh_ttl_on_read=ttl_info.refresh_ttl_on_read,
13788
)
13889

139-
def get_data_key(
140-
self,
141-
*,
142-
query_res_info: QueryAndResultInfo,
143-
from_info: Optional[PreparedFromInfo] = None,
144-
base_key: LocalKeyRepresentation = LocalKeyRepresentation(), # noqa: B008
145-
) -> Optional[LocalKeyRepresentation]:
146-
# TODO: Remove after switching to new cache keys
147-
compiled_query = self.get_query_str_for_cache(
148-
query=query_res_info.query,
149-
dialect=from_info.query_compiler.dialect, # type: ignore # 2024-01-24 # TODO: Item "None" of "PreparedFromInfo | None" has no attribute "query_compiler" [union-attr]
150-
)
151-
return base_key.extend(part_type="query", part_content=compiled_query)
152-
15390

15491
@attr.s
155-
class SelectorCacheOptionsBuilder(DatasetOptionsBuilder):
156-
_is_bleeding_edge_user: bool = attr.ib(default=False)
157-
_us_entry_buffer: USEntryBuffer = attr.ib(kw_only=True)
92+
class CompengOptionsBuilder(DatasetOptionsBuilder): # TODO: Move to compeng package
93+
cache_enabled: bool = attr.ib(kw_only=True, default=True)
94+
95+
def get_cache_enabled(self, joint_dsrc_info: PreparedFromInfo) -> bool:
96+
return self.cache_enabled
97+
15898

99+
@attr.s
100+
class SelectorCacheOptionsBuilder(DatasetOptionsBuilder): # TODO: Rename to SourceDbCacheOptionsBuilder
159101
def get_cache_enabled(self, joint_dsrc_info: PreparedFromInfo) -> bool:
160102
assert joint_dsrc_info.data_source_list is not None
161103
cache_enabled = all(dsrc.cache_enabled for dsrc in joint_dsrc_info.data_source_list)
162104
return cache_enabled
163105

164-
def get_cache_options(
165-
self,
166-
joint_dsrc_info: PreparedFromInfo,
167-
data_key: LocalKeyRepresentation,
168-
) -> BIQueryCacheOptions:
169-
"""Returns cache key, TTL for new entries, refresh TTL flag"""
170-
171-
ttl_info = self.get_cache_ttl_info(data_source_list=joint_dsrc_info.data_source_list) # type: ignore # 2024-01-24 # TODO: Argument "data_source_list" to "get_cache_ttl_info" of "CacheOptionsBuilderBase" has incompatible type "tuple[DataSource, ...] | None"; expected "Collection[DataSource]" [arg-type]
172-
cache_enabled = self.get_cache_enabled(joint_dsrc_info=joint_dsrc_info)
173-
return BIQueryCacheOptions(
174-
cache_enabled=cache_enabled,
175-
key=data_key if cache_enabled else None,
176-
ttl_sec=ttl_info.ttl_sec,
177-
refresh_ttl_on_read=ttl_info.refresh_ttl_on_read,
178-
)
179-
180-
def make_data_select_cache_key(
181-
self,
182-
from_info: PreparedFromInfo,
183-
compiled_query: str,
184-
user_types: list[UserDataType],
185-
is_bleeding_edge_user: bool,
186-
base_key: LocalKeyRepresentation = LocalKeyRepresentation(), # noqa: B008
187-
) -> LocalKeyRepresentation:
188-
# TODO: Remove after switching to new cache keys,
189-
# but put the db_name + target_connection.get_cache_key_part() parts somewhere
190-
assert from_info.target_connection_ref is not None
191-
target_connection = self._us_entry_buffer.get_entry(from_info.target_connection_ref)
192-
assert isinstance(target_connection, ConnectionBase)
193-
connection_id = target_connection.uuid
194-
assert connection_id is not None
195-
196-
local_key_rep = base_key
197-
local_key_rep = local_key_rep.extend(part_type="query", part_content=str(compiled_query))
198-
local_key_rep = local_key_rep.extend(part_type="user_types", part_content=tuple(user_types or ()))
199-
local_key_rep = local_key_rep.extend(
200-
part_type="is_bleeding_edge_user",
201-
part_content=is_bleeding_edge_user,
202-
)
203-
204-
return local_key_rep
205-
206-
def get_data_key(
207-
self,
208-
*,
209-
query_res_info: QueryAndResultInfo,
210-
from_info: Optional[PreparedFromInfo] = None,
211-
base_key: LocalKeyRepresentation = LocalKeyRepresentation(), # noqa: B008
212-
) -> Optional[LocalKeyRepresentation]:
213-
# TODO: Remove after switching to new cache keys
214-
compiled_query = self.get_query_str_for_cache(
215-
query=query_res_info.query,
216-
dialect=from_info.query_compiler.dialect, # type: ignore # 2024-01-24 # TODO: Item "None" of "PreparedFromInfo | None" has no attribute "query_compiler" [union-attr]
217-
)
218-
data_key: Optional[LocalKeyRepresentation] = self.make_data_select_cache_key(
219-
base_key=base_key,
220-
from_info=from_info, # type: ignore # 2024-01-24 # TODO: Argument "from_info" to "make_data_select_cache_key" of "SelectorCacheOptionsBuilder" has incompatible type "PreparedFromInfo | None"; expected "PreparedFromInfo" [arg-type]
221-
compiled_query=compiled_query,
222-
user_types=query_res_info.user_types,
223-
is_bleeding_edge_user=self._is_bleeding_edge_user,
224-
)
225-
return data_key
226-
227106

228107
@attr.s
229108
class DashSQLCacheOptionsBuilder(CacheOptionsBuilderBase):

lib/dl_core/dl_core/data_processing/processing/db_base/exec_adapter_base.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,6 @@ def _make_query_res_info(
143143
)
144144
return query_res_info
145145

146-
def get_data_key(
147-
self,
148-
*,
149-
query: str | Select,
150-
user_types: Sequence[UserDataType],
151-
from_info: Optional[PreparedFromInfo] = None,
152-
base_key: LocalKeyRepresentation = LocalKeyRepresentation(), # noqa: B008
153-
) -> Optional[LocalKeyRepresentation]:
154-
# TODO: Remove this method
155-
query_res_info = self._make_query_res_info(query=query, user_types=user_types)
156-
data_key = self._cache_options_builder.get_data_key(
157-
from_info=from_info,
158-
query_res_info=query_res_info,
159-
base_key=base_key,
160-
)
161-
return data_key
162-
163146
async def create_table(
164147
self,
165148
*,

lib/dl_core/dl_core/data_processing/processing/db_base/op_executors.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,36 +172,12 @@ def get_from_info_from_stream(self, source_stream: AbstractStream) -> PreparedFr
172172
def make_data_key(self, op: BaseOp) -> LocalKeyRepresentation:
173173
assert isinstance(op, CalcOp)
174174
source_stream = self.ctx.get_stream(op.source_stream_id)
175-
176-
# TODO: Remove legacy version
177-
178-
# Legacy procedure
179-
from_info = self.get_from_info_from_stream(source_stream=source_stream) # type: ignore # 2024-01-24 # TODO: Argument "source_stream" to "get_from_info_from_stream" of "CalcOpExecutorAsync" has incompatible type "AbstractStream | None"; expected "AbstractStream" [arg-type]
180-
query_compiler = from_info.query_compiler
181-
query = query_compiler.compile_select(
182-
bi_query=op.bi_query,
183-
# The info about the real source is already contained in the previous key parts,
184-
# and, also, we want to avoid the randomized table names (in compeng) to appear in the key.
185-
# So just use a fake table here.
186-
sql_source=sa.table("table"),
187-
)
188-
legacy_data_key = self.db_ex_adapter.get_data_key(
189-
query=query,
190-
user_types=source_stream.user_types, # type: ignore # 2024-01-24 # TODO: Item "None" of "AbstractStream | None" has no attribute "user_types" [union-attr]
191-
from_info=from_info,
192-
base_key=source_stream.data_key, # type: ignore # 2024-01-24 # TODO: Item "None" of "AbstractStream | None" has no attribute "data_key" [union-attr]
193-
)
194-
195-
# New procedure
196-
new_data_key = source_stream.data_key.extend("query", op.data_key_data) # type: ignore # 2024-01-24 # TODO: Item "None" of "AbstractStream | None" has no attribute "data_key" [union-attr]
197-
198-
LOGGER.info(
199-
f"Preliminary cache key info for query: " # type: ignore # 2024-01-24 # TODO: Item "None" of "LocalKeyRepresentation | None" has no attribute "key_parts_hash" [union-attr]
200-
f"legacy key: {legacy_data_key.key_parts_hash} ; "
201-
f"new key: {new_data_key.key_parts_hash}"
202-
)
203-
204-
return new_data_key
175+
assert source_stream is not None
176+
base_data_key = source_stream.data_key
177+
assert base_data_key is not None
178+
data_key = base_data_key.extend("query", op.data_key_data)
179+
LOGGER.info(f"Preliminary cache key info for query: " f"key: {data_key.key_parts_hash}")
180+
return data_key
205181

206182
@log_op # type: ignore # TODO: fix
207183
async def execute(self, op: BaseOp) -> DataSourceVS: # type: ignore # TODO: fix

lib/dl_core/dl_core/data_processing/processing/source_db/processor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ class SourceDbOperationProcessor(ExecutorBasedOperationProcessor):
2323
_dataset: Dataset = attr.ib(kw_only=True)
2424
_row_count_hard_limit: Optional[int] = attr.ib(kw_only=True, default=None)
2525
_us_entry_buffer: USEntryBuffer = attr.ib(kw_only=True)
26-
_is_bleeding_edge_user: bool = attr.ib(default=False)
2726
_default_cache_ttl_config: CacheTTLConfig = attr.ib(default=None)
2827

2928
def _make_cache_options_builder(self) -> DatasetOptionsBuilder:
3029
return SelectorCacheOptionsBuilder(
3130
default_ttl_config=self._default_cache_ttl_config,
32-
is_bleeding_edge_user=self._is_bleeding_edge_user,
33-
us_entry_buffer=self._us_entry_buffer,
3431
)
3532

3633
def _make_db_ex_adapter(self) -> Optional[ProcessorDbExecAdapterBase]:

0 commit comments

Comments
 (0)