-
-
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
Open
yuvmen
wants to merge
4
commits into
master
Choose a base branch
from
yuvmen/fix-rpe-last-seen-contention
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−16
Open
Changes from all commits
Commits
Show all changes
4 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| from typing import TypedDict | ||
|
|
||
| from django.db import models | ||
| from django.db.utils import OperationalError | ||
| from django.utils import timezone | ||
|
|
||
| from sentry.backup.scopes import RelocationScope | ||
|
|
@@ -82,14 +83,24 @@ def _get_or_create_impl(cls, release, project, environment, datetime, metrics_ta | |
|
|
||
| metrics_tags["created"] = "true" if created else "false" | ||
|
|
||
| # Same as releaseenvironment model. Minimizes last_seen updates to once a minute | ||
| # Same as releaseenvironment model. Minimizes last_seen updates to once a minute. | ||
| # The cache.add acts as a distributed lock so only one worker attempts the | ||
| # DB update per row per 60s, avoiding row-lock contention on hot releases. | ||
| bump_key = f"releaseprojectenv_bump:{instance.id}" | ||
| if not created and instance.last_seen < datetime - timedelta(seconds=60): | ||
| cls.objects.filter( | ||
| id=instance.id, last_seen__lt=datetime - timedelta(seconds=60) | ||
| ).update(last_seen=datetime) | ||
| instance.last_seen = datetime | ||
| cache.set(cache_key, instance, 3600) | ||
| metrics_tags["bumped"] = "true" | ||
| if cache.add(bump_key, "1", timeout=60): | ||
| try: | ||
| cls.objects.filter(id=instance.id, last_seen__lt=datetime).update( | ||
| last_seen=datetime | ||
| ) | ||
| except OperationalError: | ||
| metrics_tags["bumped"] = "error" | ||
| return instance | ||
| instance.last_seen = datetime | ||
| cache.set(cache_key, instance, 3600) | ||
| metrics_tags["bumped"] = "true" | ||
| else: | ||
| metrics_tags["bumped"] = "skipped" | ||
|
Comment on lines
+91
to
+103
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With two of these (and potentially more) should we have a context manager to encapsulate this behavior? with periodic_update(key=bump_key, timeout=60, metrics_tags=metrics_tags):
cls.objects.filter(id=instance.id, last_seen__lt=datetime).update(last_seen=datetime) |
||
| else: | ||
| metrics_tags["bumped"] = "false" | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a use-case that our buffers system can handle. We currently use buffers to handle updating last_seen times on Group and Release models. It could be used here as well. However, what you have is operationally lighter and if we don't need to capture the tail of each update set it will work well.