Skip to content

ref(hybridcloud): Resolve integration, org integrations and cells once per webhook - #123299

Merged
vaind merged 12 commits into
masterfrom
ivandlugos/ref/dedupe-webhook-parser-lookups
Sep 2, 2026
Merged

vaind merged 12 commits into
masterfrom
ivandlugos/ref/dedupe-webhook-parser-lookups

Conversation

@vaind

@vaind vaind commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #123298 — review that first.

Routing a webhook read three tables more than it needed to, in the shared base, so every provider paid for it. The integration was fetched and Fernet-decrypted twice (1.97 reads per request on the GitHub path) because get_response and the base's org lookup each resolved it independently; org integrations were counted and then fetched when only the ids are used; and cells were resolved by re-querying org mappings for cell_name, a column already carried on the RpcOrganizationMapping objects just fetched. Together roughly a third of the DB span time on the control-side GitHub webhook transaction.

  • integration_for_request() memoizes for the life of the parser, which is one request. The subclass hook stays uncached; the parsers that called it directly now go through the memo, which is what makes the dedup real rather than moving the second query. It needs the paired _integration_fetched flag because None is a legitimate cached result.
  • get_organizations_from_integration uses one values_list("organization_id", flat=True) instead of count-then-fetch.
  • find_cells_for_org_mappings() reads cell_name off mappings the caller already holds. It repeats the monolith short-circuit rather than reading the field — SENTRY_FALLBACK_CELL is the synthetic --monolith-- and appears in no mapping row, so inlining the comprehension would pass in prod and silently break monolith. find_cells_for_orgs stays for id-only callers.
  • GitlabRequestParser loses its own integration cache, which the base memo now covers. Its version was a class-level mutable default and only cached hits, so a webhook with no matching integration re-ran the lookup on every call.

jira_server still passes its integration explicitly: it resolves one from a URL token rather than from the request, so the memo cannot supply it.

No behaviour change, which is why this needs query-count assertions. test_forwarding_reads_each_routing_table_once (GitHub) asserts one read each of the three tables and test_forwarding_reads_the_integration_once (GitLab) covers the parser that had its own cache — both verified to fail when the memo wiring is undone. test_find_cells_for_org_mappings covers the monolith branch a siloed run would not reach.

CW-1892

… lookup

The three drop filters read the X-GitHub-Event header and the request body and
nothing else, but they sat below get_integration_from_request() and
get_cells_from_organizations(). Every webhook we were about to discard still paid
for resolving the integration and the cells it would never be routed to — and the
filters discard roughly half of inbound GitHub traffic.

Hoisting them takes that half off those queries entirely. The filters move into
_get_drop_response() so get_response() still reads as one pass.

A droppable event for an unknown or removed integration now returns 202 where it
returned 400. No delivery is lost: neither ordering stored a payload for these,
so the only change is that GitHub is no longer told a webhook we never process
failed to deliver. The ORG_INTEGRATION_DOES_NOT_EXIST halt stops being recorded
for that slice, which is the population we just decided not to route; orgs with
live activity still trip it on their forwarded events.
…e per webhook

Routing an inbound webhook read three tables more times than it needed to, on
every provider's parser:

- The integration was fetched and its encrypted metadata decrypted twice, because
  the provider's get_response and the base class's organization lookup each called
  get_integration_from_request independently.
- Org integrations were counted and then fetched, when only the ids are used.
- The cells were resolved by re-querying organization mappings for cell_name, a
  column already carried on the mappings just fetched.

The integration is now memoized for the life of the parser, which is one request.
The subclass hook stays uncached; callers go through integration_for_request.

find_cells_for_org_mappings reads cell_name off mappings the caller already holds.
It repeats the monolith short-circuit rather than reading the field, because
SENTRY_FALLBACK_CELL is a synthetic name that appears in no mapping row.

jira_server keeps passing its integration explicitly: it resolves one from a token
rather than from the request, so the memo cannot supply it.

No behaviour change. The query counts are asserted in test_github because nothing
about the responses moves when a round trip comes back.
@vaind
vaind requested review from a team as code owners September 1, 2026 08:22
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 1, 2026
@linear-code

linear-code Bot commented Sep 1, 2026

Copy link
Copy Markdown

CW-1892

… lookups

Follows the same rule as the drop filters: anything decidable from the request
alone belongs above the integration and cell queries, whatever its current share
of traffic.

