Record audit file sizes before cleanup deletes

Merged
YO
4d ago
+150-48

Summary

The audit cleaner deletes expired files and only then writes its audit_cleanup entry, with dates and a count. A later release ships audit entries to Dagu Console and tracks a byte offset per file. An offset alone cannot tell whether a deleted file held entries it never sent: dagu secret can write a file while the server is stopped, and the cleaner runs at server start, before any shipper. This is task D2 of RFC 021 (tracker dagucloud/cloud#51): the cleaner durably records each file and its size before it deletes anything.

Changes

  • The cleaner writes its audit_cleanup entry before it removes any file, and the entry lists each file with its size in bytes, oldest first.
  • The entry is synced to disk, file and directory, before the first removal. Other audit entries are written as before, without a sync.
  • If the entry cannot be written or synced, the cleaner removes nothing and tries again on its next daily run.
  • A file whose size cannot be read is left for the next run.
  • purged_from and purged_to become expired_from and expired_to, and files_removed is dropped. The entry precedes the removal, so it names the expired files instead of claiming they were purged. files gives the count. A file whose removal fails stays on disk and is listed again by the next run.

Entry details, before and after:

{"purged_from":"2026-10-23","purged_to":"2026-10-24","files_removed":2,"retention_days":7}
{"expired_from":"2026-10-23","expired_to":"2026-10-24","files":[{"name":"2026-10-23.jsonl","bytes":48213},{"name":"2026-10-24.jsonl","bytes":51022}],"retention_days":7}

Core change

purgeExpiredFiles lists the expired files with their sizes and appends the entry through the store's appendDurable, which masks it like Append and then syncs the file and its directory. It removes the files only after that succeeds (internal/persis/file/audit/cleaner.go:98):

files := c.expiredFiles()
if len(files) == 0 {
	return
}
if err := c.recordCleanup(files); err != nil {
	slog.Warn("fileaudit: kept expired audit files, the cleanup entry failed", ...)
	return
}
for _, file := range files {
	filePath := filepath.Join(c.baseDir, file.Name)
	if err := fileutil.Remove(filePath); err != nil {

The sync (internal/persis/file/audit/store.go:144):

if !durable {
	return nil
}

if err := f.Sync(); err != nil {
	return fmt.Errorf("fileaudit: failed to sync file %s: %w", filePath, err)
}
// The write may have created the file, so its directory entry is synced too.
if err := fileutil.SyncDir(s.baseDir); err != nil {
	return fmt.Errorf("fileaudit: failed to sync directory %s: %w", s.baseDir, err)
}

An entry goes to the file of its own timestamp, today's, so an expired file does not grow between the size read and the removal.

Testing

  • TestPurgeExpiredFiles_RecordsSizesFirst: both expired files are still on disk when the entry is written, and the entry lists their names and sizes. It failed before the fix: the files were gone, and the entry had only files_removed.
  • TestPurgeExpiredFiles_KeepsFilesWhenRecordFails: a failing entry leaves the expired file on disk. It failed before the fix.
  • TestStore_CleanupEntryIsMasked now runs the cleaner through appendDurable, the path it uses in production, and the entry is still masked.
  • The sync has no unit test: a write lost to a power cut cannot be produced in one.
  • The existing cleaner tests pass unchanged apart from the renamed and dropped fields.
  • Ran go test -race ./internal/persis/file/audit/... and golangci-lint for Linux and Windows on the package.

Performance

No performance risk: the cleaner already lists the directory once a day. It now reads each expired file's size from that listing (DirEntry.Info), one lstat per expired file, usually one a day, and on a day it removes files it adds one fsync of today's audit file and one of the audit directory. Other Append calls do not sync. Each entry grows by about 45 bytes per file. Cutting retention from 3,650 to 7 days lists about 3,600 files in one entry, about 160 KB, well under the reader's 10 MiB line limit.

Compatibility

  • An older binary reads the new entry; details is free-form JSON.
  • New entries carry expired_from and expired_to instead of purged_from and purged_to, and no files_removed. Nothing in the repository reads these fields.

Out of scope

  • Turning these entries into deleted gaps, and ignoring an entry whose file a failed removal left on disk: D3.

Summary by cubic

Changes the audit cleaner to record expired audit files and their sizes before deleting them, so the shipper that tracks byte offsets can tell whether a deleted file held entries it never read. The audit_cleanup entry is now written first; if it fails, no files are removed and the cleaner retries on its next daily run.

  • Drops files_removed from the entry; files now gives the count.
  • Files whose size can't be read are left for the next run.

Summary by CodeRabbit

  • Bug Fixes
    • Audit cleanup records the names and sizes of expired files before removing them.
    • Expired files are preserved if the cleanup record cannot be written. Failures to remove individual files no longer prevent cleanup from continuing with others.
    • Files whose details cannot be read are excluded from both the cleanup record and removal.