Skip to content

Compactor

The ingestor writes many small files. The compactor merges them into large files, builds the index sidecars for each merged file, and removes snapshots and files that the table does not use any more. It is the same program as the ingestor (ingestor compact), but it runs as a separate process, so that a large merge cannot take CPU from ingestion.

Compactor flow Compactor flow

Open the interactive diagram for pan and zoom, search, and export to PNG or SVG.

When a pass runs

Each backlog tick starts a pass. A pass examines each dataset in turn. After each time in the schedule, the next pass uses the rules of the schedule, which select more:

Pass Configuration key Default Selects
Backlog tick compaction.backlogCheckIntervalMinutes 15 minutes Partitions with more than compaction.backlogFileThreshold (default 100) small files.
Schedule compaction.schedule (cron, UTC) 0 2 * * * Partitions with two or more small files, and files that have no text index.

A small file is a file below compaction.minFileSizeMb (default 32). The first tick occurs when the compactor starts. The compactor examines the schedule only at a tick, so a scheduled pass starts at the first tick after the scheduled time. If a long pass continues through more than one scheduled time, the compactor does one scheduled pass, not one for each. The compactor counts scheduled times from its start: a scheduled time that passes while the compactor is stopped is not done later.

The sizes and thresholds on this page can have a different value for each dataset (ingestion.datasets[].compaction). The tick interval and the schedule apply to all datasets.

A partition that the schedule selects is not always merged. If its small files are fewer than backlogFileThreshold and their total size is below minFileSizeMb, the merged file would also be small. The compactor then leaves the partition until more data arrives, unless one of its files needs a text index (see Text index backfill).

The pass, for each selected partition

  1. Skip if done. The name of each output file comes from a hash of the input file names. If the table already contains that output, a previous run merged this partition, and the compactor skips it. This makes it safe to run a pass again after a crash.
  2. Merge. The compactor reads one input file at a time. It writes files of compaction.targetSizeMb (default 128) and keeps only one of them in memory: when a file is full, the compactor uploads it (step 4) and starts the next. The memory that a pass needs thus does not grow with the size of the partition. If the dataset has sortColumns, the rows in each output file are in that order. The compactor then writes sorted runs to the temporary disk of the pod and merges them back, as many runs at a time as fit in 384 MB of memory. The compactor reads all the input files with the current table schema. At the end it makes sure that the number of rows out is equal to the number of rows in.
  3. Build the sidecars. In the same read, the compactor builds a full-text index for the dataset's tantivy.indexingColumns, a bloom filter for its bloomFilterColumns, and distinct-value sketches for its ndvColumns. The name of each sidecar contains the UUID of its data file.
  4. Upload. The compactor writes each merged file and its sidecars to object storage when the file is full, during the merge. If the pass stops before the commit, the table does not contain the files that it uploaded. Orphan cleanup deletes them later. If the partition's small files are unchanged, the next pass writes them again with the same names.
  5. Commit. One Iceberg commit replaces the small files with the merged files. Queries see the old files or the new files, not a mixture.

Merge sequence

This is the order of the calls for one partition. An ingestor can add files to the table at the same time. If it commits first, the compactor sends its commit again.

Compactor merge sequence Compactor merge sequence

Open the interactive diagram.

Rules that protect your data

Upload first, then commit. The table does not refer to a file until that file is in your storage. If the upload of a data file fails, there is no commit.

A sidecar that is missing is not an error. If the upload of a sidecar fails, the commit continues. The query gateway scans a file that has no sidecar. It does not skip it.

An ingestor append does not undo a merge. The commit replaces only the files that the compactor merged. If another writer commits first, the catalog refuses the commit. The compactor then reads the table again and sends the commit again, up to compaction.commitRetryMax times (default 5). It does not merge the files again. Each refusal increments tracelake_catalog_commit_conflicts_total{component="compactor"}. If the retries run out, that partition fails, and the pass stops for that dataset. A later pass merges it again. If two compactors merge the same files, the second one finds that its input files are gone, and that partition fails in that pass.

The commit does not delete the old files. Queries that started before the commit can continue to read them. The two maintenance loops remove them later.

Text index backfill

