From 00d10a293497afe7e71889180100ed2fc98789b3 Mon Sep 17 00:00:00 2001 From: Youngbeom Choi Date: Wed, 25 Nov 2026 15:58:37 +0900 Subject: [PATCH 1/3] fix: Serve cached file registry reads without waiting for a refresh Registry._get_registry_proto() took _refresh_lock before checking the cache, and a refresh holds that lock for the whole registry download. While a refresh waited on slow or unresponsive object storage, every allow_cache=True read waited too, including the feature server's /health, even though the in-memory cache was still valid. Return the cached proto before taking the lock when the cache is within its TTL and, for a local registry file in sync mode, the file has not changed since it was last read. All other reads take the locked path as before. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Youngbeom Choi --- sdk/python/feast/infra/registry/registry.py | 15 +++++ .../unit/infra/registry/test_file_registry.py | 62 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/sdk/python/feast/infra/registry/registry.py b/sdk/python/feast/infra/registry/registry.py index baa8620140e..57319d66276 100644 --- a/sdk/python/feast/infra/registry/registry.py +++ b/sdk/python/feast/infra/registry/registry.py @@ -1502,6 +1502,11 @@ def _get_registry_proto( Returns: Returns a RegistryProto object which represents the state of the registry """ + # Serve a valid cache without taking _refresh_lock, so that cached reads + # do not wait for a refresh stuck on a slow registry download + if allow_cache and self.is_cache_valid() and not self._registry_file_changed(): + return self.cached_registry_proto + with self._refresh_lock: # For file-based registries in sync mode, check file modification time # to detect changes immediately, not just based on TTL @@ -1553,6 +1558,16 @@ def _get_registry_proto( return registry_proto + def _registry_file_changed(self) -> bool: + """Checks the sync-mode registry file mtime without updating _file_mtime.""" + if self.cache_mode != "sync" or self._file_path is None: + return False + try: + current_mtime = self._file_path.stat().st_mtime + except OSError: + return True + return self._file_mtime is None or current_mtime > self._file_mtime + def _check_conflicting_feature_view_names( self, feature_view: BaseFeatureView, project: str ): diff --git a/sdk/python/tests/unit/infra/registry/test_file_registry.py b/sdk/python/tests/unit/infra/registry/test_file_registry.py index 8e771d380cf..609ede87119 100644 --- a/sdk/python/tests/unit/infra/registry/test_file_registry.py +++ b/sdk/python/tests/unit/infra/registry/test_file_registry.py @@ -1,5 +1,8 @@ +import os import tempfile +from concurrent.futures import ThreadPoolExecutor from datetime import timedelta +from unittest.mock import patch import pytest @@ -11,9 +14,12 @@ from feast.feature_view import FeatureView from feast.infra.offline_stores.file_source import FileSource from feast.infra.registry.registry import Registry +from feast.project import Project +from feast.protos.feast.core.Registry_pb2 import Registry as RegistryProto from feast.repo_config import RegistryConfig from feast.stream_feature_view import StreamFeatureView from feast.types import Float32 +from feast.utils import _utc_now from feast.value_type import ValueType @@ -185,3 +191,59 @@ def test_cross_project_name_does_not_conflict_batch_vs_stream(file_registry): ) # Must not raise — same name, different project, different type. file_registry.apply_feature_view(stream_view, "project_b") + + +def test_cached_read_does_not_wait_for_in_progress_refresh( + file_registry: Registry, +) -> None: + """A read the cache can serve must not block on a refresh stuck downloading. + + The feature server's /health and online reads use allow_cache=True. If they + wait for the refresh lock, a hanging registry store download makes them + hang too, even though the in-memory cache is still valid. + """ + # Hold the lock to simulate a refresh stuck downloading in another thread + with ThreadPoolExecutor(max_workers=1) as executor: + file_registry._refresh_lock.acquire() + try: + future = executor.submit(file_registry.list_projects, allow_cache=True) + projects = future.result(timeout=2) + finally: + file_registry._refresh_lock.release() + + assert projects == [] + + +def test_expired_cache_read_fetches_from_store(file_registry: Registry) -> None: + """Once the TTL has passed, a cached read still downloads a fresh registry.""" + fresh_proto = RegistryProto() + fresh_proto.projects.append(Project(name="fresh_project").to_proto()) + file_registry.cached_registry_proto_ttl = timedelta(seconds=1) + file_registry.cached_registry_proto_created = _utc_now() - timedelta(seconds=5) + + with patch.object( + file_registry._registry_store, + "get_registry_proto", + return_value=fresh_proto, + ): + projects = file_registry.list_projects(allow_cache=True) + + assert [p.name for p in projects] == ["fresh_project"] + + +def test_cached_read_picks_up_modified_local_file(file_registry: Registry) -> None: + """In sync mode a change to the local registry file is read before the TTL ends.""" + registry_path = file_registry._file_path + last_read_mtime = file_registry._file_mtime + assert registry_path is not None + assert last_read_mtime is not None + assert file_registry.list_projects(allow_cache=True) == [] + + writer = Registry("test_project", RegistryConfig(path=str(registry_path)), None) + writer.apply_project(Project(name="written_project"), commit=True) + future_mtime = last_read_mtime + 10 + os.utime(registry_path, (future_mtime, future_mtime)) + + projects = file_registry.list_projects(allow_cache=True) + + assert [p.name for p in projects] == ["written_project"] From ff1ce31036937911e06823837fd8961108a0d269 Mon Sep 17 00:00:00 2001 From: Youngbeom Choi Date: Wed, 25 Nov 2026 16:13:54 +0900 Subject: [PATCH 2/3] fix: Record the registry file mtime only after reloading it The locked reload path stored the new file mtime as soon as it noticed the change, before it replaced the cached proto. With the lock-free cache check, a cached read in that window saw an unchanged file and returned the old snapshot. The mtime is already stored after the reload, so drop the early assignment. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Youngbeom Choi --- sdk/python/feast/infra/registry/registry.py | 1 - .../unit/infra/registry/test_file_registry.py | 66 ++++++++++++++++--- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/sdk/python/feast/infra/registry/registry.py b/sdk/python/feast/infra/registry/registry.py index 57319d66276..6a4ebfb6f7c 100644 --- a/sdk/python/feast/infra/registry/registry.py +++ b/sdk/python/feast/infra/registry/registry.py @@ -1521,7 +1521,6 @@ def _get_registry_proto( current_mtime = self._file_path.stat().st_mtime if self._file_mtime is None or current_mtime > self._file_mtime: file_modified = True - self._file_mtime = current_mtime except (OSError, FileNotFoundError): file_modified = True diff --git a/sdk/python/tests/unit/infra/registry/test_file_registry.py b/sdk/python/tests/unit/infra/registry/test_file_registry.py index 609ede87119..b8be529cd5b 100644 --- a/sdk/python/tests/unit/infra/registry/test_file_registry.py +++ b/sdk/python/tests/unit/infra/registry/test_file_registry.py @@ -1,6 +1,8 @@ import os import tempfile +import threading from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import TimeoutError as FutureTimeoutError from datetime import timedelta from unittest.mock import patch @@ -42,6 +44,19 @@ def _make_sources(): return file_source, push_source +def _write_project_to_registry_file(registry: Registry, project_name: str) -> None: + """Writes a project to the registry's local file and advances the file mtime.""" + registry_path = registry._file_path + last_read_mtime = registry._file_mtime + assert registry_path is not None + assert last_read_mtime is not None + + writer = Registry("test_project", RegistryConfig(path=str(registry_path)), None) + writer.apply_project(Project(name=project_name), commit=True) + future_mtime = last_read_mtime + 10 + os.utime(registry_path, (future_mtime, future_mtime)) + + @pytest.mark.parametrize("mode", ["pandas", "sql", "flink"]) def test_apply_batch_feature_view_stores_changed_udf(file_registry, mode): """Re-applying a BatchFeatureView with a new UDF must store the new UDF.""" @@ -233,17 +248,50 @@ def test_expired_cache_read_fetches_from_store(file_registry: Registry) -> None: def test_cached_read_picks_up_modified_local_file(file_registry: Registry) -> None: """In sync mode a change to the local registry file is read before the TTL ends.""" - registry_path = file_registry._file_path - last_read_mtime = file_registry._file_mtime - assert registry_path is not None - assert last_read_mtime is not None assert file_registry.list_projects(allow_cache=True) == [] - - writer = Registry("test_project", RegistryConfig(path=str(registry_path)), None) - writer.apply_project(Project(name="written_project"), commit=True) - future_mtime = last_read_mtime + 10 - os.utime(registry_path, (future_mtime, future_mtime)) + _write_project_to_registry_file(file_registry, "written_project") projects = file_registry.list_projects(allow_cache=True) assert [p.name for p in projects] == ["written_project"] + + +def test_cached_read_waits_for_reload_of_modified_local_file( + file_registry: Registry, +) -> None: + """A cached read during a reload of a changed file must wait for the new snapshot. + + If the reload records the new file mtime before it replaces the cached proto, + a cached read in between sees an unchanged file and returns the old snapshot. + """ + _write_project_to_registry_file(file_registry, "written_project") + reload_started = threading.Event() + release_reload = threading.Event() + real_get_registry_proto = file_registry._registry_store.get_registry_proto + + def paused_get_registry_proto() -> RegistryProto: + reload_started.set() + release_reload.wait(timeout=10) + return real_get_registry_proto() + + with ( + patch.object( + file_registry._registry_store, + "get_registry_proto", + side_effect=paused_get_registry_proto, + ), + ThreadPoolExecutor(max_workers=2) as executor, + ): + reloading_read = executor.submit(file_registry.list_projects, allow_cache=True) + try: + assert reload_started.wait(timeout=5) + concurrent_read = executor.submit( + file_registry.list_projects, allow_cache=True + ) + with pytest.raises(FutureTimeoutError): + concurrent_read.result(timeout=0.5) + finally: + release_reload.set() + + assert [p.name for p in reloading_read.result()] == ["written_project"] + assert [p.name for p in concurrent_read.result()] == ["written_project"] From 7915d25fef2a42d491b7e170b69c0f1d49924453 Mon Sep 17 00:00:00 2001 From: Youngbeom Choi Date: Wed, 25 Nov 2026 18:02:29 +0900 Subject: [PATCH 3/3] test: Trim the file registry cache read tests Drop test_cached_read_picks_up_modified_local_file, because test_cached_read_waits_for_reload_of_modified_local_file already fails when the early return skips the file modification check. Inline the setup helper into its only remaining caller, shorten the docstrings, and wait on threading.Thread like the other unit tests. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Youngbeom Choi --- .../unit/infra/registry/test_file_registry.py | 115 ++++++++---------- 1 file changed, 54 insertions(+), 61 deletions(-) diff --git a/sdk/python/tests/unit/infra/registry/test_file_registry.py b/sdk/python/tests/unit/infra/registry/test_file_registry.py index b8be529cd5b..2bc66d64c9e 100644 --- a/sdk/python/tests/unit/infra/registry/test_file_registry.py +++ b/sdk/python/tests/unit/infra/registry/test_file_registry.py @@ -1,8 +1,6 @@ import os import tempfile import threading -from concurrent.futures import ThreadPoolExecutor -from concurrent.futures import TimeoutError as FutureTimeoutError from datetime import timedelta from unittest.mock import patch @@ -44,19 +42,6 @@ def _make_sources(): return file_source, push_source -def _write_project_to_registry_file(registry: Registry, project_name: str) -> None: - """Writes a project to the registry's local file and advances the file mtime.""" - registry_path = registry._file_path - last_read_mtime = registry._file_mtime - assert registry_path is not None - assert last_read_mtime is not None - - writer = Registry("test_project", RegistryConfig(path=str(registry_path)), None) - writer.apply_project(Project(name=project_name), commit=True) - future_mtime = last_read_mtime + 10 - os.utime(registry_path, (future_mtime, future_mtime)) - - @pytest.mark.parametrize("mode", ["pandas", "sql", "flink"]) def test_apply_batch_feature_view_stores_changed_udf(file_registry, mode): """Re-applying a BatchFeatureView with a new UDF must store the new UDF.""" @@ -211,26 +196,29 @@ def test_cross_project_name_does_not_conflict_batch_vs_stream(file_registry): def test_cached_read_does_not_wait_for_in_progress_refresh( file_registry: Registry, ) -> None: - """A read the cache can serve must not block on a refresh stuck downloading. + """A read the cache can serve must not wait for a refresh in progress.""" + results: list[list[Project]] = [] - The feature server's /health and online reads use allow_cache=True. If they - wait for the refresh lock, a hanging registry store download makes them - hang too, even though the in-memory cache is still valid. - """ - # Hold the lock to simulate a refresh stuck downloading in another thread - with ThreadPoolExecutor(max_workers=1) as executor: - file_registry._refresh_lock.acquire() - try: - future = executor.submit(file_registry.list_projects, allow_cache=True) - projects = future.result(timeout=2) - finally: - file_registry._refresh_lock.release() + def cached_read() -> None: + results.append(file_registry.list_projects(allow_cache=True)) + + thread = threading.Thread(target=cached_read, daemon=True) - assert projects == [] + # Hold the lock to simulate a refresh stuck downloading in another thread. + file_registry._refresh_lock.acquire() + try: + thread.start() + thread.join(timeout=2) + assert not thread.is_alive(), "cached read waited for the refresh lock" + finally: + file_registry._refresh_lock.release() + thread.join(timeout=5) + + assert results == [[]] def test_expired_cache_read_fetches_from_store(file_registry: Registry) -> None: - """Once the TTL has passed, a cached read still downloads a fresh registry.""" + """Once the TTL has passed, a cached read must download a fresh registry.""" fresh_proto = RegistryProto() fresh_proto.projects.append(Project(name="fresh_project").to_proto()) file_registry.cached_registry_proto_ttl = timedelta(seconds=1) @@ -246,52 +234,57 @@ def test_expired_cache_read_fetches_from_store(file_registry: Registry) -> None: assert [p.name for p in projects] == ["fresh_project"] -def test_cached_read_picks_up_modified_local_file(file_registry: Registry) -> None: - """In sync mode a change to the local registry file is read before the TTL ends.""" - assert file_registry.list_projects(allow_cache=True) == [] - _write_project_to_registry_file(file_registry, "written_project") - - projects = file_registry.list_projects(allow_cache=True) - - assert [p.name for p in projects] == ["written_project"] - - def test_cached_read_waits_for_reload_of_modified_local_file( file_registry: Registry, ) -> None: - """A cached read during a reload of a changed file must wait for the new snapshot. + """A cached read during a reload of a changed file must wait for the new snapshot.""" + registry_path = file_registry._file_path + last_read_mtime = file_registry._file_mtime + assert registry_path is not None + assert last_read_mtime is not None + + # Change the registry file and move its mtime past the last read. + writer = Registry("test_project", RegistryConfig(path=str(registry_path)), None) + writer.apply_project(Project(name="written_project"), commit=True) + future_mtime = last_read_mtime + 10 + os.utime(registry_path, (future_mtime, future_mtime)) - If the reload records the new file mtime before it replaces the cached proto, - a cached read in between sees an unchanged file and returns the old snapshot. - """ - _write_project_to_registry_file(file_registry, "written_project") reload_started = threading.Event() release_reload = threading.Event() real_get_registry_proto = file_registry._registry_store.get_registry_proto + results: dict[str, list[Project]] = {} def paused_get_registry_proto() -> RegistryProto: reload_started.set() release_reload.wait(timeout=10) return real_get_registry_proto() - with ( - patch.object( - file_registry._registry_store, - "get_registry_proto", - side_effect=paused_get_registry_proto, - ), - ThreadPoolExecutor(max_workers=2) as executor, + def cached_read(name: str) -> None: + results[name] = file_registry.list_projects(allow_cache=True) + + reloading_read = threading.Thread( + target=cached_read, args=("reloading",), daemon=True + ) + concurrent_read = threading.Thread( + target=cached_read, args=("concurrent",), daemon=True + ) + + # Pause the reload inside get_registry_proto and start a second read meanwhile. + with patch.object( + file_registry._registry_store, + "get_registry_proto", + side_effect=paused_get_registry_proto, ): - reloading_read = executor.submit(file_registry.list_projects, allow_cache=True) + reloading_read.start() try: - assert reload_started.wait(timeout=5) - concurrent_read = executor.submit( - file_registry.list_projects, allow_cache=True - ) - with pytest.raises(FutureTimeoutError): - concurrent_read.result(timeout=0.5) + assert reload_started.wait(timeout=5), "reload did not start" + concurrent_read.start() + concurrent_read.join(timeout=0.5) + assert concurrent_read.is_alive(), "cached read returned during reload" finally: release_reload.set() + reloading_read.join(timeout=5) + concurrent_read.join(timeout=5) - assert [p.name for p in reloading_read.result()] == ["written_project"] - assert [p.name for p in concurrent_read.result()] == ["written_project"] + assert [p.name for p in results["reloading"]] == ["written_project"] + assert [p.name for p in results["concurrent"]] == ["written_project"]