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-warehouse): keep delta compaction inside its memory slot (#109789)

Tom Owers576e148b3b63
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/maintenance.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/maintenance.py
index 79a5a204ee17..cc50f6c37f94 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/maintenance.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/maintenance.py
@@ -1,6 +1,9 @@
+import os
import json
+import time
import asyncio
import datetime as dt
+import resource
from collections import defaultdict
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any
@@ -21,6 +24,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.del
is_offset_overflow_compaction_error,
is_transient_maintenance_error,
)
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.memory_governor import get_governor
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.ops import (
ObjectStorePermissionDeniedError,
execute_with_conflict_retry,
@@ -69,13 +73,97 @@ DEFAULT_COMPACT_TARGET_SIZE_BYTES = 100 * 1024 * 1024
# can't be avoided at any reasonable bin size still surfaces to error tracking instead of looping.
COMPACT_OFFSET_OVERFLOW_RETRIES = 3
+# delta-rs bins files by their compressed size but holds each bin decoded while it rewrites it, so a
+# bin of highly compressible text (JSON documents, say) can decode to many times its target size.
+# Keep one bin's decoded bytes at half the 2 GiB Arrow offset limit, and inside the load slot the
+# compaction runs in. A running task holds its bin's decoded batches plus the writer's buffers for
+# the file it produces, so it is budgeted at twice the decoded bin.
+COMPACT_MAX_DECODED_BIN_BYTES = 1024 * 1024 * 1024
+COMPACT_MIN_TARGET_SIZE_BYTES = 8 * 1024 * 1024
+_COMPACT_TASK_MEMORY_FACTOR = 2
+# Footers of the largest files only: compacted files are the largest, and they set the worst case.
+_COMPACT_RATIO_SAMPLE_FILES = 2
+
+
+@frozen
+class CompactionPlan:
+ target_size: int | None
+ max_concurrent_tasks: int | None
+ compression_ratio: float | None
+ slot_budget_mb: float | None
+
+
+def plan_compaction(compression_ratio: float | None, slot_budget_mb: float | None) -> CompactionPlan:
+ """Bin size and parallelism that keep one compaction inside its memory slot."""
+ rounded_ratio = round(compression_ratio, 2) if compression_ratio is not None else None
+ rounded_slot = round(slot_budget_mb, 1) if slot_budget_mb is not None else None
+ if compression_ratio is None or (slot_budget_mb is not None and slot_budget_mb <= 0):
+ return CompactionPlan(
+ target_size=None,
+ max_concurrent_tasks=None,
+ compression_ratio=rounded_ratio,
+ slot_budget_mb=rounded_slot,
+ )
+
+ ratio = max(compression_ratio, 1.0)
+ decoded_cap = float(COMPACT_MAX_DECODED_BIN_BYTES)
+ slot_budget_bytes = slot_budget_mb * 1024 * 1024 if slot_budget_mb is not None else None
+ if slot_budget_bytes is not None:
+ decoded_cap = min(decoded_cap, slot_budget_bytes / _COMPACT_TASK_MEMORY_FACTOR)
+ safe_target_size = decoded_cap / ratio
+ if safe_target_size < COMPACT_MIN_TARGET_SIZE_BYTES:
+ return CompactionPlan(
+ target_size=None,
+ max_concurrent_tasks=None,
+ compression_ratio=rounded_ratio,
+ slot_budget_mb=rounded_slot,
+ )
+
+ target_size = int(min(DEFAULT_COMPACT_TARGET_SIZE_BYTES, safe_target_size))
+ max_concurrent_tasks = None
+ if slot_budget_bytes is not None:
+ per_task_bytes = target_size * ratio * _COMPACT_TASK_MEMORY_FACTOR
+ max_concurrent_tasks = max(1, min(os.cpu_count() or 1, int(slot_budget_bytes // per_task_bytes)))
+ return CompactionPlan(
+ target_size=target_size,
+ max_concurrent_tasks=max_concurrent_tasks,
+ compression_ratio=rounded_ratio,
+ slot_budget_mb=rounded_slot,
+ )
+
+
+def _sample_compression_ratio(table: deltalake.DeltaTable) -> float | None:
+ """Decoded bytes per stored byte, from the footers of the table's largest files, or None if unreadable."""
+ sizes = table._table.get_add_file_sizes()
+ by_size = sorted(sizes.items(), key=lambda item: -(item[1] or 0))[:_COMPACT_RATIO_SAMPLE_FILES]
+ largest = {path.rpartition("/")[2] for path, _ in by_size}
+ decoded = stored = sampled = 0
+ for fragment in table.to_pyarrow_dataset().get_fragments():
+ if sampled >= _COMPACT_RATIO_SAMPLE_FILES:
+ break
+ if fragment.path.rpartition("/")[2] not in largest:
+ continue
+ metadata = fragment.metadata
+ for index in range(metadata.num_row_groups):
+ row_group = metadata.row_group(index)
+ decoded += row_group.total_byte_size
+ stored += sum(row_group.column(column).total_compressed_size for column in range(row_group.num_columns))
+ sampled += 1
+ return decoded / stored if stored else None
+
+
+def _peak_rss_mb() -> float:
+ # ru_maxrss is KiB on Linux, the platform this runs on.
+ return round(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, 1)
+
+
# How long a tombstoned file survives before vacuum may delete it. Readers that pin an older
# table version must stay inside this window — see MAX_SNAPSHOT_ROLLBACK in the fan-out
# warehouse-parent reader, which derives its bound from this value.
VACUUM_RETENTION = dt.timedelta(hours=24)
-def removable_file_count(file_sizes: Iterable[int]) -> int:
+def removable_file_count(file_sizes: Iterable[int], target_size: int = DEFAULT_COMPACT_TARGET_SIZE_BYTES) -> int:
"""The fewest files a compaction is sure to remove from one partition, given the sizes of its files.
delta-rs keeps a partition's files in their original order and packs neighbours into bins up to the
@@ -87,9 +175,9 @@ def removable_file_count(file_sizes: Iterable[int]) -> int:
compaction that removes files, and a layout that compaction cannot improve never starts one.
"""
sizes = list(file_sizes)
- small = [size for size in sizes if size < DEFAULT_COMPACT_TARGET_SIZE_BYTES // 2]
+ small = [size for size in sizes if size < target_size // 2]
runs = len(sizes) - len(small) + 1
- output_files = runs + 2 * sum(small) // DEFAULT_COMPACT_TARGET_SIZE_BYTES
+ output_files = runs + 2 * sum(small) // target_size
return max(0, len(small) - output_files)
@@ -99,18 +187,54 @@ class _PartitionFileStats:
removable_files: int
-def _partition_file_stats(table: deltalake.DeltaTable) -> list[_PartitionFileStats]:
+def _partition_file_stats(
+ table: deltalake.DeltaTable, target_size: int = DEFAULT_COMPACT_TARGET_SIZE_BYTES
+) -> list[_PartitionFileStats]:
"""Stats for each partition, read from the Delta log with no S3 request."""
# Each partition value is one directory, and an unpartitioned table keeps its files at the root.
sizes: defaultdict[str, list[int]] = defaultdict(list)
for path, size in table._table.get_add_file_sizes().items():
sizes[path.rpartition("/")[0]].append(size or 0)
return [
- _PartitionFileStats(size_bytes=sum(partition_sizes), removable_files=removable_file_count(partition_sizes))
+ _PartitionFileStats(
+ size_bytes=sum(partition_sizes),
+ removable_files=removable_file_count(partition_sizes, target_size),
+ )
for partition_sizes in sizes.values()
]
+@frozen
+class _SmallFileFragmentation:
+ fragmented: bool
+ max_removable: int
+ total_removable: int
+ compactable_over_budget: bool
+
+
+def _small_file_fragmentation(
+ partitions: Iterable[_PartitionFileStats], table_wide_small_files: bool
+) -> _SmallFileFragmentation:
+ partitions = list(partitions)
+ max_removable = max((partition.removable_files for partition in partitions), default=0)
+ total_removable = sum(partition.removable_files for partition in partitions)
+ budget = settings.DATA_WAREHOUSE_TARGET_PARTITION_BYTES
+ compactable_over_budget = any(
+ partition.size_bytes > budget and partition.removable_files > 0 for partition in partitions
+ )
+ fragmented = (
+ max_removable >= DEFAULT_COMPACT_REMOVABLE_FILES_PER_PARTITION_THRESHOLD
+ or (table_wide_small_files and total_removable >= DEFAULT_COMPACT_REMOVABLE_FILES_THRESHOLD)
+ or compactable_over_budget
+ )
+ return _SmallFileFragmentation(
+ fragmented=fragmented,
+ max_removable=max_removable,
+ total_removable=total_removable,
+ compactable_over_budget=compactable_over_budget,
+ )
+
+
class DeltaMaintenance:
"""Compaction, vacuuming, and the vacuum-watermark cadence for one schema's Delta table.
@@ -140,14 +264,41 @@ class DeltaMaintenance:
)
await self._logger.adebug(json.dumps(vacuum_stats))
- async def _compact(self, table: deltalake.DeltaTable) -> None:
- await self._logger.adebug("Compacting table...")
- target_size = DEFAULT_COMPACT_TARGET_SIZE_BYTES
+ async def _plan_compaction(self, table: deltalake.DeltaTable) -> CompactionPlan:
+ try:
+ ratio = await asyncio.to_thread(_sample_compression_ratio, table)
+ except Exception as e:
+ await self._logger.awarning(f"compact: could not sample the compression ratio: {e}")
+ ratio = None
+ return plan_compaction(ratio, get_governor().slot_budget_mb())
+
+ async def _compact(self, table: deltalake.DeltaTable, plan: CompactionPlan | None = None) -> bool:
+ plan = plan or await self._plan_compaction(table)
+ target_size = plan.target_size
+ if target_size is None:
+ await self._logger.awarning(
+ "compact: skipping because no memory-safe plan is available",
+ compact_compression_ratio=plan.compression_ratio,
+ compact_slot_budget_mb=plan.slot_budget_mb,
+ )
+ return False
+ max_concurrent_tasks = plan.max_concurrent_tasks
+ pod_mb_before = get_governor().pod.current_mb()
+ started = time.monotonic()
+ await self._logger.ainfo(
+ "Compacting table...",
+ compact_target_size=target_size,
+ compact_max_concurrent_tasks=max_concurrent_tasks,
+ compact_compression_ratio=plan.compression_ratio,
+ compact_slot_budget_mb=plan.slot_budget_mb,
+ pod_memory_mb=pod_mb_before,
+ peak_rss_mb=_peak_rss_mb(),
+ )
attempt = 0
while True:
- def _compact_op(size: int = target_size) -> dict[str, Any]:
- return table.optimize.compact(target_size=size)
+ def _compact_op(size: int = target_size, tasks: int | None = max_concurrent_tasks) -> dict[str, Any]:
+ return table.optimize.compact(target_size=size, max_concurrent_tasks=tasks)
try:
compact_stats = await execute_with_conflict_retry(
@@ -162,11 +313,24 @@ class DeltaMaintenance:
raise
attempt += 1
target_size //= 2
+ # The failed attempt's memory may not be freed yet, so the retry runs one bin at a time.
+ max_concurrent_tasks = 1
await self._logger.awarning(
f"compact: byte array offset overflow, retrying with smaller "
- f"target_size={target_size} (attempt {attempt}/{COMPACT_OFFSET_OVERFLOW_RETRIES})"
+ f"target_size={target_size} (attempt {attempt}/{COMPACT_OFFSET_OVERFLOW_RETRIES})",
+ pod_memory_mb=get_governor().pod.current_mb(),
+ peak_rss_mb=_peak_rss_mb(),
)
+ await self._logger.ainfo(
+ "compact: done",
+ compact_duration_s=round(time.monotonic() - started, 1),
+ compact_files_added=compact_stats.get("numFilesAdded"),
+ compact_files_removed=compact_stats.get("numFilesRemoved"),
+ pod_memory_mb=get_governor().pod.current_mb(),
+ peak_rss_mb=_peak_rss_mb(),
+ )
await self._logger.adebug(json.dumps(compact_stats))
+ return True
async def _vacuum_due(self, last_vacuum_version: int | None, commit_threshold: int) -> bool:
# The same cadence `vacuum_if_stale` applies. An unseeded watermark is never due.
@@ -280,7 +444,8 @@ class DeltaMaintenance:
effective_partitions = max(partition_count or 1, 1)
files_per_partition = total_files / effective_partitions
- fragmented = files_per_partition > threshold or total_files > total_threshold
+ count_fragmented = files_per_partition > threshold or total_files > total_threshold
+ fragmented = count_fragmented
stats = (
f"total_files={total_files}, partitions={effective_partitions}, "
f"files_per_partition={files_per_partition:.1f}, threshold={threshold}, "
@@ -288,30 +453,45 @@ class DeltaMaintenance:
)
if compact_small_files and not fragmented:
partitions = await asyncio.to_thread(_partition_file_stats, table)
- max_removable = max((partition.removable_files for partition in partitions), default=0)
- total_removable = sum(partition.removable_files for partition in partitions)
# Repartition detection runs after this pass and compares partition bytes to its budget.
# Small files take more bytes at rest than the same rows after compaction, so an
# over-budget partition that compaction can shrink must be compacted before it is measured.
- budget = settings.DATA_WAREHOUSE_TARGET_PARTITION_BYTES
- compactable_over_budget = any(
- partition.size_bytes > budget and partition.removable_files > 0 for partition in partitions
- )
- fragmented = (
- max_removable >= DEFAULT_COMPACT_REMOVABLE_FILES_PER_PARTITION_THRESHOLD
- or (table_wide_small_files and total_removable >= DEFAULT_COMPACT_REMOVABLE_FILES_THRESHOLD)
- or compactable_over_budget
- )
+ small_file_stats = _small_file_fragmentation(partitions, table_wide_small_files)
+ fragmented = small_file_stats.fragmented
stats += (
- f", max_removable_files_per_partition={max_removable}, removable_files={total_removable}, "
- f"compactable_over_budget={compactable_over_budget}"
+ f", max_removable_files_per_partition={small_file_stats.max_removable}, "
+ f"removable_files={small_file_stats.total_removable}, "
+ f"compactable_over_budget={small_file_stats.compactable_over_budget}"
)
if not fragmented:
await self._logger.adebug(f"compact_if_fragmented: skipping ({stats})")
return False
+ if count_fragmented:
+ await self._logger.ainfo(f"compact_if_fragmented: triggering compact ({stats})")
+ if not await self._compact(table):
+ return False
+ await self._vacuum(table)
+ return True
+
+ plan = await self._plan_compaction(table)
+ if plan.target_size is None:
+ await self._compact(table, plan)
+ return False
+
+ if compact_small_files:
+ partitions = await asyncio.to_thread(_partition_file_stats, table, plan.target_size)
+ fragmented = _small_file_fragmentation(partitions, table_wide_small_files).fragmented
+ if not fragmented:
+ await self._logger.adebug(
+ f"compact_if_fragmented: skipping; no files are removable at planned target_size="
+ f"{plan.target_size} ({stats})"
+ )
+ return False
+
await self._logger.ainfo(f"compact_if_fragmented: triggering compact ({stats})")
- await self._compact(table)
+ if not await self._compact(table, plan):
+ return False
await self._vacuum(table)
return True
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/memory_governor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/memory_governor.py
index 4774bdf32f4e..cc996a865ce5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/memory_governor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/memory_governor.py
@@ -393,6 +393,11 @@ class MemoryGovernor:
usable = limit_mb * self.config.safety - self.config.reserve_mb
return max(0.0, usable) / self.config.max_concurrent
+ def slot_budget_mb(self) -> float | None:
+ """The memory slice one load slot may use, or None when the pod's limit is unreadable."""
+ limit_mb = self.pod.limit_mb()
+ return None if limit_mb is None else self._per_upsert_budget_mb(limit_mb)
+
@contextlib.asynccontextmanager
async def admit(self, *, source_bytes: int, n_partitions: int | None = None) -> AsyncIterator[Admission]:
"""Size one upsert to its memory slice and yield the knobs to run it with.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_maintenance.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_maintenance.py
index 8d7f24ccb833..31e517d264b8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_maintenance.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta/test/test_maintenance.py
@@ -12,7 +12,11 @@ from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.maintenance import (
COMPACT_OFFSET_OVERFLOW_RETRIES,
+ DEFAULT_COMPACT_TARGET_SIZE_BYTES,
+ CompactionPlan,
DeltaMaintenance,
+ _sample_compression_ratio,
+ plan_compaction,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.ops import (
ObjectStorePermissionDeniedError,
@@ -190,6 +194,18 @@ class TestCompactIfFragmented:
maintenance = _make_maintenance(mock_delta)
with (
override_settings(DATA_WAREHOUSE_TARGET_PARTITION_BYTES=500 * _MB),
+ patch.object(
+ maintenance,
+ "_plan_compaction",
+ AsyncMock(
+ return_value=CompactionPlan(
+ target_size=DEFAULT_COMPACT_TARGET_SIZE_BYTES,
+ max_concurrent_tasks=1,
+ compression_ratio=1.0,
+ slot_budget_mb=1356.8,
+ )
+ ),
+ ),
patch.object(maintenance, "_compact", AsyncMock()) as mock_compact,
patch.object(maintenance, "_vacuum", AsyncMock()),
):
@@ -251,7 +267,20 @@ class TestCompactConflictRetry:
)
mock_delta.vacuum = MagicMock(return_value=[])
- compacted = await _make_maintenance(mock_delta).compact_if_fragmented(partition_count=1, threshold=1)
+ maintenance = _make_maintenance(mock_delta)
+ with patch.object(
+ maintenance,
+ "_plan_compaction",
+ AsyncMock(
+ return_value=CompactionPlan(
+ target_size=DEFAULT_COMPACT_TARGET_SIZE_BYTES,
+ max_concurrent_tasks=1,
+ compression_ratio=1.0,
+ slot_budget_mb=1356.8,
+ )
+ ),
+ ):
+ compacted = await maintenance.compact_if_fragmented(partition_count=1, threshold=1)
assert compacted is True
assert mock_delta.optimize.compact.call_count == 2
@@ -276,7 +305,7 @@ class TestCompactOffsetOverflow:
)
mock_delta.vacuum = MagicMock(return_value=[])
- await _make_maintenance(mock_delta)._compact(mock_delta)
+ await _make_maintenance(mock_delta)._compact(mock_delta, plan_compaction(1.0, None))
assert mock_delta.optimize.compact.call_count == 2
first_target_size = mock_delta.optimize.compact.call_args_list[0].kwargs["target_size"]
@@ -294,7 +323,7 @@ class TestCompactOffsetOverflow:
mock_delta.optimize.compact = MagicMock(side_effect=overflow_error)
with pytest.raises(deltalake.exceptions.DeltaError):
- await _make_maintenance(mock_delta)._compact(mock_delta)
+ await _make_maintenance(mock_delta)._compact(mock_delta, plan_compaction(1.0, None))
assert mock_delta.optimize.compact.call_count == COMPACT_OFFSET_OVERFLOW_RETRIES + 1
@@ -304,11 +333,124 @@ class TestCompactOffsetOverflow:
mock_delta.optimize.compact = MagicMock(side_effect=deltalake.exceptions.DeltaError("no protocol found"))
with pytest.raises(deltalake.exceptions.DeltaError):
- await _make_maintenance(mock_delta)._compact(mock_delta)
+ await _make_maintenance(mock_delta)._compact(mock_delta, plan_compaction(1.0, None))
mock_delta.optimize.compact.assert_called_once()
+class TestCompactionMemoryBounds:
+ @parameterized.expand(
+ [
+ # Incompressible data keeps delta-rs's own bin size; the slot fits a few bins at once.
+ ("incompressible", 1.0, 1356.8, DEFAULT_COMPACT_TARGET_SIZE_BYTES, 6),
+ ("modest_ratio", 2.0, 1356.8, DEFAULT_COMPACT_TARGET_SIZE_BYTES, 3),
+ # Wide JSON documents: 100 MB on disk decodes past the 2 GiB offset limit, so the bin shrinks
+ # until it decodes to half the slot and only one bin runs at a time.
+ ("wide_json", 25.0, 1356.8, int(1356.8 * _MB / 2 / 25), 1),
+ ("extreme_ratio_skips", 500.0, 1356.8, None, None),
+ ("zero_budget_skips", 1.0, 0.0, None, None),
+ # No readable limit (local dev): only the decoded bin is capped, delta-rs picks parallelism.
+ ("no_limit", 25.0, None, int(1024 * _MB / 25), None),
+ ("no_limit_no_ratio_skips", None, None, None, None),
+ ]
+ )
+ def test_plan_keeps_one_compaction_inside_its_slot(
+ self,
+ _name: str,
+ ratio: float | None,
+ slot_mb: float | None,
+ expected_target: int | None,
+ expected_tasks: int | None,
+ ) -> None:
+ with patch(f"{_MAINTENANCE_MODULE}.os.cpu_count", return_value=7):
+ plan = plan_compaction(ratio, slot_mb)
+
+ assert plan.target_size == expected_target
+ assert plan.max_concurrent_tasks == expected_tasks
+ if slot_mb is not None and plan.max_concurrent_tasks is not None:
+ assert plan.target_size is not None
+ decoded_per_task = plan.target_size * max(ratio or 1.0, 1.0) * 2
+ assert decoded_per_task * plan.max_concurrent_tasks <= slot_mb * _MB
+
+ @pytest.mark.asyncio
+ async def test_small_file_trigger_rechecks_removals_at_planned_target(self) -> None:
+ file_sizes = {f"f{i}.parquet": 30 * _MB for i in range(30)}
+ mock_delta = MagicMock()
+ mock_delta.file_uris.return_value = [f"s3://bucket/table/{path}" for path in file_sizes]
+ mock_delta._table.get_add_file_sizes.return_value = file_sizes
+ maintenance = _make_maintenance(mock_delta)
+ plan = CompactionPlan(
+ target_size=27 * _MB,
+ max_concurrent_tasks=1,
+ compression_ratio=25.0,
+ slot_budget_mb=1356.8,
+ )
+ with (
+ patch.object(maintenance, "_plan_compaction", AsyncMock(return_value=plan)),
+ patch.object(maintenance, "_compact", AsyncMock()) as compact,
+ patch.object(maintenance, "_vacuum", AsyncMock()) as vacuum,
+ ):
+ ran = await maintenance.compact_if_fragmented(partition_count=1, compact_small_files=True)
+
+ assert ran is False
+ compact.assert_not_awaited()
+ vacuum.assert_not_awaited()
+
+ @parameterized.expand([("compressible_json", True), ("random", False)])
+ def test_samples_the_compression_ratio_from_parquet_footers(self, _name: str, compressible: bool) -> None:
+ with tempfile.TemporaryDirectory() as path:
+ for row_id in range(3):
+ payload = ('{"field": "value", "n": 1}' * 4000) if compressible else os.urandom(50_000).hex()
+ deltalake.write_deltalake(path, pa.table({"id": [row_id], "data": [payload]}), mode="append")
+
+ ratio = _sample_compression_ratio(deltalake.DeltaTable(path))
+
+ assert ratio is not None
+ if compressible:
+ assert ratio > 10
+ else:
+ assert ratio < 3
+
+ @pytest.mark.asyncio
+ async def test_compact_runs_with_the_planned_bin_size_and_parallelism(self) -> None:
+ mock_delta = MagicMock()
+ mock_delta.optimize.compact = MagicMock(return_value={"numFilesAdded": 1, "numFilesRemoved": 4})
+ with (
+ patch(f"{_MAINTENANCE_MODULE}._sample_compression_ratio", return_value=25.0),
+ patch(f"{_MAINTENANCE_MODULE}.get_governor") as governor,
+ ):
+ governor.return_value.slot_budget_mb.return_value = 1356.8
+ governor.return_value.pod.current_mb.return_value = 4096.0
+ await _make_maintenance(mock_delta)._compact(mock_delta)
+
+ kwargs = mock_delta.optimize.compact.call_args.kwargs
+ assert kwargs == {"target_size": int(1356.8 * _MB / 2 / 25), "max_concurrent_tasks": 1}
+
+ @pytest.mark.asyncio
+ async def test_offset_overflow_retry_runs_one_bin_at_a_time(self) -> None:
+ mock_delta = MagicMock()
+ mock_delta.optimize.compact = MagicMock(
+ side_effect=[
+ deltalake.exceptions.DeltaError(
+ 'Generic error: task 7 panicked with message "byte array offset overflow"'
+ ),
+ {"numFilesAdded": 1},
+ ]
+ )
+ with (
+ patch(f"{_MAINTENANCE_MODULE}._sample_compression_ratio", return_value=1.0),
+ patch(f"{_MAINTENANCE_MODULE}.get_governor") as governor,
+ patch(f"{_MAINTENANCE_MODULE}.os.cpu_count", return_value=7),
+ ):
+ governor.return_value.slot_budget_mb.return_value = 1356.8
+ governor.return_value.pod.current_mb.return_value = 4096.0
+ await _make_maintenance(mock_delta)._compact(mock_delta)
+
+ first, second = (call.kwargs for call in mock_delta.optimize.compact.call_args_list)
+ assert first["max_concurrent_tasks"] == 6
+ assert second == {"target_size": first["target_size"] // 2, "max_concurrent_tasks": 1}
+
+
class TestVacuum:
@pytest.mark.asyncio
async def test_retries_on_commit_conflict_then_succeeds(self):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
index d197fb514ac8..bb7f33b77671 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/processor.py
@@ -440,6 +440,7 @@ def _run_post_load_for_already_processed_batch(export_signal: ExportSignalMessag
internal_schema.add_pyarrow_schema(pyarrow_schema_from_arrow_exportable(delta_table.schema()))
internal_schema.add_pyarrow_table(pa_table)
table_schema_dict = internal_schema.to_hogql_types()
+ del pa_table
prepared_queryable_folder = await run_post_load_operations(
job=job,
@@ -1299,6 +1300,10 @@ def _process_message_reported(
internal_schema=internal_schema,
)
+ # Post-load compaction owns a full governor slot. Drop the input batch before entering it so
+ # Arrow's buffers do not remain resident alongside the compaction working set.
+ del pa_table
+
# Every run whose final batch landed in this write completes now, in load order.
if constituents is not None:
finals = final_run_signals(constituents)