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

chore(hybridcloud) Remove role switching and update guard method #52673

Merged
merged 10 commits into from
Jul 13, 2023

Conversation

markstory
Copy link
Member

The role switching logic to catch write operations that are missing outboxes has been causing test flakiness and disrupting development workflows.

These changes finish replacing role switching with query auditing that is done during test teardown. Switching to a teardown based audit means we can't give the precise location of where the bad operation was as the stack frames have been lost. However, we are able to show the offending query and its location in the SQL logs for a test.

- Add guards for deletes on hybridcloudforeignkey models.
- Remove role creation/permisission setting.
- Add unguarded_write() context manager that emits fencing queries.
- Make in_test_psql_role_override an alias to unguarded_write
@markstory markstory requested review from a team as code owners July 11, 2023 21:00
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jul 11, 2023
_fencing_counters: MutableMapping[str, int] = defaultdict(int)


@contextlib.contextmanager
def in_test_psql_role_override(role_name: str, using: str | None = None):
Copy link
Member Author

Choose a reason for hiding this comment

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

This will be around for a bit longer until I can update getsentry.

@@ -94,6 +93,8 @@ def increment_project_counter(project, delta=1, using="default"):
# this must be idempotent because it seems to execute twice
# (at least during test runs)
def create_counter_function(app_config, using, **kwargs):
from sentry.testutils.silo import unguarded_write
Copy link
Member Author

Choose a reason for hiding this comment

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

Circular import sadness.

@@ -1,7 +1,5 @@
from typing import Optional

from sentry.models import AuditLogEntry, CommitFileChange
Copy link
Member Author

Choose a reason for hiding this comment

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

More circular import sadness.


@contextlib.contextmanager
def unguarded_write(using: str | None = None, *args: Any, **kwargs: Any):
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not 💯 sold on this name, and welcome any suggestions to improve it.

Comment on lines +283 to +293
seen_models: MutableSet[type] = set()
for app_config in apps.get_app_configs():
for model in iter_models(app_config.name):
for field in model._meta.fields:
if not isinstance(field, HybridCloudForeignKey):
continue
fk_model = field.foreign_model
if fk_model is None or fk_model in seen_models:
continue
seen_models.add(fk_model)
_protected_operations.append(protected_table(fk_model._meta.db_table, "delete"))
Copy link
Member Author

Choose a reason for hiding this comment

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

I missed adding these guards in the initial pass, but noticed these were missing when removing the role based methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

There should also be a test, somewhere, I wrote that fails if a raw deletion succeeds without throwing an exception.

@@ -10,14 +7,6 @@

@control_silo_test(stable=True)
class IntegrationTest(TestCase):
def test_cannot_delete_with_queryset(self):
Copy link
Member Author

Choose a reason for hiding this comment

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

These tests had to be removed as they now throw during teardown which we can't catch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there some alternative that ensures atleast that this logic does exist?

# TODO(mark) Move this closer to other silo code.
_fencing_counters[using] += 1
# Deprecated shim for getsentry compatibility
from sentry.testutils.silo import unguarded_write
Copy link
Contributor

Choose a reason for hiding this comment

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

does this need the using = determine_using_by_silo_mode logic for split db still?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind, it is captured inside the call.

@codecov
Copy link

codecov bot commented Jul 11, 2023

Codecov Report

Merging #52673 (6ea7521) into master (37b46b4) will increase coverage by 1.84%.
The diff coverage is 97.67%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #52673      +/-   ##
==========================================
+ Coverage   77.54%   79.39%   +1.84%     
==========================================
  Files        4921     4922       +1     
  Lines      206673   206698      +25     
  Branches    35331    35340       +9     
