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): avoid per-file S3 LIST calls when copying query files (#73452)

Tom Owers4c8af43957ff
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.py
new file mode 100644
index 000000000000..b016d373242d
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_util.py
@@ -0,0 +1,50 @@
+from types import SimpleNamespace
+
+from unittest.mock import AsyncMock, patch
+
+from products.warehouse_sources.backend.temporal.data_imports import util as util_module
+from products.warehouse_sources.backend.temporal.data_imports.util import prepare_s3_files_for_querying
+
+
+def _fake_s3(**kwargs):
+ defaults = {
+ "invalidate_cache": lambda: None,
+ "_ls": AsyncMock(return_value=[]),
+ "_exists": AsyncMock(return_value=False),
+ "_cp_file": AsyncMock(),
+ "_copy": AsyncMock(),
+ "_rm": AsyncMock(),
+ }
+ defaults.update(kwargs)
+ return SimpleNamespace(**defaults)
+
+
+class _FakeS3CM:
+ def __init__(self, s3):
+ self._s3 = s3
+
+ async def __aenter__(self):
+ return self._s3
+
+ async def __aexit__(self, *exc):
+ return False
+
+
+class TestPrepareS3FilesForQuerying:
+ async def test_copies_files_with_cp_file_not_copy(self):
+ # `_copy()` globs the source and probes whether the destination is a directory,
+ # each requiring its own S3 ListObjectsV2 call. Copying many files concurrently
+ # through `_copy()` multiplies into enough LIST traffic to trigger S3's SlowDown
+ # rate limiting (see the OSError/ClientError SlowDown pair this regresses).
+ s3 = _fake_s3()
+
+ with patch.object(util_module, "aget_s3_client", return_value=_FakeS3CM(s3)):
+ await prepare_s3_files_for_querying(
+ folder_path="job",
+ table_name="my_table",
+ file_uris=["s3://bucket/job/my_table/part-0.parquet", "s3://bucket/job/my_table/part-1.parquet"],
+ delete_existing=False,
+ )
+
+ assert s3._cp_file.await_count == 2
+ s3._copy.assert_not_awaited()
diff --git a/products/warehouse_sources/backend/temporal/data_imports/util.py b/products/warehouse_sources/backend/temporal/data_imports/util.py
index fc39a3a16c6a..7e556297fe38 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/util.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/util.py
@@ -160,7 +160,12 @@ async def prepare_s3_files_for_querying(
async def copy_file(file: str) -> None:
async with semaphore:
file_name = file.replace(f"{s3_folder_for_schema}/", "")
- await s3._copy(file, f"{s3_path_for_querying}/{file_name}")
+ # _cp_file() copies a single known source to a known destination key directly.
+ # The generic _copy() also globs the source and probes whether the destination
+ # is a directory, each requiring its own S3 ListObjectsV2 call — with hundreds of
+ # files copied concurrently, that multiplies into enough LIST traffic to trigger
+ # S3's SlowDown rate limiting on the destination prefix.
+ await s3._cp_file(file, f"{s3_path_for_querying}/{file_name}")
await asyncio.gather(*[copy_file(file) for file in file_uris])