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
16 changes: 15 additions & 1 deletion sdk/python/feast/infra/registry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +1507 to +1508

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 Offline writes use superseded destinations

While a changed local registry reloads in sync mode, _get_registry_proto can return the old snapshot. The reload publishes _file_mtime before replacing cached_registry_proto. Offline writes then persist rows to the previous batch source.

Learn more

In sync mode, a cached lookup must reload a local registry file when its modification time advances. The locked reload currently records that time before it loads the replacement proto. A second lookup during the load sees the new time and returns the old snapshot without waiting. Before the fast path, the second lookup waited for the reload. Offline writes resolve their destination once and send the batch to that source, so completing the registry reload does not repair a misdirected write.

Example: Apply a feature view update that moves its batch source from table A to table B, with the same schema. A cached lookup starts reloading the registry. An offline write resolves the view during that reload and writes to A instead of B.

Recommended fix: Do not advance _file_mtime until the replacement proto has been loaded and published. Keep readers of a changed local file on the locked path while that load is in progress. Add an event-driven test that pauses a change-triggered reload inside get_registry_proto and verifies that a second cached lookup waits for the new snapshot.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in ff1ce31. The locked reload path no longer stores the new file mtime before it reloads the file. It already stored the mtime after the reload, so only the early assignment is removed, and a cached read during the reload now sees the file as changed and waits on the lock.

Added test_cached_read_waits_for_reload_of_modified_local_file, which pauses a change-triggered reload inside get_registry_proto and checks that a second cached read waits and then returns the new snapshot. It fails on the previous commit and passes on master and with this fix.


with self._refresh_lock:
# For file-based registries in sync mode, check file modification time
# to detect changes immediately, not just based on TTL
Expand All @@ -1516,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

Expand Down Expand Up @@ -1553,6 +1557,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
):
Expand Down
103 changes: 103 additions & 0 deletions sdk/python/tests/unit/infra/registry/test_file_registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import tempfile
import threading
from datetime import timedelta
from unittest.mock import patch

import pytest

Expand All @@ -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


Expand Down Expand Up @@ -185,3 +191,100 @@ 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 wait for a refresh in progress."""
results: list[list[Project]] = []

def cached_read() -> None:
results.append(file_registry.list_projects(allow_cache=True))

thread = threading.Thread(target=cached_read, daemon=True)

# 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 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)
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_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."""
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))

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()

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.start()
try:
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 results["reloading"]] == ["written_project"]
assert [p.name for p in results["concurrent"]] == ["written_project"]
Loading