: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(warehouse-sources): write the cdc ingest mode without the source activity log (#104543)
Daniel RC97a76fcba88e
diff --git a/products/warehouse_sources/backend/management/commands/migrate_cdc_source_to_buffered.py b/products/warehouse_sources/backend/management/commands/migrate_cdc_source_to_buffered.pyindex 0319632a959d..0b8bd27a107e 100644--- a/products/warehouse_sources/backend/management/commands/migrate_cdc_source_to_buffered.py+++ b/products/warehouse_sources/backend/management/commands/migrate_cdc_source_to_buffered.py@@ -304,15 +304,10 @@ class Command(BaseCommand):# the schedules onto a state that is genuinely unchanged, not a half-flipped one.self.stdout.write("6/7 setting cdc_ingest_mode=buffered and marking the schemas served")with transaction.atomic():- source.job_inputs = {- **(source.job_inputs or {}),- "cdc_ingest_mode": "buffered",- # Kept across a rollback: it is what tells a later flip that a `_ph_cdc_seq`- # column on the warehouse table may be one this lane wrote, not one the source- # owns.- "cdc_buffered_before": True,- }- source.save(update_fields=["job_inputs"])+ # `cdc_buffered_before` is kept across a rollback: it is what tells a later flip+ # that a `_ph_cdc_seq` column on the warehouse table may be one this lane wrote,+ # not one the source owns.+ self._write_job_inputs(source, cdc_ingest_mode="buffered", cdc_buffered_before=True)self._mark_schemas(eligible, served=True)except BaseException:self.stdout.write(self.style.WARNING("flip aborted, restoring per-schema schedules"))@@ -381,16 +376,11 @@ class Command(BaseCommand):self.stdout.write("5/6 setting cdc_ingest_mode=legacy and unmarking the schemas")with transaction.atomic():self._mark_schemas(eligible, served=False)- source.job_inputs = {- **(source.job_inputs or {}),- "cdc_ingest_mode": "legacy",- # Set here as well as on the flip, so a source flipped before this marker- # existed still carries it once it rolls back — which is the population that- # would otherwise be refused a second flip over a `_ph_cdc_seq` column the- # buffered lane wrote itself.- "cdc_buffered_before": True,- }- source.save(update_fields=["job_inputs"])+ # `cdc_buffered_before` is set here as well as on the flip, so a source flipped+ # before this marker existed still carries it once it rolls back — which is the+ # population that would otherwise be refused a second flip over a `_ph_cdc_seq`+ # column the buffered lane wrote itself.+ self._write_job_inputs(source, cdc_ingest_mode="legacy", cdc_buffered_before=True)except BaseException:# The mode is still buffered, so the schemas go back to consuming the buffer, which is# what they were doing before this command ran. Leaving them paused instead would stop@@ -412,6 +402,19 @@ class Command(BaseCommand):self.stdout.write(self.style.SUCCESS(f"Source {source_id} is now legacy."))+ def _write_job_inputs(self, source: ExternalDataSource, **updates: Any) -> None:+ """A queryset update, never `source.save()`.++ Saving goes through the activity-log mixin, which diffs the instance against the row.+ A sync changing the source's status during the drain makes that diff read every job the+ source ever ran to describe the change: gigabytes on a busy source, and an OOM-killed+ worker mid-flip.+ """+ source.job_inputs = {**(source.job_inputs or {}), **updates}+ ExternalDataSource.objects.filter(id=source.id).update(+ job_inputs=source.job_inputs, updated_at=dt.datetime.now(dt.UTC)+ )+def _mark_schemas(self, schemas: list[ExternalDataSchema], *, served: bool) -> None:"""The per-schema opt-in `serves_buffered_lane` reads. `cdc_buffered_before` is never cleared.diff --git a/products/warehouse_sources/backend/tests/management/test_migrate_cdc_source_to_buffered.py b/products/warehouse_sources/backend/tests/management/test_migrate_cdc_source_to_buffered.pyindex 96b972a0a39d..3b7e78a60326 100644--- a/products/warehouse_sources/backend/tests/management/test_migrate_cdc_source_to_buffered.py+++ b/products/warehouse_sources/backend/tests/management/test_migrate_cdc_source_to_buffered.py@@ -120,6 +120,19 @@ class TestMigrateCDCSourceToBuffered(BaseTest):mocks["unpause"].assert_called_once()mocks["unpause_schema"].assert_called_once_with(str(schema.id))+ @parameterized.expand([("flip", None, "buffered"), ("rollback", "buffered", "legacy")])+ def test_the_mode_is_written_without_saving_the_source(self, _name, ingest_mode, expected_mode):+ source = self._source(ingest_mode=ingest_mode)+ self._schema(source, "users")++ with _mocked_side_effects():+ with patch.object(ExternalDataSource, "save", side_effect=AssertionError("source.save must not run")):+ self._run(source, rollback=ingest_mode == "buffered")++ source.refresh_from_db()+ assert source.job_inputs["cdc_ingest_mode"] == expected_mode+ assert source.job_inputs["cdc_buffered_before"]+def test_flip_aborts_when_a_buffer_file_survives_the_purge(self):# The purge itself is best-effort; a surviving file would replay legacy-delivered rows# against a lane with no watermark, silently. Abort with the mode unchanged.