A shed condition that names no integration_id sheds a whole provider, so it
matches on the provider alone. Checking it before the lookups matters more than
the drop reorder does — shedding is break-glass for a flood, and it was waiting on
three queries per request to take effect. Conditions that do name an integration_id
cannot match a None context, so those are still evaluated after the lookup.

Counting ignored shed conditions moves into a per-parser memo. It is a property of
the killswitch config rather than of any one check, and GitHub now consults the
killswitch three times per webhook.

MSTeams decides installationUpdate from the payload and hands it to control, so it
no longer resolves an integration and its cells first. Reachable via uninstalls:
an "add" is already answered by the new-installation check above it.

Jira looked like the same shape but is not. Every URL in its urlconf maps to a
class in one of the three view lists, so the trailing control-silo fallthrough is
unreachable and hoisting it would only add a dead branch.
… ivandlugos/ref/dedupe-webhook-parser-lookups

# Conflicts:
#	src/sentry/integrations/middleware/hybrid_cloud/parser.py
…verywhere

The shed killswitch is not GitHub's. Every parser that enqueues a WebhookPayload
evaluated it only inside get_response_from_webhookpayload, which is after the
integration, org-integration and org-mapping queries — so on every provider the
break-glass for a flood was waiting on the queries it exists to avoid.

A condition naming no integration_id sheds a whole provider and matches on the
provider alone, so gitlab, msteams and bitbucket now check it as soon as they know
the request will be enqueued. jira_server checks it with the id it already holds
from the token, before resolving organizations and cells.

Placement is below each parser's own request validation, not above it. The check
costs an option read, and an invalid request that would have been rejected without
touching anything should not start paying for one.

vsts and jira are left alone for the same reason: in both, the point where "this
will be enqueued" is known is inside get_integration_from_request, which validates
and queries together, so an early check could only precede their validation.
Confirmed by test_handles_missing_integration, which caught an earlier attempt
turning an unsigned Jira request into a DB read.

jira_server's changelog check reads only the body and now sits above the
organization and cell lookups, though still below the token lookup so the log line
can still name the integration that sent it.
… ivandlugos/ref/dedupe-webhook-parser-lookups
…ser's field

GitlabRequestParser has kept its own lookup result in `_integration` since before
the base gained a memo, so both were writing one attribute. It happens to work
because they cache the same value, but two owners of one field is a bug waiting
for either side to change what it stores.

Also pins the monolith assertion in test_find_cells_for_org_mappings to the literal
fallback name. Reading it from settings let the surrounding directory state set it
to a real cell, which made the assertion compare a value against itself.
… ivandlugos/ref/dedupe-webhook-parser-lookups
GitlabRequestParser kept its own lookup result so its second caller would not
re-query. The base memo now does that for every parser, so the private one is
duplicate machinery — and it was the reason the base had to avoid the obvious
field name.

Its version was also weaker: a class-level mutable default, and `if
self._integration` only cached hits, so a webhook whose integration was missing
re-ran the lookup on every call.

The base field goes back to `_integration` now that nothing else owns it.
@vaind
vaind requested review from a team and joseph-sentry September 1, 2026 11:57
Base automatically changed from ivandlugos/perf/github-webhook-drop-before-lookup to master September 2, 2026 18:38
The base branch (#123298) was squash-merged, so its commits reach master as
one new commit that shares no history with the copies still on this branch.
Every hunk they both touch conflicts even where the two sides agree.

Resolutions:

- github.py `_get_drop_response`: took master. The two sides differ only in
  the `github_drop_checks_without_own_repo_pr` gate, which master deleted in
  #122581 once the behaviour was baselined; keeping this branch's copy would
  resurrect an option that no longer exists.
- github.py `get_response`: kept `integration_for_request()`. Routing through
  the memo is what this branch is for.
- parser.py `__init__`: kept the `_integration` / `_integration_fetched`
  fields backing that memo.
- jira_server.py: took master's `get_request_body()` over the inlined
  `orjson.loads`. Same parse, plus the non-dict guard, in the shared helper.
- test_gitlab.py: took master's added imports; this side added none.
- test_github.py: both sides carried byte-identical copies of the four
  drop/shed tests, and git kept both. Dropped the second copy.

Co-Authored-By: Claude Opus 5 <[email protected]>
@vaind
vaind enabled auto-merge (squash) September 2, 2026 19:20
@vaind
vaind merged commit 8d55188 into master Sep 2, 2026
88 checks passed
@vaind
vaind deleted the ivandlugos/ref/dedupe-webhook-parser-lookups branch September 2, 2026 19:30

This branch was successfully deployed

1 active deployment
Preview — eef6a782 Deployed Sep 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants