-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Add async feature retrieval for Postgres Online Store #4327
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
Merged
franciscojavierarceo
merged 4 commits into
feast-dev:master
from
TomSteenbergen:postgres-async-feature-retrieval
Jul 8, 2024
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| from datetime import datetime | ||
| from typing import ( | ||
| Any, | ||
| AsyncGenerator, | ||
| Callable, | ||
| Dict, | ||
| Generator, | ||
|
|
@@ -12,18 +13,24 @@ | |
| Optional, | ||
| Sequence, | ||
| Tuple, | ||
| Union, | ||
| ) | ||
|
|
||
| import pytz | ||
| from psycopg import sql | ||
| from psycopg import AsyncConnection, sql | ||
| from psycopg.connection import Connection | ||
| from psycopg_pool import ConnectionPool | ||
| from psycopg_pool import AsyncConnectionPool, ConnectionPool | ||
|
|
||
| from feast import Entity | ||
| from feast.feature_view import FeatureView | ||
| from feast.infra.key_encoding_utils import get_list_val_str, serialize_entity_key | ||
| from feast.infra.online_stores.online_store import OnlineStore | ||
| from feast.infra.utils.postgres.connection_utils import _get_conn, _get_connection_pool | ||
| from feast.infra.utils.postgres.connection_utils import ( | ||
| _get_conn, | ||
| _get_conn_async, | ||
| _get_connection_pool, | ||
| _get_connection_pool_async, | ||
| ) | ||
| from feast.infra.utils.postgres.postgres_config import ConnectionType, PostgreSQLConfig | ||
| from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto | ||
| from feast.protos.feast.types.Value_pb2 import Value as ValueProto | ||
|
|
@@ -51,6 +58,9 @@ class PostgreSQLOnlineStore(OnlineStore): | |
| _conn: Optional[Connection] = None | ||
| _conn_pool: Optional[ConnectionPool] = None | ||
|
|
||
| _conn_async: Optional[AsyncConnection] = None | ||
| _conn_pool_async: Optional[AsyncConnectionPool] = None | ||
|
|
||
| @contextlib.contextmanager | ||
| def _get_conn(self, config: RepoConfig) -> Generator[Connection, Any, Any]: | ||
| assert config.online_store.type == "postgres" | ||
|
|
@@ -67,6 +77,24 @@ def _get_conn(self, config: RepoConfig) -> Generator[Connection, Any, Any]: | |
| self._conn = _get_conn(config.online_store) | ||
| yield self._conn | ||
|
|
||
| @contextlib.asynccontextmanager | ||
| async def _get_conn_async( | ||
| self, config: RepoConfig | ||
| ) -> AsyncGenerator[AsyncConnection, Any]: | ||
| if config.online_store.conn_type == ConnectionType.pool: | ||
| if not self._conn_pool_async: | ||
| self._conn_pool_async = await _get_connection_pool_async( | ||
| config.online_store | ||
| ) | ||
| await self._conn_pool_async.open() | ||
| connection = await self._conn_pool_async.getconn() | ||
| yield connection | ||
| await self._conn_pool_async.putconn(connection) | ||
| else: | ||
| if not self._conn_async: | ||
| self._conn_async = await _get_conn_async(config.online_store) | ||
| yield self._conn_async | ||
|
|
||
| def online_write_batch( | ||
| self, | ||
| config: RepoConfig, | ||
|
|
@@ -135,69 +163,107 @@ def online_read( | |
| entity_keys: List[EntityKeyProto], | ||
| requested_features: Optional[List[str]] = None, | ||
| ) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]: | ||
| result: List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]] = [] | ||
| keys = self._prepare_keys(entity_keys, config.entity_key_serialization_version) | ||
| query, params = self._construct_query_and_params( | ||
| config, table, keys, requested_features | ||
| ) | ||
|
|
||
| project = config.project | ||
| with self._get_conn(config) as conn, conn.cursor() as cur: | ||
| # Collecting all the keys to a list allows us to make fewer round trips | ||
| # to PostgreSQL | ||
| keys = [] | ||
| for entity_key in entity_keys: | ||
| keys.append( | ||
| serialize_entity_key( | ||
| entity_key, | ||
| entity_key_serialization_version=config.entity_key_serialization_version, | ||
| ) | ||
| ) | ||
| cur.execute(query, params) | ||
| rows = cur.fetchall() | ||
|
|
||
| if not requested_features: | ||
| cur.execute( | ||
| sql.SQL( | ||
| """ | ||
| SELECT entity_key, feature_name, value, event_ts | ||
| FROM {} WHERE entity_key = ANY(%s); | ||
| """ | ||
| ).format( | ||
| sql.Identifier(_table_id(project, table)), | ||
| ), | ||
| (keys,), | ||
| ) | ||
| else: | ||
| cur.execute( | ||
| sql.SQL( | ||
| """ | ||
| SELECT entity_key, feature_name, value, event_ts | ||
| FROM {} WHERE entity_key = ANY(%s) and feature_name = ANY(%s); | ||
| """ | ||
| ).format( | ||
| sql.Identifier(_table_id(project, table)), | ||
| ), | ||
| (keys, requested_features), | ||
| ) | ||
| return self._process_rows(keys, rows) | ||
|
|
||
| rows = cur.fetchall() | ||
| async def online_read_async( | ||
| self, | ||
| config: RepoConfig, | ||
| table: FeatureView, | ||
| entity_keys: List[EntityKeyProto], | ||
| requested_features: Optional[List[str]] = None, | ||
| ) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]: | ||
| keys = self._prepare_keys(entity_keys, config.entity_key_serialization_version) | ||
| query, params = self._construct_query_and_params( | ||
| config, table, keys, requested_features | ||
| ) | ||
|
|
||
| # Since we don't know the order returned from PostgreSQL we'll need | ||
| # to construct a dict to be able to quickly look up the correct row | ||
| # when we iterate through the keys since they are in the correct order | ||
| values_dict = defaultdict(list) | ||
| for row in rows if rows is not None else []: | ||
| values_dict[ | ||
| row[0] if isinstance(row[0], bytes) else row[0].tobytes() | ||
| ].append(row[1:]) | ||
|
|
||
| for key in keys: | ||
| if key in values_dict: | ||
| value = values_dict[key] | ||
| res = {} | ||
| for feature_name, value_bin, event_ts in value: | ||
| val = ValueProto() | ||
| val.ParseFromString(bytes(value_bin)) | ||
| res[feature_name] = val | ||
| result.append((event_ts, res)) | ||
| else: | ||
| result.append((None, None)) | ||
| async with self._get_conn_async(config) as conn: | ||
| async with conn.cursor() as cur: | ||
| await cur.execute(query, params) | ||
| rows = await cur.fetchall() | ||
|
|
||
| return self._process_rows(keys, rows) | ||
|
|
||
| @staticmethod | ||
| def _construct_query_and_params( | ||
| config: RepoConfig, | ||
| table: FeatureView, | ||
| keys: List[bytes], | ||
| requested_features: Optional[List[str]] = None, | ||
| ) -> Tuple[sql.Composed, Union[Tuple[List[bytes], List[str]], Tuple[List[bytes]]]]: | ||
| """Construct the SQL query based on the given parameters.""" | ||
| if requested_features: | ||
| query = sql.SQL( | ||
| """ | ||
| SELECT entity_key, feature_name, value, event_ts | ||
| FROM {} WHERE entity_key = ANY(%s) AND feature_name = ANY(%s); | ||
| """ | ||
| ).format( | ||
| sql.Identifier(_table_id(config.project, table)), | ||
| ) | ||
| params = (keys, requested_features) | ||
| else: | ||
| query = sql.SQL( | ||
| """ | ||
| SELECT entity_key, feature_name, value, event_ts | ||
| FROM {} WHERE entity_key = ANY(%s); | ||
| """ | ||
| ).format( | ||
| sql.Identifier(_table_id(config.project, table)), | ||
| ) | ||
| params = (keys, []) | ||
| return query, params | ||
|
|
||
| @staticmethod | ||
| def _prepare_keys( | ||
|
Contributor
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. No new logic, just moved to a separate method so it can be re-used. |
||
| entity_keys: List[EntityKeyProto], entity_key_serialization_version: int | ||
| ) -> List[bytes]: | ||
| """Prepare all keys in a list to make fewer round trips to the database.""" | ||
| return [ | ||
| serialize_entity_key( | ||
| entity_key, | ||
| entity_key_serialization_version=entity_key_serialization_version, | ||
| ) | ||
| for entity_key in entity_keys | ||
| ] | ||
|
|
||
| @staticmethod | ||
| def _process_rows( | ||
|
Contributor
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. No new logic, just moved to a separate method so it can be re-used. |
||
| keys: List[bytes], rows: List[Tuple] | ||
| ) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]: | ||
| """Transform the retrieved rows in the desired output. | ||
|
|
||
| PostgreSQL may return rows in an unpredictable order. Therefore, `values_dict` | ||
| is created to quickly look up the correct row using the keys, since these are | ||
| actually in the correct order. | ||
| """ | ||
| values_dict = defaultdict(list) | ||
| for row in rows if rows is not None else []: | ||
| values_dict[ | ||
| row[0] if isinstance(row[0], bytes) else row[0].tobytes() | ||
| ].append(row[1:]) | ||
|
|
||
| result: List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]] = [] | ||
| for key in keys: | ||
| if key in values_dict: | ||
| value = values_dict[key] | ||
| res = {} | ||
| for feature_name, value_bin, event_ts in value: | ||
| val = ValueProto() | ||
| val.ParseFromString(bytes(value_bin)) | ||
| res[feature_name] = val | ||
| result.append((event_ts, res)) | ||
| else: | ||
| result.append((None, None)) | ||
| return result | ||
|
|
||
| def update( | ||
|
|
||
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
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.
No new logic, just moved to a separate method so it can be re-used.