Skip to content

Commit

Permalink
Addressing review comment for making the call to redis
Browse files Browse the repository at this point in the history
a synchronous call. Additionally also checking if sample_rate is not
set to None.
  • Loading branch information
Nachiappan Veerappan Nachiappan authored and Nachiappan Veerappan Nachiappan committed Jun 7, 2024
1 parent cf9c7d6 commit 435405d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
10 changes: 4 additions & 6 deletions snuba/state/cache/redis/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions tests/state/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 435405d

Please sign in to comment.