Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): chunk checkout.com payment searches to avoid 422s (#79050)

Tom Owersa5f61d5efd96
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/payments.py b/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/payments.py
index 0a1f98401434..3acf4e412ff4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/payments.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/payments.py
@@ -45,6 +45,12 @@ DEFAULT_BACKFILL_DAYS = 365
# A window that still fills a whole page at this span can't be split further; anything
# past it is yielded with an error log rather than silently truncated.
MIN_WINDOW = timedelta(seconds=1)
+# The search endpoint's documented behaviour is a 90-day default lookback when no range
+# is given, with no documented support for much larger custom ranges; a full backfill
+# window (up to `DEFAULT_BACKFILL_DAYS`) sent as one request 422s in practice. Chunking
+# the overall sync range to this span keeps every request within the documented norm;
+# each chunk is still split further on page-fullness as before.
+MAX_SEARCH_WINDOW = timedelta(days=90)
# Bound one sync's API usage: searches are one call per ~1000 payments, fan-out lookups
# are one call per payment/customer/instrument. A capped run stops after the last fully
# processed window, so the next run continues from its watermark.
@@ -165,6 +171,26 @@ def _iter_payment_windows(
yield from _iter_payment_windows(session, auth, api_base, _Window(start=middle, end=window.end), logger, budget)
+def _iter_bounded_payment_windows(
+ session: requests.Session,
+ auth: OAuth2Auth,
+ api_base: str,
+ start: datetime,
+ end: datetime,
+ logger: FilteringBoundLogger,
+ budget: _SyncBudget,
+) -> Iterator[tuple[_Window, list[dict[str, Any]]]]:
+ """Walk the full sync range in `MAX_SEARCH_WINDOW`-sized chunks, each further split
+ by `_iter_payment_windows` on page-fullness."""
+ cursor = start
+ while cursor < end:
+ chunk_end = min(cursor + MAX_SEARCH_WINDOW, end)
+ yield from _iter_payment_windows(session, auth, api_base, _Window(start=cursor, end=chunk_end), logger, budget)
+ if budget.exhausted:
+ return
+ cursor = chunk_end
+
+
def _fanout_get(
session: requests.Session,
auth: OAuth2Auth,
@@ -276,9 +302,7 @@ def _get_rows(
seen_ids: set[str] = set()
chunk: list[dict[str, Any]] = []
- for window, payments in _iter_payment_windows(
- session, auth, hosts["api"], _Window(start=start, end=now), logger, budget
- ):
+ for window, payments in _iter_bounded_payment_windows(session, auth, hosts["api"], start, now, logger, budget):
for payment in payments:
if schema_name == "payments":
chunk.append(_strip_links(payment))
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/tests/test_checkout_com_payments.py b/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/tests/test_checkout_com_payments.py
index 386c1adb1393..32908b2f6b4b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/tests/test_checkout_com_payments.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/checkout_com/tests/test_checkout_com_payments.py
@@ -1,3 +1,4 @@
+from datetime import datetime
from typing import Any, Optional
import pytest
@@ -8,6 +9,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.checkout_c
CheckoutComResumeConfig,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.checkout_com.payments import (
+ MAX_SEARCH_WINDOW,
checkout_com_payments_source,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
@@ -174,18 +176,26 @@ class TestPaymentsWindowWalking:
]
@pytest.mark.parametrize(
- "start_date, should_use_incremental_field, last_value, expected_from",
+ "start_date, should_use_incremental_field, last_value, expected_from, expected_search_count",
[
- (None, False, None, "2023-03-02T00:00:00Z"),
- ("2024-01-01", False, None, "2024-01-01T00:00:00Z"),
- ("2024-01-01", True, "2024-02-01T00:00:00Z", "2024-02-01T00:00:00Z"),
+ # A year-long default backfill exceeds MAX_SEARCH_WINDOW, so it's chunked
+ # into 5 requests (4 full 90-day chunks plus a 5-day remainder).
+ (None, False, None, "2023-03-02T00:00:00Z", 5),
+ ("2024-01-01", False, None, "2024-01-01T00:00:00Z", 1),
+ ("2024-01-01", True, "2024-02-01T00:00:00Z", "2024-02-01T00:00:00Z", 1),
],
)
@mock.patch(SESSION_PATCH)
def test_range_start_resolution(
- self, mock_make_session, start_date, should_use_incremental_field, last_value, expected_from
+ self,
+ mock_make_session,
+ start_date,
+ should_use_incremental_field,
+ last_value,
+ expected_from,
+ expected_search_count,
):
- session = _FakeSession(search_responses=[_search_page([])])
+ session = _FakeSession(search_responses=[_search_page([]) for _ in range(expected_search_count)])
mock_make_session.side_effect = [session]
rows = _rows(
@@ -198,8 +208,31 @@ class TestPaymentsWindowWalking:
)
assert rows == []
+ assert len(session.searches) == expected_search_count
assert session.searches[0]["json"]["from"] == expected_from
- assert session.searches[0]["json"]["to"] == NOW
+ assert session.searches[-1]["json"]["to"] == NOW
+
+ @mock.patch(SESSION_PATCH)
+ def test_default_backfill_is_chunked_to_max_search_window(self, mock_make_session):
+ # The search API 422s on a full one-year range sent as a single request, so the
+ # overall sync range is walked in MAX_SEARCH_WINDOW-sized chunks.
+ session = _FakeSession(search_responses=[_search_page([]) for _ in range(5)])
+ mock_make_session.side_effect = [session]
+
+ _rows(_source("payments"))
+
+ spans = [
+ (
+ datetime.fromisoformat(s["json"]["from"].replace("Z", "+00:00")),
+ datetime.fromisoformat(s["json"]["to"].replace("Z", "+00:00")),
+ )
+ for s in session.searches
+ ]
+ assert all(end - start <= MAX_SEARCH_WINDOW for start, end in spans)
+ # Chunks are contiguous and ascending, covering the full backfill range.
+ assert all(spans[i][1] == spans[i + 1][0] for i in range(len(spans) - 1))
+ assert spans[0][0] == datetime.fromisoformat("2023-03-02T00:00:00+00:00")
+ assert spans[-1][1] == datetime.fromisoformat(NOW.replace("Z", "+00:00"))
@mock.patch(SESSION_PATCH)
def test_resume_checkpoint_wins_over_watermark(self, mock_make_session):
@@ -261,7 +294,7 @@ class TestPaymentActionsFanout:
)
mock_make_session.side_effect = [session]
- rows = _rows(_source("payment_actions"))
+ rows = _rows(_source("payment_actions", start_date="2024-02-28"))
assert rows == [
{
@@ -305,6 +338,7 @@ class TestPaymentActionsFanout:
schema_name="payment_actions",
logger=logger,
resumable_source_manager=manager,
+ start_date="2024-02-28",
)
rows = _rows(response)
@@ -340,7 +374,7 @@ class TestReferencedRecordFanout:
)
mock_make_session.side_effect = [session]
- rows = _rows(_source(schema_name))
+ rows = _rows(_source(schema_name, start_date="2024-02-28"))
assert rows == [{"id": record_id, "payment_requested_on": "2024-02-29T06:00:00Z"}]
assert [lookup["url"] for lookup in session.lookups] == [url]
@@ -358,7 +392,7 @@ class TestReferencedRecordFanout:
mock_make_session.side_effect = [session]
manager = _FakeManager()
- rows = _rows(_source("customers", manager=manager))
+ rows = _rows(_source("customers", manager=manager, start_date="2024-02-28"))
assert [row["id"] for row in rows] == ["cus_2"]
# A deleted record doesn't fail the window; it still checkpoints.