From 435405d6c44b816718a18468944b43dbe76d0103 Mon Sep 17 00:00:00 2001 From: Nachiappan Veerappan Nachiappan Date: Fri, 7 Jun 2024 09:38:03 -0700 Subject: [PATCH] Addressing review comment for making the call to redis a synchronous call. Additionally also checking if sample_rate is not set to None. --- snuba/state/cache/redis/backend.py | 10 ++++------ tests/state/test_cache.py | 18 ------------------ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/snuba/state/cache/redis/backend.py b/snuba/state/cache/redis/backend.py index eecd073c713..feefd8a35a6 100644 --- a/snuba/state/cache/redis/backend.py +++ b/snuba/state/cache/redis/backend.py @@ -306,7 +306,7 @@ def __get_value_with_simple_readthrough( return self.__codec.decode(cached_value) else: try: - value = self.__executor.submit(function).result(timeout) + value = function() self.__client.set( result_key, self.__codec.encode(value), @@ -315,9 +315,6 @@ def __get_value_with_simple_readthrough( record_cache_hit_type(RESULT_EXECUTE) if timer is not None: timer.mark("cache_set") - except concurrent.futures.TimeoutError as error: - metrics.increment("execute_timeout", tags=metric_tags) - raise TimeoutError("timed out while running query") from error except Exception as e: metrics.increment("execute_error", tags=metric_tags) raise e @@ -341,8 +338,9 @@ def get_readthrough( sample_rate = get_config( "read_through_cache.disable_lua_scripts_sample_rate", 0 ) - assert sample_rate is not None - disable_lua_scripts = random.random() < float(sample_rate) + disable_lua_scripts = sample_rate is not None and random.random() < float( + sample_rate + ) if disable_lua_scripts: return self.__get_value_with_simple_readthrough( key, function, record_cache_hit_type, timeout, timer diff --git a/tests/state/test_cache.py b/tests/state/test_cache.py index 7fe732d3195..e9179b604d0 100644 --- a/tests/state/test_cache.py +++ b/tests/state/test_cache.py @@ -172,24 +172,6 @@ def function() -> bytes: backend.get_readthrough(key, SingleCallFunction(function), noop, 1) -@pytest.mark.redis_db -def test_get_readthrough_missed_deadline_with_disable_lua_scripts( - backend: Cache[bytes], -) -> None: - set_config("read_through_cache.disable_lua_scripts_sample_rate", 1) - key = "key" - value = b"value" - - def function() -> bytes: - time.sleep(1.5) - return value - - with pytest.raises(TimeoutError): - backend.get_readthrough(key, SingleCallFunction(function), noop, 1) - - assert backend.get(key) is None - - @pytest.mark.redis_db def test_get_readthrough(backend: Cache[bytes]) -> None: key = "key"