diff --git a/sdk/python/feast/infra/registry/proto_registry_utils.py b/sdk/python/feast/infra/registry/proto_registry_utils.py index 0896fe9646a..eb783ffb470 100644 --- a/sdk/python/feast/infra/registry/proto_registry_utils.py +++ b/sdk/python/feast/infra/registry/proto_registry_utils.py @@ -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 @@ -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 @@ -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), project, tags_key, kwargs_key, @@ -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 + + proto_key = _registry_proto_key(registry_proto) if ( registry_proto is not cache_proto @@ -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: diff --git a/sdk/python/feast/infra/registry/snowflake.py b/sdk/python/feast/infra/registry/snowflake.py index 71ca51112ee..ec0a11e5f83 100644 --- a/sdk/python/feast/infra/registry/snowflake.py +++ b/sdk/python/feast/infra/registry/snowflake.py @@ -1,3 +1,4 @@ +import hashlib import logging import os import uuid @@ -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): diff --git a/sdk/python/feast/infra/registry/sql.py b/sdk/python/feast/infra/registry/sql.py index 771a3890886..36693656495 100644 --- a/sdk/python/feast/infra/registry/sql.py +++ b/sdk/python/feast/infra/registry/sql.py @@ -1,3 +1,4 @@ +import hashlib import logging import uuid from concurrent.futures import ThreadPoolExecutor @@ -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): diff --git a/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py b/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py index 9778ba78491..90828a32a3a 100644 --- a/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py +++ b/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py @@ -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 diff --git a/sdk/python/tests/unit/infra/registry/test_sql_registry.py b/sdk/python/tests/unit/infra/registry/test_sql_registry.py index e39aaa247e6..8b634ecc517 100644 --- a/sdk/python/tests/unit/infra/registry/test_sql_registry.py +++ b/sdk/python/tests/unit/infra/registry/test_sql_registry.py @@ -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()