Repository navigation
atelet: one eviction loop for every node-local file cache, with one metric family #2889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zoe Zhao (zoez7)
merged 5 commits into
agent-substrate:main
from
dberkov:cachegc-driver
Oct 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30a6aee
atelet: one eviction loop for every node-local file cache, with one m…
dberkov 4d67287
filecachegc: register the flags on a flag set the caller passes
dberkov cbf872f
metrics registry: anchor the file cache instruments at the renamed file
dberkov 6ba25cb
filecachegc: validate the flags in Start, and export only what a cach…
dberkov 564a9d5
docs, e2e: follow the sandbox asset cache metrics into the file cache…
dberkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,317 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package filecachegc runs the eviction loop of a node-local file cache: a | ||
| // filecache store with a byte budget, evicted least recently used first, | ||
| // down to a low-water fraction of the budget, never taking an entry a | ||
| // consumer still links. One loop per cache, one set of flags per cache under | ||
| // the cache's flag prefix, and one set of instruments for all of them, | ||
| // labeled by cache name. | ||
| package filecachegc | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "runtime/debug" | ||
| "time" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| "go.opentelemetry.io/otel/metric" | ||
|
|
||
| "go.ate.dev/substrate/cmd/atelet/internal/filecache" | ||
| "go.ate.dev/substrate/internal/ateattr" | ||
| ) | ||
|
|
||
| // lowWater is the fraction of the budget a pass evicts down to once the | ||
| // cache is over it, so passes do not fire on every tick while usage hovers | ||
| // at the budget. | ||
| const lowWater = 0.9 | ||
|
|
||
| const ( | ||
| // shortfallWarnLimit is how many consecutive shortfalls warn before the | ||
| // loop backs off to shortfallReminderEvery. | ||
| shortfallWarnLimit = 3 | ||
| // shortfallReminderEvery keeps a persistent shortfall visible without a | ||
| // line per tick (at the 5m default: roughly hourly). | ||
| shortfallReminderEvery = 12 | ||
| ) | ||
|
|
||
| const ( | ||
| sizeMetric = "ate.filecache.size" | ||
| evictionsMetric = "ate.filecache.evictions" | ||
| gcPassesMetric = "ate.filecache.gc.passes" | ||
| ) | ||
|
|
||
| // Store is what the loop needs from a *filecache.Store, a seam so a pass's | ||
| // skip and recovery paths are testable without a real cache. | ||
| type Store interface { | ||
| TotalBytes(ctx context.Context) (int64, error) | ||
| EvictUnused(ctx context.Context, targetBytes int64, dryRun bool) (filecache.EvictStats, error) | ||
| } | ||
|
|
||
| // Flags are one cache's eviction flags: --<prefix>-gc-period, | ||
| // --<prefix>-max-bytes and --<prefix>-gc-dry-run. | ||
| type Flags struct { | ||
| Period *time.Duration | ||
| MaxBytes *int64 | ||
| DryRun *bool | ||
| prefix string | ||
| } | ||
|
|
||
| // NewFlags registers the eviction flags of the cache called what, for | ||
| // example "sandbox asset cache", under prefix, for example | ||
| // "sandbox-asset-cache", on fs. | ||
| func NewFlags(fs *pflag.FlagSet, prefix, what string) *Flags { | ||
| return &Flags{ | ||
| prefix: prefix, | ||
| Period: fs.Duration(prefix+"-gc-period", 5*time.Minute, fmt.Sprintf("How often to run the %s eviction pass. 0 disables it.", what)), | ||
| MaxBytes: fs.Int64(prefix+"-max-bytes", 0, fmt.Sprintf("Budget for the %s, in bytes of disk the cached files occupy. A pass that finds the cache over it evicts least recently used entries that nothing links, down to %d%% of it. 0 means no budget: the cache only grows.", what, int(lowWater*100))), | ||
| DryRun: fs.Bool(prefix+"-gc-dry-run", false, fmt.Sprintf("Compute and log eviction decisions for the %s without deleting anything.", what)), | ||
| } | ||
| } | ||
|
|
||
| // Validate rejects values the loop could not act on. | ||
| func (f *Flags) Validate() error { | ||
| if *f.Period < 0 { | ||
| return fmt.Errorf("--%s-gc-period %v must be >= 0", f.prefix, *f.Period) | ||
| } | ||
| if *f.MaxBytes < 0 { | ||
| return fmt.Errorf("--%s-max-bytes %d must be >= 0", f.prefix, *f.MaxBytes) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // target computes the bytes a pass should free: nothing while the cache is | ||
| // within its budget, otherwise enough to bring it down to the low-water | ||
| // fraction of the budget. A zero budget never asks for anything. | ||
| func target(total, maxBytes int64) int64 { | ||
| if maxBytes <= 0 || total <= maxBytes { | ||
| return 0 | ||
| } | ||
| target := total - int64(float64(maxBytes)*lowWater) | ||
| if target > total { | ||
| target = total | ||
| } | ||
| return target | ||
| } | ||
|
|
||
| // Instruments are the metrics of every file cache on the node, one set | ||
| // labeled by cache name: how much disk each holds, how many entries its | ||
| // passes evict, and how each pass ended. Nil-safe: a nil receiver records | ||
| // nothing. | ||
| type Instruments struct { | ||
| meter metric.Meter | ||
| size metric.Int64ObservableUpDownCounter | ||
| evictions metric.Int64Counter | ||
| passes metric.Int64Counter | ||
| } | ||
|
|
||
| // NewInstruments creates the instruments on meter, once per process. | ||
| func NewInstruments(meter metric.Meter) (*Instruments, error) { | ||
| size, err := meter.Int64ObservableUpDownCounter( | ||
| sizeMetric, | ||
| metric.WithUnit("By"), | ||
| metric.WithDescription("Bytes of disk a node-local file cache occupies, as the sum of its entries' allocated sizes, by cache."), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("create %s updowncounter: %w", sizeMetric, err) | ||
| } | ||
| evictions, err := meter.Int64Counter( | ||
| evictionsMetric, | ||
| metric.WithUnit("{entry}"), | ||
| metric.WithDescription("Number of entries eviction passes removed from a node-local file cache, by cache."), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("create %s counter: %w", evictionsMetric, err) | ||
| } | ||
| passes, err := meter.Int64Counter( | ||
| gcPassesMetric, | ||
| metric.WithUnit("{pass}"), | ||
| metric.WithDescription("Number of eviction passes over a node-local file cache that had work to do, by cache and outcome."), | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("create %s counter: %w", gcPassesMetric, err) | ||
| } | ||
| return &Instruments{meter: meter, size: size, evictions: evictions, passes: passes}, nil | ||
| } | ||
|
|
||
| // observe has the size of store reported under name on every collection. | ||
| // The callback walks the store's entries; that is a few hundred stats on a | ||
| // full node. Called whether or not the cache has a budget, so the size has | ||
| // a series on every install. | ||
| func (i *Instruments) observe(name string, store Store) error { | ||
| if i == nil { | ||
| return nil | ||
| } | ||
| attrs := metric.WithAttributes(ateattr.FileCacheNameKey.String(name)) | ||
| _, err := i.meter.RegisterCallback(func(ctx context.Context, o metric.Observer) error { | ||
| total, err := store.TotalBytes(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| o.ObserveInt64(i.size, total, attrs) | ||
| return nil | ||
| }, i.size) | ||
| if err != nil { | ||
| return fmt.Errorf("register %s callback for %s: %w", sizeMetric, name, err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // recordPass records one pass over name that had work to do, with what it | ||
| // evicted. | ||
| func (i *Instruments) recordPass(ctx context.Context, name, outcome string, evicted int) { | ||
| if i == nil { | ||
| return | ||
| } | ||
| i.passes.Add(ctx, 1, metric.WithAttributes(ateattr.FileCacheNameKey.String(name), ateattr.FileCacheGCOutcomeKey.String(outcome))) | ||
| if evicted > 0 { | ||
| i.evictions.Add(ctx, int64(evicted), metric.WithAttributes(ateattr.FileCacheNameKey.String(name))) | ||
| } | ||
| } | ||
|
|
||
| // gc is one cache's eviction loop: configuration snapshotted from the flags | ||
| // at construction, and the shortfall backoff. | ||
| type gc struct { | ||
| name string | ||
| store Store | ||
| instruments *Instruments | ||
| period time.Duration | ||
| maxBytes int64 | ||
| dryRun bool | ||
|
|
||
| consecutiveShortfalls int | ||
| } | ||
|
|
||
| // newGC returns the loop for the cache called name, a value of | ||
| // ateattr.FileCacheNameKey. | ||
| func newGC(name string, store Store, flags *Flags, instruments *Instruments) *gc { | ||
| return &gc{ | ||
| name: name, | ||
| store: store, | ||
| instruments: instruments, | ||
| period: *flags.Period, | ||
| maxBytes: *flags.MaxBytes, | ||
| dryRun: *flags.DryRun, | ||
| } | ||
| } | ||
|
|
||
| // Start validates the cache's flags, reports its size through instruments, | ||
| // and runs its eviction loop in the background when the flags give it a | ||
| // budget and a period. Without a budget it warns once: the cache then only | ||
| // grows. | ||
| func Start(ctx context.Context, name string, store Store, flags *Flags, instruments *Instruments) error { | ||
| if err := flags.Validate(); err != nil { | ||
| return err | ||
| } | ||
| if err := instruments.observe(name, store); err != nil { | ||
| return err | ||
| } | ||
| switch { | ||
| case *flags.MaxBytes == 0: | ||
| slog.WarnContext(ctx, "File cache has no budget; it only grows", slog.String("cache", name), slog.String("flag", "--"+flags.prefix+"-max-bytes")) | ||
| case *flags.Period > 0: | ||
| go newGC(name, store, flags, instruments).run(ctx) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // run executes eviction passes on the configured period until ctx is done, | ||
| // the first one immediately so a node booting over budget does not wait a | ||
| // full period. Passes are strictly serialized. | ||
| func (g *gc) run(ctx context.Context) { | ||
| g.runPass(ctx) | ||
| ticker := time.NewTicker(g.period) | ||
| defer ticker.Stop() | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-ticker.C: | ||
| } | ||
| g.runPass(ctx) | ||
| } | ||
| } | ||
|
|
||
| // runPass performs one pass. It recovers from panics: this is a background | ||
| // janitor, and a bug here must not take atelet down with it and strand every | ||
| // actor on the node. | ||
| func (g *gc) runPass(ctx context.Context) { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| slog.ErrorContext(ctx, "File cache GC pass panicked; skipping this pass", | ||
| slog.String("cache", g.name), slog.Any("panic", r), slog.String("stack", string(debug.Stack()))) | ||
| } | ||
| }() | ||
|
|
||
| total, err := g.store.TotalBytes(ctx) | ||
| if err != nil { | ||
| // Fail toward retention: retry next tick. | ||
| slog.WarnContext(ctx, "File cache GC: sizing the cache failed; skipping this pass", slog.String("cache", g.name), slog.Any("err", err)) | ||
| g.instruments.recordPass(ctx, g.name, ateattr.FileCacheGCOutcomeSkipped, 0) | ||
| return | ||
| } | ||
| target := target(total, g.maxBytes) | ||
| if target == 0 { | ||
| // Within budget: nothing to do, nothing to say. | ||
| g.consecutiveShortfalls = 0 | ||
| return | ||
| } | ||
|
|
||
| tStart := time.Now() | ||
| stats, err := g.store.EvictUnused(ctx, target, g.dryRun) | ||
| attrs := []any{ | ||
| slog.String("cache", g.name), | ||
| slog.Int64("cache_bytes", total), | ||
| slog.Int64("max_bytes", g.maxBytes), | ||
| slog.Int64("target_bytes", target), | ||
| slog.Int64("freed_bytes", stats.FreedBytes), | ||
| slog.Int("evicted", stats.Retired), | ||
| slog.Int("skipped_linked", stats.SkippedLinked), | ||
| slog.Int("skipped_young", stats.SkippedYoung), | ||
| slog.Int("skipped_busy", stats.SkippedBusy), | ||
| slog.Bool("dry_run", g.dryRun), | ||
| slog.Duration("took", time.Since(tStart)), | ||
| } | ||
| // A dry run retires nothing: its Retired is what a real pass would have | ||
| // taken, which is logged above and must not count as an eviction. | ||
| evicted := stats.Retired | ||
| if g.dryRun { | ||
| evicted = 0 | ||
| } | ||
| if err != nil { | ||
| // Per-entry failures; each retries next pass. | ||
| slog.WarnContext(ctx, "File cache GC pass finished with errors", append(attrs, slog.Any("err", err))...) | ||
| } | ||
| if stats.FreedBytes < target { | ||
| // Everything left is linked, young, or busy: the remaining usage is | ||
| // not the cache's to free. Warn on the first few, then a periodic | ||
| // reminder, never a line per tick. | ||
| g.consecutiveShortfalls++ | ||
| switch { | ||
| case g.consecutiveShortfalls <= shortfallWarnLimit: | ||
| slog.WarnContext(ctx, "File cache GC could not reach its budget", | ||
| append(attrs, slog.Int("consecutive", g.consecutiveShortfalls))...) | ||
| case g.consecutiveShortfalls%shortfallReminderEvery == 0: | ||
| slog.WarnContext(ctx, "File cache GC still over its budget; what remains is linked by a consumer, younger than the min age, or busy", | ||
| append(attrs, slog.Int("consecutive", g.consecutiveShortfalls))...) | ||
| } | ||
| g.instruments.recordPass(ctx, g.name, ateattr.FileCacheGCOutcomeShortfall, evicted) | ||
| return | ||
| } | ||
| g.consecutiveShortfalls = 0 | ||
| slog.InfoContext(ctx, "File cache GC pass complete", attrs...) | ||
| g.instruments.recordPass(ctx, g.name, ateattr.FileCacheGCOutcomeComplete, evicted) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.