: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(data-imports): catch delta race when refreshing file listing (#101228)
Tom Owersffc3a4c72c72
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.pyindex 8821e453a56a..aca97c0dc14d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py@@ -5,6 +5,7 @@ import pytestfrom unittest.mock import AsyncMock, MagicMock, patchimport botocore.exceptions+import deltalake.exceptionsfrom parameterized import parameterizedfrom posthog.temporal.common.errors import NonReportableError@@ -126,6 +127,36 @@ class TestPrepareS3FilesForQuerying:assert refresh_file_uris.await_count == 2assert cp_file.await_args_list[-1].args[0] == "s3://bucket/job/my_table/part-2.parquet"+ async def test_retries_when_refresh_file_uris_hits_the_same_table_race(self):+ # `refresh_file_uris` reopens the same Delta table it's re-listing, so it can lose the same+ # compact/vacuum race the copy above did - delta-rs raises that as `TableNotFoundError`+ # ("No files in log segment"), not `FileNotFoundError`/`OSError`, so it used to escape this+ # loop uncaught on the very first unlucky refresh instead of being retried like the race above.+ vanished_file = "s3://bucket/job/my_table/part-0.parquet"+ cp_file = AsyncMock(side_effect=[FileNotFoundError(vanished_file), FileNotFoundError(vanished_file), None])+ s3 = _fake_s3(_cp_file=cp_file)+ refresh_file_uris = AsyncMock(+ side_effect=[+ deltalake.exceptions.TableNotFoundError("Generic delta kernel error: No files in log segment"),+ [vanished_file],+ ]+ )++ with (+ patch.object(util_module, "aget_s3_client", return_value=_FakeS3CM(s3)),+ patch("asyncio.sleep", new_callable=AsyncMock),+ ):+ await prepare_s3_files_for_querying(+ folder_path="job",+ table_name="my_table",+ file_uris=[vanished_file],+ delete_existing=False,+ refresh_file_uris=refresh_file_uris,+ )++ assert refresh_file_uris.await_count == 2+ assert cp_file.await_count == 3+async def test_gives_up_after_max_attempts_exhausted(self):# The retry loop is bounded: a source file that keeps vanishing on every attempt must# eventually raise instead of retrying forever.diff --git a/products/warehouse_sources/backend/temporal/data_imports/util.py b/products/warehouse_sources/backend/temporal/data_imports/util.pyindex 3b5bd4853527..5d7def9d5e65 100644--- a/products/warehouse_sources/backend/temporal/data_imports/util.py+++ b/products/warehouse_sources/backend/temporal/data_imports/util.py@@ -204,6 +204,8 @@ async def prepare_s3_files_for_querying(# S3's SlowDown rate limiting on the destination prefix.await s3._cp_file(file, f"{s3_path_for_querying}/{file_name}")+ import deltalake.exceptions # noqa: PLC0415 — keeps the heavy deltalake dep off this module's top-level import path+from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.errors import ( # noqa: PLC0415 — keeps the heavy deltalake dep off this module's top-level import pathis_transient_object_store_error,)@@ -228,7 +230,20 @@ async def prepare_s3_files_for_querying(level="error",)await asyncio.sleep(2**attempt)- file_uris = await refresh_file_uris()+ try:+ file_uris = await refresh_file_uris()+ except deltalake.exceptions.TableNotFoundError as refresh_error:+ # Re-listing reopens the same table mid-race, and can lose it the same way the+ # copy above did: delta-rs raises this (not FileNotFoundError/OSError) when the+ # rewrite in progress has left the log segment with zero readable commits for the+ # moment before its own commit lands. Keep the stale listing so the next attempt's+ # copy fails the same way and this loop retries the refresh again, instead of the+ # delta-kernel error escaping uncaught on the first unlucky refresh.+ await _log(+ f"Refreshing file listing hit the same table race (attempt "+ f"{attempt}/{_COPY_FILES_MAX_ATTEMPTS}), retrying: {refresh_error}",+ level="error",+ )except OSError as e:# s3fs wraps a CopyObject/PutObject 5xx (e.g. S3's InternalError, already retried to# exhaustion at the boto layer) as a plain OSError. That's a blip on S3's side, not a