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

Add registry refreshing and caching #1431

Merged
merged 8 commits into from
Apr 3, 2021
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
80 changes: 59 additions & 21 deletions sdk/python/feast/feature_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from collections import defaultdict
from datetime import datetime
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -51,6 +51,7 @@ class FeatureStore:

config: RepoConfig
repo_path: Optional[str]
_registry: Registry

def __init__(
self, repo_path: Optional[str] = None, config: Optional[RepoConfig] = None,
Expand All @@ -72,15 +73,40 @@ def __init__(
),
)

metadata_store_config = self.config.get_metadata_store_config()
self._registry = Registry(
registry_path=metadata_store_config.path,
cache_ttl=timedelta(seconds=metadata_store_config.cache_ttl_seconds),
)

@property
def project(self) -> str:
return self.config.project

def _get_provider(self) -> Provider:
return get_provider(self.config)

def _get_registry(self) -> Registry:
return Registry(self.config.metadata_store)
def refresh_registry(self):
"""Fetches and caches a copy of the feature registry in memory.

Explicitly calling this method allows for direct control of the state of the registry cache. Every time this
method is called the complete registry state will be retrieved from the remote registry store backend
(e.g., GCS, S3), and the cache timer will be reset. If refresh_registry() is run before get_online_features()
is called, then get_online_feature() will use the cached registry instead of retrieving (and caching) the
registry itself.

Additionally, the TTL for the registry cache can be set to infinity (by setting it to 0), which means that
refresh_registry() will become the only way to update the cached registry. If the TTL is set to a value
greater than 0, then once the cache becomes stale (more time than the TTL has passed), a new cache will be
downloaded synchronously, which may increase latencies if the triggering method is get_online_features()
"""

metadata_store_config = self.config.get_metadata_store_config()
self._registry = Registry(
registry_path=metadata_store_config.path,
cache_ttl=timedelta(seconds=metadata_store_config.cache_ttl_seconds),
)
self._registry.refresh()

def list_entities(self) -> List[Entity]:
"""
Expand All @@ -89,7 +115,7 @@ def list_entities(self) -> List[Entity]:
Returns:
List of entities
"""
return self._get_registry().list_entities(self.project)
return self._registry.list_entities(self.project)

def list_feature_views(self) -> List[FeatureView]:
"""
Expand All @@ -98,7 +124,7 @@ def list_feature_views(self) -> List[FeatureView]:
Returns:
List of feature views
"""
return self._get_registry().list_feature_views(self.project)
return self._registry.list_feature_views(self.project)

def get_entity(self, name: str) -> Entity:
"""
Expand All @@ -111,7 +137,7 @@ def get_entity(self, name: str) -> Entity:
Returns either the specified entity, or raises an exception if
none is found
"""
return self._get_registry().get_entity(name, self.project)
return self._registry.get_entity(name, self.project)

def get_feature_view(self, name: str) -> FeatureView:
"""
Expand All @@ -124,7 +150,7 @@ def get_feature_view(self, name: str) -> FeatureView:
Returns either the specified feature view, or raises an exception if
none is found
"""
return self._get_registry().get_feature_view(name, self.project)
return self._registry.get_feature_view(name, self.project)

def delete_feature_view(self, name: str):
"""
Expand All @@ -133,7 +159,7 @@ def delete_feature_view(self, name: str):
Args:
name: Name of feature view
"""
return self._get_registry().delete_feature_view(name, self.project)
return self._registry.delete_feature_view(name, self.project)

def apply(self, objects: List[Union[FeatureView, Entity]]):
"""Register objects to metadata store and update related infrastructure.
Expand Down Expand Up @@ -166,15 +192,14 @@ def apply(self, objects: List[Union[FeatureView, Entity]]):

# TODO: Add locking
# TODO: Optimize by only making a single call (read/write)
registry = self._get_registry()

views_to_update = []
for ob in objects:
if isinstance(ob, FeatureView):
registry.apply_feature_view(ob, project=self.config.project)
self._registry.apply_feature_view(ob, project=self.config.project)
views_to_update.append(ob)
elif isinstance(ob, Entity):
registry.apply_entity(ob, project=self.config.project)
self._registry.apply_entity(ob, project=self.config.project)
else:
raise ValueError(
f"Unknown object type ({type(ob)}) provided as part of apply() call"
Expand Down Expand Up @@ -226,8 +251,9 @@ def get_historical_features(
>>> model.fit(feature_data) # insert your modeling framework here.
"""

registry = self._get_registry()
all_feature_views = registry.list_feature_views(project=self.config.project)
all_feature_views = self._registry.list_feature_views(
project=self.config.project
)
feature_views = _get_requested_feature_views(feature_refs, all_feature_views)
offline_store = get_offline_store_for_retrieval(feature_views)
job = offline_store.get_historical_features(
Expand Down Expand Up @@ -263,14 +289,15 @@ def materialize_incremental(
>>> )
"""
feature_views_to_materialize = []
registry = self._get_registry()
if feature_views is None:
feature_views_to_materialize = registry.list_feature_views(
feature_views_to_materialize = self._registry.list_feature_views(
self.config.project
)
else:
for name in feature_views:
feature_view = registry.get_feature_view(name, self.config.project)
feature_view = self._registry.get_feature_view(
name, self.config.project
)
feature_views_to_materialize.append(feature_view)

# TODO paging large loads
Expand Down Expand Up @@ -316,14 +343,15 @@ def materialize(
>>> )
"""
feature_views_to_materialize = []
registry = self._get_registry()
if feature_views is None:
feature_views_to_materialize = registry.list_feature_views(
feature_views_to_materialize = self._registry.list_feature_views(
self.config.project
)
else:
for name in feature_views:
feature_view = registry.get_feature_view(name, self.config.project)
feature_view = self._registry.get_feature_view(
name, self.config.project
)
feature_views_to_materialize.append(feature_view)

# TODO paging large loads
Expand Down Expand Up @@ -369,6 +397,15 @@ def get_online_features(
) -> OnlineResponse:
"""
Retrieves the latest online feature data.

Note: This method will download the full feature registry the first time it is run. If you are using a
remote registry like GCS or S3 then that may take a few seconds. The registry remains cached up to a TTL
duration (which can be set to infinitey). If the cached registry is stale (more time than the TTL has
passed), then a new registry will be downloaded synchronously by this method. This download may
introduce latency to online feature retrieval. In order to avoid synchronous downloads, please call
refresh_registry() prior to the TTL being reached. Remember it is possible to set the cache TTL to
infinity (cache forever).

Args:
feature_refs: List of feature references that will be returned for each entity.
Each feature reference should have the following format:
Expand Down Expand Up @@ -416,8 +453,9 @@ def _get_online_features(
entity_keys.append(_entity_row_to_key(row))
result_rows.append(_entity_row_to_field_values(row))

registry = self._get_registry()
all_feature_views = registry.list_feature_views(project=self.config.project)
all_feature_views = self._registry.list_feature_views(
project=self.config.project, allow_cache=True
)

grouped_refs = _group_refs(feature_refs, all_feature_views)
for table, requested_features in grouped_refs:
Expand Down
Loading