Skip to content

feat(recordlog): move the record log port to its consumers - #166

Open
karauda wants to merge 11 commits into
mainfrom
feat/s29-recordlog-port
Open

karauda wants to merge 11 commits into
mainfrom
feat/s29-recordlog-port

Conversation

@karauda

@karauda karauda commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

What changed, and why

The record log port moves to its consumers in new package internal/recordlog. Log appends, seals, prunes, reads back written lines and counts Stats. Reader reads the manifest, opens segments without a lock and reports whether a writer holds the log as held, free or unknown (never read as free). A test uses go list -deps to verify no file, OS or kind package imports on linux, darwin and windows.

internal/segmentlog is its sole kind, segments. internal/trailfile writes and reads only through the port, reading no segment file by name. Manifest 0.1 bytes, the export, cursors, flags and exit codes are unchanged. scripts/ports.txt gains the record-log row; conformance cases 10 to 13 in testdata/ports/record-log each include a negative control.

A writer of a directory of segments now holds an exclusive lock on the log directory itself, taken through the directory it judged before segments.lock and held until it closes, so a second writer (prune beside a running collect) is refused with segmentlog: the directory is held by another writer even with segments.lock unlinked or replaced and no live segment. Before, such a prune could open beside the collector, and the collector's next seal left a log that refused to open. segments.lock stays for readers and writers of earlier builds; stop every collect and prune of an earlier version before running this one on the same directory. A file system on which a directory cannot be locked refuses the writer.

Whether a writer holds the log is asked of the directory's own lock, segments.lock and the live segment's lock, in that order. Any held lock answers held; the answer is free only if every lock asked is free, a missing live segment counting as free, and unknown otherwise, including when the path no longer names the inspected directory. trail segments exits 1 on unknown; trail and trail export refuse the same way when the live segment ends in a partial line. Options.OpenLive must lock the live file it returns, as trailfile's does; the kind opens the file again and asks its lock, and refuses an unlocked one with segmentlog: the live file was opened and not locked (an open is refused; at an append, the log fails).

collect reads resent records back through its open directory, judging each segment file as on write (regular file, not a link, owner, mode). A segment file another account could write fails the log on resend; collect refuses that record and later appends until reopened.

trail segments prints a third line, kind=segments max_line_bytes=786432 segment_bytes=unknown max_bytes=unknown budget_refusals=unknown. Byte bounds and budget refusals belong to the collector and another process cannot see them, so they print unknown, never 0. Shared log refusals start recordlog:; kind-only ones keep segmentlog:.

Invariant or record

ADR-0091 is accepted, resolving ADR-0072's "interface beside the kind". Invariants 4 and 5: unknown holding refuses, never reads as free.

How it was tested

  • macOS binaries passed: internal/recordlog, internal/segmentlog, internal/trailfile, internal/docscheck and cmd/guardana-gateway. New tests: TestASecondWriterIsRefusedWithTheLockFileUnlinkedAndNoLiveFile, TestAPruneBesideAnOpenWriterIsRefused, TestAReplacedLockFileWithNoLiveFileReadsHeld, TestAReplacedLockAndLiveFileReadHeld, TestAnOpenerThatDoesNotLockTheLiveFileIsRefused, TestACopyOfTheLogAtItsPathIsUnknown, TestAWriterHoldsTheLogThroughEitherLock, TestAHeldDirectoryLockIsHeldWhateverTheLiveFile, TestAHoldingThatCannotBeAskedIsUnknown, TestAReadBackOfASegmentOthersMayWriteFailsTheLog and trailfile's TestAnUnlinkedDirectoryLockLeavesALiveWritersTailHeld.
  • Mutants killed by tests: zero kind accepted, unknown holding read as free (port, kind, trailfile), read-back digest compare off (kind, trailfile), read-back opening without judging files (kind, trailfile), os imported in internal/recordlog, directory identity check dropped, the writer's directory lock not taken, holding not asking it, the live-file lock check off. Each new one-writer test was red on the commit before the directory lock.
  • Linux arm64 container: internal/segmentlog and internal/trailfile passed, including the directory lock's tests; flock on a directory descriptor was checked on darwin and linux before building on it. The other failures there lack the go binary or go.mod. The last change (an unaskable live lock reported as it is, and the directory-copy test) was not run on Linux.
  • Not verified: a network file system (NFS, SMB), and platforms without flock beyond go vet.
  • make vet, go vet -tags twoaccounts, golangci-lint darwin and GOOS=linux, gofmt, make check-sizes, scripts/check-brand.sh and make docs-check passed.
  • Local make quality was not verified; CI's make quality on this pull request is the gate.

