cachevolumedir

package
v1.0.0-beta.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package cachevolumedir keeps cache volumes as plain directories under one directory, apart from the engine's snapshots, so that the directory can be handed from one engine to the next.

One engine owns the directory at a time. Every volume is named by a digest of its identity and carries its own metadata, so an engine that receives the directory from another can use it with no database.

Layout, for format 1:

<dir>/owner.lock             flock held by the owning process
<dir>/v1/volumes/<id>/       one volume
    meta.json                identity and bookkeeping (temp file + rename)
    dirty                    present while the stored size may be stale
    fs/                      the volume's contents
<dir>/v1/tmp/                volumes being created
<dir>/v1/trash/              volumes being removed

The store reads and writes only its own format's subdirectory, and leaves anything else under <dir> alone.

Every change a crash could interrupt is a rename or a temp-file-and-rename, so the directory is consistent at any point a process stops.

Index

Constants

View Source
const FormatVersion = 1

FormatVersion is the on-disk format this package writes, as v<N>/ under the directory.

Variables

View Source
var ErrClosed = errors.New("cache volume directory is closed")

ErrClosed is returned by Acquire once the store is closed.

View Source
var ErrOwned = errors.New("cache volume directory is owned by another process")

ErrOwned is returned by Open when another process owns the directory.

View Source
var ErrPinned = errors.New("cache volumes are still in use")

ErrPinned is returned by Close when volumes are still pinned.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	ID         string
	Identity   Identity
	SizeBytes  int64
	CreatedAt  time.Time
	LastUsedAt time.Time
	Pinned     bool
	// contains filtered or unexported fields
}

Entry describes one volume in the directory.

type Identity

type Identity struct {
	Namespace string `json:"namespace,omitempty"`
	Key       string `json:"key"`
	Sharing   string `json:"sharing,omitempty"`
	Owner     string `json:"owner,omitempty"`
	// Source is the recipe digest of the volume's source directory, or empty.
	// The recipe includes which subdirectory of its parent the source is, so
	// the identity needs no separate selector.
	Source string `json:"source,omitempty"`
}

Identity is what names a cache volume. Two calls with equal identities use the same directory, in any engine.

func (Identity) ID

func (id Identity) ID() string

ID is the volume's directory name: the hex SHA-256 of the identity's canonical JSON, versioned.

type Options

type Options struct {
	// EngineVersion is recorded in the metadata of volumes this store creates.
	EngineVersion string
	// Now is the clock; time.Now when nil.
	Now func() time.Time
}

Options configures a Store.

type Pin

type Pin struct {
	// contains filtered or unexported fields
}

Pin keeps a volume from being collected until it is released.

func (*Pin) Created

func (p *Pin) Created() bool

Created reports whether the Acquire that took this pin created the volume.

func (*Pin) FirstPin

func (p *Pin) FirstPin() bool

FirstPin reports whether this is the store's first pin on the volume since it took the directory, or since it last removed the volume.

func (*Pin) Path

func (p *Pin) Path() string

Path is the volume's contents directory, to bind-mount.

func (*Pin) Release

func (p *Pin) Release()

Release ends the pin. Only the first call counts.

type Policy

type Policy struct {
	ReservedSpace int64
	MaxUsedSpace  int64
	MinFreeSpace  int64
	// CurrentFreeSpace is the filesystem's free bytes, for MinFreeSpace.
	CurrentFreeSpace int64
	// All removes every volume that isn't pinned, whatever the thresholds.
	All bool
}

Policy decides what Collect removes. Byte values are resolved; zero means unset. Thresholds work as in the engine's other cache policies.

type Report

type Report struct {
	Removed        []Entry
	ReclaimedBytes int64
}

Report is what one Collect removed.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is an owned cache volume directory.

func Open

func Open(dir string, opts Options) (*Store, error)

Open takes ownership of dir, creating it and its layout if needed, and removes what a previous owner left in tmp/ and trash/. It returns ErrOwned if another process owns dir.

func (*Store) Acquire

func (s *Store) Acquire(ctx context.Context, ident Identity, writable bool, populate func(ctx context.Context, fsDir string) error) (*Pin, error)

Acquire pins the volume, creating it first if it doesn't exist: populate fills the new volume's contents directory, which exists and is empty when it is called. Creation of one volume is serialized; a failed populate leaves no volume behind. A writable pin marks the volume's stored size stale until a measurement taken with no writer finds it again.

func (*Store) Close

func (s *Store) Close() error

Close gives up ownership. If volumes are still pinned, or acquisitions are still in progress, it keeps ownership and returns ErrPinned: another owner could otherwise collect a volume that is still in use, or sweep one being created. The ownership lock then lasts until the process exits. Once closed, Acquire fails with ErrClosed. Removals in progress keep running until they finish.

func (*Store) Closed

func (s *Store) Closed() bool

Closed reports whether the store has given up ownership. A nil store is closed.

func (*Store) Collect

func (s *Store) Collect(ctx context.Context, policy Policy) (Report, error)

Collect removes volumes until the policy is met, the least recently used first, never a pinned one. Bytes already being removed count as freed. Removed volumes are moved aside at once and deleted in the background. Collect stops between volumes when ctx ends, leaving the directory consistent.

func (*Store) Dir

func (s *Store) Dir() string

Dir is the directory the store owns.

func (*Store) PinnedPath

func (s *Store) PinnedPath(ident Identity) (string, error)

PinnedPath is the contents directory of a volume this process has pinned. Mounting it is safe while the pin is held; it is an error to ask for a volume that isn't pinned, since nothing then keeps it from being removed.

func (*Store) Usage

func (s *Store) Usage(ctx context.Context) ([]Entry, int64, error)

Usage lists the volumes, measuring those whose stored size is stale, and returns the bytes the whole directory uses, including what is still being created or removed.

func (*Store) WaitRemovals

func (s *Store) WaitRemovals(ctx context.Context) error

WaitRemovals waits until every removal Collect started has finished, or ctx ends. It reports the removals that failed since it last reported.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL