Skip to content

Commit 4a66610

Browse files
authored
Ref #497: Bust cache when comparing push timestamps (#646)
* Compare push timestamp using cache busting * Refactor fetching monitor/changes * @smarnach feedback
1 parent c960821 commit 4a66610

File tree

8 files changed

+42
-13
lines changed

8 files changed

+42
-13
lines changed

checks/remotesettings/attachments_availability.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ async def test_url(url):
2020

2121

2222
async def run(server: str) -> CheckResult:
23-
client = KintoClient(server_url=server, bucket="monitor", collection="changes")
23+
client = KintoClient(server_url=server)
2424

2525
info = await client.server_info()
2626
base_url = info["capabilities"]["attachments"]["base_url"]
2727

2828
# Fetch collections records in parallel.
29-
entries = await client.get_records()
29+
entries = await client.get_monitor_changes()
3030
futures = [
3131
client.get_records(
3232
bucket=entry["bucket"],

checks/remotesettings/certificates_expiration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ async def fetch_collection_metadata(server_url, entry):
4040

4141

4242
async def run(server: str, min_remaining_days: int) -> CheckResult:
43-
client = KintoClient(server_url=server, bucket="monitor", collection="changes")
44-
entries = await client.get_records()
43+
client = KintoClient(server_url=server)
44+
entries = await client.get_monitor_changes()
4545

4646
# First, fetch all collections metadata in parallel.
4747
futures = [fetch_collection_metadata(server, entry) for entry in entries]

checks/remotesettings/changes_timestamps.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
For each collection the change `entry` timestamp is returned along with the
55
`collection` timestamp. The `datetime` is the human-readable version.
66
"""
7-
import random
8-
97
from poucave.typings import CheckResult
108
from poucave.utils import run_parallel, utcfromtimestamp
119

@@ -16,10 +14,9 @@
1614

1715

1816
async def run(server: str) -> CheckResult:
19-
client = KintoClient(server_url=server, bucket="monitor", collection="changes")
17+
client = KintoClient(server_url=server)
2018
# Make sure we obtain the latest list of changes (bypass webhead microcache)
21-
random_cache_bust = random.randint(999999000000, 999999999999)
22-
entries = await client.get_records(_expected=random_cache_bust)
19+
entries = await client.get_monitor_changes(bust_cache=True)
2320
futures = [
2421
client.get_records_timestamp(
2522
bucket=entry["bucket"],

checks/remotesettings/cloudfront_invalidations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def fetch_timestamps(client, bucket, collection):
2525

2626
async def run(server: str, cdn: str, min_age: int = 300) -> CheckResult:
2727
origin_client = KintoClient(server_url=server)
28-
entries = await origin_client.get_records(bucket="monitor", collection="changes")
28+
entries = await origin_client.get_monitor_changes()
2929

3030
# Fetch timestamps on source server.
3131
origin_futures = [

checks/remotesettings/push_timestamp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def get_push_timestamp(uri) -> str:
4141

4242
async def get_remotesettings_timestamp(uri) -> str:
4343
client = KintoClient(server_url=uri)
44-
entries = await client.get_records(bucket="monitor", collection="changes")
44+
entries = await client.get_monitor_changes(bust_cache=True)
4545
# Some collections are excluded (eg. preview)
4646
# https://github.com/mozilla-services/cloudops-deployment/blob/master/projects/kinto/puppet/modules/kinto/templates/kinto.ini.erb
4747
matched = [e for e in entries if "preview" not in e["bucket"]]

checks/remotesettings/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import copy
3+
import random
34
from typing import Dict, List, Optional, Tuple
45

56
import backoff
@@ -69,6 +70,15 @@ async def get_records(self, *args, **kwargs) -> List[Dict]:
6970
None, lambda: self._client.get_records(*args, **kwargs)
7071
)
7172

73+
@retry_timeout
74+
async def get_monitor_changes(self, bust_cache=False, **kwargs) -> List[Dict]:
75+
if bust_cache:
76+
if "_expected" in kwargs:
77+
raise ValueError("Pick one of `bust_cache` and `_expected` parameters")
78+
random_cache_bust = random.randint(999999000000, 999999999999)
79+
kwargs["_expected"] = random_cache_bust
80+
return await self.get_records(bucket="monitor", collection="changes", **kwargs)
81+
7282
@retry_timeout
7383
async def get_record(self, *args, **kwargs) -> Dict:
7484
loop = asyncio.get_event_loop()

checks/remotesettings/validate_signatures.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ async def validate_signature(verifier, metadata, records, timestamp):
5252

5353

5454
async def run(server: str, buckets: List[str], root_hash: str) -> CheckResult:
55-
client = KintoClient(server_url=server, bucket="monitor", collection="changes")
55+
client = KintoClient(server_url=server)
5656
entries = [
57-
entry for entry in await client.get_records() if entry["bucket"] in buckets
57+
entry
58+
for entry in await client.get_monitor_changes()
59+
if entry["bucket"] in buckets
5860
]
5961

6062
# Fetch collections records in parallel.

tests/checks/remotesettings/test_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,23 @@ async def test_user_agent(mock_responses):
139139
user_agent = mock_responses.calls[0].request.headers["User-Agent"]
140140
assert "poucave" in user_agent
141141
assert "kinto_http" in user_agent
142+
143+
144+
async def test_get_monitor_changes(mock_responses):
145+
server_url = "http://fake.local/v1"
146+
monitor_url = f"{server_url}/buckets/monitor/collections/changes/records"
147+
mock_responses.get(monitor_url, payload={})
148+
149+
client = KintoClient(server_url=server_url)
150+
151+
await client.get_monitor_changes()
152+
assert mock_responses.calls[0].request.params == {}
153+
154+
await client.get_monitor_changes(bust_cache=True)
155+
assert "_expected" in mock_responses.calls[1].request.params
156+
157+
await client.get_monitor_changes(_expected="bim")
158+
assert mock_responses.calls[2].request.params["_expected"] == "bim"
159+
160+
with pytest.raises(ValueError):
161+
await client.get_monitor_changes(bust_cache=True, _expected="boom")

0 commit comments

Comments
 (0)