Skip to content

Commit 0faf56c

Browse files
committed
ref(hybridcloud): Require the claim's deadline and mailbox on drains
The transitional fallbacks from the drain-claim work come out: valid_until and mailbox are now required, so _begin_drain reduces to building the claim and asking whether it lapsed — no head-row read and no nullable mailbox downstream. The drain_mailbox_parallel alias goes with them; dispatch stopped enqueueing it when the drains merged. Must deploy at least one deploy after the drain merge: a drain enqueued before it carries neither argument and now raises TypeError. That failure is noisy rather than lossy — the rows keep their claim's schedule_for and the next dispatcher re-claims them once it passes.
1 parent 87b4c7e commit 0faf56c

2 files changed

Lines changed: 228 additions & 244 deletions

File tree

src/sentry/hybridcloud/tasks/deliver_webhooks.py

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -391,40 +391,17 @@ def _begin_drain(
391391
payload_id: int,
392392
claimed_count: int,
393393
dispatcher: str | None,
394-
valid_until: float | None,
395-
mailbox: str | None,
394+
valid_until: float,
395+
mailbox: str,
396396
) -> _MailboxClaim | None:
397-
"""
398-
The claim a drain runs under, or None when it must stand down first.
399-
400-
A drain enqueued before dispatch sent the mailbox and deadline reads both off
401-
its head row: its claim already wrote its deadline as the rows' schedule_for.
402-
That one-query fallback goes away once no such drains are left in flight.
403-
"""
404-
deadline = (
405-
datetime.datetime.fromtimestamp(valid_until, tz=datetime.UTC)
406-
if valid_until is not None
407-
else None
408-
)
409-
if mailbox is None or deadline is None:
410-
head = (
411-
WebhookPayload.objects.filter(id=payload_id)
412-
.values_list("mailbox_name", "schedule_for")
413-
.first()
414-
)
415-
if head is None:
416-
# Head already delivered or discarded; whoever claimed the mailbox
417-
# next is delivering the rest.
418-
return None
419-
mailbox = mailbox if mailbox is not None else head[0]
420-
deadline = deadline if deadline is not None else head[1]
397+
"""The claim a drain runs under, or None when it has already lapsed."""
421398
_set_webhook_delivery_sentry_context(mailbox, _provider_from_mailbox(mailbox))
422399
claim = _MailboxClaim(
423400
claimed=claimed_count,
424401
head_id=payload_id,
425402
mailbox_name=mailbox,
426403
dispatcher=dispatcher,
427-
valid_until=deadline,
404+
valid_until=datetime.datetime.fromtimestamp(valid_until, tz=datetime.UTC),
428405
)
429406
if claim.lapsed(log_key="deliver_webhook.stale_claim", extra={"id": payload_id}):
430407
return None
@@ -913,15 +890,15 @@ def drain_mailbox(
913890
payload_id: int,
914891
claimed_count: int,
915892
dispatcher: str | None = None,
916-
valid_until: float | None = None,
917-
mailbox: str | None = None,
893+
*,
894+
valid_until: float,
895+
mailbox: str,
918896
) -> None:
919897
"""
920898
Deliver webhooks from the mailbox that `payload_id` is the head of — in order,
921899
or in concurrent waves when the claim qualifies (`_MailboxClaim.threaded`).
922900
923-
The arguments are one claim flattened for the wire (`_MailboxClaim.task_args`);
924-
each defaults so a rolling deploy can bind drains the previous version sent.
901+
The arguments are one claim flattened for the wire (`_MailboxClaim.task_args`).
925902
"""
926903
claim = _begin_drain(payload_id, claimed_count, dispatcher, valid_until, mailbox)
927904
if claim is not None:
@@ -1244,30 +1221,6 @@ def _run_parallel_delivery_batch(
12441221
return delivered
12451222

12461223

1247-
@instrumented_task(
1248-
name="sentry.hybridcloud.tasks.deliver_webhooks.drain_mailbox_parallel",
1249-
namespace=hybridcloud_control_tasks,
1250-
# The pre-merge task's deadline, kept for the in-flight drains this shim serves.
1251-
processing_deadline_duration=int(BATCH_SCHEDULE_OFFSET.total_seconds() + 10),
1252-
silo_mode=SiloMode.CONTROL,
1253-
)
1254-
def drain_mailbox_parallel(
1255-
payload_id: int,
1256-
claimed_count: int,
1257-
dispatcher: str | None = None,
1258-
valid_until: float | None = None,
1259-
mailbox: str | None = None,
1260-
) -> None:
1261-
"""
1262-
Transitional alias from when sequential and parallel delivery were separate
1263-
tasks; `drain_mailbox` now runs both modes. Dispatch no longer enqueues this,
1264-
so it is deletable once no drains from the previous deploy are left in flight.
1265-
"""
1266-
claim = _begin_drain(payload_id, claimed_count, dispatcher, valid_until, mailbox)
1267-
if claim is not None:
1268-
_drain_mailbox(claim)
1269-
1270-
12711224
def deliver_message_parallel(payload: WebhookPayload) -> tuple[WebhookPayload, Exception | None]:
12721225
try:
12731226
perform_request(payload)

0 commit comments

Comments
 (0)