Skip to content
Open
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
14 changes: 11 additions & 3 deletions sdk/python/feast/infra/registry/proto_registry_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
# that leave version_id empty (SqlRegistry) then read another registry's objects.


def _registry_proto_key(registry_proto: RegistryProto):
"""Return the registry's version_id, or the object identity when it has none."""
return registry_proto.version_id or id(registry_proto)


def registry_proto_cache(func):
cache_proto = None
cache_key = None
Expand All @@ -42,7 +47,7 @@ def registry_proto_cache(func):
def wrapper(registry_proto: RegistryProto, project: str):
nonlocal cache_proto, cache_key, cache_value

key = (registry_proto.version_id, project)
key = (_registry_proto_key(registry_proto), project)

if registry_proto is cache_proto and key == cache_key:
return cache_value
Expand Down Expand Up @@ -72,7 +77,7 @@ def wrapper(
# Snapshot the filter so caller mutations cannot change a cached key.
tags_key = frozenset(tags.items()) if tags is not None else None
key = (
registry_proto.version_id,
_registry_proto_key(registry_proto),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the cache-hit condition below still requires registry_proto is cache_proto, so parsing a new proto with the same version always misses. the same identity check remains in registry_proto_cache; changing the key alone does not deliver the refresh reuse described by this PR, and the added same-version test cannot pass. we should compare the version-based key for versioned registries while retaining a strong object-identity guard for registries without a version.

project,
tags_key,
kwargs_key,
Expand All @@ -95,7 +100,9 @@ def registry_proto_cache_name(func):

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_proto_key has no binding in the enclosing function, so this nonlocal declaration raises SyntaxError when Python imports the module. this prevents the registry utilities and Feast from loading. the remaining assignments also still use cache_proto and cache_version. we need to finish the closure-state refactor so every declared nonlocal is initialized and used consistently.


proto_key = _registry_proto_key(registry_proto)

if (
registry_proto is not cache_proto
Expand Down Expand Up @@ -126,6 +133,7 @@ def get_project_metadata(
return None


@registry_proto_cache_name
def get_feature_service(
registry_proto: RegistryProto, name: str, project: str
) -> FeatureService:
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/feast/infra/registry/snowflake.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging
import os
import uuid
Expand Down Expand Up @@ -1367,6 +1368,7 @@ def process_project(project: Project):
if last_updated_timestamps:
r.last_updated.FromDatetime(max(last_updated_timestamps))

r.version_id = hashlib.sha1(r.SerializeToString(deterministic=True)).hexdigest()
return r

def _get_last_updated_metadata(self, project: str):
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/feast/infra/registry/sql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging
import uuid
from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -1570,6 +1571,7 @@ def process_project(project: Project):
if last_updated_timestamps:
r.last_updated.FromDatetime(max(last_updated_timestamps))

r.version_id = hashlib.sha1(r.SerializeToString(deterministic=True)).hexdigest()
return r

def commit(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,58 @@ def test_list_saved_datasets_uses_spec_tags(self):
)
assert len(result) == 1
assert result[0].name == "test_dataset"


def _registry_with_entity(version_id: str) -> RegistryProto:
registry = RegistryProto(version_id=version_id)
entity = Entity(name="driver", value_type=ValueType.INT64).to_proto()
entity.spec.project = "test_project"
registry.entities.append(entity)
return registry


def test_reparsed_registry_with_same_version_reuses_cache() -> None:
first_parse = _registry_with_entity("v1")
second_parse = _registry_with_entity("v1")
assert first_parse is not second_parse

with patch.object(Entity, "from_proto", wraps=Entity.from_proto) as from_proto:
list_entities(first_parse, "test_project", None)
list_entities(second_parse, "test_project", None)
from_proto.assert_called_once()

list_entities(_registry_with_entity("v2"), "test_project", None)
assert from_proto.call_count == 2


def test_registry_without_version_id_is_cached_per_object() -> None:
first = _registry_with_entity("")
second = _registry_with_entity("")

with patch.object(Entity, "from_proto", wraps=Entity.from_proto) as from_proto:
list_entities(first, "test_project", None)
list_entities(second, "test_project", None)
assert from_proto.call_count == 2


def test_get_feature_service_is_cached_per_version() -> None:
from feast.feature_service import FeatureService
from feast.infra.registry.proto_registry_utils import get_feature_service

def registry(version_id: str) -> RegistryProto:
proto = RegistryProto(version_id=version_id)
service = FeatureService(name="svc", features=[]).to_proto()
service.spec.project = "test_project"
proto.feature_services.append(service)
return proto

with patch.object(
FeatureService, "from_proto", wraps=FeatureService.from_proto
) as from_proto:
first = get_feature_service(registry("v1"), "svc", "test_project")
second = get_feature_service(registry("v1"), "svc", "test_project")
assert first is second
from_proto.assert_called_once()

get_feature_service(registry("v2"), "svc", "test_project")
assert from_proto.call_count == 2
36 changes: 36 additions & 0 deletions sdk/python/tests/unit/infra/registry/test_sql_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,39 @@ def test_schema_mode_invalid_value_rejected(self):
path="sqlite:///dummy.db",
schema_mode="bogus",
)


def test_proto_version_id_is_a_content_hash(shared_sqlite_db_path):
from unittest.mock import patch

from feast.feature_service import FeatureService

config = SqlRegistryConfig(
registry_type="sql",
path=f"sqlite:///{shared_sqlite_db_path}",
purge_feast_metadata=False,
)
registry = SqlRegistry(config, "project_hash", None)
registry.apply_feature_service(
FeatureService(name="svc", features=[]), "project_hash"
)

first = registry.proto()
second = registry.proto()
assert first.version_id
assert first.version_id == second.version_id
assert first is not second

with patch.object(
FeatureService, "from_proto", wraps=FeatureService.from_proto
) as from_proto:
registry.refresh("project_hash")
a = registry.get_feature_service("svc", "project_hash", allow_cache=True)
registry.refresh("project_hash")
b = registry.get_feature_service("svc", "project_hash", allow_cache=True)
assert a is b
from_proto.assert_called_once()

registry.apply_entity(Entity(name="customer", join_keys=["cid"]), "project_hash")
assert registry.proto().version_id != first.version_id
registry.teardown()
Loading