-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(releases): Prevent row-lock contention on last_seen bump #115443
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d67d08
fix(releases): Prevent row-lock contention on last_seen bump for Rele…
yuvmen 5ef02bb
fix(releases): Simplify SQL filter to last_seen__lt=datetime
yuvmen 0e710d2
Merge branch 'master' into yuvmen/fix-rpe-last-seen-contention
yuvmen 23a4662
fix(releases): Move cache import to module level in tests
yuvmen 7bdb919
ref(releases): Extract try_bump_last_seen into shared utility
yuvmen 30204da
fix(typing): Use Any for try_bump_last_seen params to avoid mypy errors
yuvmen bdd1e90
fix(releases): Use Protocol for typing, restore bumped=false for crea…
yuvmen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime as dt | ||
| from datetime import timedelta | ||
| from typing import Any, Protocol | ||
|
|
||
| from django.core.cache import cache | ||
| from django.db.utils import OperationalError | ||
|
|
||
|
|
||
| class HasLastSeen(Protocol): | ||
| id: int | ||
| last_seen: dt | ||
|
|
||
|
|
||
| def try_bump_last_seen( | ||
| *, | ||
| model_class: Any, | ||
| instance: HasLastSeen, | ||
| datetime: dt, | ||
| bump_key: str, | ||
| cache_key: str, | ||
| metrics_tags: dict[str, str], | ||
| ) -> bool: | ||
| """Throttled last_seen bump — at most once per 60s per row via a cache-based lock.""" | ||
| if instance.last_seen >= datetime - timedelta(seconds=60): | ||
| metrics_tags["bumped"] = "false" | ||
| return False | ||
|
|
||
| if not cache.add(bump_key, "1", timeout=60): | ||
| metrics_tags["bumped"] = "skipped" | ||
| return False | ||
|
|
||
| try: | ||
| model_class.objects.filter(id=instance.id, last_seen__lt=datetime).update( | ||
| last_seen=datetime | ||
| ) | ||
| except OperationalError: | ||
| metrics_tags["bumped"] = "error" | ||
| return False | ||
|
|
||
| instance.last_seen = datetime | ||
| cache.set(cache_key, instance, 3600) | ||
| metrics_tags["bumped"] = "true" | ||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.