Skip to content
Merged
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
14 changes: 10 additions & 4 deletions src/corva/cache_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ def delete_all(self) -> None:
class HashMigrator:
MINIMUM_ALLOWED_REDIS_SERVER = semver.Version(major=7, minor=4, patch=0)
NEW_HASH_PREFIX = "migrated/"
_version_checked: bool = False

def __init__(self, hash_name: str, client: redis.Redis):
self.hash_name = hash_name
self.zset_name = f"{hash_name}.EXPIREAT"
self.client = client

def check_redis_server_version(self) -> None:
if HashMigrator._version_checked:
return

# Require Redis 7.4+ for per-field TTL commands
redis_version_str = self.client.info(section="server")["redis_version"]
server_version = semver.Version.parse(version=redis_version_str)
Expand All @@ -72,6 +76,8 @@ def check_redis_server_version(self) -> None:
f"less then {self.MINIMUM_ALLOWED_REDIS_SERVER} -> "
f"incompatible with used python SDK version `{version('corva-sdk')}`")

HashMigrator._version_checked = True

def run(self) -> bool:
"""Prepare parallel new-style cache while keeping legacy structures.

Expand All @@ -91,16 +97,16 @@ def run(self) -> bool:

from corva import Logger

# Legacy structure must exist; otherwise nothing to do
if not self.client.exists(self.zset_name):
return False

new_hash_name = self.NEW_HASH_PREFIX + self.hash_name

# If new hash already exists, consider migration already done
if self.client.exists(new_hash_name):
return False

# Legacy structure must exist; otherwise nothing to do
if not self.client.exists(self.zset_name):
return False

# Current server time in ms
sec, micro = self.client.time()
now_ms = int(sec) * 1000 + int(micro) // 1000
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_cache_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def current_redis_server_time(redis_client):


def test_server_version_incompatible_with_sdk(redis_client):
HashMigrator._version_checked = False

with (pytest.raises(RuntimeError) as exc):
with mock.patch.object(redis_client, "info",
Expand Down