gc

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gc reclaims storage (decision 0010). A pass purges trash entries past their retention, reaps abandoned upload sessions, deletes blobs no item or trash entry references, and deletes content in the blob store that the index never recorded (a deploy whose transaction failed after its content was written).

Role

artifacts module, core layer: the garbage collector. Passes run on a schedule and on POST /api/system/storage/gc, under a lease so two instances sharing a database never collect at once. A pass also removes the records of expired access tokens, through access/api's Tokens, the only way it reaches the access module (decision 0044, dependency rule 6). As artifacts core, it never imports the module's adapters or an ecosystem http package (dependency rule 8).

Inputs

  • Driving: the composition root, which builds the Collector (New) with the reapers of the multipart and registry upload sessions and the registry's expiry of finished uploads, and runs a pass every interval (Loop); and the storage API, whose POST /api/system/storage/gc runs one pass (Run).
  • Driven: the metadata store, through Store (the lease table, upload sessions, and the trash can's store, trash.Store, which holds the blob index), the blob store (platform/blob.Store) whose content it deletes, the trash service (artifacts/core/trash), which purges expired entries, the reapers and expirers Options names, and accessapi.Tokens.

Outputs

  • Report: what one pass did, or that it was skipped because another instance held the lease.
  • DefaultInterval (Artifactory's schedule, every four hours), DefaultGrace and DefaultUploadExpiry: what a zero interval or a zero Options field means.

Invariants

  • Every blob is removed in three steps: its index row is marked deleting, its content is deleted, and the row becomes a tombstone. A deleting row refuses references, and a deploy that revives a tombstone checks the content is still there, so a deploy of the same content racing the removal is refused and repeated rather than left referencing missing content.
  • A pass holds the lease named "storage-gc" and renews it between steps, so a crashed pass frees it within the lease's TTL. Every instance on one database takes that name, so it never changes.
  • Content is deleted only once it has been unreferenced, or unindexed, for the grace period: content is written before its reference, so younger content may be a deploy about to commit.

References

  • Decision 0010: docs/research/decisions/0010-retention-trash-and-gc.md.
  • Decision 0004: docs/research/decisions/0004-storage-sql-metadata-and-blobs.md (the blob index and content-addressed content).
  • Decision 0005: docs/research/decisions/0005-authentication-and-tokens.md (how long a token record outlives its token).
  • Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md, "Dependency rules".
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, line 204 (core/gc), line 1027 (the mapping row) and rule 1 at lines 30 to 34.
  • Plan: docs/implementation/plan.md#p3-repositories-generic-artifacts-cli-aql-shape (trash can and garbage collection, #48) and #p9d-modular-monolith-migration, epic ART (https://github.com/weaveplatform/artifactweave/issues/941).
  • https://github.com/weaveplatform/artifactweave/issues/1012 and https://github.com/weaveplatform/artifactweave/issues/1088

Status

Implemented P3 to P9c. Moved here in P9d.

Testing

  • gc_test.go and gc_more_test.go: passes over a store and a blob store, the grace period, reapers and expirers, orphaned content, the lease held elsewhere, failing steps and the loop.
  • race_test.go: no content is lost to a deploy racing a removal, and a deploy finishes a crashed pass's removal.
  • tokens_test.go: expired token records removed through accessapi.Tokens.

Index

Constants

View Source
const (
	// DefaultInterval is Artifactory's default garbage collection schedule,
	// every four hours.
	DefaultInterval = 4 * time.Hour
	// DefaultGrace is how long a blob must have been unreferenced, or its
	// content unindexed, before it is deleted. It must outlast any upload in
	// flight: content is written before its reference, so younger
	// unreferenced content may be a deploy about to commit.
	DefaultGrace = time.Hour
	// DefaultUploadExpiry is how long an upload session may sit untouched
	// before it is abandoned and reaped.
	DefaultUploadExpiry = 24 * time.Hour
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

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

Collector runs garbage collection passes over one store and its blob store. Passes in one process run one at a time.

func New

func New(st Store, blobs blob.Store, o Options) (*Collector, error)

New returns a Collector over st and blobs.

func (*Collector) Loop

func (c *Collector) Loop(
	ctx context.Context,
	every time.Duration,
	onRun func(Report, error),
) error

Loop runs a pass every interval until ctx ends, then returns ctx's error. Each pass's report and error go to onRun when it is set; a failed pass does not stop the loop.

func (*Collector) Run

func (c *Collector) Run(ctx context.Context) (Report, error)

Run makes one pass. A step that fails does not stop the others; their errors are joined.

type Options

type Options struct {
	Grace        time.Duration
	UploadExpiry time.Duration
	// Reapers discard stale upload sessions of other kinds, keyed by the
	// session's Backend: a JFrog CLI multipart session holds staged parts
	// on local disk as well as a blob upload. Sessions of a backend with no
	// reaper are blob store uploads.
	Reapers map[string]func(context.Context, session.Upload) error
	// Expirers remove finished uploads nothing took in time: the registry's
	// <image>/_uploads files, which a manifest push links from (decision
	// 0031). Each removes what was last written before the cutoff it is
	// given, the upload expiry ago, and returns how many it removed.
	Expirers []func(ctx context.Context, before time.Time) (int, error)
	// Tokens removes, on each pass, the records of access tokens whose
	// refresh grace (accessapi.DefaultRefreshGrace) has run out, which
	// Report.TokensExpired counts (decision 0005). Nil skips that step, for a
	// collector built without a token service, as some tests build one.
	Tokens accessapi.Tokens
	// Now supplies the time; nil means time.Now.
	Now func() time.Time
}

Options configures New. Zero values mean the defaults.

type Report

type Report struct {
	// Skipped is set when another instance held the lease; nothing was done.
	Skipped        bool
	TrashPurged    int
	UploadsReaped  int
	BlobsDeleted   int
	OrphansDeleted int
	BytesFreed     int64
	// Kept counts blobs that were referenced again while being collected,
	// and so were left alone.
	Kept int
	// Pruned counts tombstones dropped and revived rows forgotten after
	// tombstoneRetention.
	Pruned int64
	// TokensExpired counts access-token records removed after their refresh
	// grace ran out.
	TokensExpired int64
}

Report is what one pass did.

type Store

type Store interface {
	trash.Store
	Uploads() session.UploadStore
	Leases() work.LeaseStore
}

Store is what a collection pass reads and writes: the trash can's store, whose expired entries a pass purges (trash.Store), with the upload sessions it expires and the lease table that keeps one pass running across the nodes. Each composite store in app is a Store.

Jump to

Keyboard shortcuts

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