When Rotation Moves the Boundary: Recovering an Unread Access-Log Tail

Technical illustration of an active access log rotating into an archive while an unread tail is added to an analytics database

Log rotation is usually treated as an operational detail: the active file is renamed, a new one is created, and logging continues. For an incremental analyzer, rotation is also a state boundary. The analyzer must remember exactly which bytes it has already processed, even after those bytes move under a different filename.

The need for backfill surfaced while I was initially populating the database from archived access logs. I needed to import historical rotation files, including compressed archives, and compare the results with AWStats to check statistical parity. The specific unread-tail edge case was identified by Tim Urista in a comment on my earlier article; I investigated it and turned it into a reproducible test and operator procedure.

During that work it became clear that backfill was not merely a helper command for importing archives. The same processed-prefix matching mechanism also makes it possible to read an unread tail after rotation without counting the prefix that was already processed.

The boundary

The scenario is simple:

  1. Process the initial part of access.log.
  2. Append new records, but do not run the analyzer.
  3. Rename the file to access.log.1.
  4. Create a new empty access.log.
  5. Write new records to the new active file.
  6. Run a normal update.

The question was whether the unread records from the old file could later be recovered without recounting the already processed prefix.

Rotation boundary: a checkpointed prefix and unread tail move into an archive while a new active log is updated

The normal update command deliberately reads only the configured active log. It does not search for access.log.1, access.log.2.gz, or other archives. That is sensible for a timer-driven job: archive discovery should be part of an explicit historical operation, not every routine update.

After rotation, however, the analyzer may see the new active file while the unread tail remains in the rotated file. backfill provides the explicit path both for the initial historical import and for recovering that tail.

Why the prefix matters

A filename is not a sufficient identity. After rotation, the same bytes have a different name. Conversely, a new file may reuse the old name while containing entirely different data.

The analyzer stores information about the processed prefix: a stable source identity, its size, the number of complete lines, and a digest of the processed bytes. When an archive is passed to backfill, its contents are compared with the stored prefix.

If the archive starts with content that has already been counted, the common prefix is skipped and only the unread suffix is applied. The correct operation is therefore not simply to import the rotated file, but to match the known prefix and import the remaining tail.

The regression fixture uses synthetic records. One run checkpoints 100 records, 50 more are appended but left unread, the file is rotated, and a new active file receives 30 records. The normal update accounts for the original 100 and the new 30. Backfill must then add the 50-record tail exactly once.

Normal update and backfill

Backfill discovers the active file and numbered rotations, including supported compressed formats, orders them by time, validates the complete set, and applies it in one transaction.

The recommended sequence is:

  1. Wait for an update of the active log before rotation.
  2. Rotate the log.
  3. Let the web server reopen the new active file.
  4. Wait for an update of the new active file.
  5. Run backfill after the complete archive set has been formed.

Backfill is not intended to run after every rotation. It is an explicit historical operation, run after the archive set is complete.

Three outcomes, not one

The most important result of the investigation was that “backfill completed” is not a sufficient operational statement. The tests and operator guide distinguish three different outcomes:

Backfill outcomes: safe rejection, unchanged replay, or recovery of an unread suffix

Safe rejection

Some archive sets cannot be matched reliably: two files overlap at the beginning, an archive is corrupt, a legacy checkpoint cannot establish a safe match for a compressed archive, or multiple checkpoints are ambiguous.

In these cases backfill fails before changing either aggregate data or checkpoints. A rejection does not prove that the unread tail was recovered. The operator must inspect the archives and checkpoints, resolve the ambiguity, and retry. A safe rejection is better than a silent double count or an unnoticed loss of history.

Unchanged replay

Backfill is idempotent for files that have already been processed. Running it again with the same archive set leaves the record count and totals unchanged because the stored checkpoint already covers the input.

Actual tail recovery

When the processed prefix can be matched, backfill skips it and records only the unread suffix. The resulting aggregates match a clean import of the same records.

These outcomes are deliberately different: rejection means no write and no proof of recovery; unchanged replay means the input was already covered; actual recovery means the unread suffix was imported. If a rebuild is required, the complete archive history must be available.

Compression and older databases

Compression changes both the filename and the byte representation. access.log.1 may later become access.log.1.gz. Before matching the prefix, backfill decompresses supported formats, so the comparison is made against log contents rather than compressed bytes or filenames. The regression suite covers gzip, bzip2, and xz.

There is a limitation for older databases. A legacy checkpoint created before stable prefix fields existed cannot prove that a compressed archive does not overlap already processed data. In that case backfill refuses to guess. The recovery procedure is to rebuild aggregates from the complete archive sequence, starting with the oldest file, and then process the remaining files in order.

What the operator guide establishes

The tests prove that the code behaves correctly in specific rotation states. The operator guide records the requirements:

  • normal updates read only the active file;
  • rotated archives require an explicit backfill;
  • after rotation, the web server must reopen the new active file;
  • backfill should run only after the complete archive set has formed;
  • overlap, corruption, and uncertain legacy matching must fail without writing data;
  • a rejection leaves aggregates and checkpoints unchanged, but does not confirm tail recovery;
  • archives must be retained if a replay or rebuild may need to restore historical data.

If the web server continues using the old file descriptor after the rename, it may keep writing to access.log.1 while the new access.log remains empty. The analyzer cannot determine this from filenames alone. That is why the logrotate configuration sends a deployment-specific reopen signal before the post-rotation update.

From an AWStats parity check to a regression contract

Backfill began as a way to populate the database from archived history and verify parity with AWStats. The unread-tail scenario was raised by Tim Urista in the comments under my earlier article, Building an AWStats-style log analyzer in Rust. I investigated the scenario and documented one tested solution; this article does not claim that the approach is novel or definitive.

Tim Urista, a Senior Cloud Engineer, proposed turning the rotation boundary into a fixture. The follow-up discussion sharpened the distinction between safe rejection, unchanged replay, and actual recovery. That feedback led to the prefix verification, compressed-archive coverage, regression tests, and clearer operator instructions.

The difficult part of log rotation is not detecting that a filename changed. The difficult part is preserving the boundary between bytes that have already been counted and bytes that have only just appeared.

The source is available in the Stats repository. The executable rotation fixture is rotation_boundary.rs, and the operator procedure is documented in docs/operations.md. Together, the test and the guide keep the operational contract executable and reviewable.

Tags: Rust, Log Analysis, AWStats, Nginx, SQLite, Reliability, Open Source

← Back to all posts