Documentation
¶
Overview ¶
Package trash restores, cleans and purges the trash can (decision 0010). Deleting an item moves its record here (internal/artifacts/core/item), as does deleting a repository (Keep, internal/artifacts/core/repos).
Role ¶
artifacts module, core layer: restore, clean and purge of the trash can, and the tree the HTTP layer shows as Artifactory's auto-trashcan repository. Restored items are recreated through the item service. As artifacts core, it never imports the module's adapters or an ecosystem http package (decision 0044, dependency rule 8).
Inputs ¶
- Driving: the trash can API (POST /api/trash/empty, DELETE /api/trash/clean/{repoKey}/{path} and POST /api/trash/restore/{repoKey}/{path}) and the storage API's views of auto-trashcan paths; the repository delete (internal/artifacts/core/repos), which keeps a repository's content here (Keep); the garbage collector (internal/artifacts/core/gc), which expires entries (PurgeExpired); and the format contract suite (artifacts/core/formattest), which restores.
- Driven: the metadata store, through Store, for trash entries, items and their properties, blob references, the trash can settings and transactions (Settings takes the settings store alone); and the item service (internal/artifacts/core/item), which recreates restored items and names the sidecars a file's format keeps with it (format.SidecarKeeper).
- The trash can settings: whether it is on, and the retention period in days. Unset, it is on with Artifactory's 14 days.
- The caller's permit (Restore) and filter (Children, Below, Walk, Holds), which judge each item at its original path. Nil allows every item, for an administrator.
Outputs ¶
- Settings, with Artifactory's defaults filled in.
- Entries and Find: what was trashed at or below a path, and the newest entry at one path.
- Keep: an entry for every item of a repository's content, and how many references the entries hold per sha256, so that the repository's delete releases only the others.
- Restore: how many items it brought back, each path's newest trashed copy with the sidecars its delete removed; ErrNothingTrashed, ErrNotPermitted, or the item service's error on a conflict.
- Clean, Empty and PurgeExpired: how many entries each purged, with the blob references their files held released.
- Children and Below: the trash can's tree as Artifactory's auto-trashcan repository holds it (Child, Node), sorted by path. Walk yields Below's nodes a store page at a time, from the entries in path order (port.TrashStore.BelowByPath), for the storage views that write the tree as they read it (#1409); Holds says whether the caller sees anything below a folder, reading one entry when the first is visible.
- Repository ("auto-trashcan"), DefaultRetentionDays and DefaultBatch.
Invariants ¶
- A trashed file keeps its blob reference until a restore takes it or a purge releases it, so it can be restored until its retention period passes. Once a purge releases it, the next garbage collection removes the content when nothing else holds it.
- A restore asks the permit about every selected item before any batch moves, so one refusal restores nothing.
- Each batch (Service.Batch, DefaultBatch when zero) is one transaction, so emptying a large trash can never holds one long transaction. A conflict stops a restore with the earlier batches kept.
- A purge skips an entry that is already gone, whose reference whoever removed it released, so no reference is released twice.
- Nothing expires while the trash can is off, as 7.x's small garbage collection does not run then.
- Walk yields exactly Below's nodes in Below's order, holding one store page and the answers for the current path's prefixes: before an entry's node it asks whether each prefix the path continues with a byte before "/" is a folder, since such a folder sorts before the entry though what makes it one sorts after.
References ¶
- Decision 0010: docs/research/decisions/0010-retention-trash-and-gc.md, "Trash can" and "Implementation (P3)".
- Decision 0032: docs/research/decisions/0032-package-format-handlers.md (SidecarKeeper, whose sidecars a restore brings back with their file).
- Research 02: docs/research/02-artifactory-core-api-inventory.md, "Trash can and garbage collection".
- https://docs.jfrog.com/artifactory/docs/trash-can and https://docs.jfrog.com/artifactory/reference/restoreitemfromtrashcan.md
- Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md, "Dependency rules".
- Target layout: docs/research/architecture-options/5-modular-monolith.md, line 203 (core/trash), line 1037 (the mapping row) and rule 2 at lines 35 to 36.
- Plan: docs/implementation/plan.md#p3-repositories-generic-artifacts-cli-aql-shape (task 2 and tracking item 7), #p8-pypi-nuget-helm-terraform-locals and #p9d-modular-monolith-migration, epic ART (https://github.com/weaveplatform/artifactweave/issues/941).
- https://github.com/weaveplatform/artifactweave/issues/973 and https://github.com/weaveplatform/artifactweave/issues/1088
Status ¶
Implemented P3 and P8. P3 added restore, clean, empty and expiry (https://github.com/weaveplatform/artifactweave/issues/48), and its follow-ups added the permit on the repository a restore takes items from (https://github.com/weaveplatform/artifactweave/issues/380), the filter that shows anyone else only what they may see (https://github.com/weaveplatform/artifactweave/issues/439), the tree behind the storage API's views (Below and Node, https://github.com/weaveplatform/artifactweave/issues/455), no expiry while the trash can is off (https://github.com/weaveplatform/artifactweave/issues/490) and Keep, for a deleted repository's content (https://github.com/weaveplatform/artifactweave/issues/504). P8 restores the sidecars a format keeps with a file (https://github.com/weaveplatform/artifactweave/issues/533). Moved here in P9d, which narrowed the service's store to Store (https://github.com/weaveplatform/artifactweave/issues/1088).
Testing ¶
- TestUnitSettings: the defaults, a stored setting, a stored setting with no retention, and a malformed one.
- TestUnitRestoreToOriginal, TestUnitRestoreElsewhere, TestUnitRestoreNewestAndOverwrite and TestUnitRestoreIntoExistingFolder: where a restore puts items, and which copy it takes.
- TestUnitRestorePermit and TestUnitRestoreConflict: one refusal restores nothing, and a conflict keeps the earlier batches.
- TestUnitRestoreTakesSidecars, TestUnitRestoreLeavesOtherDeletesSidecars, TestUnitRestoreSidecarPermit and TestUnitRestoreSidecarFindFails: the sidecars a restore brings back, and those it leaves.
- TestUnitKeep and TestUnitKeepPages: a repository's whole content, a page at a time.
- TestUnitCleanEmptyPurge and TestUnitPurgeExpiredWhileOff: purges, and expiry with the trash can on and off.
- TestUnitChildrenAndFind, TestUnitBelow and TestUnitChildrenFiltered: the tree, and what a filter keeps of it.
- TestUnitStoreFailures and TestUnitDefaultClock: every store failure surfaces, and a service with no clock uses the current time.
Index ¶
- Constants
- Variables
- func Newest(entries []model.TrashEntry) []model.TrashEntry
- func Settings(ctx context.Context, st interface{ ... }) (model.TrashcanSettings, error)
- type Child
- type Destination
- type Node
- type Service
- func (s *Service) Below(ctx context.Context, dir string, deep bool, sees func(model.Item) bool) ([]Node, error)
- func (s *Service) Children(ctx context.Context, dir string, sees func(model.Item) bool) ([]Child, error)
- func (s *Service) Clean(ctx context.Context, repoKey, prefix string) (int, error)
- func (s *Service) Empty(ctx context.Context) (int, error)
- func (s *Service) Entries(ctx context.Context, repoKey, prefix string) ([]model.TrashEntry, error)
- func (s *Service) Find(ctx context.Context, repoKey, fullPath string) (model.TrashEntry, error)
- func (s *Service) Holds(ctx context.Context, dir string, sees func(model.Item) bool) (bool, error)
- func (s *Service) Keep(ctx context.Context, c repo.Config, by string) (map[string]int64, error)
- func (s *Service) PurgeExpired(ctx context.Context) (int, error)
- func (s *Service) Restore(ctx context.Context, repoKey, from string, dst Destination, by string, ...) (int, error)
- func (s *Service) Walk(dir string, deep bool, sees func(model.Item) bool) *Walk
- type Store
- type Walk
Constants ¶
const ( // Repository is the key Artifactory shows the trash can under. Repository = "auto-trashcan" // DefaultRetentionDays is Artifactory's default trash can retention. DefaultRetentionDays = 14 // DefaultBatch bounds the entries one transaction restores or purges, // so emptying a large trash can never holds one long transaction. DefaultBatch = 500 )
Variables ¶
var ErrNotPermitted = errors.New("trash: restoring an item is not permitted")
ErrNotPermitted is a restore that selected an item its permit refused.
var ErrNothingTrashed = errors.New("trash: nothing is trashed at the path")
ErrNothingTrashed is a restore from a path with no trashed items.
Functions ¶
func Newest ¶
func Newest(entries []model.TrashEntry) []model.TrashEntry
Newest keeps, for each original path, the most recently trashed entry, ordered so folders come before what they contain.
func Settings ¶
func Settings( ctx context.Context, st interface{ Settings() settings.SettingsStore }, ) (model.TrashcanSettings, error)
Settings returns the trash can settings. Unset means on with Artifactory's 14 days, and a stored retention below one day means the default too: the settings document may carry only "enabled".
Types ¶
type Destination ¶
Destination is where a restore puts items: a repository and a path. With no path the items return to their original paths inside Repo; with one, the restored path lands there and what was below it below that.
type Node ¶
type Node struct {
Path string
Folder bool
// Entry is the newest entry trashed at Path. It is nil for a folder that
// only holds what was trashed below it, such as a repository's folder at
// the root, or the parent of a file deleted on its own.
Entry *model.TrashEntry
}
Node is one item of the trash can's tree, as Artifactory's auto-trashcan repository holds it: Path is the original repository key and the item's original path, joined.
type Service ¶
type Service struct {
Store Store
// Items recreates restored items; only Restore needs it.
Items *item.Service
// Now supplies the time; nil means time.Now.
Now func() time.Time
// Batch bounds the entries per transaction; zero means DefaultBatch.
Batch int
// contains filtered or unexported fields
}
Service works on the trash can of one store.
func (*Service) Below ¶
func (s *Service) Below( ctx context.Context, dir string, deep bool, sees func(model.Item) bool, ) ([]Node, error)
Below lists the trash can's tree under dir ("" for its root): the direct children, or with deep every descendant, sorted by path. It collects Walk, which a caller that writes the tree as it reads it uses instead.
sees filters the entries before the tree is derived from them, so a repository or folder shows only when an entry the caller sees lies at or below it. A nil sees shows every entry, for an administrator. At each path the node carries the newest entry the caller sees there, and a file trashed where a folder later held trashed items gives way to the folder: Artifactory's trash can replaces it with the folder (TrashServiceImpl.undeployFileWithSameNameInTrash in 6.20).
func (*Service) Children ¶
func (s *Service) Children( ctx context.Context, dir string, sees func(model.Item) bool, ) ([]Child, error)
Children lists what the trash can shows directly inside dir: the repositories with trashed items at its root ("" for dir), and below a repository the next path segment of every trashed path, as Artifactory's auto-trashcan repository does. sees filters as for Below.
func (*Service) Clean ¶
Clean purges every entry trashed from repoKey at or below prefix and returns how many it purged.
func (*Service) Entries ¶
Entries returns every entry trashed from repoKey at or below prefix ("" for the whole repository; a repoKey of "" for the whole trash can).
func (*Service) Holds ¶
Holds reports whether the caller sees anything trashed below dir, which is whether Below(dir, ...) lists anything. It reads one entry when the first is visible, as it always is for a nil sees.
func (*Service) Keep ¶
Keep records every item of c's own content as trashed by by, as a delete of the repository's root through the item service records them, so a deleted repository can be restored. It changes nothing else: call it inside the transaction that then deletes the items, which hands each file's blob reference to its entry instead of releasing it. It returns how many references the entries hold per sha256, for that delete to release only the ones no entry took over.
It pages through the items rather than listing them at once, so a large repository costs one page of memory, not all of it.
func (*Service) PurgeExpired ¶
PurgeExpired purges the entries older than the retention period and returns how many it purged. It purges nothing while the trash can is off, as 7.x's small garbage collection, which expires the trash can, does not run then (KB "The complete Garbage Collection Guide"; 6.20's TrashServiceImpl.getGCCandidatesFromTrash). What was trashed before the trash can was turned off stays until it is turned on again and the entry expires, or until clean or empty removes it.
func (*Service) Restore ¶
func (s *Service) Restore( ctx context.Context, repoKey, from string, dst Destination, by string, permit func(model.Item) bool, ) (int, error)
Restore moves the newest trashed copy of everything at or below repoKey/from back into dst, each file with the sidecars its delete removed (format.SidecarKeeper), and returns how many items it restored. Each batch is one transaction that recreates the items and drops their entries; a conflict stops the restore with the earlier batches kept.
permit is asked about every selected item, at its original path, before any batch moves, so one refusal restores nothing and answers ErrNotPermitted. Asking per batch instead would leave the batches before the refused item restored. A nil permit allows every item, for a caller such as an administrator that may restore anything.
type Store ¶
type Store interface {
tx.Runner
Trash() port.TrashStore
Items() port.ItemStore
Settings() settings.SettingsStore
Blobs() port.BlobIndex
}
Store is what the trash can reads and writes: its entries, the items they were taken from, the trash can settings and the blob references an entry holds. Every call joins the transaction ctx carries; the service runs its own transactions through Tx, a batch of entries at a time. Each composite store in app is a Store, and so is the item service's store.
type Walk ¶
type Walk struct {
// contains filtered or unexported fields
}
Walk is Below read a store page at a time (decision 0043, "Streaming complete lists"): the same nodes in the same order, from the entries in path order (port.TrashStore.BelowByPath), never all of them at once. Start one with Service.Walk and read it with Page; it is not safe for concurrent use.
Below sorts the nodes by path, byte by byte, and the entries arrive in that order, but a folder implied by what was trashed below it does not always sort next to that: "-", "." and every other byte before "/" put a/b-1.jar between a/b and a/b/c.jar. So before it yields an entry's node the walk asks whether each prefix the entry's path continues with such a byte is a folder, by looking for a visible entry below it, first in the page it has read ahead and else in the store. The answers for the current path's prefixes are kept, since the next paths mostly share them.