Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(relay): Write project config revision to Redis #75523

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 14 additions & 3 deletions src/sentry/relay/projectconfig_cache/redis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from collections.abc import Mapping
from typing import Any

import zstandard

Expand Down Expand Up @@ -28,11 +30,14 @@ def validate(self):
def __get_redis_key(self, public_key):
return f"relayconfig:{public_key}"

def set_many(self, configs):
def __get_redis_rev_key(self, public_key):
return f"{self.__get_redis_key(public_key)}.rev"

def set_many(self, configs: dict[str, Mapping[str, Any]]):
metrics.incr("relay.projectconfig_cache.write", amount=len(configs), tags={"action": "set"})

# Note: Those are multiple pipelines, one per cluster node
p = self.cluster.pipeline()
p = self.cluster.pipeline(transaction=False)
Copy link
Member Author

Choose a reason for hiding this comment

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

There is no need for this pipeline to be transactional (default value), all keys are independent.

for public_key, config in configs.items():
serialized = json.dumps(config).encode()
compressed = zstandard.compress(serialized, level=COMPRESSION_LEVEL)
Expand All @@ -42,6 +47,8 @@ def set_many(self, configs):
metrics.distribution("relay.projectconfig_cache.size", len(compressed), unit="byte")

p.setex(self.__get_redis_key(public_key), REDIS_CACHE_TIMEOUT, compressed)
if rev := config.get("rev"):
p.setex(self.__get_redis_rev_key(public_key), REDIS_CACHE_TIMEOUT, rev)

p.execute()

Expand All @@ -63,6 +70,10 @@ def get(self, public_key):
rv = zstandard.decompress(rv_b).decode()
except (TypeError, zstandard.ZstdError):
# assume raw json
pass
rv = rv_b
Copy link
Member Author

Choose a reason for hiding this comment

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

Drive by fix ... That would have just blown up before because rv is not defined.

return json.loads(rv)
return None

def get_rev(self, public_key) -> str | None:
if value := self.cluster_read.get(self.__get_redis_rev_key(public_key)):
return value.decode("utf-8")
16 changes: 13 additions & 3 deletions tests/sentry/relay/test_projectconfig_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def test_delete_count(monkeypatch):
@django_db_all
def test_read_write():
cache = redis.RedisProjectConfigCache()
my_key = "fake-dsn-1"
cache.set_many({my_key: "my-value"})
assert cache.get(my_key) == "my-value"

dsn1 = "fake-dsn-1"
value1 = {"my-value": "foo", "rev": "my_rev_123"}

dsn2 = "fake-dsn-2"
value2 = {"my-value": "bar", "has_no_rev": "123"}

cache.set_many({dsn1: value1, dsn2: value2})
assert cache.get(dsn1) == value1
assert cache.get(dsn2) == value2

assert cache.get_rev(dsn1) == "my_rev_123"
assert cache.get_rev(dsn2) is None
Loading