==========================================
+ Hits       160264   164100    +3836     
+ Misses      41347    37573    -3774     
+ Partials     5062     5025      -37     
Impacted Files Coverage Δ
src/sentry/tasks/organization_mapping.py 77.27% <66.66%> (+31.81%) ⬆️
src/sentry/models/counter.py 84.31% <100.00%> (ø)
src/sentry/models/outbox.py 95.41% <100.00%> (ø)
.../sentry/services/hybrid_cloud/organization/impl.py 79.83% <100.00%> (+6.04%) ⬆️
...services/hybrid_cloud/organization_mapping/impl.py 61.11% <100.00%> (ø)
...es/hybrid_cloud/organizationmember_mapping/impl.py 86.00% <100.00%> (ø)
src/sentry/tasks/check_auth.py 71.66% <100.00%> (ø)
src/sentry/testutils/helpers/api_gateway.py 91.13% <100.00%> (+13.92%) ⬆️
src/sentry/testutils/silo.py 84.06% <100.00%> (+3.71%) ⬆️
src/sentry/utils/migrations.py 100.00% <100.00%> (ø)
... and 1 more

... and 268 files with indirect coverage changes

markstory added a commit that referenced this pull request Jul 12, 2023
I'm having a hard time getting #52673 to pass CI because of getsentry
dependencies. These changes add the new helpers to adopt query auditing
instead of role swapping without touching role. My plan now is to get
these changes merged, then update getsentry to use query auditing and
then update sentry and remove role swapping.
markstory added a commit that referenced this pull request Jul 12, 2023
I'm having a hard time getting #52673 to pass CI because of getsentry
dependencies. These changes add the new helpers to adopt query auditing
instead of role swapping without touching role. My plan now is to get
these changes merged, then update getsentry to use query auditing and
then update sentry and remove role swapping.
@markstory markstory merged commit 07c498a into master Jul 13, 2023
@markstory markstory deleted the chore-cleanup-query-audits branch July 13, 2023 15:08
markstory added a commit that referenced this pull request Jul 13, 2023
@markstory markstory added the Trigger: Revert Add to a merged PR to revert it (skips CI) label Jul 13, 2023
@getsentry-bot
Copy link
Contributor

PR reverted: ace3ab5

getsentry-bot added a commit that referenced this pull request Jul 13, 2023
…hod (#52673)"

This reverts commit 07c498a.

Co-authored-by: markstory <24086+markstory@users.noreply.github.com>
markstory added a commit that referenced this pull request Jul 13, 2023
markstory added a commit that referenced this pull request Jul 13, 2023
Mulligan on #52673 as using testutils code in application code blows up
docker image generation.

Because of that I've moved unguarded_write to sentry.silo instead.
markstory added a commit that referenced this pull request Jul 13, 2023
…52802)

Mulligan on #52673 as using
testutils code in application code blows up docker image generation.

Because of that I've moved unguarded_write to sentry.silo instead.
michellewzhang pushed a commit that referenced this pull request Jul 13, 2023
)

The role switching logic to catch write operations that are missing
outboxes has been causing test flakiness and disrupting development
workflows.

These changes finish replacing role switching with query auditing that
is done during test teardown. Switching to a teardown based audit means
we can't give the precise location of where the bad operation was as the
stack frames have been lost. However, we are able to show the offending
query and its location in the SQL logs for a test.
michellewzhang pushed a commit that referenced this pull request Jul 13, 2023
…hod (#52673)"

This reverts commit 07c498a.

Co-authored-by: markstory <24086+markstory@users.noreply.github.com>
michellewzhang pushed a commit that referenced this pull request Jul 13, 2023
…52802)

Mulligan on #52673 as using
testutils code in application code blows up docker image generation.

Because of that I've moved unguarded_write to sentry.silo instead.
mifu67 pushed a commit that referenced this pull request Jul 13, 2023
)

The role switching logic to catch write operations that are missing
outboxes has been causing test flakiness and disrupting development
workflows.

These changes finish replacing role switching with query auditing that
is done during test teardown. Switching to a teardown based audit means
we can't give the precise location of where the bad operation was as the
stack frames have been lost. However, we are able to show the offending
query and its location in the SQL logs for a test.
mifu67 pushed a commit that referenced this pull request Jul 13, 2023
…hod (#52673)"

This reverts commit 07c498a.

Co-authored-by: markstory <24086+markstory@users.noreply.github.com>
mifu67 pushed a commit that referenced this pull request Jul 13, 2023
…52802)

Mulligan on #52673 as using
testutils code in application code blows up docker image generation.

Because of that I've moved unguarded_write to sentry.silo instead.
@github-actions github-actions bot locked and limited conversation to collaborators Jul 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components Trigger: Revert Add to a merged PR to revert it (skips CI)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants