Documentation
¶
Overview ¶
Package filecache is a node-local disk cache of downloaded artifacts, keyed by opaque identities (see Key). An artifact wanted by N concurrent callers is fetched once, publication into the cache is atomic and crash-safe, and entries are evicted under byte-budget pressure without breaking consumers.
On-disk layout, under a store's root (which the store owns exclusively):
entries/<sha256(key)>/
data # the cached file (or directory tree)
meta.json # canonical key + creation time; debugging only
tmp/ # in-flight fetches; same filesystem as entries/, so
# publication is one atomic rename
.rm-* # retired entries awaiting removal
An entry directory's mtime is its last-use clock. Nothing on a correctness path reads meta.json: entries are matched to keys by hashing the keys. Everything under entries/ is a complete, published entry; SweepDebris clears crash leftovers from the other two locations at startup.
Contracts ¶
Eviction never takes what a caller holds:
- GetFileTo serves a hit as a hard link, created in the same locked step that resolves the hit. While any consumer's link exists the entry is not evicted: the link is how a consumer declares the entry in use, and removing the cache's own link would free nothing.
- GetDirTo serves a hit as a directory the consumer owns, in which every file is a hard link, built in that same locked step. The entry counts as linked while any of its files is.
- GetFileCopyTo serves a private copy. The consumer owns the inode and may mutate it, and a copy in progress reads a held-open handle, so a concurrent eviction cannot corrupt it.
- The store's min age (WithMinAge) vetoes eviction of entries younger than the window between publication and a consumer's use becoming visible. Size it for the slowest consumer.
Files are published read-only (0444, keeping any execute bits so a cached binary stays runnable): a consumer writing through its hard link fails with EACCES instead of corrupting the shared bytes. Consumers that must mutate a file use GetFileCopyTo.
Keys are identities, not addresses. The store never invalidates: a key must name content that is immutable at its source (see URIKey); changed content must arrive under a new key.
Index ¶
- type DirFetcher
- type EvictStats
- type FileFetcher
- type Key
- type Option
- type Store
- func (s *Store) EvictUnused(ctx context.Context, targetBytes int64, dryRun bool) (EvictStats, error)
- func (s *Store) GetDirTo(ctx context.Context, key Key, dst string, fetch DirFetcher) error
- func (s *Store) GetFileCopyTo(ctx context.Context, key Key, dst string, fetch FileFetcher) error
- func (s *Store) GetFileTo(ctx context.Context, key Key, dst string, fetch FileFetcher) error
- func (s *Store) SweepDebris(ctx context.Context) (SweepStats, error)
- func (s *Store) TotalBytes(ctx context.Context) (int64, error)
- type SweepStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DirFetcher ¶
DirFetcher downloads a directory-tree artifact (an extracted release archive, say) into dstDir, which the store has created empty. The tree may hold only directories and regular files. As with FileFetcher, the store runs it at most once per key across concurrent callers, detached from their contexts and bounded by the fetch timeout, and its error reaches every waiting caller wrapped with %w.
type EvictStats ¶
type EvictStats struct {
// Retired counts entries renamed out of the cache's namespace: from
// that rename on, lookups miss and refetch, whether or not the later
// physical removal succeeded (FreedBytes tracks that part). In a dry
// run, the entries a real pass would have retired.
Retired int
// FreedBytes counts allocated (on-disk) bytes actually returned to the
// filesystem: the footprints of retired entries whose physical removal
// completed. An entry retired but not removed (a RemoveAll failure,
// reported in the returned error) is excluded; its bytes sit in a .rm-*
// dir until SweepDebris. In a dry run this is the would-free estimate.
FreedBytes int64
// SkippedLinked counts entries some consumer still hard-links. They are
// never evicted: retiring one frees nothing while the link lasts, and
// would only make the next caller download a second copy beside it.
SkippedLinked int
// SkippedYoung counts entries vetoed by the store's min age.
SkippedYoung int
// SkippedBusy counts entries vetoed at retire time: a hit or a fresh
// fetch moved their last-use clock, or a consumer linked them, after
// the pass listed them.
SkippedBusy int
}
EvictStats reports what an EvictUnused pass did (or, dry-run, would do).
type FileFetcher ¶
FileFetcher downloads a single-file artifact to dstPath, creating the file itself so it can seek and truncate for sparse output. The store runs it at most once per key across concurrent callers, detached from their contexts and bounded by the fetch timeout. Its error reaches every waiting caller wrapped with %w, so errors.Is classification sees through the store.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key identifies a cache entry. It is an identity, not an address: it says nothing about where the bytes come from (the fetcher does), only what they are, so two sources serving the same identity share one entry.
Keys are built via constructors only. The zero Key is invalid.
func SHA256Key ¶
SHA256Key returns the key for a content-addressed artifact, identified by the lowercase hex sha256 of its bytes. Uppercase digits are normalized so equal digests always yield equal keys.
func URIKey ¶
URIKey returns the key for an artifact identified by an immutable source, e.g. URIKey(goldenSnapshotURI, fileName). The parts must identify content that never changes underneath them; the cache has no invalidation, so a republished URI would serve stale bytes forever. Callers pass at least one non-empty part.
type Option ¶
type Option func(*Store)
Option configures a Store.
func WithFetchTimeout ¶
WithFetchTimeout sets the per-fetch bound.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is one on-disk cache. It is safe for concurrent use and assumes it is the only writer under its root (one atelet per node).
func (*Store) EvictUnused ¶
func (s *Store) EvictUnused(ctx context.Context, targetBytes int64, dryRun bool) (EvictStats, error)
EvictUnused frees cache space until targetBytes are reclaimed or no eligible entries remain, least-recently-used first. Only entries no consumer links are eligible: a hard link is how a consumer declares an entry in use, and evicting one would free nothing while the link lasts. Entries younger than the store's min age are skipped too, and a hit or fetch racing the pass vetoes its entry at retire time. Eviction therefore never takes anything a caller holds; it only ever costs the next caller a re-download.
With dryRun, nothing is touched and the stats report what a real pass would have chosen. Passes are pressure-driven: the caller decides when and how much; a non-positive target is a no-op.
func (*Store) GetDirTo ¶
GetDirTo materializes the tree identified by key at dst, fetching it with fetch if it is not cached. dst must be an absolute path that does not exist yet and lives on the cache's filesystem. On success it is a directory the caller owns, mirroring the cached tree, in which every regular file is a hard link to the read-only cache copy. The caller keeps a valid tree regardless of later eviction, and may add files of its own without touching the cache; writing through a linked file fails loudly.
dst is built beside its final name and renamed into place, so it never exists half-linked: an existing dst, reported as fs.ErrExist, is a complete tree. Concurrent calls for one key share a single fetch, as in GetFileTo.
func (*Store) GetFileCopyTo ¶
GetFileCopyTo materializes the artifact identified by key at dst as a private, hole-preserving copy, fetching it with fetch if it is not cached. Unlike GetFileTo's hard link, the caller owns the resulting inode (mode 0600) and may mutate it in place, and dst may be on any filesystem. dst must be an absolute path that does not exist yet.
Concurrent calls for one key share a single fetch, as in GetFileTo. A copy in progress reads a held-open handle, so a concurrent eviction cannot corrupt it.
func (*Store) GetFileTo ¶
GetFileTo materializes the artifact identified by key at dst, fetching it with fetch if it is not cached. dst must be an absolute path that does not exist yet and lives on the cache's filesystem (the same mount, not just the same disk): on success it is a hard link to the read-only cache copy, so the caller keeps a valid file regardless of later eviction, and the entry is published read-only (mode 0444, keeping any execute bits the fetcher set) so an in-place write fails loudly rather than corrupting the shared bytes.
Concurrent calls for one key share a single fetch. A caller whose ctx is canceled returns early with ctx.Err() while the fetch keeps running for the others; there is no negative caching, so after a failed fetch the next call starts fresh.
func (*Store) SweepDebris ¶
func (s *Store) SweepDebris(ctx context.Context) (SweepStats, error)
SweepDebris removes crash debris: everything under tmp/ (fetches a crash cut short) and every .rm-* dir at the store root (evictions that renamed but never removed). It must run once at startup, before the store serves requests, and not again: a later sweep would delete the working directories of in-flight fetches. No periodic schedule is needed either — debris only appears at crash or eviction time, and eviction passes retry leftover .rm-* removals themselves.
Removal failures are joined and reported after the sweep visits everything, so one bad path does not shadow the rest; published entries are never touched.
func (*Store) TotalBytes ¶
TotalBytes sums the allocated (on-disk) bytes of all published entries' regular files (see sparsefile.AllocatedBytes), not their logical lengths: the cache's biggest artifacts are sparse guest memory images, and every byte figure the store reports is compared against real disk usage (statfs watermarks, byte budgets). It is the GC driver's usage measure against the store's byte budget.
type SweepStats ¶
type SweepStats struct {
// TmpRemoved counts removed tmp/ children (unfinished fetches).
TmpRemoved int
// RetiredRemoved counts removed .rm-* dirs (interrupted evictions).
RetiredRemoved int
}
SweepStats reports what a SweepDebris pass removed.