The full-text index is built only by the compactor. The ingestor does not build it, because that would take CPU from ingestion. A file that the ingestor wrote at its maximum size is not a small file, so the size rule does not select it. On a scheduled pass, the compactor therefore also selects a file when all of these are true:

  • the dataset has tantivy.indexingColumns;
  • the file has no text index;
  • TraceLake wrote the file, at <namespace>/<dataset>/data/ under storage.destinationUri, with a name that TraceLake gives (a UUID or a 64-character hash). Trino and Spark write to the same folder with other names;
  • the file is not larger than 2 × targetSizeMb.

The limit is two times the target, not the target, because the compactor closes a file after the file crosses targetSizeMb. Its own files are therefore a little larger than the target, and a file that loses its index must be able to get it back. The compactor reads each input file fully into memory, so it does not select a larger file. It counts such files in tracelake_compactor_unindexed_oversized_files (see the runbook).

Maintenance loops

Two loops run on their own timers, independently of the compaction pass. A third, the retention delete, runs only if you turn it on:

Loop Interval key Default Does
Expire snapshots maintenance.expireSnapshotsIntervalMinutes 60 minutes Removes table snapshots older than compaction.snapshotRetentionHours (default 24). It deletes no files.
Orphan cleanup maintenance.orphanCleanupIntervalMinutes 60 minutes Deletes data files that no snapshot refers to and that are older than maintenance.orphanMinAgeHours (default 24). It deletes only files with a name that TraceLake gives: a file that another engine wrote stays, so that a long write of that engine does not lose its files before it commits. That file stays also after a merge or a retention delete removes it from the table. To delete such files, run the cleanup of that engine on the table, for example Trino ALTER TABLE … EXECUTE remove_orphan_files(retention_threshold => '7d'). That cleanup also deletes the unreferenced files of TraceLake, so set its threshold to maintenance.orphanMinAgeHours or more. Just before each delete it reads the file's ETag and last-modified time again, and keeps the file if it was uploaded again since the listing (runbook). It deletes the sidecars of each file with it. It also deletes the files in the table's metadata/ folder that no snapshot refers to, with the same age limit, but never a *.metadata.json file. The cleanup of another engine can delete a file of TraceLake before the orphan cleanup of TraceLake does: until the next orphan cleanup of TraceLake after the snapshot expiry (maintenance.orphanCleanupIntervalMinutes, default 60), or while the compactor is stopped. The sidecars of that file then stay in _metadata/<namespace>/datasets/<dataset>/, because the cleanup of the other engine does not examine this folder, and TraceLake deletes a sidecar only together with its data file. No query reads these sidecars, so they cost only storage. Dropping a dataset deletes them. To delete them before that, delete a sidecar _metadata/<namespace>/datasets/<dataset>/puffin/<name>.puffin or _metadata/<namespace>/datasets/<dataset>/idx/<name>.idx only if it is older than maintenance.orphanMinAgeHours and the data file <namespace>/<dataset>/data/<name>.parquet does not exist. Both paths are under storage.destinationUri.

The age limit protects files that an ingestor has uploaded but not committed yet. For data files, orphan cleanup lists only <namespace>/<dataset>/data/ under storage.destinationUri. For metadata, it lists only the table's own metadata/ folder. Thus it never sees the files of a deployment on a different namespace (see Where the files are). It also does not see files written before the paths contained the namespace: when compaction replaces one of those, nothing deletes it.

Orphan cleanup has no switch: it starts with the compactor. Before you start the compactor for the first time in a deployment, run ingestor compact --config <file> --orphan-dry-run. It lists the files that a cleanup would delete, and deletes nothing.

Retention delete

With maintenance.retentionDeleteEnabled: true, a third loop removes old data from the table. It runs every maintenance.retentionIntervalMinutes (default 1440, once a day), only for datasets with a retentionDays value.

The loop removes a data file from the table only if all the rows of the file are old:

  • The newest timeColumn value in the file, from the file's statistics, is more than retentionDays + maintenance.retentionSafetyMarginDays (default 1) days before now. The age of the file object is not used, so a file of late data is kept until its rows are old.
  • No row of the file has a NULL timeColumn value. Those rows have no age and are always kept.

The loop never removes part of a file. A file with old and new rows stays until all of its rows are old. A file with no timeColumn statistics stays always. Such files are in tracelake_retention_unreclaimable_bytes.

The loop removes at most 10 000 files for each dataset in one pass, the oldest first, so that the first pass over a long archive is not one very large commit. The next passes remove the rest. To change the limit, start the compactor with --retention-max-files <N>.

