Skip to content
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
35 changes: 21 additions & 14 deletions sdk/python/feast/infra/registry/proto_registry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,34 @@
from feast.saved_dataset import SavedDataset, ValidationReference
from feast.stream_feature_view import StreamFeatureView

# The caches hold the registry proto itself and compare by identity. An id() key
# is unsafe: once a proto is freed, a new proto can reuse its id, and registries
# that leave version_id empty (SqlRegistry) then read another registry's objects.


def registry_proto_cache(func):
cache_proto = None
cache_key = None
cache_value = None

@wraps(func)
def wrapper(registry_proto: RegistryProto, project: str):
nonlocal cache_key, cache_value
nonlocal cache_proto, cache_key, cache_value

key = tuple([id(registry_proto), registry_proto.version_id, project])
key = (registry_proto.version_id, project)

if key == cache_key:
if registry_proto is cache_proto and key == cache_key:
return cache_value
else:
cache_value = func(registry_proto, project)
cache_key = key
cache_proto, cache_key = registry_proto, key
return cache_value

return wrapper


def registry_proto_cache_with_tags(func):
cache_proto = None
cache_key = None
cache_value = None

Expand All @@ -60,42 +66,43 @@ def wrapper(
tags: Optional[dict[str, str]],
**kwargs,
):
nonlocal cache_key, cache_value
nonlocal cache_proto, cache_key, cache_value

kwargs_key = tuple(sorted(kwargs.items())) if kwargs else ()
# Snapshot the filter so caller mutations cannot change a cached key.
tags_key = frozenset(tags.items()) if tags is not None else None
key = (
id(registry_proto),
registry_proto.version_id,
project,
tags_key,
kwargs_key,
)

if key == cache_key:
if registry_proto is cache_proto and key == cache_key:
return cache_value
else:
cache_value = func(registry_proto, project, tags, **kwargs)
cache_key = key
cache_proto, cache_key = registry_proto, key
return cache_value

return wrapper


def registry_proto_cache_name(func):
cache_proto_key = None
cache_proto = None
cache_version = None
cache = {}

@wraps(func)
def wrapper(registry_proto: RegistryProto, name: str, project: str):
nonlocal cache_proto_key, cache

proto_key = (id(registry_proto), registry_proto.version_id)
nonlocal cache_proto, cache_version, cache

if proto_key != cache_proto_key:
if (
registry_proto is not cache_proto
or registry_proto.version_id != cache_version
):
cache = {}
cache_proto_key = proto_key
cache_proto, cache_version = registry_proto, registry_proto.version_id

key = (project, name)
if key in cache:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from typing import Callable
from unittest.mock import patch

from feast import Entity
from feast.infra.registry.proto_registry_utils import list_entities, list_saved_datasets
import pytest

from feast import Entity, FeatureView, FileSource
from feast.infra.registry import proto_registry_utils
from feast.infra.registry.proto_registry_utils import (
get_any_feature_view,
list_entities,
list_project_metadata,
list_saved_datasets,
)
from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto
from feast.protos.feast.core.SavedDataset_pb2 import SavedDataset as SavedDatasetProto
from feast.protos.feast.core.SavedDataset_pb2 import (
Expand Down Expand Up @@ -35,6 +44,49 @@ def test_equivalent_tag_filters_reuse_cached_entities() -> None:
from_proto.assert_called_once()


def _registry(marker: str) -> RegistryProto:
# SqlRegistry leaves version_id empty, so only the proto object tells them apart.
proto = RegistryProto()
proto.project_metadata.add(project="test_project", project_uuid=marker)
entity = Entity(name="driver", value_type=ValueType.INT64, description=marker)
proto.entities.append(entity.to_proto())
proto.entities[0].spec.project = "test_project"
view = FeatureView(
name="driver_stats",
source=FileSource(path="driver.parquet"),
description=marker,
)
proto.feature_views.append(view.to_proto())
proto.feature_views[0].spec.project = "test_project"
return proto


@pytest.mark.parametrize(
"read",
[
lambda proto: list_project_metadata(proto, "test_project")[0].project_uuid,
lambda proto: list_entities(proto, "test_project", None)[0].description,
lambda proto: (
get_any_feature_view(proto, "driver_stats", "test_project").description
),
],
ids=[
"registry_proto_cache",
"registry_proto_cache_with_tags",
"registry_proto_cache_name",
],
)
def test_registries_that_share_an_id_are_not_served_from_each_others_cache(
monkeypatch: pytest.MonkeyPatch, read: Callable[[RegistryProto], str]
) -> None:
# Give both protos the same id(), as CPython does when a freed address is reused.
monkeypatch.setattr(proto_registry_utils, "id", lambda _proto: 0, raising=False)
first, second = _registry("old"), _registry("new")

assert read(first) == "old"
assert read(second) == "new"


class TestRegistryProto:
"""Test class for proto_registry_utils functions"""

Expand Down
Loading