Skip to content

Commit 38689ff

Browse files
authored
fix: CachingRegistry threading cache refresh (#179)
* add logging to CachingRegistry * chore: formatting * fix: add more logs to find what line is bringing down thread * fix: delete added coverage files * fix: switch to time.sleep in refresh thread * fix: remove verbose logging and add exception handling
1 parent a6881dc commit 38689ff

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

sdk/python/feast/infra/registry/caching_registry.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import atexit
22
import logging
33
import threading
4+
import time
45
import warnings
56
from abc import abstractmethod
67
from datetime import timedelta
@@ -37,6 +38,8 @@ def __init__(self, project: str, cache_ttl_seconds: int, cache_mode: str):
3738
)
3839
self.cached_registry_proto = self.proto()
3940
self.cached_registry_proto_created = _utc_now()
41+
self._stop_event = threading.Event()
42+
logger.info(f"Registry initialized with cache mode: {cache_mode}")
4043
if cache_mode == "thread":
4144
self._start_thread_async_refresh(cache_ttl_seconds)
4245
atexit.register(self._exit_handler)
@@ -458,12 +461,29 @@ def _refresh_cached_registry_if_necessary(self):
458461
def _start_thread_async_refresh(self, cache_ttl_seconds):
459462
self.refresh()
460463
if cache_ttl_seconds <= 0:
464+
logger.info("Registry cache refresh thread not started as TTL is 0")
461465
return
462-
self.registry_refresh_thread = threading.Timer(
463-
cache_ttl_seconds, self._start_thread_async_refresh, [cache_ttl_seconds]
464-
)
465-
self.registry_refresh_thread.daemon = True
466-
self.registry_refresh_thread.start()
466+
467+
def refresh_loop():
468+
while not self._stop_event.is_set():
469+
try:
470+
time.sleep(cache_ttl_seconds)
471+
if not self._stop_event.is_set():
472+
self.refresh()
473+
except Exception as e:
474+
logger.exception("Exception in refresh_loop: %s", e)
475+
476+
try:
477+
self.registry_refresh_thread = threading.Thread(
478+
target=refresh_loop, daemon=True
479+
)
480+
self.registry_refresh_thread.start()
481+
logger.info(
482+
f"Registry cache refresh thread started with TTL {cache_ttl_seconds}"
483+
)
484+
except Exception as e:
485+
logger.exception("Failed to start registry refresh thread: %s", e)
467486

468487
def _exit_handler(self):
469-
self.registry_refresh_thread.cancel()
488+
logger.info("Exiting, setting stop event for registry cache refresh thread")
489+
self._stop_event.set()

0 commit comments

Comments
 (0)