The loop removes the files with one Iceberg commit for each dataset, through the catalog. It deletes no object. Snapshot expiry and orphan cleanup then delete the objects and their sidecars, as for a file that a merge replaced. Other engines that read the table see the rows go at the commit and never find a missing file. The gateway stops serving a row at retentionDays, before the delete, so the delete does not change the result of a query on the dataset. A query on the cold tier only (tier=cold, or the <namespace>_cold schema) and other engines see all rows, so for them the old rows go at the delete.

While the loop is on, a merge does not use a file whose newest row is older than retentionDays and that has no NULL timeColumn value. The safety margin is then the time before such a file can be deleted, so a merge never loses a race with the delete. With a margin of 0 that protection is gone: a merge can fail when the delete removes one of its files. The failure counts on tracelake_compactor_runs_total, and the next pass merges again. No data is lost.

Before you turn the loop on, run ingestor compact --config <file> --retention-dry-run. It lists the files that a pass would remove and their size, and deletes nothing. It also works while retentionDeleteEnabled is false. It previews every dataset that has a retentionDays value, and does nothing else: it does not accept --dataset, --date-range, --partition, --dry-run or --orphan-dry-run. Its exit code is not zero if the preview failed for a dataset.

If a pass fails for one dataset, the compactor writes retention pass failed to its log and tries again at the next interval. Look at the retention row of the dashboard to see that old data goes: the oldest data stays near retentionDays + the margin.

States of a data file

Data file states Data file states

Open the interactive diagram.

A file goes through the numbered states in order. The diagram draws no arrows between them.

  • Live. The current snapshot of the table refers to the file.
  • Replaced. A merge commit replaced the file, or a retention delete removed it. Only older snapshots refer to it, and queries that use those snapshots can continue to read it.
  • Unreferenced. Snapshot expiry removed the last snapshot that referred to the file.
  • Deleted. Orphan cleanup deleted the file and its sidecars, after the file was older than maintenance.orphanMinAgeHours.

A file that a writer has uploaded but not committed yet has no snapshot that refers to it. To orphan cleanup, it looks the same as an unreferenced file. The age limit prevents its deletion before the commit. If the writer stopped and the commit never occurs, the file becomes an unreferenced file, and orphan cleanup deletes it later.

When a pass fails

The compactor counts the failure, writes the cause to its log, and continues with the next dataset. The process does not stop, so the schedule continues to operate for the other datasets. An alert on the failure counter is the signal to the operator.

If the files of one partition have column types that are not compatible, the compactor does not merge that partition. The small files stay, and the pass reports a failure. You must migrate that partition with an external engine.

compaction.runtimeDeadlineHours (default 6) does not stop a pass. A pass that runs longer increments a counter for an alert.

Manual runs

You can run one pass by hand. A run is a manual run only if it has one or more of the six flags below --config. With none of them, ingestor compact --config <file> starts the long-running compactor, which includes orphan cleanup.

A manual run does one pass and exits. It does not expire snapshots or delete files, and its exit code is not zero if the pass failed. Like a scheduled pass, it also selects files that have no text index.

Flag Effect
--config <file> Required. The configuration file, the same file that the ingestor reads.
--dataset <name> Compact one dataset only.
--date-range <from>:<to> Compact the partitions in an inclusive YYYY-MM-DD date range. The dataset must have a date partition: a year, month, day or hour transform, or a column with a year, month, day or date_int normalizer. After a change to partitionMapping, partitions written before the change are not in a --date-range or --partition slice; run with --dataset only to compact them. Also use --dataset only between a change to partitionMapping and the ingestor start that applies it: in that window a slice reads the table's partitions by the new mapping.
--partition <field>=<value> Compact the partitions with this value. The field is the key of a partitionMapping entry, or the partition field name for an entry with a transform (see Partitions), for example ts_day=20746 (days since 1970-01-01). You can repeat the flag; a partition must then match all the values.
--dry-run List the files that a run would merge. Merge and commit nothing.
--orphan-dry-run List the files that orphan cleanup would delete. Delete nothing, and do no compaction.
--retention-dry-run List the files that the retention delete would remove, and their size. Delete nothing, and do no compaction.

--retention-max-files <N> is not a manual-run flag: it sets the most files that one retention pass removes for each dataset (default 10000), for the long-running compactor and for --retention-dry-run.

You can run several compactors at the same time on different datasets or date ranges.