Documentation

Updated ADR-0091 (accepted), docs/contracts.md (port, the three locks), docs/status.md (trail-collector details), docs/guides/watch-a-plane-without-a-collector.md, docs/reference/cli.md (third line; covers), docs/concepts/glossary.md, docs/backlog.md (B14 item removed), CHANGELOG Security, Changed and Fixed entries; ADR-0091 states the directory lock, the OpenLive check and the upgrade order. Gateway pages listed by make docs-impact were read; none states locks, the third line or refusal text.

The record log port now lives in internal/recordlog, as ADR-0091
decides: the Log port, its Reader and Stats, the Holding state whose
zero value is unknown, the segment and manifest types, and the port's
refusals. The package imports no file-system package. internal/segmentlog
is the segments kind behind it: Open and Inspect take a recordlog.Config,
refuse the zero kind, and read lines back through the directory handle
the writer holds, failing the log on any mismatch. internal/trailfile
names the kind in one place and reads export, cursors and reindex through
the Reader; an unknown holding state is an error, never free. trail
segments prints from Stats and says unknown where another process cannot
see the writer's bounds. Conformance cases 10 to 13 join the port's suite,
each with its negative control.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
…sees

A segment others may write now fails a read-back in segmentlog and
refuses a repeat in trailfile; both tests kill the mutant that swaps the
checked open for a plain one. The import check drops a case that could
only ever be an import cycle. Reader.Stats says which numbers are the
writer's and unknown to a reader, never a measured zero.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
A reader asked only the directory's lock file whether a writer held the
log. With `segments.lock` unlinked under a running collector, `trail` and
`trail export` read its partial line as unheld and reported a
`partial_tail` gap, where they had asked the live file's own lock before.
Holding now asks both: held when either is held, free only when each is
free or absent, unknown when either cannot be asked. It also answers
unknown when the directory at the log's path is no longer the one the
view inspected, and HeldByWriter is ErrNoLock where the platform has no
file lock, so a lock it cannot ask is never read as free.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
…ocks

ADR-0091 is accepted. The changelog names the read-back's file checks,
the line `trail segments` adds, the lock a holding is asked of and the
`recordlog:` refusals. The watch guide, the glossary, the status rows,
the export contract and B14's acceptance say what the port is, what a
reader sees and what stays open; two pages cover internal/recordlog.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
Holding asks segments.lock first and the live segment's lock beside it:
held when either is held, unknown when the directory's lock cannot be
asked or the directory at the log's path is no longer the one inspected.
OpenLive's contract now says the live file is locked, so a second writer
is refused even with the directory's lock file unlinked.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
Signed-off-by: Konrad Karauda <konrad@karauda.com>
Signed-off-by: Konrad Karauda <konrad@karauda.com>

# Conflicts:
#	CHANGELOG.md
#	site/docs/changelog.html
A writer takes an exclusive lock on the log directory itself, through the
directory it judged, before segments.lock, and holds it until it closes.
No unlink or replace of a name in the directory drops it, so a second
writer, a prune beside a running collector included, is refused whether
or not a live segment exists.

Holding asks that lock first, through the directory it inspected, then
segments.lock and the live segment's lock: held when any is held, free
only when every lock asked is free.

Once OpenLive returns, the log opens the live file again and refuses it
unless a writer holds its lock (ErrLiveUnlocked): an open is refused, an
append fails the log. The package's test opener locks, so the conformance
suite no longer passes an opener that does not.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
…directory check

A live file whose lock the check cannot ask is refused with that error,
not as unlocked, so an append does not fail the log for good on a passing
open failure. A test pins that a copy of the log put at its path reads as
unknown to a reader that inspected the original.

Signed-off-by: Konrad Karauda <konrad@karauda.com>
…e upgrade order

Signed-off-by: Konrad Karauda <konrad@karauda.com>
@karauda
karauda requested a review from a team November 28, 2026 21:12
@karauda
karauda requested a review from a team as a code owner November 28, 2026 21:12
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Failed ❌

View logs ↗
4801da8 2026-11-28T21:16:37.188Z View logs ↗
  • Build: Failed ❌

View logs ↗
21d1c07 2026-11-28T21:12:57.326Z View logs ↗

Signed-off-by: Konrad Karauda <konrad@karauda.com>

# Conflicts:
#	CHANGELOG.md
#	site/docs/changelog.html

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant