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

fix(hc): Support split db monolith mode via smarter delegators #53365

Merged

Conversation

corps
Copy link
Contributor

@corps corps commented Jul 21, 2023

Monolith mode delegators now also pick their implementation by attempting to examine any open transactions and selecting for an implementation that would align with the currently open tranaction.

This fixes the subtle issue for hybrid cloud services like the log_service, as well as some helper methods on the organization_service, that do not invoke RPC, but rather, pick different models locally based on the silo mode. For Monolith mode, this means picking one model that works on one db, even if being invoked inside the transaction of the other db, threatening the transactional atomicity that is desired. (schedule_signal, for instance, hopes to leave an outbox behind in the same transaction as invocation).

The Sentry Options backend may also need this, but unclear. Going to analyze split db tests after this lands and follow up.

@corps corps requested a review from a team July 21, 2023 20:02
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jul 21, 2023
for model, constructor in self._constructors.items():
# Shared memory reference for the transaction.on_commit
switch = _Switch()
transaction.on_commit(switch, router.db_for_read(model))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actualy, there may be an easier way to make this check, may improve.

@codecov
Copy link

codecov bot commented Jul 21, 2023

Codecov Report

Merging #53365 (8d1bc1b) into master (bf041f3) will decrease coverage by 1.32%.
The diff coverage is 77.41%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #53365      +/-   ##
==========================================
- Coverage   79.54%   78.23%   -1.32%     
==========================================
  Files        4940     4917      -23     
  Lines      208184   208298     +114     
  Branches    35483    35490       +7     
==========================================
- Hits       165600   162960    -2640     
- Misses      37545    40154    +2609     
- Partials     5039     5184     +145     
Impacted Files Coverage Δ
src/sentry/services/hybrid_cloud/log/impl.py 74.19% <41.66%> (-2.28%) ⬇️
src/sentry/services/hybrid_cloud/__init__.py 88.95% <100.00%> (+1.37%) ⬆️

... and 272 files with indirect coverage changes


from sentry.db.postgres.transactions import django_test_transaction_water_mark
from sentry.models import AuditLogEntry, OutboxCategory, OutboxScope, RegionOutbox, UserIP
from sentry.services.hybrid_cloud.log import AuditLogEvent, LogService, UserIpEvent
from sentry.utils import metrics


class DatabaseBackedLogService(LogService):
Copy link
Member

Choose a reason for hiding this comment

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

Awesome that the service implementations themselves get simpler because of this

}
)

with override_settings(SILO_MODE=SiloMode.CONTROL):
Copy link
Member

Choose a reason for hiding this comment

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

pytest.mark.parametrize is sometimes useful for these types of matrices, not sure it'd necessarily make things more clear here though

ServiceInterface,
DelegatedByOpenTransaction(
{
User: mapping[SiloMode.CONTROL],
Copy link
Member

Choose a reason for hiding this comment

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

Thinking out loud - if more models are added here, could the silo mode be inferred from the model?

@corps corps merged commit 7b0a692 into master Jul 22, 2023
54 of 55 checks passed
@corps corps deleted the zc/ensure-monolith-services-align-with-open-transactions branch July 22, 2023 02:00
@corps corps added the Trigger: Revert Add to a merged PR to revert it (skips CI) label Jul 22, 2023
@getsentry-bot
Copy link
Contributor

PR reverted: 97c76f2

getsentry-bot added a commit that referenced this pull request Jul 22, 2023
#53365)"

This reverts commit 7b0a692.

Co-authored-by: corps <593850+corps@users.noreply.github.com>
armenzg pushed a commit that referenced this pull request Jul 24, 2023
Monolith mode delegators now *also* pick their implementation by
attempting to examine any open transactions and selecting for an
implementation that would align with the currently open tranaction.

This fixes the subtle issue for hybrid cloud services like the
log_service, as well as some helper methods on the organization_service,
that do not invoke RPC, but rather, *pick different models* locally
based on the silo mode. For Monolith mode, this means picking one model
that works on one db, even if being invoked inside the transaction of
the other db, threatening the transactional atomicity that is desired.
(`schedule_signal`, for instance, hopes to leave an outbox behind in the
same transaction as invocation).

The Sentry Options backend *may* also need this, but unclear. Going to
analyze split db tests after this lands and follow up.
armenzg pushed a commit that referenced this pull request Jul 24, 2023
#53365)"

This reverts commit 7b0a692.

Co-authored-by: corps <593850+corps@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Aug 6, 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.

4 participants